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