diff options
author | Sven Gothel <[email protected]> | 2020-06-25 01:49:16 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-06-25 01:49:16 +0200 |
commit | a78965ca7904574e259672e0ae50bbb932343f29 (patch) | |
tree | 6b0a53b474654b297d1871d2ce506a07f87baf93 /src/direct_bt | |
parent | d216c80596bd662087f47d275f2fc302e4061abc (diff) |
GATTHandler: Add mtx_command for all public accessible functions, ensuring sequential processing is ensured
GATTHandler servers one device, however, applications may utilize multithreading
and concurrent command requests may lead to wrong replies.
Similar to DBTManager and HCIHandler, top-level entries shall be synchronized.
Diffstat (limited to 'src/direct_bt')
-rw-r--r-- | src/direct_bt/GATTHandler.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp index 0a5ee71b..dac264d5 100644 --- a/src/direct_bt/GATTHandler.cpp +++ b/src/direct_bt/GATTHandler.cpp @@ -399,7 +399,7 @@ uint16_t GATTHandler::exchangeMTU(const uint16_t clientMaxMTU) { throw IllegalArgumentException("clientMaxMTU "+std::to_string(clientMaxMTU)+" > ClientMaxMTU "+std::to_string(number(Defaults::MAX_ATT_MTU)), E_FILE_LINE); } const AttExchangeMTU req(clientMaxMTU); - + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor PERF_TS_T0(); uint16_t mtu = 0; @@ -445,6 +445,7 @@ GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_ } std::vector<GATTServiceRef> & GATTHandler::discoverCompletePrimaryServices() { + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor if( !discoverPrimaryServices(services) ) { return services; } @@ -466,7 +467,7 @@ bool GATTHandler::discoverPrimaryServices(std::vector<GATTServiceRef> & result) * in the Read by Type Group Response is 0xFFFF. */ const uuid16_t groupType = uuid16_t(GattAttributeType::PRIMARY_SERVICE); - + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor PERF_TS_T0(); bool done=false; @@ -526,6 +527,7 @@ bool GATTHandler::discoverCharacteristics(GATTServiceRef & service) { * </p> */ const uuid16_t characteristicTypeReq = uuid16_t(GattAttributeType::CHARACTERISTIC); + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor DBG_PRINT("GATT discoverCharacteristics Service: %s", service->toString().c_str()); PERF_TS_T0(); @@ -590,6 +592,7 @@ bool GATTHandler::discoverDescriptors(GATTServiceRef & service) { * </p> */ DBG_PRINT("GATT discoverDescriptors Service: %s", service->toString().c_str()); + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor PERF_TS_T0(); bool done=false; @@ -683,6 +686,7 @@ bool GATTHandler::readCharacteristicValue(const GATTCharacteristic & decl, POcte bool GATTHandler::readValue(const uint16_t handle, POctets & res, int expectedLength) { /* BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.1 Read Characteristic Value */ /* BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.3 Read Long Characteristic Value */ + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor PERF2_TS_T0(); bool done=false; @@ -791,6 +795,7 @@ bool GATTHandler::writeValue(const uint16_t handle, const TROOctets & value, con WARN_PRINT("GATT writeValue size <= 0, no-op: %s", value.toString().c_str()); return false; } + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor // TODO: Long Value! PERF2_TS_T0(); @@ -863,6 +868,8 @@ std::shared_ptr<GenericAccess> GATTHandler::getGenericAccess(std::vector<GATTCha AppearanceCat appearance = AppearanceCat::UNKNOWN; PeriphalPreferredConnectionParameters * prefConnParam = nullptr; + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor + for(size_t i=0; i<genericAccessCharDeclList.size(); i++) { const GATTCharacteristic & charDecl = *genericAccessCharDeclList.at(i); if( _GENERIC_ACCESS != *charDecl.service->type ) { @@ -900,8 +907,9 @@ std::shared_ptr<GenericAccess> GATTHandler::getGenericAccess(std::vector<GATTSer } bool GATTHandler::ping() { - std::shared_ptr<GenericAccess> res = nullptr; - for(size_t i=0; i<services.size() && nullptr == res; i++) { + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor + + for(size_t i=0; i<services.size(); i++) { std::vector<GATTCharacteristicRef> & genericAccessCharDeclList = services.at(i)->characteristicList; POctets value(32, 0); @@ -935,6 +943,8 @@ std::shared_ptr<DeviceInformation> GATTHandler::getDeviceInformation(std::vector PnP_ID * pnpID = nullptr; bool found = false; + const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor + for(size_t i=0; i<characteristicDeclList.size(); i++) { const GATTCharacteristic & charDecl = *characteristicDeclList.at(i); if( _DEVICE_INFORMATION != *charDecl.service->type ) { |