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