Advertisement
Guest User

Untitled

a guest
May 21st, 2018
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.43 KB | None | 0 0
  1. #define BLE_TAG          "BLE"
  2. #define DEVICE_NAME      "BLE_TEST"
  3. #define REMOTE_SERVICE_UUID         0x00FF
  4. #define REMOTE_NOTIFY_CHAR_UUID     0xFF01
  5. #define REMOTE_BATTERY_SERVICE_UUID 0x180F
  6. #define PROFILE_NUM      1
  7. #define PROFILE_A_APP_ID 0
  8. #define INVALID_HANDLE   0
  9.  
  10. #define PROFILE_BATTERY_SERVICE 0 // 0x180F
  11. #define PROFILE_DEVICE_INFO     1 // 0x180A
  12. #define PROFILE_HRM             2 // 0x180D
  13.  
  14. static const char remote_device_name[] = "Nordic_HRM";
  15. static bool connect_ble    = false;
  16. static bool get_server = false;
  17. static esp_gattc_char_elem_t *char_elem_result   = NULL;
  18. static esp_gattc_descr_elem_t *descr_elem_result = NULL;
  19.  
  20. struct gattc_profile_inst {
  21.     esp_gattc_cb_t gattc_cb;
  22.     uint16_t gattc_if;
  23.     uint16_t app_id;
  24.     uint16_t conn_id;
  25.     uint16_t service_start_handle;
  26.     uint16_t service_end_handle;
  27.     uint16_t char_handle;
  28.     esp_bd_addr_t remote_bda;
  29. };
  30.  
  31.  
  32. /* Declare static functions */
  33. static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
  34. static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
  35. static void gattc_profile_battery_service_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
  36.  
  37.  
  38. static esp_bt_uuid_t remote_filter_service_uuid = {
  39.     .len = ESP_UUID_LEN_16,
  40.     .uuid = {.uuid16 = REMOTE_SERVICE_UUID,},
  41. };
  42.  
  43.  
  44. static esp_bt_uuid_t remote_filter_battery_service_uuid = {
  45.     .len = ESP_UUID_LEN_16,
  46.     .uuid = {.uuid16 = REMOTE_BATTERY_SERVICE_UUID,},
  47. };
  48.  
  49. static esp_bt_uuid_t remote_filter_char_uuid = {
  50.     .len = ESP_UUID_LEN_16,
  51.     .uuid = {.uuid16 = REMOTE_NOTIFY_CHAR_UUID,},
  52. };
  53.  
  54. static esp_bt_uuid_t remote_battery_filter_char_uuid = {
  55.     .len = ESP_UUID_LEN_16,
  56.     .uuid = {.uuid16 = ESP_GATT_UUID_BATTERY_LEVEL,},
  57. };
  58.  
  59. static esp_bt_uuid_t notify_descr_uuid = {
  60.     .len = ESP_UUID_LEN_16,
  61.     .uuid = {.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG,},
  62. };
  63.  
  64. static esp_ble_scan_params_t ble_scan_params = {
  65.     .scan_type              = BLE_SCAN_TYPE_ACTIVE,
  66.     .own_addr_type          = BLE_ADDR_TYPE_PUBLIC,
  67.     .scan_filter_policy     = BLE_SCAN_FILTER_ALLOW_ALL,
  68.     .scan_interval          = 0x50,
  69.     .scan_window            = 0x30
  70. };
  71.  
  72.  
  73. /* One gatt-based profile one app_id and one gattc_if, this array will store the gattc_if returned by ESP_GATTS_REG_EVT */
  74. static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = {
  75.     [PROFILE_BATTERY_SERVICE] = {
  76.         .gattc_cb = gattc_profile_battery_service_event_handler,
  77.         .gattc_if = ESP_GATT_IF_NONE,
  78.     },
  79.    
  80. //    [PROFILE_DEVICE_INFO] = {
  81. //        .gattc_cb = gattc_profile_device_info_event_handler,
  82. //        .gattc_if = ESP_GATT_IF_NONE,       /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
  83. //    },
  84.  
  85. //    [PROFILE_HRM] = {
  86. //        .gattc_cb = gattc_profile_hrm_event_handler,
  87. //        .gattc_if = ESP_GATT_IF_NONE,
  88. //    },
  89. };
  90.  
  91.  
  92. static void gattc_profile_battery_service_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
  93. {
  94.     esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;
  95.  
  96.     switch (event) {
  97.     case ESP_GATTC_REG_EVT:
  98.         ESP_LOGI(BLE_TAG, "(battery)REG_EVT");
  99.         esp_err_t scan_ret = esp_ble_gap_set_scan_params(&ble_scan_params);
  100.         if (scan_ret){
  101.             ESP_LOGE(BLE_TAG, "(battery)set scan params error, error code = %x", scan_ret);
  102.         }
  103.         break;
  104.     case ESP_GATTC_CONNECT_EVT:{
  105.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if);
  106.         gl_profile_tab[PROFILE_BATTERY_SERVICE].conn_id = p_data->connect.conn_id;
  107.         memcpy(gl_profile_tab[PROFILE_BATTERY_SERVICE].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t));
  108.         ESP_LOGI(BLE_TAG, "(battery)REMOTE BDA:");
  109.         esp_log_buffer_hex(BLE_TAG, gl_profile_tab[PROFILE_BATTERY_SERVICE].remote_bda, sizeof(esp_bd_addr_t));
  110.         esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->connect.conn_id);
  111.         if (mtu_ret){
  112.             ESP_LOGE(BLE_TAG, "(battery)config MTU error, error code = %x", mtu_ret);
  113.         }
  114.         break;
  115.     }
  116.     case ESP_GATTC_OPEN_EVT:
  117.         if (param->open.status != ESP_GATT_OK){
  118.             ESP_LOGE(BLE_TAG, "(battery)open failed, status %d", p_data->open.status);
  119.             break;
  120.         }
  121.         ESP_LOGI(BLE_TAG, "(battery)open success");
  122.         break;
  123.     case ESP_GATTC_CFG_MTU_EVT:
  124.         if (param->cfg_mtu.status != ESP_GATT_OK){
  125.             ESP_LOGE(BLE_TAG,"(battery)config mtu failed, error status = %x", param->cfg_mtu.status);
  126.         }
  127.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_CFG_MTU_EVT, Status %d, MTU %d, conn_id %d", param->cfg_mtu.status, param->cfg_mtu.mtu, param->cfg_mtu.conn_id);
  128.         esp_err_t ret_search_gatt;
  129.         ret_search_gatt = esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, &remote_filter_battery_service_uuid); // with filter
  130.         //ret_search_gatt = esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, NULL);
  131.         if(ret_search_gatt){
  132.             ESP_LOGI(BLE_TAG, "(battery)service not found, error code = %x", ret_search_gatt);
  133.         }else{
  134.             ESP_LOGI(BLE_TAG, "(battery)service found");
  135.         }
  136.         break;
  137.     case ESP_GATTC_SEARCH_RES_EVT: {
  138.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_SEARCH_RES_EVT");
  139.         esp_gatt_srvc_id_t *srvc_id =(esp_gatt_srvc_id_t *)&p_data->search_res.srvc_id;
  140.  
  141.         ESP_LOGI(BLE_TAG, "(battery)srvc_id_len:%d [%x]", srvc_id->id.uuid.len, srvc_id->id.uuid.len);
  142. //        for (uint8_t i = 0; i < 15; i++)
  143. //        {
  144. //            ESP_LOGI(BLE_TAG, "(battery)srvc_id:%d [%x]", i, srvc_id->id.uuid.uuid.uuid128[i]);
  145. //        }
  146.  
  147.         if (srvc_id->id.uuid.len == ESP_UUID_LEN_16 && srvc_id->id.uuid.uuid.uuid16 == REMOTE_BATTERY_SERVICE_UUID) {
  148.             ESP_LOGI(BLE_TAG, "(battery)service found");
  149.             get_server = true;
  150.             gl_profile_tab[PROFILE_BATTERY_SERVICE].service_start_handle = p_data->search_res.start_handle;
  151.             gl_profile_tab[PROFILE_BATTERY_SERVICE].service_end_handle = p_data->search_res.end_handle;
  152.             ESP_LOGI(BLE_TAG, "(battery)UUID16: %x", srvc_id->id.uuid.uuid.uuid16);
  153.         }
  154.         break;
  155.     }
  156.     case ESP_GATTC_SEARCH_CMPL_EVT:
  157.         if (p_data->search_cmpl.status != ESP_GATT_OK){
  158.             ESP_LOGE(BLE_TAG, "(battery)search service failed, error status = %x", p_data->search_cmpl.status);
  159.             break;
  160.         }
  161.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_SEARCH_CMPL_EVT");
  162.         if (get_server){
  163.             ESP_LOGI(BLE_TAG, "(battery)get_server");
  164.             uint16_t count = 0;
  165.             esp_gatt_status_t status = esp_ble_gattc_get_attr_count( gattc_if,
  166.                                                                      p_data->search_cmpl.conn_id,
  167.                                                                      ESP_GATT_DB_CHARACTERISTIC,
  168.                                                                      gl_profile_tab[PROFILE_BATTERY_SERVICE].service_start_handle,
  169.                                                                      gl_profile_tab[PROFILE_BATTERY_SERVICE].service_end_handle,
  170.                                                                      INVALID_HANDLE,
  171.                                                                      &count);
  172.             if (status != ESP_GATT_OK){
  173.                 ESP_LOGE(BLE_TAG, "(battery)esp_ble_gattc_get_attr_count error");
  174.             }
  175.  
  176.             if (count > 0){
  177.                 char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count);
  178.                 if (!char_elem_result){
  179.                     ESP_LOGE(BLE_TAG, "(battery)gattc no mem");
  180.                 }else{
  181.                     status = esp_ble_gattc_get_char_by_uuid( gattc_if,
  182.                                                              p_data->search_cmpl.conn_id,
  183.                                                              gl_profile_tab[PROFILE_BATTERY_SERVICE].service_start_handle,
  184.                                                              gl_profile_tab[PROFILE_BATTERY_SERVICE].service_end_handle,
  185.                                                              remote_battery_filter_char_uuid, //remote_filter_char_uuid,
  186.                                                              char_elem_result,
  187.                                                              &count);
  188.                     if (status != ESP_GATT_OK){
  189.                         ESP_LOGE(BLE_TAG, "(battery)esp_ble_gattc_get_char_by_uuid error");
  190.                     }
  191.  
  192.                     /*  Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */
  193.                     if (count > 0 && (char_elem_result[0].properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)){
  194.                         gl_profile_tab[PROFILE_BATTERY_SERVICE].char_handle = char_elem_result[0].char_handle;
  195.                         esp_err_t errorcode = esp_ble_gattc_register_for_notify (gattc_if, gl_profile_tab[PROFILE_BATTERY_SERVICE].remote_bda, char_elem_result[0].char_handle);
  196.  
  197.                         if(errorcode)
  198.                         {
  199.                             ESP_LOGI(BLE_TAG, "(battery)esp_ble_gattc_register_for_notify error, code: %x", errorcode);
  200.                         }else{
  201.                             ESP_LOGI(BLE_TAG, "(battery)esp_ble_gattc_register_for_notify success");
  202.                         }
  203.                     }
  204.                 }
  205.                 /* free char_elem_result */
  206.                 free(char_elem_result);
  207.             }else{
  208.                 ESP_LOGE(BLE_TAG, "(battery)no char found");
  209.             }
  210.         }else{
  211.             ESP_LOGI(BLE_TAG, "(battery)GATT server not found");
  212.         }
  213.          break;
  214.     case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
  215.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_REG_FOR_NOTIFY_EVT");
  216.         if (p_data->reg_for_notify.status != ESP_GATT_OK){
  217.             ESP_LOGE(BLE_TAG, "(battery)REG FOR NOTIFY failed: error status = %d", p_data->reg_for_notify.status);
  218.         }else{
  219.             uint16_t count = 0;
  220.             uint16_t notify_en = 1;
  221.             esp_gatt_status_t ret_status = esp_ble_gattc_get_attr_count( gattc_if,
  222.                                                                          gl_profile_tab[PROFILE_BATTERY_SERVICE].conn_id,
  223.                                                                          ESP_GATT_DB_DESCRIPTOR,
  224.                                                                          gl_profile_tab[PROFILE_BATTERY_SERVICE].service_start_handle,
  225.                                                                          gl_profile_tab[PROFILE_BATTERY_SERVICE].service_end_handle,
  226.                                                                          gl_profile_tab[PROFILE_BATTERY_SERVICE].char_handle,
  227.                                                                          &count);
  228.             if (ret_status != ESP_GATT_OK){
  229.                 ESP_LOGE(BLE_TAG, "(battery)esp_ble_gattc_get_attr_count error");
  230.             }
  231.             if (count > 0)
  232.             {
  233.                 descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count);
  234.                 if (!descr_elem_result){
  235.                     ESP_LOGE(BLE_TAG, "(battery)malloc error, gattc no mem");
  236.                 }else{
  237.                     ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if,
  238.                                                                          gl_profile_tab[PROFILE_BATTERY_SERVICE].conn_id,
  239.                                                                          p_data->reg_for_notify.handle,
  240.                                                                          notify_descr_uuid,
  241.                                                                          descr_elem_result,
  242.                                                                          &count);
  243.                     if (ret_status != ESP_GATT_OK){
  244.                         ESP_LOGE(BLE_TAG, "(battery)esp_ble_gattc_get_descr_by_char_handle error");
  245.                     }
  246.  
  247.                     /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */
  248.                     if (count > 0 && descr_elem_result[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){
  249. //                        ret_status = esp_ble_gattc_write_char_descr( gattc_if,
  250. //                                                                     gl_profile_tab[PROFILE_BATTERY_SERVICE].conn_id,
  251. //                                                                     descr_elem_result[0].handle,
  252. //                                                                     sizeof(notify_en),
  253. //                                                                     (uint8_t *)&notify_en,
  254. //                                                                     ESP_GATT_WRITE_TYPE_RSP,
  255. //                                                                     ESP_GATT_AUTH_REQ_NONE);
  256.                         ret_status = esp_ble_gattc_read_char_descr( gattc_if,
  257.                                                                     gl_profile_tab[PROFILE_BATTERY_SERVICE].conn_id,
  258.                                                                     descr_elem_result[0].handle,
  259.                                                                     ESP_GATT_AUTH_REQ_NONE);
  260.                     }
  261.  
  262.                     if (ret_status != ESP_GATT_OK){
  263.                         //ESP_LOGE(BLE_TAG, "(battery)esp_ble_gattc_write_char_descr error");
  264.                         ESP_LOGE(BLE_TAG, "(battery)esp_ble_gattc_read_char_descr error, code: %x", ret_status);
  265.                     }
  266.  
  267.                     /* free descr_elem_result */
  268.                     free(descr_elem_result);
  269.                 }
  270.             }
  271.             else{
  272.                 ESP_LOGE(BLE_TAG, "(battery)decsr not found");
  273.             }
  274.  
  275.         }
  276.         break;
  277.     }
  278.     case ESP_GATTC_NOTIFY_EVT:
  279.         if (p_data->notify.is_notify){
  280.             ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_NOTIFY_EVT, receive notify value:");
  281.         }else{
  282.             ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_NOTIFY_EVT, receive indicate value:");
  283.         }
  284.         esp_log_buffer_hex(BLE_TAG, p_data->notify.value, p_data->notify.value_len);
  285.         break;
  286.     case ESP_GATTC_READ_CHAR_EVT:
  287.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_READ_CHAR_EVT");
  288.  
  289.         if (p_data->read.status != ESP_GATT_OK){
  290.             ESP_LOGE(BLE_TAG, "(battery)read char failed, error status = %x", p_data->read.status);
  291.             break;
  292.         }
  293.         ESP_LOGI(BLE_TAG, "(battery)read char success ");
  294.  
  295.         break;
  296.     case ESP_GATTC_READ_DESCR_EVT:
  297.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_READ_DESCR_EVT");
  298.         if (p_data->read.status != ESP_GATT_OK){
  299.              ESP_LOGE(BLE_TAG, "(battery)read descr failed, error status = %x", p_data->write.status);
  300.              break;
  301.         }
  302.         ESP_LOGI(BLE_TAG, "(battery)read descr success ");
  303.  
  304.         esp_gatt_status_t ret_status = esp_ble_gattc_read_char( gattc_if,
  305.                                 gl_profile_tab[PROFILE_BATTERY_SERVICE].conn_id,
  306.                                 gl_profile_tab[PROFILE_BATTERY_SERVICE].char_handle,
  307.                                 ESP_GATT_AUTH_REQ_NONE);
  308.         if(ret_status){
  309.             ESP_LOGE(BLE_TAG, "(battery) esp_ble_gattc_read_char failed, error code = %x", ret_status);
  310.         }else{
  311.             ESP_LOGI(BLE_TAG, "(battery) esp_ble_gattc_read_char success ");
  312.         }
  313.         break;
  314.     case ESP_GATTC_WRITE_DESCR_EVT:
  315.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_WRITE_DESCR_EVT");
  316. //        if (p_data->write.status != ESP_GATT_OK){
  317. //            ESP_LOGE(BLE_TAG, "(battery)write descr failed, error status = %x", p_data->write.status);
  318. //            break;
  319. //        }
  320. //        ESP_LOGI(BLE_TAG, "(battery)write descr success ");
  321. //        uint8_t write_char_data[35];
  322. //        for (int i = 0; i < sizeof(write_char_data); ++i)
  323. //        {
  324. //            write_char_data[i] = i % 256;
  325. //        }
  326. //        esp_ble_gattc_write_char( gattc_if,
  327. //                                  gl_profile_tab[PROFILE_BATTERY_SERVICE].conn_id,
  328. //                                  gl_profile_tab[PROFILE_BATTERY_SERVICE].char_handle,
  329. //                                  sizeof(write_char_data),
  330. //                                  write_char_data,
  331. //                                  ESP_GATT_WRITE_TYPE_RSP,
  332. //                                  ESP_GATT_AUTH_REQ_NONE);
  333.         break;
  334.  
  335.     case ESP_GATTC_SRVC_CHG_EVT: {
  336.         esp_bd_addr_t bda;
  337.         memcpy(bda, p_data->srvc_chg.remote_bda, sizeof(esp_bd_addr_t));
  338.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_SRVC_CHG_EVT, bd_addr:");
  339.         esp_log_buffer_hex(BLE_TAG, bda, sizeof(esp_bd_addr_t));
  340.         break;
  341.     }
  342.     case ESP_GATTC_WRITE_CHAR_EVT:
  343.         if (p_data->write.status != ESP_GATT_OK){
  344.             ESP_LOGE(BLE_TAG, "(battery)write char failed, error status = %x", p_data->write.status);
  345.             break;
  346.         }
  347.         ESP_LOGI(BLE_TAG, "(battery)write char success ");
  348.         break;
  349.     case ESP_GATTC_DISCONNECT_EVT:
  350.         connect_ble = false;
  351.         get_server = false;
  352.         ESP_LOGI(BLE_TAG, "(battery)ESP_GATTC_DISCONNECT_EVT, reason = %d", p_data->disconnect.reason);
  353.         break;
  354.     default:
  355.         ESP_LOGI(BLE_TAG, "unregistered EVT:%d", event);
  356.         break;
  357.     }
  358. }
  359.  
  360. static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
  361. {
  362.     uint8_t *adv_name = NULL;
  363.     uint8_t adv_name_len = 0;
  364.     switch (event) {
  365.     case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: {
  366.         //the unit of the duration is second
  367.         uint32_t duration = 30;
  368.         esp_ble_gap_start_scanning(duration);
  369.         break;
  370.     }
  371.     case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
  372.         //scan start complete event to indicate scan start successfully or failed
  373.         if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
  374.             ESP_LOGE(BLE_TAG, "scan start failed, error status = %x", param->scan_start_cmpl.status);
  375.             break;
  376.         }
  377.         ESP_LOGI(BLE_TAG, "scan start success");
  378.         break;
  379.     case ESP_GAP_BLE_SCAN_RESULT_EVT: {
  380.         esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
  381.         switch (scan_result->scan_rst.search_evt) {
  382.         case ESP_GAP_SEARCH_INQ_RES_EVT:
  383.             esp_log_buffer_hex(BLE_TAG, scan_result->scan_rst.bda, 6);
  384.             ESP_LOGI(BLE_TAG, "searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
  385.             adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
  386.                                                 ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
  387.             ESP_LOGI(BLE_TAG, "searched Device Name Len %d", adv_name_len);
  388.             esp_log_buffer_char(BLE_TAG, adv_name, adv_name_len);
  389.             ESP_LOGI(BLE_TAG, "\n");
  390.             if (adv_name != NULL) {
  391.                 if (strlen(remote_device_name) == adv_name_len && strncmp((char *)adv_name, remote_device_name, adv_name_len) == 0) {
  392.                     ESP_LOGI(BLE_TAG, "searched device %s\n", remote_device_name);
  393.                     if (connect_ble == false) {
  394.                         connect_ble = true;
  395.                         ESP_LOGI(BLE_TAG, "connect to the remote device.");
  396.                         esp_ble_gap_stop_scanning();
  397.                         esp_ble_gattc_open(gl_profile_tab[PROFILE_BATTERY_SERVICE].gattc_if, scan_result->scan_rst.bda, true);
  398.                         //esp_ble_gattc_open(gl_profile_tab[PROFILE_DEVICE_INFO].gattc_if, scan_result->scan_rst.bda, true);
  399.                         //esp_ble_gattc_open(gl_profile_tab[PROFILE_HRM].gattc_if, scan_result->scan_rst.bda, true);
  400.                     }
  401.                 }
  402.             }
  403.             break;
  404.         case ESP_GAP_SEARCH_INQ_CMPL_EVT:
  405.             break;
  406.         default:
  407.             break;
  408.         }
  409.         break;
  410.     }
  411.  
  412.     case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
  413.         if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){
  414.             ESP_LOGE(BLE_TAG, "scan stop failed, error status = %x", param->scan_stop_cmpl.status);
  415.             break;
  416.         }
  417.         ESP_LOGI(BLE_TAG, "stop scan successfully");
  418.         break;
  419.  
  420.     case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
  421.         if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){
  422.             ESP_LOGE(BLE_TAG, "adv stop failed, error status = %x", param->adv_stop_cmpl.status);
  423.             break;
  424.         }
  425.         ESP_LOGI(BLE_TAG, "stop adv successfully");
  426.         break;
  427.     case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
  428.          ESP_LOGI(BLE_TAG, "update connection params status = %d, min_int = %d, max_int = %d,conn_int = %d,latency = %d, timeout = %d",
  429.                   param->update_conn_params.status,
  430.                   param->update_conn_params.min_int,
  431.                   param->update_conn_params.max_int,
  432.                   param->update_conn_params.conn_int,
  433.                   param->update_conn_params.latency,
  434.                   param->update_conn_params.timeout);
  435.         break;
  436.     default:
  437.         break;
  438.     }
  439. }
  440.  
  441. static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
  442. {
  443.     /* If event is register event, store the gattc_if for each profile */
  444.     if (event == ESP_GATTC_REG_EVT) {
  445.         if (param->reg.status == ESP_GATT_OK) {
  446.             gl_profile_tab[param->reg.app_id].gattc_if = gattc_if;
  447.         } else {
  448.             ESP_LOGI(BLE_TAG, "reg app failed, app_id %04x, status %d",
  449.                     param->reg.app_id,
  450.                     param->reg.status);
  451.             return;
  452.         }
  453.     }
  454.  
  455.     /* If the gattc_if equal to profile A, call profile A cb handler,
  456.      * so here call each profile's callback */
  457.     do {
  458.         int idx;
  459.         for (idx = 0; idx < PROFILE_NUM; idx++) {
  460.             if (gattc_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
  461.                     gattc_if == gl_profile_tab[idx].gattc_if) {
  462.                 if (gl_profile_tab[idx].gattc_cb) {
  463.                     gl_profile_tab[idx].gattc_cb(event, gattc_if, param);
  464.                 }
  465.             }
  466.         }
  467.     } while (0);
  468. }
  469.  
  470.  
  471. void app_main()
  472. {
  473.     esp_err_t ret = nvs_flash_init();
  474.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES)
  475.     {
  476.         ESP_ERROR_CHECK(nvs_flash_erase());
  477.         ret = nvs_flash_init();
  478.     }
  479.     ESP_ERROR_CHECK( ret );
  480.  
  481.  
  482.     // BLE
  483.     {
  484.         ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
  485.  
  486.         esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  487.         ret = esp_bt_controller_init(&bt_cfg);
  488.         if (ret) {
  489.             ESP_LOGE(BLE_TAG, "%s initialize controller failed, error code = %x\n", __func__, ret);
  490.             return;
  491.         }
  492.  
  493.         ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
  494.         if (ret) {
  495.             ESP_LOGE(BLE_TAG, "%s enable controller failed, error code = %x\n", __func__, ret);
  496.             return;
  497.         }
  498.  
  499.         ret = esp_bluedroid_init();
  500.         if (ret) {
  501.             ESP_LOGE(BLE_TAG, "%s init bluetooth failed, error code = %x\n", __func__, ret);
  502.             return;
  503.         }
  504.  
  505.         ret = esp_bluedroid_enable();
  506.         if (ret) {
  507.             ESP_LOGE(BLE_TAG, "%s enable bluetooth failed, error code = %x\n", __func__, ret);
  508.             return;
  509.         }
  510.  
  511.         //register the  callback function to the gap module
  512.         ret = esp_ble_gap_register_callback(esp_gap_cb);
  513.         if (ret){
  514.             ESP_LOGE(BLE_TAG, "%s gap register failed, error code = %x\n", __func__, ret);
  515.             return;
  516.         }
  517.  
  518.         //register the callback function to the gattc module
  519.         ret = esp_ble_gattc_register_callback(esp_gattc_cb);
  520.         if(ret){
  521.             ESP_LOGE(BLE_TAG, "%s gattc register failed, error code = %x\n", __func__, ret);
  522.             return;
  523.         }
  524.  
  525.  
  526.         ret = esp_ble_gattc_app_register(PROFILE_BATTERY_SERVICE);
  527.         if (ret){
  528.             ESP_LOGE(BLE_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
  529.         }
  530.  
  531. //        ret = esp_ble_gattc_app_register(PROFILE_DEVICE_INFO);
  532. //        if (ret){
  533. //            ESP_LOGE(BLE_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
  534. //        }    
  535.        
  536. //        ret = esp_ble_gattc_app_register(PROFILE_HRM);
  537. //        if (ret){
  538. //            ESP_LOGE(BLE_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
  539. //        }
  540.  
  541.         esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
  542.         if (local_mtu_ret){
  543.             ESP_LOGE(BLE_TAG, "set local  MTU failed, error code = %x", local_mtu_ret);
  544.         }
  545.     }
  546. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement