Libraries
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;
}