ble_client.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "include.h"
  2. #include "ble_client.h"
  3. #include "ble_user_service.h"
  4. #include "ble_hid_service.h"
  5. #define MAX_SERVICE_NUM 1
  6. typedef enum {
  7. STA_IDLE,
  8. STA_W4_ENCRYPTED_CONNECTION,
  9. STA_W4_SERVICE_RESULT,
  10. STA_W4_DISCONNECT
  11. } client_state_t;
  12. typedef struct {
  13. gatt_client_service_t service[MAX_SERVICE_NUM];
  14. uint8_t service_cnt;
  15. uint8_t service_index;
  16. uint16_t conn_handle;
  17. } server_info_t;
  18. static server_info_t server_info;
  19. static att_client_handler_t client_handler;
  20. static client_state_t tc_state = STA_IDLE;
  21. u8 attribute_uuid128[16] = {0x79, 0x05, 0xf4, 0x31, 0xb5, 0xce, 0x4e, 0x99, 0xa4, 0x0f, 0x4b, 0x1e, 0x12, 0x2d, 0x00, 0xd0};
  22. void ble_client_event_callback(uint8_t event_type, uint8_t *packet, uint16_t size)
  23. {
  24. switch(event_type){
  25. case BLE_EVT_CONNECT: {
  26. uint8_t status = packet[10];
  27. if (status) {
  28. printf("%s, connection failed\n", __func__);
  29. return;
  30. }
  31. printf("BLE_EVT_CONNECT\n");
  32. memcpy(&server_info.conn_handle, &packet[7], 2);
  33. printf("server_info.conn_handle:%x\n",server_info.conn_handle);
  34. tc_state = STA_W4_ENCRYPTED_CONNECTION;
  35. } return;
  36. case BLE_EVT_DISCONNECT:
  37. printf("BLE_EVT_DISCONNECT\n");
  38. server_info.conn_handle = 0;
  39. return;
  40. }
  41. switch(tc_state){
  42. case STA_W4_ENCRYPTED_CONNECTION:
  43. switch (event_type) {
  44. case BLE_EVT_ENCRYPTION_CHANGE:
  45. printf("BLE_EVT_ENCRYPTION_CHANGE\n");
  46. tc_state = STA_W4_SERVICE_RESULT;
  47. server_info.service_cnt = 0;
  48. ble_client_discover_primary_services_by_uuid128(server_info.conn_handle, attribute_uuid128);
  49. break;
  50. default:
  51. break;
  52. }
  53. case STA_W4_SERVICE_RESULT:
  54. switch(event_type){
  55. case BLE_EVT_SERVICE_FIND_RESULT:
  56. printf("BLE_EVT_SERVICE_FIND_RESULT\n");
  57. if(server_info.service_cnt >= MAX_SERVICE_NUM){
  58. printf("error: server_info.service_cnt >= MAX_SERVICE_NUM\n");
  59. }else{
  60. printf("Target Primary Service is Discovery.\n");
  61. ble_service_query_result_get_service(packet, &server_info.service[server_info.service_cnt++]);
  62. }
  63. break;
  64. case BLE_EVT_PROFILE_FIND_DONE:
  65. server_info.service_index = 0;
  66. if(server_info.service_cnt == 0){
  67. server_info.service_cnt = 0;
  68. /*Android may not write cccd, assign it manully*/
  69. ble_hid_set_mouse_cccd(1);
  70. printf("Client Discovery service failed->No service\n");
  71. }
  72. tc_state = STA_IDLE;
  73. break;
  74. default:
  75. break;
  76. }
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. void ble_client_init(void)
  83. {
  84. printf("%s\n", __func__);
  85. client_handler.event_handler = ble_client_event_callback;
  86. client_handler.notify_callback = NULL;
  87. client_handler.read_callback = NULL;
  88. att_client_register_handler(&client_handler);
  89. }