hci.c (5b9b590f58f3115ba11e98713a5d54c5cf833a9a) hci.c (f5a23e02c7923a319e59f5fc9df1fa1bf8f23af0)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 33 unchanged lines hidden (view full) ---

42 *
43 */
44
45#include "btstack-config.h"
46
47#include "hci.h"
48#include "gap.h"
49
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 33 unchanged lines hidden (view full) ---

42 *
43 */
44
45#include "btstack-config.h"
46
47#include "hci.h"
48#include "gap.h"
49
50#ifdef HAVE_TICK
51#include "run_loop_embedded.h"
52#endif
53
54#ifdef HAVE_BLE
50#ifdef HAVE_BLE
55#include "gap.h"
51#include "gap_le.h"
56#endif
57
58#include <stdarg.h>
59#include <string.h>
60#include <stdio.h>
61#include <inttypes.h>
62
63#ifndef EMBEDDED
64#ifdef _WIN32
65#include "Winsock2.h"
66#else
67#include <unistd.h> // gethostbyname
68#endif
52#endif
53
54#include <stdarg.h>
55#include <string.h>
56#include <stdio.h>
57#include <inttypes.h>
58
59#ifndef EMBEDDED
60#ifdef _WIN32
61#include "Winsock2.h"
62#else
63#include <unistd.h> // gethostbyname
64#endif
69#include "version.h"
65#include <btstack/version.h>
70#endif
71
72#include "btstack_memory.h"
73#include "debug.h"
74#include "hci_dump.h"
75
66#endif
67
68#include "btstack_memory.h"
69#include "debug.h"
70#include "hci_dump.h"
71
76#include "bk_linked_list.h"
77#include "hci_cmds.h"
72#include <btstack/linked_list.h>
73#include <btstack/hci_cmds.h>
78
79#define HCI_CONNECTION_TIMEOUT_MS 10000
80
81#ifdef USE_BLUETOOL
74
75#define HCI_CONNECTION_TIMEOUT_MS 10000
76
77#ifdef USE_BLUETOOL
82#include "../port/ios/src/bt_control_iphone.h"
78#include "../platforms/ios/src/bt_control_iphone.h"
83#endif
84
85static void hci_update_scan_enable(void);
86static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
87static void hci_connection_timeout_handler(timer_source_t *timer);
88static void hci_connection_timestamp(hci_connection_t *connection);
89static int hci_power_control_on(void);
90static void hci_power_control_off(void);

--- 111 unchanged lines hidden (view full) ---

202 struct timeval tv;
203 gettimeofday(&tv, NULL);
204 if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
205 // connections might be timed out
206 hci_emit_l2cap_check_timeout(connection);
207 }
208#endif
209#ifdef HAVE_TICK
79#endif
80
81static void hci_update_scan_enable(void);
82static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
83static void hci_connection_timeout_handler(timer_source_t *timer);
84static void hci_connection_timestamp(hci_connection_t *connection);
85static int hci_power_control_on(void);
86static void hci_power_control_off(void);

--- 111 unchanged lines hidden (view full) ---

