ukBaz / python-bluezero

A simple Python interface to Bluez
MIT License
387 stars 112 forks source link

Characteristic Length Error #328

Closed Owenber123 closed 3 years ago

Owenber123 commented 3 years ago

I have just resized all of my characteristics to take 1 - 16 bytes on my embedded peripheral bluetooth device and when I try to write a 16 byte value to one of the characteristics I get a bluezero error of "...threw an exception: Expected a length-1 bytes but found 16 bytes". I have confirmed with bluetoothctl that writing a 16 byte value works just fine. I have removed all bluetooth caching from my central device and can confirm that the other characteristic reads are working by receiving 16 byte values and validating them. I am convinced that at no point does the central configure the characteristic lengths and its all done by the peripheral. Am I missing something? Thanks in advance.

ukBaz commented 3 years ago

How have you resized your characteristics?

Owenber123 commented 3 years ago

I am using the Nordic nrf52840. When configuring characteristics I have set the max length to 16.

ukBaz commented 3 years ago

I will need some more information to help you debug this.

When running your script have separate terminals open with the following running to get more debug information:

Hopefully there will be something in there that narrows down where the issue is.

Owenber123 commented 3 years ago

Here are the results: Thanks for the help!

bluetootctl displays the characteristics uuid properly:

[NEW] Characteristic (Handle 0xfb5b)
    /org/bluez/hci0/dev_DA_AE_22_8E_37_C6/service000e/char002b
    2a191b22-9dc6-4b0b-bb11-9eae903da5c2
    Vendor specific

jounalctl -f -u bluetooth: bluetoothd[2297892]: src/device.c:load_gatt_db() No cache for DA_AE_22_8E_37_C6

sudo busctl monitor org.bluez:

DICT_ENTRY "oa{sa{sv}}" {
                          OBJECT_PATH "/org/bluez/hci0/dev_DA_AE_22_8E_37_C6/service000e/char002b";
                          ARRAY "{sa{sv}}" {
                                  DICT_ENTRY "sa{sv}" {
                                          STRING "org.freedesktop.DBus.Introspectable";
                                          ARRAY "{sv}" {
                                          };
                                  };
                                  DICT_ENTRY "sa{sv}" {
                                          STRING "org.bluez.GattCharacteristic1";
                                          ARRAY "{sv}" {
                                                  DICT_ENTRY "sv" {
                                                          STRING "UUID";
                                                          VARIANT "s" {
                                                                  STRING "2a191b22-9dc6-4b0b-bb11-9eae903da5c2";
                                                          };
                                                  };
                                                  DICT_ENTRY "sv" {
                                                          STRING "Service";
                                                          VARIANT "o" {
                                                                  OBJECT_PATH "/org/bluez/hci0/dev_DA_AE_22_8E_37_C6/service000e";
                                                          };
                                                  };
                                                  DICT_ENTRY "sv" {
                                                          STRING "Value";
                                                          VARIANT "ay" {
                                                                  ARRAY "y" {
                                                                  };
                                                          };
                                                  };
                                                  DICT_ENTRY "sv" {
                                                          STRING "Flags";
                                                          VARIANT "as" {
                                                                  ARRAY "s" {
                                                                          STRING "read";
                                                                          STRING "write";
                                                                  };
                                                          };
                                                  };
                                          };
                                  };
                                  DICT_ENTRY "sa{sv}" {
                                          STRING "org.freedesktop.DBus.Properties";
                                          ARRAY "{sv}" {
                                          };
                                  };
                          };
                  };

Here is something that also stands out from busctl monitor org.bluez

‣ Type=method_call  Endian=l  Flags=0  Version=1 Cookie=3663
  Sender=:1.2645  Destination=:1.2643  Path=/org/bluez/hci0/dev_DA_AE_22_8E_37_C6/service000e/char002b  Interface=org.freedesktop.DBus.Introspectable  Member=Introspect
  UniqueName=:1.2645
  MESSAGE "" {
  };

