Sfoglia il codice sorgente

Code beautifications

CapXilinx 9 anni fa
parent
commit
03431b157c

+ 38 - 32
source/src/cip/cipassembly.c

@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Copyright (c) 2009, Rockwell Automation, Inc.
- * All rights reserved. 
+ * All rights reserved.
  *
  ******************************************************************************/
 
@@ -19,8 +19,9 @@
  *  Currently only supports Attribute 3 (CIP_BYTE_ARRAY) of an Assembly
  */
 EipStatus SetAssemblyAttributeSingle(
-    CipInstance *const instance, CipMessageRouterRequest *const message_router_request,
-    CipMessageRouterResponse *const message_router_response);
+  CipInstance *const instance,
+  CipMessageRouterRequest *const message_router_request,
+  CipMessageRouterResponse *const message_router_response);
 
 /** @brief Constructor for the assembly object class
  *
@@ -30,15 +31,15 @@ EipStatus SetAssemblyAttributeSingle(
 CipClass *CreateAssemblyClass(void) {
   /* create the CIP Assembly object with zero instances */
   CipClass *assembly_class = CreateCipClass(kCipAssemblyClassCode, 0, /* # class attributes*/
-                                  0, /* 0 as the assembly object should not have a get_attribute_all service*/
-                                  0, /* # class services*/
-                                  2, /* # instance attributes*/
-                                  0, /* 0 as the assembly object should not have a get_attribute_all service*/
-                                  1, /* # instance services*/
-                                  0, /* # instances*/
-                                  "assembly", /* name */
-                                  2 /* Revision, according to the CIP spec currently this has to be 2 */
-                                  );
+                                            0, /* 0 as the assembly object should not have a get_attribute_all service*/
+                                            0, /* # class services*/
+                                            2, /* # instance attributes*/
+                                            0, /* 0 as the assembly object should not have a get_attribute_all service*/
+                                            1, /* # instance services*/
+                                            0, /* # instances*/
+                                            "assembly", /* name */
+                                            2 /* Revision, according to the CIP spec currently this has to be 2 */
+                                            );
   if (NULL != assembly_class) {
     InsertService(assembly_class, kSetAttributeSingle,
                   &SetAssemblyAttributeSingle, "SetAssemblyAttributeSingle");
@@ -51,7 +52,7 @@ CipClass *CreateAssemblyClass(void) {
  *
  */
 EipStatus CipAssemblyInitialize(void) {
-  return (NULL != CreateAssemblyClass()) ? kEipStatusOk : kEipStatusError;
+  return ( NULL != CreateAssemblyClass() ) ? kEipStatusOk : kEipStatusError;
 }
 
 void ShutdownAssemblies(void) {
@@ -69,18 +70,21 @@ void ShutdownAssemblies(void) {
   }
 }
 
-CipInstance *CreateAssemblyObject(const EipUint32 instance_id, EipByte *const data,
+CipInstance *CreateAssemblyObject(const EipUint32 instance_id,
+                                  EipByte *const data,
                                   const EipUint16 data_length) {
   CipClass *assembly_class = NULL;
-  if (NULL == (assembly_class = GetCipClass(kCipAssemblyClassCode))) {
-    if (NULL == (assembly_class = CreateAssemblyClass())) {
+  if ( NULL == ( assembly_class = GetCipClass(kCipAssemblyClassCode) ) ) {
+    if ( NULL == ( assembly_class = CreateAssemblyClass() ) ) {
       return NULL;
     }
   }
 
   CipInstance *const instance = AddCIPInstance(assembly_class, instance_id); /* add instances (always succeeds (or asserts))*/
 
-  CipByteArray *const assembly_byte_array = (CipByteArray *) CipCalloc(1, sizeof(CipByteArray));
+  CipByteArray *const assembly_byte_array = (CipByteArray *) CipCalloc( 1,
+                                                                        sizeof(
+                                                                          CipByteArray) );
   if (assembly_byte_array == NULL) {
     return NULL; /*TODO remove assembly instance in case of error*/
   }
@@ -101,7 +105,8 @@ EipStatus NotifyAssemblyConnectedDataReceived(CipInstance *const instance,
                                               const EipUint16 data_length) {
   /* empty path (path size = 0) need to be checked and taken care of in future */
   /* copy received data to Attribute 3 */
-  CipByteArray *assembly_byte_array = (CipByteArray *) instance->attributes->data;
+  CipByteArray *assembly_byte_array =
+    (CipByteArray *) instance->attributes->data;
   if (assembly_byte_array->length != data_length) {
     OPENER_TRACE_ERR("wrong amount of data arrived for assembly object\n");
     return kEipStatusError; /*TODO question should we notify the application that wrong data has been received???*/
@@ -114,8 +119,9 @@ EipStatus NotifyAssemblyConnectedDataReceived(CipInstance *const instance,
 }
 
 EipStatus SetAssemblyAttributeSingle(
-    CipInstance *const instance, CipMessageRouterRequest *const message_router_request,
-    CipMessageRouterResponse *const message_router_response) {
+  CipInstance *const instance,
+  CipMessageRouterRequest *const message_router_request,
+  CipMessageRouterResponse *const message_router_response) {
   OPENER_TRACE_INFO(" setAttribute %d\n",
                     message_router_request->request_path.attribute_number);
 
@@ -123,32 +129,32 @@ EipStatus SetAssemblyAttributeSingle(
 
   message_router_response->data_length = 0;
   message_router_response->reply_service = (0x80
-      | message_router_request->service);
+                                            | message_router_request->service);
   message_router_response->general_status = kCipErrorAttributeNotSupported;
   message_router_response->size_of_additional_status = 0;
 
   CipAttributeStruct *attribute = GetCipAttribute(
-      instance, message_router_request->request_path.attribute_number);
+    instance, message_router_request->request_path.attribute_number);
 
-  if ((attribute != NULL)
-      && (3 == message_router_request->request_path.attribute_number)) {
+  if ( (attribute != NULL)
+       && (3 == message_router_request->request_path.attribute_number) ) {
     if (attribute->data != NULL) {
-      CipByteArray *data = (CipByteArray*) attribute->data;
+      CipByteArray *data = (CipByteArray *) attribute->data;
 
       /* TODO: check for ATTRIBUTE_SET/GETABLE MASK */
-      if (true == IsConnectedOutputAssembly(instance->instance_number)) {
+      if ( true == IsConnectedOutputAssembly(instance->instance_number) ) {
         OPENER_TRACE_WARN(
-            "Assembly AssemblyAttributeSingle: received data for connected output assembly\n\r");
+          "Assembly AssemblyAttributeSingle: received data for connected output assembly\n\r");
         message_router_response->general_status = kCipErrorAttributeNotSetable;
       } else {
         if (message_router_request->data_length < data->length) {
           OPENER_TRACE_INFO(
-              "Assembly setAssemblyAttributeSingle: not enough data received.\r\n");
+            "Assembly setAssemblyAttributeSingle: not enough data received.\r\n");
           message_router_response->general_status = kCipErrorNotEnoughData;
         } else {
           if (message_router_request->data_length > data->length) {
             OPENER_TRACE_INFO(
-                "Assembly setAssemblyAttributeSingle: too much data received.\r\n");
+              "Assembly setAssemblyAttributeSingle: too much data received.\r\n");
             message_router_response->general_status = kCipErrorTooMuchData;
           } else {
             memcpy(data->data, router_request_data, data->length);
@@ -163,7 +169,7 @@ EipStatus SetAssemblyAttributeSingle(
                * data was not ok.
                */
               message_router_response->general_status =
-                  kCipErrorInvalidAttributeValue;
+                kCipErrorInvalidAttributeValue;
             } else {
               message_router_response->general_status = kCipErrorSuccess;
             }
@@ -176,8 +182,8 @@ EipStatus SetAssemblyAttributeSingle(
     }
   }
 
-  if ((attribute != NULL)
-      && (4 == message_router_request->request_path.attribute_number)) {
+  if ( (attribute != NULL)
+       && (4 == message_router_request->request_path.attribute_number) ) {
     message_router_response->general_status = kCipErrorAttributeNotSetable;
   }
 

+ 5 - 5
source/src/cip/cipassembly.h

@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Copyright (c) 2009, Rockwell Automation, Inc.
- * All rights reserved. 
+ * All rights reserved.
  *
  ******************************************************************************/
 #ifndef OPENER_CIPASSEMBLY_H_
@@ -15,7 +15,7 @@ static const int kCipAssemblyClassCode = 0x04;
 /* public functions */
 
 /** @brief Setup the Assembly object
- * 
+ *
  * Creates the Assembly Class with zero instances and sets up all services.
  *
  * @return Returns kEipStatusOk if assembly object was successfully created, otherwise kEipStatusError
@@ -32,14 +32,14 @@ EipStatus CipAssemblyInitialize(void);
 void ShutdownAssemblies(void);
 
 /** @brief notify an Assembly object that data has been received for it.
- * 
+ *
  *  The data will be copied into the assembly objects attribute 3 and
  *  the application will be informed with the IApp_after_assembly_data_received function.
- *  
+ *
  *  @param instance the assembly object instance for which the data was received
  *  @param data pointer to the data received
  *  @param data_length number of bytes received
- *  @return 
+ *  @return
  *     - kEipStatusOk the received data was okay
  *     - kEipStatusError the received data was wrong
  */

+ 1 - 1
source/src/cip/cipcommon.h

@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Copyright (c) 2009, Rockwell Automation, Inc.
- * All rights reserved. 
+ * All rights reserved.
  *
  ******************************************************************************/
 #ifndef OPENER_CIPCOMMON_H_

+ 55 - 47
source/src/cip/ciptcpipinterface.c

@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Copyright (c) 2009, Rockwell Automation, Inc.
- * All rights reserved. 
+ * All rights reserved.
  *
  ******************************************************************************/
 #include <string.h>
@@ -20,19 +20,20 @@ CipDword configuration_capability_ = 0x04 | 0x20; /**< #2  This is a default val
 CipDword configuration_control_ = 0; /**< #3  This is a TCP/IP object attribute. For now it is always zero and is not used for anything. */
 CipEpath physical_link_object_ = /**< #4 */
 { 2, /**< EIP_UINT16 (UINT) PathSize in 16 Bit chunks */
-CIP_ETHERNETLINK_CLASS_CODE, /**< EIP_UINT16 ClassID */
-1, /**< EIP_UINT16 InstanceNr*/
-0 /**< EIP_UINT16 AttributNr (not used as this is the EPATH the EthernetLink object) */
+  CIP_ETHERNETLINK_CLASS_CODE, /**< EIP_UINT16 ClassID */
+  1, /**< EIP_UINT16 InstanceNr*/
+  0 /**< EIP_UINT16 AttributNr (not used as this is the EPATH the EthernetLink object) */
 };
 
 CipTcpIpNetworkInterfaceConfiguration interface_configuration_ = /**< #5 IP, network mask, gateway, name server 1 & 2, domain name*/
 { 0, /* default IP address */
-0, /* NetworkMask */
-0, /* Gateway */
-0, /* NameServer */
-0, /* NameServer2 */
-{ /* DomainName */
-0, NULL } };
+  0, /* NetworkMask */
+  0, /* Gateway */
+  0, /* NameServer */
+  0, /* NameServer2 */
+  { /* DomainName */
+    0, NULL
+  } };
 
 CipString hostname_ = /**< #6 Hostname*/
 { 0, NULL };
@@ -48,19 +49,21 @@ EipUint8 g_time_to_live_value = 1;
  * allocation algorithm
  */
 MulticastAddressConfiguration g_multicast_configuration = { 0, /* us the default allocation algorithm */
-0, /* reserved */
-1, /* we currently use only one multicast address */
-0 /* the multicast address will be allocated on ip address configuration */
+                                                            0, /* reserved */
+                                                            1, /* we currently use only one multicast address */
+                                                            0 /* the multicast address will be allocated on ip address configuration */
 };
 
 /************** Functions ****************************************/
 EipStatus GetAttributeSingleTcpIpInterface(
-    CipInstance *instance, CipMessageRouterRequest *message_router_request,
-    CipMessageRouterResponse *message_router_response);
+  CipInstance *instance,
+  CipMessageRouterRequest *message_router_request,
+  CipMessageRouterResponse *message_router_response);
 
 EipStatus GetAttributeAllTcpIpInterface(
-    CipInstance *instance, CipMessageRouterRequest *message_router_request,
-    CipMessageRouterResponse *message_router_response);
+  CipInstance *instance,
+  CipMessageRouterRequest *message_router_request,
+  CipMessageRouterResponse *message_router_response);
 
 EipStatus ConfigureNetworkInterface(const char *const ip_address,
                                     const char *const subnet_mask,
@@ -72,12 +75,12 @@ EipStatus ConfigureNetworkInterface(const char *const ip_address,
 
   /* calculate the CIP multicast address. The multicast address is calculated, not input*/
   EipUint32 host_id = ntohl(interface_configuration_.ip_address)
-      & ~ntohl(interface_configuration_.network_mask); /* see CIP spec 3-5.3 for multicast address algorithm*/
+                      & ~ntohl(interface_configuration_.network_mask); /* see CIP spec 3-5.3 for multicast address algorithm*/
   host_id -= 1;
   host_id &= 0x3ff;
 
   g_multicast_configuration.starting_multicast_address = htonl(
-      ntohl(inet_addr("239.192.1.0")) + (host_id << 5));
+    ntohl( inet_addr("239.192.1.0") ) + (host_id << 5) );
 
   return kEipStatusOk;
 }
@@ -92,8 +95,9 @@ void ConfigureDomainName(const char *const restrict domain_name) {
   interface_configuration_.domain_name.length = strlen(domain_name);
   if (interface_configuration_.domain_name.length) {
     interface_configuration_.domain_name.string = (EipByte *) CipCalloc(
-        interface_configuration_.domain_name.length + 1, sizeof(EipInt8));
-    strcpy((char *)(interface_configuration_.domain_name.string), domain_name);
+      interface_configuration_.domain_name.length + 1, sizeof(EipInt8) );
+    strcpy( (char *)(interface_configuration_.domain_name.string),
+            domain_name );
   } else {
     interface_configuration_.domain_name.string = NULL;
   }
@@ -108,19 +112,20 @@ void ConfigureHostName(const char *const restrict hostname) {
   }
   hostname_.length = strlen(hostname);
   if (hostname_.length) {
-    hostname_.string = (EipByte *) CipCalloc(hostname_.length + 1,
-                                             sizeof(EipByte));
-    strcpy((char *)(hostname_.string), hostname);
+    hostname_.string = (EipByte *) CipCalloc( hostname_.length + 1,
+                                              sizeof(EipByte) );
+    strcpy( (char *)(hostname_.string), hostname );
   } else {
     hostname_.string = NULL;
   }
 }
 
 EipStatus SetAttributeSingleTcp(
-    CipInstance *instance, CipMessageRouterRequest *message_router_request,
-    CipMessageRouterResponse *message_router_response) {
+  CipInstance *instance,
+  CipMessageRouterRequest *message_router_request,
+  CipMessageRouterResponse *message_router_response) {
   CipAttributeStruct *attribute = GetCipAttribute(
-      instance, message_router_request->request_path.attribute_number);
+    instance, message_router_request->request_path.attribute_number);
   (void) instance; /*Suppress compiler warning */
 
   if (0 != attribute) {
@@ -136,7 +141,7 @@ EipStatus SetAttributeSingleTcp(
   message_router_response->size_of_additional_status = 0;
   message_router_response->data_length = 0;
   message_router_response->reply_service = (0x80
-      | message_router_request->service);
+                                            | message_router_request->service);
   return kEipStatusOkSend;
 }
 
@@ -200,8 +205,9 @@ void ShutdownTcpIpInterface(void) {
 }
 
 EipStatus GetAttributeSingleTcpIpInterface(
-    CipInstance *const restrict instance, CipMessageRouterRequest *restrict const message_router_request,
-    CipMessageRouterResponse *restrict const message_router_response) {
+  CipInstance *const restrict instance,
+  CipMessageRouterRequest *restrict const message_router_request,
+  CipMessageRouterResponse *restrict const message_router_response) {
 
   EipStatus status = kEipStatusOkSend;
   EipByte *message = message_router_response->data;
@@ -209,22 +215,22 @@ EipStatus GetAttributeSingleTcpIpInterface(
   if (9 == message_router_request->request_path.attribute_number) { /* attribute 9 can not be easily handled with the default mechanism therefore we will do it by hand */
     message_router_response->data_length = 0;
     message_router_response->reply_service = (0x80
-        | message_router_request->service);
+                                              | message_router_request->service);
     message_router_response->general_status = kCipErrorSuccess;
     message_router_response->size_of_additional_status = 0;
 
     message_router_response->data_length += EncodeData(
-        kCipUsint, &(g_multicast_configuration.alloc_control), &message);
+      kCipUsint, &(g_multicast_configuration.alloc_control), &message);
     message_router_response->data_length += EncodeData(
-        kCipUsint, &(g_multicast_configuration.reserved_shall_be_zero),
-        &message);
+      kCipUsint, &(g_multicast_configuration.reserved_shall_be_zero),
+      &message);
     message_router_response->data_length += EncodeData(
-        kCipUint,
-        &(g_multicast_configuration.number_of_allocated_multicast_addresses),
-        &message);
+      kCipUint,
+      &(g_multicast_configuration.number_of_allocated_multicast_addresses),
+      &message);
 
     EipUint32 multicast_address = ntohl(
-        g_multicast_configuration.starting_multicast_address);
+      g_multicast_configuration.starting_multicast_address);
 
     message_router_response->data_length += EncodeData(kCipUdint,
                                                        &multicast_address,
@@ -237,8 +243,9 @@ EipStatus GetAttributeSingleTcpIpInterface(
 }
 
 EipStatus GetAttributeAllTcpIpInterface(
-    CipInstance *instance, CipMessageRouterRequest *message_router_request,
-    CipMessageRouterResponse *message_router_response) {
+  CipInstance *instance,
+  CipMessageRouterRequest *message_router_request,
+  CipMessageRouterResponse *message_router_response) {
 
   EipUint8 *response = message_router_response->data; /* pointer into the reply */
   CipAttributeStruct *attribute = instance->attributes;
@@ -246,9 +253,10 @@ EipStatus GetAttributeAllTcpIpInterface(
   for (int j = 0; j < instance->cip_class->number_of_attributes; j++) /* for each instance attribute of this class */
   {
     int attribute_number = attribute->attribute_number;
-    if (attribute_number < 32
-        && (instance->cip_class->get_attribute_all_mask & 1 << attribute_number)) /* only return attributes that are flagged as being part of GetAttributeALl */
-        {
+    if ( attribute_number < 32
+         && (instance->cip_class->get_attribute_all_mask & 1 <<
+      attribute_number) )                                                         /* only return attributes that are flagged as being part of GetAttributeALl */
+    {
       message_router_request->request_path.attribute_number = attribute_number;
 
       if (8 == attribute_number) { /* insert 6 zeros for the required empty safety network number according to Table 5-3.10 */
@@ -256,9 +264,9 @@ EipStatus GetAttributeAllTcpIpInterface(
         message_router_response->data += 6;
       }
 
-      if (kEipStatusOkSend
-          != GetAttributeSingleTcpIpInterface(instance, message_router_request,
-                                              message_router_response)) {
+      if ( kEipStatusOkSend
+           != GetAttributeSingleTcpIpInterface(instance, message_router_request,
+                                               message_router_response) ) {
         message_router_response->data = response;
         return kEipStatusError;
       }
@@ -267,7 +275,7 @@ EipStatus GetAttributeAllTcpIpInterface(
     attribute++;
   }
   message_router_response->data_length = message_router_response->data
-      - response;
+                                         - response;
   message_router_response->data = response;
 
   return kEipStatusOkSend;

+ 1 - 1
source/src/cip/ciptcpipinterface.h

@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Copyright (c) 2009, Rockwell Automation, Inc.
- * All rights reserved. 
+ * All rights reserved.
  *
  ******************************************************************************/
 #ifndef OPENER_CIPTCPIPINTERFACE_H_

+ 14 - 14
source/src/enet_encap/cpf.h

@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Copyright (c) 2009, Rockwell Automation, Inc.
- * All rights reserved. 
+ * All rights reserved.
  *
  ******************************************************************************/
 #ifndef OPENER_CPF_H_
@@ -73,7 +73,7 @@ typedef struct {
 
 /** @ingroup ENCAP
  * Parse the CPF data from a received unconnected explicit message and
- * hand the data on to the message router 
+ * hand the data on to the message router
  *
  * @param  received_data pointer to the encapsulation structure with the received message
  * @param  reply_buffer reply buffer
@@ -84,8 +84,8 @@ int NotifyCommonPacketFormat(EncapsulationData *const received_data,
 
 /** @ingroup ENCAP
  * Parse the CPF data from a received connected explicit message, check
- * the connection status, update any timers, and hand the data on to 
- * the message router 
+ * the connection status, update any timers, and hand the data on to
+ * the message router
  *
  * @param  received_data pointer to the encapsulation structure with the received message
  * @param  reply_buffer reply buffer
@@ -100,12 +100,13 @@ int NotifyConnectedCommonPacketFormat(EncapsulationData *received_data,
  *  @param  data_length	length of data in pa_Data.
  *  @param  common_packet_format_data	pointer to structure of CPF data item.
  *  @return status
- * 	       EIP_OK .. success
- * 	       EIP_ERROR .. error
+ *             EIP_OK .. success
+ *             EIP_ERROR .. error
  */
 EipStatus CreateCommonPacketFormatStructure(
-    const EipUint8 *data, int data_length,
-    CipCommonPacketFormatData *common_packet_format_data);
+  const EipUint8 *data,
+  int data_length,
+  CipCommonPacketFormatData *common_packet_format_data);
 
 /** @ingroup ENCAP
  * Copy data from CPFDataItem into linear memory in message for transmission over in encapsulation.
@@ -116,8 +117,7 @@ EipStatus CreateCommonPacketFormatStructure(
  *     EIP_ERROR .. error
  */
 int AssembleIOMessage(
-    CipCommonPacketFormatData *common_packet_format_data_item,
-    EipUint8 *message);
+  CipCommonPacketFormatData *common_packet_format_data_item,EipUint8 *message);
 
 
 /** @ingroup ENCAP
@@ -126,12 +126,12 @@ int AssembleIOMessage(
  * @param  common_packet_format_data_item	pointer to CPF structure which has to be aligned into linear memory.
  * @param  message		pointer to linear memory.
  * @return length of reply in pa_msg in bytes
- * 	   EIP_ERROR .. error
+ *         EIP_ERROR .. error
  */
 int AssembleLinearMessage(
-    CipMessageRouterResponse *message_router_response,
-    CipCommonPacketFormatData *common_packet_format_data_item,
-    EipUint8 *message);
+  CipMessageRouterResponse *message_router_response,
+  CipCommonPacketFormatData *common_packet_format_data_item,
+  EipUint8 *message);
 
 /** @ingroup ENCAP
  * @brief Data storage for the any CPF data

+ 101 - 78
source/src/enet_encap/encap.c

@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Copyright (c) 2009, Rockwell Automation, Inc.
- * All rights reserved. 
+ * All rights reserved.
  *
  ******************************************************************************/
 #include <string.h>
@@ -66,7 +66,8 @@ typedef enum {
 
 #define ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES 2 /**< According to EIP spec at least 2 delayed message requests should be supported */
 
-#define ENCAP_MAX_DELAYED_ENCAP_MESSAGE_SIZE (ENCAPSULATION_HEADER_LENGTH + 39 + sizeof(OPENER_DEVICE_NAME)) /* currently we only have the size of an encapsulation message */
+#define ENCAP_MAX_DELAYED_ENCAP_MESSAGE_SIZE ( ENCAPSULATION_HEADER_LENGTH + \
+                                               39 + sizeof(OPENER_DEVICE_NAME) )                             /* currently we only have the size of an encapsulation message */
 
 /* Encapsulation layer data  */
 
@@ -83,7 +84,8 @@ EncapsulationInterfaceInformation g_interface_information;
 
 int g_registered_sessions[OPENER_NUMBER_OF_SUPPORTED_SESSIONS];
 
-DelayedEncapsulationMessage g_delayed_encapsulation_messages[ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES];
+DelayedEncapsulationMessage g_delayed_encapsulation_messages[
+  ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES];
 
 /*** private functions ***/
 void HandleReceivedListServicesCommand(EncapsulationData *receive_data);
@@ -100,12 +102,12 @@ void HandleReceivedRegisterSessionCommand(int socket,
                                           EncapsulationData *receive_data);
 
 EipStatus HandleReceivedUnregisterSessionCommand(
-    EncapsulationData *receive_data);
+  EncapsulationData *receive_data);
 
 EipStatus HandleReceivedSendUnitDataCommand(EncapsulationData *receive_data);
 
 EipStatus HandleReceivedSendRequestResponseDataCommand(
-    EncapsulationData *receive_data);
+  EncapsulationData *receive_data);
 
 int GetFreeSessionIndex(void);
 
@@ -136,7 +138,8 @@ void EncapsulationInit(void) {
     g_registered_sessions[i] = kEipInvalidSocket;
   }
 
-  for (unsigned int i = 0; i < ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES; i++) {
+  for (unsigned int i = 0; i < ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES;
+       i++) {
     g_delayed_encapsulation_messages[i].socket = -1;
   }
 
@@ -146,12 +149,14 @@ void EncapsulationInit(void) {
   g_interface_information.length = sizeof(g_interface_information);
   g_interface_information.encapsulation_protocol_version = 1;
   g_interface_information.capability_flags = kCapabilityFlagsCipTcp
-      | kCapabilityFlagsCipUdpClass0or1;
-  strcpy((char *) g_interface_information.name_of_service, "Communications");
+                                             | kCapabilityFlagsCipUdpClass0or1;
+  strcpy( (char *) g_interface_information.name_of_service, "Communications" );
 }
 
-int HandleReceivedExplictTcpData(int socket, EipUint8 *buffer,
-                                 unsigned int length, int *remaining_bytes) {
+int HandleReceivedExplictTcpData(int socket,
+                                 EipUint8 *buffer,
+                                 unsigned int length,
+                                 int *remaining_bytes) {
   EipStatus return_value = kEipStatusOk;
   EncapsulationData encapsulation_data;
   /* eat the encapsulation header*/
@@ -192,12 +197,12 @@ int HandleReceivedExplictTcpData(int socket, EipUint8 *buffer,
 
         case (kEncapsulationCommandUnregisterSession):
           return_value = HandleReceivedUnregisterSessionCommand(
-              &encapsulation_data);
+            &encapsulation_data);
           break;
 
         case (kEncapsulationCommandSendRequestReplyData):
           return_value = HandleReceivedSendRequestResponseDataCommand(
-              &encapsulation_data);
+            &encapsulation_data);
           break;
 
         case (kEncapsulationCommandSendUnitData):
@@ -219,16 +224,19 @@ int HandleReceivedExplictTcpData(int socket, EipUint8 *buffer,
   return return_value;
 }
 
-int HandleReceivedExplictUdpData(int socket, struct sockaddr_in *from_address,
-                                 EipUint8 *buffer, unsigned int buffer_length,
-                                 int *number_of_remaining_bytes, int unicast) {
+int HandleReceivedExplictUdpData(int socket,
+                                 struct sockaddr_in *from_address,
+                                 EipUint8 *buffer,
+                                 unsigned int buffer_length,
+                                 int *number_of_remaining_bytes,
+                                 int unicast) {
   EipStatus status = kEipStatusOk;
   EncapsulationData encapsulation_data;
   /* eat the encapsulation header*/
   /* the structure contains a pointer to the encapsulated data*/
   /* returns how many bytes are left after the encapsulated data*/
   *number_of_remaining_bytes = CreateEncapsulationStructure(
-      buffer, buffer_length, &encapsulation_data);
+    buffer, buffer_length, &encapsulation_data);
 
   if (kEncapsulationHeaderOptionsFlag == encapsulation_data.options) /*TODO generate appropriate error response*/
   {
@@ -244,21 +252,21 @@ int HandleReceivedExplictUdpData(int socket, struct sockaddr_in *from_address,
           break;
 
         case (kEncapsulationCommandListIdentity):
-            if(unicast == true) {
-              HandleReceivedListIdentityCommandTcp(&encapsulation_data);
-            }
-            else {
-              HandleReceivedListIdentityCommandUdp(socket, from_address,
-                                               &encapsulation_data);
-              status = kEipStatusOk;
-            }/* as the response has to be delayed do not send it now */
+          if(unicast == true) {
+            HandleReceivedListIdentityCommandTcp(&encapsulation_data);
+          }
+          else {
+            HandleReceivedListIdentityCommandUdp(socket, from_address,
+                                                 &encapsulation_data);
+            status = kEipStatusOk;
+          }  /* as the response has to be delayed do not send it now */
           break;
 
         case (kEncapsulationCommandListInterfaces):
           HandleReceivedListInterfacesCommand(&encapsulation_data);
           break;
 
-          /* The following commands are not to be sent via UDP */
+        /* The following commands are not to be sent via UDP */
         case (kEncapsulationCommandNoOperation):
         case (kEncapsulationCommandRegisterSession):
         case (kEncapsulationCommandUnregisterSession):
@@ -295,33 +303,33 @@ int EncapsulateData(const EncapsulationData *const send_data) {
  */
 void HandleReceivedListServicesCommand(EncapsulationData *receive_data) {
   EipUint8 *communication_buffer = receive_data
-      ->current_communication_buffer_position;
+                                   ->current_communication_buffer_position;
 
   receive_data->data_length = g_interface_information.length + 2;
 
   /* copy Interface data to msg for sending */
   AddIntToMessage(1, &communication_buffer);
   AddIntToMessage(g_interface_information.type_code, &communication_buffer);
-  AddIntToMessage((EipUint16) (g_interface_information.length - 4),
-                  &communication_buffer);
+  AddIntToMessage( (EipUint16) (g_interface_information.length - 4),
+                   &communication_buffer );
   AddIntToMessage(g_interface_information.encapsulation_protocol_version,
                   &communication_buffer);
   AddIntToMessage(g_interface_information.capability_flags,
                   &communication_buffer);
-  memcpy(communication_buffer, g_interface_information.name_of_service,
-         sizeof(g_interface_information.name_of_service));
+  memcpy( communication_buffer, g_interface_information.name_of_service,
+          sizeof(g_interface_information.name_of_service) );
 }
 
 void HandleReceivedListInterfacesCommand(EncapsulationData *receive_data) {
   EipUint8 *communication_buffer = receive_data
-      ->current_communication_buffer_position;
+                                   ->current_communication_buffer_position;
   receive_data->data_length = 2;
   AddIntToMessage(0x0000, &communication_buffer); /* copy Interface data to msg for sending */
 }
 
-void HandleReceivedListIdentityCommandTcp(EncapsulationData * receive_data) {
+void HandleReceivedListIdentityCommandTcp(EncapsulationData *receive_data) {
   receive_data->data_length = EncapsulateListIdentyResponseMessage(
-      receive_data->current_communication_buffer_position);
+    receive_data->current_communication_buffer_position);
 }
 
 void HandleReceivedListIdentityCommandUdp(int socket,
@@ -329,7 +337,8 @@ void HandleReceivedListIdentityCommandUdp(int socket,
                                           EncapsulationData *receive_data) {
   DelayedEncapsulationMessage *delayed_message_buffer = NULL;
 
-  for (unsigned int i = 0; i < ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES; i++) {
+  for (unsigned int i = 0; i < ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES;
+       i++) {
     if (kEipInvalidSocket == g_delayed_encapsulation_messages[i].socket) {
       delayed_message_buffer = &(g_delayed_encapsulation_messages[i]);
       break;
@@ -338,8 +347,8 @@ void HandleReceivedListIdentityCommandUdp(int socket,
 
   if (NULL != delayed_message_buffer) {
     delayed_message_buffer->socket = socket;
-    memcpy((&delayed_message_buffer->receiver), from_address,
-           sizeof(struct sockaddr_in));
+    memcpy( (&delayed_message_buffer->receiver), from_address,
+            sizeof(struct sockaddr_in) );
 
     DetermineDelayTime(receive_data->communication_buffer_start,
                        delayed_message_buffer);
@@ -349,7 +358,7 @@ void HandleReceivedListIdentityCommandUdp(int socket,
            ENCAPSULATION_HEADER_LENGTH);
 
     delayed_message_buffer->message_size = EncapsulateListIdentyResponseMessage(
-        &(delayed_message_buffer->message[ENCAPSULATION_HEADER_LENGTH]));
+      &(delayed_message_buffer->message[ENCAPSULATION_HEADER_LENGTH]) );
 
     EipUint8 *communication_buffer = delayed_message_buffer->message + 2;
     AddIntToMessage(delayed_message_buffer->message_size,
@@ -361,7 +370,7 @@ void HandleReceivedListIdentityCommandUdp(int socket,
 int EncapsulateListIdentyResponseMessage(EipByte *const communication_buffer) {
   EipUint8 *communication_buffer_runner = communication_buffer;
 
-  AddIntToMessage(1, &(communication_buffer_runner)); /* Item count: one item */
+  AddIntToMessage( 1, &(communication_buffer_runner) ); /* Item count: one item */
   AddIntToMessage(kCipItemIdListIdentityResponse, &communication_buffer_runner);
 
   EipByte *id_length_buffer = communication_buffer_runner;
@@ -399,14 +408,15 @@ void DetermineDelayTime(EipByte *buffer_start,
                         DelayedEncapsulationMessage *delayed_message_buffer) {
 
   buffer_start += 12; /* start of the sender context */
-  EipUint16 maximum_delay_time = GetIntFromMessage((const EipUint8 **const)&buffer_start);
+  EipUint16 maximum_delay_time = GetIntFromMessage(
+     (const EipUint8 **const)&buffer_start );
 
   if (0 == maximum_delay_time) {
     maximum_delay_time = kListIdentityDefaultDelayTime;
   } else if (kListIdentityMinimumDelayTime > maximum_delay_time) { /* if maximum_delay_time is between 1 and 500ms set it to 500ms */
     maximum_delay_time = kListIdentityMinimumDelayTime;
   }
-  delayed_message_buffer->time_out = (maximum_delay_time * rand()) / RAND_MAX; /* Sets delay time between 0 and maximum_delay_time */
+  delayed_message_buffer->time_out = ( maximum_delay_time * rand() ) / RAND_MAX; /* Sets delay time between 0 and maximum_delay_time */
 }
 
 /* @brief Check supported protocol, generate session handle, send replay back to originator.
@@ -418,13 +428,13 @@ void HandleReceivedRegisterSessionCommand(int socket,
   int session_index = 0;
   const EipUint8 *receive_data_buffer = NULL;
   EipUint16 protocol_version = GetIntFromMessage(
-      (const EipUint8 **const)&receive_data->current_communication_buffer_position);
+    (const EipUint8 **const)&receive_data->current_communication_buffer_position);
   EipUint16 nOptionFlag = GetIntFromMessage(
-      (const EipUint8 **const)&receive_data->current_communication_buffer_position);
+    (const EipUint8 **const)&receive_data->current_communication_buffer_position);
 
   /* check if requested protocol version is supported and the register session option flag is zero*/
-  if ((0 < protocol_version) && (protocol_version <= kSupportedProtocolVersion)
-      && (0 == nOptionFlag)) { /*Option field should be zero*/
+  if ( (0 < protocol_version) && (protocol_version <= kSupportedProtocolVersion)
+       && (0 == nOptionFlag) ) { /*Option field should be zero*/
     /* check if the socket has already a session open */
     for (int i = 0; i < OPENER_NUMBER_OF_SUPPORTED_SESSIONS; ++i) {
       if (g_registered_sessions[i] == socket) {
@@ -433,8 +443,10 @@ void HandleReceivedRegisterSessionCommand(int socket,
         receive_data->status = kEncapsulationProtocolInvalidCommand;
         session_index = kSessionStatusInvalid;
         receive_data_buffer =
-            &receive_data->communication_buffer_start[kEncapsulationHeaderSessionHandlePosition];
-        AddDintToMessage(receive_data->session_handle, (EipUint8 **const)&receive_data_buffer); /*EncapsulateData will not update the session handle so we have to do it here by hand*/
+          &receive_data->communication_buffer_start[
+            kEncapsulationHeaderSessionHandlePosition];
+        AddDintToMessage(receive_data->session_handle,
+                         (EipUint8 **const)&receive_data_buffer);                               /*EncapsulateData will not update the session handle so we have to do it here by hand*/
         break;
       }
     }
@@ -449,8 +461,10 @@ void HandleReceivedRegisterSessionCommand(int socket,
         receive_data->session_handle = session_index + 1;
         receive_data->status = kEncapsulationProtocolSuccess;
         receive_data_buffer =
-            &receive_data->communication_buffer_start[kEncapsulationHeaderSessionHandlePosition];
-        AddDintToMessage(receive_data->session_handle, (EipUint8 **const)&receive_data_buffer); /*EncapsulateData will not update the session handle so we have to do it here by hand*/
+          &receive_data->communication_buffer_start[
+            kEncapsulationHeaderSessionHandlePosition];
+        AddDintToMessage(receive_data->session_handle,
+                         (EipUint8 **const)&receive_data_buffer);                               /*EncapsulateData will not update the session handle so we have to do it here by hand*/
       }
     }
   } else { /* protocol not supported */
@@ -465,10 +479,11 @@ void HandleReceivedRegisterSessionCommand(int socket,
  *      pa_S_ReceiveData pointer to unregister session request with corresponding socket handle.
  */
 EipStatus HandleReceivedUnregisterSessionCommand(
-    EncapsulationData *receive_data) {
+  EncapsulationData *receive_data) {
 
-  if ((0 < receive_data->session_handle)
-      && (receive_data->session_handle <= OPENER_NUMBER_OF_SUPPORTED_SESSIONS)) {
+  if ( (0 < receive_data->session_handle)
+       && (receive_data->session_handle <=
+           OPENER_NUMBER_OF_SUPPORTED_SESSIONS) ) {
     int i = receive_data->session_handle - 1;
     if (kEipInvalidSocket != g_registered_sessions[i]) {
       IApp_CloseSocket_tcp(g_registered_sessions[i]);
@@ -493,16 +508,18 @@ EipStatus HandleReceivedSendUnitDataCommand(EncapsulationData *receive_data) {
   if (receive_data->data_length >= 6) {
     /* Command specific data UDINT .. Interface Handle, UINT .. Timeout, CPF packets */
     /* don't use the data yet */
-    GetDintFromMessage((const EipUint8 **const)&receive_data->current_communication_buffer_position); /* skip over null interface handle*/
-    GetIntFromMessage((const EipUint8 **const)&receive_data->current_communication_buffer_position); /* skip over unused timeout value*/
+    GetDintFromMessage(
+       (const EipUint8 **const)&receive_data->current_communication_buffer_position );                /* skip over null interface handle*/
+    GetIntFromMessage(
+       (const EipUint8 **const)&receive_data->current_communication_buffer_position );               /* skip over unused timeout value*/
     receive_data->data_length -= 6; /* the rest is in CPF format*/
 
-    if (kSessionStatusValid == CheckRegisteredSessions(receive_data)) /* see if the EIP session is registered*/
+    if ( kSessionStatusValid == CheckRegisteredSessions(receive_data) ) /* see if the EIP session is registered*/
     {
       send_size =
-          NotifyConnectedCommonPacketFormat(
-              receive_data,
-              &receive_data->communication_buffer_start[ENCAPSULATION_HEADER_LENGTH]);
+        NotifyConnectedCommonPacketFormat(
+          receive_data,
+          &receive_data->communication_buffer_start[ENCAPSULATION_HEADER_LENGTH]);
 
       if (0 < send_size) { /* need to send reply */
         receive_data->data_length = send_size;
@@ -519,27 +536,29 @@ EipStatus HandleReceivedSendUnitDataCommand(EncapsulationData *receive_data) {
 
 /** @brief Call UCMM or Message Router if UCMM not implemented.
  *  @param receive_data Pointer to structure with data and header information.
- *  @return status 	0 .. success.
- * 					-1 .. error
+ *  @return status      0 .. success.
+ *                                      -1 .. error
  */
 EipStatus HandleReceivedSendRequestResponseDataCommand(
-    EncapsulationData *receive_data) {
+  EncapsulationData *receive_data) {
   EipInt16 send_size = 0;
   EipStatus return_value = kEipStatusOkSend;
 
   if (receive_data->data_length >= 6) {
     /* Command specific data UDINT .. Interface Handle, UINT .. Timeout, CPF packets */
     /* don't use the data yet */
-    GetDintFromMessage((const EipUint8 **const)&receive_data->current_communication_buffer_position); /* skip over null interface handle*/
-    GetIntFromMessage((const EipUint8 **const)&receive_data->current_communication_buffer_position); /* skip over unused timeout value*/
+    GetDintFromMessage(
+       (const EipUint8 **const)&receive_data->current_communication_buffer_position );                /* skip over null interface handle*/
+    GetIntFromMessage(
+       (const EipUint8 **const)&receive_data->current_communication_buffer_position );               /* skip over unused timeout value*/
     receive_data->data_length -= 6; /* the rest is in CPF format*/
 
-    if (kSessionStatusValid == CheckRegisteredSessions(receive_data)) /* see if the EIP session is registered*/
+    if ( kSessionStatusValid == CheckRegisteredSessions(receive_data) ) /* see if the EIP session is registered*/
     {
       send_size =
-          NotifyCommonPacketFormat(
-              receive_data,
-              &receive_data->communication_buffer_start[ENCAPSULATION_HEADER_LENGTH]);
+        NotifyCommonPacketFormat(
+          receive_data,
+          &receive_data->communication_buffer_start[ENCAPSULATION_HEADER_LENGTH]);
 
       if (send_size >= 0) { /* need to send reply */
         receive_data->data_length = send_size;
@@ -556,10 +575,11 @@ EipStatus HandleReceivedSendRequestResponseDataCommand(
 
 /** @brief search for available sessions an return index.
  *  @return return index of free session in anRegisteredSessions.
- * 			kInvalidSession .. no free session available
+ *                      kInvalidSession .. no free session available
  */
 int GetFreeSessionIndex(void) {
-  for (int session_index = 0; session_index < OPENER_NUMBER_OF_SUPPORTED_SESSIONS; session_index++) {
+  for (int session_index = 0;
+       session_index < OPENER_NUMBER_OF_SUPPORTED_SESSIONS; session_index++) {
     if (kEipInvalidSocket == g_registered_sessions[session_index]) {
       return session_index;
     }
@@ -572,9 +592,9 @@ int GetFreeSessionIndex(void) {
  * @param length Length of the data in receive_buffer. Might be more than one message
  * @param encapsulation_data	structure to which data shall be copied
  * @return return difference between bytes in pa_buf an data_length
- *  		0 .. full package received
- * 			>0 .. more than one packet received
- * 			<0 .. only fragment of data portion received
+ *              0 .. full package received
+ *                      >0 .. more than one packet received
+ *                      <0 .. only fragment of data portion received
  */
 EipInt16 CreateEncapsulationStructure(const EipUint8 *receive_buffer,
                                       int receive_buffer_length,
@@ -587,19 +607,21 @@ EipInt16 CreateEncapsulationStructure(const EipUint8 *receive_buffer,
 
   receive_buffer += kSenderContextSize;
   encapsulation_data->options = GetDintFromMessage(&receive_buffer);
-  encapsulation_data->current_communication_buffer_position = (EipUint8 *)receive_buffer;
+  encapsulation_data->current_communication_buffer_position =
+    (EipUint8 *)receive_buffer;
   return (receive_buffer_length - ENCAPSULATION_HEADER_LENGTH
-      - encapsulation_data->data_length);
+          - encapsulation_data->data_length);
 }
 
 /** @brief Check if received package belongs to registered session.
  *  @param receive_data Received data.
  *  @return 0 .. Session registered
- *  		kInvalidSession .. invalid session -> return unsupported command received
+ *              kInvalidSession .. invalid session -> return unsupported command received
  */
 SessionStatus CheckRegisteredSessions(EncapsulationData *receive_data) {
-  if ((0 < receive_data->session_handle)
-      && (receive_data->session_handle <= OPENER_NUMBER_OF_SUPPORTED_SESSIONS)) {
+  if ( (0 < receive_data->session_handle)
+       && (receive_data->session_handle <=
+           OPENER_NUMBER_OF_SUPPORTED_SESSIONS) ) {
     if (kEipInvalidSocket
         != g_registered_sessions[receive_data->session_handle - 1]) {
       return kSessionStatusValid;
@@ -629,10 +651,11 @@ void EncapsulationShutDown(void) {
 }
 
 void ManageEncapsulationMessages(const MilliSeconds elapsed_time) {
-  for (unsigned int i = 0; i < ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES; i++) {
+  for (unsigned int i = 0; i < ENCAP_NUMBER_OF_SUPPORTED_DELAYED_ENCAP_MESSAGES;
+       i++) {
     if (kEipInvalidSocket != g_delayed_encapsulation_messages[i].socket) {
       g_delayed_encapsulation_messages[i].time_out -=
-          elapsed_time;
+        elapsed_time;
       if (0 > g_delayed_encapsulation_messages[i].time_out) {
         /* If delay is reached or passed, send the UDP message */
         SendUdpData(&(g_delayed_encapsulation_messages[i].receiver),

+ 1 - 1
source/src/ports/POSIX/networkhandler.h

@@ -13,7 +13,7 @@
 
 /** @brief Executes platform dependent network handler initialization code
  *
- * 	@return EipStatusOk if initialization was successful, otherwise EipStatusError
+ *      @return EipStatusOk if initialization was successful, otherwise EipStatusError
  */
 EipStatus NetworkHandlerInitializePlatform(void);