This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Confusing definition

C:\Nordic Semiconductor\nrf51_sdk_v4_4_1_31827\nrf51822\Include\ble\softdevice\ble_gatts.h


typedef struct {
  uint16_t     handle;    /**< Attribute Handle. */
  uint8_t       op;          /**< Type of write operation, see @ref BLE_GATTS_OPS. */
  ble_gatts_attr_context_t    context;     /**< Attribute Context. */
  uint16_t      offset;      /**< Offset for the write operation. */
  uint16_t      len;          /**< Length of the incoming data. */
  uint8_t       data[1];    /**< Incoming data, variable length. */
} ble_gatts_evt_write_t;

My question is how to interpret the element data[1]? Is it just uint8_t *data(C-wise it is)? If so why is it defined in such a way? Especially with the size of the data array as one with the following comment about its variable length which all together makes this definition very confusing.

Thanks.

  • Matt, are you saying that length can't exceed one byte? It seems to me wrong... Yet we have another ambiguity...

  • This is the answer: ble_stack_handler_init(). Don't like it though but this is matter of taste and > de gustibus non est disputandum .
    Even taking programming stile aside yet the comments must be better and cleaner.

  • I'm sorry for the delay here, but we've been busy with internal training lately.

    I guess you're right that another option instead of using the [1] would have been to use [BLE_L2CAP_MTU_DEF], but in my opinion whether that's any clearer is mostly a matter of taste, when most of this array may very well be uninitialized memory.

    This array will actually be variable length, since it will contain whatever was in the write event coming over the air. From the S110 side, you have no control over this, it's only limited by the peer device and the Bluetooth Spec.

    I'm a little unsure why you found my answer worthy a down-vote, though, as I seriously tried to explain why this was there and what it means. I'm sorry if I came off rude, but I can very well see why this seems confusing. What I can't quite understand is how you can insist that I and my explanation is wrong.

  • This array will always have a length >= 1. For the write event in particular, it can be up to BLE_L2CAP_MTU_DEF-3 bytes, as given in the Bluetooth Core Specification, Volume 3, Part F, section 3.4.5.1 and 3.4.5.3.

    As I mentioned in another comment below, the actual length will indeed be variable, and depends on how much data the peer device actually wrote.

  • Beware that you are never expected to initialize a struct of this type, as it's only an event structure, initialized by the softdevice to notify you as an application of an incoming Write Command / Request.

    If you are to initialize some other struct employing the same concept, you would manually have to make sure to initialize it with appropriate size for the data you want to fit.

    After checking a little more, it's apparently known as the "struct hack", and explained in more detail here, including how to initialize such struct: http://c-faq.com/struct/structhack.html

Related