‣ Type=method_return  Endian=l  Flags=1  Version=1 Cookie=3008  ReplyCookie=3663
  Sender=:1.2643  Destination=:1.2645
  UniqueName=:1.2643
  MESSAGE "s" {
          STRING "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node><interface name="org.freedesktop.DBus.Introspectable"><method name="Introspect"><arg name="xml" type="s" direction="out"/>
</method></interface><interface name="org.bluez.GattCharacteristic1"><method name="ReadValue"><arg name="options" type="a{sv}" direction="in"/>
<arg name="value" type="ay" direction="out"/>
</method><method name="WriteValue"><arg name="value" type="ay" direction="in"/>
<arg name="options" type="a{sv}" direction="in"/>
</method><method name="AcquireWrite"><arg name="options" type="a{sv}" direction="in"/>
<arg name="fd" type="h" direction="out"/>
<arg name="mtu" type="q" direction="out"/>
</method><method name="AcquireNotify"><arg name="options" type="a{sv}" direction="in"/>
<arg name="fd" type="h" direction="out"/>
<arg name="mtu" type="q" direction="out"/>
</method><method name="StartNotify"></method><method name="StopNotify"></method><property name="UUID" type="s" access="read"></property><property name="Service" type="o" access="read"></property><property name="Value" type="ay" access="read"></property><property name="Notifying" type="b" access="read"></property><property name="Flags" type="as" access="read"></property><property name="WriteAcquired" type="b" access="read"></property><property name="NotifyAcquired" type="b" access="read"></property></interface><interface name="org.freedesktop.DBus.Properties"><method name="Get"><arg name="interface" type="s" direction="in"/>
<arg name="name" type="s" direction="in"/>
<arg name="value" type="v" direction="out"/>
</method><method name="Set"><arg name="interface" type="s" direction="in"/>
<arg name="name" type="s" direction="in"/>
<arg name="value" type="v" direction="in"/>
</method><method name="GetAll"><arg name="interface" type="s" direction="in"/>
<arg name="properties" type="a{sv}" direction="out"/>
</method><signal name="PropertiesChanged"><arg name="interface" type="s"/>
<arg name="changed_properties" type="a{sv}"/>
<arg name="invalidated_properties" type="as"/>
</signal>
</interface></node>";
  };

and lastly sudo btmon starting with the request write of the characteristic:

< ACL Data TX: Han.. flags 0x00 dlen 39  #1633 [hci0] 117.400033
      ATT: Write Request (0x12) len 34
        Handle: 0x0014
          Data: e563d03f2f38a2133f1048dd7c889045262ebd88e70338576e17847b71eb718d ===========> VALUE IM WRITING
> HCI Event: Number of... (0x13) plen 5  #1634 [hci0] 117.437244
        Num handles: 1
        Handle: 16
        Count: 1
> ACL Data RX: Hand.. flags 0x02 dlen 5  #1635 [hci0] 117.481041
      ATT: Write Response (0x13) len 0
< ACL Data TX: Hand.. flags 0x00 dlen 7  #1636 [hci0] 117.481679
      ATT: Read Request (0x0a) len 2
        Handle: 0x0014
> HCI Event: Number of... (0x13) plen 5  #1637 [hci0] 117.526210
        Num handles: 1
        Handle: 16
        Count: 1
> ACL Data RX: Hand.. flags 0x02 dlen 6  #1638 [hci0] 117.571066
      ATT: Read Response (0x0b) len 1
        Value: 31
< ACL Data TX: Hand.. flags 0x00 dlen 7  #1639 [hci0] 117.590482
      ATT: Read Request (0x0a) len 2
        Handle: 0x0010
> HCI Event: Number of... (0x13) plen 5  #1640 [hci0] 117.616213
        Num handles: 1
        Handle: 16
        Count: 1
> ACL Data RX: Han.. flags 0x02 dlen 21  #1641 [hci0] 117.661225
      ATT: Read Response (0x0b) len 16
        Value: 9f27acbdf08ded39d91b71c68569c693
> ACL Data RX: Han.. flags 0x02 dlen 16  #1642 [hci0] 119.821226
      LE L2CAP: Connection Parameter Update Request (0x12) ident 3 len 8
        Min interval: 12
        Max interval: 24
        Slave latency: 0
        Timeout multiplier: 400
@ MGMT Event: New.. (0x001c) plen 16  {0x0001} [hci0] 119.821266
        Store hint: Reserved (0xc6)
        BR/EDR Address: 02:DA:AE:22:8E:37 (OUI 02-DA-AE)
        Min connection interval: 12
        Max connection interval: 24
        Connection latency: 0 (0x0000)
        Supervision timeout: 400
< ACL Data TX: Han.. flags 0x00 dlen 10  #1643 [hci0] 119.821272
      LE L2CAP: Connection Parameter Update Response (0x13) ident 3 len 2
        Result: Connection Parameters accepted (0x0000)
< HCI Command:... (0x08|0x0013) plen 14  #1644 [hci0] 119.821278
        Handle: 16
        Min connection interval: 15.00 msec (0x000c)
        Max connection interval: 30.00 msec (0x0018)
        Connection latency: 0 (0x0000)
        Supervision timeout: 4000 msec (0x0190)
        Min connection length: 0.000 msec (0x0000)
        Max connection length: 0.000 msec (0x0000)
> HCI Event: Command St.. (0x0f) plen 4  #1645 [hci0] 119.822205
      LE Connection Update (0x08|0x0013) ncmd 2
        Status: Success (0x00)
> HCI Event: Number of... (0x13) plen 5  #1646 [hci0] 119.911211
        Num handles: 1
        Handle: 16
        Count: 1
> HCI Event: LE Meta E.. (0x3e) plen 10  #1647 [hci0] 120.316213
      LE Connection Update Complete (0x03)
        Status: Success (0x00)
        Handle: 16
        Connection interval: 30.00 msec (0x0018)
        Connection latency: 0 (0x0000)
        Supervision timeout: 4000 msec (0x0190)
Owenber123 commented 3 years ago

I just needed to convert my bytes to a list of integers when writing in my setter, seems to be working fine now. Thanks for the assistance.