TReK ANSI-C  5.3.3
All ANSI-C APIs
network_bp_source/main.c
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program sends and receives four packets using bundle protocol via ION
// and logs info and error messages. Program uses RegisterPrintMessage,
// StartLoggingMessages, CreateBPDevice, RegisterReceivePacket,
// and SendPacketFromBPDevice API functions.
//
// The "network_bp_destination" example must be running on the destination
// platform prior to starting this example.
//
// ION must be configured with service numbers 3, 4, and 5. The destination
// platform's ION configuration must include service numbers 3, 4 and 5 and
// 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
// ////////////////////////////////////////////////////////////////////////////
//
// 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 (ipn:<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)
{
// Print the data zone packets.
printf("Data\t%s\n",(char *)packet_buffer_ptr + 5);
}
// ////////////////////////////////////////////////////////////////////////////
//
// CreateDataPackets creates the data packets with a three byte header.
// The first three header bytes contains a sync hex pattern = fafbfc.
// The fourth and fifth header bytes contain the overall packet length in big
// endian byte order.
//
// ////////////////////////////////////////////////////////////////////////////
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 char sync_pattern[3];
unsigned short big_endian_order = 1;
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 a five byte header for all the packets. The header is populated
// with the sync bytes and a big endian two byte overall packet length.
// Populate packet data zone
strcpy((char *)packet1_ptr+5,"Mary had a little lamb");
strcpy((char *)packet2_ptr+5,"Its feet were black as soot");
strcpy((char *)packet3_ptr+5,"And into Mary's bread and jam ");
strcpy((char *)packet4_ptr+5,"Its sooty foot it put");
// Get packet length including 5 byte header and null terminator.
*length1_ptr = strlen((char *)packet1_ptr+5) + 6;
*length2_ptr = strlen((char *)packet2_ptr+5) + 6;
*length3_ptr = strlen((char *)packet3_ptr+5) + 6;
*length4_ptr = strlen((char *)packet4_ptr+5) + 6;
// Populate headers.
// Populate packet length field.
if (big_endian_order == 1)
{
memmove(packet1_ptr+3,length1_ptr,2);
memmove(packet2_ptr+3,length2_ptr,2);
memmove(packet3_ptr+3,length3_ptr,2);
memmove(packet4_ptr+3,length4_ptr,2);
}
else
{
memmove(packet1_ptr+4,length1_ptr,1);
memmove(packet2_ptr+4,length2_ptr,1);
memmove(packet3_ptr+4,length3_ptr,1);
memmove(packet4_ptr+4,length4_ptr,1);
}
// Populate sync field.
sync_pattern[0] = 0xfa;
sync_pattern[1] = 0xfb;
sync_pattern[2] = 0xfc;
memcpy(packet1_ptr,sync_pattern,3);
memcpy(packet2_ptr,sync_pattern,3);
memcpy(packet3_ptr,sync_pattern,3);
memcpy(packet4_ptr,sync_pattern,3);
}
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program sends and receives four packets using bundle protocol via ION
// and logs info and error messages. Program uses RegisterPrintMessage,
// StartLoggingMessages, CreateBPDevice, RegisterReceivePacket,
// and SendPacketFromBPDevice API functions.
//
// The "network_bp_destination" example must be running on the destination
// platform prior to starting this example.
//
// ION must be configured with service numbers 3, 4, and 5. The destination
// platform's ION configuration must include service numbers 3, 4 and 5 and
// eid/node = 2.
//
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
unsigned int source_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
char source_device_key3[50];
char source_device_key4[50];
char source_device_key5[50];
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;
unsigned int key_buffer_size;
char log_path[256];
char log_filename[256];
long long destination_node_number;
unsigned int destination_service_number;
// Create data packets.
CreateDataPackets(packet1,
packet2,
packet3,
packet4,
&length1,
&length2,
&length3,
&length4);
// Register the PrintMessage callback function prior to InitToolkitCfdp
// to process error messages that may be generated during initialization.
RegisterMessage(&PrintTheMessage);
printf("\nNetwork BP Source\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_source_log_file");
log_filename,
// Create a BP source device
source_service_number = 3;
lifespan = 86400;
ordinal = 0;
mode = BPD_ASSURED;
criticality = BPD_NOT_CRITICAL;
key_buffer_size = sizeof(source_device_key3);
if (CreateBPDevice(source_service_number,lifespan,cos,ordinal,mode,criticality,&key_buffer_size,source_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(source_device_key3,
&ReceivePacket) != SUCCESS)
{
return 0;
}
// Create and register a second BP source device
source_service_number = 4;
if (CreateBPDevice(source_service_number,lifespan,cos,ordinal,mode,criticality,&key_buffer_size,source_device_key4) != SUCCESS)
{
return 0;
}
if (RegisterReceivePacket(source_device_key4,
&ReceivePacket) != SUCCESS)
{
return 0;
}
// Create and register a third BP source device
source_service_number = 5;
if (CreateBPDevice(source_service_number,lifespan,cos,ordinal,mode,criticality,&key_buffer_size,source_device_key5) != SUCCESS)
{
return 0;
}
if (RegisterReceivePacket(source_device_key5,
&ReceivePacket) != SUCCESS)
{
return 0;
}
// BP service 3 device.
destination_node_number = 2;
destination_service_number = 3;
if (SendPacketFromBPDevice(source_device_key3, //SendPacketFromBPDevice (source_device_key3,
length1,packet1,
destination_node_number,
destination_service_number) == SUCCESS)
{
if (SendPacketFromBPDevice(source_device_key3, //SendPacketFromBPDevice (source_device_key3,
length2,
packet2,
destination_node_number,
destination_service_number) == SUCCESS)
{
if (SendPacketFromBPDevice(source_device_key3, //SendPacketFromBPDevice (source_device_key3,
length3,
packet3,
destination_node_number,
destination_service_number) == SUCCESS)
{
SendPacketFromBPDevice(source_device_key3, //SendPacketFromBPDevice (source_device_key3,
length4,
packet4,
destination_node_number,
destination_service_number);
}
}
}
// Send and receive the four data packets to the destination via the
// BP service 4 device.
destination_node_number = 2;
destination_service_number = 4;
if (SendPacketFromBPDevice (source_device_key4,
length1,packet1,
destination_node_number,
destination_service_number) == SUCCESS)
{
if (SendPacketFromBPDevice (source_device_key4,
length2,
packet2,
destination_node_number,
destination_service_number) == SUCCESS)
{
if (SendPacketFromBPDevice (source_device_key4,
length3,
packet3,
destination_node_number,
destination_service_number) == SUCCESS)
{
SendPacketFromBPDevice (source_device_key4,
length4,
packet4,
destination_node_number,
destination_service_number);
}
}
}
// Send and receive the four data packets to the destination via the
// BP service 5 device.
destination_node_number = 2;
destination_service_number = 5;
if (SendPacketFromBPDevice (source_device_key5,
length1,packet1,
destination_node_number,
destination_service_number) == SUCCESS)
{
if (SendPacketFromBPDevice (source_device_key5,
length2,
packet2,
destination_node_number,
destination_service_number) == SUCCESS)
{
if (SendPacketFromBPDevice (source_device_key5,
length3,
packet3,
destination_node_number,
destination_service_number) == SUCCESS)
{
SendPacketFromBPDevice (source_device_key5,
length4,
packet4,
destination_node_number,
destination_service_number);
}
}
}
// Allow time to print and log messages.
#ifdef __linux__
sleep(4);
#else
Sleep(4000);
#endif
// 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