From a78965ca7904574e259672e0ae50bbb932343f29 Mon Sep 17 00:00:00 2001
From: Sven Gothel
Date: Thu, 25 Jun 2020 01:49:16 +0200
Subject: 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.
---
src/direct_bt/GATTHandler.cpp | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
(limited to 'src/direct_bt')
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 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 & GATTHandler::discoverCompletePrimaryServices() {
+ const std::lock_guard lock(mtx_command); // RAII-style acquire and relinquish via destructor
if( !discoverPrimaryServices(services) ) {
return services;
}
@@ -466,7 +467,7 @@ bool GATTHandler::discoverPrimaryServices(std::vector & result)
* in the Read by Type Group Response is 0xFFFF.
*/
const uuid16_t groupType = uuid16_t(GattAttributeType::PRIMARY_SERVICE);
-
+ const std::lock_guard 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) {
*
*/
const uuid16_t characteristicTypeReq = uuid16_t(GattAttributeType::CHARACTERISTIC);
+ const std::lock_guard 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) {
*
*/
DBG_PRINT("GATT discoverDescriptors Service: %s", service->toString().c_str());
+ const std::lock_guard 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 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 lock(mtx_command); // RAII-style acquire and relinquish via destructor
// TODO: Long Value!
PERF2_TS_T0();
@@ -863,6 +868,8 @@ std::shared_ptr GATTHandler::getGenericAccess(std::vector lock(mtx_command); // RAII-style acquire and relinquish via destructor
+
for(size_t i=0; itype ) {
@@ -900,8 +907,9 @@ std::shared_ptr GATTHandler::getGenericAccess(std::vector res = nullptr;
- for(size_t i=0; i lock(mtx_command); // RAII-style acquire and relinquish via destructor
+
+ for(size_t i=0; i & genericAccessCharDeclList = services.at(i)->characteristicList;
POctets value(32, 0);
@@ -935,6 +943,8 @@ std::shared_ptr GATTHandler::getDeviceInformation(std::vector
PnP_ID * pnpID = nullptr;
bool found = false;
+ const std::lock_guard lock(mtx_command); // RAII-style acquire and relinquish via destructor
+
for(size_t i=0; itype ) {
--
cgit v1.2.3