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


Groups > comp.arch.embedded > #28287

Re: How to transmit the value of an enumeration

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.arch.embedded
Subject Re: How to transmit the value of an enumeration
Date 2018-10-25 09:31 +0200
Organization A noiseless patient Spider
Message-ID <pqrrh3$d0$1@dont-email.me> (permalink)
References <pqpkdn$ed3$1@dont-email.me> <846dnZ-IQIryLU3GnZ2dnUU7-bmdnZ2d@earthlink.com>

Show all headers | View raw


On 24/10/18 19:48, Joe Chisolm wrote:
> On Wed, 24 Oct 2018 13:18:15 +0200, pozz wrote:
> 
>> enum type_t {
>>    TYPE_1,
>>    TYPE_2,
>>    ...
>>    TYPE_100,
>> }
>>
>> void tx_byte(unsigned char c);
>>
>> enum type_t t;
>> ...
>> tx_byte((unsigned char)t);
>>
>>
>> If the maximum value in the enumeration is lower than 256, you can cast 
>> to (unsigned char) and transmit it as a byte.
>>
>>
>> However the receiver must have the same enumeration.  The transmitter 
>> and the receiver must agreed on *the exact values* of the enumeration.
>>
>> What happens if the developer of the transmitter thinks that it is 
>> better to swap some values during a future release?  The enumeration 
>> isn't used only for transmission, it is also used for general logic of 
>> the software.
>>
>> Until now I didn't found a good solution.  I only write some comments 
>> before the enumeration declaration:
>>
>> /* WARNING!! DON'T CHANGE THE ORDER OF THE VALUES INSIDE ENUMERATION
>>   * They are also used in the transmission */
>>
>> I sometimes prefer explicit the value transmitted:
>>
>> if (t == TYPE_1) tx_byte(0);
>> if (t == TYPE_2) tx_byte(1);
>> ...
>>
>>
>> What is your strategy?
> 
> Your protocol document defines what is sent on the communications channel.
> In your code you may have:
>  enum type_t {
>     TYPE_2,
>     TYPE_1,
>     ...
>     TYPE_100
>  }
> 
> But in your protocol handler you have
>  enum protocol_type_t {
>     PTYPE_1,
>     PTYPE_2,
>     ...
>     PTYPE_100
>  }
> switch(t) {
> case TYPE_1:
>   tx_byte(PTYPE_1);
>  
> ....
> 
> You convert your internal type_t to the external protocol protocol_type_t
> and protocol_type_t always (well should always) matches the spec.  If the code
> passes a type_t that does not match a protocol_type_t you have a error and about
> to violate the protocol - logs and alarms happen. It's up to the receiver to
> decide what to do if a protocol_type_t does not match a value it can deal with.
> 
> If your protocol says protocol_type_t is sent as 8 bits I think it's a good idea
> to fill out the enum with all 256 possibilities defined.
> PTYPE_100,
> SPARE_PTYPE_101,
> SPARE_PTYPE_102,
> ...
> 
> The protocol doc defines what PTYPE_1 to PTYPE_100 mean and notes 101 to max value
> are reserved for future use.
> 

I can't help thinking this is extremely inefficient, and completely
unnecessary.

In reality, in a small system you might have perhaps a dozen different
message types - "protocol types".  Make an enum for these.  Make
documentation for them (which will use the real numbers).  Have a little
static assert at the end of the enum definition to make sure you have
not missed one out by accident.  Put the enum definition, and other
protocol-related common declarations, in a single header that is shared
by both the client and server sides.  If the client programmers and the
server programmers get mixed up versions or make their own independent
changes, fire the project manager (not the programmers).

All this extra code - the endless unnecessary declarations of "spare"
values, the vast useless switches - they are nothing but magnets for
errors, typos, copy-and-paste errors, maintenance nightmares, and are
going to be completely untestable.


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