[Tinyos-help] Casting between independent type and dependent type in tinyos

Michael Schippling schip at santafe.edu
Tue Apr 13 08:52:55 PDT 2010


There's a couple things going on in your example...

First, my belief based on not using or having looked
very deeply into nx_ types, is that they are byte reversed --
nx_int16 is the big-endian version of the usual suspect
which is little-endian on all the platforms we commonly use.
However I don't know where the byte reversal occurs, if it's
in the message transmit code then some of the following probably
doesn't pertain.

Second, the "typeA  x = (typeA) y;" code is actually a structure
assignment, not just a cast, so it would try to copy the contents
of y into x. I think this will work on any two structs, but it
might be size dependent. Your two structs are probably the same
size so you might not have an issue. However, depending on where
the byte-reversal takes place, you might end up with reversed
integer values in x after the copy if you have anything other
than single byte objects in the struct -- you should slip by
given the two definitions you show here though.

If your code was this it would be a simple cast:
     typeA  *x = (typeA) &y;
I think you would have no complaints from the C compiler, unless
something in NESCC traps nx_ conversions. You might still have
the byte reversal issues though.

MS

Tran Thi Thuy Trang wrote:
> Hi,
> I am a beginner in TinyOS and nesC programming. I am using nesC and 
> sending message between platforms.
> If we have two following structs
> 
> typedef nx_uint8_t nx_typeB;
> typedef uint8_t typeB;
> 
> typedef struct nx_typeA {
> nx_uint8_t a;
> nx_typeB b;
> nx_uint8_t c;
> } nx_typeA;
> 
> typedef struct typeA{
> uint8_t a;
> typeB b;
> uint8_t c;
> } typeA;
> 
> can I do casting between these two types like following
> nx_typeA y;
> typeA  x = (typeA) y;
> 
> If it can, I wonder there is any notice that I should remember to handle 
> these types of casting...Is there any possible error?
> 
> Thank you in advance.
> Regards,
> Trang
> Trang
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tinyos-help mailing list
> Tinyos-help at millennium.berkeley.edu
> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help


More information about the Tinyos-help mailing list