Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/Migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,13 @@ The security callback methods are now incorporated in the `NimBLEServerCallbacks

The callback methods are:

> `bool onConfirmPIN(const NimBLEConnInfo& connInfo, uint32_t pin)`
> `bool onConfirmPIN(NimBLEConnInfo& connInfo, uint32_t pin)`

Receives the pin when using numeric comparison authentication.
Call `NimBLEDevice::injectConfirmPIN(connInfo, true);` to accept or `NimBLEDevice::injectConfirmPIN(connInfo, false);` to reject.
<br/>

> `void onPassKeyEntry(const NimBLEConnInfo& connInfo)`
> `void onPassKeyEntry(NimBLEConnInfo& connInfo)`

Client callback; client should respond with the passkey (pin) by calling `NimBLEDevice::injectPassKey(connInfo, 123456);`
<br/>
Expand All @@ -399,7 +399,7 @@ Client callback; client should respond with the passkey (pin) by calling `NimBLE
Server callback; should return the passkey (pin) expected from the client.
<br/>

> `void onAuthenticationComplete(const NimBLEConnInfo& connInfo)`
> `void onAuthenticationComplete(NimBLEConnInfo& connInfo)`

Authentication complete, success or failed information is available from the `NimBLEConnInfo` methods.
<br/>
Expand Down
8 changes: 4 additions & 4 deletions examples/NimBLE_Client/NimBLE_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ class ClientCallbacks : public NimBLEClientCallbacks {

/********************* Security handled here **********************
****** Note: these are the same return values as defaults ********/
void onPassKeyEntry(const NimBLEConnInfo& connInfo){
void onPassKeyEntry(NimBLEConnInfo& connInfo){
Serial.println("Server Passkey Entry");
/** This should prompt the user to enter the passkey displayed
* on the peer device.
*/
NimBLEDevice::injectPassKey(connInfo, 123456);
};

void onConfirmPIN(const NimBLEConnInfo& connInfo, uint32_t pass_key){
void onConfirmPIN(NimBLEConnInfo& connInfo, uint32_t pass_key){
Serial.print("The passkey YES/NO number: ");Serial.println(pass_key);
/** Inject false if passkeys don't match. */
NimBLEDevice::injectConfirmPIN(connInfo, true);
};

/** Pairing process complete, we can check the results in connInfo */
void onAuthenticationComplete(const NimBLEConnInfo& connInfo){
void onAuthenticationComplete(NimBLEConnInfo& connInfo){
if(!connInfo.isEncrypted()) {
Serial.println("Encrypt connection failed - disconnecting");
/** Find the client with the connection handle provided in desc */
Expand Down Expand Up @@ -112,7 +112,7 @@ void notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData,
std::string str = (isNotify == true) ? "Notification" : "Indication";
str += " from ";
/** NimBLEAddress and NimBLEUUID have std::string operators */
str += std::string(pRemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress());
str += std::string(pRemoteCharacteristic->getClient()->getPeerAddress());
str += ": Service = " + std::string(pRemoteCharacteristic->getRemoteService()->getUUID());
str += ", Characteristic = " + std::string(pRemoteCharacteristic->getUUID());
str += ", Value = " + std::string((char*)pData, length);
Expand Down
6 changes: 3 additions & 3 deletions examples/NimBLE_Server/NimBLE_Server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class ServerCallbacks: public NimBLEServerCallbacks {
return 123456;
};

void onConfirmPIN(const NimBLEConnInfo& connInfo, uint32_t pass_key) {
void onConfirmPIN(NimBLEConnInfo& connInfo, uint32_t pass_key) {
Serial.print("The passkey YES/NO number: ");Serial.println(pass_key);
/** Inject false if passkeys don't match. */
NimBLEDevice::injectConfirmPIN(connInfo, true);
};

void onAuthenticationComplete(const NimBLEConnInfo& connInfo) {
void onAuthenticationComplete(NimBLEConnInfo& connInfo) {
/** Check that encryption was successful, if not we disconnect the client */
if(!connInfo.isEncrypted()) {
NimBLEDevice::getServer()->disconnect(connInfo.getConnHandle());
Expand Down Expand Up @@ -241,7 +241,7 @@ void loop() {
if(pSvc) {
NimBLECharacteristic* pChr = pSvc->getCharacteristic("F00D");
if(pChr) {
pChr->notify(true);
pChr->notify();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ServerCallbacks : public NimBLEServerCallbacks {
}

// Same as before but now includes the name parameter
void onAuthenticationComplete(const NimBLEConnInfo& connInfo, const std::string& name) override {
void onAuthenticationComplete(NimBLEConnInfo& connInfo, const std::string& name) override {
if (!connInfo.isEncrypted()) {
NimBLEDevice::getServer()->disconnect(connInfo.getConnHandle());
Serial.println("Encrypt connection failed - disconnecting client");
Expand Down
19 changes: 11 additions & 8 deletions src/NimBLEAttValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,15 @@ class NimBLEAttValue {
return setValue(reinterpret_cast<const uint8_t*>(s), len);
}

/**
* @brief Get a pointer to the value buffer with timestamp.
* @param[in] timestamp A pointer to a time_t variable to store the timestamp.
* @returns A pointer to the internal value buffer.
*/
const uint8_t* getValue(time_t* timestamp = nullptr) const {
const NimBLEAttValue& getValue(time_t* timestamp = nullptr) const {
if (timestamp != nullptr) {
# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
*timestamp = m_timestamp;
# else
*timestamp = 0;
# endif
}
return m_attr_value;
return *this;
}

/**
Expand Down Expand Up @@ -271,7 +266,15 @@ class NimBLEAttValue {
if (!skipSizeCheck && size() < sizeof(T)) {
return T();
}
return *(reinterpret_cast<const T*>(getValue(timestamp)));
if (timestamp != nullptr) {
# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
*timestamp = m_timestamp;
# else
*timestamp = 0;
# endif
}

return *(reinterpret_cast<const T*>(m_attr_value));
}

/*********************** Operators ************************/
Expand Down
50 changes: 50 additions & 0 deletions src/NimBLEAttribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* NimBLEAttribute.h
*
* Created: on July 28 2024
* Author H2zero
*/

#ifndef NIMBLE_CPP_ATTRIBUTE_H_
#define NIMBLE_CPP_ATTRIBUTE_H_

#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && (defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) || defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL))

# include "NimBLEUUID.h"

/**
* @brief A base class for BLE attributes.
*/
class NimBLEAttribute {
public:
/**
* @brief Get the UUID of the attribute.
* @return The UUID.
*/
const NimBLEUUID& getUUID() const { return m_uuid; }

/**
* @brief Get the handle of the attribute.
*/
uint16_t getHandle() const { return m_handle; };

protected:
/**
* @brief Construct a new NimBLEAttribute object.
* @param [in] handle The handle of the attribute.
* @param [in] uuid The UUID of the attribute.
*/
NimBLEAttribute(const NimBLEUUID& uuid, uint16_t handle) : m_uuid{uuid}, m_handle{handle} {}

/**
* @brief Destroy the NimBLEAttribute object.
*/
~NimBLEAttribute() = default;

const NimBLEUUID m_uuid{};
uint16_t m_handle{0};
};

#endif // CONFIG_BT_ENABLED && (CONFIG_BT_NIMBLE_ROLE_PERIPHERAL || CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#endif // NIMBLE_CPP_ATTRIBUTE_H_
Loading
Loading