TReK ANSI-C  5.3.3
All ANSI-C APIs
cfdp_put_example_1/main.c
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program creates a 3MB data file. The file is created and
// transferred from/to its home/user directory via the cfdp_destination
// application. The program uses the PutComponentCFDP API function. The
// destination file is renamed with a time tag extension and the source file
// is deleted. Calls the MonitorTransactions example function to the monitor
// CFDP transaction using the PopulateCFDPStructArray function.
//
// Main with optional input defining pathname for the CFDP configuration file.
// The default CFDP configuration file pathname is
// "./cfdp_put_example_1_config.txt".
//
// The "cfdp_destination" example must be running prior to starting this example.
//
// ////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/timeb.h>
#include <string.h>
#include "ds_shared.h"
#include "cfdp_shared.h"
#include "trek_error.h"
#ifdef __linux__
#include <unistd.h>
#else
#include <windows.h>
#endif
unsigned int message_mask;
// ////////////////////////////////////////////////////////////////////////////
//
// PrintTheMessage controls how messages are processed and displayed to
// the user.
//
// ////////////////////////////////////////////////////////////////////////////
void PrintTheMessage(message_struct_type *mess_struct_ptr)
{
if (mess_struct_ptr->category & message_mask)
{
printf("%s%s\n",GetMessageCategoryAsPaddedString(mess_struct_ptr->category),
mess_struct_ptr->message);
}
}
// ////////////////////////////////////////////////////////////////////////////
//
// DeviceData's packet_buffer_ptr processes a cfdp_struct ptr defined as
// follows:
//
// char source_pathname[200];
// char destination_pathname[200];
// long long eid; (destination)
// char transaction_id[50];
// unsigned short percent_transferred;
// unsigned long bytes_transferred;
// unsigned long file_size;
// unsigned long cfdp_configuration; (e.g., DS_DEVICE_SENDING,
// DS_DEVICE_RECEIVING)
// unsigned long cfdp_status; (e.g., DS_PACKET_SENDING,
// DS_PACKET_RECEIVING,
// DS_SUSPEND, DS_RESUME,
// DS_REPORT,
// DS_CANCEL,
// DS_SUCCESS,
// DS_FAIL,
// DS_ABANDON,
// DS_UNKNOWN)
//
// The cfdp_struct can be used to determine each file's transactions ID as
// well as monitor a file's transfer status.
//
// ////////////////////////////////////////////////////////////////////////////
void DeviceData(const char *device_key,
int packet_length,
unsigned char *packet_buffer_ptr)
{
unsigned int count;
cfdp_struct_type *cfdp_struct_ptr;
// Retrieve the individual cfdp structs from the packet_buffer;
for (count = 0; count*sizeof(cfdp_struct_type) < (unsigned int)packet_length; count++)
{
cfdp_struct_ptr = (cfdp_struct_type *)(packet_buffer_ptr + count*sizeof(cfdp_struct_type));
if (message_mask & MSG_CAT_PROGRESS)
{
printf("Progress Transaction_id %s -> %s\t file size=%lld\t bytes trans=%lld\t percent trans=%u%%\n",
cfdp_struct_ptr->transaction_id,
cfdp_struct_ptr->destination_pathname,
cfdp_struct_ptr->file_size,
cfdp_struct_ptr->bytes_transferred,
cfdp_struct_ptr->percent_transferred);
}
// Delete the source file if it was successfully transferred to its destination
if (cfdp_struct_ptr->cfdp_status == DS_SUCCESS)
{
if (remove(cfdp_struct_ptr->source_pathname) != 0)
{
printf("Error Failed to remove the file: %s.\n",
cfdp_struct_ptr->source_pathname);
}
}
}
}
// ////////////////////////////////////////////////////////////////////////////
//
// CreateDataFile creates a binary file with the specified pathname and size.
//
// ////////////////////////////////////////////////////////////////////////////
int CreateDataFile(char *pathname,
unsigned int file_size)
{
FILE *file_ptr = NULL;
char fill_data[250];
unsigned int i;
if ((file_ptr = fopen(pathname,"wb")) == NULL)
{
printf("Error Failed to open example data file.\n");
return(FAIL);
}
// Write to data file
memset(fill_data,0xfe,sizeof(fill_data));
for (i = 0; i < file_size; i+=sizeof(fill_data))
fwrite(fill_data,1,sizeof(fill_data),file_ptr);
fflush(file_ptr);
fclose(file_ptr);
return(SUCCESS);
}
// ////////////////////////////////////////////////////////////////////////////
//
// InitExampleDataFiles creates a 3MB example data file and returns the
// appropriate source and destination pathnames.
//
// ////////////////////////////////////////////////////////////////////////////
int InitExampleDataFiles(char *pathname3MB,
char *destination_pathname3MB)
{
struct timeb timebuffer;
char home_path[256];
if (TCAACGetHomeDirectory(home_path,sizeof(home_path)) != SUCCESS)
{
return(FAIL);
}
// Create a 3MB example data files
ftime(&timebuffer);
sprintf(pathname3MB, "%s/put_example_1_data_file_3MB",home_path);
if (CreateDataFile(pathname3MB,3000000) == FAIL)
{
return(FAIL);
}
// Create a destination time tag appended file name
sprintf(destination_pathname3MB,"%s_%u",
pathname3MB,
(unsigned int)timebuffer.time);
return(SUCCESS);
}
// ////////////////////////////////////////////////////////////////////////////
//
// MonitorTransactions provides an example on how to monitor CFDP transactions
// using the GetCFDPStructArray function. It returns upon completion of all
// transactions. If a transaction fails an error message is printed to the
// screen.
//
// NOTE: The cfdp config file "support_cfdp_status_requests" parameter must be
// "true" to support transaction status monitoring.
//
// ////////////////////////////////////////////////////////////////////////////
int MonitorTransactions(unsigned int monitor_timeout,
unsigned int transaction_count)
{
int return_value;
unsigned int current_transaction_count = 0;
unsigned int final_transaction_count = transaction_count;
unsigned int number_of_cfdp_structs = 0;
cfdp_struct_type *cfdp_struct_array_ptr = NULL;
struct timeb start_timebuffer;
struct timeb current_timebuffer;
unsigned int i;
// Start timer
ftime(&start_timebuffer);
// Allocate memory for the cfdp_struct_array
cfdp_struct_array_ptr=(cfdp_struct_type *)malloc(final_transaction_count *
sizeof(cfdp_struct_type));
// Wait for transaction to complete by checking to see if the transaction
// is still being processed using PopulateCFDPStructArray.
while (current_transaction_count != final_transaction_count)
{
// Set the number_of_cfdp_structs equal to the number of structs
// availiable in the array (i.e., every transaction will have a
// corresponding cfdp_struct in the array)
number_of_cfdp_structs = final_transaction_count;
// Get an array of the current CFDP transactions
if ((return_value = PopulateCFDPStructArray(&number_of_cfdp_structs,
cfdp_struct_array_ptr)) == DS_ARRAY_SIZE_ERROR)
{
// The cfdp_struct_array is not large enough to hold the current
// number of cfdp structs so allocate additional memory using the
// correct number_of_cfdp_structs returned by the
// GetCFDPStructArray function.
free(cfdp_struct_array_ptr);
cfdp_struct_array_ptr=(cfdp_struct_type *)malloc(number_of_cfdp_structs *
sizeof(cfdp_struct_type));
// Reset the final_transaction_count
final_transaction_count = number_of_cfdp_structs;
return_value = PopulateCFDPStructArray(&number_of_cfdp_structs,
cfdp_struct_array_ptr);
}
if (return_value == SUCCESS)
{
// Loop through the array
for (i=0; i<number_of_cfdp_structs;i++)
{
// Check to see if the transaction has completed the file
// transfer attempt
if (cfdp_struct_array_ptr[i].cfdp_status == DS_CANCEL ||
cfdp_struct_array_ptr[i].cfdp_status == DS_SUCCESS ||
cfdp_struct_array_ptr[i].cfdp_status == DS_FAIL ||
cfdp_struct_array_ptr[i].cfdp_status == DS_ABANDON ||
cfdp_struct_array_ptr[i].cfdp_status == DS_UNKNOWN)
{
// Check for unsuccessful file transfer status
//(e.g.,DS_CANCEL, DS_FAIL, DS_ABANDON, DS_UNKNOWN)
if (cfdp_struct_array_ptr[i].cfdp_status != DS_SUCCESS)
{
// Take appropriate action
printf("Info Failed to execute put file for destination pathname: %s and eid: %d.\n",
cfdp_struct_array_ptr[i].destination_pathname,
(int)cfdp_struct_array_ptr[i].eid);
}
// Increment count of completed transactions
current_transaction_count += 1;
}
}
// Sleep 1 second prior to checking status
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
ftime(&current_timebuffer);
if (current_timebuffer.time - start_timebuffer.time >
monitor_timeout)
{
return_value = FAIL;
printf("Error CFDP monitor transaction timeout.\n");
current_transaction_count= final_transaction_count;
}
}
else
{
// Error getting CFDP status
return_value = FAIL;
printf("Error Failed to GetCFDPStructArray.\n");
current_transaction_count = final_transaction_count;
}
}
free(cfdp_struct_array_ptr);
return(return_value);
}
// ////////////////////////////////////////////////////////////////////////////
//
// Main with optional input defining pathname for the CFDP configuration file.
// The default CFDP configuration file pathname is
// "./cfdp_put_example_1_config.txt".
//
// Sample program creates a 3MB data file. This file is created and
// transferred from/to its home/user directory by the cfdp_destination
// application. This program uses the PutComponentCFDP API method. The
// destination file is renamed with a time tag extension and the source file
// is deleted. Calls the MonitorTransactions example function to the monitor
// CFDP transaction using the PopulateCFDPStructArray function.
//
// The "cfdp_destination" example must running prior to starting this example.
//
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
char pathname[256];
char pathname3MB[256];
char destination_pathname3MB[256];
long long eid;
unsigned int transaction_count;
unsigned int timeout_in_sec;
char log_path[256];
char log_filename[256];
// Intialize message mask to print error messages.
message_mask = MSG_CAT_ERROR;
// Set default CFDP config_pathname
strcpy (pathname, "./cfdp_put_example_1_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 CFDP initialization.
RegisterMessage(&PrintTheMessage);
// Create a log file in the home/<username> or Users/<username> directory
// by passing a empty string for directory path. Only log error and
// information messages.
strcpy(log_path,"");
strcpy(log_filename,"cfdp_put_example_1_log_file");
log_filename,
// Add a user message to the log file
LogMessage(MSG_CAT_INFO,"CFDP log.");
// Create a statistics file in the home/<username> or Users/<username> directory
// by passing a empty string for directory path. Only record device statistics.
strcpy(log_path,"");
strcpy(log_filename,"cfdp_put_example_1_statistics_file");
log_filename,
// Initialize CFDP. This funcition creates, connects and configures the
// socket and CFDP library.
if (InitToolkitCfdp(pathname) == SUCCESS)
{
if (GetDisplayMessageMask(&message_mask) != SUCCESS)
{
printf("Error Failed to GetDisplayMessageMask.\n");
}
// Register the CFDPDeviceData callback function after initialization
// since the CFDPDevice does not exist prior to initialization and this
// function would fail.
if (RegisterCFDPDeviceData(&DeviceData) != SUCCESS)
{
printf("Error Failed to RegisterCFDPDeviceData.\n");
}
// Create a 3MB data file.
if (InitExampleDataFiles(pathname3MB,destination_pathname3MB)!=SUCCESS)
{
printf("Error Failed to InitExampleDataFiles.\n");
return 0;
}
printf("\nPut Example 1\n\n");
// Copy the 3MB file to the current directory with the time tag
// appended to the file name. The DeviceData function deletes the
// source file if the source file was successfully transferred.
// Initialize the destination eid
eid = 100;
if (PutComponentCFDP(pathname3MB,
destination_pathname3MB,
{
timeout_in_sec = 30;
transaction_count = 1;
// NOTE: The cfdp config file "support_cfdp_status_requests"
// parameter must be "true" to support transaction status
// monitoring.
if (MonitorTransactions(timeout_in_sec,
transaction_count) != SUCCESS)
{
printf("Error Failed to MonitorTransactions.\n");
return 0;
}
}
else
{
printf("Error Failed 3Mb PutComponentCFDP.\n");
return 0;
}
} // end if (InitToolkitCfdp(pathname) == SUCCESS)
// Sleep 1 second prior to exiting.
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
return 0;
}
CFDP toolkit enumerations.
@ CFDP_CLASS_2
CFDP class 2 service.
Definition: cfdp_shared.h:23
Message codes and structure definition for the Device Service library.
#define DS_SUCCESS
40 DS message code for Success
Definition: ds_shared.h:65
#define DS_FAIL
41 DS message code for Fail
Definition: ds_shared.h:66
@ FALSE_OR_NO
False.
Definition: ds_shared.h:105
#define DS_UNKNOWN
43 DS message code for Unknown
Definition: ds_shared.h:68
#define DS_ABANDON
42 DS message code for Abandon
Definition: ds_shared.h:67
#define DS_CANCEL
39 DS message code for Cancel
Definition: ds_shared.h:64
Structure of parameters needed for CFDP transaction status.
Definition: ds_shared.h:145
char destination_pathname[256]
Destination path and filename.
Definition: ds_shared.h:147
unsigned long cfdp_status
(e.g., DS_PACKET_SENDING, DS_PACKET_RECEIVING, DS_SUSPEND, DS_RESUME, DS_CANCEL, DS_SUCCESS,...
Definition: ds_shared.h:164
unsigned short percent_transferred
Bytes transferred divided by file size. Will reset with packet/PDU retransmissions.
Definition: ds_shared.h:150
long long bytes_transferred
The current number of bytes transmitted or received.
Definition: ds_shared.h:151
char transaction_id[50]
The transaction ID string is a combination of the decimal dotted notation EID (NATIVE) or decimal EID...
Definition: ds_shared.h:149
char source_pathname[256]
Source path and filename.
Definition: ds_shared.h:146
long long file_size
The size of the file being transferred.
Definition: ds_shared.h:152
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
An ANSI C CFDP API.
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION RegisterCFDPDeviceData(void(*function_ptr)(const char *device_key, int packet_length, unsigned char *packet_buffer_ptr))
Register a callback function to receive cfdp_struct status messages for all transactions currently be...
Definition: toolkit_cfdp_api_ansi_c.cpp:7904
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION PopulateCFDPStructArray(unsigned int *number_of_cfdp_structs_ptr, cfdp_struct_type *cfdp_struct_array_ptr)
Populates an array with cfdp structs describing current CFDP transactions. Supports both NATIVE and I...
Definition: toolkit_cfdp_api_ansi_c.cpp:7379
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION InitToolkitCfdp(const char *config_pathname)
Intializes the CFDP library using parameters read from a configuration file. Supports both Native and...
Definition: toolkit_cfdp_api_ansi_c.cpp:98
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION GetDisplayMessageMask(unsigned int *display_mask_ptr)
Populates an unsigned integer with a mask value using the display message parameters in the configura...
Definition: toolkit_cfdp_api_ansi_c.cpp:6597
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION PutComponentCFDP(const char *source_pathname, const char *destination_pathname, long long destination_eid, cfdp_class_of_service_type cfdp_class_of_service)
Initiates a "put" file transfer using the components of a CFDP primitive string. Supports Native CFDP...
Definition: toolkit_cfdp_api_ansi_c.cpp:789
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION StartLoggingCFDPMessages(const char *log_file_path, const char *log_filename, boolean_type log_debug_messages)
Starts logging messages to a file. Supports both NATIVE and ION CFDP.
Definition: toolkit_cfdp_api_ansi_c.cpp:6804
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 LogMessage(enum message_category category, const char *message)
Logs a user message in the log file. The maximum null terminated message size is 512 bytes.
Definition: toolkit_ds_api_ansi_c.cpp:3426
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION StartRecordingStatSnapshot(const char *record_file_path, const char *record_filename, boolean_type record_packet_statistics_flag)
Starts recording a snapshot of the current statistics to a file.
Definition: toolkit_ds_api_ansi_c.cpp:3573
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)
#define DS_ARRAY_SIZE_ERROR
The size of the array is not large enough to hold the requested values.
Definition: toolkit_ds_api_error_codes.h:38
Command codes for TReK.
#define SUCCESS
The function completed successfully.
Definition: trek_error.h:8
#define FAIL
The function failed for an unknown reason.
Definition: trek_error.h:9
The commonly shared macros, structures and functions.
@ MSG_CAT_PROGRESS
Progress message.
Definition: trek_toolkit_common_api_ansi_c.h:62
@ MSG_CAT_INFO
Information message.
Definition: trek_toolkit_common_api_ansi_c.h:60
@ MSG_CAT_ERROR
Error message.
Definition: trek_toolkit_common_api_ansi_c.h:56
int EXPORT_THIS_TREK_TOOLKIT_COMMON_API_FUNCTION TCAACGetHomeDirectory(char *directory_buffer, int buffer_size)
Gets the path to the users home directory.
Definition: trek_toolkit_common_api_ansi_c.cpp:142
const char EXPORT_THIS_TREK_TOOLKIT_COMMON_API_FUNCTION * GetMessageCategoryAsPaddedString(enum message_category input)
Converts an enumerated message category value into its equivalent character string with padded spaces...
Definition: trek_toolkit_common_api_ansi_c.cpp:90