Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* File: example_ble_sec_gatts_demo.h */
- // Battery Service
- enum {
- BS_IDX_SVC,
- BS_IDX_MEAS_CHAR,
- BS_IDX_MEAS_VAL,
- BS_IDX_NB,
- };
- // Environmental Sensing Service (ESS) (partly implemented)
- enum {
- ESS_IDX_SVC, // ESS service index
- ESS_IDX_ES_TEMP_MEAS_CHAR, // Characteristic index
- ESS_IDX_ES_TEMP_MEAS_VAL, // Characteristic value index
- ESS_IDX_ES_MEAS_NTF_CFG, ESS notifications configuration index
- ESS_IDX_NB, // number of table elements
- };
- /* File: example_ble_sec_gatts_demo.c */
- #include "example_ble_sec_gatts_demo.h"
- #include "environmental_sensing_service.h" #includes UUIDs of the service not found in
- #define PROFILE_NUM 2
- #define BATTERY_PROFILE_APP_IDX 0
- #define ESP_BATTERY_APP_ID 0x57 //arbitrarily chosen
- #define BATTERY_SVC_INST_ID 0
- static uint16_t battery_service_handle_table[BS_IDX_NB];
- #define ESS_PROFILE_APP_IDX 1
- #define ESP_ESS_APP_ID 0x75 //arbitrarily chosen
- #define ESS_SVC_INST_ID 0
- static uint16_t ess_handle_table[ESS_IDX_NB];
- static struct gatts_profile_inst profile_tab[PROFILE_NUM] = {
- [BATTERY_PROFILE_APP_IDX] = {
- .gatts_cb = gatts_profile_event_handler, //TODO set another event handler?
- .gatts_if = ESP_GATT_IF_NONE,
- },
- [ESS_PROFILE_APP_IDX] = {
- .gatts_cb = gatts_profile_event_handler, //TODO set another event handler?
- .gatts_if = ESP_GATT_IF_NONE,
- },
- };
- /*Environmental Sensing PROFILE ATTRIBUTES */
- // Environmental Sensing Service
- static const uint16_t ess_svc = ESS_UUID_SERVICE; //0x181A
- /// ESS Characteristic
- static const uint16_t temperature_uuid = ESS_UUID_TEMP_CHAR; //0x2A6E
- static const uint8_t temperature_value[1] = {0x0A};
- static const esp_gatts_attr_db_t ess_gatt_db[ESS_IDX_NB] = {
- // Environmental Sensing Service Declaration
- [ESS_IDX_SVC] =
- {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
- sizeof(uint16_t), sizeof(ess_svc), (uint8_t *)&ess_svc}},
- // Battery Service Characteristic Declaration - wenn ich das auskommentiere sehe ich nur einen Leeren Battery Service mit dem ich nichts machen kann
- [ESS_IDX_ES_TEMP_MEAS_CHAR] =
- {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
- CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
- // Battery Service Characteristic Value - wenn ich das auskommentiere sehe ich den Battery Service nicht
- [ESS_IDX_ES_TEMP_MEAS_VAL] =
- {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&temperature_uuid, ESP_GATT_PERM_READ_ENCRYPTED,
- sizeof(uint8_t), sizeof(temperature_value), (uint8_t *)temperature_value}},
- };
- #endif
- /* Battery Service PROFILE ATRIBUTES */
- /// Battery Service
- static const uint16_t battery_svc = ESP_GATT_UUID_BATTERY_SERVICE_SVC;
- /// Battery Level Characteristic
- static const uint16_t battery_level_uuid = ESP_GATT_UUID_BATTERY_LEVEL;
- static const uint8_t battery_level_value[1] = {0x60};
- /// Full Battery Level Service Database Description - Used to add attributes into the database
- static const esp_gatts_attr_db_t battery_level_gatt_db[BS_IDX_NB] = {
- // Battery Level Service Declaration - ohne den sehe ich in der Nordic App den Service nicht, HeartRate jedoch immer noch doppelt :D
- [BS_IDX_SVC] =
- {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
- sizeof(uint16_t), sizeof(battery_svc), (uint8_t *)&battery_svc}},
- // Battery Service Characteristic Declaration - wenn ich das auskommentiere sehe ich nur einen Leeren Battery Service mit dem ich nichts machen kann
- [BS_IDX_MEAS_CHAR] =
- {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
- CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
- // Battery Service Characteristic Value - wenn ich das auskommentiere sehe ich den Battery Service nicht
- [BS_IDX_MEAS_VAL] =
- {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&battery_level_uuid, ESP_GATT_PERM_READ_ENCRYPTED,
- sizeof(uint8_t), sizeof(battery_level_value), (uint8_t *)battery_level_value}},
- };
- 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)
- {
- ESP_LOGV(GATTS_TABLE_TAG, "event = %x\n",event);
- switch (event) {
- case ESP_GATTS_REG_EVT:
- esp_ble_gap_set_device_name(EXCAMPLE_DEVICE_NAME);
- //generate a resolvable random address
- esp_ble_gap_config_local_privacy(true);
- esp_ble_gatts_create_attr_tab(battery_level_gatt_db, gatts_if, BS_IDX_NB, BATTERY_SVC_INST_ID);
- esp_ble_gatts_create_attr_tab(ess_gatt_db, gatts_if, ESS_IDX_NB, ESS_SVC_INST_ID);
- break;
- //(...)
- case ESP_GATTS_CREAT_ATTR_TAB_EVT: {
- ESP_LOGI(GATTS_TABLE_TAG, "The number handle = %x",param->add_attr_tab.num_handle);
- if (param->create.status == ESP_GATT_OK){ //ESP_GATT_OK=0x0
- if (param->add_attr_tab.num_handle == BS_IDX_NB) {
- memcpy(battery_service_handle_table, param->add_attr_tab.handles,
- sizeof(battery_service_handle_table));
- esp_ble_gatts_start_service(battery_service_handle_table[BS_IDX_SVC]);
- } else if (param->add_attr_tab.num_handle == ESS_IDX_NB) {
- memcpy(ess_handle_table, param->add_attr_tab.handles,
- sizeof(ess_handle_table));
- esp_ble_gatts_start_service(ess_handle_table[ESS_IDX_SVC]);
- } else {
- ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table abnormally, num_handle (%d) doesn't equal to BS_IDX_NB(%d) or ESS_IDX_NB(%d)",
- param->add_attr_tab.num_handle, BS_IDX_NB, param->add_attr_tab.num_handle, ESS_IDX_NB);
- }
- }else{
- ESP_LOGE(GATTS_TABLE_TAG, " Create attribute table failed, error code = %x", param->create.status);
- }
- break;
- }
- default:
- break;
- }
- }
- do {
- int idx;
- for (idx = 0; idx < PROFILE_NUM; idx++) {
- if (gatts_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
- gatts_if == profile_tab[idx].gatts_if) {
- if (profile_tab[idx].gatts_cb) {
- profile_tab[idx].gatts_cb(event, gatts_if, param);
- }
- }
- }
- } while (0);
- void app_main()
- {
- // ---
- ret = esp_ble_gatts_app_register(ESP_BATTERY_APP_ID);
- if (ret){
- ESP_LOGE(GATTS_TABLE_TAG, "gatts app register error, error code = %x", ret);
- return;
- }
- ret = esp_ble_gatts_app_register(ESP_ESS_APP_ID);
- if (ret){
- ESP_LOGE(GATTS_TABLE_TAG, "gatts app register error, error code = %x", ret);
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement