cembeddedbluetooth-lowenergybluetooth-gattzephyr-rtos

Discover custom UUID (services, characteristics and CCCDs) with Zephyr RTOS as a BLE Central


I'm developing a BLE Central device on a nRF52840 DK with Zephyr RTOS. After connecting, pairing and bonding, I want to discover and log every services (with their characteristics and CCCDs) the peripheral has.

I haven't any problem to discover standard services (understand services that are defined by the Bluetooth Sig). Where the thing become more difficult is when I want to retrieve the custom services.

My central indeed discover the custom services but the values that are logged are the same for every services (I actually print 3 times the same UUID with different formats: short (16 bits), medium (32 bits) and long (128 bits) UUID).

This is the central discovery code:

static discover_phases_t discover_phase;
static struct bt_gatt_discover_params discover_params;

static void connected_cb(struct bt_conn *conn, uint8_t conn_err)
{
  const bt_addr_le_t * addr = bt_conn_get_dst(conn);

  if (conn_err) {
    bt_conn_unref(default_conn);
    default_conn = NULL;
    start_scan();
  }

  if (conn == default_conn)
  {
    int err = bt_conn_set_security(conn, BT_SECURITY_L3);
    if (err)
    {
      LOG_ERR("Error while pairing %d", err);
      return;
    }

    LOG_WRN("Connected: %02X:%02X:%02X:%02X:%02X:%02X", addr->a.val[5], addr->a.val[4], addr->a.val[3], addr->a.val[2], addr->a.val[1], addr->a.val[0]);

    discover_params.uuid = NULL;
    discover_params.func = discover_services_cb;
    discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
    discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
    discover_params.type = BT_GATT_DISCOVER_PRIMARY;
    discover_phase = DISC_PHASE_SERVICE;

    err = bt_gatt_discover(conn, &discover_params);
    if (err)
    {
      LOG_ERR("Discovery failed (err %d)", err);
    }
  }
}

static uint8_t discover_services_cb(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_gatt_discover_params *params)
{
  static uint16_t last_handle;

  if (attr)
    LOG_INF("[ATTRIBUTE] handle: %u  UUID type: %d  val: %x", attr->handle, attr->uuid->type, BT_UUID_16(attr->uuid)->val);

  switch (discover_phase)
  {
    case DISC_PHASE_SERVICE:
      if (attr)
      {
        struct bt_gatt_service_val *svc_attr = attr->user_data;
        if (svc_attr->uuid->type == BT_UUID_TYPE_16)
          LOG_INF("Attr is primary service UUID type: %d  val: %04x", svc_attr->uuid->type, BT_UUID_16(svc_attr->uuid)->val);
        else if (svc_attr->uuid->type == BT_UUID_TYPE_32)
          LOG_INF("Attr is UUID 32bits");
        else if (svc_attr->uuid->type == BT_UUID_TYPE_128)
        {
          LOG_INF("Attr is primary service UUID type: %d  val 16:  %04x", svc_attr->uuid->type, BT_UUID_16(svc_attr->uuid)->val);
          LOG_INF("Attr is primary service UUID type: %d  val 32:  %04x", svc_attr->uuid->type, BT_UUID_32(svc_attr->uuid)->val);
          LOG_INF("Attr is primary service UUID type: %d  val 128: %s", svc_attr->uuid->type, BT_UUID_128(svc_attr->uuid)->val);

          LOG_INF("Found Custom service");
        }
        else
        {
          LOG_ERR("discover_services_cb: UUID type check failed");
          return BT_GATT_ITER_STOP;
        }
        last_handle = attr->handle;
      }
      else
      {
        LOG_WRN("No more services, looking for characteristics");
        discover_params.uuid = NULL;
        discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
        discover_params.start_handle = last_handle + 1;

        int err = bt_gatt_discover(conn, &discover_params);
        if (err)
          LOG_ERR("Discover failed - services (err %d)", err);
        discover_phase = DISC_PHASE_CHRC;
        return BT_GATT_ITER_STOP;
      }
      break;

    case DISC_PHASE_CHRC:
      if (attr)
      {
        struct bt_gatt_chrc *chrc_attr = attr->user_data;
        LOG_INF("attr is characteristic. UUID type: %d  val: %x", chrc_attr->uuid->type, BT_UUID_16(chrc_attr->uuid)->val);

        last_handle = attr->handle;
      }
      else
      {
        LOG_WRN("No more characteristics, looking for CCCDs");
        discover_params.uuid = NULL;
        discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
        discover_params.start_handle = last_handle + 1;

        int err = bt_gatt_discover(conn, &discover_params);
        if (err)
          LOG_ERR("Discover failed - characteristics (err %d)", err);
        discover_phase = DISC_PHASE_CCCD;
        return BT_GATT_ITER_STOP;
      }
      break;

    case DISC_PHASE_CCCD:
      if (attr)
      {
        LOG_INF("attr is CCCD. UUID type: %d  val: %x", attr->uuid->type, BT_UUID_16(attr->uuid)->val);
        if (attr->user_data == NULL)
          LOG_INF("user_data is NULL");
        else
        {
          struct bt_gatt_ccc *dope = attr->user_data;
          LOG_INF("user_data is %x", dope->flags);
        }
        last_handle = attr->handle;
      }
      else
      {
        LOG_WRN("Discover complete");
      }
      break;

    default:
      break;
  }

  return BT_GATT_ITER_CONTINUE;
}

This is the log associate:

