Why use uint8_t sm : 4;

Hi, I'm using nRF52832 ble_app_uart example project. In ble_gap.h file, I could see: 

typedef struct
{
  // var : 4 means that var is 4-bit 
  uint8_t sm : 4;                     /**< Security Mode (1 or 2), 0 for no permissions at all. */
  uint8_t lv : 4;                     /**< Level (1, 2, 3 or 4), 0 for no permissions at all. */

} ble_gap_conn_sec_mode_t;

From my understanding, the number 4 means that the variable sm (security mode) will only use the lowest 4 bits as its value. For example, if sm = 0000 0001, then sm = 0001 due to the ": 4".

Is that correct? 

Also, why Nordic defines uint8_t, then use ": 4" (only uses 4 bits), please? 

Thanks

Parents
  • Hi,

    Your understanding about how C bit fields works is correct. That means that the size of ble_gap_conn_sec_mode_t is 1 byte (and not 2 as it would if it contained two full uint8_t instances).

    Regarding why it is like this I am not sure, as it has been like this since the early days of the SoftDevice. Probably it was done so because it matches how this is in the Bluetooth protocol on air.

Reply
  • Hi,

    Your understanding about how C bit fields works is correct. That means that the size of ble_gap_conn_sec_mode_t is 1 byte (and not 2 as it would if it contained two full uint8_t instances).

    Regarding why it is like this I am not sure, as it has been like this since the early days of the SoftDevice. Probably it was done so because it matches how this is in the Bluetooth protocol on air.

Children
No Data
Related