Skip to content

Commit c547733

Browse files
committed
Remove NimBLEServer::getPeerNameOnConnect
With the implementation of the NimBLEServer::getClient function this is now redundant.
1 parent ac3d357 commit c547733

File tree

2 files changed

+12
-157
lines changed

2 files changed

+12
-157
lines changed

src/NimBLEServer.cpp

Lines changed: 4 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ static NimBLEServerCallbacks defaultCallbacks;
4545
*/
4646
NimBLEServer::NimBLEServer()
4747
: m_gattsStarted{false},
48-
m_getPeerNameOnConnect{false},
4948
m_svcChanged{false},
5049
m_deleteCallbacks{false},
5150
# if !CONFIG_BT_NIMBLE_EXT_ADV
@@ -257,15 +256,6 @@ void NimBLEServer::advertiseOnDisconnect(bool enable) {
257256
} // advertiseOnDisconnect
258257
# endif
259258

260-
/**
261-
* @brief Set the server to automatically read the name from the connected peer before
262-
* the onConnect callback is called and enables the override callback with name parameter.
263-
* @param [in] enable Enable reading the connected peer name upon connection.
264-
*/
265-
void NimBLEServer::getPeerNameOnConnect(bool enable) {
266-
m_getPeerNameOnConnect = enable;
267-
} // getPeerNameOnConnect
268-
269259
/**
270260
* @brief Return the number of connected clients.
271261
* @return The number of connected clients.
@@ -348,100 +338,6 @@ NimBLEConnInfo NimBLEServer::getPeerInfoByHandle(uint16_t connHandle) const {
348338
return peerInfo;
349339
} // getPeerIDInfo
350340

351-
/**
352-
* @brief Callback that is called after reading from the peer name characteristic.
353-
* @details This will check the task pointer in the task data struct to determine
354-
* the action to take once the name has been read. If there is a task waiting then
355-
* it will be resumed, if not, the the RC value is checked to determine which callback
356-
* should be called.
357-
*/
358-
int NimBLEServer::peerNameCB(uint16_t connHandle, const ble_gatt_error* error, ble_gatt_attr* attr, void* arg) {
359-
NimBLETaskData* pTaskData = (NimBLETaskData*)arg;
360-
std::string* name = (std::string*)pTaskData->m_pBuf;
361-
int rc = error->status;
362-
363-
if (rc == 0) {
364-
if (attr) {
365-
name->append(OS_MBUF_DATA(attr->om, char*), OS_MBUF_PKTLEN(attr->om));
366-
return rc;
367-
}
368-
}
369-
370-
if (rc == BLE_HS_EDONE) {
371-
if (pTaskData->m_flags != -1) {
372-
NimBLEServer* pServer = (NimBLEServer*)pTaskData->m_pInstance;
373-
NimBLEConnInfo peerInfo{};
374-
ble_gap_conn_find(connHandle, &peerInfo.m_desc);
375-
376-
// check the flag to indicate which callback should be called.
377-
if (pTaskData->m_flags == NIMBLE_SERVER_GET_PEER_NAME_ON_CONNECT_CB) {
378-
pServer->m_pServerCallbacks->onConnect(pServer, peerInfo, *name);
379-
} else if (pTaskData->m_flags == NIMBLE_SERVER_GET_PEER_NAME_ON_AUTH_CB) {
380-
pServer->m_pServerCallbacks->onAuthenticationComplete(peerInfo, *name);
381-
}
382-
}
383-
} else {
384-
NIMBLE_LOGE(LOG_TAG, "NimBLEServerPeerNameCB rc=%d; %s", rc, NimBLEUtils::returnCodeToString(rc));
385-
}
386-
387-
if (pTaskData->m_flags == -1) {
388-
NimBLEUtils::taskRelease(*pTaskData, rc);
389-
} else {
390-
// If the read was triggered for callback use then these were allocated.
391-
delete name;
392-
delete pTaskData;
393-
}
394-
395-
return rc;
396-
}
397-
398-
/**
399-
* @brief Implementation of the function that sends the read command.
400-
*/
401-
std::string NimBLEServer::getPeerNameImpl(uint16_t connHandle, int cbType) const {
402-
std::string* buf = new std::string{};
403-
NimBLETaskData* pTaskData = new NimBLETaskData(const_cast<NimBLEServer*>(this), cbType, buf);
404-
ble_uuid16_t uuid{{BLE_UUID_TYPE_16}, BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME};
405-
int rc = ble_gattc_read_by_uuid(connHandle, 1, 0xffff, &uuid.u, NimBLEServer::peerNameCB, pTaskData);
406-
if (rc != 0) {
407-
NIMBLE_LOGE(LOG_TAG, "ble_gattc_read_by_uuid rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
408-
NimBLEConnInfo peerInfo{};
409-
ble_gap_conn_find(connHandle, &peerInfo.m_desc);
410-
if (cbType == NIMBLE_SERVER_GET_PEER_NAME_ON_CONNECT_CB) {
411-
m_pServerCallbacks->onConnect(const_cast<NimBLEServer*>(this), peerInfo, *buf);
412-
} else if (cbType == NIMBLE_SERVER_GET_PEER_NAME_ON_AUTH_CB) {
413-
m_pServerCallbacks->onAuthenticationComplete(peerInfo, *buf);
414-
}
415-
delete buf;
416-
delete pTaskData;
417-
} else if (cbType == -1) {
418-
NimBLEUtils::taskWait(*pTaskData, BLE_NPL_TIME_FOREVER);
419-
rc = pTaskData->m_flags;
420-
std::string name{*(std::string*)pTaskData->m_pBuf};
421-
delete buf;
422-
delete pTaskData;
423-
424-
if (rc != 0 && rc != BLE_HS_EDONE) {
425-
NIMBLE_LOGE(LOG_TAG, "getPeerName rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
426-
}
427-
428-
return name;
429-
}
430-
// TaskData and name buffer will be deleted in the callback.
431-
return "";
432-
}
433-
434-
/**
435-
* @brief Get the name of the connected peer.
436-
* @param connInfo A reference to a NimBLEConnInfo instance to read the name from.
437-
* @returns A string containing the name.
438-
* @note This is a blocking call and should NOT be called from any callbacks!
439-
*/
440-
std::string NimBLEServer::getPeerName(const NimBLEConnInfo& connInfo) const {
441-
std::string name = getPeerNameImpl(connInfo.getConnHandle());
442-
return name;
443-
}
444-
445341
/**
446342
* @brief Gap event handler.
447343
*/
@@ -473,11 +369,7 @@ int NimBLEServer::handleGapEvent(ble_gap_event* event, void* arg) {
473369
}
474370
}
475371

476-
if (pServer->m_getPeerNameOnConnect) {
477-
pServer->getPeerNameImpl(event->connect.conn_handle, NIMBLE_SERVER_GET_PEER_NAME_ON_CONNECT_CB);
478-
} else {
479-
pServer->m_pServerCallbacks->onConnect(pServer, peerInfo);
480-
}
372+
pServer->m_pServerCallbacks->onConnect(pServer, peerInfo);
481373
}
482374

