You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

589 lines
23 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include "ble_gatts_table.h"
  2. static uint16_t *hid_conn_id;
  3. static uint8_t adv_config_done = 0;
  4. uint16_t heart_rate_handle_table[HRS_IDX_NB];
  5. static prepare_type_env_t prepare_write_env;
  6. #ifdef CONFIG_SET_RAW_ADV_DATA
  7. static uint8_t raw_adv_data[] = {
  8. /* flags */
  9. 0x02, 0x01, 0x06,
  10. /* tx power*/
  11. 0x02, 0x0a, 0xeb,
  12. /* service uuid */
  13. 0x03, 0x03, 0xFF, 0x00,
  14. /* device name */
  15. 0x0f, 0x09, 'E', 'S', 'P', '_', 'G', 'A', 'T', 'T', 'S', '_', 'D', 'E', 'M', 'O'};
  16. static uint8_t raw_scan_rsp_data[] = {
  17. /* flags */
  18. 0x02, 0x01, 0x06,
  19. /* tx power */
  20. 0x02, 0x0a, 0xeb,
  21. /* service uuid */
  22. 0x03, 0x03, 0xFF, 0x00};
  23. #else
  24. static uint8_t service_uuid[16] = {
  25. /* LSB <--------------------------------------------------------------------------------> MSB */
  26. // first uuid, 16bit, [12],[13] is the value
  27. 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, //
  28. };
  29. /* The length of adv data must be less than 31 bytes */
  30. static esp_ble_adv_data_t adv_data = {
  31. .set_scan_rsp = false,
  32. .include_name = true,
  33. .include_txpower = true,
  34. .min_interval = 0x0006, // slave connection min interval, Time = min_interval * 1.25 msec
  35. .max_interval = 0x0010, // slave connection max interval, Time = max_interval * 1.25 msec
  36. .appearance = 0x00,
  37. .manufacturer_len = 0, // TEST_MANUFACTURER_DATA_LEN,
  38. .p_manufacturer_data = NULL, // test_manufacturer,
  39. .service_data_len = 0,
  40. .p_service_data = NULL,
  41. .service_uuid_len = sizeof(service_uuid),
  42. .p_service_uuid = service_uuid,
  43. .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
  44. };
  45. // scan response data
  46. static esp_ble_adv_data_t scan_rsp_data = {
  47. .set_scan_rsp = true,
  48. .include_name = true,
  49. .include_txpower = true,
  50. .min_interval = 0x0006,
  51. .max_interval = 0x0010,
  52. .appearance = 0x00,
  53. .manufacturer_len = 0, // TEST_MANUFACTURER_DATA_LEN,
  54. .p_manufacturer_data = NULL, //&test_manufacturer[0],
  55. .service_data_len = 0,
  56. .p_service_data = NULL,
  57. .service_uuid_len = sizeof(service_uuid),
  58. .p_service_uuid = service_uuid,
  59. .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
  60. };
  61. #endif /* CONFIG_SET_RAW_ADV_DATA */
  62. static esp_ble_adv_params_t adv_params = {
  63. .adv_int_min = 0x20,
  64. .adv_int_max = 0x40,
  65. .adv_type = ADV_TYPE_IND,
  66. .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
  67. .channel_map = ADV_CHNL_ALL,
  68. .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
  69. };
  70. static void gatts_profile_event_handler(esp_gatts_cb_event_t event,
  71. esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
  72. /* One gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */
  73. static struct gatts_profile_inst heart_rate_profile_tab[PROFILE_NUM] = {
  74. [PROFILE_APP_IDX] = {
  75. .gatts_cb = gatts_profile_event_handler,
  76. .gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
  77. },
  78. };
  79. /* Service */
  80. // static const uint16_t GATTS_SERVICE_UUID_TEST = 0x00FF;
  81. // static const uint16_t GATTS_CHAR_UUID_TEST_A = 0xFF01;
  82. // static const uint16_t GATTS_CHAR_UUID_TEST_B = 0xFF02;
  83. // static const uint16_t GATTS_CHAR_UUID_TEST_C = 0xFF03;
  84. static const uint8_t adv_service_uuid128[16] = {
  85. /* LSB <--------------------------------------------------------------------------------> MSB */
  86. 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E, //
  87. };
  88. static const uint8_t adv_char_uuid128_a[16] = {
  89. /* LSB <--------------------------------------------------------------------------------> MSB */
  90. 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x02, 0x00, 0x40, 0x6E, //
  91. };
  92. static const uint8_t adv_char_uuid128_b[16] = {
  93. /* LSB <--------------------------------------------------------------------------------> MSB */
  94. 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E, //
  95. };
  96. static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE;
  97. static const uint16_t character_declaration_uuid = ESP_GATT_UUID_CHAR_DECLARE;
  98. static const uint16_t character_client_config_uuid = ESP_GATT_UUID_CHAR_CLIENT_CONFIG;
  99. static const uint8_t char_prop_read = ESP_GATT_CHAR_PROP_BIT_READ;
  100. // static const uint8_t char_prop_write = ESP_GATT_CHAR_PROP_BIT_WRITE;
  101. static const uint8_t char_prop_read_write_notify = ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
  102. static const uint8_t heart_measurement_ccc[2] = {0x00, 0x00};
  103. static const uint8_t char_value[4] = {0x11, 0x22, 0x33, 0x44};
  104. /* Full Database Description - Used to add attributes into the database */
  105. static const esp_gatts_attr_db_t gatt_db[HRS_IDX_NB] =
  106. {
  107. // Service Declaration
  108. [IDX_SVC] =
  109. {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ, //
  110. sizeof(adv_service_uuid128), sizeof(adv_service_uuid128), (uint8_t *)&adv_service_uuid128}},
  111. /* Characteristic Declaration */
  112. [IDX_CHAR_A] =
  113. {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, //
  114. CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write_notify}},
  115. /* Characteristic Value */
  116. [IDX_CHAR_VAL_A] =
  117. {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_128, (uint8_t *)&adv_char_uuid128_a, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, //
  118. GATTS_DEMO_CHAR_VAL_LEN_MAX, sizeof(char_value), (uint8_t *)char_value}},
  119. /* Client Characteristic Configuration Descriptor */
  120. [IDX_CHAR_CFG_A] =
  121. {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, //
  122. sizeof(uint16_t), sizeof(heart_measurement_ccc), (uint8_t *)heart_measurement_ccc}},
  123. /* Characteristic Declaration */
  124. [IDX_CHAR_B] =
  125. {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, //
  126. CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
  127. /* Characteristic Value */
  128. [IDX_CHAR_VAL_B] =
  129. {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_128, (uint8_t *)&adv_char_uuid128_b, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, //
  130. GATTS_DEMO_CHAR_VAL_LEN_MAX, sizeof(char_value), (uint8_t *)char_value}},
  131. // /* Characteristic Declaration */
  132. // [IDX_CHAR_C] =
  133. // {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
  134. // CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_write}},
  135. // /* Characteristic Value */
  136. // [IDX_CHAR_VAL_C] =
  137. // {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&GATTS_CHAR_UUID_TEST_C, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
  138. // GATTS_DEMO_CHAR_VAL_LEN_MAX, sizeof(char_value), (uint8_t *)char_value}},
  139. };
  140. static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
  141. {
  142. switch (event)
  143. {
  144. #ifdef CONFIG_SET_RAW_ADV_DATA
  145. case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
  146. adv_config_done &= (~ADV_CONFIG_FLAG);
  147. if (adv_config_done == 0)
  148. {
  149. esp_ble_gap_start_advertising(&adv_params);
  150. }
  151. break;
  152. case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT:
  153. adv_config_done &= (~SCAN_RSP_CONFIG_FLAG);
  154. if (adv_config_done == 0)
  155. {
  156. esp_ble_gap_start_advertising(&adv_params);
  157. }
  158. break;
  159. #else
  160. case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
  161. adv_config_done &= (~ADV_CONFIG_FLAG);
  162. if (adv_config_done == 0)
  163. {
  164. esp_ble_gap_start_advertising(&adv_params);
  165. }
  166. break;
  167. case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
  168. adv_config_done &= (~SCAN_RSP_CONFIG_FLAG);
  169. if (adv_config_done == 0)
  170. {
  171. esp_ble_gap_start_advertising(&adv_params);
  172. }
  173. break;
  174. #endif
  175. case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
  176. /* advertising start complete event to indicate advertising start successfully or failed */
  177. if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS)
  178. {
  179. ESP_LOGE(GATTS_TABLE_TAG, "advertising start failed");
  180. }
  181. else
  182. {
  183. ESP_LOGI(GATTS_TABLE_TAG, "advertising start successfully");
  184. }
  185. break;
  186. case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
  187. if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS)
  188. {
  189. ESP_LOGE(GATTS_TABLE_TAG, "Advertising stop failed");
  190. }
  191. else
  192. {
  193. ESP_LOGI(GATTS_TABLE_TAG, "Stop adv successfully\n");
  194. }
  195. break;
  196. case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
  197. ESP_LOGI(GATTS_TABLE_TAG, "update connection params status = %d, min_int = %d, max_int = %d,conn_int = %d,latency = %d, timeout = %d",
  198. param->update_conn_params.status,
  199. param->update_conn_params.min_int,
  200. param->update_conn_params.max_int,
  201. param->update_conn_params.conn_int,
  202. param->update_conn_params.latency,
  203. param->update_conn_params.timeout);
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. void example_prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param)
  210. {
  211. ESP_LOGI(GATTS_TABLE_TAG, "prepare write, handle = %d, value len = %d", param->write.handle, param->write.len);
  212. esp_gatt_status_t status = ESP_GATT_OK;
  213. if (prepare_write_env->prepare_buf == NULL)
  214. {
  215. prepare_write_env->prepare_buf = (uint8_t *)malloc(PREPARE_BUF_MAX_SIZE * sizeof(uint8_t));
  216. prepare_write_env->prepare_len = 0;
  217. if (prepare_write_env->prepare_buf == NULL)
  218. {
  219. ESP_LOGE(GATTS_TABLE_TAG, "%s, Gatt_server prep no mem", __func__);
  220. status = ESP_GATT_NO_RESOURCES;
  221. }
  222. }
  223. else
  224. {
  225. if (param->write.offset > PREPARE_BUF_MAX_SIZE)
  226. {
  227. status = ESP_GATT_INVALID_OFFSET;
  228. }
  229. else if ((param->write.offset + param->write.len) > PREPARE_BUF_MAX_SIZE)
  230. {
  231. status = ESP_GATT_INVALID_ATTR_LEN;
  232. }
  233. }
  234. /*send response when param->write.need_rsp is true */
  235. if (param->write.need_rsp)
  236. {
  237. esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *)malloc(sizeof(esp_gatt_rsp_t));
  238. if (gatt_rsp != NULL)
  239. {
  240. gatt_rsp->attr_value.len = param->write.len;
  241. gatt_rsp->attr_value.handle = param->write.handle;
  242. gatt_rsp->attr_value.offset = param->write.offset;
  243. gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
  244. memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len);
  245. esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp);
  246. if (response_err != ESP_OK)
  247. {
  248. ESP_LOGE(GATTS_TABLE_TAG, "Send response error");
  249. }
  250. free(gatt_rsp);
  251. }
  252. else
  253. {
  254. ESP_LOGE(GATTS_TABLE_TAG, "%s, malloc failed", __func__);
  255. }
  256. }
  257. if (status != ESP_GATT_OK)
  258. {
  259. return;
  260. }
  261. memcpy(prepare_write_env->prepare_buf + param->write.offset,
  262. param->write.value,
  263. param->write.len);
  264. prepare_write_env->prepare_len += param->write.len;
  265. }
  266. void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param)
  267. {
  268. if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC && prepare_write_env->prepare_buf)
  269. {
  270. esp_log_buffer_hex(GATTS_TABLE_TAG, prepare_write_env->prepare_buf, prepare_write_env->prepare_len);
  271. }
  272. else
  273. {
  274. ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATT_PREP_WRITE_CANCEL");
  275. }
  276. if (prepare_write_env->prepare_buf)
  277. {
  278. free(prepare_write_env->prepare_buf);
  279. prepare_write_env->prepare_buf = NULL;
  280. }
  281. prepare_write_env->prepare_len = 0;
  282. }
  283. static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
  284. {
  285. switch (event)
  286. {
  287. case ESP_GATTS_REG_EVT:
  288. {
  289. esp_err_t set_dev_name_ret = esp_ble_gap_set_device_name(SAMPLE_DEVICE_NAME);
  290. if (set_dev_name_ret)
  291. {
  292. ESP_LOGE(GATTS_TABLE_TAG, "set device name failed, error code = %x", set_dev_name_ret);
  293. }
  294. #ifdef CONFIG_SET_RAW_ADV_DATA
  295. esp_err_t raw_adv_ret = esp_ble_gap_config_adv_data_raw(raw_adv_data, sizeof(raw_adv_data));
  296. if (raw_adv_ret)
  297. {
  298. ESP_LOGE(GATTS_TABLE_TAG, "config raw adv data failed, error code = %x ", raw_adv_ret);
  299. }
  300. adv_config_done |= ADV_CONFIG_FLAG;
  301. esp_err_t raw_scan_ret = esp_ble_gap_config_scan_rsp_data_raw(raw_scan_rsp_data, sizeof(raw_scan_rsp_data));
  302. if (raw_scan_ret)
  303. {
  304. ESP_LOGE(GATTS_TABLE_TAG, "config raw scan rsp data failed, error code = %x", raw_scan_ret);
  305. }
  306. adv_config_done |= SCAN_RSP_CONFIG_FLAG;
  307. #else
  308. // config adv data
  309. esp_err_t ret = esp_ble_gap_config_adv_data(&adv_data);
  310. if (ret)
  311. {
  312. ESP_LOGE(GATTS_TABLE_TAG, "config adv data failed, error code = %x", ret);
  313. }
  314. adv_config_done |= ADV_CONFIG_FLAG;
  315. // config scan response data
  316. ret = esp_ble_gap_config_adv_data(&scan_rsp_data);
  317. if (ret)
  318. {
  319. ESP_LOGE(GATTS_TABLE_TAG, "config scan response data failed, error code = %x", ret);
  320. }
  321. adv_config_done |= SCAN_RSP_CONFIG_FLAG;
  322. #endif
  323. esp_err_t create_attr_ret = esp_ble_gatts_create_attr_tab(gatt_db, gatts_if, HRS_IDX_NB, SVC_INST_ID);
  324. if (create_attr_ret)
  325. {
  326. ESP_LOGE(GATTS_TABLE_TAG, "create attr table failed, error code = %x", create_attr_ret);
  327. }
  328. }
  329. break;
  330. case ESP_GATTS_READ_EVT:
  331. ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_READ_EVT");
  332. break;
  333. case ESP_GATTS_WRITE_EVT:
  334. if (!param->write.is_prep)
  335. {
  336. // the data length of gattc write must be less than GATTS_DEMO_CHAR_VAL_LEN_MAX.
  337. ESP_LOGI(GATTS_TABLE_TAG, "GATT_WRITE_EVT, handle = %d, value len = %d, value :", param->write.handle, param->write.len);
  338. esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
  339. if (heart_rate_handle_table[IDX_CHAR_CFG_A] == param->write.handle && param->write.len == 2)
  340. {
  341. uint16_t descr_value = param->write.value[1] << 8 | param->write.value[0];
  342. if (descr_value == 0x0001)
  343. {
  344. ESP_LOGI(GATTS_TABLE_TAG, "notify enable");
  345. uint8_t notify_data[15];
  346. for (int i = 0; i < sizeof(notify_data); ++i)
  347. {
  348. notify_data[i] = i % 0xff;
  349. }
  350. // the size of notify_data[] need less than MTU size
  351. esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, heart_rate_handle_table[IDX_CHAR_VAL_A],
  352. sizeof(notify_data), notify_data, false);
  353. }
  354. else if (descr_value == 0x0002)
  355. {
  356. ESP_LOGI(GATTS_TABLE_TAG, "indicate enable");
  357. uint8_t indicate_data[15];
  358. for (int i = 0; i < sizeof(indicate_data); ++i)
  359. {
  360. indicate_data[i] = i % 0xff;
  361. }
  362. // the size of indicate_data[] need less than MTU size
  363. esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, heart_rate_handle_table[IDX_CHAR_VAL_A],
  364. sizeof(indicate_data), indicate_data, true);
  365. }
  366. else if (descr_value == 0x0000)
  367. {
  368. ESP_LOGI(GATTS_TABLE_TAG, "notify/indicate disable ");
  369. }
  370. else
  371. {
  372. ESP_LOGE(GATTS_TABLE_TAG, "unknown descr value");
  373. esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
  374. }
  375. }
  376. /* send response when param->write.need_rsp is true*/
  377. if (param->write.need_rsp)
  378. {
  379. esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
  380. }
  381. }
  382. else
  383. {
  384. /* handle prepare write */
  385. example_prepare_write_event_env(gatts_if, &prepare_write_env, param);
  386. }
  387. break;
  388. case ESP_GATTS_EXEC_WRITE_EVT:
  389. // the length of gattc prepare write data must be less than GATTS_DEMO_CHAR_VAL_LEN_MAX.
  390. ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_EXEC_WRITE_EVT");
  391. example_exec_write_event_env(&prepare_write_env, param);
  392. break;
  393. case ESP_GATTS_MTU_EVT:
  394. ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
  395. break;
  396. case ESP_GATTS_CONF_EVT:
  397. ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_CONF_EVT, status = %d, attr_handle %d", param->conf.status, param->conf.handle);
  398. break;
  399. case ESP_GATTS_START_EVT:
  400. ESP_LOGI(GATTS_TABLE_TAG, "SERVICE_START_EVT, status %d, service_handle %d", param->start.status, param->start.service_handle);
  401. break;
  402. case ESP_GATTS_CONNECT_EVT:
  403. ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_CONNECT_EVT, conn_id = %d", param->connect.conn_id);
  404. esp_log_buffer_hex(GATTS_TABLE_TAG, param->connect.remote_bda, 6);
  405. esp_ble_conn_update_params_t conn_params = {0};
  406. memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
  407. /* For the iOS system, please refer to Apple official documents about the BLE connection parameters restrictions. */
  408. conn_params.latency = 0;
  409. conn_params.max_int = 0x20; // max_int = 0x20*1.25ms = 40ms
  410. conn_params.min_int = 0x10; // min_int = 0x10*1.25ms = 20ms
  411. conn_params.timeout = 400; // timeout = 400*10ms = 4000ms
  412. // start sent the update connection parameters to the peer device.
  413. esp_ble_gap_update_conn_params(&conn_params);
  414. *hid_conn_id = param->connect.conn_id;
  415. // *hid_conn_id = 100;
  416. break;
  417. case ESP_GATTS_DISCONNECT_EVT:
  418. ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_DISCONNECT_EVT, reason = 0x%x", param->disconnect.reason);
  419. esp_ble_gap_start_advertising(&adv_params);
  420. break;
  421. case ESP_GATTS_CREAT_ATTR_TAB_EVT:
  422. {
  423. if (param->add_attr_tab.status != ESP_GATT_OK)
  424. {
  425. ESP_LOGE(GATTS_TABLE_TAG, "create attribute table failed, error code=0x%x", param->add_attr_tab.status);
  426. }
  427. else if (param->add_attr_tab.num_handle != HRS_IDX_NB)
  428. {
  429. ESP_LOGE(GATTS_TABLE_TAG, "create attribute table abnormally, num_handle (%d) \
  430. doesn't equal to HRS_IDX_NB(%d)",
  431. param->add_attr_tab.num_handle, HRS_IDX_NB);
  432. }
  433. else
  434. {
  435. ESP_LOGI(GATTS_TABLE_TAG, "create attribute table successfully, the number handle = %d\n", param->add_attr_tab.num_handle);
  436. memcpy(heart_rate_handle_table, param->add_attr_tab.handles, sizeof(heart_rate_handle_table));
  437. esp_ble_gatts_start_service(heart_rate_handle_table[IDX_SVC]);
  438. }
  439. break;
  440. }
  441. case ESP_GATTS_STOP_EVT:
  442. case ESP_GATTS_OPEN_EVT:
  443. case ESP_GATTS_CANCEL_OPEN_EVT:
  444. case ESP_GATTS_CLOSE_EVT:
  445. case ESP_GATTS_LISTEN_EVT:
  446. case ESP_GATTS_CONGEST_EVT:
  447. case ESP_GATTS_UNREG_EVT:
  448. case ESP_GATTS_DELETE_EVT:
  449. default:
  450. break;
  451. }
  452. }
  453. static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
  454. {
  455. /* If event is register event, store the gatts_if for each profile */
  456. if (event == ESP_GATTS_REG_EVT)
  457. {
  458. if (param->reg.status == ESP_GATT_OK)
  459. {
  460. heart_rate_profile_tab[PROFILE_APP_IDX].gatts_if = gatts_if;
  461. }
  462. else
  463. {
  464. ESP_LOGE(GATTS_TABLE_TAG, "reg app failed, app_id %04x, status %d",
  465. param->reg.app_id,
  466. param->reg.status);
  467. return;
  468. }
  469. }
  470. do
  471. {
  472. int idx;
  473. for (idx = 0; idx < PROFILE_NUM; idx++)
  474. {
  475. /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
  476. if (gatts_if == ESP_GATT_IF_NONE || gatts_if == heart_rate_profile_tab[idx].gatts_if)
  477. {
  478. if (heart_rate_profile_tab[idx].gatts_cb)
  479. {
  480. heart_rate_profile_tab[idx].gatts_cb(event, gatts_if, param);
  481. }
  482. }
  483. }
  484. } while (0);
  485. }
  486. void ble_init(uint16_t *conn_id_ble)
  487. {
  488. esp_err_t ret;
  489. hid_conn_id = conn_id_ble;
  490. /* Initialize NVS. */
  491. ret = nvs_flash_init();
  492. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
  493. {
  494. ESP_ERROR_CHECK(nvs_flash_erase());
  495. ret = nvs_flash_init();
  496. }
  497. ESP_ERROR_CHECK(ret);
  498. ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
  499. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  500. ret = esp_bt_controller_init(&bt_cfg);
  501. if (ret)
  502. {
  503. ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret));
  504. return;
  505. }
  506. ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
  507. if (ret)
  508. {
  509. ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret));
  510. return;
  511. }
  512. ret = esp_bluedroid_init();
  513. if (ret)
  514. {
  515. ESP_LOGE(GATTS_TABLE_TAG, "%s init bluetooth failed: %s", __func__, esp_err_to_name(ret));
  516. return;
  517. }
  518. ret = esp_bluedroid_enable();
  519. if (ret)
  520. {
  521. ESP_LOGE(GATTS_TABLE_TAG, "%s enable bluetooth failed: %s", __func__, esp_err_to_name(ret));
  522. return;
  523. }
  524. ret = esp_ble_gatts_register_callback(gatts_event_handler);
  525. if (ret)
  526. {
  527. ESP_LOGE(GATTS_TABLE_TAG, "gatts register error, error code = %x", ret);
  528. return;
  529. }
  530. ret = esp_ble_gap_register_callback(gap_event_handler);
  531. if (ret)
  532. {
  533. ESP_LOGE(GATTS_TABLE_TAG, "gap register error, error code = %x", ret);
  534. return;
  535. }
  536. ret = esp_ble_gatts_app_register(ESP_APP_ID);
  537. if (ret)
  538. {
  539. ESP_LOGE(GATTS_TABLE_TAG, "gatts app register error, error code = %x", ret);
  540. return;
  541. }
  542. esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
  543. if (local_mtu_ret)
  544. {
  545. ESP_LOGE(GATTS_TABLE_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
  546. }
  547. }