TReK C++  5.3.3
Telemetry/Command API
tlm_api_cpp_next/cpp_next_main.cpp
#include <stdio.h>
#include "telemetry_api.h"
#include "parameter.h"
#include "packet.h"
//
// NOTE: This example requires the TReK Data GUI to be started and the PDSS Payload
// Packet with APID 7 to be activated with a Real-Time data mode.
//
using namespace trek;
int main()
{
TelemetryApi tlm_api;
StringArray pkt_key_list;
Packet pkt;
uint8_t buf[4096];
uint32_t buf_len;
uint32_t token = 0; // always initialize the token to zero
uint32_t last_token = 0;
int ret_value;
int next_ret_value;
//
// Connect to a data store.
//
ret_value = tlm_api.Connect("DefaultDataStore");
if( ret_value )
{
printf( "Error %d: Could not connect to data store.\n", ret_value );
return 1;
}
//
// Calling this method allows the API to clean up resources that may be
// left over from a user program that crashed. Not checking the return
// value as it is safe to continue if this method fails.
//
tlm_api.Cleanse();
//
// Get the packet definition so that parameters can be extracted
// from the packet.
//
ret_value = tlm_api.GetPacketDefinition( "PdssPayload.RT.PL.7", pkt );
if( ret_value )
{
printf( "Error %d: Could not get packet definition.\n", ret_value );
return 1;
}
//
// Register a signal that will allow you to know when a packet arrives.
//
ret_value = tlm_api.RegisterPacketSemaphore( "PdssPayload.RT.PL.7" );
if( ret_value )
{
printf( "Error %d: Could not register for packet semaphore. Is the key correct?\n", ret_value );
return 1;
}
//
// Check to see if a packet is already arrived and process all of them. Once there
// is no more new data, wait for the next packet to arrive and continue. This code
// will exit once the packet arrival event is released 100 times.
//
uint32_t event_triggered = 0;
do
{
do
{
buf_len = 4096; // always reset this value for each call
next_ret_value = tlm_api.GetNextPacket( "PdssPayload.RT.PL.7", token, buf, buf_len );
if( ret_value == SUCCESS || ret_value == TLM_API_MORE_DATA_AVAILABLE || ret_value == TLM_API_MISSING_DATA )
{
//
// Use the packet definition to extract the data.
//
uint32_t last_bit_used;
ret_value = pkt.Extract( buf, buf_len, last_bit_used );
if( ret_value )
{
printf( "Error %d: Could not extract parameter data from the packet.\n", ret_value );
}
else
{
if( last_token == token )
{
// data has not changed. this is possible when using packet arrival triggers and
// get next packet. if preventing duplicate data is important, this is a good
// check.
}
else
{
//
// The packet has been extracted and data is new. You can also print all of the
// values in the Packet at one time.
//
last_token = token;
printf( "Printing all parameter values\n" );
printf( "%s\n", pkt.PrintValues() );
}
}
}
else
{
printf( "Error %d: Could not get the next packet.\n", ret_value );
}
} while( next_ret_value == TLM_API_MORE_DATA_AVAILABLE || next_ret_value == TLM_API_MISSING_DATA );
//
// Now wait for a new packet to arrive. While you are retrieving that packet other data may
// also arrive. The GetNextPacket code above will get it without having to wait again if
// this occurs.
//
ret_value = tlm_api.WaitForPacket( "PdssPayload.RT.PL.7" ); // using default, infinite wait
if( ret_value )
{
// If you have a timeout, you may get TCA_WAIT_TIMEOUT returned. You can wait again
// after a timeout as it is expected behavior and not an error.
printf( "Error %d: Error when attempting to wait for packet.\n", ret_value );
}
event_triggered++;
} while( event_triggered < 100 );
//
// You should unregister the signal to free up resources if it is no longer needed.
//
ret_value = tlm_api.UnregisterPacketSemaphore( "PdssPayload.RT.PL.7" );
if( ret_value )
{
printf( "Error %d: Could not unregister packet semaphore.\n", ret_value );
return 1;
}
//
// It is always best to call Disconnect when you are finished with the
// API. There are system resources that can be freed when you no
// longer need the API.
//
ret_value = tlm_api.Disconnect();
if( ret_value )
{
printf( "Error %d: API did not disconnect properly.\n", ret_value );
return 1;
}
return 0;
}
int32_t Connect(const char *name)
Connects this instance of the API to TReK for the specified destination.
Definition: api_client.cpp:131
int32_t Disconnect()
Disconnects from the destination.
Definition: api_client.cpp:344
int32_t Cleanse()
Cleans up any resources from crashed user programs.
Definition: api_client.cpp:469
This class describes a packet composed of one or more parameters.
Definition: packet.h:72
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
const char * PrintValues()
Returns a string with the value of all parameters in the packet.
Definition: packet.cpp:2898
This class provides a wrapper for std::vector and std::string so different versions of the standard t...
Definition: string_array.h:19
Provides access to telemetry features of TReK.
Definition: telemetry_api.h:44
int32_t RegisterPacketSemaphore(const char *pkt_key)
Registers a semaphore to be signaled when a packet arrives.
Definition: telemetry_api.cpp:706
int32_t GetNextPacket(const char *pkt_key, uint32_t &token, uint8_t *buf, uint32_t &len)
Gets a copy of the next packet available from the data store.
Definition: telemetry_api.cpp:502
int32_t GetPacketDefinition(const char *pkt_key, Packet &pkt)
Retrieve the Packet definition for the specified key.
Definition: telemetry_api.cpp:296
int32_t WaitForPacket(const char *pkt_key, uint32_t timeout=0)
Waits for the specified packet to arrive.
Definition: telemetry_api.cpp:823
int32_t UnregisterPacketSemaphore(const char *pkt_key)
Unregisters the packet arrival semaphore.
Definition: telemetry_api.cpp:1104
Defines the trek::Packet class.
Defines the trek::Parameter class.
Defines the trek::TelemetryApi class.
Error codes for the Telemetry API (starts at 28001)
#define TLM_API_MORE_DATA_AVAILABLE
(Considered Success) The function completed successfully and you can make another call to get even mo...
Definition: telemetry_api_error_codes.h:38
#define TLM_API_MISSING_DATA
(Considered Success) Some of the requested data was missing. Oldest available data returned.
Definition: telemetry_api_error_codes.h:37
#define SUCCESS
The function completed successfully.
Definition: trek_error.h:8