483375
break;
@@ -632,12 +524,7 @@ int NimBLEServer::handleGapEvent(ble_gap_event* event, void* arg) {
632524
return BLE_ATT_ERR_INVALID_HANDLE;
633525
}
634526

635-
if (pServer->m_getPeerNameOnConnect) {
636-
pServer->getPeerNameImpl(event->enc_change.conn_handle, NIMBLE_SERVER_GET_PEER_NAME_ON_AUTH_CB);
637-
} else {
638-
pServer->m_pServerCallbacks->onAuthenticationComplete(peerInfo);
639-
}
640-
527+
pServer->m_pServerCallbacks->onAuthenticationComplete(peerInfo);
641528
break;
642529
} // BLE_GAP_EVENT_ENC_CHANGE
643530

@@ -1095,10 +982,6 @@ void NimBLEServerCallbacks::onConnect(NimBLEServer* pServer, NimBLEConnInfo& con
1095982
NIMBLE_LOGD("NimBLEServerCallbacks", "onConnect(): Default");
1096983
} // onConnect
1097984

1098-
void NimBLEServerCallbacks::onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, std::string& name) {
1099-
NIMBLE_LOGD("NimBLEServerCallbacks", "onConnect(): Default");
1100-
} // onConnect
1101-
1102985
void NimBLEServerCallbacks::onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) {
1103986
NIMBLE_LOGD("NimBLEServerCallbacks", "onDisconnect(): Default");
1104987
} // onDisconnect
@@ -1113,9 +996,9 @@ uint32_t NimBLEServerCallbacks::onPassKeyDisplay() {
1113996
} // onPassKeyDisplay
1114997

1115998
void NimBLEServerCallbacks::onConfirmPassKey(NimBLEConnInfo& connInfo, uint32_t pin) {
1116-
NIMBLE_LOGD("NimBLEServerCallbacks", "onConfirmPIN: default: true");
999+
NIMBLE_LOGD("NimBLEServerCallbacks", "onConfirmPasskey: default: true");
11171000
NimBLEDevice::injectConfirmPasskey(connInfo, true);
1118-
} // onConfirmPIN
1001+
} // onConfirmPasskey
11191002

