edrosten / libblepp

Modern clean C++ Bluetooth Low Energy on Linux without the Bluez DBUS API
Other
239 stars 62 forks source link

Subscribing to several characteristics in service #40

Open asmusbhansen opened 5 years ago

asmusbhansen commented 5 years ago

Hi,

Thanks to your great examples i was able to connect to my device and read characteristic values with notifications. However, when i try to get the device to notify on several characteristics i mess up the state machine.

I have expanded the already existing lambda. The program runs as intended with only the first of the two if statements.

std::function<void()> cb = [&gatt, &notify_cb_roll, &notify_cb_pitch, &enable](){

        pretty_print_tree(gatt);

        for(auto& service: gatt.primary_services)
            for(auto& characteristic: service.characteristics) {

                if(service.uuid == UUID("8efc0000-6ee9-4c70-8615-3456c364d7e6") && characteristic.uuid == UUID("8efc0001-6ee9-4c70-8615-3456c364d7e6"))
                {
                    cout << "Found and subscribed to roll characteristic\n";
                    characteristic.cb_notify_or_indicate = notify_cb_roll;
                    characteristic.set_notify_and_indicate(enable, false);
                }

                if(service.uuid == UUID("8efc0000-6ee9-4c70-8615-3456c364d7e6") && characteristic.uuid == UUID("8efc0002-6ee9-4c70-8615-3456c364d7e6"))
                {

                    cout << "Found and subscribed to pitch characteristic\n";
                    characteristic.cb_notify_or_indicate = notify_cb_pitch;
                    characteristic.set_notify_and_indicate(enable, false);

                }

            }

    };

When the code looks as above i get the following output when connecting to my device.

info 1543589836.865952: Socket success: 228 (/home/asmus/Documents/Projects/CppBLE/libblepp-master/src/blestatemachine.cc) info 1543589836.866000: Socket success: 271 (/home/asmus/Documents/Projects/CppBLE/libblepp-master/src/blestatemachine.cc) info 1543589836.866020: Socket success: 174 (/home/asmus/Documents/Projects/CppBLE/libblepp-master/src/blestatemachine.cc) info 1543589836.866036: options.omtu = 672 info 1543589836.866053: options.imtu = 672 info 1543589836.866067: options.flush_to = 65535 info 1543589836.866080: options.mode = 0 info 1543589836.866095: options.fcs = 1 info 1543589836.866109: options.max_tx = 3 info 1543589836.866121: options.txwin_size = 63 error 1543589836.866153: Error on line: 298 (/home/asmus/Documents/Projects/CppBLE/libblepp-master/src/blestatemachine.cc): Operation now in progress Probably connected? info 1543589837.321752: Socket success: 493 (/home/asmus/Documents/Projects/CppBLE/libblepp-master/src/blestatemachine.cc) info 1543589837.321819: errval = Success From read_and_process_next - We're in state: 3 From read_and_process_next - We're in state: 3 From read_and_process_next - We're in state: 4 From read_and_process_next - We're in state: 4 From read_and_process_next - We're in state: 4 From read_and_process_next - We're in state: 4 From read_and_process_next - We're in state: 4 From read_and_process_next - We're in state: 5 From read_and_process_next - We're in state: 5 Primary services: Start: 0001 End: 0009 UUID: 1800 org.bluetooth.service.generic_access: Generic Access Characteristic: 2a00 Start: 0002 End: 0003 Flags: Read Write Value at handle: 3

Characteristic: 2a01 Start: 0004 End: 0005 Flags: Read Value at handle: 5

Characteristic: 2a04 Start: 0006 End: 0007 Flags: Read Value at handle: 7

Characteristic: 2aa6 Start: 0008 End: 0009 Flags: Read Value at handle: 9

Start: 000a End: 000d UUID: 1801 org.bluetooth.service.generic_attribute: Generic Attribute Characteristic: 2a05 Start: 000b End: 000d Flags: Indicate Value at handle: 12 CCC: (000d) 0000

Start: 000e End: ffff UUID: 8efc0000-6ee9-4c70-8615-3456c364d7e6 Unknown Characteristic: 8efc0001-6ee9-4c70-8615-3456c364d7e6 Start: 000f End: 0011 Flags: Read Write Notify Value at handle: 16 CCC: (0011) 0000

Characteristic: 8efc0002-6ee9-4c70-8615-3456c364d7e6 Start: 0012 End: ffff Flags: Notify Value at handle: 19 CCC: (0014) 0000

Found and subscribed to roll characteristic In set_notify_and_indicate In set_notify_and_indicate - State:6 Found and subscribed to pitch characteristic In set_notify_and_indicate Oops, someone fouled up: Error trying to issue command mid state info 1543589838.339664: Socket success: 151 (/home/asmus/Documents/Projects/CppBLE/libblepp-master/src/blestatemachine.cc)

To debug the program it prints the state and which function it exits from. When the state machine enters the set_notify_and_indicate function the state is checked and it exists if the state is not idle. At the end of the function, the state is set to AwaitingWriteResponse before state_machine_write() is called. Next time the function is called it exits, since the state is still AwaitingWriteResponse.

I suspect that this is not really a bug, but rather a lack of understanding of the state machine. How do i properly turn on notifications for several characteristics, without breaking the state machine?