aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-04-15 16:00:38 +0200
committerSven Gothel <[email protected]>2022-04-15 16:00:38 +0200
commitb3484f794a8ad22d3453d68acb4a3eb52323725b (patch)
tree5c23838c9d8327f88492361dffa7f321045b4845 /examples
parentb7389ed1207ce47d133add693aebccaa13afd7ce (diff)
Align dbt_peripheral00, DBTPeripheral00 to Trial's DBTServer00
Diffstat (limited to 'examples')
-rw-r--r--examples/dbt_peripheral00.cpp93
-rw-r--r--examples/java/DBTPeripheral00.java66
2 files changed, 93 insertions, 66 deletions
diff --git a/examples/dbt_peripheral00.cpp b/examples/dbt_peripheral00.cpp
index 9d12f5a6..e214ce37 100644
--- a/examples/dbt_peripheral00.cpp
+++ b/examples/dbt_peripheral00.cpp
@@ -76,13 +76,31 @@ static std::shared_ptr<BTAdapter> chosenAdapter = nullptr;
static BTSecurityLevel adapter_sec_level = BTSecurityLevel::UNSET;
static bool SHOW_UPDATE_EVENTS = false;
static bool RUN_ONLY_ONCE = false;
+static jau::sc_atomic_bool sync_data;
+static BTDeviceRef connectedDevice = nullptr;
+
static jau::relaxed_atomic_nsize_t servedConnections = 0;
static bool startAdvertising(BTAdapter *a, std::string msg);
static bool stopAdvertising(BTAdapter *a, std::string msg);
-static void processReadyDevice(BTDeviceRef device);
static void processDisconnectedDevice(BTDeviceRef device);
+static void setDevice(const BTDeviceRef cd) {
+ jau::sc_atomic_critical sync(sync_data);
+ connectedDevice = cd;
+}
+
+static BTDeviceRef getDevice() {
+ jau::sc_atomic_critical sync(sync_data);
+ return connectedDevice;
+}
+
+static bool matches(const BTDeviceRef& device) {
+ const BTDeviceRef d = getDevice();
+ return nullptr != d ? (*d) == *device : false;
+}
+
+
static jau::POctets make_poctets(const char* name) {
return jau::POctets( (const uint8_t*)name, (nsize_t)strlen(name), endian::little );
}
@@ -231,7 +249,11 @@ class MyAdapterStatusListener : public AdapterStatusListener {
void deviceConnected(BTDeviceRef device, const uint16_t handle, const uint64_t timestamp) override {
fprintf_td(stderr, "****** CONNECTED: %s\n", device->toString(true).c_str());
-
+ const bool available = nullptr == getDevice();
+ if( available ) {
+ setDevice(device);
+ BTDeviceRegistry::addToProcessingDevices(device->getAddressAndType(), device->getName());
+ }
(void)handle;
(void)timestamp;
}
@@ -295,18 +317,7 @@ class MyAdapterStatusListener : public AdapterStatusListener {
void deviceReady(BTDeviceRef device, const uint64_t timestamp) override {
(void)timestamp;
- if( !BTDeviceRegistry::isDeviceProcessing( device->getAddressAndType() ) &&
- ( !BTDeviceRegistry::isWaitingForAnyDevice() ||
- BTDeviceRegistry::isWaitingForDevice(device->getAddressAndType().address, device->getName())
- )
- )
- {
- fprintf_td(stderr, "****** READY-0: Processing %s\n", device->toString(true).c_str());
- BTDeviceRegistry::addToProcessingDevices(device->getAddressAndType(), device->getName());
- processReadyDevice(device); // AdapterStatusListener::deviceReady() explicitly allows prolonged and complex code execution!
- } else {
- fprintf_td(stderr, "****** READY-1: NOP %s\n", device->toString(true).c_str());
- }
+ fprintf_td(stderr, "****** READY-1: NOP %s\n", device->toString(true).c_str());
}
void deviceDisconnected(BTDeviceRef device, const HCIStatusCode reason, const uint16_t handle, const uint64_t timestamp) override {
@@ -315,6 +326,10 @@ class MyAdapterStatusListener : public AdapterStatusListener {
servedConnections.load(), static_cast<uint8_t>(reason), to_string(reason).c_str(),
to_hexstring(handle).c_str(), device->toString(true).c_str());
+ const bool match = matches(device);
+ if( match ) {
+ setDevice(nullptr);
+ }
std::thread sd(::processDisconnectedDevice, device); // @suppress("Invalid arguments")
sd.detach();
(void)timestamp;
@@ -328,7 +343,6 @@ class MyAdapterStatusListener : public AdapterStatusListener {
class MyGATTServerListener : public DBGattServer::Listener {
private:
- mutable jau::sc_atomic_bool sync_data;
std::thread pulseSenderThread;
jau::sc_atomic_bool stopPulseSender = false;
@@ -337,14 +351,8 @@ class MyGATTServerListener : public DBGattServer::Listener {
jau::sc_atomic_uint16 handleResponseDataNotify = 0;
jau::sc_atomic_uint16 handleResponseDataIndicate = 0;
- BTDeviceRef connectedDevice;
uint16_t usedMTU = BTGattHandler::number(BTGattHandler::Defaults::MIN_ATT_MTU);
- bool matches(const BTDeviceRef& device) const {
- jau::sc_atomic_critical sync(sync_data);
- return nullptr != connectedDevice ? *connectedDevice == *device : false;
- }
-
void clear() {
jau::sc_atomic_critical sync(sync_data);
@@ -352,7 +360,6 @@ class MyGATTServerListener : public DBGattServer::Listener {
handlePulseDataIndicate = 0;
handleResponseDataNotify = 0;
handleResponseDataIndicate = 0;
- connectedDevice = nullptr;
dbGattServer->resetGattClientCharConfig(DataServiceUUID, PulseDataUUID);
dbGattServer->resetGattClientCharConfig(DataServiceUUID, ResponseUUID);
@@ -362,18 +369,19 @@ class MyGATTServerListener : public DBGattServer::Listener {
while( !stopPulseSender ) {
{
jau::sc_atomic_critical sync(sync_data);
- if( nullptr != connectedDevice && connectedDevice->getConnected() ) {
+ 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);
+ 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);
+ fprintf_td(stderr, "****** GATT::sendIndication: PULSE to %s\n", connectedDevice_->toString().c_str());
+ connectedDevice_->sendIndication(handlePulseDataIndicate, v);
}
}
}
@@ -383,17 +391,18 @@ class MyGATTServerListener : public DBGattServer::Listener {
}
void sendResponse(jau::POctets data) {
- if( nullptr != connectedDevice && connectedDevice->getConnected() ) {
+ BTDeviceRef connectedDevice_ = getDevice();
+ if( nullptr != connectedDevice_ && connectedDevice_->getConnected() ) {
if( 0 != handleResponseDataNotify || 0 != handleResponseDataIndicate ) {
if( 0 != handleResponseDataNotify ) {
fprintf_td(stderr, "****** GATT::sendNotification: %s to %s\n",
- data.toString().c_str(), connectedDevice->toString().c_str());
- connectedDevice->sendNotification(handleResponseDataNotify, data);
+ data.toString().c_str(), connectedDevice_->toString().c_str());
+ connectedDevice_->sendNotification(handleResponseDataNotify, data);
}
if( 0 != handleResponseDataIndicate ) {
fprintf_td(stderr, "****** GATT::sendIndication: %s to %s\n",
- data.toString().c_str(), connectedDevice->toString().c_str());
- connectedDevice->sendIndication(handleResponseDataIndicate, data);
+ data.toString().c_str(), connectedDevice_->toString().c_str());
+ connectedDevice_->sendIndication(handleResponseDataIndicate, data);
}
}
}
@@ -410,7 +419,6 @@ class MyGATTServerListener : public DBGattServer::Listener {
{
jau::sc_atomic_critical sync(sync_data);
stopPulseSender = true;
- connectedDevice = nullptr;
}
if( pulseSenderThread.joinable() ) {
pulseSenderThread.join();
@@ -419,18 +427,17 @@ class MyGATTServerListener : public DBGattServer::Listener {
void connected(BTDeviceRef device, const uint16_t initialMTU) override {
jau::sc_atomic_critical sync(sync_data);
- const bool available = nullptr == connectedDevice;
- fprintf_td(stderr, "****** GATT::connected(available %d): initMTU %d, %s\n",
- available, (int)initialMTU, device->toString().c_str());
- if( available ) {
- connectedDevice = device;
+ const bool match = matches(device);
+ fprintf_td(stderr, "****** GATT::connected(match %d): initMTU %d, %s\n",
+ match, (int)initialMTU, device->toString().c_str());
+ if( match ) {
usedMTU = initialMTU;
}
}
void disconnected(BTDeviceRef device) override {
jau::sc_atomic_critical sync(sync_data);
- const bool match = nullptr != connectedDevice ? *connectedDevice == *device : false;
+ const bool match = matches(device);
fprintf_td(stderr, "****** GATT::disconnected(match %d): %s\n", match, device->toString().c_str());
if( match ) {
clear();
@@ -577,7 +584,9 @@ static void processDisconnectedDevice(BTDeviceRef device) {
// already unpaired
stopAdvertising(&device->getAdapter(), "device-disconnected");
+ device->remove();
BTDeviceRegistry::removeFromProcessingDevices(device->getAddressAndType());
+
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait a little (FIXME: Fast restart of advertising error)
if( !RUN_ONLY_ONCE ) {
@@ -587,12 +596,6 @@ static void processDisconnectedDevice(BTDeviceRef device) {
fprintf_td(stderr, "****** Disonnected Device: End %s\n", device->toString().c_str());
}
-static void processReadyDevice(BTDeviceRef device) {
- fprintf_td(stderr, "****** Processing Ready Device: Start %s\n", device->toString().c_str());
-
- fprintf_td(stderr, "****** Processing Ready Device: End %s\n", device->toString().c_str());
-}
-
static bool initAdapter(std::shared_ptr<BTAdapter>& adapter) {
if( useAdapter != EUI48::ALL_DEVICE && useAdapter != adapter->getAddressAndType().address ) {
fprintf_td(stderr, "initAdapter: Adapter not selected: %s\n", adapter->toString().c_str());
diff --git a/examples/java/DBTPeripheral00.java b/examples/java/DBTPeripheral00.java
index a09e6fc6..567305c7 100644
--- a/examples/java/DBTPeripheral00.java
+++ b/examples/java/DBTPeripheral00.java
@@ -81,9 +81,28 @@ public class DBTPeripheral00 {
BTSecurityLevel adapter_sec_level = BTSecurityLevel.UNSET;
boolean SHOW_UPDATE_EVENTS = false;
boolean RUN_ONLY_ONCE = false;
+ private volatile boolean sync_data;
+ private volatile BTDevice connectedDevice;
volatile int servedConnections = 0;
+ private final void setDevice(final BTDevice cd) {
+ final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
+ connectedDevice = cd;
+ sync_data = local; // SC-DRF release via sc_atomic_bool::store()
+ }
+
+ private final BTDevice getDevice() {
+ final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
+ final BTDevice d = connectedDevice;
+ sync_data = local; // SC-DRF release via sc_atomic_bool::store()
+ return d;
+ }
+
+ private boolean matches(final BTDevice device) {
+ final BTDevice d = getDevice();
+ return null != d ? d.equals(device) : false;
+ }
boolean matches(final List<BDAddressAndType> cont, final BDAddressAndType mac) {
for(final Iterator<BDAddressAndType> it = cont.iterator(); it.hasNext(); ) {
if( it.next().matches(mac) ) {
@@ -264,6 +283,11 @@ public class DBTPeripheral00 {
@Override
public void deviceConnected(final BTDevice device, final short handle, final long timestamp) {
BTUtils.println(System.err, "****** CONNECTED: "+device.toString());
+ final boolean available = null == getDevice();
+ if( available ) {
+ setDevice(device);
+ BTDeviceRegistry.addToProcessingDevices(device.getAddressAndType(), device.getName());
+ }
}
@Override
@@ -327,7 +351,10 @@ public class DBTPeripheral00 {
public void deviceDisconnected(final BTDevice device, final HCIStatusCode reason, final short handle, final long timestamp) {
servedConnections++;
BTUtils.println(System.err, "****** DISCONNECTED (count "+servedConnections+"): Reason "+reason+", old handle 0x"+Integer.toHexString(handle)+": "+device+" on "+device.getAdapter());
-
+ final boolean match = matches(device);
+ if( match ) {
+ setDevice(null);
+ }
executeOffThread( () -> { processDisconnectedDevice(device); },
"DBT-Disconnected-"+device.getAdapter().getAddressAndType(), true /* detach */);
}
@@ -339,7 +366,6 @@ public class DBTPeripheral00 {
};
class MyGATTServerListener extends DBGattServer.Listener implements AutoCloseable {
- private volatile boolean sync_data;
private final Thread pulseSenderThread;
private volatile boolean stopPulseSender = false;
@@ -348,7 +374,6 @@ public class DBTPeripheral00 {
private volatile short handleResponseDataNotify = 0;
private volatile short handleResponseDataIndicate = 0;
- private BTDevice connectedDevice;
private int usedMTU = 23; // BTGattHandler::number(BTGattHandler::Defaults::MIN_ATT_MTU);
private boolean matches(final BTDevice device) {
@@ -365,7 +390,6 @@ public class DBTPeripheral00 {
handlePulseDataIndicate = 0;
handleResponseDataNotify = 0;
handleResponseDataIndicate = 0;
- connectedDevice = null;
dbGattServer.resetGattClientCharConfig(DataServiceUUID, PulseDataUUID);
dbGattServer.resetGattClientCharConfig(DataServiceUUID, ResponseUUID);
@@ -377,17 +401,18 @@ public class DBTPeripheral00 {
while( !stopPulseSender ) {
{
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
- if( null != connectedDevice && connectedDevice.getConnected() ) {
+ final BTDevice connectedDevice_ = getDevice();
+ if( null != connectedDevice_ && connectedDevice_.getConnected() ) {
if( 0 != handlePulseDataNotify || 0 != handlePulseDataIndicate ) {
final String data = String.format("Dynamic Data Example. Elapsed Milliseconds: %,9d", BTUtils.elapsedTimeMillis());
final byte[] v = data.getBytes(StandardCharsets.UTF_8);
if( 0 != handlePulseDataNotify ) {
- BTUtils.fprintf_td(System.err, "****** GATT::sendNotification: PULSE to %s\n", connectedDevice.toString());
- connectedDevice.sendNotification(handlePulseDataNotify, v);
+ BTUtils.fprintf_td(System.err, "****** GATT::sendNotification: PULSE to %s\n", connectedDevice_.toString());
+ connectedDevice_.sendNotification(handlePulseDataNotify, v);
}
if( 0 != handlePulseDataIndicate ) {
- BTUtils.fprintf_td(System.err, "****** GATT::sendIndication: PULSE to %s\n", connectedDevice.toString());
- connectedDevice.sendIndication(handlePulseDataIndicate, v);
+ BTUtils.fprintf_td(System.err, "****** GATT::sendIndication: PULSE to %s\n", connectedDevice_.toString());
+ connectedDevice_.sendIndication(handlePulseDataIndicate, v);
}
}
}
@@ -400,17 +425,18 @@ public class DBTPeripheral00 {
}
void sendResponse(final byte[] data) {
- if( null != connectedDevice && connectedDevice.getConnected() ) {
+ final BTDevice connectedDevice_ = getDevice();
+ if( null != connectedDevice_ && connectedDevice_.getConnected() ) {
if( 0 != handleResponseDataNotify || 0 != handleResponseDataIndicate ) {
if( 0 != handleResponseDataNotify ) {
BTUtils.fprintf_td(System.err, "****** GATT::sendNotification: %s to %s\n",
- BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice.toString());
- connectedDevice.sendNotification(handleResponseDataNotify, data);
+ BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice_.toString());
+ connectedDevice_.sendNotification(handleResponseDataNotify, data);
}
if( 0 != handleResponseDataIndicate ) {
BTUtils.fprintf_td(System.err, "****** GATT::sendIndication: %s to %s\n",
- BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice.toString());
- connectedDevice.sendIndication(handleResponseDataIndicate, data);
+ BTUtils.bytesHexString(data, 0, data.length, true /* lsb */), connectedDevice_.toString());
+ connectedDevice_.sendIndication(handleResponseDataIndicate, data);
}
}
}
@@ -425,7 +451,6 @@ public class DBTPeripheral00 {
{
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
stopPulseSender = true;
- connectedDevice = null;
sync_data = local; // SC-DRF release via sc_atomic_bool::store()
}
if( !pulseSenderThread.isDaemon() ) {
@@ -439,11 +464,10 @@ public class DBTPeripheral00 {
@Override
public void connected(final BTDevice device, final int initialMTU) {
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
- final boolean available = null == connectedDevice;
- BTUtils.fprintf_td(System.err, "****** GATT::connected(available %b): initMTU %d, %s\n",
- available, initialMTU, device.toString());
- if( available ) {
- connectedDevice = device;
+ final boolean match = matches(device);
+ BTUtils.fprintf_td(System.err, "****** GATT::connected(match %b): initMTU %d, %s\n",
+ match, initialMTU, device.toString());
+ if( match ) {
usedMTU = initialMTU;
}
sync_data = local; // SC-DRF release via sc_atomic_bool::store()
@@ -452,7 +476,7 @@ public class DBTPeripheral00 {
@Override
public void disconnected(final BTDevice device) {
final boolean local = sync_data; // SC-DRF acquire via sc_atomic_bool::load()
- final boolean match = null != connectedDevice ? connectedDevice.equals(device) : false;
+ final boolean match = matches(device);
BTUtils.fprintf_td(System.err, "****** GATT::disconnected(match %b): %s\n", match, device.toString());
if( match ) {
clear();