]> granicus.if.org Git - esp-idf/blob - examples/bluetooth/gatt_server_service_table/main/gatts_table_creat_demo.c
Merge branch 'feature/base_mac_address' into 'master'
[esp-idf] / examples / bluetooth / gatt_server_service_table / main / gatts_table_creat_demo.c
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
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
6
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15
16 #include "freertos/FreeRTOS.h"
17 #include "freertos/task.h"
18 #include "freertos/event_groups.h"
19 #include "esp_system.h"
20 #include "esp_log.h"
21 #include "nvs_flash.h"
22 #include "bt.h"
23 #include "bta_api.h"
24
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"
31
32 #define GATTS_TABLE_TAG "GATTS_TABLE_DEMO"
33
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
40
41 #define GATTS_DEMO_CHAR_VAL_LEN_MAX             0x40
42
43 uint8_t char1_str[] ={0x11,0x22,0x33};
44
45 uint16_t heart_rate_handle_table[HRS_IDX_NB];
46
47 esp_attr_value_t gatts_demo_char1_val = 
48 {
49         .attr_max_len = GATTS_DEMO_CHAR_VAL_LEN_MAX,
50         .attr_len               = sizeof(char1_str),
51         .attr_value     = char1_str,
52 };
53
54
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,
59 };
60
61
62 static esp_ble_adv_data_t heart_rate_adv_config = {
63     .set_scan_rsp = false,
64     .include_name = true,
65     .include_txpower = true,
66     .min_interval = 0x20,
67     .max_interval = 0x40,
68     .appearance = 0x00,
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),
76 };
77
78 static esp_ble_adv_params_t heart_rate_adv_params = {
79     .adv_int_min        = 0x20,
80     .adv_int_max        = 0x40,
81     .adv_type           = ADV_TYPE_IND,
82     .own_addr_type      = BLE_ADDR_TYPE_PUBLIC,
83     //.peer_addr            =
84     //.peer_addr_type       =
85     .channel_map        = ADV_CHNL_ALL,
86     .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
87 };
88
89 struct gatts_profile_inst {
90     esp_gatts_cb_t gatts_cb;
91     uint16_t gatts_if;
92     uint16_t app_id;
93     uint16_t conn_id;
94     uint16_t service_handle;
95     esp_gatt_srvc_id_t service_id;
96     uint16_t char_handle;
97     esp_bt_uuid_t char_uuid;
98     esp_gatt_perm_t perm;
99     esp_gatt_char_prop_t property;
100     uint16_t descr_handle;
101     esp_bt_uuid_t descr_uuid;
102 };
103
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);
106
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 */
112     },
113     
114 };
115
116 /*
117  * HTPT PROFILE ATTRIBUTES
118  ****************************************************************************************
119  */
120
121
122 /*
123  *  Heart Rate PROFILE ATTRIBUTES
124  ****************************************************************************************
125  */
126
127 /// Heart Rate Sensor Service
128 static const uint16_t heart_rate_svc = ESP_GATT_UUID_HEART_RATE_SVC;
129
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;
137
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};
141
142
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};
146
147
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};
151
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] =
154 {
155     // Heart Rate Service Declaration
156     [HRS_IDX_SVC]                       =  
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}},
159
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}},
164       
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}},
169
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}},
174
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}},
179
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}},
184
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}},
189                                                                 
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}},  
194 };
195
196
197
198 static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
199 {
200     ESP_LOGE(GATTS_TABLE_TAG, "GAP_EVT, event %d\n", event);
201
202     switch (event) {
203     case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
204         esp_ble_gap_start_advertising(&heart_rate_adv_params);
205         break;
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");
210         }
211         break;
212     default:
213         break;
214     }
215 }
216
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) 
219 {
220     ESP_LOGE(GATTS_TABLE_TAG, "event = %x\n",event);
221     switch (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);
227
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);
231         break;
232         case ESP_GATTS_READ_EVT:
233        
234          break;
235         case ESP_GATTS_WRITE_EVT: 
236                 break;
237         case ESP_GATTS_EXEC_WRITE_EVT:
238                 break;
239         case ESP_GATTS_MTU_EVT:
240                 break;
241          case ESP_GATTS_CONF_EVT:
242                 break;
243         case ESP_GATTS_UNREG_EVT:
244                 break;
245         case ESP_GATTS_DELETE_EVT:
246                 break;
247         case ESP_GATTS_START_EVT:
248                 break;
249         case ESP_GATTS_STOP_EVT:
250                 break;
251         case ESP_GATTS_CONNECT_EVT:
252                 break;
253         case ESP_GATTS_DISCONNECT_EVT:
254                 break;
255         case ESP_GATTS_OPEN_EVT:
256                 break;
257         case ESP_GATTS_CANCEL_OPEN_EVT:
258                 break;
259         case ESP_GATTS_CLOSE_EVT:
260                 break;
261         case ESP_GATTS_LISTEN_EVT:
262                 break;
263         case ESP_GATTS_CONGEST_EVT:
264                 break;
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);
269         }
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);
273         }
274         else {
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]);
277         }
278         break;
279                                       }
280
281     default:
282         break;
283     }
284 }
285
286
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)
289 {
290     ESP_LOGI(GATTS_TABLE_TAG, "EVT %d, gatts if %d\n", event, gatts_if);
291
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;
296         } else {
297             ESP_LOGI(GATTS_TABLE_TAG, "Reg app failed, app_id %04x, status %d\n",
298                     param->reg.app_id, 
299                     param->reg.status);
300             return;
301         }
302     }
303         
304     do {
305         int idx;
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);
311                 }
312             }
313         }
314     } while (0);
315 }
316
317 void app_main()
318 {
319     esp_err_t ret;
320
321     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
322     ret = esp_bt_controller_init(&bt_cfg);
323     if (ret) {
324         ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed\n", __func__);
325         return;
326     }
327
328     ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
329     if (ret) {
330         ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed\n", __func__);
331         return;
332     }
333
334     ESP_LOGI(GATTS_TABLE_TAG, "%s init bluetooth\n", __func__);
335     ret = esp_bluedroid_init();
336     if (ret) {
337         ESP_LOGE(GATTS_TABLE_TAG, "%s init bluetooth failed\n", __func__);
338         return;
339     }
340     ret = esp_bluedroid_enable();
341     if (ret) {
342         ESP_LOGE(GATTS_TABLE_TAG, "%s enable bluetooth failed\n", __func__);
343         return;
344     }
345
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);
349     return;
350 }