@@ -101,8 +101,8 @@ NimBLEClient::~NimBLEClient() {
101
101
*/
102
102
void NimBLEClient::deleteServices () {
103
103
// Delete all the services.
104
- for (auto & it : m_svcVec) {
105
- delete it ;
104
+ for (auto & svc : m_svcVec) {
105
+ delete svc ;
106
106
}
107
107
108
108
std::vector<NimBLERemoteService*>().swap (m_svcVec);
@@ -243,8 +243,7 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
243
243
break ;
244
244
245
245
default :
246
- NIMBLE_LOGE (LOG_TAG,
247
- " Failed to connect to %s, rc=%d; %s" ,
246
+ NIMBLE_LOGE (LOG_TAG, " Failed to connect to %s, rc=%d; %s" ,
248
247
std::string (m_peerAddress).c_str (),
249
248
rc,
250
249
NimBLEUtils::returnCodeToString (rc));
@@ -631,48 +630,36 @@ NimBLERemoteService* NimBLEClient::getService(const char* uuid) {
631
630
*/
632
631
NimBLERemoteService* NimBLEClient::getService (const NimBLEUUID& uuid) {
633
632
NIMBLE_LOGD (LOG_TAG, " >> getService: uuid: %s" , uuid.toString ().c_str ());
633
+ NimBLERemoteService *pSvc = nullptr ;
634
+ NimBLEUUID uuidTmp;
634
635
635
- for (auto & it : m_svcVec) {
636
- if (it ->getUUID () == uuid) {
636
+ for (auto & svc : m_svcVec) {
637
+ if (svc ->getUUID () == uuid) {
637
638
NIMBLE_LOGD (LOG_TAG, " << getService: found the service with uuid: %s" , uuid.toString ().c_str ());
638
- return it ;
639
+ return svc ;
639
640
}
640
641
}
641
642
642
- size_t prevSize = m_svcVec.size ();
643
- if (retrieveServices (&uuid)) {
644
- if (m_svcVec.size () > prevSize) {
645
- return m_svcVec.back ();
646
- }
643
+ if (!retrieveServices (&uuid, pSvc) || pSvc) {
644
+ goto Done;
645
+ }
647
646
648
- // If the request was successful but 16/32 bit uuid not found
649
- // try again with the 128 bit uuid.
650
- if (uuid.bitSize () == BLE_UUID_TYPE_16 || uuid.bitSize () == BLE_UUID_TYPE_32) {
651
- NimBLEUUID uuid128 (uuid);
652
- uuid128.to128 ();
653
- if (retrieveServices (&uuid128)) {
654
- if (m_svcVec.size () > prevSize) {
655
- return m_svcVec.back ();
656
- }
657
- }
658
- } else {
659
- // If the request was successful but the 128 bit uuid not found
660
- // try again with the 16 bit uuid.
661
- NimBLEUUID uuid16 (uuid);
662
- uuid16.to16 ();
663
- // if the uuid was 128 bit but not of the BLE base type this check will fail
664
- if (uuid16.bitSize () == BLE_UUID_TYPE_16) {
665
- if (retrieveServices (&uuid16)) {
666
- if (m_svcVec.size () > prevSize) {
667
- return m_svcVec.back ();
668
- }
669
- }
670
- }
671
- }
647
+ // Try again with 128 bit uuid if request succeeded with no uuid found.
648
+ if (uuid.bitSize () == BLE_UUID_TYPE_16 || uuid.bitSize () == BLE_UUID_TYPE_32) {
649
+ uuidTmp = NimBLEUUID (uuid).to128 ();
650
+ retrieveServices (&uuidTmp, pSvc);
651
+ goto Done;
652
+ }
653
+ // Try again with 16 bit uuid if request succeeded with no uuid found.
654
+ // If the uuid was 128 bit but not of the BLE base type this check will fail.
655
+ uuidTmp = NimBLEUUID (uuid).to16 ();
656
+ if (uuidTmp.bitSize () == BLE_UUID_TYPE_16) {
657
+ retrieveServices (&uuidTmp, pSvc);
672
658
}
673
659
660
+ Done:
674
661
NIMBLE_LOGD (LOG_TAG, " << getService: not found" );
675
- return nullptr ;
662
+ return pSvc ;
676
663
} // getService
677
664
678
665
/* *
@@ -725,20 +712,16 @@ bool NimBLEClient::discoverAttributes() {
725
712
* * Here we ask the server for its set of services and wait until we have received them all.
726
713
* @return true on success otherwise false if an error occurred
727
714
*/
728
- bool NimBLEClient::retrieveServices (const NimBLEUUID* uuidFilter) {
715
+ bool NimBLEClient::retrieveServices (const NimBLEUUID* uuidFilter, NimBLERemoteService *out ) {
729
716
if (!isConnected ()) {
730
717
NIMBLE_LOGE (LOG_TAG, " Disconnected, could not retrieve services -aborting" );
731
718
return false ;
732
719
}
733
720
734
- int rc = 0 ;
735
721
NimBLETaskData taskData (this );
736
-
737
- if (uuidFilter == nullptr ) {
738
- rc = ble_gattc_disc_all_svcs (m_connHandle, NimBLEClient::serviceDiscoveredCB, &taskData);
739
- } else {
740
- rc = ble_gattc_disc_svc_by_uuid (m_connHandle, uuidFilter->getBase (), NimBLEClient::serviceDiscoveredCB, &taskData);
741
- }
722
+ int rc = (uuidFilter == nullptr )
723
+ ? ble_gattc_disc_all_svcs (m_connHandle, svcDiscCB, &taskData)
724
+ : ble_gattc_disc_svc_by_uuid (m_connHandle, uuidFilter->getBase (), svcDiscCB, &taskData);
742
725
743
726
if (rc != 0 ) {
744
727
NIMBLE_LOGE (LOG_TAG, " ble_gattc_disc_all_svcs: rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
@@ -748,7 +731,10 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter) {
748
731
749
732
NimBLEUtils::taskWait (taskData, BLE_NPL_TIME_FOREVER);
750
733
rc = taskData.m_flags ;
751
- if (rc == 0 || rc == BLE_HS_EDONE) {
734
+ if (rc == BLE_HS_EDONE) {
735
+ if (out) {
736
+ out = m_svcVec.back ();
737
+ }
752
738
return true ;
753
739
}
754
740
@@ -762,39 +748,29 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter) {
762
748
* @details When a service is found or there is none left or there was an error
763
749
* the API will call this and report findings.
764
750
*/
765
- int NimBLEClient::serviceDiscoveredCB (uint16_t connHandle,
766
- const struct ble_gatt_error * error,
767
- const struct ble_gatt_svc * service,
768
- void * arg) {
769
- NIMBLE_LOGD (LOG_TAG,
770
- " Service Discovered >> status: %d handle: %d" ,
771
- error->status ,
772
- (error->status == 0 ) ? service->start_handle : -1 );
773
-
774
- NimBLETaskData* pTaskData = (NimBLETaskData*)arg;
775
- NimBLEClient* pClient = (NimBLEClient*)pTaskData->m_pInstance ;
776
-
777
- if (error->status == BLE_HS_ENOTCONN) {
778
- NIMBLE_LOGE (LOG_TAG, " << Service Discovered; Disconnected" );
779
- NimBLEUtils::taskRelease (*pTaskData, error->status );
780
- return error->status ;
781
- }
751
+ int NimBLEClient::svcDiscCB (uint16_t connHandle,
752
+ const struct ble_gatt_error * error,
753
+ const struct ble_gatt_svc * service,
754
+ void * arg) {
755
+ const int rc = error->status ;
756
+ auto pTaskData = (NimBLETaskData*)arg;
757
+ auto pClient = (NimBLEClient*)pTaskData->m_pInstance ;
758
+ NIMBLE_LOGD (LOG_TAG, " Service Discovered >> status: %d handle: %d" , rc, !rc ? service->start_handle : -1 );
782
759
783
760
// Make sure the service discovery is for this device
784
761
if (pClient->getConnHandle () != connHandle) {
785
762
return 0 ;
786
763
}
787
764
788
- if (error->status == 0 ) {
789
- // Found a service - add it to the vector
765
+ if (rc == 0 ) { // Found a service - add it to the vector
790
766
pClient->m_svcVec .push_back (new NimBLERemoteService (pClient, service));
791
767
return 0 ;
792
768
}
793
769
794
770
NimBLEUtils::taskRelease (*pTaskData, error->status );
795
- NIMBLE_LOGD (LOG_TAG, " << Service Discovered" );
771
+ NIMBLE_LOGD (LOG_TAG, " << Service Discovered%s " , (rc == BLE_HS_ENOTCONN) ? " ; Disconnected " : " " );
796
772
return error->status ;
797
- } // serviceDiscoveredCB
773
+ } // serviceDiscCB
798
774
799
775
/* *
800
776
* @brief Get the value of a specific characteristic associated with a specific service.
@@ -803,10 +779,8 @@ int NimBLEClient::serviceDiscoveredCB(uint16_t connHandle,
803
779
* @returns characteristic value or an empty value if not found.
804
780
*/
805
781
NimBLEAttValue NimBLEClient::getValue (const NimBLEUUID& serviceUUID, const NimBLEUUID& characteristicUUID) {
806
- NIMBLE_LOGD (LOG_TAG,
807
- " >> getValue: serviceUUID: %s, characteristicUUID: %s" ,
808
- serviceUUID.toString ().c_str (),
809
- characteristicUUID.toString ().c_str ());
782
+ NIMBLE_LOGD (LOG_TAG, " >> getValue: serviceUUID: %s, characteristicUUID: %s" ,
783
+ serviceUUID.toString ().c_str (), characteristicUUID.toString ().c_str ());
810
784
811
785
NimBLEAttValue ret{};
812
786
auto pService = getService (serviceUUID);
@@ -833,15 +807,13 @@ bool NimBLEClient::setValue(const NimBLEUUID& serviceUUID,
833
807
const NimBLEUUID& characteristicUUID,
834
808
const NimBLEAttValue& value,
835
809
bool response) {
836
- NIMBLE_LOGD (LOG_TAG,
837
- " >> setValue: serviceUUID: %s, characteristicUUID: %s" ,
838
- serviceUUID.toString ().c_str (),
839
- characteristicUUID.toString ().c_str ());
810
+ NIMBLE_LOGD (LOG_TAG, " >> setValue: serviceUUID: %s, characteristicUUID: %s" ,
811
+ serviceUUID.toString ().c_str (), characteristicUUID.toString ().c_str ());
840
812
841
813
bool ret = false ;
842
814
auto pService = getService (serviceUUID);
843
815
if (pService != nullptr ) {
844
- NimBLERemoteCharacteristic* pChar = pService->getCharacteristic (characteristicUUID);
816
+ auto pChar = pService->getCharacteristic (characteristicUUID);
845
817
if (pChar != nullptr ) {
846
818
ret = pChar->writeValue (value, response);
847
819
}
@@ -858,11 +830,13 @@ bool NimBLEClient::setValue(const NimBLEUUID& serviceUUID,
858
830
*/
859
831
NimBLERemoteCharacteristic* NimBLEClient::getCharacteristic (uint16_t handle) {
860
832
for (const auto & svc : m_svcVec) {
861
- if (svc->getStartHandle () <= handle && handle <= svc->getEndHandle ()) {
862
- for (const auto & chr : svc->m_vChars ) {
863
- if (chr->getHandle () == handle) {
864
- return chr;
865
- }
833
+ if (svc->getStartHandle () > handle && handle > svc->getEndHandle ()) {
834
+ continue ;
835
+ }
836
+
837
+ for (const auto & chr : svc->m_vChars ) {
838
+ if (chr->getHandle () == handle) {
839
+ return chr;
866
840
}
867
841
}
868
842
}
@@ -882,28 +856,28 @@ uint16_t NimBLEClient::getMTU() const {
882
856
* @brief Callback for the MTU exchange API function.
883
857
* @details When the MTU exchange is complete the API will call this and report the new MTU.
884
858
*/
885
- int NimBLEClient::exchangeMTUCb (uint16_t conn_handle , const ble_gatt_error* error, uint16_t mtu, void * arg) {
886
- NIMBLE_LOGD (LOG_TAG, " exchangeMTUCb : status=%d, mtu=%d" , error->status , mtu);
859
+ int NimBLEClient::exchangeMTUCB (uint16_t connHandle , const ble_gatt_error* error, uint16_t mtu, void * arg) {
860
+ NIMBLE_LOGD (LOG_TAG, " exchangeMTUCB : status=%d, mtu=%d" , error->status , mtu);
887
861
888
862
NimBLEClient* pClient = (NimBLEClient*)arg;
889
- if (pClient->getConnHandle () != conn_handle ) {
863
+ if (pClient->getConnHandle () != connHandle ) {
890
864
return 0 ;
891
865
}
892
866
893
867
if (error->status != 0 ) {
894
- NIMBLE_LOGE (LOG_TAG, " exchangeMTUCb () rc=%d %s" , error->status , NimBLEUtils::returnCodeToString (error->status ));
868
+ NIMBLE_LOGE (LOG_TAG, " exchangeMTUCB () rc=%d %s" , error->status , NimBLEUtils::returnCodeToString (error->status ));
895
869
pClient->m_lastErr = error->status ;
896
870
}
897
871
898
872
return 0 ;
899
- } // exchangeMTUCb
873
+ } // exchangeMTUCB
900
874
901
875
/* *
902
876
* @brief Begin the MTU exchange process with the server.
903
877
* @returns true if the request was sent successfully.
904
878
*/
905
879
bool NimBLEClient::exchangeMTU () {
906
- int rc = ble_gattc_exchange_mtu (m_connHandle, NimBLEClient::exchangeMTUCb , this );
880
+ int rc = ble_gattc_exchange_mtu (m_connHandle, NimBLEClient::exchangeMTUCB , this );
907
881
if (rc != 0 ) {
908
882
NIMBLE_LOGE (LOG_TAG, " MTU exchange error; rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
909
883
m_lastErr = rc;
@@ -989,25 +963,25 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
989
963
pClient->m_pClientCallbacks ->onConnect (pClient);
990
964
}
991
965
992
- if (pClient->m_config .exchangeMTU ) {
993
- if (!pClient->exchangeMTU ()) {
994
- rc = pClient->m_lastErr ; // sets the error in the task data
995
- break ;
996
- }
997
-
998
- return 0 ; // return as we may have a task waiting for the MTU before releasing it.
966
+ if (!pClient->m_config .exchangeMTU ) {
967
+ break ;
999
968
}
1000
- } else {
1001
- pClient->m_connHandle = BLE_HS_CONN_HANDLE_NONE;
1002
969
1003
- if (pClient->m_config .asyncConnect ) {
1004
- pClient->m_pClientCallbacks ->onConnectFail (pClient, rc);
1005
- if (pClient->m_config .deleteOnConnectFail ) {
1006
- NimBLEDevice::deleteClient (pClient);
1007
- }
970
+ if (!pClient->exchangeMTU ()) {
971
+ rc = pClient->m_lastErr ; // sets the error in the task data
972
+ break ;
1008
973
}
974
+
975
+ return 0 ; // return as we may have a task waiting for the MTU before releasing it.
1009
976
}
1010
977
978
+ pClient->m_connHandle = BLE_HS_CONN_HANDLE_NONE;
979
+ if (pClient->m_config .asyncConnect ) {
980
+ pClient->m_pClientCallbacks ->onConnectFail (pClient, rc);
981
+ if (pClient->m_config .deleteOnConnectFail ) {
982
+ NimBLEDevice::deleteClient (pClient);
983
+ }
984
+ }
1011
985
break ;
1012
986
} // BLE_GAP_EVENT_CONNECT
1013
987
@@ -1035,23 +1009,23 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
1035
1009
continue ;
1036
1010
}
1037
1011
1038
- NIMBLE_LOGD (LOG_TAG,
1039
- " checking service %s for handle: %d" ,
1012
+ NIMBLE_LOGD (LOG_TAG, " checking service %s for handle: %d" ,
1040
1013
svc->getUUID ().toString ().c_str (),
1041
1014
event->notify_rx .attr_handle );
1042
1015
1043
1016
for (const auto & chr : svc->m_vChars ) {
1044
- if (chr->getHandle () == event->notify_rx .attr_handle ) {
1045
- NIMBLE_LOGD (LOG_TAG, " Got Notification for characteristic %s" , chr->toString ().c_str ());
1017
+ if (chr->getHandle () != event->notify_rx .attr_handle ) {
1018
+ continue ;
1019
+ }
1046
1020
1047
- uint32_t data_len = OS_MBUF_PKTLEN (event->notify_rx .om );
1048
- chr->m_value .setValue (event->notify_rx .om ->om_data , data_len);
1021
+ NIMBLE_LOGD (LOG_TAG, " Got Notification for characteristic %s" , chr->toString ().c_str ());
1022
+ uint32_t data_len = OS_MBUF_PKTLEN (event->notify_rx .om );
1023
+ chr->m_value .setValue (event->notify_rx .om ->om_data , data_len);
1049
1024
1050
- if (chr->m_notifyCallback != nullptr ) {
1051
- chr->m_notifyCallback (chr, event->notify_rx .om ->om_data , data_len, !event->notify_rx .indication );
1052
- }
1053
- break ;
1025
+ if (chr->m_notifyCallback != nullptr ) {
1026
+ chr->m_notifyCallback (chr, event->notify_rx .om ->om_data , data_len, !event->notify_rx .indication );
1054
1027
}
1028
+ break ;
1055
1029
}
1056
1030
}
1057
1031
@@ -1064,8 +1038,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
1064
1038
return 0 ;
1065
1039
}
1066
1040
NIMBLE_LOGD (LOG_TAG, " Peer requesting to update connection parameters" );
1067
- NIMBLE_LOGD (LOG_TAG,
1068
- " MinInterval: %d, MaxInterval: %d, Latency: %d, Timeout: %d" ,
1041
+ NIMBLE_LOGD (LOG_TAG, " MinInterval: %d, MaxInterval: %d, Latency: %d, Timeout: %d" ,
1069
1042
event->conn_update_req .peer_params ->itvl_min ,
1070
1043
event->conn_update_req .peer_params ->itvl_max ,
1071
1044
event->conn_update_req .peer_params ->latency ,
0 commit comments