BLE header files

mehbstnc
Posts: 8
Joined: Mon Aug 22, 2022 12:15 pm

BLE header files

Postby mehbstnc » Wed Oct 05, 2022 11:44 am

Hello,
I am just trying to uınderstand what is happening in the belown code. It seems like casting pointers but I didnt get the
whole idea.
Can some one explain it ?
Thanks for helps

#define BLE_UUID16_DECLARE(uuid16) \
((ble_uuid_t *) (&(ble_uuid16_t) BLE_UUID16_INIT(uuid16)))

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: BLE header files

Postby Craige Hales » Wed Oct 05, 2022 3:32 pm

Expand the macros. I think this might be the ones: https://github.com/apache/mynewt-nimble ... ble_uuid.h

Code: Select all

#define BLE_UUID16_INIT(uuid16)         \
    {                                   \
        .u = {                          \
            .type = BLE_UUID_TYPE_16,   \
        },                              \
        .value = (uuid16),              \
    }
so

Code: Select all

(ble_uuid16_t) BLE_UUID16_INIT(uuid16)
means

Code: Select all

(ble_uuid16_t) 
    {                                   \
        .u = {                          \
            .type = BLE_UUID_TYPE_16,   \
        },                              \
        .value = (uuid16),              \
    }
which is an initialized structure. The next bit,

Code: Select all

((ble_uuid_t *) (&
takes the address of the structure and casts it to a more generic structure pointer.

Code: Select all

/** Generic UUID type, to be used only as a pointer */
typedef struct {
    /** Type of the UUID */
    uint8_t type;
} ble_uuid_t;
That in turn is part of a union.

Code: Select all

/** Universal UUID type, to be used for any-UUID static allocation */
typedef union {
    ble_uuid_t u;
    ble_uuid16_t u16;
    ble_uuid32_t u32;
    ble_uuid128_t u128;
} ble_uuid_any_t;
The union has four possible interpretations, depending on the ble_uuid_t at the beginning of each member of the union. The last three all look like

Code: Select all

/** 16-bit UUID */
typedef struct {
    ble_uuid_t u;
    uint16_t value;
} ble_uuid16_t;
which embeds a ble_uuid_t, followed by more data. The first just doesn't have any more data.
I think the extra parens

Code: Select all

(ble_uuid16_t)
are required by the macro to separate ble_uuid16_t from BLE_UUID16_INIT, maybe.
Craige

mehbstnc
Posts: 8
Joined: Mon Aug 22, 2022 12:15 pm

Re: BLE header files

Postby mehbstnc » Thu Oct 06, 2022 3:38 pm

Thank you for your help..

Who is online

Users browsing this forum: No registered users and 86 guests