ESP32  1.0.4
BLEService クラス

The model of a BLE service. [詳解]

#include <BLEService.h>

公開メンバ関数

void addCharacteristic (BLECharacteristic *pCharacteristic)
 Add a characteristic to the service. [詳解]
 
BLECharacteristiccreateCharacteristic (const char *uuid, uint32_t properties)
 Create a new BLE Characteristic associated with this service. [詳解]
 
BLECharacteristiccreateCharacteristic (BLEUUID uuid, uint32_t properties)
 Create a new BLE Characteristic associated with this service. [詳解]
 
void dump ()
 Dump details of this BLE GATT service. [詳解]
 
void executeCreate (BLEServer *pServer)
 Create the service. Create the service. [詳解]
 
void executeDelete ()
 Delete the service. Delete the service. [詳解]
 
BLECharacteristicgetCharacteristic (const char *uuid)
 
BLECharacteristicgetCharacteristic (BLEUUID uuid)
 
BLEUUID getUUID ()
 Get the UUID of the service. [詳解]
 
BLEServergetServer ()
 Get the BLE server associated with this service. [詳解]
 
void start ()
 Start the service. Here we wish to start the service which means that we will respond to partner requests about it. Starting a service also means that we can create the corresponding characteristics. [詳解]
 
void stop ()
 Stop the service. [詳解]
 
std::string toString ()
 Return a string representation of this service. A service is defined by: [詳解]
 
uint16_t getHandle ()
 Get the handle associated with this service. [詳解]
 

公開変数類

uint8_t m_instId = 0
 

フレンド

class BLEServer
 
class BLEServiceMap
 
class BLEDescriptor
 
class BLECharacteristic
 
class BLEDevice
 

詳解

The model of a BLE service.

BLEService.h49 行目に定義があります。

関数詳解

◆ addCharacteristic()

void BLEService::addCharacteristic ( BLECharacteristic pCharacteristic)

Add a characteristic to the service.

引数
[in]pCharacteristicA pointer to the characteristic to be added.

BLEService.cpp217 行目に定義があります。

217  {
218  // We maintain a mapping of characteristics owned by this service. These are managed by the
219  // BLECharacteristicMap class instance found in m_characteristicMap. We add the characteristic
220  // to the map and then ask the service to add the characteristic at the BLE level (ESP-IDF).
221 
222  log_v(">> addCharacteristic()");
223  log_d("Adding characteristic: uuid=%s to service: %s",
224  pCharacteristic->getUUID().toString().c_str(),
225  toString().c_str());
226 
227  // Check that we don't add the same characteristic twice.
228  if (m_characteristicMap.getByUUID(pCharacteristic->getUUID()) != nullptr) {
229  log_w("<< Adding a new characteristic with the same UUID as a previous one");
230  //return;
231  }
232 
233  // Remember this characteristic in our map of characteristics. At this point, we can lookup by UUID
234  // but not by handle. The handle is allocated to us on the ESP_GATTS_ADD_CHAR_EVT.
235  m_characteristicMap.setByUUID(pCharacteristic, pCharacteristic->getUUID());
236 
237  log_v("<< addCharacteristic()");
238 } // addCharacteristic
BLEUUID getUUID()
Get the UUID of the characteristic.
#define log_w(format,...)
#define log_d(format,...)
Definition: esp32-hal-log.h:92
std::string toString()
Return a string representation of this service. A service is defined by:
Definition: BLEService.cpp:383
BLECharacteristic * getByUUID(const char *uuid)
Return the characteristic by UUID.
void setByUUID(BLECharacteristic *pCharacteristic, const char *uuid)
std::string toString()
Get a string representation of the UUID.
Definition: BLEUUID.cpp:350
#define log_v(format,...)
Definition: esp32-hal-log.h:84

◆ createCharacteristic() [1/2]

BLECharacteristic * BLEService::createCharacteristic ( const char *  uuid,
uint32_t  properties 
)

Create a new BLE Characteristic associated with this service.

引数
[in]uuid- The UUID of the characteristic.
[in]properties- The properties of the characteristic.
戻り値
The new BLE characteristic.

BLEService.cpp247 行目に定義があります。

247  {
248  return createCharacteristic(BLEUUID(uuid), properties);
249 }
A model of a BLE UUID.
Definition: BLEUUID.h:18
BLECharacteristic * createCharacteristic(const char *uuid, uint32_t properties)
Create a new BLE Characteristic associated with this service.
Definition: BLEService.cpp:247

◆ createCharacteristic() [2/2]

BLECharacteristic * BLEService::createCharacteristic ( BLEUUID  uuid,
uint32_t  properties 
)

Create a new BLE Characteristic associated with this service.

引数
[in]uuid- The UUID of the characteristic.
[in]properties- The properties of the characteristic.
戻り値
The new BLE characteristic.

BLEService.cpp258 行目に定義があります。

258  {
259  BLECharacteristic* pCharacteristic = new BLECharacteristic(uuid, properties);
260  addCharacteristic(pCharacteristic);
261  return pCharacteristic;
262 } // createCharacteristic
friend class BLECharacteristic
Definition: BLEService.h:73
void addCharacteristic(BLECharacteristic *pCharacteristic)
Add a characteristic to the service.
Definition: BLEService.cpp:217
The model of a BLE Characteristic.

◆ dump()

void BLEService::dump ( )

Dump details of this BLE GATT service.

戻り値
N/A.

BLEService.cpp106 行目に定義があります。

106  {
107  log_d("Service: uuid:%s, handle: 0x%.2x",
108  m_uuid.toString().c_str(),
109  m_handle);
110  log_d("Characteristics:\n%s", m_characteristicMap.toString().c_str());
111 } // dump
#define log_d(format,...)
Definition: esp32-hal-log.h:92
std::string toString()
Return a string representation of the characteristic map.
std::string toString()
Get a string representation of the UUID.
Definition: BLEUUID.cpp:350

◆ executeCreate()

void BLEService::executeCreate ( BLEServer pServer)

Create the service. Create the service.

引数
[in]gatts_ifThe handle of the GATT server interface.
戻り値
N/A.

BLEService.cpp59 行目に定義があります。

