comp.lang.ada
 help / color / mirror / Atom feed
* Choices for modelling binary packet protocol in SPARK
@ 2009-07-18  6:46 xorque
  2009-07-18 11:23 ` Phil Thornley
  0 siblings, 1 reply; 5+ messages in thread
From: xorque @ 2009-07-18  6:46 UTC (permalink / raw)


Hello.

I'm working on a protocol that uses many different binary packet
types.
They all inherit from one basic packet type.

Here's an example of a similar ASN.1 representation:

Base-Module
DEFINITIONS EXPLICIT TAGS ::=
BEGIN

P1-Type ::= SEQUENCE {
  value1 [0] INTEGER,
  value2 [1] INTEGER OPTIONAL
}

P2-Type ::= SEQUENCE {
  value1 [0] INTEGER,
  value2 [1] INTEGER,
  value3 [2] INTEGER
}

P-Data-Type ::= CHOICE {
  p1 [0] P1-Type,
  p2 [1] P2-Type
}

P-Type ::= SEQUENCE {
  timestamp [0] INTEGER,
  data      [1] P-Data-Type,
}

END

The equivalent in Ada might be:

type P1_Type is record
  Value1         : Integer;
  Value2_Present : Boolean;
  Value2         : Integer;
end record;

type P2_Type is record
  Value1 : Integer;
  Value2 : Integer;
  Value3 : Integer;
end record;

type P_Option_Type
  (P1_Selected,
   P2_Selected);

type P_Data_Type is (Option : P_Option_Type) is record
  case Option is
    when P1_Selected => P1 : P1_Type;
    when P2_Selected => P2 : P2_Type;
  end case;
end record;

type P_Type is (Option : P_Option_Type) record
  Timestamp : Integer;
  Data      : P_Data_Type (Option);
end record;

The program needs to be able to pack any and every type of packet
into a byte array to be written to a network socket. The part that
writes the data to the socket obviously doesn't care about the
contents of the packet and therefore only needs to see a byte
array or possibly a private Packet type that it can call a
general Pack procedure on in order to obtain a byte array for
writing. I would like to write the Pack procedure in SPARK as
it seems like it would be the exact type of 'pure' code that
SPARK is suited to.

I'm not sure, exactly, with a lack of variant records, how I'm
meant to model this in SPARK.

Am I going to be writing enormous case statements or is there
some clever way to handle this kind of thing?

xw



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-07-21 14:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-18  6:46 Choices for modelling binary packet protocol in SPARK xorque
2009-07-18 11:23 ` Phil Thornley
2009-07-19  6:47   ` xorque
2009-07-21 14:13     ` Hibou57 (Yannick Duchêne)
2009-07-21 14:13       ` Hibou57 (Yannick Duchêne)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox