TReK ANSI-C  5.3.3
All ANSI-C APIs
network_bp_polling/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, ReceivePacketFromDevice,
// 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 number 3. The destination
// platform's ION configuration must include service number 3 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>
#include <pthread.h>
#else
#include <windows.h>
#include <process.h>
#endif
char bp_device_key[50];
boolean_type poll_thread_exit;
// ////////////////////////////////////////////////////////////////////////////
//
// 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);
}
}
// ////////////////////////////////////////////////////////////////////////////
//
// 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);
}
// ////////////////////////////////////////////////////////////////////////////
//
// PollThread repeatedly calls the ReceivePacketFromDevice function to recieve
// packets.
//
// ////////////////////////////////////////////////////////////////////////////
#ifdef WIN32
unsigned __stdcall PollThread(void *arg)
#else
void *PollThread(void *arg)
#endif
{
unsigned int receive_timeout; // in milli seconds
unsigned int packet_buffer_size;
int packet_length;
unsigned char packet_buffer[50];
unsigned int message_code;
void *return_val = NULL;
int return_code;
// Continue to poll the BP device for incoming packets until main thread
// sets the poll_thread_exit flag
receive_timeout = 100;
packet_buffer_size = 50;
while (poll_thread_exit != TRUE_OR_YES)
{
// Wait up to 100 millisec to receive additional packet.
if ( (return_code = ReceivePacketFromDevice(bp_device_key,
receive_timeout,
&packet_buffer_size,
&packet_length,
packet_buffer,
&message_code)) == SUCCESS)
{
printf("Data\t%s\n",(char *)packet_buffer + 5);
}
//if (return_code == DS_TIMEOUT)
// printf("Info\tDS_TIMEOUT.\n");
}
printf("Info\tExiting PollThread.\n");
#ifdef WIN32
return SUCCESS;
#else
return(return_val);
#endif
}
// ////////////////////////////////////////////////////////////////////////////
//
// Sample program sends and receives four packets using bundle protocol via ION
// and logs info and error messages. Program uses RegisterPrintMessage,
// StartLoggingMessages, CreateBPDevice, ReceivePacketFromDevice,
// 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 number 3. The destination
// platform's ION configuration must include service number 3 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
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;
#ifdef WIN32
HANDLE d_thread_handle;
unsigned d_thread_id;
#else
pthread_t d_thread_id;
#endif
poll_thread_exit = FALSE_OR_NO;
// 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 Polling\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_polling_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(bp_device_key);
if (CreateBPDevice(source_service_number,
lifespan,cos,ordinal,mode,criticality,&key_buffer_size,
bp_device_key) != SUCCESS)
{
return 0;
}
// Create a thread that receives the packets sent by the
// BP Destination application.
#ifdef __linux__
if (pthread_create(&d_thread_id, NULL, PollThread,NULL) != 0)
{
printf("Error\tFailed to create UdpThread.\n");
return 0;
}
#else
// Create thread
d_thread_handle = (HANDLE)_beginthreadex(NULL,0,&PollThread,NULL,0,
&d_thread_id);
if (d_thread_handle == NULL)
{
printf("Error\tFailed to create UdpThread.\n");
return 0;
}
#endif
// Sleep 1 second to allow thread creation to complete
#ifdef __linux__
sleep(1);
#else
Sleep(1000);
#endif
// Send the four data packets to the destination via the BP device.
destination_node_number = 2;
destination_service_number = 3;
if (SendPacketFromBPDevice (bp_device_key,
length1,packet1,
destination_node_number,
destination_service_number) != SUCCESS)
{
printf("Error\tFailed to send first packet.\n");
return 0;
}
// Send second packet
if (SendPacketFromBPDevice (bp_device_key,
length2,
packet2,
destination_node_number,
destination_service_number) != SUCCESS)
{
printf("Error\tFailed to send second packet.\n");
return 0;
}
// Send third packet
if (SendPacketFromBPDevice (bp_device_key,
length3,
packet3,
destination_node_number,
destination_service_number) != SUCCESS)
{
printf("Error\tFailed to send third packet.\n");
return 0;
}
// Send fourth packet
if (SendPacketFromBPDevice (bp_device_key,
length4,
packet4,
destination_node_number,
destination_service_number) != SUCCESS)
{
printf("Error\tFailed to send fourth packet.\n");
return 0;
}
// Allow time to print and log messages.
#ifdef __linux__
sleep(2);
#else
Sleep(2000);
#endif
// Exit UdpThread
poll_thread_exit = TRUE_OR_YES;
// 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.
boolean_type
Boolean enumeration.
Definition: ds_shared.h:104
@ FALSE_OR_NO
False.
Definition: ds_shared.h:105
@ TRUE_OR_YES
True.
Definition: ds_shared.h:106
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 ReceivePacketFromDevice(const char *device_key, unsigned int receive_timeout, unsigned int *packet_buffer_size_ptr, int *packet_length_ptr, unsigned char *packet_buffer_ptr, unsigned int *message_code_ptr)
Receive a packet from a device within a specified time period.
Definition: toolkit_ds_api_ansi_c.cpp:1435
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 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