Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #28277

Re: How to transmit the value of an enumeration

From pozz <pozzugno@gmail.com>
Newsgroups comp.arch.embedded
Subject Re: How to transmit the value of an enumeration
Date 2018-10-24 15:18 +0200
Organization A noiseless patient Spider
Message-ID <pqprem$pnv$1@dont-email.me> (permalink)
References <pqpkdn$ed3$1@dont-email.me> <2f1efaa1-e0f4-47ba-a052-3b86f43c8d56@googlegroups.com>

Show all headers | View raw


Il 24/10/2018 13:45, Jack ha scritto:
> On Wednesday, October 24, 2018 at 1:18:20 PM UTC+2, pozz wrote:
>> enum type_t {
>>     TYPE_1,
>>     TYPE_2,
>>     ...
>>     TYPE_100,
>> }
>>
[...]
>>
>> What is your strategy?
> 
> Long answer: read the definition of "enumeration"
> 
> Short answer:
> An enumeration is a normal type disguised and is implementation dependent. Usually is signed int (whatever it means) but you have to check your compiler doc to be sure.

These details are generally important, but not for my question.  I don't 
know if C standard guarantees that, if not specified, the first value is 
zero and the others are contiguous.  If it is guaranteed, my example 
above works.  Otherwise, the declaration of my enumeration should have been:

enum type_t {
     TYPE_1 = 0,
     TYPE_2,
     ...
     TYPE_100,
}


> You can also explicitly set the value of every member of an enum:
> (and if you use an enum in a packet that is sent to someone else it may be a very good idea to do so)
> 
> enum type_t {
>      TYPE_1 = 34,
>      TYPE_2 = 56,
>      TYPE_3,
>      TYPE_4 = 0,
>      TYPE_5 = 0,
>      TYPE_100,
>   }
> 
> Exercise left to the reader: what's the value of TYPE_3 and TYPE_100?
> 
> If you need to send something to someone else there is a thing called "protocol" that explain in detail (hopefully) what's inside every byte you send. If for some reason the protocol changes...both transmitter and receiver needs to be modified.

I know that, but I often have a piece of code that manages values from a 
long enumeration.  It's ok to swap the members or change explicitly the 
values of them.  The code will always work while you insist using the 
name and not the value in the code, as it should be (otherwise what is 
the purpose of the enumeration?).

I often need to share some info with another device (or PC) connected in 
some way (serial line, Ethernet, Internet...)[*].
So I often need to transfer the value of an enumeration. The simplest 
way would be to transfer the (explicitly or implicitly) integer value of 
the enumeration (here it's not important if signed or unsigned, 1 2 or 4 
bytes).  This is very handy, because I only need a cast.

I understand this isn't the more robust way, because the enumeration 
could be changed in the future, broking the protocol.

What is the alternative? Re-define the long enumeration with another 
long list of values used in the protocol only and write a long piece of 
code:

   enum proto_type_t {
     PROTO_TYPE_1,
     PROTO_TYPE_2,
     ...
     PROTO_TYPE_100
   };

   if (type == TYPE_1) transmit(PROTO_TYPE_1);
   if (type == TYPE_2) transmit(PROTO_TYPE_2);
   ...

Most of the time, both enumerations are identical, so this long piece of 
error-prone code seems stupid.

Another alternative is to be sure the enumeration members aren't changed 
in the future. But is a simple comment sufficient?



[*] This will be the origin of another post in this ng.

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to transmit the value of an enumeration pozz <pozzugno@gmail.com> - 2018-10-24 13:18 +0200
  Re: How to transmit the value of an enumeration Jack <jack4747@gmail.com> - 2018-10-24 04:45 -0700
    Re: How to transmit the value of an enumeration pozz <pozzugno@gmail.com> - 2018-10-24 15:18 +0200
      Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-24 17:05 +0200
  Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-24 15:10 +0200
    Re: How to transmit the value of an enumeration pozz <pozzugno@gmail.com> - 2018-10-24 15:27 +0200
      Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-24 16:59 +0200
      Re: How to transmit the value of an enumeration Clifford Heath <no.spam@please.net> - 2018-10-25 08:57 +1100
  Re: How to transmit the value of an enumeration Joe Chisolm <jchisolm6@earthlink.net> - 2018-10-24 12:48 -0500
    Re: How to transmit the value of an enumeration pozz <pozzugno@gmail.com> - 2018-10-24 23:20 +0200
      Re: How to transmit the value of an enumeration Joe Chisolm <jchisolm6@earthlink.net> - 2018-10-24 22:21 -0500
    Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-25 09:31 +0200
      Re: How to transmit the value of an enumeration Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2018-10-25 09:58 -0700
      Re: How to transmit the value of an enumeration Joe Chisolm <jchisolm6@earthlink.net> - 2018-10-25 18:38 -0500
        Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-26 09:24 +0200
  Re: How to transmit the value of an enumeration George Neuner <gneuner2@comcast.net> - 2018-10-24 15:19 -0400
  Re: How to transmit the value of an enumeration Les Cargill <lcargill99@comcast.com> - 2018-10-26 20:30 -0500

csiph-web