TReK ANSI-C  5.3.3
All ANSI-C APIs
record_api/main.c
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program records four packets in two different record files
// (two packets per file) using the record library device api. The record
// library device is configured using the record_api_config.txt file and the
// record_api_php_config.txt file. The record library initiates two CFDP
// "put" primitive when the two record files are closed. Two instances
// of the CFDP console application must be running to successfully transfer
// the files. The CFDP configuration files are cfdp_source_config.txt and
// cfdp_destination_config.txt. All paths defined in the record_api_config.txt
// file are relative to the location of the executable file so both
// the record_api executable and the trek_cfdp_console executables must
// be run from the same directory. The program uses RegisterPrintMessage,
// StartLoggingMessages, StopLoggingMessages, InitToolkitRecordDevice,
// RegisterRecordDeviceData, RecordPacket, CloseRecordFile and
// DSCleanUp API functions.
//
// ////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "ds_shared.h"
#include "trek_error.h"
#include <sys/timeb.h>
#ifdef __linux__
#include <unistd.h>
#else
#include <windows.h>
#endif
// ////////////////////////////////////////////////////////////////////////////
//
// PrintTheMessage controls how messages are processed and displayed to
// the user.
//
// ////////////////////////////////////////////////////////////////////////////
void PrintTheMessage(message_struct_type *mess_struct_ptr)
{
// Do not print the debug messages
if (mess_struct_ptr->category != MSG_CAT_DEBUG)
printf("%s\t%s\n",GetMessageCategoryAsString(mess_struct_ptr->category),
mess_struct_ptr->message);
}
// ////////////////////////////////////////////////////////////////////////////
//
// DeviceData's packet_buffer_ptr processes a record_device_struct ptr
// defined as follows:
//
// unsigned long ds_message_code; (e.g., DS_CLOSE)
// char record_library_device_name[256];
// char record_file_base_name[256];
// char record_path_and_filename[256];
//
// The record_device_struct can be used to determine when the record library
// device closes a record file as well as the new name of the
// record file
//
// ////////////////////////////////////////////////////////////////////////////
void DeviceData(const char *record_library_device_name,
int packet_length,
unsigned char *packet_buffer_ptr)
{
record_device_struct_type *record_device_struct_ptr;
record_device_struct_ptr = (record_device_struct_type *)packet_buffer_ptr;
if (record_device_struct_ptr->ds_message_code == DS_CLOSE)
{
printf("Successfully closed record file with pathname: %s.\n",
record_device_struct_ptr->record_path_and_filename);
}
}
//
// NAME: ByteSwap4Bytes
//
// INPUTS:
//
// OUTPUTS: unsigned char *packet
//
// RETURNS: none
//
// DESCRIPTION:
//
void ByteSwap4Bytes(unsigned char *packet)
{
unsigned char buffer[4];
memmove(buffer, packet, 4);
memmove(packet, &buffer[3], 1);
memmove(packet + 1, &buffer[2], 1);
memmove(packet + 2, &buffer[1], 1);
memmove(packet + 3, &buffer[0], 1);
}
// ////////////////////////////////////////////////////////////////////////////
//
// CreateDataPackets creates four packets with an eight byte header.
// The header is defined in the record_tcp_php_config.txt file.
// Header definition: 2 byte big endian overall packet length
// 4 byte big endian unix time stamp
// 2 byte big endian mille time stamp
//
// ////////////////////////////////////////////////////////////////////////////
void CreateDataPackets(unsigned char *packet1_ptr,
unsigned char *packet2_ptr,
unsigned char *packet3_ptr,
unsigned char *packet4_ptr,
unsigned short *length1_ptr,
unsigned short *length2_ptr,
unsigned short *length3_ptr,
unsigned short *length4_ptr)
{
// Determine if this is a little endian or big endian platform.
unsigned short tester= 1;
unsigned char tester_byte[1];
unsigned short big_endian_order = 1;
struct timeb current_time;
unsigned int tim;
memcpy(tester_byte,&tester,1);
if (tester_byte[0] == 0x01)
{
big_endian_order = 0;
}
// Zero fill the packets prior to populating.
memset(packet1_ptr,0x00,50);
memset(packet2_ptr,0x00,50);
memset(packet3_ptr,0x00,50);
memset(packet4_ptr,0x00,50);
// Create an eight byte header for all the packets. The header is defined
// in the record_tcp_php_config.txt file.
// Header definition: 2 byte big endian overall packet length
// 4 byte big endian unix time stamp
// 2 byte big endian mille time stamp
// Populate the time zone
if (big_endian_order == 1)
{
ftime(&current_time);
tim = (unsigned int)current_time.time;
memmove(packet1_ptr+2, (unsigned char*)&tim,4);
memmove(packet1_ptr+6, (unsigned char*)&current_time.millitm,2);
//
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
ftime(&current_time);
tim = (unsigned int)current_time.time;
memmove(packet2_ptr+2, (unsigned char*)&tim,4);
memmove(packet2_ptr+6, (unsigned char*)&current_time.millitm,2);
//
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
ftime(&current_time);
tim = (unsigned int)current_time.time;
memmove(packet3_ptr+2, (unsigned char*)&tim,4);
memmove(packet3_ptr+6, (unsigned char*)&current_time.millitm,2);
//
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
ftime(&current_time);
tim = (unsigned int)current_time.time;
memmove(packet4_ptr+2,(unsigned char*)&tim,4);
memmove(packet4_ptr+6,(unsigned char*)&current_time.millitm,2);
}
else
{
ftime(&current_time);
tim = (unsigned int)current_time.time;
ByteSwap4Bytes((unsigned char*)&tim);
memmove(packet1_ptr + 2, (unsigned char*)&tim, 4);
memmove(packet1_ptr+6, (unsigned char*)&current_time.millitm+1,1);
memmove(packet1_ptr+7, (unsigned char*)&current_time.millitm,1);
//
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
ftime(&current_time);
tim = (unsigned int)current_time.time;
ByteSwap4Bytes((unsigned char*)&tim);
memmove(packet2_ptr + 2, (unsigned char*)&tim, 4);
memmove(packet2_ptr+6, (unsigned char*)&current_time.millitm+1,1);
memmove(packet2_ptr+7, (unsigned char*)&current_time.millitm,1);
//
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
ftime(&current_time);
tim = (unsigned int)current_time.time;
ByteSwap4Bytes((unsigned char*)&tim);
memmove(packet3_ptr + 2, (unsigned char*)&tim, 4);
memmove(packet3_ptr+6, (unsigned char*)&current_time.millitm+1,1);
memmove(packet3_ptr+7, (unsigned char*)&current_time.millitm,1);
//
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
ftime(&current_time);
tim = (unsigned int)current_time.time;
ByteSwap4Bytes((unsigned char*)&tim);
memmove(packet4_ptr + 2, (unsigned char*)&tim, 4);
memmove(packet4_ptr+6, (unsigned char*)&current_time.millitm+1,1);
memmove(packet4_ptr+7, (unsigned char*)&current_time.millitm,1);
}
// Populate packet data zone
strcpy((char *)packet1_ptr+8,"Mary had a little lamb");
strcpy((char *)packet2_ptr+8,"Its feet were black as soot");
strcpy((char *)packet3_ptr+8,"And into Mary's bread and jam ");
strcpy((char *)packet4_ptr+8,"Its sooty foot it put");
// Get packet length including 5 byte header and null terminator.
*length1_ptr = (unsigned short)strlen(packet1_ptr+8) + 8;
*length2_ptr = (unsigned short)strlen(packet2_ptr+8) + 8;
*length3_ptr = (unsigned short)strlen(packet3_ptr+8) + 8;
*length4_ptr = (unsigned short)strlen(packet4_ptr+8) + 8;
// Populate headers.
// Populate packet length field.
if (big_endian_order == 1)
{
memmove(packet1_ptr,length1_ptr,2);
memmove(packet2_ptr,length2_ptr,2);
memmove(packet3_ptr,length3_ptr,2);
memmove(packet4_ptr,length4_ptr,2);
}
else
{
memmove(packet1_ptr+1,length1_ptr,1);
memmove(packet2_ptr+1,length2_ptr,1);
memmove(packet3_ptr+1,length3_ptr,1);
memmove(packet4_ptr+1,length4_ptr,1);
}
return;
}
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program records four packets in two different record files
// (two packets per file) using the record library device api. The record
// library device is configured using the record_api_config.txt file and the
// record_api_php_config.txt file. The record library initiates two CFDP
// "put" primitive when the two record files are closed. Two instances
// of the CFDP console application must be running to successfully transfer
// the files. The CFDP configuration files are cfdp_source_config.txt and
// cfdp_destination_config.txt. All paths defined in the record_api_config.txt
// file are relative to the location of the executable file so both
// the record_api executable and the trek_cfdp_console executables must
// be run from the same directory. The program uses RegisterPrintMessage,
// StartLoggingMessages, StopLoggingMessages, InitToolkitRecordDevice,
// RegisterRecordDeviceData, RecordPacket, CloseRecordFile and
// DSCleanUp API functions.
//
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
unsigned char packet1[50];
unsigned char packet2[50];
unsigned char packet3[50];
unsigned char packet4[50];
unsigned short length1;
unsigned short length2;
unsigned short length3;
unsigned short length4;
char pathname[256];
char record_filename[256];
char time_tagged_record_file_name[256];
unsigned int time_tagged_record_file_name_buffer_size;
int return_value;
// Set default record library config_pathname
strcpy (pathname, "./record_api_config.txt");
if (argc == 2)
{
strcpy(pathname,argv[1]);
}
else if (argc > 2)
{
printf("Error Command line contains too many arguments.\n");
return 0;
}
// Register the PrintMessage callback function prior to InitToolkitCfdp
// to process error messages that may be generated during initialization.
RegisterMessage(&PrintTheMessage);
printf("\nRecord TCP\n\n");
printf("Create four data packets.\n");
// Create data packets.
CreateDataPackets(packet1,
packet2,
packet3,
packet4,
&length1,
&length2,
&length3,
&length4);
// Initialize the record library device. This funcition creates,
// connects and configures the sockets associated with
// record library
if (InitToolkitRecordDevice(pathname) == SUCCESS)
{
// Associate the DeviceData callback function with the record
// device library. The function will print a message when a
// record file is closed and renamed by the record library device.
if (RegisterRecordDeviceData(&DeviceData) != SUCCESS)
{
return 0;
}
// Send packet packet1 using record library device api
// NOTE: The record file name is found in the record
// library device configuration file.
strcpy(record_filename,"pkt1andpkt2");
if (RecordPacket(record_filename,length1,packet1) != SUCCESS)
{
return 0;
}
// Send packet packet2 using record library device api
if (RecordPacket(record_filename,length2,packet2) != SUCCESS)
{
return 0;
}
// Close the pkt1andpkt2 record file
time_tagged_record_file_name_buffer_size = sizeof(time_tagged_record_file_name);
if ((return_value = CloseRecordFile(record_filename,
&time_tagged_record_file_name_buffer_size, time_tagged_record_file_name)) == SUCCESS)
{
printf("First time tagged record file name: %s\n", time_tagged_record_file_name);
}
else
{
return 0;
}
// Send packet packet3 using record library device api
// NOTE: The record file name is found in the record
// library device configuration file.
strcpy(record_filename,"pkt3andpkt4");
if (RecordPacket(record_filename,length3,packet3) != SUCCESS)
{
return 0;
}
// Send packet packet4 using record library device api
if (RecordPacket(record_filename,length4,packet4) != SUCCESS)
{
return 0;
}
// Close the pkt3andpkt4 record file
if ((return_value = CloseRecordFile(record_filename,
&time_tagged_record_file_name_buffer_size, time_tagged_record_file_name)) == SUCCESS)
{
printf("Second time tagged record file name: %s\n", time_tagged_record_file_name);
}
else
{
return 0;
}
}
// Allow time to print and log messages.
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
// Close the log file. A timetag is appended to the log filename.
return 0;
}
Message codes and structure definition for the Device Service library.
#define DS_CLOSE
4 DS message code for Close
Definition: ds_shared.h:29
Structure of parameters needed for message support.
Definition: trek_toolkit_common_api_ansi_c.h:74
char message[MAX_MESSAGE_SIZE]
Message.
Definition: trek_toolkit_common_api_ansi_c.h:77
enum message_category category
Message category (e.g., MSG_CAT_ERROR, MSG_CAT_ERROR_ALERT, MSG_CAT_WARNING, MSG_CAT_WARNING_ALERT,...
Definition: trek_toolkit_common_api_ansi_c.h:76
Structure of parameters needed for record device status.
Definition: ds_shared.h:198
char record_path_and_filename[256]
Path and filename of closed record file.
Definition: ds_shared.h:202
unsigned long ds_message_code
DS message code describing the event (i.e., DS_CLOSE, a record file was closed).
Definition: ds_shared.h:199
An ANSI C Data Service API.
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION DSCleanUp()
Initiates a graceful shutdown of the Device Service library and all supporting device libraries,...
Definition: toolkit_ds_api_ansi_c.cpp:3843
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION StopLoggingMessages()
Stops logging messages to a file, closes the log file and renames the log file by concatenating the l...
Definition: toolkit_ds_api_ansi_c.cpp:3479
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION RegisterMessage(void(*function_ptr)(message_struct_type *message_struct_ptr))
Register a callback function to receive and process messages issued by the DS library.
Definition: toolkit_ds_api_ansi_c.cpp:3951
Error codes for the DS API (starts at 50001)
An ANSI C record device API.
int EXPORT_THIS_TOOLKIT_RECORD_DEVICE_C_FUNCTION RecordPacket(const char *record_file_name, int packet_length, unsigned char *packet_buffer_ptr)
Records packet in the designated record file. The record file name must be defined in the Record Devi...
Definition: toolkit_record_device_api_ansi_c.cpp:458
int EXPORT_THIS_TOOLKIT_RECORD_DEVICE_C_FUNCTION InitToolkitRecordDevice(const char *config_pathname)
Intializes the Record Device library using parameters read from a configuration file.
Definition: toolkit_record_device_api_ansi_c.cpp:110
int EXPORT_THIS_TOOLKIT_RECORD_DEVICE_C_FUNCTION RegisterRecordDeviceData(void(*function_ptr)(const char *record_library_device_name, int packet_length, unsigned char *packet))
Register a callback function to receive receive record_devcie_struct status messages including the na...
Definition: toolkit_record_device_api_ansi_c.cpp:852
int EXPORT_THIS_TOOLKIT_RECORD_DEVICE_C_FUNCTION CloseRecordFile(const char *record_file_name, unsigned int *time_tagged_record_file_name_buffer_size_ptr, char *time_tagged_record_file_name)
CloseRecordFile closes the designated record file. The new time tagged record file name is returned i...
Definition: toolkit_record_device_api_ansi_c.cpp:617
Command codes for TReK.
#define SUCCESS
The function completed successfully.
Definition: trek_error.h:8
The commonly shared macros, structures and functions.
@ MSG_CAT_DEBUG
Debug message.
Definition: trek_toolkit_common_api_ansi_c.h:63
const char EXPORT_THIS_TREK_TOOLKIT_COMMON_API_FUNCTION * GetMessageCategoryAsString(enum message_category input)
Converts an enumerated message category value into its equivalent character string.
Definition: trek_toolkit_common_api_ansi_c.cpp:45