Problem assigning a uint8_t value to an "octet"

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys, today I found a weird thing: an "octet". It seems to be like a byte although its not.
As it is the first time I see this, and dont find any information about this, Im here asking for help. The problem is this:

I have this structure with an "octet"

typedef struct PACK
{
  LITTLE_ENDIAN_OCTET(3,(
    uint8_t randStartTime : 1,
    uint8_t randEndTime : 1,
    uint8_t reserved : 6
  ))
} ZCL_EventControl_t;

and somewhere in the code I need to assign to this octet a uint8_t variable like this:

 loadControlEvent.eventControl = commandFrame.payload[22];

where eventControl is an instance of ZCL_EventControl_t and commandFrame.payload[22] is a member of a stucture with uint8_t type.

Compiler throws me this error:

./../../src/esp.c:640:32: error: incompatible types when assigning to type 'ZCL_EventControl_t' from type 'uint8_t'

How can I assign to the octet the value of a uint8_t variable?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We'll need to see this macro: LITTLE_ENDIAN_OCTET

But how could this work anyway. Forget the macro for the time being and just consider:

typedef struct {
  uint8_t foo:6;
  uint8_t bar:2;
} mystr_type;

mystr_type mystr;

mystr = 37;

that could never work. Sure you might use:

mystr.foo = 37;

but you cannot assign an integer to a struct. Perhaps you intended a union?..

union {
 mystr_type mystr;
 uint8_t n;
} joined;

joined.n = 37;
something = joioned.mystr.foo;

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks clawson!

No, I dont want a union.

I done like you show in the example, setting each bit individualy of the structure and of course it works but what I intended to do was the "easy way" as I mentioned before.
The deal is, I receive a large payload (bytes) from USART and I want to put those bytes "directly" into some 8 bit variables.

But if I have to assign each bit individually there is no problem, it is only a little more work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
but you cannot assign an integer to a struct
But the size of the struct should be 1 byte (1 bit plus 1 bit plus 6 bits). You will have to coerce it:

loadControlEvent.eventControl = (ZCL_EventControl_t)commandFrame.payload[22];

Edit: I found the definition of LITTLE_ENDIAN_OCTET

#define  LITTLE_ENDIAN_OCTET(amount, fields) BIT_FIELDS_ ## amount fields

which with 3 as the amount ends up calling:

#define  BIT_FIELDS_3(f1, f2, f3)  f1; f2; f3;

or

#define  BIT_FIELDS_3(f1, f2, f3)  f3; f2; f1;

depending on settings, which puts the elements into proper little endian order.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I know you said you didn't want to use a union but did you know they can be used anonymously...

#include 

typedef struct {
	union {
		struct {
		  uint8_t bar:2;
		  uint8_t foo:6;
		};
		uint8_t byte;
	};
} foo_t;			

foo_t test;

int main(void)
{
		test.byte = 37;
		PORTB = test.foo;
}

Neither then union nor the internal struct in that has a name.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koschi,

I tried to use the coersion but I keep getting the an error. So, that dont work.

Clawson,

The union worked perfectly! Thats what I was looking for! Thanks guys for this "C expert" tips :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I tried to use the coersion but I keep getting the an error.
What error are you getting?

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

duduspot wrote:
Hey guys, today I found a weird thing: an "octet". It seems to be like a byte although its not.
Today, an "octet" and a "byte" are the same thing - 8 bits.

A long time ago (up until maybe the late 80s or early 90s), this wasn't always true. A "byte" or a "character" could be other sizes - 7, 8, and 9 bits were popular. An "octet" was always 8 bits.

"Octet" *might* also imply something about the endian-ness of the bits (whether bit 0 = 1 or bit 0 = 128) but I am not sure about that. A "byte" usually has whatever endian-ness the underlying CPU has.

The word "octet" tends to get used a lot in network protocol definitions - might be an ITU or OSI thing. It still means "8 bits" there.

I hope this helps!