TReK ANSI-C  5.3.3
All ANSI-C APIs
network_bp_destination/main.c
// ////////////////////////////////////////////////////////////////////
//
// Sample program receives and sends four packets using bundle protocol
// via ION and logs info and error messages. Program uses
// RegisterPrintMessage, StartLoggingMessages, CreateBPDevice,
// RegisterReceivePacket, and SendPacket API functions.
//
// This program must be running on the destination platform prior to
// starting the network BP polling or network BP source examples.
//
// ION must be configured with BP service numbers 3
// and 4. Destination eid/node = 2.
//
// ////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "ds_shared.h"
#include "bp_shared.h"
#include "trek_error.h"
#ifdef __linux__
#include <unistd.h>
#else
#include <windows.h>
#endif
char bp_device_key3[50];
// ////////////////////////////////////////////////////////////////////
//
// PrintTheMessage controls how messages are processed and displayed to
// the user.
//
// ////////////////////////////////////////////////////////////////////
void PrintTheMessage(message_struct_type *mess_struct_ptr)
{
if (mess_struct_ptr->category == MSG_CAT_INFO ||
mess_struct_ptr->category == MSG_CAT_INFO_ALERT ||
mess_struct_ptr->category == MSG_CAT_ERROR ||
mess_struct_ptr->category == MSG_CAT_ERROR_ALERT)
{
printf("%s\t%s\n",GetMessageCategoryAsString(mess_struct_ptr->category),
mess_struct_ptr->message);
}
}
// ////////////////////////////////////////////////////////////////////
//
// ReceivePacket's key parameter is set to the packet's origin or
// source endpoint id string (e.g.,<source node number>.<source service
// number>). The packet_buffer_ptr parameter points to the newly
// received packet.
//
// ////////////////////////////////////////////////////////////////////
void ReceivePacket (const char *key,
int packet_length,
unsigned char *packet_buffer_ptr)
{
char *arg[2];
long long source_node_number;
unsigned int source_service_number;
// Print the data zone packets.
printf("Data\t%s\n",(char *)packet_buffer_ptr + 5);
// Send the packet back to its source by retrieving the
// packet's source node number and source service number from the
// key string that is returned with the packet.
// NOTE: The key string is formatted as follows:
// <source node number>.<source service number> (e.g., 1.64)
arg[0] = strtok((char *)key,".");
if (arg[0] != NULL)
{
arg[1] = strtok(NULL,"."); // NULL allows function to continue scanning
// where a previous call to function ended
if (arg[1] != NULL)
{
#ifdef _WIN32
source_node_number = _strtoi64(arg[0],NULL,0);
#else
source_node_number = strtoll(arg[0],NULL,0);
#endif
source_service_number = (unsigned int)atof(arg[1]);
SendPacketFromBPDevice (bp_device_key3,
packet_length,packet_buffer_ptr,
source_node_number,
source_service_number);
}
}
}
// ////////////////////////////////////////////////////////////////////
//
// Sample program receives and sends four packets using bundle protocol
// via ION and logs info and error messages. Program uses
// RegisterPrintMessage, StartLoggingMessages, CreateBPDevice,
// RegisterReceivePacket, and SendPacket API functions.
//
// This program must be running on the destination platform prior to
// starting the network BP polling or network BP source examples.
//
// ION must be configured with BP service numbers 3
// and 4. Destination eid/node = 2.
//
// ////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
unsigned int receive_service_number;
unsigned int lifespan;
bp_class_of_service_type cos; // BPD_BULK_PRIORITY, BPD_STD_PRIORITY or BPD_EXPEDITED_PRIORITY
unsigned int ordinal; // Values from 0-254. Only associated with EXPEDITED_PRIORITY class_of_service.
bp_transmission_mode_type mode; // BPD_BEST_EFFORT, BPD_ASSURED or BPD_ASSURED_WITH_CUSTODY_TRANSFER
bp_criticality_type criticality; // BPD_NOT_CRITICAL or BPD_CRITICAL
unsigned int key_buffer_size;
char log_path[256];
char log_filename[256];
unsigned short exit_flag = 0;
char arg1[50];
char bp_device_key4[50];
char bp_device_key5[50];
// Register the PrintMessage callback function prior to InitToolkitCfdp
// to process error messages that may be generated during initialization.
RegisterMessage(&PrintTheMessage);
printf("\nNetwork BP Destination\n\n");
// 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,"network_bp_destination_log_file");
log_filename,
// Create a BP receive device
receive_service_number = 3;
lifespan = 86400;
ordinal = 0;
mode = BPD_ASSURED;
criticality = BPD_NOT_CRITICAL;
key_buffer_size = sizeof(bp_device_key3);
if (CreateBPDevice(receive_service_number,
lifespan,cos,ordinal,mode,criticality,&key_buffer_size,
bp_device_key3) != SUCCESS)
{
return 0;
}
// Associate the ReceivePacket callback function with the BP
// device so all packets received by the device will
// be processed by ReceivePacket callback function.
if (RegisterReceivePacket(bp_device_key3,&ReceivePacket) != SUCCESS)
{
return 0;
}
// Create and register a second BP receive device
receive_service_number = 4;
if (CreateBPDevice(receive_service_number,
lifespan,cos,ordinal,mode,criticality,&key_buffer_size,
bp_device_key4) != SUCCESS)
{
return 0;
}
if (RegisterReceivePacket(bp_device_key4,&ReceivePacket) != SUCCESS)
{
return 0;
}
// Create and register a second BP receive device
receive_service_number = 5;
if (CreateBPDevice(receive_service_number,
lifespan, cos, ordinal, mode, criticality, &key_buffer_size,
bp_device_key5) != SUCCESS)
{
return 0;
}
if (RegisterReceivePacket(bp_device_key5, &ReceivePacket) != SUCCESS)
{
return 0;
}
printf("To exit application enter: \"e\" or \"q\" or \"exit\" or \"quit\".\n\n");
while (exit_flag == 0)
{
fgets(arg1, 50, stdin);
arg1[strlen(arg1)-1] = '\0';
if (strcmp(arg1,"exit") == 0 || strcmp(arg1,"quit") == 0 ||
strcmp(arg1,"e") == 0 || strcmp(arg1,"q") == 0)
{
exit_flag = 1;
}
else
{
printf("\nTo exit application enter: \"e\" or \"q\" or \"exit\" or \"quit\".\n\n");
}
}
// Close the log file. A timetag is appended to the log file name.
return 0;
}
Enumerated types for the BP device library.
bp_class_of_service_type
Definition: bp_shared.h:44
@ BPD_STD_PRIORITY
Standard priority BP class of service.
Definition: bp_shared.h:47
bp_transmission_mode_type
Definition: bp_shared.h:29
@ BPD_ASSURED
Assured BP transmission mode.
Definition: bp_shared.h:32
bp_criticality_type
Definition: bp_shared.h:37
@ BPD_NOT_CRITICAL
Not critical BP criticality.
Definition: bp_shared.h:39
Message codes and structure definition for the Device Service library.
@ FALSE_OR_NO
False.
Definition: ds_shared.h:105
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 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 SendPacketFromBPDevice(const char *device_key, int packet_length, unsigned char *packet_buffer_ptr, long long destination_node_number, unsigned int destination_service_number)
Sends a packet from a bundle protocol device to the destination node.
Definition: toolkit_ds_api_ansi_c.cpp:2010
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION RegisterReceivePacket(const char *device_key, void(*function_ptr)(const char *key, int packet_length, unsigned char *packet_buffer_ptr))
Register a callback function to receive packets from a device.
Definition: toolkit_ds_api_ansi_c.cpp:4013
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
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION CreateBPDevice(unsigned int source_service_number, unsigned int lifespan, bp_class_of_service_type cos, unsigned int ordinal, bp_transmission_mode_type mode, bp_criticality_type criticality, unsigned int *device_key_buffer_size_ptr, char *device_key)
Creates a Bundle Protocol (BP) device that attaches to ION's BP library. ION's BP application must be...
Definition: toolkit_ds_api_ansi_c.cpp:688
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION StartLoggingMessages(const char *log_file_path, const char *log_filename, boolean_type log_debug_messages)
Starts logging messages to a file.
Definition: toolkit_ds_api_ansi_c.cpp:3339
Error codes for the DS API (starts at 50001)
Command codes for TReK.
#define SUCCESS
The function completed successfully.
Definition: trek_error.h:8
The commonly shared macros, structures and functions.
@ MSG_CAT_INFO_ALERT
Information alert message (supports information alert pop up dialog box)
Definition: trek_toolkit_common_api_ansi_c.h:61
@ 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
@ MSG_CAT_ERROR_ALERT
Error alert message (supports error alert pop up dialog box)
Definition: trek_toolkit_common_api_ansi_c.h:57
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