quick_blue icon indicating copy to clipboard operation
quick_blue copied to clipboard

[Windows] Getting a wrong service or characteristic causes the library to crash.

Open fotiDim opened this issue 4 years ago • 6 comments

Is I try to get a non-existent service or characteristic then the library crashes on the Windows side. The problem lies in those 2 functions. Basically it never inserts a service or characteristic in the corresponding array and it tries to return null.

IAsyncOperation<GattDeviceService> GetServiceAsync(std::string service) {
    if (gattServices.count(service) == 0) {
      auto serviceResult = co_await device.GetGattServicesAsync();
      if (serviceResult.Status() != GattCommunicationStatus::Success)
        co_return nullptr;

      for (auto s : serviceResult.Services())
        if (to_uuidstr(s.Uuid()) == service)
          gattServices.insert(std::make_pair(service, s));
    }
    co_return gattServices.at(service);
  }
  IAsyncOperation<GattCharacteristic> GetCharacteristicAsync(std::string service, std::string characteristic) {
    if (gattCharacteristics.count(characteristic) == 0) {
      auto gattService = co_await GetServiceAsync(service);

      auto characteristicResult = co_await gattService.GetCharacteristicsAsync();
      if (characteristicResult.Status() != GattCommunicationStatus::Success)
        co_return nullptr;

      for (auto c : characteristicResult.Characteristics())
        if (to_uuidstr(c.Uuid()) == characteristic)
          gattCharacteristics.insert(std::make_pair(characteristic, c));
    }
    co_return gattCharacteristics.at(characteristic);
  }

fotiDim avatar Mar 12 '22 10:03 fotiDim

Is there any peripheral I could test on with?

Sunbreak avatar Mar 12 '22 11:03 Sunbreak

I don't think that the peripheral matters. Try to connect to any device and then get a service or characteristic with a non-existent random string. You can use the example app for that. It should replicate the crash. If not then let me know and I will investigate further.

fotiDim avatar Mar 15 '22 21:03 fotiDim

If not nullptr what the expected result do you prefer?

Sunbreak avatar Mar 16 '22 03:03 Sunbreak

Well, the important thing is that the library should not crash and that on the Dart side the Future of writeValue() etc. should return an error.

In the Windows plugin GetCharacteristicAsync() can return nullptr and then WriteValueAsync() (and the rest of the methods) should check the return value of GetCharacteristicAsync and if it is null then throw an error on the Dart side.

fotiDim avatar Mar 18 '22 17:03 fotiDim

Thanks for your advise. I'd like to add a flutter driver test to ensure that case implemented on ALL platforms.

Leave the issue until then

Sunbreak avatar Mar 18 '22 21:03 Sunbreak

i also found this issue on MacOs Things i tried

Scan and Connect to device

read characteristic -> App Crashed

now again after connecting first discover Services then read characteristic , and it worked

now if i read a characteristic , unknown to device App Crashed -> same error as of first case

quick_blue_macos/QuickBlueMacosPlugin.swift:241: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Screenshot 2022-03-20 at 9 28 26 PM

rohitsangwan01 avatar Mar 20 '22 16:03 rohitsangwan01