TReK C++  5.3.3
Telemetry/Command API
cmd_api_cpp/cmd_api_cpp_main.cpp
#include "command_api.h"
#include "string_array.h"
#include "packet.h"
#include "track_item.h"
#include <map>
#include <string>
using namespace trek;
// want to have a map of command responses
typedef std::map< std::string, CommandResponse * > CMD_RESP_MAP;
int main(void)
{
int32_t ret_value;
//
// Connect to a destination. The name passed into the Connect() call is
// the name used when configuring the destination in the TReK Command
// application.
//
ret_value = api.Connect( "POIC" );
if( ret_value )
{
printf( "Error %d: Could not connect to destination.\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.
//
api.Cleanse();
//
// Once connected to the API, you can make calls to the API to see what is
// available for the interface. Call to see if there are any status
// messages. If there are, print out all of the information in each
// message.
//
StringArray status_list;
ret_value = api.GetStatusMessageList( status_list );
if( ret_value )
{
printf( "Error %d: Could not get status message list.\n", ret_value );
return 1;
}
printf( "There are %u status messages for this destination.\n", status_list.Size() );
for( uint32_t ii = 0; ii < status_list.Size(); ii++ )
{
printf( "Status Message: %s\n", status_list.GetAt(ii) );
char info[1024];
uint32_t info_space = 1024;
ret_value = api.GetStatusMessage( status_list.GetAt(ii),
pc,
info,
&info_space );
if( ret_value )
{
printf( "Error %d: Could not retrieve status message %s.\n", ret_value, status_list.GetAt(ii) );
// not exiting here, we can still use the API
}
else
{
StringArray param_list;
// get all of the parameters that make up the status message
pc.GetSortedParameterList( param_list, false );
for( uint32_t jj = 0; jj < param_list.Size(); jj++ )
{
Parameter *param_ptr;
// print each name/value in status mesesage
ret_value = pc.FindParameter( param_list.GetAt(jj), &param_ptr );
if( ret_value )
printf( "Error %d: Unexpected error when finding parameter %s.\n", ret_value, param_list.GetAt(jj) );
else
printf( "%s: %s\n", param_list.GetAt(jj), param_ptr->GetValueAsString() );
}
}
}
//
// Check to see if there are command responses associated with this
// destination. If there is, get a copy of the responses so we can
// check any errors that are received when sending commands. Store
// the responses in a map for quick lookup.
//
StringArray resp_list;
ret_value = api.GetCommandResponseList( resp_list );
if( ret_value )
{
printf( "Error %d: Could not get command response list.\n", ret_value );
return 1;
}
CMD_RESP_MAP response_map;
for( uint32_t ii = 0; ii < resp_list.Size(); ii++ )
{
CommandResponse *cmd_response_ptr = new CommandResponse;
ret_value = api.GetCommandResponse( resp_list.GetAt(ii),
*cmd_response_ptr );
if( ret_value )
{
printf( "Error %d: Could not retrieve response %s.\n", ret_value, resp_list.GetAt(ii) );
// don't exit as this isn't fatal...and should never occur unless API has coding error.
}
else
response_map[resp_list.GetAt(ii)] = cmd_response_ptr;
}
//
// Send a command by initiating an uplink from the destination. This method
// of sending commands is not avaiable for all destinations, but will fail
// gracefully. The token will be used to get command track information.
//
uint32_t token;
ret_value = api.InitiateCommand( "ACCELERATE", token );
if( ret_value )
printf( "Error %d: Could not initiate command.\n", ret_value );
TrackItem track_item;
ret_value = api.GetTrack( token, track_item );
if( ret_value )
printf( "Error %d: Could not track command.\n", ret_value );
else
{
for( uint32_t ii = 0; ii < track_item.GetNumberOfResponses(); ii++ )
{
response_item resp_item;
track_item.GetResponse( ii, resp_item );
CMD_RESP_MAP::iterator response_map_it;
response_map_it = response_map.find( resp_item.name );
if( response_map_it != response_map.end() )
{
struct response_info resp_info;
response_map_it->second->GetErrorDetails( resp_item.value, &resp_info );
printf( "Response %s: %s.\n", resp_item.name, resp_info.info );
}
}
}
//
// 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 = 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
Provides access to command features of TReK.
Definition: command_api.h:34
int32_t GetCommandResponse(const char *name, CommandResponse &resp)
Get the details for a command response.
Definition: command_api.cpp:1606
int32_t GetCommandResponseList(StringArray &resp_list)
Get a list of possible command responses for the command source.
Definition: command_api.cpp:1522
int32_t GetTrack(uint32_t token, TrackItem &track_item)
Find out any command track information for this 'token'.
Definition: command_api.cpp:2610
int32_t GetStatusMessageList(StringArray &status_list)
Get a list of messages that the user can send.
Definition: command_api.cpp:852
int32_t GetStatusMessage(const char *name, ParameterCollection &pc, char *info, uint32_t *space_available)
Get the specified status message details.
Definition: command_api.cpp:944
int32_t InitiateCommand(const char *name, uint32_t &token)
Sends a request to the final destination to uplink a command.
Definition: command_api.cpp:2516
This class holds all of the responses associated with a single command uplink.
Definition: command_response.h:28
This class describes a parameter collection composed of one or more parameters.
Definition: parameter_collection.h:24
virtual int32_t FindParameter(const char *name, Parameter **param_ptr)
Finds the specified parameter in the collection.
Definition: parameter_collection.cpp:423
virtual void GetSortedParameterList(StringArray &param_list, bool full_name=true, bool mod_only=false)
Returns a list of all parameters within a collection sorted by their start location.
Definition: parameter_collection.cpp:713
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
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
This class holds all of the responses associated with a single command uplink. Only methods that are ...
Definition: track_item.h:30
uint32_t GetNumberOfResponses()
Returns the number of responses available.
Definition: track_item.cpp:118
int32_t GetResponse(uint32_t index, struct response_item &item)
Gets the information about the specified response as a structure.
Definition: track_item.cpp:140
Defines the trek::CommandApi class.
Defines the trek::CommandResponse class.
Defines the trek::Packet class.
Defines the trek::ParameterCollection class.
Defines the trek::StringArray class.
Defines the trek::TrackItem class.