Skip to content

Commit f3dcf47

Browse files
committed
ENH: Subscribe to PowerStateChanged and WifiStateChanged callbacks
Set CCCD for PowerPublishedChar and WifiPublishedCharChar to notify so that callbacks are invoked when data is received. The WifiStateChanged callback is still not called however, so add a final check after timeout to see if the Wifi info has been received.
1 parent 92628f7 commit f3dcf47

1 file changed

Lines changed: 36 additions & 13 deletions

File tree

‎src/PlusDataCollection/Clarius/ClariusBLE.cxx‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class ClariusBLEPrivate
194194

195195
// setup function for the Clarius WiFi service and related characteristics
196196
PlusStatus SetupWifiService();
197-
197+
198198
// call this immediately after SetupPowerService / SetupWifiService members
199199
// to ensure state is initialized correctly
200200
PlusStatus InitializeState();
@@ -205,6 +205,9 @@ class ClariusBLEPrivate
205205
// convert GattCommunicationStatus enum to string representations
206206
std::string GattCommunicationStatusToString(GattCommunicationStatus status);
207207

208+
// parses Clarius WiFi info string into this->WifiInfo
209+
void ProcessWifiInfo(std::string info);
210+
208211
// members
209212

210213
// pointer to ClariusBLE instance
@@ -253,9 +256,6 @@ class ClariusBLEPrivate
253256
GattCharacteristic& characteristic
254257
);
255258

256-
// parses Clarius WiFi info string into this->WifiInfo
257-
void ProcessWifiInfo(std::string info);
258-
259259
// helper for ProcessWifiInfo to split string up
260260
std::vector<std::string> TokenizeString(std::string str, const char delimiter);
261261

@@ -314,7 +314,7 @@ void ClariusBLEPrivate::PowerStateChanged(GattCharacteristic sender, GattValueCh
314314
void ClariusBLEPrivate::WifiStateChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
315315
{
316316
DataReader reader = DataReader::FromBuffer(args.CharacteristicValue());
317-
winrt::hstring wifiHString= reader.ReadString(reader.UnconsumedBufferLength());
317+
winrt::hstring wifiHString = reader.ReadString(reader.UnconsumedBufferLength());
318318
std::string wifiStr = to_narrow_string(wifiHString);
319319
this->ProcessWifiInfo(wifiStr);
320320

@@ -344,6 +344,8 @@ PlusStatus ClariusBLEPrivate::SetupPowerService()
344344
}
345345

346346
// subscribe to power state changes
347+
this->PowerPublishedChar.WriteClientCharacteristicConfigurationDescriptorAsync(
348+
GattClientCharacteristicConfigurationDescriptorValue::Notify);
347349
this->PowerPublishedChar.ValueChanged({ this, &ClariusBLEPrivate::PowerStateChanged });
348350

349351
return PLUS_SUCCESS;
@@ -371,6 +373,9 @@ PlusStatus ClariusBLEPrivate::SetupWifiService()
371373
}
372374

373375
// subscribe to wifi state changes
376+
// TODO: Even after subscribing, WifiStateChanged callback doesn't seem to be called.
377+
this->WifiPublishedChar.WriteClientCharacteristicConfigurationDescriptorAsync(
378+
GattClientCharacteristicConfigurationDescriptorValue::Notify);
374379
this->WifiPublishedChar.ValueChanged({ this, &ClariusBLEPrivate::WifiStateChanged });
375380

