mynewt-mcumgr icon indicating copy to clipboard operation
mynewt-mcumgr copied to clipboard

Bit field in header

Open 5frank opened this issue 5 years ago • 3 comments

https://github.com/apache/mynewt-mcumgr/blob/a3d5117b0888ca52b967886467b5bb350028c4ba/mgmt/include/mgmt/mgmt.h#L79-L87

I believe it is unwise to have a bit field in the header. Memory layout endianness (i.e. byte order) do not necessarily correlate with bit order and compilers are free to pick any layout and even add padding as it is not in C specification.

5frank avatar Nov 19 '20 20:11 5frank

What can I do for this issue. Can you show me ropes?

alokprinc avatar Sep 09 '21 12:09 alokprinc

@alokprinc perhaps remove bitfield and change all access to nh_op to use a bitmask. example:

struct mgmt_hdr_no_bitfield { 
    uint8_t  nh_op; /
..
#define MGMT_HDR_OP_MASK (0x7)
...
        uint8_t op = some_header.nh_op & MGMT_HDR_OP_MASK;
.
}

5frank avatar Sep 16 '21 10:09 5frank

 struct mgmt_hdr { 
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 
     uint8_t  nh_op:3;           /* MGMT_OP_[...] */ 
     uint8_t  _res1:5; 
 #endif 
 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 
     uint8_t  _res1:5; 
     uint8_t  nh_op:3;           /* MGMT_OP_[...] */ 
 #endif 

do i have to change this piece of code. And i still not getting what shold i do please guide.

alokprinc avatar Sep 16 '21 11:09 alokprinc