TReK C++  5.3.3
Telemetry/Command API
extract_packet/extract_packet.cpp
/*
This example extracts the packet described by the define_packet example. If
that example hasn't run, you will need to do that now. The file that is
created must be located where this program can find it.
The table below shows what is defined in the packet:
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>
#include <stdio.h>
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;
Parameter *param_ptr;
std::string param_name;
int32_t ret_value;
//
// Read in the packet file.
//
ret_value = pkt.LoadFile( "my_file.xml" );
if( ret_value )
{
// Most likely the file doesn't exist. Check that the file is there or
// use a full path name.
cout << "Could not open file\n";
return 1;
}
//
// After loading a packet, you must create the 'map' that allows parameter lookup.
//
//
// Normally you would extract a packet that came in from the network or
// some other interface. Since this is a simple example a buffer is shown
// below already popluated with data that is valid (including the
// checksum).
//
uint8_t buf[100];
uint32_t buf_size;
uint32_t last_bit;
buf[0] = 0x00; // Version = 0
buf[1] = 0x36; // PktId = 54
buf[2] = 0x00; // PktLen = 36 (40 - 4 byte offset)
buf[3] = 0x24;
buf[4] = 0xfe; // PktCnt = 254
buf[5] = 0x53; // Time = 2014-02-28 21:57:47
buf[6] = 0x11;
buf[7] = 0x06;
buf[8] = 0x5b;
buf[9] = 0xff; // IntParam = -1
buf[10] = 0xff;
buf[11] = 0x00; // UintParam(1) = 255
buf[12] = 0x00;
buf[13] = 0x00;
buf[14] = 0xff;
buf[15] = 0x00; // UintParam(2) = 256
buf[16] = 0x00;
buf[17] = 0x01;
buf[18] = 0x00;
buf[19] = 0x00; // UintParam(3) = 65,536
buf[20] = 0x01;
buf[21] = 0x00;
buf[22] = 0x00;
buf[23] = 0x00; // FloatParam = -10000.125
buf[24] = 0x00;
buf[25] = 0x00;
buf[26] = 0x00;
buf[27] = 0x10;
buf[28] = 0x88;
buf[29] = 0xc3;
buf[30] = 0xc0;
buf[31] = 0x48; // StrParam = "Help!"
buf[32] = 0x65;
buf[33] = 0x6c;
buf[34] = 0x70;
buf[35] = 0x21;
buf[36] = 0x00;
buf[37] = 0x20; // Checksum
buf[38] = 0x96;
buf[39] = 0x54;
buf[40] = 0x1c;
buf_size = 41;
ret_value = pkt.Extract( buf, buf_size, last_bit );
if( ret_value )
{
cout << "Error extracting data\n";
return 1;
}
//
// Get a list of the parameters in the packet sorted by their start
// location. Get the values of all samples of each parameter as a string.
//
StringArray all_params;
pkt.GetSortedParameterList( all_params );
for( uint32_t ii = 0; ii < all_params.Size(); ii++ )
{
pkt.FindParameter( all_params.GetAt(ii), &param_ptr );
//
// See how many samples a parameter has and get each one.
//
for( uint32_t jj = 1; jj <= param_ptr->GetNumberOfSamples(); jj++ )
printf("%s (sample %u): %s\n", all_params.GetAt(ii),
jj,
param_ptr->GetValueAsString(jj) );
}
// THE END
return 0;
}
This class describes a packet composed of one or more parameters.
Definition: packet.h:72
int32_t LoadFile(const char *filename)
Loads the Packet definition from the specified file.
Definition: packet.cpp:2756
void GetSortedParameterList(StringArray &param_list, bool full_name=true, bool mod_only=false)
Returns a list of all parameters within a packet sorted by their start location.
Definition: packet.cpp:2553
int32_t Extract(uint8_t *input_ptr, uint32_t input_length, uint32_t &last_bit_used)
Extracts all of the parameters in the packet from the specified buffer.
Definition: packet.cpp:1714
int32_t CreateGlobalPacketMap()
Creates the map used to hold all of the parameters.
Definition: packet.cpp:2138
int32_t FindParameter(const char *name, Parameter **param_ptr)
Finds the specified parameter name in the packet.
Definition: packet.cpp:2348
This class describes a single parameter within a telemetry or command message including its value.
Definition: parameter.h:95
const char * GetValueAsString(uint16_t sample_number=1)
Returns the value of the parameter as a string.
Definition: parameter.cpp:11511
uint32_t GetNumberOfSamples()
Gets the number of samples for the parameter.
Definition: parameter.cpp:3261
This class provides a wrapper for std::vector and std::string so different versions of the standard t...
Definition: string_array.h:19
uint32_t Size()
Returns the number of items in the string array.
Definition: string_array.cpp:67
const char * GetAt(uint32_t index)
Returns the string at the given location.
Definition: string_array.cpp:89
Defines the trek::Packet class.
Defines the trek::Parameter class.
Defines the trek::ParameterCollection class.