198 struct timeval tv;
199 gettimeofday(&tv, NULL);
200 if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
201 // connections might be timed out
202 hci_emit_l2cap_check_timeout(connection);
203 }
204#endif
205#ifdef HAVE_TICK
210 if (run_loop_embedded_get_ticks() > connection->timestamp + run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
206 if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
211 // connections might be timed out
212 hci_emit_l2cap_check_timeout(connection);
213 }
214#endif
207 // connections might be timed out
208 hci_emit_l2cap_check_timeout(connection);
209 }
210#endif
215#ifdef HAVE_TIME_MS
216 if (run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){
217 // connections might be timed out
218 hci_emit_l2cap_check_timeout(connection);
219 }
220#endif
221 run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
222 run_loop_add_timer(timer);
223}
224
225static void hci_connection_timestamp(hci_connection_t *connection){
226#ifdef HAVE_TIME
227 gettimeofday(&connection->timestamp, NULL);
228#endif
229#ifdef HAVE_TICK
211 run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
212 run_loop_add_timer(timer);
213}
214
215static void hci_connection_timestamp(hci_connection_t *connection){
216#ifdef HAVE_TIME
217 gettimeofday(&connection->timestamp, NULL);
218#endif
219#ifdef HAVE_TICK
230 connection->timestamp = run_loop_embedded_get_ticks();
220 connection->timestamp = embedded_get_ticks();
231#endif
221#endif
232#ifdef HAVE_TIME_MS
233 connection->timestamp = run_loop_get_time_ms();
234#endif
235}
236
237
238inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
239 conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
240}
241
242inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){

--- 128 unchanged lines hidden (view full) ---

371 for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
372 hci_connection_t * connection = (hci_connection_t *) it;
373 num_sco_packets_sent += connection->num_sco_packets_sent;
374 }
375 if (num_sco_packets_sent > hci_stack->sco_packets_total_num){
376 log_info("hci_number_free_sco_slots_for_handle: outgoing packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num);
377 return 0;
378 }
222}
223
224
225inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
226 conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
227}
228
229inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){

--- 128 unchanged lines hidden (view full) ---

358 for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
359 hci_connection_t * connection = (hci_connection_t *) it;
360 num_sco_packets_sent += connection->num_sco_packets_sent;
361 }
362 if (num_sco_packets_sent > hci_stack->sco_packets_total_num){
363 log_info("hci_number_free_sco_slots_for_handle: outgoing packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num);
364 return 0;
365 }
379 // log_info("hci_number_free_sco_slots_for_handle %x: sent %u", handle, num_sco_packets_sent);
380 return hci_stack->sco_packets_total_num - num_sco_packets_sent;
381}
382
383// new functions replacing hci_can_send_packet_now[_using_packet_buffer]
384int hci_can_send_command_packet_now(void){
385 if (hci_stack->hci_packet_buffer_reserved) return 0;
386
387 // check for async hci transport implementations

--- 452 unchanged lines hidden (view full) ---

840 pos += data_length;
841 offset += data_length + 1; // rssi
842 hci_dump_packet( HCI_EVENT_PACKET, 0, event, pos);
843 hci_stack->packet_handler(HCI_EVENT_PACKET, event, pos);
844 }
845}
846#endif
847
366 return hci_stack->sco_packets_total_num - num_sco_packets_sent;
367}
368
369// new functions replacing hci_can_send_packet_now[_using_packet_buffer]
370int hci_can_send_command_packet_now(void){
371 if (hci_stack->hci_packet_buffer_reserved) return 0;
372
373 // check for async hci transport implementations

--- 452 unchanged lines hidden (view full) ---

826 pos += data_length;
827 offset += data_length + 1; // rssi
828 hci_dump_packet( HCI_EVENT_PACKET, 0, event, pos);
829 hci_stack->packet_handler(HCI_EVENT_PACKET, event, pos);
830 }
831}
832#endif
833
848static uint32_t hci_transport_uart_get_main_baud_rate(void){
849 if (!hci_stack->config) return 0;
850 uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
851 // Limit baud rate for Broadcom chipsets to 3 mbps
852 if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){
853 baud_rate = 3000000;
854 }
855 return baud_rate;
856}
857
858static void hci_initialization_timeout_handler(timer_source_t * ds){
859 switch (hci_stack->substate){
860 case HCI_INIT_W4_SEND_RESET:
861 log_info("Resend HCI Reset");
862 hci_stack->substate = HCI_INIT_SEND_RESET;
863 hci_stack->num_cmd_packets = 1;
864 hci_run();
865 break;
866 case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
867 log_info("Resend HCI Reset - CSR Warm Boot");
868 hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
869 hci_stack->num_cmd_packets = 1;
870 hci_run();
871 break;
834static void hci_initialization_timeout_handler(timer_source_t * ds){
835 switch (hci_stack->substate){
836 case HCI_INIT_W4_SEND_RESET:
837 log_info("Resend HCI Reset");
838 hci_stack->substate = HCI_INIT_SEND_RESET;
839 hci_stack->num_cmd_packets = 1;
840 hci_run();
841 break;
842 case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
843 log_info("Resend HCI Reset - CSR Warm Boot");
844 hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT;
845 hci_stack->num_cmd_packets = 1;
846 hci_run();
847 break;
872 case HCI_INIT_W4_SEND_BAUD_CHANGE: {
873 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
874 log_info("Local baud rate change to %"PRIu32, baud_rate);
875 hci_stack->hci_transport->set_baudrate(baud_rate);
876 break;
877 }
848 case HCI_INIT_W4_SEND_BAUD_CHANGE:
849 log_info("Local baud rate change to %"PRIu32, ((hci_uart_config_t *)hci_stack->config)->baudrate_main);
850 hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
851 break;
878 default:
879 break;
880 }
881}
882
883static void hci_initializing_next_state(void){
884 hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1);
885}

--- 29 unchanged lines hidden (view full) ---

915 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
916 hci_send_cmd(&hci_reset);
917 break;
918 case HCI_INIT_SEND_RESET_ST_WARM_BOOT:
919 hci_state_reset();
920 hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT;
921 hci_send_cmd(&hci_reset);
922 break;
852 default:
853 break;
854 }
855}
856
857static void hci_initializing_next_state(void){
858 hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1);
859}

--- 29 unchanged lines hidden (view full) ---

889 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
890 hci_send_cmd(&hci_reset);
891 break;
892 case HCI_INIT_SEND_RESET_ST_WARM_BOOT:
893 hci_state_reset();
894 hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT;
895 hci_send_cmd(&hci_reset);
896 break;
923 case HCI_INIT_SEND_BAUD_CHANGE: {
924 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
925 hci_stack->control->baudrate_cmd(hci_stack->config, baud_rate, hci_stack->hci_packet_buffer);
897 case HCI_INIT_SET_BD_ADDR:
898 log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr));
899 hci_stack->control->set_bd_addr_cmd(hci_stack->config, hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer);
926 hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
900 hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
901 hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR;
902 hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
903 break;
904 case HCI_INIT_SEND_BAUD_CHANGE:
905 hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
906 hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
927 hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
928 hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
929 // STLC25000D: baudrate change happens within 0.5 s after command was send,
930 // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial)
907 hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE;
908 hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
909 // STLC25000D: baudrate change happens within 0.5 s after command was send,
910 // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial)
931 if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){
911 if (hci_stack->manufacturer == 0x0030){
932 run_loop_set_timer(&hci_stack->timeout, 100);
933 run_loop_add_timer(&hci_stack->timeout);
934 }
935 break;
912 run_loop_set_timer(&hci_stack->timeout, 100);
913 run_loop_add_timer(&hci_stack->timeout);
914 }
915 break;
936 }
937 case HCI_INIT_SEND_BAUD_CHANGE_BCM: {
938 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
939 hci_stack->control->baudrate_cmd(hci_stack->config, baud_rate, hci_stack->hci_packet_buffer);
940 hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
941 hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM;
942 hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
943 break;
944 }
945 case HCI_INIT_CUSTOM_INIT:
946 log_info("Custom init");
947 // Custom initialization
948 if (hci_stack->control && hci_stack->control->next_cmd){
949 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
950 if (valid_cmd){
951 int size = 3 + hci_stack->hci_packet_buffer[2];
952 hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);

--- 9 unchanged lines hidden (view full) ---

962 run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
963 run_loop_add_timer(&hci_stack->timeout);
964 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
965 break;
966 }
967 hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
968 break;
969 }
916 case HCI_INIT_CUSTOM_INIT:
917 log_info("Custom init");
918 // Custom initialization
919 if (hci_stack->control && hci_stack->control->next_cmd){
920 int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
921 if (valid_cmd){
922 int size = 3 + hci_stack->hci_packet_buffer[2];
923 hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);

--- 9 unchanged lines hidden (view full) ---

933 run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler);
934 run_loop_add_timer(&hci_stack->timeout);
935 hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT;
936 break;
937 }
938 hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
939 break;
940 }
970 log_info("hci_run: init script done");
971
972 // Init script download causes baud rate to reset on Broadcom chipsets, restore UART baud rate if needed
973 if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){
974 int need_baud_change = hci_stack->config
975 && hci_stack->control
976 && hci_stack->control->baudrate_cmd
977 && hci_stack->hci_transport->set_baudrate
978 && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
979 if (need_baud_change) {
980 uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init;
981 log_info("Local baud rate change to %"PRIu32" after init script", baud_rate);
982 hci_stack->hci_transport->set_baudrate(baud_rate);
983 }
984 }
941 log_info("hci_run: init script done");
985 }
986 // otherwise continue
942 }
943 // otherwise continue
987 hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
988 hci_send_cmd(&hci_read_local_supported_commands);
989 break;
990 case HCI_INIT_SET_BD_ADDR:
991 log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr));
992 hci_stack->control->set_bd_addr_cmd(hci_stack->config, hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer);
993 hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0);
994 hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR;
995 hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
996 break;
997 case HCI_INIT_READ_BD_ADDR:
998 hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR;
999 hci_send_cmd(&hci_read_bd_addr);
1000 break;
944 hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR;
945 hci_send_cmd(&hci_read_bd_addr);
946 break;
947 case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS:
948 hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS;
949 hci_send_cmd(&hci_read_local_supported_commands);
950 break;
1001 case HCI_INIT_READ_BUFFER_SIZE:
1002 hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE;
1003 hci_send_cmd(&hci_read_buffer_size);
1004 break;
951 case HCI_INIT_READ_BUFFER_SIZE:
952 hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE;
953 hci_send_cmd(&hci_read_buffer_size);
954 break;
1005 case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES:
1006 hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES;
955 case HCI_INIT_READ_LOCAL_SUPPORTED_FEATUES:
956 hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATUES;
1007 hci_send_cmd(&hci_read_local_supported_features);
1008 break;
1009 case HCI_INIT_SET_EVENT_MASK:
1010 hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK;
1011 if (hci_le_supported()){
1012 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1013 } else {
1014 // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...

--- 133 unchanged lines hidden (view full) ---

1148
1149
1150 if (!command_completed) return;
1151
1152 int need_baud_change = hci_stack->config
1153 && hci_stack->control
1154 && hci_stack->control->baudrate_cmd
1155 && hci_stack->hci_transport->set_baudrate
957 hci_send_cmd(&hci_read_local_supported_features);
958 break;
959 case HCI_INIT_SET_EVENT_MASK:
960 hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK;
961 if (hci_le_supported()){
962 hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
963 } else {
964 // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...

--- 133 unchanged lines hidden (view full) ---

1098
1099
1100 if (!command_completed) return;
1101
1102 int need_baud_change = hci_stack->config
1103 && hci_stack->control
1104 && hci_stack->control->baudrate_cmd
1105 && hci_stack->hci_transport->set_baudrate
1156 && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main;
1106 && ((hci_uart_config_t *)hci_stack->config)->baudrate_main;
1157
1158 int need_addr_change = hci_stack->custom_bd_addr_set
1159 && hci_stack->control
1160 && hci_stack->control->set_bd_addr_cmd;
1161
1162 switch(hci_stack->substate){
1163 case HCI_INIT_W4_SEND_RESET:
1164 run_loop_remove_timer(&hci_stack->timeout);
1165 break;
1166 case HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION:
1107
1108 int need_addr_change = hci_stack->custom_bd_addr_set
1109 && hci_stack->control
1110 && hci_stack->control->set_bd_addr_cmd;
1111
1112 switch(hci_stack->substate){
1113 case HCI_INIT_W4_SEND_RESET:
1114 run_loop_remove_timer(&hci_stack->timeout);
1115 break;
1116 case HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION:
1117 if (need_addr_change){
1118 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
1119 return;
1120 }
1121 // skipping addr change
1167 if (need_baud_change){
1168 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
1169 return;
1170 }
1122 if (need_baud_change){
1123 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
1124 return;
1125 }
1171 // skip baud change
1126 // also skip baud change
1172 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1173 return;
1127 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1128 return;
1129 case HCI_INIT_W4_SET_BD_ADDR:
1130 // for STLC2500D, bd addr change only gets active after sending reset command
1131 if (hci_stack->manufacturer == 0x0030){
1132 hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT;
1133 return;
1134 }
1135 // skipping warm boot on STLC2500D
1136 if (need_baud_change){
1137 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
1138 return;
1139 }
1140 // skipping baud change
1141 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1142 return;
1143 case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT:
1144 if (need_baud_change){
1145 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE;
1146 return;
1147 }
1148 // skipping baud change
1149 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1150 return;
1174 case HCI_INIT_W4_SEND_BAUD_CHANGE:
1175 // for STLC2500D, baud rate change already happened.
1151 case HCI_INIT_W4_SEND_BAUD_CHANGE:
1152 // for STLC2500D, baud rate change already happened.
1176 // for others, baud rate gets changed now
1177 if (hci_stack->manufacturer != COMPANY_ID_ST_MICROELECTRONICS){
1178 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
1179 log_info("Local baud rate change to %"PRIu32, baud_rate);
1180 hci_stack->hci_transport->set_baudrate(baud_rate);
1153 // for CC256x, baud rate gets changed now
1154 if (hci_stack->manufacturer != 0x0030){
1155 uint32_t new_baud = ((hci_uart_config_t *)hci_stack->config)->baudrate_main;
1156 log_info("Local baud rate change to %"PRIu32, new_baud);
1157 hci_stack->hci_transport->set_baudrate(new_baud);
1181 }
1182 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1183 return;
1184 case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
1185 run_loop_remove_timer(&hci_stack->timeout);
1186 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1187 return;
1188 case HCI_INIT_W4_CUSTOM_INIT:
1189 // repeat custom init
1190 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1191 return;
1192 case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS:
1158 }
1159 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1160 return;
1161 case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT:
1162 run_loop_remove_timer(&hci_stack->timeout);
1163 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1164 return;
1165 case HCI_INIT_W4_CUSTOM_INIT:
1166 // repeat custom init
1167 hci_stack->substate = HCI_INIT_CUSTOM_INIT;
1168 return;
1169 case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS:
1193 if (need_baud_change && hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){
1194 hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM;
1195 return;
1196 }
1197 if (need_addr_change){
1198 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
1199 return;
1200 }
1201 hci_stack->substate = HCI_INIT_READ_BD_ADDR;
1170 // skip read buffer size if not supported
1171 if (hci_stack->local_supported_commands[0] & 0x01) break;
1172 hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATUES;
1202 return;
1173 return;
1203 case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: {
1204 uint32_t baud_rate = hci_transport_uart_get_main_baud_rate();
1205 log_info("Local baud rate change to %"PRIu32" after init script", baud_rate);
1206 hci_stack->hci_transport->set_baudrate(baud_rate);
1207 if (need_addr_change){
1208 hci_stack->substate = HCI_INIT_SET_BD_ADDR;
1209 return;
1210 }
1211 hci_stack->substate = HCI_INIT_READ_BD_ADDR;
1212 return;
1213 }
1214 case HCI_INIT_W4_SET_BD_ADDR:
1215 // for STLC2500D, bd addr change only gets active after sending reset command
1216 if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){
1217 hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT;
1218 return;
1219 }
1220 // skipping st warm boot
1221 hci_stack->substate = HCI_INIT_READ_BD_ADDR;
1222 return;
1223 case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT:
1224 hci_stack->substate = HCI_INIT_READ_BD_ADDR;
1225 return;
1226 case HCI_INIT_W4_READ_BD_ADDR:
1227 // only read buffer size if supported
1228 if (hci_stack->local_supported_commands[0] & 0x01) {
1229 hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE;
1230 return;
1231 }
1232 // skipping read buffer size
1233 hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES;
1234 return;
1235 case HCI_INIT_W4_SET_EVENT_MASK:
1236 // skip Classic init commands for LE only chipsets
1237 if (!hci_classic_supported()){
1238 if (hci_le_supported()){
1239 hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command
1240 return;
1241 } else {
1242 log_error("Neither BR/EDR nor LE supported");

--- 121 unchanged lines hidden (view full) ---

1364 (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0X80) >> 7 | // Octet 14, bit 7
1365 (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5; // Octet 24, bit 6
1366 }
1367 if (COMMAND_COMPLETE_EVENT(packet, hci_write_synchronous_flow_control_enable)){
1368 if (packet[5] == 0){
1369 // @TODO cache value from hci_write_synchronous_flow_control_enable instead of assuming == 1
1370 hci_stack->synchronous_flow_control_enabled = 1;
1371 }
1174 case HCI_INIT_W4_SET_EVENT_MASK:
1175 // skip Classic init commands for LE only chipsets
1176 if (!hci_classic_supported()){
1177 if (hci_le_supported()){
1178 hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command
1179 return;
1180 } else {
1181 log_error("Neither BR/EDR nor LE supported");

--- 121 unchanged lines hidden (view full) ---

1303 (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0X80) >> 7 | // Octet 14, bit 7
1304 (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5; // Octet 24, bit 6
1305 }
1306 if (COMMAND_COMPLETE_EVENT(packet, hci_write_synchronous_flow_control_enable)){
1307 if (packet[5] == 0){
1308 // @TODO cache value from hci_write_synchronous_flow_control_enable instead of assuming == 1
1309 hci_stack->synchronous_flow_control_enabled = 1;
1310 }
1372 }
1311 }
1373 break;
1374
1375 case HCI_EVENT_COMMAND_STATUS:
1376 // get num cmd packets
1377 // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]);
1378 hci_stack->num_cmd_packets = packet[3];
1379 break;
1380

--- 1776 unchanged lines hidden (view full) ---

3157
3158 return 0;
3159}
3160
3161void gap_set_local_name(const char * local_name){
3162 hci_stack->local_name = local_name;
3163}
3164
1312 break;
1313
1314 case HCI_EVENT_COMMAND_STATUS:
1315 // get num cmd packets
1316 // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]);
1317 hci_stack->num_cmd_packets = packet[3];
1318 break;
1319

--- 1776 unchanged lines hidden (view full) ---

3096
3097 return 0;
3098}
3099
3100void gap_set_local_name(const char * local_name){
3101 hci_stack->local_name = local_name;
3102}
3103
3165uint8_t le_central_start_scan(void){
3166 if (hci_stack->le_scanning_state == LE_SCANNING) return 0;
3104le_command_status_t le_central_start_scan(void){
3105 if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK;
3167 hci_stack->le_scanning_state = LE_START_SCAN;
3168 hci_run();
3106 hci_stack->le_scanning_state = LE_START_SCAN;
3107 hci_run();
3169 return 0;
3108 return BLE_PERIPHERAL_OK;
3170}
3171
3109}
3110
3172uint8_t le_central_stop_scan(void){
3173 if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return 0;
3111le_command_status_t le_central_stop_scan(void){
3112 if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK;
3174 hci_stack->le_scanning_state = LE_STOP_SCAN;
3175 hci_run();
3113 hci_stack->le_scanning_state = LE_STOP_SCAN;
3114 hci_run();
3176 return 0;
3115 return BLE_PERIPHERAL_OK;
3177}
3178
3179void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
3180 hci_stack->le_scan_type = scan_type;
3181 hci_stack->le_scan_interval = scan_interval;
3182 hci_stack->le_scan_window = scan_window;
3183 hci_run();
3184}
3185
3116}
3117
3118void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
3119 hci_stack->le_scan_type = scan_type;
3120 hci_stack->le_scan_interval = scan_interval;
3121 hci_stack->le_scan_window = scan_window;
3122 hci_run();
3123}
3124
3186uint8_t le_central_connect(bd_addr_t addr, bd_addr_type_t addr_type){
3125le_command_status_t le_central_connect(bd_addr_t addr, bd_addr_type_t addr_type){
3187 hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
3188 if (!conn){
3189 log_info("le_central_connect: no connection exists yet, creating context");
3190 conn = create_connection_for_bd_addr_and_type(addr, addr_type);
3191 if (!conn){
3192 // notify client that alloc failed
3193 hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
3194 log_info("le_central_connect: failed to alloc hci_connection_t");
3126 hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
3127 if (!conn){
3128 log_info("le_central_connect: no connection exists yet, creating context");
3129 conn = create_connection_for_bd_addr_and_type(addr, addr_type);
3130 if (!conn){
3131 // notify client that alloc failed
3132 hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
3133 log_info("le_central_connect: failed to alloc hci_connection_t");
3195 return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller
3134 return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller
3196 }
3197 conn->state = SEND_CREATE_CONNECTION;
3198 log_info("le_central_connect: send create connection next");
3199 hci_run();
3135 }
3136 conn->state = SEND_CREATE_CONNECTION;
3137 log_info("le_central_connect: send create connection next");
3138 hci_run();
3200 return 0;
3139 return BLE_PERIPHERAL_OK;
3201 }
3202
3203 if (!hci_is_le_connection(conn) ||
3204 conn->state == SEND_CREATE_CONNECTION ||
3205 conn->state == SENT_CREATE_CONNECTION) {
3206 hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED);
3207 log_error("le_central_connect: classic connection or connect is already being created");
3140 }
3141
3142 if (!hci_is_le_connection(conn) ||
3143 conn->state == SEND_CREATE_CONNECTION ||
3144 conn->state == SENT_CREATE_CONNECTION) {
3145 hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED);
3146 log_error("le_central_connect: classic connection or connect is already being created");
3208 return GATT_CLIENT_IN_WRONG_STATE;
3147 return BLE_PERIPHERAL_IN_WRONG_STATE;
3209 }
3210
3211 log_info("le_central_connect: context exists with state %u", conn->state);
3212 hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0);
3213 hci_run();
3148 }
3149
3150 log_info("le_central_connect: context exists with state %u", conn->state);
3151 hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0);
3152 hci_run();
3214 return 0;
3153 return BLE_PERIPHERAL_OK;
3215}
3216
3217// @assumption: only a single outgoing LE Connection exists
3218static hci_connection_t * le_central_get_outgoing_connection(void){
3219 linked_item_t *it;
3220 for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
3221 hci_connection_t * conn = (hci_connection_t *) it;
3222 if (!hci_is_le_connection(conn)) continue;
3223 switch (conn->state){
3224 case SEND_CREATE_CONNECTION:
3225 case SENT_CREATE_CONNECTION:
3226 return conn;
3227 default:
3228 break;
3229 };
3230 }
3231 return NULL;
3232}
3233
3154}
3155
3156// @assumption: only a single outgoing LE Connection exists
3157static hci_connection_t * le_central_get_outgoing_connection(void){
3158 linked_item_t *it;
3159 for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
3160 hci_connection_t * conn = (hci_connection_t *) it;
3161 if (!hci_is_le_connection(conn)) continue;
3162 switch (conn->state){
3163 case SEND_CREATE_CONNECTION:
3164 case SENT_CREATE_CONNECTION:
3165 return conn;
3166 default:
3167 break;
3168 };
3169 }
3170 return NULL;
3171}
3172
3234uint8_t le_central_connect_cancel(void){
3173le_command_status_t le_central_connect_cancel(void){
3235 hci_connection_t * conn = le_central_get_outgoing_connection();
3174 hci_connection_t * conn = le_central_get_outgoing_connection();
3236 if (!conn) return 0;
3175 if (!conn) return BLE_PERIPHERAL_OK;
3237 switch (conn->state){
3238 case SEND_CREATE_CONNECTION:
3239 // skip sending create connection and emit event instead
3240 hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
3241 linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
3242 btstack_memory_hci_connection_free( conn );
3243 break;
3244 case SENT_CREATE_CONNECTION:
3245 // request to send cancel connection
3246 conn->state = SEND_CANCEL_CONNECTION;
3247 hci_run();
3248 break;
3249 default:
3250 break;
3251 }
3176 switch (conn->state){
3177 case SEND_CREATE_CONNECTION:
3178 // skip sending create connection and emit event instead
3179 hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER);
3180 linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
3181 btstack_memory_hci_connection_free( conn );
3182 break;
3183 case SENT_CREATE_CONNECTION:
3184 // request to send cancel connection
3185 conn->state = SEND_CANCEL_CONNECTION;
3186 hci_run();
3187 break;
3188 default:
3189 break;
3190 }
3252 return 0;
3191 return BLE_PERIPHERAL_OK;
3253}
3254
3255/**
3256 * @brief Updates the connection parameters for a given LE connection
3257 * @param handle
3258 * @param conn_interval_min (unit: 1.25ms)
3259 * @param conn_interval_max (unit: 1.25ms)
3260 * @param conn_latency

--- 97 unchanged lines hidden (view full) ---

3358 }
3359 if (!enabled && hci_stack->le_advertisements_active){
3360 hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE;
3361 }
3362 hci_run();
3363}
3364
3365
3192}
3193
3194/**
3195 * @brief Updates the connection parameters for a given LE connection
3196 * @param handle
3197 * @param conn_interval_min (unit: 1.25ms)
3198 * @param conn_interval_max (unit: 1.25ms)
3199 * @param conn_latency

--- 97 unchanged lines hidden (view full) ---

3297 }
3298 if (!enabled && hci_stack->le_advertisements_active){
3299 hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE;
3300 }
3301 hci_run();
3302}
3303
3304
3366uint8_t gap_disconnect(hci_con_handle_t handle){
3305le_command_status_t gap_disconnect(hci_con_handle_t handle){
3367 hci_connection_t * conn = hci_connection_for_handle(handle);
3368 if (!conn){
3369 hci_emit_disconnection_complete(handle, 0);
3306 hci_connection_t * conn = hci_connection_for_handle(handle);
3307 if (!conn){
3308 hci_emit_disconnection_complete(handle, 0);
3370 return 0;
3309 return BLE_PERIPHERAL_OK;
3371 }
3372 conn->state = SEND_DISCONNECT;
3373 hci_run();
3310 }
3311 conn->state = SEND_DISCONNECT;
3312 hci_run();
3374 return 0;
3313 return BLE_PERIPHERAL_OK;
3375}
3376
3377/**
3378 * @brief Get connection type
3379 * @param con_handle
3380 * @result connection_type
3381 */
3382gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){

--- 123 unchanged lines hidden ---
3314}
3315
3316/**
3317 * @brief Get connection type
3318 * @param con_handle
3319 * @result connection_type
3320 */
3321gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){

--- 123 unchanged lines hidden ---