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.

598 lines
23 KiB

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