59  {
60  log_v(">> executeCreate() - Creating service (esp_ble_gatts_create_service) service uuid: %s", getUUID().toString().c_str());
61  m_pServer = pServer;
62  m_semaphoreCreateEvt.take("executeCreate"); // Take the mutex and release at event ESP_GATTS_CREATE_EVT
63 
64  esp_gatt_srvc_id_t srvc_id;
65  srvc_id.is_primary = true;
66  srvc_id.id.inst_id = m_instId;
67  srvc_id.id.uuid = *m_uuid.getNative();
68  esp_err_t errRc = ::esp_ble_gatts_create_service(getServer()->getGattsIf(), &srvc_id, m_numHandles); // The maximum number of handles associated with the service.
69 
70  if (errRc != ESP_OK) {
71  log_e("esp_ble_gatts_create_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
72  return;
73  }
74 
75  m_semaphoreCreateEvt.wait("executeCreate");
76  log_v("<< executeCreate");
77 } // executeCreate
#define ESP_OK
Definition: esp_err.h:27
esp_bt_uuid_t * getNative()
Get the native UUID value.
Definition: BLEUUID.cpp:273
esp_err_t esp_ble_gatts_create_service(esp_gatt_if_t gatts_if, esp_gatt_srvc_id_t *service_id, uint16_t num_handle)
Create a service. When service creation is done, a callback event BTA_GATTS_CREATE_SRVC_EVT is called...
std::string toString()
Return a string representation of this service. A service is defined by:
Definition: BLEService.cpp:383
static const char * errorToString(esp_err_t errCode)
Convert an ESP error code to a string.
uint32_t wait(std::string owner="<Unknown>")
Wait for a semaphore to be released by trying to take it and then releasing it again.
Definition: FreeRTOS.cpp:62
BLEUUID getUUID()
Get the UUID of the service.
Definition: BLEService.cpp:118
#define log_e(format,...)
BLEServer * getServer()
Get the BLE server associated with this service.
Definition: BLEService.cpp:409
int32_t esp_err_t
Definition: esp_err.h:24
#define log_v(format,...)
Definition: esp32-hal-log.h:84
uint8_t m_instId
Definition: BLEService.h:65
bool take(std::string owner="<Unknown>")
Take a semaphore. Take a semaphore and wait indefinitely.
Definition: FreeRTOS.cpp:188

◆ executeDelete()

void BLEService::executeDelete ( )

Delete the service. Delete the service.

戻り値
N/A.

BLEService.cpp86 行目に定義があります。

86  {
87  log_v(">> executeDelete()");
88  m_semaphoreDeleteEvt.take("executeDelete"); // Take the mutex and release at event ESP_GATTS_DELETE_EVT
89 
91 
92  if (errRc != ESP_OK) {
93  log_e("esp_ble_gatts_delete_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
94  return;
95  }
96 
97  m_semaphoreDeleteEvt.wait("executeDelete");
98  log_v("<< executeDelete");
99 } // executeDelete
#define ESP_OK
Definition: esp_err.h:27
uint16_t getHandle()
Get the handle associated with this service.
Definition: BLEService.cpp:208
static const char * errorToString(esp_err_t errCode)
Convert an ESP error code to a string.
uint32_t wait(std::string owner="<Unknown>")
Wait for a semaphore to be released by trying to take it and then releasing it again.
Definition: FreeRTOS.cpp:62
#define log_e(format,...)
int32_t esp_err_t
Definition: esp_err.h:24
esp_err_t esp_ble_gatts_delete_service(uint16_t service_handle)
This function is called to delete a service. When this is done, a callback event BTA_GATTS_DELETE_EVT...
#define log_v(format,...)
Definition: esp32-hal-log.h:84
bool take(std::string owner="<Unknown>")
Take a semaphore. Take a semaphore and wait indefinitely.
Definition: FreeRTOS.cpp:188

◆ getCharacteristic() [1/2]

BLECharacteristic * BLEService::getCharacteristic ( const char *  uuid)

BLEService.cpp366 行目に定義があります。

366  {
367  return getCharacteristic(BLEUUID(uuid));
368 }
A model of a BLE UUID.
Definition: BLEUUID.h:18
BLECharacteristic * getCharacteristic(const char *uuid)
Definition: BLEService.cpp:366

◆ getCharacteristic() [2/2]

BLECharacteristic * BLEService::getCharacteristic ( BLEUUID  uuid)

BLEService.cpp371 行目に定義があります。

371  {
372  return m_characteristicMap.getByUUID(uuid);
373 }
BLECharacteristic * getByUUID(const char *uuid)
Return the characteristic by UUID.

◆ getHandle()

uint16_t BLEService::getHandle ( )

Get the handle associated with this service.

戻り値
The handle associated with this service.

BLEService.cpp208 行目に定義があります。

208  {
209  return m_handle;
210 } // getHandle

◆ getServer()

BLEServer * BLEService::getServer ( )

Get the BLE server associated with this service.

戻り値
The BLEServer associated with this service.

BLEService.cpp409 行目に定義があります。

409  {
410  return m_pServer;
411 } // getServer

◆ getUUID()

BLEUUID BLEService::getUUID ( void  )

Get the UUID of the service.

戻り値
the UUID of the service.

BLEService.cpp118 行目に定義があります。

118  {
119  return m_uuid;
120 } // getUUID

◆ start()

void BLEService::start ( )

Start the service. Here we wish to start the service which means that we will respond to partner requests about it. Starting a service also means that we can create the corresponding characteristics.

戻り値
Start the service.

BLEService.cpp129 行目に定義があります。

129  {
130 // We ask the BLE runtime to start the service and then create each of the characteristics.
131 // We start the service through its local handle which was returned in the ESP_GATTS_CREATE_EVT event
132 // obtained as a result of calling esp_ble_gatts_create_service().
133 //
134  log_v(">> start(): Starting service (esp_ble_gatts_start_service): %s", toString().c_str());
135  if (m_handle == NULL_HANDLE) {
136  log_e("<< !!! We attempted to start a service but don't know its handle!");
137  return;
138  }
139 
140  BLECharacteristic *pCharacteristic = m_characteristicMap.getFirst();
141 
142  while (pCharacteristic != nullptr) {
143  m_lastCreatedCharacteristic = pCharacteristic;
144  pCharacteristic->executeCreate(this);
145 
146  pCharacteristic = m_characteristicMap.getNext();
147  }
148  // Start each of the characteristics ... these are found in the m_characteristicMap.
149 
150  m_semaphoreStartEvt.take("start");
151  esp_err_t errRc = ::esp_ble_gatts_start_service(m_handle);
152 
153  if (errRc != ESP_OK) {
154  log_e("<< esp_ble_gatts_start_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
155  return;
156  }
157  m_semaphoreStartEvt.wait("start");
158 
159  log_v("<< start()");
160 } // start
#define ESP_OK
Definition: esp_err.h:27
BLECharacteristic * getNext()
Get the next characteristic in the map.
The model of a BLE Characteristic.
std::string toString()
Return a string representation of this service. A service is defined by:
Definition: BLEService.cpp:383
static const char * errorToString(esp_err_t errCode)
Convert an ESP error code to a string.
uint32_t wait(std::string owner="<Unknown>")
Wait for a semaphore to be released by trying to take it and then releasing it again.
Definition: FreeRTOS.cpp:62
#define log_e(format,...)
esp_err_t esp_ble_gatts_start_service(uint16_t service_handle)
This function is called to start a service.
BLECharacteristic * getFirst()
Get the first characteristic in the map.
int32_t esp_err_t
Definition: esp_err.h:24
#define log_v(format,...)
Definition: esp32-hal-log.h:84
bool take(std::string owner="<Unknown>")
Take a semaphore. Take a semaphore and wait indefinitely.
Definition: FreeRTOS.cpp:188
#define NULL_HANDLE
Definition: BLEService.cpp:25

◆ stop()

void BLEService::stop ( )

Stop the service.

BLEService.cpp166 行目に定義があります。

166  {
167 // We ask the BLE runtime to start the service and then create each of the characteristics.
168 // We start the service through its local handle which was returned in the ESP_GATTS_CREATE_EVT event
169 // obtained as a result of calling esp_ble_gatts_create_service().
170  log_v(">> stop(): Stopping service (esp_ble_gatts_stop_service): %s", toString().c_str());
171  if (m_handle == NULL_HANDLE) {
172  log_e("<< !!! We attempted to stop a service but don't know its handle!");
173  return;
174  }
175 
176  m_semaphoreStopEvt.take("stop");
177  esp_err_t errRc = ::esp_ble_gatts_stop_service(m_handle);
178 
179  if (errRc != ESP_OK) {
180  log_e("<< esp_ble_gatts_stop_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
181  return;
182  }
183  m_semaphoreStopEvt.wait("stop");
184 
185  log_v("<< stop()");
186 } // start
#define ESP_OK
Definition: esp_err.h:27
std::string toString()
Return a string representation of this service. A service is defined by:
Definition: BLEService.cpp:383
esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle)
This function is called to stop a service.
static const char * errorToString(esp_err_t errCode)
Convert an ESP error code to a string.
uint32_t wait(std::string owner="<Unknown>")
Wait for a semaphore to be released by trying to take it and then releasing it again.
Definition: FreeRTOS.cpp:62
#define log_e(format,...)
int32_t esp_err_t
Definition: esp_err.h:24
#define log_v(format,...)
Definition: esp32-hal-log.h:84
bool take(std::string owner="<Unknown>")
Take a semaphore. Take a semaphore and wait indefinitely.
Definition: FreeRTOS.cpp:188
#define NULL_HANDLE
Definition: BLEService.cpp:25

◆ toString()

std::string BLEService::toString ( void  )

Return a string representation of this service. A service is defined by:

  • Its UUID
  • Its handle
    戻り値
    A string representation of this service.

BLEService.cpp383 行目に定義があります。

383  {
384  std::string res = "UUID: " + getUUID().toString();
385  char hex[5];
386  snprintf(hex, sizeof(hex), "%04x", getHandle());
387  res += ", handle: 0x";
388  res += hex;
389  return res;
390 } // toString
const char *const string
Definition: cJSON.h:170
uint16_t getHandle()
Get the handle associated with this service.
Definition: BLEService.cpp:208
BLEUUID getUUID()
Get the UUID of the service.
Definition: BLEService.cpp:118
std::string toString()
Get a string representation of the UUID.
Definition: BLEUUID.cpp:350

フレンドと関連関数の詳解

◆ BLECharacteristic

friend class BLECharacteristic
friend

BLEService.h73 行目に定義があります。

◆ BLEDescriptor

friend class BLEDescriptor
friend

BLEService.h72 行目に定義があります。

◆ BLEDevice

friend class BLEDevice
friend

BLEService.h74 行目に定義があります。

◆ BLEServer

friend class BLEServer
friend

BLEService.h70 行目に定義があります。

◆ BLEServiceMap

friend class BLEServiceMap
friend

BLEService.h71 行目に定義があります。

メンバ詳解

◆ m_instId

uint8_t BLEService::m_instId = 0

BLEService.h65 行目に定義があります。


このクラス詳解は次のファイルから抽出されました: