diff options
-rw-r--r-- | examples/dbt_peripheral00.cpp | 74 | ||||
-rw-r--r-- | trial/direct_bt/dbt_server00.hpp | 89 | ||||
-rw-r--r-- | trial/direct_bt/dbt_server01.hpp | 89 |
3 files changed, 122 insertions, 130 deletions
diff --git a/examples/dbt_peripheral00.cpp b/examples/dbt_peripheral00.cpp index f84aba70..18aeccba 100644 --- a/examples/dbt_peripheral00.cpp +++ b/examples/dbt_peripheral00.cpp @@ -345,8 +345,7 @@ class MyAdapterStatusListener : public AdapterStatusListener { class MyGATTServerListener : public DBGattServer::Listener { private: - std::thread pulseSenderThread; - jau::sc_atomic_bool stopPulseSender = false; + jau::service_runner pulse_service; jau::sc_atomic_uint16 handlePulseDataNotify = 0; jau::sc_atomic_uint16 handlePulseDataIndicate = 0; @@ -367,29 +366,38 @@ class MyGATTServerListener : public DBGattServer::Listener { dbGattServer->resetGattClientCharConfig(DataServiceUUID, ResponseUUID); } - void pulseSender() { - while( !stopPulseSender ) { - { - jau::sc_atomic_critical sync(sync_data); - BTDeviceRef connectedDevice_ = getDevice(); - if( nullptr != connectedDevice_ && connectedDevice_->getConnected() ) { - if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) { - std::string data( "Dynamic Data Example. Elapsed Milliseconds: "+jau::to_decstring(environment::getElapsedMillisecond(), ',', 9) ); - jau::POctets v(data.size()+1, jau::endian::little); - v.put_string_nc(0, data, v.size(), true /* includeEOS */); - if( 0 != handlePulseDataNotify ) { - fprintf_td(stderr, "****** GATT::sendNotification: PULSE to %s\n", connectedDevice_->toString().c_str()); - connectedDevice_->sendNotification(handlePulseDataNotify, v); - } - if( 0 != handlePulseDataIndicate ) { - fprintf_td(stderr, "****** GATT::sendIndication: PULSE to %s\n", connectedDevice_->toString().c_str()); - connectedDevice_->sendIndication(handlePulseDataIndicate, v); - } - } + void pulse_worker_init(jau::service_runner& sr) noexcept { + (void)sr; + const BTDeviceRef connectedDevice_ = getDevice(); + const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; + fprintf_td(stderr, "****** Server GATT::PULSE Start %s\n", connectedDeviceStr.c_str()); + } + void pulse_worker(jau::service_runner& sr) noexcept { + BTDeviceRef connectedDevice_ = getDevice(); + if( nullptr != connectedDevice_ && connectedDevice_->getConnected() ) { + if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) { + std::string data( "Dynamic Data Example. Elapsed Milliseconds: "+jau::to_decstring(environment::getElapsedMillisecond(), ',', 9) ); + jau::POctets v(data.size()+1, jau::endian::little); + v.put_string_nc(0, data, v.size(), true /* includeEOS */); + if( 0 != handlePulseDataNotify ) { + fprintf_td(stderr, "****** GATT::sendNotification: PULSE to %s\n", connectedDevice_->toString().c_str()); + connectedDevice_->sendNotification(handlePulseDataNotify, v); + } + if( 0 != handlePulseDataIndicate ) { + fprintf_td(stderr, "****** GATT::sendIndication: PULSE to %s\n", connectedDevice_->toString().c_str()); + connectedDevice_->sendIndication(handlePulseDataIndicate, v); } } - std::this_thread::sleep_for(std::chrono::milliseconds(500)); } + if( !sr.shall_stop() ) { + jau::sleep_for( 500_ms ); + } + } + void pulse_worker_end(jau::service_runner& sr) noexcept { + (void)sr; + const BTDeviceRef connectedDevice_ = getDevice(); + const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; + fprintf_td(stderr, "****** Server GATT::PULSE End %s\n", connectedDeviceStr.c_str()); } void sendResponse(jau::POctets data) { @@ -412,19 +420,21 @@ class MyGATTServerListener : public DBGattServer::Listener { public: MyGATTServerListener() - : pulseSenderThread(&MyGATTServerListener::pulseSender, this) - { } + : pulse_service("MyGATTServerListener::pulse", THREAD_SHUTDOWN_TIMEOUT_MS, + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker), + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker_init), + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker_end)) + { + pulse_service.start(); + } - ~MyGATTServerListener() noexcept { close(); } + ~MyGATTServerListener() noexcept { + pulse_service.stop(); + } void close() noexcept { - { - jau::sc_atomic_critical sync(sync_data); - stopPulseSender = true; - } - if( pulseSenderThread.joinable() ) { - pulseSenderThread.join(); - } + pulse_service.stop(); + clear(); } void connected(BTDeviceRef device, const uint16_t initialMTU) override { diff --git a/trial/direct_bt/dbt_server00.hpp b/trial/direct_bt/dbt_server00.hpp index 22d7f25c..27231d8c 100644 --- a/trial/direct_bt/dbt_server00.hpp +++ b/trial/direct_bt/dbt_server00.hpp @@ -306,8 +306,7 @@ class DBTServer00 : public DBTServerTest { class MyGATTServerListener : public DBGattServer::Listener { private: DBTServer00& parent; - std::thread pulseSenderThread; - jau::sc_atomic_bool stopPulseSenderFlag = false; + jau::service_runner pulse_service; jau::sc_atomic_uint16 handlePulseDataNotify = 0; jau::sc_atomic_uint16 handlePulseDataIndicate = 0; @@ -316,48 +315,43 @@ class DBTServer00 : public DBTServerTest { uint16_t usedMTU = BTGattHandler::number(BTGattHandler::Defaults::MIN_ATT_MTU); - bool shallStopPulseSender() { - const std::lock_guard<std::mutex> lock(parent.mtx_sync); // RAII-style acquire and relinquish via destructor - return stopPulseSenderFlag; + void pulse_worker_init(jau::service_runner& sr) noexcept { + (void)sr; + const BTDeviceRef connectedDevice_ = parent.getDevice(); + const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; + fprintf_td(stderr, "****** Server GATT::PULSE Start %s\n", connectedDeviceStr.c_str()); } - - void pulseSender() { - { - const BTDeviceRef connectedDevice_ = parent.getDevice(); - const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; - fprintf_td(stderr, "****** Server GATT::PULSE Start %s\n", connectedDeviceStr.c_str()); - } - while( !shallStopPulseSender() ) { - BTDeviceRef connectedDevice_ = parent.getDevice(); - if( nullptr != connectedDevice_ && connectedDevice_->getConnected() ) { - if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) { - std::string data( "Dynamic Data Example. Elapsed Milliseconds: "+jau::to_decstring(environment::getElapsedMillisecond(), ',', 9) ); - jau::POctets v(data.size()+1, jau::endian::little); - v.put_string_nc(0, data, v.size(), true /* includeEOS */); - if( 0 != handlePulseDataNotify ) { - if( GATT_VERBOSE ) { - fprintf_td(stderr, "****** Server GATT::sendNotification: PULSE to %s\n", connectedDevice_->toString().c_str()); - } - connectedDevice_->sendNotification(handlePulseDataNotify, v); + void pulse_worker(jau::service_runner& sr) noexcept { + BTDeviceRef connectedDevice_ = parent.getDevice(); + if( nullptr != connectedDevice_ && connectedDevice_->getConnected() ) { + if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) { + std::string data( "Dynamic Data Example. Elapsed Milliseconds: "+jau::to_decstring(environment::getElapsedMillisecond(), ',', 9) ); + jau::POctets v(data.size()+1, jau::endian::little); + v.put_string_nc(0, data, v.size(), true /* includeEOS */); + if( 0 != handlePulseDataNotify ) { + if( GATT_VERBOSE ) { + fprintf_td(stderr, "****** Server GATT::sendNotification: PULSE to %s\n", connectedDevice_->toString().c_str()); } - if( 0 != handlePulseDataIndicate ) { - if( GATT_VERBOSE ) { - fprintf_td(stderr, "****** Server GATT::sendIndication: PULSE to %s\n", connectedDevice_->toString().c_str()); - } - connectedDevice_->sendIndication(handlePulseDataIndicate, v); + connectedDevice_->sendNotification(handlePulseDataNotify, v); + } + if( 0 != handlePulseDataIndicate ) { + if( GATT_VERBOSE ) { + fprintf_td(stderr, "****** Server GATT::sendIndication: PULSE to %s\n", connectedDevice_->toString().c_str()); } + connectedDevice_->sendIndication(handlePulseDataIndicate, v); } } - if( !shallStopPulseSender() ) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } } - { - const BTDeviceRef connectedDevice_ = parent.getDevice(); - const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; - fprintf_td(stderr, "****** Server GATT::PULSE End %s\n", connectedDeviceStr.c_str()); + if( !sr.shall_stop() ) { + jau::sleep_for( 100_ms ); } } + void pulse_worker_end(jau::service_runner& sr) noexcept { + (void)sr; + const BTDeviceRef connectedDevice_ = parent.getDevice(); + const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; + fprintf_td(stderr, "****** Server GATT::PULSE End %s\n", connectedDeviceStr.c_str()); + } void sendResponse(jau::POctets data) { BTDeviceRef connectedDevice_ = parent.getDevice(); @@ -384,14 +378,17 @@ class DBTServer00 : public DBTServerTest { public: MyGATTServerListener(DBTServer00& p) - : parent(p), pulseSenderThread(&MyGATTServerListener::pulseSender, this) - { } + : parent(p), + pulse_service("MyGATTServerListener::pulse", THREAD_SHUTDOWN_TIMEOUT_MS, + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker), + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker_init), + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker_end)) + { + pulse_service.start(); + } ~MyGATTServerListener() noexcept { - stopPulseSenderFlag = true; - if( pulseSenderThread.joinable() ) { - pulseSenderThread.join(); - } + pulse_service.stop(); } void clear() { @@ -407,14 +404,8 @@ class DBTServer00 : public DBTServerTest { } void close() noexcept { + pulse_service.stop(); clear(); - { - const std::lock_guard<std::mutex> lock(parent.mtx_sync); // RAII-style acquire and relinquish via destructor - stopPulseSenderFlag = true; - } - if( pulseSenderThread.joinable() ) { - pulseSenderThread.join(); - } } void connected(BTDeviceRef device, const uint16_t initialMTU) override { diff --git a/trial/direct_bt/dbt_server01.hpp b/trial/direct_bt/dbt_server01.hpp index 386a4bb7..e435b83d 100644 --- a/trial/direct_bt/dbt_server01.hpp +++ b/trial/direct_bt/dbt_server01.hpp @@ -310,8 +310,7 @@ class DBTServer01 : public DBTServerTest { class MyGATTServerListener : public DBGattServer::Listener { private: DBTServer01& parent; - std::thread pulseSenderThread; - jau::sc_atomic_bool stopPulseSenderFlag = false; + jau::service_runner pulse_service; jau::sc_atomic_uint16 handlePulseDataNotify = 0; jau::sc_atomic_uint16 handlePulseDataIndicate = 0; @@ -320,48 +319,43 @@ class DBTServer01 : public DBTServerTest { uint16_t usedMTU = BTGattHandler::number(BTGattHandler::Defaults::MIN_ATT_MTU); - bool shallStopPulseSender() { - const std::lock_guard<std::mutex> lock(parent.mtx_sync); // RAII-style acquire and relinquish via destructor - return stopPulseSenderFlag; + void pulse_worker_init(jau::service_runner& sr) noexcept { + (void)sr; + const BTDeviceRef connectedDevice_ = parent.getDevice(); + const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; + fprintf_td(stderr, "****** Server GATT::PULSE Start %s\n", connectedDeviceStr.c_str()); } - - void pulseSender() { - { - const BTDeviceRef connectedDevice_ = parent.getDevice(); - const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; - fprintf_td(stderr, "****** Server GATT::PULSE Start %s\n", connectedDeviceStr.c_str()); - } - while( !shallStopPulseSender() ) { - BTDeviceRef connectedDevice_ = parent.getDevice(); - if( nullptr != connectedDevice_ && connectedDevice_->getConnected() ) { - if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) { - std::string data( "Dynamic Data Example. Elapsed Milliseconds: "+jau::to_decstring(environment::getElapsedMillisecond(), ',', 9) ); - jau::POctets v(data.size()+1, jau::endian::little); - v.put_string_nc(0, data, v.size(), true /* includeEOS */); - if( 0 != handlePulseDataNotify ) { - if( GATT_VERBOSE ) { - fprintf_td(stderr, "****** Server GATT::sendNotification: PULSE to %s\n", connectedDevice_->toString().c_str()); - } - connectedDevice_->sendNotification(handlePulseDataNotify, v); + void pulse_worker(jau::service_runner& sr) noexcept { + BTDeviceRef connectedDevice_ = parent.getDevice(); + if( nullptr != connectedDevice_ && connectedDevice_->getConnected() ) { + if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) { + std::string data( "Dynamic Data Example. Elapsed Milliseconds: "+jau::to_decstring(environment::getElapsedMillisecond(), ',', 9) ); + jau::POctets v(data.size()+1, jau::endian::little); + v.put_string_nc(0, data, v.size(), true /* includeEOS */); + if( 0 != handlePulseDataNotify ) { + if( GATT_VERBOSE ) { + fprintf_td(stderr, "****** Server GATT::sendNotification: PULSE to %s\n", connectedDevice_->toString().c_str()); } - if( 0 != handlePulseDataIndicate ) { - if( GATT_VERBOSE ) { - fprintf_td(stderr, "****** Server GATT::sendIndication: PULSE to %s\n", connectedDevice_->toString().c_str()); - } - connectedDevice_->sendIndication(handlePulseDataIndicate, v); + connectedDevice_->sendNotification(handlePulseDataNotify, v); + } + if( 0 != handlePulseDataIndicate ) { + if( GATT_VERBOSE ) { + fprintf_td(stderr, "****** Server GATT::sendIndication: PULSE to %s\n", connectedDevice_->toString().c_str()); } + connectedDevice_->sendIndication(handlePulseDataIndicate, v); } } - if( !shallStopPulseSender() ) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } } - { - const BTDeviceRef connectedDevice_ = parent.getDevice(); - const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; - fprintf_td(stderr, "****** Server GATT::PULSE End %s\n", connectedDeviceStr.c_str()); + if( !sr.shall_stop() ) { + jau::sleep_for( 100_ms ); } } + void pulse_worker_end(jau::service_runner& sr) noexcept { + (void)sr; + const BTDeviceRef connectedDevice_ = parent.getDevice(); + const std::string connectedDeviceStr = nullptr != connectedDevice_ ? connectedDevice_->toString() : "n/a"; + fprintf_td(stderr, "****** Server GATT::PULSE End %s\n", connectedDeviceStr.c_str()); + } void sendResponse(jau::POctets data) { BTDeviceRef connectedDevice_ = parent.getDevice(); @@ -406,14 +400,17 @@ class DBTServer01 : public DBTServerTest { public: MyGATTServerListener(DBTServer01& p) - : parent(p), pulseSenderThread(&MyGATTServerListener::pulseSender, this) - { } + : parent(p), + pulse_service("MyGATTServerListener::pulse", THREAD_SHUTDOWN_TIMEOUT_MS, + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker), + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker_init), + jau::bindMemberFunc(this, &MyGATTServerListener::pulse_worker_end)) + { + pulse_service.start(); + } ~MyGATTServerListener() noexcept { - stopPulseSenderFlag = true; - if( pulseSenderThread.joinable() ) { - pulseSenderThread.join(); - } + pulse_service.stop(); } void clear() { @@ -429,14 +426,8 @@ class DBTServer01 : public DBTServerTest { } void close() noexcept { + pulse_service.stop(); clear(); - { - const std::lock_guard<std::mutex> lock(parent.mtx_sync); // RAII-style acquire and relinquish via destructor - stopPulseSenderFlag = true; - } - if( pulseSenderThread.joinable() ) { - pulseSenderThread.join(); - } } public: |