TReK C++  5.3.3
Telemetry/Command API
define_packet/define_packet.cpp
/*
This example is for a simple, completely made up packet. It has a header,
data, and trailer with all of the packet attributes set. The 'purpose' of
this example is to define the packet and save it to disk. Other examples
will use the saved packet to build and extract the packet. There are other
ways to create packet definitions in TReK (e.g., simple comma delimited
files).
Just for fun here is the packet definition in a small table. All of the
data is 'big endian' except for a single parameter. All sizes are listed
in bits. The data is packed (i.e., there are no gaps). The code below will
set the start location for parameter, but it isn't shown in the table.
Name Data Type Size Notes
Version unsigned integer 4 Should always be zero
PktId unsigned integer 12 For this packet, the value is 54
PktLen unsigned integer 16 Length of packet after this point
PktCnt unsigned integer 8 Packet counter
Time system time 32 A.k.a. DT_UNIX_TIME
IntParam signed integer 16 First data parameter
UintParam unsigned integer 32 Three consecutive values
FloatParam double 64 Little-endian floating point
StrParam string 800 Varible length string
Checksum unsigned integer 32 CRC-32 checksum
*/
#include "packet.h"
#include "parameter.h"
#include <iostream>
using namespace trek;
using namespace std;
int main()
{
//
// NOTE: There is only minimal error checking shown in the code below in
// order to simplify the example. In general, only the first time a
// method is used will the error handling be in this example. You should
// always check the returned value when one is available to ensure proper
// operation of the code. Error handling for this example is to exit on
// error.
//
Packet pkt;
int32_t ret_value;
//
// Need to create the 'zones' for the packet and add it to the packet.
// The general steps for this are:
//
// 1. Initialize the parameter collection
// 2. Initialize the parameter.
// 3. Set the parameter attributes as needed (data type, name, length,
// byte order, start bit, etc.)
// 4. Add the parameter to the collection.
// 5. Repeat steps 2, 3, and 4 until all the parameters are in the
// parameter collection.
// 6. Add the parameter collection into the correct zone.
//
//
// Using the above procedure as a guideline, create the header zone for
// the packet. This will consist of 5 parameters: Version, PktId, PktLen,
// PktCnt, and Time.
//
pc.Init(); // this will clear out any data in the collection
pc.SetName( "MyHeader" ); // not required, but can be useful
p.Init(); // this will clear out any data in the parameter
p.SetName( "Version" );
if( p.SetStartBit( 0 ) )
{
// Currently only will be success, but may return errors at a late
// time.
cout << "Error setting start bit\n";
return 1;
}
{
// There are a lot of return codes for this method. The returned error
// is usually self explanatory, but an attempt at explanation is made
// throughout the documentation.
cout << "Error setting data type\n";
return 1;
}
p.SetModifiableFlag( false ); // don't want this value changed
if( p.SetValue( (uint8_t)0 ) )
{
// If this method fails it is usually because you have a data type
// mismatch or the value won't fit in the data. Since this is a 4-bit
// unsigned integer, any value above 15 will fail.
cout <<"Error setting value\n";
return 1;
}
if( pc.AddParameter( p ) ) // add the parameter to the collection
{
// If this fails, it's most likely because you already have a parameter
// with the same name.
cout << "Error adding parameter\n";
return 1;
}
//
// Time to add the rest of the header parameters...and without error
// checking.
//
p.Init(); // this will clear out any data in the parameter
p.SetName( "PktId" );
p.SetStartBit( 4 ); // Don't forget to set this!
p.SetModifiableFlag( false ); // don't want this value changed
pc.AddParameter( p ); // add the parameter to the collection
p.Init();
p.SetName( "PktLen" );
p.SetStartBit( 16 );
p.SetModifiableFlag( true ); // packet length will change
pc.AddParameter( p );
p.Init(); // this will clear out any data in the parameter
p.SetName( "PktCnt" );
p.SetStartBit( 32 );
p.SetModifiableFlag( true ); // sequence count will change
pc.AddParameter( p );
p.Init(); // this will clear out any data in the parameter
p.SetName( "Time" );
p.SetStartBit( 40 );
p.SetModifiableFlag( true ); // time will change
pc.AddParameter( p );
//
// Now all of the parameters are in the collection, add the header. The
// Packet will make a copy of the input, so feel free to use it again.
//
if( pkt.AddHeader( pc ) )
{
// If it fails, you probably have already added a header. Use
// RemoveHeader first if you really want to add a different one.
cout << "Error adding header\n";
return 1;
}
//
// Time to add the 'data zone' to the packet. Same basic procedure as the
// header. The start location for each parameter is relative to the
// collection so they will restart at zero. This makes it much easier to
// reuse collections and packets within other packets.
//
pc.Init(); // we're going to reuse this, so must initialize it again.
pc.SetName( "MyData" );
p.Init(); // this will clear out any data in the parameter
p.SetName( "IntParam" );
p.SetStartBit( 0 ); // Don't forget to start over for a new collection
p.SetModifiableFlag( true );
pc.AddParameter( p ); // add the parameter to the collection
//
// This parameter has 3 samples, you should set the number of samples and
// also the sample offset. The sample offset will default to 0 which is
// the required value in this case. The sample offset is the number of
// bits that are between each of the samples.
//
p.Init();
p.SetName( "UintParam" );
p.SetStartBit( 16 );
p.SetNumberOfSamples( 3 ); // the default is 1, need 3 samples
p.SetModifiableFlag( true );
pc.AddParameter( p ); // add the parameter to the collection
//
// This parameter has a little endian byte order. The default byte order
// when setting the data type is big endian so you must pass in an extra
// parameter here.
//
p.Init();
p.SetName( "FloatParam" );
p.SetStartBit( 112 );
p.SetModifiableFlag( true );
pc.AddParameter( p ); // add the parameter to the collection
//
// The last parameter in the data is variable length. Variable length
// data must always be at the end of the container. You'll need to
// specify the parameter is variable length when setting the data type.
//
p.Init();
p.SetName( "StrParam" );
p.SetStartBit( 176 );
800, // 100 byte string (as bits)
true );
p.SetModifiableFlag( true );
pc.AddParameter( p ); // add the parameter to the collection
//
// Now add the data zone to the packet.
//
pkt.AddData( pc );
//
// The trailer consists of a single 32-bit parameter that is used for a
// checksum. The type of checksum is specified in the packet attributes.
// You just need to create the parameter to hold it here.
//
pc.Init();
p.Init();
p.SetName( "Checksum" );
p.SetStartBit( 0 );
p.SetModifiableFlag( true ); // checksum will change
p.SetValue( (uint32_t)0 );
pc.AddParameter( p );
pkt.AddTrailer( pc );
//
// Once all of the data is in the packet, create the packet map that will
// allow lookup of the parameters in the packet.
//
//
// Now it's time to set the packet attributes. None of the attributes are
// required, but all are used in this example.
//
std::string name; // the name of the parameter to use for an attribute
//
// Add an identifier. This packet only has one, but you can have any
// number of identifiers as required. The "Version" of the packet could
// be used here to ensure that the packet header hasn't changed, but we
// won't use it today.
//
id_struct.zone = HEADER_ZONE; // the parameter is in the header
id_struct.type = CONTENT_IDENTIFIER; // it identifies content (data)
id_struct.default_value_available = false; // using expect value instead
id_struct.expected_value_available = true;
id_struct.expected_value = 54;
ret_value = pkt.AddIdentifier( "PktId", &id_struct );
if( ret_value )
{
// Check the parameter name and ensure that you have set up the global
// packet map with CreateGlobalPacketMap.
cout << "Error adding identifier parameter\n";
return 1;
}
//
// Add the length parameter. In this case the actual length of the packet
// is 4 bytes longer than the value of this field.
//
ret_value = pkt.SetLengthParameter( "PktLen", HEADER_ZONE, 4 );
if( ret_value )
{
// Check the parameter name and ensure that you have set up the global
// packet map with CreateGlobalPacketMap.
cout << "Error setting length parameter\n";
return 1;
}
//
// Add the packet counter. Since it is a simple forward based counter,
// the defaults work fine.
//
ret_value = pkt.SetCounterParameter( "PktCnt", HEADER_ZONE );
if( ret_value )
{
// Check the parameter name and ensure that you have set up the global
// packet map with CreateGlobalPacketMap.
cout << "Error setting counter parameter\n";
return 1;
}
//
// Add the time stamp. There's no offset here, so the defaults will work.
//
ret_value = pkt.SetTimeStampParameter( "Time", HEADER_ZONE );
if( ret_value )
{
// Check the parameter name and ensure that you have set up the global
// packet map with CreateGlobalPacketMap.
cout << "Error setting time stamp parameter\n";
return 1;
}
//
// Add the checksum. It's a CRC-32 of all of the header and data.
//
ret_value = pkt.SetChecksumParameter( "Checksum",
0,
0,
if( ret_value )
{
// Check the parameter name and ensure that you have set up the global
// packet map with CreateGlobalPacketMap.
cout << "Error setting checksum parameter\n";
return 1;
}
//
// Saving this packet to a file is easy.
//
ret_value = pkt.SaveFile( "my_file.xml" ); // output is XML
if( ret_value )
{
// Could not save the file. Most likely cause is that you don't have
// write permission to the directory.
cout << "Error saving file\n";
return 1;
}
//
// The definition of the packet now is saved to a file. You can use this
// definition to build a packet to send or extract a packet you receive.
// Examples for both are provided.
//
// THE END
return 0;
}
void SetName(const char *input_ptr)
Sets the name of the item.
Definition: named_item.cpp:114
This class describes a packet composed of one or more parameters.
Definition: packet.h:72
int32_t AddData(PacketItem &item)
Sets the specified packet item as the data for the packet.
Definition: packet.cpp:478
int32_t SetChecksumParameter(const char *name, packet_zone_type zone, packet_checksum_point_type start_pt, int32_t start_offset, packet_checksum_point_type end_pt, int32_t end_offset, packet_checksum_type type)
Sets the checksum parameter for the packet.
Definition: packet.cpp:3577
int32_t AddHeader(PacketItem &item)
Sets the specified packet item as the header for the packet.
Definition: packet.cpp:437
int32_t AddIdentifier(const char *name, packet_identifier_struct *id_struct_ptr)
Adds an identifier to the packet.
Definition: packet.cpp:3818
int32_t AddTrailer(PacketItem &item)
Sets the specified packet item as the trailer for the packet.
Definition: packet.cpp:519
int32_t SaveFile(const char *filename)
Saves the Packet definition to the specified file.
Definition: packet.cpp:2790
int32_t SetLengthParameter(const char *name, packet_zone_type zone, int32_t offset=0)
Sets the length parameter for the packet.
Definition: packet.cpp:3074
int32_t SetTimeStampParameter(const char *name, packet_zone_type zone, double offset_in_secs=0)
Sets the time stamp parameter for the packet.
Definition: packet.cpp:3400
int32_t CreateGlobalPacketMap()
Creates the map used to hold all of the parameters.
Definition: packet.cpp:2138
int32_t SetCounterParameter(const char *name, packet_zone_type zone, uint32_t start_count=0, bool fwd_count=true, uint32_t min_count=0, uint32_t max_count=0, uint32_t reset_count=0)
Sets the counter parameter for the packet.
Definition: packet.cpp:3229
This class describes a parameter collection composed of one or more parameters.
Definition: parameter_collection.h:24
virtual void Init()
Initializes the object.
Definition: parameter_collection.cpp:128
int32_t AddParameter(Parameter *input_ptr)
Adds the parameter to the collection.
Definition: parameter_collection.cpp:291
This class describes a single parameter within a telemetry or command message including its value.
Definition: parameter.h:95
int32_t SetStartBit(uint32_t input)
Sets the start location for the parameter as bits.
Definition: parameter.cpp:1725
void Init()
Initializes the object.
Definition: parameter.cpp:412
void SetSampleOffset(uint32_t input)
Sets the sample offset for the parameter.
Definition: parameter.cpp:1788
int32_t SetDataType(parameter_data_type input_type, uint32_t input_len, byte_order_type input_order=BIG_ENDIAN_BYTE_ORDER, bool var_len=false)
Sets the data type, length, and byte order for the parameter.
Definition: parameter.cpp:1401
void SetModifiableFlag(bool input)
Sets the modifiable flag for the parameter.
Definition: parameter.cpp:2522
int32_t SetNumberOfSamples(uint32_t input)
Sets the number of samples for the parameter.
Definition: parameter.cpp:1747
int32_t SetValue(int8_t input, uint16_t sample_number=1)
Sets the value of the parameter with an 8-bit signed integer.
Definition: parameter.cpp:6151
@ DT_UNSIGNED_INTEGER
Unsigned integer value ranging from 1 to 64 bits in length.
Definition: parameter.h:42
@ DT_NULL_TERMINATED_STRING
ASCII string terminated by a NULL (0x00) character.
Definition: parameter.h:47
@ DT_UNIX_TIME
32 bits seconds since midnight January 1, 1970
Definition: parameter.h:56
@ DT_TWOS_COMPLEMENT
Two's complement integer value ranging from 2 to 64 bits in length.
Definition: parameter.h:41
@ DT_IEEE_FLOATING_POINT
IEEE floating point value. Single precision is 32 bits. Double precision is 64 bits.
Definition: parameter.h:46
@ CONTENT_IDENTIFIER
used to determine content of packet
Definition: trek.h:87
@ START_HEADER
Start of header (beginning of packet)
Definition: packet.h:33
@ END_DATA
End of data (or start of trailer if you prefer)
Definition: packet.h:35
@ TRAILER_ZONE
Trailer (last) zone in packet.
Definition: packet.h:27
@ HEADER_ZONE
Header (first) zone in packet.
Definition: packet.h:25
@ CT_CRC32
32-bit Cyclic redundance check (see ???)
Definition: packet.h:42
@ BIG_ENDIAN_BYTE_ORDER
Also known as network byte order. Most significant byte is first.
Definition: trek.h:76
@ LITTLE_ENDIAN_BYTE_ORDER
Least significant byte is first.
Definition: trek.h:77
Defines the trek::Packet class.
Defines the trek::Parameter class.
Defines the trek::ParameterCollection class.
Structure of information needed for identifier parameters.
Definition: packet.h:49
packet_zone_type zone
The zone of the packet to find the idenitifer.
Definition: packet.h:50
bool expected_value_available
Is an expected value available?
Definition: packet.h:54
identifier_types type
The type of identifier.
Definition: packet.h:51
int32_t expected_value
Expected value (only used if expected_value_available = true)
Definition: packet.h:55
bool default_value_available
Is a default value available?
Definition: packet.h:52