main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "Includes.h"
  4. #include "profile.h"
  5. #include "ble.h"
  6. #include "platform.h"
  7. hci_con_handle_t connection_handle;
  8. void sm_peripheral_setup (void){
  9. }
  10. void _HWradio_Go_To_Idle_State_Patch (void){
  11. }
  12. static uint32_t min(uint32_t a, uint32_t b){
  13. return a < b ? a : b;
  14. }
  15. static int g_conn_stat = 0;
  16. int app_get_connect_state(void)
  17. {
  18. return g_conn_stat;
  19. }
  20. // att_read_callback helpers
  21. static uint16_t att_read_callback_handle_blob(const uint8_t * blob, uint16_t blob_size, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
  22. if (buffer){
  23. uint16_t bytes_to_copy = min(blob_size - offset, buffer_size);
  24. memcpy(buffer, &blob[offset], bytes_to_copy);
  25. return bytes_to_copy;
  26. } else {
  27. return blob_size;
  28. }
  29. }
  30. // read requests
  31. 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){
  32. printf("%s, con_handle=%x, att_handle=%x, offset=%x, buffer=%x, size=%x\n",__func__, con_handle, att_handle, offset,(uint32_t)buffer, buffer_size);
  33. if((att_handle != ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE) &&
  34. (att_handle != ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE)) return 0;
  35. static uint8_t read_test[10] = "012345678";
  36. return att_read_callback_handle_blob((const uint8_t *)read_test, sizeof(read_test), offset, buffer, buffer_size);
  37. }
  38. // write requests
  39. 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){
  40. uint32_t le_notification_enabled;
  41. printf("%s, con_handle=%x, att_handle=%x, offset=%x, buffer=%x, size=%x\n", __func__, con_handle, att_handle, offset, (uint32_t)buffer, buffer_size);
  42. if (transaction_mode != ATT_TRANSACTION_MODE_NONE) return 0;
  43. switch(att_handle)
  44. {
  45. case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
  46. le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
  47. printf("Notifications enabled %u\n",le_notification_enabled);
  48. break;
  49. case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
  50. case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
  51. printf("att_handle=0x%x, offset=0x%x, length=0x%x\n", att_handle, offset, buffer_size);
  52. printf("att data: ");
  53. for(uint32_t i=0; i<buffer_size; i++) printf("0x%x ", buffer[i]);
  54. printf("\n");
  55. break;
  56. default:
  57. break;
  58. }
  59. return 0;
  60. }
  61. static int con_count;
  62. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  63. if (packet_type != HCI_EVENT_PACKET) return;
  64. switch(hci_event_packet_get_type(packet))
  65. {
  66. case BTSTACK_EVENT_STATE:
  67. if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
  68. printf("BTstack up and running.\n");
  69. break;
  70. case HCI_EVENT_LE_META:
  71. switch (hci_event_le_meta_get_subevent_code(packet))
  72. {
  73. case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: {
  74. connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
  75. printf("\n CONNECT RIGHT ! (HANDLE = 0x%x)\n", connection_handle);
  76. g_conn_stat = 1;
  77. printf("Connected, requesting conn param update for handle 0x%04x\n", connection_handle);
  78. con_count = 1;
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. break;
  85. case HCI_EVENT_DISCONNECTION_COMPLETE:
  86. printf("\n DISCONNECT ! (HANDLE = 0x%x) ", hci_event_disconnection_complete_get_connection_handle(packet));
  87. printf("REASON = 0x%x\n", hci_event_disconnection_complete_get_reason(packet));
  88. g_conn_stat = 0;
  89. break;
  90. case L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE:
  91. printf("L2CAP Connection Parameter Update Complete, response: %x\n", l2cap_event_connection_parameter_update_response_get_result(packet));
  92. break;
  93. case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
  94. printf("ATT MTU = %u\n", att_event_mtu_exchange_complete_get_MTU(packet));
  95. break;
  96. case ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE:
  97. (att_event_handle_value_indication_complete_get_status(packet) == 0) ? printf("INDICATION OK\n") : printf("INDICATION FAIL\n");
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. static const blestack_init_t blestack_init = {
  104. /* uint16_t (*att_read_callback)(...);*/ att_read_callback,
  105. /* int (*att_write_callback)(...); */ att_write_callback,
  106. /* void (*packet_handler)(...) */ packet_handler,
  107. /* const uint8_t *ble_device_db */ profile_data
  108. };
  109. const uint8_t adv_data[] = {
  110. // Flags general discoverable, BR/EDR not supported
  111. 0x02, 0x01, 0x06,
  112. // Name
  113. 0x0A, 0x09, 'X', 'i', 'c', '_', 'B', 'L', 'E', 'v', '1',
  114. };
  115. uint8_t adv_data_len = sizeof(adv_data);
  116. const uint8_t scanresp_data[] = {
  117. //- Manufacture.
  118. 0x0c, 0xff, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1',
  119. //0x03,0x03,0x0,0xFF,0x09,0x09,0x42,0x52,0x54,0x20,0x44,0x41,0x54,0x41,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
  120. };
  121. uint8_t scanresp_data_len = sizeof(scanresp_data);
  122. void ble_listen(void){
  123. xc_wdog_init(0x9);
  124. }
  125. //setup mac
  126. void set_bd_addr(void)
  127. {
  128. extern uint8_t bd_addr[6];
  129. bd_addr[0]= 0x45;
  130. bd_addr[1]= 0x1B;
  131. bd_addr[2]= 0x26;
  132. bd_addr[3]= 0xA3;
  133. bd_addr[4]= 0x84;
  134. bd_addr[5]= 0x1D;
  135. }
  136. enum adv_type{
  137. ADV_IND,
  138. ADV_DIRECT_IND,
  139. ADV_SCAN_IND,
  140. ADV_NONCONN_IND,
  141. };
  142. int i =0;
  143. void stack_reset(void)
  144. {
  145. hci_power_control(1);
  146. GPIO_Sleep_Config();
  147. memset(null_addr, 0, 6);
  148. gap_advertisements_set_params(adv_int_min, adv_int_max, ADV_IND, 0, null_addr, 0x07, 0x00);
  149. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  150. gap_scan_response_set_data(scanresp_data_len , (uint8_t*) scanresp_data);
  151. gap_advertisements_enable(1);
  152. }
  153. int main(void)
  154. {
  155. set_bd_addr();
  156. // setup advertisements
  157. uint16_t adv_int_min = 0x00a0;
  158. uint16_t adv_int_max = 0x00a0;
  159. bd_addr_t null_addr;
  160. ble_init((void *)&blestack_init);
  161. GPIO_Sleep_Config();
  162. memset(null_addr, 0, 6);
  163. gap_advertisements_set_params(adv_int_min, adv_int_max, ADV_IND, 0, null_addr, 0x07, 0x00);
  164. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  165. gap_scan_response_set_data(scanresp_data_len , (uint8_t*) scanresp_data);
  166. gap_advertisements_enable(1);
  167. ble_system_idle_init();
  168. while(1) {
  169. ble_mainloop();
  170. ble_system_idle();
  171. if(con_count == 1){
  172. i++;
  173. if(i >= 5000){
  174. i=0;
  175. // gap_request_connection_parameter_update(connection_handle, 400-20, 400, 0, 600);
  176. gap_request_connection_parameter_update(connection_handle, 800-20, 800, 0, 600);
  177. con_count = 0;
  178. }
  179. }
  180. }
  181. }