TReK ANSI-C  5.3.3
All ANSI-C APIs
cfdp_filestore_example/main.c
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program creates 1MB, 2MB and 3MB pathnames to 1MB, 2MB and 3MB files
// created by the cfdp_destination example application. Filestore actions,
// initiated by this program, are performed on these files. The program uses
// the Filestore, FilestoreComponent, AddFilestore,
// AddFilestoreComponentRequest, SendAllFilestoreRequests,
// MonitorAllCFDPTransactions and RemoveFilestoreComponentRequest API
// functions.
//
// Main with optional input defining pathname for the CFDP configuration file.
// The default CFDP configuration file pathname is
// "./cfdp_filestore_example_config.txt".
//
// The "cfdp_destination" example must be running on the destination
// platform prior to starting other CFDP examples. Assumes the source and
// and destination platforms are using the same operating system and user
// account. Remote eid/node = 100.
//
// ////////////////////////////////////////////////////////////////////////////
#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;
char home_path[256];
// ////////////////////////////////////////////////////////////////////////////
//
// 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);
}
}
}
// ////////////////////////////////////////////////////////////////////////////
//
// InitExampleDataFilesPathnames creates and returns
// the appropriate source and destination pathnames.
//
// ////////////////////////////////////////////////////////////////////////////
int InitExampleDataFilesPathnames(char *pathname1MB,
char *destination_pathname1MB,
char *pathname2MB,
char *destination_pathname2MB,
char *pathname3MB,
char *destination_pathname3MB)
{
struct timeb timebuffer;
if (TCAACGetHomeDirectory(home_path,sizeof(home_path)) != SUCCESS)
{
return(FAIL);
}
// Create a 1MB path names
sprintf(pathname1MB, "%s/cfdp_destination_data_file_1MB",home_path);
// Create a destination time tag appended file name
ftime(&timebuffer);
sprintf(destination_pathname1MB,"%s_%u",
pathname1MB,
(unsigned int)timebuffer.time);
// Create a 2MB path names
sprintf(pathname2MB, "%s/cfdp_destination_data_file_2MB",home_path);
// Create a destination time tag appended file name
sprintf(destination_pathname2MB,"%s_%u",
pathname2MB,
(unsigned int)timebuffer.time);
// Create a 3MB path names
sprintf(pathname3MB, "%s/cfdp_destination_data_file_3MB",home_path);
// Create a destination time tag appended file name
sprintf(destination_pathname3MB,"%s_%u",
pathname3MB,
(unsigned int)timebuffer.time);
return(SUCCESS);
}
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program creates 1MB, 2MB and 3MB pathnames to 1MB, 2MB and 3MB files
// created by the cfdp_destination example application. Filestore actions,
// initiated by this program, are performed on these files. The program uses
// the Filestore, FilestoreComponent, AddFilestore,
// AddFilestoreComponentRequest, SendAllFilestoreRequests,
// MonitorAllCFDPTransactions and RemoveFilestoreComponentRequest API
// functions.
//
// Main with optional input defining pathname for the CFDP configuration file.
// The default CFDP configuration file pathname is
// "./cfdp_filestore_example_config.txt".
//
// The "cfdp_destination" example must be running on the destination
// platform prior to starting other CFDP examples. Assumes the source and
// and destination platforms are using the same operating system and user
// account. Remote eid/node = 100.
//
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
int return_value;
char pathname[256];
char pathname1MB[256];
char destination_pathname1MB[256];
char pathname2MB[256];
char destination_pathname2MB[256];
char pathname3MB[256];
char destination_pathname3MB[256];
long long eid;
unsigned int transaction_count;
cfdp_struct_type *cfdp_struct_array_ptr = NULL;
unsigned int timeout_in_sec;
unsigned int i;
char log_path[256];
char log_filename[256];
char filestore_pathname[256];
// Intialize message mask to print error messages.
message_mask = MSG_CAT_ERROR;
// Set default CFDP config_pathname
strcpy (pathname, "./cfdp_filestore_example_config.txt");
if (argc == 2)
{
strcpy(pathname,argv[1]);
}
else if (argc > 2)
{
printf("Error Command line contains too many arguments.\n");
return 0;
}
strcpy(log_path,"");
strcpy(log_filename,"cfdp_filestore_example_log_file");
log_filename,
// Register the PrintMessage callback function prior to InitToolkitCfdp
// to process error messages that may be generated during CFDP
// initialization.
RegisterMessage(&PrintTheMessage);
// Initialize CFDP. This funcition creates and connects the CFDP library.
if (InitToolkitCfdp(pathname) == SUCCESS)
{
// Must initialize toolkit CFDP prior to recording CFDP metrics
strcpy(log_path,"");
strcpy(log_filename,"cfdp_filestore_example_metrics_file");
StartRecordingCFDPMetricsSnapshot(log_path,log_filename);
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 1MB, 2MB and 3MB example data file pathnames.
if (InitExampleDataFilesPathnames(pathname1MB,
destination_pathname1MB,
pathname2MB,
destination_pathname2MB,
pathname3MB,
destination_pathname3MB) != SUCCESS)
{
printf("Error Failed to InitExampleDataFiles.\n");
return 0;
}
// Initialize the destination eid node number
eid = 100;
printf("\nCFDP Filestore Example\n\n");
sprintf(filestore_pathname, "%s/test_create_file",home_path);
// Exercise various filestore actions using the API.
// Create a file in the home directory.
if (Filestore(CFDP_CREATE_FILE,filestore_pathname,NULL,eid) != SUCCESS)
{
printf("Error Failed to create file.\n");
return 0;
}
// Delete the newly created 3MB data file in the home directory.
if (FilestoreComponent(CFDP_DELETE_FILE,pathname3MB,NULL,eid,
{
printf("Error Failed to delete file.\n");
return 0;
}
// Add the filestore action to append the 2MB data file to the end of the
// 1MB data file to a list of filestore actions.
if (AddFilestore(CFDP_APPEND_FILE,pathname1MB,pathname2MB,eid) != SUCCESS)
{
printf("Error Failed to append file.\n");
return 0;
}
// Add the filestore action to rename the 1MB data file to a list of filestore
// actions.
if (AddFilestore(CFDP_RENAME_FILE,pathname1MB,destination_pathname3MB,eid) != SUCCESS)
{
printf("Error Failed to rename file.\n");
return 0;
}
// Add the filestore action to delete the 2MB data file to a list of filestore
// actions.
{
printf("Error Failed to add delete file request.\n");
return 0;
}
// Execute the list of filestore actions.
{
printf("Error Failed to SendAllFilestoreRequests.\n");
return 0;
}
// Set the monitor timeout so MonitorAllCFDPTransactions will not hang
// indefinitely waiting for a transaction to complete execution.
timeout_in_sec = 300;
transaction_count = 5;
// Get the final transaction status for the CFDP transactions.
// NOTE: The cfdp config file "support_cfdp_status_requests" parameter
// and the "support_transaction_result_message" must be "true" to
// support transaction status monitoring.
return_value = MonitorAllCFDPTransactions(timeout_in_sec,
&transaction_count,
&cfdp_struct_array_ptr);
if (return_value == SUCCESS || return_value == CFDP_TIMEOUT)
{
// Loop through the array of cfdp structs to determine the final
// status of each CFDP transaction
for (i=0; i<transaction_count;i++)
{
// 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)
{
// Remove the request so subsequent calls to "Filestore" will not
// attempt to send the same filestore action.
cfdp_struct_array_ptr[i].cfdp_filestore_action,
cfdp_struct_array_ptr[i].source_pathname,
cfdp_struct_array_ptr[i].destination_pathname,
cfdp_struct_array_ptr[i].eid);
}
else
{
// Take appropriate action
printf("Info Failed to execute filestore with transaction ID: %s.\n",
cfdp_struct_array_ptr[i].transaction_id);
}
}
// NOTE: Problems may develop when freeing the memory associated
// with the cfdp_struct_array if the CFDP library's compiler or
// run time environment does not match the application code's
// compiler or run time environment. This method is provided to
// avoid those problems. The code, "free(cfdp_struct_array_ptr)"
// will work if the library and application code's compilers
// are the same.
FreeCFDPStructArrayMemoryAlloc(cfdp_struct_array_ptr);
}
else
{
printf("Error Failed to MonitorAllCFDPTransactions.\n");
}
} // end if (InitToolkitCfdp(pathname) == SUCCESS)
// Sleep 1 second prior to exiting.
#ifdef __linux__
sleep(2);
#else
Sleep(2000);
#endif
return 0;
}
CFDP toolkit enumerations.
cfdp_filestore_action_type
CFDP filestore enumerations.
Definition: cfdp_shared.h:28
@ CFDP_APPEND_FILE
CFDP append file.
Definition: cfdp_shared.h:33
@ CFDP_CREATE_FILE
CFDP create file.
Definition: cfdp_shared.h:30
@ CFDP_DELETE_FILE
CFDP delete file.
Definition: cfdp_shared.h:31
@ CFDP_RENAME_FILE
CFDP rename file.
Definition: cfdp_shared.h:32
@ 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
@ FALSE_OR_NO
False.
Definition: ds_shared.h:105
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 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
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 Filestore(cfdp_filestore_action_type filestore_action, const char *first_pathname, const char *second_pathname, long long destination_eid)
Initiates a filestore action using the common components of a filestore primitive string....
Definition: toolkit_cfdp_api_ansi_c.cpp:2864
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION AddFilestoreComponentRequest(cfdp_filestore_action_type filestore_action, const char *first_pathname, const char *second_pathname, long long destination_eid, cfdp_class_of_service_type cfdp_class_of_service)
Adds a filestore request (e.g., CFDP_CREATE_FILE, CFDP_DELETE_FILE ...) using filestore components,...
Definition: toolkit_cfdp_api_ansi_c.cpp:3743
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 RemoveFilestoreComponentRequest(cfdp_filestore_action_type filestore_action, const char *first_pathname, const char *second_pathname, long long destination_eid)
Removes a filestore request, using the common components of a filestore primitive string,...
Definition: toolkit_cfdp_api_ansi_c.cpp:4243
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION MonitorAllCFDPTransactions(unsigned int monitor_timeout, unsigned int *transaction_count_ptr, cfdp_struct_type **cfdp_struct_array_ptr)
Monitors the status of all CFDP transactions and returns when all the transactions have completed or ...
Definition: toolkit_cfdp_api_ansi_c.cpp:7577
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION FilestoreComponent(cfdp_filestore_action_type filestore_action, const char *first_pathname, const char *second_pathname, long long destination_eid, cfdp_class_of_service_type cfdp_class_of_service)
Initiates a filestore action (e.g., CFDP_CREATE_FILE, CFDP_DELETE_FILE ...) using the components of a...
Definition: toolkit_cfdp_api_ansi_c.cpp:3116
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION SendAllRequests()
Initiates all "put" and "get" file requests and all filestore and message requests in the lists of CF...
Definition: toolkit_cfdp_api_ansi_c.cpp:5497
int EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION AddFilestore(cfdp_filestore_action_type filestore_action, const char *first_pathname, const char *second_pathname, long long destination_eid)
Adds a filestore request, using the common components of a filestore primitive string,...
Definition: toolkit_cfdp_api_ansi_c.cpp:3516
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 StartRecordingCFDPMetricsSnapshot(const char *record_file_path, const char *record_filename)
Starts recording a snapshot of the current CFDP metrics to a file. Supports both NATIVE and ION CFDP.
Definition: toolkit_cfdp_api_ansi_c.cpp:7026
void EXPORT_THIS_TOOLKIT_CFDP_C_FUNCTION FreeCFDPStructArrayMemoryAlloc(cfdp_struct_type *cfdp_struct_array_ptr)
Frees the memory associated with the cfdp_struct_array that was returned by MonitorAllCFDPTransaction...
Definition: toolkit_cfdp_api_ansi_c.cpp:7753
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
Error codes for the CFDP API (starts at 51001)
#define CFDP_TIMEOUT
The timer associated with the request expired.
Definition: toolkit_cfdp_api_error_codes.h:13
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 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)
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_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