SHARE
TWEET

Untitled

a guest May 19th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* File: example_ble_sec_gatts_demo.h */
  2.  
  3. // Battery Service
  4. enum {
  5.     BS_IDX_SVC,
  6.  
  7.     BS_IDX_MEAS_CHAR,
  8.     BS_IDX_MEAS_VAL,
  9.  
  10.     BS_IDX_NB,
  11. };
  12.  
  13. // Environmental Sensing Service (ESS) (partly implemented)
  14. enum {
  15.     ESS_IDX_SVC,            // ESS service index
  16.  
  17.     ESS_IDX_ES_TEMP_MEAS_CHAR,   // Characteristic index
  18.     ESS_IDX_ES_TEMP_MEAS_VAL,    // Characteristic value index
  19.      ESS_IDX_ES_MEAS_NTF_CFG,     ESS notifications configuration index
  20.  
  21.     ESS_IDX_NB,             // number of table elements
  22. };
  23.  
  24.  
  25.  
  26.  
  27.  
  28. /* File: example_ble_sec_gatts_demo.c */
  29. #include "example_ble_sec_gatts_demo.h"
  30. #include "environmental_sensing_service.h"  #includes UUIDs of the service not found in
  31.  
  32. #define PROFILE_NUM                 2
  33.  
  34. #define BATTERY_PROFILE_APP_IDX         0
  35. #define ESP_BATTERY_APP_ID              0x57    //arbitrarily chosen
  36. #define BATTERY_SVC_INST_ID             0
  37. static uint16_t battery_service_handle_table[BS_IDX_NB];
  38.  
  39. #define ESS_PROFILE_APP_IDX         1
  40. #define ESP_ESS_APP_ID              0x75    //arbitrarily chosen
  41. #define ESS_SVC_INST_ID             0
  42. static uint16_t ess_handle_table[ESS_IDX_NB];
  43.  
  44.  
  45. static struct gatts_profile_inst profile_tab[PROFILE_NUM] = {
  46.         [BATTERY_PROFILE_APP_IDX] = {
  47.             .gatts_cb = gatts_profile_event_handler,    //TODO set another event handler?
  48.             .gatts_if = ESP_GATT_IF_NONE,
  49.         },
  50.     [ESS_PROFILE_APP_IDX] = {
  51.         .gatts_cb = gatts_profile_event_handler, //TODO set another event handler?
  52.         .gatts_if = ESP_GATT_IF_NONE,
  53.     },
  54. };
  55.  
  56.  
  57. /*Environmental Sensing PROFILE ATTRIBUTES */
  58. // Environmental Sensing Service
  59. static const uint16_t ess_svc = ESS_UUID_SERVICE;   //0x181A
  60.  
  61. /// ESS Characteristic
  62. static const uint16_t temperature_uuid = ESS_UUID_TEMP_CHAR;    //0x2A6E
  63. static const uint8_t temperature_value[1] = {0x0A};
  64.  
  65. static const esp_gatts_attr_db_t ess_gatt_db[ESS_IDX_NB] = {
  66.     // Environmental Sensing Service Declaration
  67.     [ESS_IDX_SVC]                    =
  68.     {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
  69.       sizeof(uint16_t), sizeof(ess_svc), (uint8_t *)&ess_svc}},
  70.  
  71.     // Battery Service Characteristic Declaration - wenn ich das auskommentiere sehe ich nur einen Leeren Battery Service mit dem ich nichts machen kann
  72.     [ESS_IDX_ES_TEMP_MEAS_CHAR]  =
  73.     {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
  74.       CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
  75.  
  76.     // Battery Service Characteristic Value - wenn ich das auskommentiere sehe ich den Battery Service nicht
  77.     [ESS_IDX_ES_TEMP_MEAS_VAL]   =
  78.     {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&temperature_uuid, ESP_GATT_PERM_READ_ENCRYPTED,
  79.       sizeof(uint8_t), sizeof(temperature_value), (uint8_t *)temperature_value}},
  80. };
  81. #endif
  82.  
  83.  
  84. /* Battery Service PROFILE ATRIBUTES */
  85. /// Battery Service
  86. static const uint16_t battery_svc = ESP_GATT_UUID_BATTERY_SERVICE_SVC;
  87.  
  88. /// Battery Level Characteristic
  89. static const uint16_t battery_level_uuid = ESP_GATT_UUID_BATTERY_LEVEL;
  90. static const uint8_t battery_level_value[1] = {0x60};
  91.  
  92. /// Full Battery Level Service Database Description - Used to add attributes into the database
  93. static const esp_gatts_attr_db_t battery_level_gatt_db[BS_IDX_NB] = {
  94.      // Battery Level Service Declaration - ohne den sehe ich in der Nordic App den Service nicht, HeartRate jedoch immer noch doppelt :D
  95.     [BS_IDX_SVC]                    =
  96.     {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
  97.       sizeof(uint16_t), sizeof(battery_svc), (uint8_t *)&battery_svc}},
  98.  
  99.     // Battery Service Characteristic Declaration - wenn ich das auskommentiere sehe ich nur einen Leeren Battery Service mit dem ich nichts machen kann
  100.     [BS_IDX_MEAS_CHAR]  =
  101.     {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
  102.       CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
  103.  
  104.     // Battery Service Characteristic Value - wenn ich das auskommentiere sehe ich den Battery Service nicht
  105.     [BS_IDX_MEAS_VAL]   =
  106.     {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&battery_level_uuid, ESP_GATT_PERM_READ_ENCRYPTED,
  107.       sizeof(uint8_t), sizeof(battery_level_value), (uint8_t *)battery_level_value}},
  108. };
  109.  
  110.  
  111.  
  112. 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)
  113. {
  114.     ESP_LOGV(GATTS_TABLE_TAG, "event = %x\n",event);
  115.     switch (event) {
  116.         case ESP_GATTS_REG_EVT:
  117.             esp_ble_gap_set_device_name(EXCAMPLE_DEVICE_NAME);
  118.             //generate a resolvable random address
  119.             esp_ble_gap_config_local_privacy(true);
  120.            
  121.             esp_ble_gatts_create_attr_tab(battery_level_gatt_db, gatts_if, BS_IDX_NB, BATTERY_SVC_INST_ID);
  122.             esp_ble_gatts_create_attr_tab(ess_gatt_db, gatts_if, ESS_IDX_NB, ESS_SVC_INST_ID);
  123.         break;
  124.        
  125.         //(...)
  126.    
  127.      case ESP_GATTS_CREAT_ATTR_TAB_EVT: {
  128.                 ESP_LOGI(GATTS_TABLE_TAG, "The number handle = %x",param->add_attr_tab.num_handle);
  129.                 if (param->create.status == ESP_GATT_OK){   //ESP_GATT_OK=0x0
  130.  
  131.                         if (param->add_attr_tab.num_handle == BS_IDX_NB) {
  132.                             memcpy(battery_service_handle_table, param->add_attr_tab.handles,
  133.                             sizeof(battery_service_handle_table));
  134.                             esp_ble_gatts_start_service(battery_service_handle_table[BS_IDX_SVC]);
  135.              
  136.                         } else if (param->add_attr_tab.num_handle == ESS_IDX_NB) {
  137.                             memcpy(ess_handle_table, param->add_attr_tab.handles,
  138.                             sizeof(ess_handle_table));
  139.                             esp_ble_gatts_start_service(ess_handle_table[ESS_IDX_SVC]);
  140.            
  141.                     } else {
  142.                 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)",
  143.                              param->add_attr_tab.num_handle, BS_IDX_NB, param->add_attr_tab.num_handle, ESS_IDX_NB);
  144.                     }
  145.                 }else{
  146.                     ESP_LOGE(GATTS_TABLE_TAG, " Create attribute table failed, error code = %x", param->create.status);
  147.                 }
  148.     break;
  149.   }
  150.  
  151.         default:
  152.            break;
  153.     }
  154. }
  155.  
  156.  
  157.  
  158. do {
  159.         int idx;
  160.         for (idx = 0; idx < PROFILE_NUM; idx++) {
  161.             if (gatts_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
  162.                     gatts_if == profile_tab[idx].gatts_if) {
  163.                 if (profile_tab[idx].gatts_cb) {
  164.                     profile_tab[idx].gatts_cb(event, gatts_if, param);
  165.                 }
  166.             }
  167.         }
  168.     } while (0);
  169.  
  170.  
  171.  
  172. void app_main()
  173. {
  174.     // ---
  175.  
  176.     ret = esp_ble_gatts_app_register(ESP_BATTERY_APP_ID);
  177.     if (ret){
  178.         ESP_LOGE(GATTS_TABLE_TAG, "gatts app register error, error code = %x", ret);
  179.         return;
  180.     }
  181.  
  182.     ret = esp_ble_gatts_app_register(ESP_ESS_APP_ID);
  183.     if (ret){
  184.         ESP_LOGE(GATTS_TABLE_TAG, "gatts app register error, error code = %x", ret);
  185.         return;
  186.     }
  187. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top