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"
|
|
|
|
# include "NimBLEDescriptor.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 = "NimBLEDescriptor";
|
2020-03-30 01:44:20 +02:00
|
|
|
static NimBLEDescriptorCallbacks defaultCallbacks;
|
|
|
|
|
|
|
|
/**
|
2022-01-17 04:11:18 +01:00
|
|
|
* @brief Construct a descriptor
|
|
|
|
* @param [in] uuid - UUID (const char*) for the descriptor.
|
|
|
|
* @param [in] properties - Properties for the descriptor.
|
|
|
|
* @param [in] max_len - The maximum length in bytes that the descriptor value can hold. (Default: 512 bytes for esp32, 20 for all others).
|
|
|
|
* @param [in] pCharacteristic - pointer to the characteristic instance this descriptor belongs to.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLEDescriptor::NimBLEDescriptor(const char* uuid, uint16_t properties, uint16_t max_len, NimBLECharacteristic* pCharacteristic)
|
|
|
|
: NimBLEDescriptor(NimBLEUUID(uuid), properties, max_len, pCharacteristic) {}
|
2021-05-23 21:07:00 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2022-01-17 04:11:18 +01:00
|
|
|
* @brief Construct a descriptor
|
|
|
|
* @param [in] uuid - UUID (const char*) for the descriptor.
|
|
|
|
* @param [in] properties - Properties for the descriptor.
|
|
|
|
* @param [in] max_len - The maximum length in bytes that the descriptor value can hold. (Default: 512 bytes for esp32, 20 for all others).
|
|
|
|
* @param [in] pCharacteristic - pointer to the characteristic instance this descriptor belongs to.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLEDescriptor::NimBLEDescriptor(const NimBLEUUID& uuid, uint16_t properties, uint16_t max_len, NimBLECharacteristic* pCharacteristic)
|
|
|
|
: NimBLELocalValueAttribute{uuid, 0, max_len}, m_pCallbacks{&defaultCallbacks}, m_pCharacteristic{pCharacteristic} {
|
2024-07-03 22:03:12 +02:00
|
|
|
// Check if this is the client configuration descriptor and set to removed if true.
|
|
|
|
if (uuid == NimBLEUUID((uint16_t)0x2902)) {
|
|
|
|
NIMBLE_LOGW(LOG_TAG, "Manually created 2902 descriptor has no functionality; please remove.");
|
2024-07-26 22:47:36 +02:00
|
|
|
setRemoved(NIMBLE_ATT_REMOVE_HIDE);
|
2024-07-03 22:03:12 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
// convert uint16_t properties to uint8_t for descriptor properties
|
|
|
|
uint8_t descProperties = 0;
|
|
|
|
if (properties & NIMBLE_PROPERTY::READ) {
|
|
|
|
descProperties |= BLE_ATT_F_READ;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
if (properties & (NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::WRITE)) {
|
|
|
|
descProperties |= BLE_ATT_F_WRITE;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
if (properties & NIMBLE_PROPERTY::READ_ENC) {
|
|
|
|
descProperties |= BLE_ATT_F_READ_ENC;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
if (properties & NIMBLE_PROPERTY::READ_AUTHEN) {
|
|
|
|
descProperties |= BLE_ATT_F_READ_AUTHEN;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
if (properties & NIMBLE_PROPERTY::READ_AUTHOR) {
|
|
|
|
descProperties |= BLE_ATT_F_READ_AUTHOR;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
if (properties & NIMBLE_PROPERTY::WRITE_ENC) {
|
|
|
|
descProperties |= BLE_ATT_F_WRITE_ENC;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
if (properties & NIMBLE_PROPERTY::WRITE_AUTHEN) {
|
|
|
|
descProperties |= BLE_ATT_F_WRITE_AUTHEN;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2024-07-26 22:47:36 +02:00
|
|
|
if (properties & NIMBLE_PROPERTY::WRITE_AUTHOR) {
|
|
|
|
descProperties |= BLE_ATT_F_WRITE_AUTHOR;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
setProperties(descProperties);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // NimBLEDescriptor
|
|
|
|
|
2021-05-23 21:07:00 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the characteristic this descriptor belongs to.
|
|
|
|
* @return A pointer to the characteristic this descriptor belongs to.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
NimBLECharacteristic* NimBLEDescriptor::getCharacteristic() const {
|
2021-05-23 21:07:00 +02:00
|
|
|
return m_pCharacteristic;
|
|
|
|
} // getCharacteristic
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the callback handlers for this descriptor.
|
|
|
|
* @param [in] pCallbacks An instance of a callback structure used to define any callbacks for the descriptor.
|
|
|
|
*/
|
|
|
|
void NimBLEDescriptor::setCallbacks(NimBLEDescriptorCallbacks* pCallbacks) {
|
2024-07-26 22:47:36 +02:00
|
|
|
if (pCallbacks != nullptr) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_pCallbacks = pCallbacks;
|
|
|
|
} else {
|
|
|
|
m_pCallbacks = &defaultCallbacks;
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setCallbacks
|
|
|
|
|
2021-05-23 21:07:00 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the characteristic this descriptor belongs to.
|
2022-07-31 19:00:12 +02:00
|
|
|
* @param [in] pChar A pointer to the characteristic this descriptor belongs to.
|
2021-05-23 21:07:00 +02:00
|
|
|
*/
|
|
|
|
void NimBLEDescriptor::setCharacteristic(NimBLECharacteristic* pChar) {
|
|
|
|
m_pCharacteristic = pChar;
|
|
|
|
} // setCharacteristic
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Return a string representation of the descriptor.
|
|
|
|
* @return A string representation of the descriptor.
|
|
|
|
*/
|
2024-07-26 22:47:36 +02:00
|
|
|
std::string NimBLEDescriptor::toString() const {
|
2020-05-14 06:03:56 +02:00
|
|
|
char hex[5];
|
2024-07-26 22:47:36 +02:00
|
|
|
snprintf(hex, sizeof(hex), "%04x", getHandle());
|
2020-05-14 06:03:56 +02:00
|
|
|
std::string res = "UUID: " + m_uuid.toString() + ", handle: 0x" + hex;
|
|
|
|
return res;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // toString
|
|
|
|
|
2024-07-26 22:47:36 +02:00
|
|
|
void NimBLEDescriptor::readEvent(NimBLEConnInfo& connInfo) {
|
|
|
|
m_pCallbacks->onRead(this, connInfo);
|
|
|
|
} // readEvent
|
|
|
|
|
|
|
|
void NimBLEDescriptor::writeEvent(const uint8_t* val, uint16_t len, NimBLEConnInfo& connInfo) {
|
|
|
|
setValue(val, len);
|
|
|
|
m_pCallbacks->onWrite(this, connInfo);
|
|
|
|
} // writeEvent
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback function to support a read request.
|
|
|
|
* @param [in] pDescriptor The descriptor that is the source of the event.
|
2022-08-27 20:38:53 +02:00
|
|
|
* @param [in] connInfo A reference to a NimBLEConnInfo instance containing the peer info.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2022-08-27 16:28:15 +02:00
|
|
|
void NimBLEDescriptorCallbacks::onRead(NimBLEDescriptor* pDescriptor, NimBLEConnInfo& connInfo) {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onRead: default");
|
2020-03-30 01:44:20 +02:00
|
|
|
} // onRead
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback function to support a write request.
|
|
|
|
* @param [in] pDescriptor The descriptor that is the source of the event.
|
2022-08-27 20:38:53 +02:00
|
|
|
* @param [in] connInfo A reference to a NimBLEConnInfo instance containing the peer info.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2022-08-27 16:28:15 +02:00
|
|
|
void NimBLEDescriptorCallbacks::onWrite(NimBLEDescriptor* pDescriptor, NimBLEConnInfo& connInfo) {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onWrite: default");
|
2020-03-30 01:44:20 +02:00
|
|
|
} // onWrite
|
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
|