contiki-os / contiki

The official git repository for Contiki, the open source OS for the Internet of Things
http://www.contiki-os.org/
Other
3.72k stars 2.58k forks source link

sizeof(linkaddr_t) gives wrong size on 32bit platform #555

Closed beshrns closed 10 years ago

beshrns commented 10 years ago

I am porting Contiki to a new 32bit platform, and I am using gcc. I noticed that when using Rime, sizeof(linkaddr_t) gives 4 instead of 2. I suggest testing LINKADDR_SIZE directly.

Affected file: core/net/mac/framer-802154.c (which causes the framer to assume using long addresses when it should use short addresses).

g-oikonomou commented 10 years ago

And you're sure there's no definition of LINKADDR_CONF_SIZE changing the size to 4?

beshrns commented 10 years ago

Yes, I am sure, because when I test LINKADDR_SIZE directly it just gives 2. I think the problem comes from default alignment of union type to word-size of the CPU which is 32bit.

vsaw commented 10 years ago

What compiler are you using? Btw one could try to use a compiler independent packing of structs like the __attribute__((__packed__)) of GCC

beshrns commented 10 years ago

I am using ba-elf-gcc (GCC) 4.1.2 on jn5168 Thanks for the tip, I will try it.

On Mon, Feb 3, 2014 at 3:40 PM, Valentin notifications@github.com wrote:

What compiler are you using? Btw one could try to use a compiler independent packing of structs like the attribute((packed)) of GCChttp://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html

— Reply to this email directly or view it on GitHubhttps://github.com/contiki-os/contiki/issues/555#issuecomment-33959664 .

beshrns commented 10 years ago

Actually using __attribute__((__packed__)) solves the problem.

typedef union __attribute__((__packed__)) {
  unsigned char u8[LINKADDR_SIZE];
} linkaddr_t;

Now sizeof(linkaddr_t) gives LINKADDR_SIZE (i.e. 2). So maybe the definition of linkaddr_t should be modified?

g-oikonomou commented 10 years ago

@beshrns did you make any progress with that one? I am fairly certain we won't see __attribute__((__packed__)) or any other toolchain-specific extensions in Contiki's upstream core any time soon, but if you are happy with this workaround for your own work we can close this?

beshrns commented 10 years ago

Well, not really. Although it solves my problem in the mean time, I believe there are platform-specific pieces of code in core\net; i.e., testing for sizeof(linkaddr_t)==2 is platform-specific. Therefore, I suggest to test LINKADDR_SIZE directly (in core/net/mac/framer-802154.c)

nvt commented 10 years ago

@beshrns Sure, would you like to submit a pull request with this change?