2020-03-30 01:44:20 +02:00
|
|
|
/*
|
2024-12-13 03:21:03 +01:00
|
|
|
* Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> and
|
|
|
|
* esp-nimble-cpp, NimBLE-Arduino contributors.
|
2020-03-30 01:44:20 +02:00
|
|
|
*
|
2024-12-13 03:21:03 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2024-12-13 03:21:03 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-03-30 01:44:20 +02:00
|
|
|
*
|
2024-12-13 03:21:03 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
# include "NimBLEService.h"
|
2024-11-25 03:59:56 +01:00
|
|
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
# include "NimBLEExtAdvertising.h"
|
|
|
|
# else
|
|
|
|
# include "NimBLEAdvertising.h"
|
|
|
|
# endif
|
|
|
|
# include "NimBLEDevice.h"
|
2024-07-26 22:47:36 +02:00
|
|
|
# include "NimBLEUtils.h"
|
|
|
|
# include "NimBLELog.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
# include <string>
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
static const char* LOG_TAG = "NimBLEService";
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
2020-06-08 02:42:28 +02:00
|
|
|
* @brief Construct an instance of the NimBLEService
|
2020-03-30 01:44:20 +02:00
|
|
|
* @param [in] uuid The UUID of the service.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLEService::NimBLEService(const char* uuid) : NimBLEService(NimBLEUUID(uuid)) {}
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Construct an instance of the BLEService
|
|
|
|
* @param [in] uuid The UUID of the service.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLEService::NimBLEService(const NimBLEUUID& uuid)
|
|
|
|
: NimBLELocalAttribute{uuid, 0}, m_pSvcDef{{0, getUUID().getBase(), nullptr, nullptr},{}} {}
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
/**
|
|
|
|
* @brief Destructor, make sure we release the resources allocated for the service.
|
|
|
|
*/
|
2020-07-14 05:24:07 +02:00
|
|
|
NimBLEService::~NimBLEService() {
|
2024-07-26 22:47:36 +02:00
|
|
|
if (m_pSvcDef->characteristics) {
|
|
|
|
if (m_pSvcDef->characteristics->descriptors) {
|
|
|
|
delete[] m_pSvcDef->characteristics->descriptors;
|
2020-07-14 05:24:07 +02:00
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
delete[] m_pSvcDef->characteristics;
|
2020-07-14 05:24:07 +02:00
|
|
|
}
|
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
for (const auto& it : m_vChars) {
|
2020-07-14 05:24:07 +02:00
|
|
|
delete it;
|
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
} // ~NimBLEService
|
2020-07-14 05:24:07 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Dump details of this BLE GATT service.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
void NimBLEService::dump() const {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "Service: uuid:%s, handle: 0x%2x", getUUID().toString().c_str(), getHandle());
|
2020-06-08 02:42:28 +02:00
|
|
|
|
|
|
|
std::string res;
|
2024-07-26 22:47:36 +02:00
|
|
|
int count = 0;
|
|
|
|
char hex[5];
|
|
|
|
for (const auto& it : m_vChars) {
|
|
|
|
if (count > 0) {
|
|
|
|
res += "\n";
|
|
|
|
}
|
2020-06-08 02:42:28 +02:00
|
|
|
snprintf(hex, sizeof(hex), "%04x", it->getHandle());
|
|
|
|
count++;
|
|
|
|
res += "handle: 0x";
|
|
|
|
res += hex;
|
|
|
|
res += ", uuid: " + std::string(it->getUUID());
|
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Characteristics:\n%s", res.c_str());
|
2020-03-30 01:44:20 +02:00
|
|
|
} // dump
|
|
|
|
|
|
|
|
/**
|
2020-06-08 02:42:28 +02:00
|
|
|
* @brief Builds the database of characteristics/descriptors for the service
|
|
|
|
* and registers it with the NimBLE stack.
|
|
|
|
* @return bool success/failure .
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
bool NimBLEService::start() {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> start(): Starting service: %s", toString().c_str());
|
2024-12-20 20:16:27 +01:00
|
|
|
|
|
|
|
// If started previously and no characteristics have been added or removed,
|
|
|
|
// then we can skip the service registration process.
|
|
|
|
if (m_pSvcDef->characteristics && !getServer()->m_svcChanged) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
// If started previously, clear everything and start over
|
|
|
|
if (m_pSvcDef->characteristics) {
|
|
|
|
if (m_pSvcDef->characteristics->descriptors) {
|
|
|
|
delete[] m_pSvcDef->characteristics->descriptors;
|
|
|
|
}
|
|
|
|
delete[] m_pSvcDef->characteristics;
|
|
|
|
}
|
2021-07-31 04:56:52 +02:00
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
size_t numChrs = 0;
|
|
|
|
for (const auto& chr : m_vChars) {
|
|
|
|
if (chr->getRemoved()) {
|
|
|
|
continue;
|
2022-01-18 22:44:47 +01:00
|
|
|
}
|
2024-11-27 02:22:37 +01:00
|
|
|
++numChrs;
|
2021-07-31 04:56:52 +02:00
|
|
|
}
|
2020-07-14 05:24:07 +02:00
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Adding %d characteristics for service %s", numChrs, toString().c_str());
|
|
|
|
if (numChrs) {
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// Nimble requires the last characteristic to have it's uuid = 0 to indicate the end
|
|
|
|
// of the characteristics for the service. We create 1 extra and set it to null
|
|
|
|
// for this purpose.
|
|
|
|
ble_gatt_chr_def* pChrs = new ble_gatt_chr_def[numChrs + 1]{};
|
2024-07-26 22:47:36 +02:00
|
|
|
for (const auto& chr : m_vChars) {
|
|
|
|
if (chr->getRemoved()) {
|
2021-07-31 04:56:52 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-14 05:24:07 +02:00
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
size_t numDscs = 0;
|
|
|
|
for (const auto& dsc : chr->m_vDescriptors) {
|
|
|
|
if (dsc->getRemoved()) {
|
2021-07-31 04:56:52 +02:00
|
|
|
continue;
|
|
|
|
}
|
2024-11-27 02:22:37 +01:00
|
|
|
++numDscs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numDscs) {
|
|
|
|
int j = 0;
|
2020-07-14 05:24:07 +02:00
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
// Must have last descriptor uuid = 0 so we have to create 1 extra
|
|
|
|
ble_gatt_dsc_def* pDscs = new ble_gatt_dsc_def[numDscs + 1]{};
|
2024-07-26 22:47:36 +02:00
|
|
|
for (const auto& dsc : chr->m_vDescriptors) {
|
|
|
|
if (dsc->getRemoved()) {
|
2021-07-31 04:56:52 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-14 05:24:07 +02:00
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
pDscs[j].uuid = dsc->getUUID().getBase();
|
|
|
|
pDscs[j].att_flags = dsc->getProperties();
|
|
|
|
pDscs[j].min_key_size = 0;
|
|
|
|
pDscs[j].access_cb = NimBLEServer::handleGattEvent;
|
|
|
|
pDscs[j].arg = dsc;
|
|
|
|
++j;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
pChrs[i].descriptors = pDscs;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
pChrs[i].uuid = chr->getUUID().getBase();
|
|
|
|
pChrs[i].access_cb = NimBLEServer::handleGattEvent;
|
|
|
|
pChrs[i].arg = chr;
|
|
|
|
pChrs[i].flags = chr->getProperties();
|
|
|
|
pChrs[i].min_key_size = 0;
|
|
|
|
pChrs[i].val_handle = &chr->m_handle;
|
|
|
|
++i;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
2024-11-27 02:22:37 +01:00
|
|
|
|
|
|
|
m_pSvcDef->characteristics = pChrs;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
|
|
|
|
2024-11-27 02:22:37 +01:00
|
|
|
m_pSvcDef->type = BLE_GATT_SVC_TYPE_PRIMARY;
|
|
|
|
int rc = ble_gatts_count_cfg(m_pSvcDef);
|
2020-03-30 01:44:20 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "ble_gatts_count_cfg failed, rc= %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
rc = ble_gatts_add_svcs(m_pSvcDef);
|
2020-03-30 01:44:20 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "ble_gatts_add_svcs, rc= %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< start()");
|
2020-03-30 01:44:20 +02:00
|
|
|
return true;
|
|
|
|
} // start
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a new BLE Characteristic associated with this service.
|
|
|
|
* @param [in] uuid - The UUID of the characteristic.
|
|
|
|
* @param [in] properties - The properties of the characteristic.
|
2022-01-17 04:11:18 +01:00
|
|
|
* @param [in] max_len - The maximum length in bytes that the characteristic value can hold.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @return The new BLE characteristic.
|
|
|
|
*/
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLECharacteristic* NimBLEService::createCharacteristic(const char* uuid, uint32_t properties, uint16_t max_len) {
|
|
|
|
return createCharacteristic(NimBLEUUID(uuid), properties, max_len);
|
2024-07-26 22:47:36 +02:00
|
|
|
} // createCharacteristic
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a new BLE Characteristic associated with this service.
|
|
|
|
* @param [in] uuid - The UUID of the characteristic.
|
|
|
|
* @param [in] properties - The properties of the characteristic.
|
2022-01-17 04:11:18 +01:00
|
|
|
* @param [in] max_len - The maximum length in bytes that the characteristic value can hold.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @return The new BLE characteristic.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLECharacteristic* NimBLEService::createCharacteristic(const NimBLEUUID& uuid, uint32_t properties, uint16_t max_len) {
|
|
|
|
NimBLECharacteristic* pChar = new NimBLECharacteristic(uuid, properties, max_len, this);
|
2020-06-08 02:42:28 +02:00
|
|
|
if (getCharacteristic(uuid) != nullptr) {
|
2024-07-26 22:47:36 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Adding a duplicate characteristic with UUID: %s", std::string(uuid).c_str());
|
2020-06-08 02:42:28 +02:00
|
|
|
}
|
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
addCharacteristic(pChar);
|
|
|
|
return pChar;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // createCharacteristic
|
|
|
|
|
2021-07-31 04:56:52 +02:00
|
|
|
/**
|
|
|
|
* @brief Add a characteristic to the service.
|
2024-07-26 22:47:36 +02:00
|
|
|
* @param[in] pChar A pointer to the characteristic instance to add to the service.
|
2021-07-31 04:56:52 +02:00
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
void NimBLEService::addCharacteristic(NimBLECharacteristic* pChar) {
|
2021-07-31 04:56:52 +02:00
|
|
|
bool foundRemoved = false;
|
2024-07-26 22:47:36 +02:00
|
|
|
if (pChar->getRemoved() > 0) {
|
|
|
|
for (const auto& chr : m_vChars) {
|
|
|
|
if (chr == pChar) {
|
2021-07-31 04:56:52 +02:00
|
|
|
foundRemoved = true;
|
2024-07-26 22:47:36 +02:00
|
|
|
pChar->setRemoved(0);
|
2021-07-31 04:56:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
// Check if the characteristic is already in the service and if so, return.
|
|
|
|
for (const auto& chr : m_vChars) {
|
|
|
|
if (chr == pChar) {
|
|
|
|
pChar->setService(this); // Update the service pointer in the characteristic.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundRemoved) {
|
|
|
|
m_vChars.push_back(pChar);
|
2021-07-31 04:56:52 +02:00
|
|
|
}
|
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
pChar->setService(this);
|
2021-07-31 04:56:52 +02:00
|
|
|
getServer()->serviceChanged();
|
|
|
|
} // addCharacteristic
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove a characteristic from the service.
|
2024-07-26 22:47:36 +02:00
|
|
|
* @param[in] pChar A pointer to the characteristic instance to remove from the service.
|
2021-07-31 04:56:52 +02:00
|
|
|
* @param[in] deleteChr If true it will delete the characteristic instance and free it's resources.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
void NimBLEService::removeCharacteristic(NimBLECharacteristic* pChar, bool deleteChr) {
|
2021-07-31 04:56:52 +02:00
|
|
|
// Check if the characteristic was already removed and if so, check if this
|
|
|
|
// is being called to delete the object and do so if requested.
|
|
|
|
// Otherwise, ignore the call and return.
|
2024-07-26 22:47:36 +02:00
|
|
|
if (pChar->getRemoved() > 0) {
|
|
|
|
if (deleteChr) {
|
|
|
|
for (auto it = m_vChars.begin(); it != m_vChars.end(); ++it) {
|
|
|
|
if ((*it) == pChar) {
|
|
|
|
delete (*it);
|
|
|
|
m_vChars.erase(it);
|
2021-07-31 04:56:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
pChar->setRemoved(deleteChr ? NIMBLE_ATT_REMOVE_DELETE : NIMBLE_ATT_REMOVE_HIDE);
|
2021-07-31 04:56:52 +02:00
|
|
|
getServer()->serviceChanged();
|
|
|
|
} // removeCharacteristic
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief Get a pointer to the characteristic object with the specified UUID.
|
|
|
|
* @param [in] uuid The UUID of the characteristic.
|
2024-07-26 22:47:36 +02:00
|
|
|
* @param idx The index of the characteristic to return (used when multiple characteristics have the same UUID).
|
2020-07-09 03:27:26 +02:00
|
|
|
* @return A pointer to the characteristic object or nullptr if not found.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLECharacteristic* NimBLEService::getCharacteristic(const char* uuid, uint16_t idx) const {
|
|
|
|
return getCharacteristic(NimBLEUUID(uuid), idx);
|
|
|
|
} // getCharacteristic
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief Get a pointer to the characteristic object with the specified UUID.
|
|
|
|
* @param [in] uuid The UUID of the characteristic.
|
2024-07-26 22:47:36 +02:00
|
|
|
* @param idx The index of the characteristic to return (used when multiple characteristics have the same UUID).
|
2020-07-09 03:27:26 +02:00
|
|
|
* @return A pointer to the characteristic object or nullptr if not found.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLECharacteristic* NimBLEService::getCharacteristic(const NimBLEUUID& uuid, uint16_t idx) const {
|
2021-02-08 16:28:32 +01:00
|
|
|
uint16_t position = 0;
|
2024-07-26 22:47:36 +02:00
|
|
|
for (const auto& chr : m_vChars) {
|
|
|
|
if (chr->getUUID() == uuid) {
|
|
|
|
if (position == idx) {
|
|
|
|
return chr;
|
2021-02-08 16:28:32 +01:00
|
|
|
}
|
|
|
|
position++;
|
2020-06-08 02:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
|
2021-02-08 16:28:32 +01:00
|
|
|
return nullptr;
|
2024-07-26 22:47:36 +02:00
|
|
|
} // getCharacteristic
|
2020-06-08 02:42:28 +02:00
|
|
|
|
2021-02-08 16:28:32 +01:00
|
|
|
/**
|
|
|
|
* @brief Get a pointer to the characteristic object with the specified handle.
|
|
|
|
* @param handle The handle of the characteristic.
|
|
|
|
* @return A pointer to the characteristic object or nullptr if not found.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLECharacteristic* NimBLEService::getCharacteristicByHandle(uint16_t handle) const {
|
|
|
|
for (const auto& chr : m_vChars) {
|
|
|
|
if (chr->getHandle() == handle) {
|
|
|
|
return chr;
|
2021-02-08 16:28:32 +01:00
|
|
|
}
|
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
return nullptr;
|
2024-07-26 22:47:36 +02:00
|
|
|
} // getCharacteristicByHandle
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2021-02-08 16:28:32 +01:00
|
|
|
/**
|
|
|
|
* @return A vector containing pointers to each characteristic associated with this service.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
const std::vector<NimBLECharacteristic*>& NimBLEService::getCharacteristics() const {
|
|
|
|
return m_vChars;
|
|
|
|
} // getCharacteristics
|
2021-02-08 16:28:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return A vector containing pointers to each characteristic with the provided UUID associated with this service.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
std::vector<NimBLECharacteristic*> NimBLEService::getCharacteristics(const char* uuid) const {
|
2021-02-08 16:28:32 +01:00
|
|
|
return getCharacteristics(NimBLEUUID(uuid));
|
2024-07-26 22:47:36 +02:00
|
|
|
} // getCharacteristics
|
2021-02-08 16:28:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return A vector containing pointers to each characteristic with the provided UUID associated with this service.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
std::vector<NimBLECharacteristic*> NimBLEService::getCharacteristics(const NimBLEUUID& uuid) const {
|
2021-02-08 16:28:32 +01:00
|
|
|
std::vector<NimBLECharacteristic*> result;
|
2024-07-26 22:47:36 +02:00
|
|
|
for (const auto& chr : m_vChars) {
|
|
|
|
if (chr->getUUID() == uuid) {
|
|
|
|
result.push_back(chr);
|
2021-02-08 16:28:32 +01:00
|
|
|
}
|
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
|
2021-02-08 16:28:32 +01:00
|
|
|
return result;
|
2024-07-26 22:47:36 +02:00
|
|
|
} // getCharacteristics
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return a string representation of this service.
|
|
|
|
* A service is defined by:
|
|
|
|
* * Its UUID
|
|
|
|
* * Its handle
|
|
|
|
* @return A string representation of this service.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
std::string NimBLEService::toString() const {
|
2020-05-14 06:03:56 +02:00
|
|
|
std::string res = "UUID: " + getUUID().toString();
|
2024-07-26 22:47:36 +02:00
|
|
|
char hex[5];
|
2020-05-14 06:03:56 +02:00
|
|
|
snprintf(hex, sizeof(hex), "%04x", getHandle());
|
|
|
|
res += ", handle: 0x";
|
|
|
|
res += hex;
|
|
|
|
return res;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // toString
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the BLE server associated with this service.
|
|
|
|
* @return The BLEServer associated with this service.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLEServer* NimBLEService::getServer() const {
|
2021-12-29 16:12:07 +01:00
|
|
|
return NimBLEDevice::getServer();
|
2024-07-26 22:47:36 +02:00
|
|
|
} // getServer
|
2024-06-15 17:52:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Checks if the service has been started.
|
|
|
|
* @return True if the service has been started.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
bool NimBLEService::isStarted() const {
|
|
|
|
return m_pSvcDef->type > 0;
|
|
|
|
} // isStarted
|
2024-06-15 17:52:35 +02:00
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
|