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.

  • Sorry again...How can one implement variable length? Byte by byte with the offset 1? Why then have member len? I have serious trouble understanding your point. Yes one can create ble_gatts_evt_X_write_t, ble_gatts_evt_Y_write_t, ble_gatts_evt_Z_write_t and so on and in this case each event will have its own constant not variable length. Or use ble_gatts_evt_write_t with the length 1 and use offset if more than one byte is required. Or I'm totally misunderstanding what those types are trying to achieve...

    Lets say I have LED which is using event write to turn it on and off(bool) and I have a DAC and need to set it up(uint16_t). How to manage this combination?

    Thanks!

  • "to make it clear that the array is placed inside the struct, and not just a pointer to somewhere else." Understood. To achieve that one has to allocate the maximum possible length and then length will become variable from 1 to max. If such maximum length is defined as one how come it possible be variable as the comment says?! Also this approach means that EACH write event has to have its own type and if so how this type is defined inside the common library header?

    I'm afraid that this dialog is going to nowhere and if so it is probably the best to stop it as is unresolved...

  • Please notice that we're arguing NOT about C but about stile and readability.

  • I think the confusion here is about the comment associated with the struct member data. It is clearly an array of length one, but the comment documenting it says it is variable length. If data were more than one element long the it could be undestood that the maximum expected size was always allocated but not always used, but here data is only one element. I suppose this struct could be populated with zero or one elements in data which would make it variable length.

    So the questions:

    1. Is it valid for length to be zero, implying data should be ignored?
    2. is it valid for length to be one, implying data contains a single element?
    3. If zero length is not valid, is the comment stating that data is variable length erroneous (i.e. the result of copying a template, pasting, editing names, and not updating the comments)?
  • Matt, are you saying that length can't exceed one byte? It seems to me wrong... Yet we have another ambiguity...

Related