rtt_btstack_gatt_blufi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-03-14 super_mcu first version
  9. */
  10. #define BTSTACK_FILE__ "gatt_blufi_config.c"
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "rtt_btstack_gatt_blufi.h"
  16. #include "btstack.h"
  17. #include "rtthread.h"
  18. #include "wifi.h"
  19. #define HEARTBEAT_PERIOD_MS 1000
  20. static int le_notification_enabled = 0;
  21. static btstack_timer_source_t heartbeat;
  22. static btstack_packet_callback_registration_t hci_event_callback_registration;
  23. static hci_con_handle_t con_handle;
  24. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  25. static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  26. static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  27. static void heartbeat_handler(struct btstack_timer_source *ts);
  28. //static void beat(void);
  29. const uint8_t adv_data[] = {
  30. // Flags general discoverable, BR/EDR not supported
  31. 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x05,
  32. // Name
  33. 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'A', 'R', 'T', '-', 'P', 'i', ' ', ' ', ' ', ' ',
  34. // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing!
  35. };
  36. const uint8_t adv_data_len = sizeof(adv_data);
  37. static void le_counter_setup(void)
  38. {
  39. l2cap_init();
  40. // setup le device db
  41. le_device_db_init();
  42. // setup SM: Display only
  43. sm_init();
  44. // setup ATT server
  45. att_server_init(profile_data, att_read_callback, att_write_callback);
  46. // setup advertisements
  47. uint16_t adv_int_min = 0x0030;
  48. uint16_t adv_int_max = 0x0030;
  49. uint8_t adv_type = 0;
  50. bd_addr_t null_addr;
  51. memset(null_addr, 0, 6);
  52. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  53. gap_advertisements_set_data(adv_data_len, (uint8_t *)adv_data);
  54. gap_advertisements_enable(1);
  55. // register for HCI events
  56. hci_event_callback_registration.callback = &packet_handler;
  57. hci_add_event_handler(&hci_event_callback_registration);
  58. // register for ATT event
  59. att_server_register_packet_handler(packet_handler);
  60. // set one-shot timer
  61. heartbeat.process = &heartbeat_handler;
  62. btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
  63. btstack_run_loop_add_timer(&heartbeat);
  64. // beat once
  65. // beat();
  66. }
  67. /* LISTING_END */
  68. /*
  69. * @section Heartbeat Handler
  70. *
  71. * @text The heartbeat handler updates the value of the single Characteristic provided in this example,
  72. * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat.
  73. */
  74. /* LISTING_START(heartbeat): Hearbeat Handler */
  75. //static int counter = 0;
  76. static char counter_string[1024];
  77. static int counter_string_len;
  78. static uint32_t send_buffer_index = 0;
  79. static uint8_t send_buffer_complete = 1;
  80. #define GATT_SEND_MAX_SIZE 20
  81. #define BLUFI_RECV_BUFF_SIZE 200
  82. struct basl_blufi_recv_data
  83. {
  84. uint8_t buf[BLUFI_RECV_BUFF_SIZE];
  85. uint16_t alloc_len;
  86. uint8_t recv_offset;
  87. };
  88. static struct basl_blufi_recv_data blufi_data;
  89. uint8_t bsal_blufi_push_data(struct basl_blufi_recv_data *blufi_data, uint8_t length, uint8_t *data)
  90. {
  91. if (blufi_data->recv_offset + length > blufi_data->alloc_len)
  92. {
  93. //error
  94. return 1;
  95. }
  96. if (data[0] == '{')
  97. {
  98. //first entry
  99. blufi_data->recv_offset = 0;
  100. memset(blufi_data->buf, 0, blufi_data->alloc_len);
  101. }
  102. log_debug("\r\n====length:%d \r\n", length);
  103. memcpy(&blufi_data->buf[blufi_data->recv_offset], data, length);
  104. blufi_data->recv_offset += length;
  105. //check the data
  106. if (data[length - 1] == '}')
  107. {
  108. return 0xff;
  109. }
  110. return 0;
  111. }
  112. int bt_stack_blufi_send(uint8_t *string, uint32_t length)
  113. {
  114. memcpy(counter_string, string, length);
  115. counter_string_len = length;
  116. if (le_notification_enabled)
  117. {
  118. log_debug("\r\n===start send string====\r\n");
  119. send_buffer_complete = 0;
  120. send_buffer_index = 0;
  121. }
  122. else
  123. {
  124. log_error("\r\n===le_notification_enabled:%d can't send====\r\n", le_notification_enabled);
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. void bt_send_api(void)
  130. {
  131. uint8_t wifi_status = 1;
  132. uint8_t ip_address[4] = {192, 168, 1, 1};
  133. char temp_string[100] = {0};
  134. uint32_t temp_length = 0;
  135. // counter_string_len = sprintf(counter_string, "{wifi:'%s'}", wifi_status?"on":"off");
  136. temp_length = sprintf(temp_string, "{wifi:'%s', url:' http://%d.%d.%d.%d/index.html'}", wifi_status ? "on" : "off", ip_address[0], ip_address[1], ip_address[2], ip_address[3]);
  137. log_debug("\r\n======counter_string_len:%d======\r\n", temp_length);
  138. bt_stack_blufi_send((uint8_t *)temp_string, temp_length);
  139. }
  140. MSH_CMD_EXPORT(bt_send_api, send data);
  141. static void heartbeat_handler(struct btstack_timer_source *ts)
  142. {
  143. if (send_buffer_complete == 1)
  144. {
  145. //tx complete no need
  146. //printf("\r\n === tx complete===\r\n ");
  147. btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
  148. btstack_run_loop_add_timer(ts);
  149. return;
  150. }
  151. if ((le_notification_enabled) && (counter_string_len != 0))
  152. {
  153. att_server_request_can_send_now_event(con_handle);
  154. }
  155. btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
  156. btstack_run_loop_add_timer(ts);
  157. }
  158. /* LISTING_END */
  159. /*
  160. * @section Packet Handler
  161. *
  162. * @text The packet handler is used to:
  163. * - stop the counter after a disconnect
  164. * - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received
  165. */
  166. /* LISTING_START(packetHandler): Packet Handler */
  167. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
  168. {
  169. UNUSED(channel);
  170. UNUSED(size);
  171. uint8_t send_length = 0;
  172. switch (packet_type)
  173. {
  174. case HCI_EVENT_PACKET:
  175. switch (hci_event_packet_get_type(packet))
  176. {
  177. case HCI_EVENT_DISCONNECTION_COMPLETE:
  178. le_notification_enabled = 0;
  179. break;
  180. case ATT_EVENT_CAN_SEND_NOW:
  181. if ((counter_string_len - send_buffer_index) > GATT_SEND_MAX_SIZE)
  182. {
  183. send_length = GATT_SEND_MAX_SIZE;
  184. }
  185. else
  186. {
  187. send_length = counter_string_len - send_buffer_index;
  188. }
  189. att_server_notify(con_handle, ATT_CHARACTERISTIC_FF01_01_VALUE_HANDLE, (uint8_t *)(counter_string + send_buffer_index), send_length);
  190. send_buffer_index += send_length;
  191. log_debug("\r\n====send length= %d ==left:%s===\r\n", send_length, (uint8_t *)(counter_string + send_buffer_index));
  192. if (send_buffer_index >= counter_string_len)
  193. {
  194. //send complete
  195. send_buffer_complete = 1;
  196. }
  197. break;
  198. }
  199. break;
  200. }
  201. }
  202. /* LISTING_END */
  203. /*
  204. * @section ATT Read
  205. *
  206. * @text The ATT Server handles all reads to constant data. For dynamic data like the custom characteristic, the registered
  207. * att_read_callback is called. To handle long characteristics and long reads, the att_read_callback is first called
  208. * with buffer == NULL, to request the total value length. Then it will be called again requesting a chunk of the value.
  209. * See Listing attRead.
  210. */
  211. /* LISTING_START(attRead): ATT Read */
  212. // ATT Client Read Callback for Dynamic Data
  213. // - if buffer == NULL, don't copy data, just return size of value
  214. // - if buffer != NULL, copy data and return number bytes copied
  215. // @param offset defines start of attribute value
  216. static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size)
  217. {
  218. // UNUSED(connection_handle);
  219. uint8_t string[] = {192, 168, 0, 1};
  220. log_debug("\r\n read the att %x, %x, offset:%x,buffer:%p, buffer_size:%x\r\n", connection_handle, att_handle, offset, buffer, buffer_size);
  221. if (att_handle == ATT_CHARACTERISTIC_FF01_01_VALUE_HANDLE)
  222. {
  223. return att_read_callback_handle_blob((const uint8_t *)string, 4, offset, buffer, buffer_size);
  224. }
  225. return 0;
  226. }
  227. /* LISTING_END */
  228. /*
  229. * @section ATT Write
  230. *
  231. * @text The only valid ATT write in this example is to the Client Characteristic Configuration, which configures notification
  232. * and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored and used
  233. * in the heartbeat handler to decide if a new value should be sent. See Listing attWrite.
  234. */
  235. /* LISTING_START(attWrite): ATT Write */
  236. static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size)
  237. {
  238. UNUSED(transaction_mode);
  239. UNUSED(offset);
  240. UNUSED(buffer_size);
  241. log_debug("\r\n write the att %x, %x, offset:%x,buffer:%p, buffer_size:%x transaction_mode %d \r\n", connection_handle, att_handle, offset, buffer, buffer_size, transaction_mode);
  242. if (att_handle == ATT_CHARACTERISTIC_FF01_01_CLIENT_CONFIGURATION_HANDLE)
  243. {
  244. le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
  245. log_debug("\r\n deal with the notify le_notification_enabled:%d %x, %x\r\n", le_notification_enabled, buffer[0], buffer[1]);
  246. }
  247. if (att_handle == ATT_CHARACTERISTIC_FF01_01_VALUE_HANDLE)
  248. {
  249. log_debug("\r\n recv data: %s length :%d\r\n", buffer, buffer_size);
  250. uint8_t ret = bsal_blufi_push_data(&blufi_data, buffer_size, buffer);
  251. if (ret == 0xff)
  252. {
  253. if (!wifi_is_ready())
  254. {
  255. wifi_connect((char *)blufi_data.buf);
  256. }
  257. else
  258. {
  259. int retry_cnt = 3;
  260. char *wifi_status = wifi_status_get();
  261. while (retry_cnt)
  262. {
  263. if (bt_stack_blufi_send((uint8_t *)wifi_status, strlen(wifi_status)) < 0)
  264. {
  265. retry_cnt--;
  266. }
  267. else
  268. {
  269. break;
  270. }
  271. }
  272. }
  273. //the data is ready
  274. log_debug("\r\n BLUFI: THE RECEIVE DATA IS :%s \r\n", blufi_data.buf);
  275. }
  276. }
  277. con_handle = connection_handle;
  278. return 0;
  279. }
  280. /* LISTING_END */
  281. int btstack_main(void);
  282. int btstack_main(void)
  283. {
  284. le_counter_setup();
  285. blufi_data.alloc_len = BLUFI_RECV_BUFF_SIZE;
  286. blufi_data.recv_offset = 0;
  287. // turn on!
  288. hci_power_control(HCI_POWER_ON);
  289. return 0;
  290. }
  291. /* EXAMPLE_END */