[00:00:07.685,424] <wrn> main: Connected: 5F:5E:48:13:59:F4
[00:00:07.935,607] <inf> main: [ATTRIBUTE] handle: 1  UUID type: 0  val: 2800
[00:00:07.935,638] <inf> main: Attr is primary service UUID type: 0  val: 1800
[00:00:07.935,668] <inf> main: [ATTRIBUTE] handle: 10  UUID type: 0  val: 2800
[00:00:07.935,668] <inf> main: Attr is primary service UUID type: 0  val: 1801
[00:00:08.239,196] <inf> main: [ATTRIBUTE] handle: 14  UUID type: 0  val: 2800
[00:00:08.239,227] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.239,227] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.239,288] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.239,288] <inf> main: Found Custom service
[00:00:08.335,418] <inf> main: [ATTRIBUTE] handle: 20  UUID type: 0  val: 2800
[00:00:08.335,418] <inf> main: Attr is primary service UUID type: 0  val: 180a
[00:00:08.435,577] <inf> main: [ATTRIBUTE] handle: 31  UUID type: 0  val: 2800
[00:00:08.435,577] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.435,607] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.435,638] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.435,638] <inf> main: Found Custom service
[00:00:08.435,668] <inf> main: [ATTRIBUTE] handle: 39  UUID type: 0  val: 2800
[00:00:08.435,668] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.435,699] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.435,729] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.435,760] <inf> main: Found Custom service
[00:00:08.435,760] <inf> main: [ATTRIBUTE] handle: 45  UUID type: 0  val: 2800
[00:00:08.435,760] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.435,791] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.435,821] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.435,852] <inf> main: Found Custom service
[00:00:08.785,339] <inf> main: [ATTRIBUTE] handle: 51  UUID type: 0  val: 2800
[00:00:08.785,369] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.785,369] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.785,430] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.785,430] <inf> main: Found Custom service
[00:00:08.785,461] <inf> main: [ATTRIBUTE] handle: 57  UUID type: 0  val: 2800
[00:00:08.785,461] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.785,491] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.785,522] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.785,552] <inf> main: Found Custom service
[00:00:08.785,614] <inf> main: [ATTRIBUTE] handle: 63  UUID type: 0  val: 2800
[00:00:08.785,614] <inf> main: Attr is primary service UUID type: 2  val 16:  dcca
[00:00:08.785,614] <inf> main: Attr is primary service UUID type: 2  val 32:  a9e50e24
[00:00:08.785,675] <inf> main: Attr is primary service UUID type: 2  val 128: ���$������
[00:00:08.785,675] <inf> main: Found Custom service
[00:00:08.785,705] <wrn> main: No more services, looking for characteristics
[00:00:08.935,821] <inf> main: [ATTRIBUTE] handle: 64  UUID type: 0  val: 2803
[00:00:08.935,852] <inf> main: attr is characteristic. UUID type: 2  val: dcca
[00:00:08.935,852] <inf> main: [ATTRIBUTE] handle: 66  UUID type: 0  val: 2803
[00:00:08.935,882] <inf> main: attr is characteristic. UUID type: 2  val: dcca
[00:00:09.105,957] <wrn> main: No more characteristics, looking for CCCDs
[00:00:09.135,406] <inf> main: [ATTRIBUTE] handle: 67  UUID type: 2  val: dcca
[00:00:09.135,406] <inf> main: attr is CCCD. UUID type: 2  val: dcca
[00:00:09.135,406] <inf> main: user_data is NULL
[00:00:09.165,313] <inf> main: [ATTRIBUTE] handle: 68  UUID type: 0  val: 2902
[00:00:09.165,344] <inf> main: attr is CCCD. UUID type: 0  val: 2902
[00:00:09.165,344] <inf> main: user_data is NULL
[00:00:09.195,312] <wrn> main: Discover complete

I used my phone with the nRF Connect Mobile app to connect to the peripheral so I could see the different services, UUID, etc... With the phone, I was able to read, write and subscribe to characteristics (So the peripheral work great and the issue must come from the central).

One of the custom service's UUID is 8ab20b00-3044-4385-9315-e71db302460e. As I understand the BLE specs, the 16 bits UUID is the third and fourth bytes of the long UUID, where the 32 bits is the first to fourth bytes of the long UUID. So in this case, it must be:

UUID format (number of bits) Result
16 0b00
32 8ab20b00
128 8ab20b00-3044-4385-9315-e71db302460e

But as we can see in the log above, it doesn't print that at all.

However, as far as I understand, it seems that there is a endian conflict between the BT_UUID_XX macro and the value provide in the bt_gatt_attr *attr in the parameters. I think that one works with little-endian while the other works with big-endian. I thought that because the value printed in the log correspond to the end of the 128 bits UUID.

Is my thoughts about this endian conflict right ? If not, how can I resolve this ?


Solution

  • As you can see in https://docs.zephyrproject.org/apidoc/latest/uuid_8h_source.html, Zephyr stores UUIDs in three different ways (the most optimal I guess for a given UUID). A UUID is represented in memory as a one-byte type tag (indicating 16, 32 or 128-bit data) followed by an aligned 16-bit integer, an aligned 32-bit integer or a 16 byte long byte array (little endian, so "reversed" compared to the normally used big endian as used when a UUID is printed as a hexadecimal string with dashes). So when you try to read a value using the incorrect type, it will not apply the Bluetooth standard conversion formula you mention, but instead read memory from unexpected locations.

    The 128-bit value should not be printed using %s with printf, since the type for such an UUID is not a NUL-terminated printable string (it is stored as 16 raw bytes). Instead, you can e.g. printf each individual byte (by accessing BT_UUID_128(svc_attr->uuid)->val[15], BT_UUID_128(svc_attr->uuid)->val[14], and so on) using the %02x format.