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


Groups > comp.arch.embedded > #28282

Re: How to transmit the value of an enumeration

From Joe Chisolm <jchisolm6@earthlink.net>
Subject Re: How to transmit the value of an enumeration
Newsgroups comp.arch.embedded
References <pqpkdn$ed3$1@dont-email.me>
Message-ID <846dnZ-IQIryLU3GnZ2dnUU7-bmdnZ2d@earthlink.com> (permalink)
Date 2018-10-24 12:48 -0500

Show all headers | View raw


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.

-- 
Chisolm
Republic of Texas

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