11201003
void NimBLEServerCallbacks::onIdentity(NimBLEConnInfo& connInfo) {
11211004
NIMBLE_LOGD("NimBLEServerCallbacks", "onIdentity: default");
@@ -1125,10 +1008,6 @@ void NimBLEServerCallbacks::onAuthenticationComplete(NimBLEConnInfo& connInfo) {
11251008
NIMBLE_LOGD("NimBLEServerCallbacks", "onAuthenticationComplete: default");
11261009
} // onAuthenticationComplete
11271010

1128-
void NimBLEServerCallbacks::onAuthenticationComplete(NimBLEConnInfo& connInfo, const std::string& name) {
1129-
NIMBLE_LOGD("NimBLEServerCallbacks", "onAuthenticationComplete: default");
1130-
} // onAuthenticationComplete
1131-
11321011
void NimBLEServerCallbacks::onConnParamsUpdate(NimBLEConnInfo& connInfo) {
11331012
NIMBLE_LOGD("NimBLEServerCallbacks", "onConnParamsUpdate: default");
11341013
} // onConnParamsUpdate

src/NimBLEServer.h

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
# undef max
3030
/**************************/
3131

32-
# include <string>
3332
# include <vector>
3433
# include <array>
3534

@@ -75,8 +74,6 @@ class NimBLEServer {
7574
NimBLEConnInfo getPeerInfo(uint8_t index) const;
7675
NimBLEConnInfo getPeerInfo(const NimBLEAddress& address) const;
7776
NimBLEConnInfo getPeerInfoByHandle(uint16_t connHandle) const;
78-
std::string getPeerName(const NimBLEConnInfo& connInfo) const;
79-
void getPeerNameOnConnect(bool enable);
8077
void advertiseOnDisconnect(bool enable);
8178
void setDataLen(uint16_t connHandle, uint16_t tx_octets) const;
8279

@@ -114,7 +111,6 @@ class NimBLEServer {
114111
~NimBLEServer();
115112

116113
bool m_gattsStarted : 1;
117-
bool m_getPeerNameOnConnect : 1;
118114
bool m_svcChanged : 1;
119115
bool m_deleteCallbacks : 1;
120116
# if !CONFIG_BT_NIMBLE_EXT_ADV
@@ -128,17 +124,15 @@ class NimBLEServer {
128124
NimBLEClient* m_pClient{nullptr};
129125
# endif
130126

131-
static int handleGapEvent(struct ble_gap_event* event, void* arg);
132-
static int handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt* ctxt, void* arg);
133-
static int peerNameCB(uint16_t connHandle, const ble_gatt_error* error, ble_gatt_attr* attr, void* arg);
134-
std::string getPeerNameImpl(uint16_t connHandle, int cb_type = -1) const;
135-
void serviceChanged();
136-
void resetGATT();
127+
static int handleGapEvent(struct ble_gap_event* event, void* arg);
128+
static int handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt* ctxt, void* arg);
129+
void serviceChanged();
130+
void resetGATT();
137131

138132
}; // NimBLEServer
139133

140134
/**
141-
* @brief Callbacks associated with the operation of a %BLE server.
135+
* @brief Callbacks associated with the operation of a BLE server.
142136
*/
143137
class NimBLEServerCallbacks {
144138
public:
@@ -147,26 +141,16 @@ class NimBLEServerCallbacks {
147141
/**
148142
* @brief Handle a client connection.
149143
* This is called when a client connects.
150-
* @param [in] pServer A pointer to the %BLE server that received the client connection.
144+
* @param [in] pServer A pointer to the BLE server that received the client connection.
151145
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information.
152146
* about the peer connection parameters.
153147
*/
154148
virtual void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo);
155149

156-
/**
157-
* @brief Handle a client connection.
158-
* This is called when a client connects.
159-
* @param [in] pServer A pointer to the %BLE server that received the client connection.
160-
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information.
161-
* @param [in] name The name of the connected peer device.
162-
* about the peer connection parameters.
163-
*/
164-
virtual void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, std::string& name);
165-
166150
/**
167151
* @brief Handle a client disconnection.
168-
* This is called when a client discconnects.
169-
* @param [in] pServer A pointer to the %BLE server that received the client disconnection.
152+
* This is called when a client disconnects.
153+
* @param [in] pServer A pointer to the BLE server that received the client disconnection.
170154
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
171155
* about the peer connection parameters.
172156
* @param [in] reason The reason code for the disconnection.
@@ -202,14 +186,6 @@ class NimBLEServerCallbacks {
202186
*/
203187
virtual void onAuthenticationComplete(NimBLEConnInfo& connInfo);
204188

205-
/**
206-
* @brief Called when the pairing procedure is complete.
207-
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
208-
* @param [in] name The name of the connected peer device.
209-
* about the peer connection parameters.
210-
*/
211-
virtual void onAuthenticationComplete(NimBLEConnInfo& connInfo, const std::string& name);
212-
213189
/**
214190
* @brief Called when the peer identity address is resolved.
215191
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information

0 commit comments

Comments
 (0)