1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 #include "freertos/FreeRTOS.h"
17 #include "freertos/task.h"
18 #include "freertos/event_groups.h"
19 #include "esp_system.h"
21 #include "nvs_flash.h"
25 #include "esp_gap_ble_api.h"
26 #include "esp_gatts_api.h"
27 #include "esp_bt_defs.h"
28 #include "esp_bt_main.h"
29 #include "esp_bt_main.h"
30 #include "gatts_table_creat_demo.h"
32 #define GATTS_TABLE_TAG "GATTS_TABLE_DEMO"
34 #define HEART_PROFILE_NUM 1
35 #define HEART_PROFILE_APP_IDX 0
36 #define ESP_HEART_RATE_APP_ID 0x55
37 #define SAMPLE_DEVICE_NAME "ESP_HEART_RATE"
38 #define SAMPLE_MANUFACTURER_DATA_LEN 17
39 #define HEART_RATE_SVC_INST_ID 0
41 #define GATTS_DEMO_CHAR_VAL_LEN_MAX 0x40
43 uint8_t char1_str[] ={0x11,0x22,0x33};
45 uint16_t heart_rate_handle_table[HRS_IDX_NB];
47 esp_attr_value_t gatts_demo_char1_val =
49 .attr_max_len = GATTS_DEMO_CHAR_VAL_LEN_MAX,
50 .attr_len = sizeof(char1_str),
51 .attr_value = char1_str,
55 static uint8_t heart_rate_service_uuid[16] = {
56 /* LSB <--------------------------------------------------------------------------------> MSB */
57 //first uuid, 16bit, [12],[13] is the value
58 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x18, 0x0D, 0x00, 0x00,
62 static esp_ble_adv_data_t heart_rate_adv_config = {
63 .set_scan_rsp = false,
65 .include_txpower = true,
69 .manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN,
70 .p_manufacturer_data = NULL, //&test_manufacturer[0],
71 .service_data_len = 0,
72 .p_service_data = NULL,
73 .service_uuid_len = sizeof(heart_rate_service_uuid),
74 .p_service_uuid = heart_rate_service_uuid,
75 .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
78 static esp_ble_adv_params_t heart_rate_adv_params = {
81 .adv_type = ADV_TYPE_IND,
82 .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
85 .channel_map = ADV_CHNL_ALL,
86 .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
89 struct gatts_profile_inst {
90 esp_gatts_cb_t gatts_cb;
94 uint16_t service_handle;
95 esp_gatt_srvc_id_t service_id;
97 esp_bt_uuid_t char_uuid;
99 esp_gatt_char_prop_t property;
100 uint16_t descr_handle;
101 esp_bt_uuid_t descr_uuid;
104 static void gatts_profile_event_handler(esp_gatts_cb_event_t event,
105 esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
107 /* One gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */
108 static struct gatts_profile_inst heart_rate_profile_tab[HEART_PROFILE_NUM] = {
109 [HEART_PROFILE_APP_IDX] = {
110 .gatts_cb = gatts_profile_event_handler,
111 .gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
117 * HTPT PROFILE ATTRIBUTES
118 ****************************************************************************************
123 * Heart Rate PROFILE ATTRIBUTES
124 ****************************************************************************************
127 /// Heart Rate Sensor Service
128 static const uint16_t heart_rate_svc = ESP_GATT_UUID_HEART_RATE_SVC;
130 #define CHAR_DECLARATION_SIZE (sizeof(uint8_t))
131 static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE;
132 static const uint16_t character_declaration_uuid = ESP_GATT_UUID_CHAR_DECLARE;
133 static const uint16_t character_client_config_uuid = ESP_GATT_UUID_CHAR_CLIENT_CONFIG;
134 static const uint8_t char_prop_notify = ESP_GATT_CHAR_PROP_BIT_NOTIFY;
135 static const uint8_t char_prop_read = ESP_GATT_CHAR_PROP_BIT_READ;
136 static const uint8_t char_prop_read_write = ESP_GATT_CHAR_PROP_BIT_WRITE|ESP_GATT_CHAR_PROP_BIT_READ;
138 /// Heart Rate Sensor Service - Heart Rate Measurement Characteristic, notify
139 static const uint16_t heart_rate_meas_uuid = ESP_GATT_HEART_RATE_MEAS;
140 static const uint8_t heart_measurement_ccc[2] ={ 0x00, 0x00};
143 /// Heart Rate Sensor Service -Body Sensor Location characteristic, read
144 static const uint16_t body_sensor_location_uuid = ESP_GATT_BODY_SENSOR_LOCATION;
145 static const uint8_t body_sensor_loc_val[1] = {0x00};
148 /// Heart Rate Sensor Service - Heart Rate Control Point characteristic, write&read
149 static const uint16_t heart_rate_ctrl_point = ESP_GATT_HEART_RATE_CNTL_POINT;
150 static const uint8_t heart_ctrl_point[1] = {0x00};
152 /// Full HRS Database Description - Used to add attributes into the database
153 static const esp_gatts_attr_db_t heart_rate_gatt_db[HRS_IDX_NB] =
155 // Heart Rate Service Declaration
157 {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
158 sizeof(uint16_t), sizeof(heart_rate_svc), (uint8_t *)&heart_rate_svc}},
160 // Heart Rate Measurement Characteristic Declaration
161 [HRS_IDX_HR_MEAS_CHAR] =
162 {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
163 CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_notify}},
165 // Heart Rate Measurement Characteristic Value
166 [HRS_IDX_HR_MEAS_VAL] =
167 {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&heart_rate_meas_uuid, ESP_GATT_PERM_READ,
168 HRPS_HT_MEAS_MAX_LEN,0, NULL}},
170 // Heart Rate Measurement Characteristic - Client Characteristic Configuration Descriptor
171 [HRS_IDX_HR_MEAS_NTF_CFG] =
172 {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
173 sizeof(uint16_t),sizeof(heart_measurement_ccc), (uint8_t *)heart_measurement_ccc}},
175 // Body Sensor Location Characteristic Declaration
176 [HRS_IDX_BOBY_SENSOR_LOC_CHAR] =
177 {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
178 CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
180 // Body Sensor Location Characteristic Value
181 [HRS_IDX_BOBY_SENSOR_LOC_VAL] =
182 {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&body_sensor_location_uuid, ESP_GATT_PERM_READ,
183 sizeof(uint8_t), sizeof(body_sensor_loc_val), (uint8_t *)body_sensor_loc_val}},
185 // Heart Rate Control Point Characteristic Declaration
186 [HRS_IDX_HR_CTNL_PT_CHAR] =
187 {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
188 CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}},
190 // Heart Rate Control Point Characteristic Value
191 [HRS_IDX_HR_CTNL_PT_VAL] =
192 {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&heart_rate_ctrl_point, ESP_GATT_PERM_WRITE|ESP_GATT_PERM_READ,
193 sizeof(uint8_t), sizeof(heart_ctrl_point), (uint8_t *)heart_ctrl_point}},
198 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
200 ESP_LOGE(GATTS_TABLE_TAG, "GAP_EVT, event %d\n", event);
203 case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
204 esp_ble_gap_start_advertising(&heart_rate_adv_params);
206 case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
207 //advertising start complete event to indicate advertising start successfully or failed
208 if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
209 ESP_LOGE(GATTS_TABLE_TAG, "Advertising start failed\n");
217 static void gatts_profile_event_handler(esp_gatts_cb_event_t event,
218 esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
220 ESP_LOGE(GATTS_TABLE_TAG, "event = %x\n",event);
222 case ESP_GATTS_REG_EVT:
223 ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__);
224 esp_ble_gap_set_device_name(SAMPLE_DEVICE_NAME);
225 ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__);
226 esp_ble_gap_config_adv_data(&heart_rate_adv_config);
228 ESP_LOGI(GATTS_TABLE_TAG, "%s %d\n", __func__, __LINE__);
229 esp_ble_gatts_create_attr_tab(heart_rate_gatt_db, gatts_if,
230 HRS_IDX_NB, HEART_RATE_SVC_INST_ID);
232 case ESP_GATTS_READ_EVT:
235 case ESP_GATTS_WRITE_EVT:
237 case ESP_GATTS_EXEC_WRITE_EVT:
239 case ESP_GATTS_MTU_EVT:
241 case ESP_GATTS_CONF_EVT:
243 case ESP_GATTS_UNREG_EVT:
245 case ESP_GATTS_DELETE_EVT:
247 case ESP_GATTS_START_EVT:
249 case ESP_GATTS_STOP_EVT:
251 case ESP_GATTS_CONNECT_EVT:
253 case ESP_GATTS_DISCONNECT_EVT:
255 case ESP_GATTS_OPEN_EVT:
257 case ESP_GATTS_CANCEL_OPEN_EVT:
259 case ESP_GATTS_CLOSE_EVT:
261 case ESP_GATTS_LISTEN_EVT:
263 case ESP_GATTS_CONGEST_EVT:
265 case ESP_GATTS_CREAT_ATTR_TAB_EVT:{
266 ESP_LOGI(GATTS_TABLE_TAG, "The number handle =%x\n",param->add_attr_tab.num_handle);
267 if (param->add_attr_tab.status != ESP_GATT_OK){
268 ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table failed, error code=0x%x", param->add_attr_tab.status);
270 else if (param->add_attr_tab.num_handle != HRS_IDX_NB){
271 ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table abnormally, num_handle (%d) \
272 doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, HRS_IDX_NB);
275 memcpy(heart_rate_handle_table, param->add_attr_tab.handles, sizeof(heart_rate_handle_table));
276 esp_ble_gatts_start_service(heart_rate_handle_table[HRS_IDX_SVC]);
287 static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
288 esp_ble_gatts_cb_param_t *param)
290 ESP_LOGI(GATTS_TABLE_TAG, "EVT %d, gatts if %d\n", event, gatts_if);
292 /* If event is register event, store the gatts_if for each profile */
293 if (event == ESP_GATTS_REG_EVT) {
294 if (param->reg.status == ESP_GATT_OK) {
295 heart_rate_profile_tab[HEART_PROFILE_APP_IDX].gatts_if = gatts_if;
297 ESP_LOGI(GATTS_TABLE_TAG, "Reg app failed, app_id %04x, status %d\n",
306 for (idx = 0; idx < HEART_PROFILE_NUM; idx++) {
307 if (gatts_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
308 gatts_if == heart_rate_profile_tab[idx].gatts_if) {
309 if (heart_rate_profile_tab[idx].gatts_cb) {
310 heart_rate_profile_tab[idx].gatts_cb(event, gatts_if, param);
321 esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
322 ret = esp_bt_controller_init(&bt_cfg);
324 ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed\n", __func__);
328 ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
330 ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed\n", __func__);
334 ESP_LOGI(GATTS_TABLE_TAG, "%s init bluetooth\n", __func__);
335 ret = esp_bluedroid_init();
337 ESP_LOGE(GATTS_TABLE_TAG, "%s init bluetooth failed\n", __func__);
340 ret = esp_bluedroid_enable();
342 ESP_LOGE(GATTS_TABLE_TAG, "%s enable bluetooth failed\n", __func__);
346 esp_ble_gatts_register_callback(gatts_event_handler);
347 esp_ble_gap_register_callback(gap_event_handler);
348 esp_ble_gatts_app_register(ESP_HEART_RATE_APP_ID);