376381
return PLUS_SUCCESS;
@@ -380,7 +385,7 @@ PlusStatus ClariusBLEPrivate::SetupWifiService()
380385
PlusStatus ClariusBLEPrivate::InitializeState()
381386
{
382387
// initialize power state
383-
IAsyncOperation<GattReadResult> powerOp =
388+
IAsyncOperation<GattReadResult> powerOp =
384389
this->PowerPublishedChar.ReadValueAsync(BluetoothCacheMode::Uncached);
385390
if (await_async(powerOp) != PLUS_SUCCESS)
386391
{
@@ -431,7 +436,7 @@ PlusStatus ClariusBLEPrivate::InitializeState()
431436
winrt::hstring wifiHString = wifiReader.ReadString(wifiReader.UnconsumedBufferLength());
432437
std::string wifiStr = to_narrow_string(wifiHString);
433438
this->ProcessWifiInfo(wifiStr);
434-
439+
435440
return PLUS_SUCCESS;
436441
}
437442

@@ -471,7 +476,7 @@ PlusStatus ClariusBLEPrivate::RetrieveService(const guid& serviceUuid, GattDevic
471476
}
472477

473478
GattDeviceServicesResult servicesResult = getServicesOp.GetResults();
474-
479+
475480
// check that GetGattServicesForUuidAsync returned a successful status
476481
if (servicesResult.Status() != GattCommunicationStatus::Success)
477482
{
@@ -629,13 +634,13 @@ void ClariusBLEPrivate::ProcessWifiInfo(std::string info)
629634
std::string castTag = "cast: ";
630635
std::string castStrInt = castStr.replace(0, castTag.length(), "");
631636
newInfo.CastPort = std::stoi(castStrInt);
632-
637+
633638
// parse Channel
634639
std::string channelStr = infoList.at(8);
635640
std::string channelTag = "channel: ";
636641
std::string channelStrInt = channelStr.replace(0, channelTag.length(), "");
637642
newInfo.Channel = std::stoi(channelStrInt);
638-
643+
639644
// set WifiInfo member variable and set variable
640645
this->WifiInfo = newInfo;
641646
this->WifiInfoSet = true;
@@ -676,7 +681,7 @@ PlusStatus ClariusBLE::FindBySerial(std::string serialNum)
676681
{
677682
std::string fullBleName = "CUS-" + serialNum;
678683
_impl->ProbeId = to_wide_string(fullBleName);
679-
684+
680685
DeviceWatcher deviceWatcher{ nullptr };
681686
winrt::hstring aqsFilter{ BluetoothLEDevice::GetDeviceSelectorFromPairingState(true) };
682687
deviceWatcher = DeviceInformation::CreateWatcher(
@@ -729,7 +734,7 @@ PlusStatus ClariusBLE::Connect()
729734

730735
// get BLE device
731736
IAsyncOperation<BluetoothLEDevice> deviceOp = BluetoothLEDevice::FromIdAsync(_impl->DeviceInfo.Id());
732-
737+
733738
if (await_async(deviceOp) != PLUS_SUCCESS)
734739
{
735740
return PLUS_FAIL;
@@ -852,7 +857,25 @@ bool ClariusBLE::AwaitWifiInfoReady()
852857
// wait for wifi info to be set
853858
std::future<void> wifiInfoFuture = _impl->WifiInfoPromise.get_future();
854859

855-
if (wifiInfoFuture.wait_for(std::chrono::seconds(POWER_ON_TIMEOUT_SEC)) != std::future_status::ready)
860+
if (wifiInfoFuture.wait_for(std::chrono::seconds(POWER_ON_TIMEOUT_SEC)) == std::future_status::ready)
861+
{
862+
return true;
863+
}
864+
865+
// TODO: WifiStateChanged callback is not currently being called.
866+
// After waiting for the timeout, try once to see if WifiPublishedChar has been updated.
867+
IAsyncOperation<GattReadResult> wifiOp =
868+
_impl->WifiPublishedChar.ReadValueAsync(BluetoothCacheMode::Uncached);
869+
if (await_async(wifiOp) == PLUS_SUCCESS)
870+
{
871+
GattReadResult wifiResult = wifiOp.GetResults();
872+
DataReader wifiReader = DataReader::FromBuffer(wifiResult.Value());
873+
winrt::hstring wifiHString = wifiReader.ReadString(wifiReader.UnconsumedBufferLength());
874+
std::string wifiStr = to_narrow_string(wifiHString);
875+
_impl->ProcessWifiInfo(wifiStr);
876+
}
877+
878+
if (!_impl->WifiInfoSet)
856879
{
857880
std::stringstream msg;
858881
msg << "Clarius probe took longer than the maximum allowed " << POWER_ON_TIMEOUT_SEC

0 commit comments

Comments
 (0)