Kaynağa Gözat

Adds default in cipconnectionmanager to satisfy compiler and detect errors

CapXilinx 9 yıl önce
ebeveyn
işleme
9d2fbcf794

+ 128 - 101
source/src/cip/appcontype.c

@@ -30,7 +30,8 @@ typedef struct {
   unsigned int output_assembly; /**< the O-to-T point for the connection */
   unsigned int input_assembly; /**< the T-to-O point for the connection */
   unsigned int config_assembly; /**< the config point for the connection */
-  ConnectionObject connection_data[OPENER_CIP_NUM_INPUT_ONLY_CONNS_PER_CON_PATH]; /*< the connection data */
+  ConnectionObject connection_data[OPENER_CIP_NUM_INPUT_ONLY_CONNS_PER_CON_PATH
+  ];                                                                              /*< the connection data */
 } InputOnlyConnection;
 
 /** @brief Listen Only connection data */
@@ -38,14 +39,17 @@ typedef struct {
   unsigned int output_assembly; /**< the O-to-T point for the connection */
   unsigned int input_assembly; /**< the T-to-O point for the connection */
   unsigned int config_assembly; /**< the config point for the connection */
-  ConnectionObject connection_data[OPENER_CIP_NUM_LISTEN_ONLY_CONNS_PER_CON_PATH]; /**< the connection data */
+  ConnectionObject connection_data[
+    OPENER_CIP_NUM_LISTEN_ONLY_CONNS_PER_CON_PATH];                                /**< the connection data */
 } ListenOnlyConnection;
 
-ExclusiveOwnerConnection g_exlusive_owner_connections[OPENER_CIP_NUM_EXLUSIVE_OWNER_CONNS]; /**< Exclusive Owner connections */
+ExclusiveOwnerConnection g_exlusive_owner_connections[
+  OPENER_CIP_NUM_EXLUSIVE_OWNER_CONNS];                                                     /**< Exclusive Owner connections */
 
 InputOnlyConnection g_input_only_connections[OPENER_CIP_NUM_INPUT_ONLY_CONNS]; /**< Input Only connections */
 
-ListenOnlyConnection g_listen_only_connections[OPENER_CIP_NUM_LISTEN_ONLY_CONNS]; /**< Listen Only connections */
+ListenOnlyConnection g_listen_only_connections[OPENER_CIP_NUM_LISTEN_ONLY_CONNS
+];                                                                                /**< Listen Only connections */
 
 /** @brief Takes an ConnectionObject and searches and returns an Exclusive Owner Connection based on the ConnectionObject,
  * if there is non it returns NULL
@@ -55,7 +59,8 @@ ListenOnlyConnection g_listen_only_connections[OPENER_CIP_NUM_LISTEN_ONLY_CONNS]
  * @return The corresponding Exclusive Owner Connection or NULL if there is non
  */
 ConnectionObject *GetExclusiveOwnerConnection(
-    const ConnectionObject *restrict connection_object, EipUint16 *const extended_error);
+  const ConnectionObject *restrict connection_object,
+  EipUint16 *const extended_error);
 
 /** @brief Takes an ConnectionObject and searches and returns an Input Only Connection based on the ConnectionObject,
  * if there is non it returns NULL
@@ -64,8 +69,9 @@ ConnectionObject *GetExclusiveOwnerConnection(
  * @param extended_error Pointer to the extended error variable, will be written if an error occurs
  * @return The corresponding Exclusive Owner Connection or NULL if there is non
  */
-ConnectionObject *GetInputOnlyConnection(const ConnectionObject *const restrict connection_object,
-                                         EipUint16 *const extended_error);
+ConnectionObject *GetInputOnlyConnection(
+  const ConnectionObject *const restrict connection_object,
+  EipUint16 *const extended_error);
 
 /** @brief Takes an ConnectionObject and searches and returns an Listen Only Connection based on the ConnectionObject,
  * if there is non it returns NULL
@@ -74,57 +80,64 @@ ConnectionObject *GetInputOnlyConnection(const ConnectionObject *const restrict
  * @param extended_error Pointer to the extended error variable, will be written if an error occurs
  * @return The corresponding Exclusive Owner Connection or NULL if there is non
  */
-ConnectionObject *GetListenOnlyConnection(const ConnectionObject *const restrict connection_object,
-                                          EipUint16 *const extended_error);
-
-void ConfigureExclusiveOwnerConnectionPoint(const unsigned int connection_number,
-                                            const unsigned int output_assembly,
-                                            const unsigned int input_assembly,
-                                            const unsigned int config_assembly) {
+ConnectionObject *GetListenOnlyConnection(
+  const ConnectionObject *const restrict connection_object,
+  EipUint16 *const extended_error);
+
+void ConfigureExclusiveOwnerConnectionPoint(
+  const unsigned int connection_number,
+  const unsigned int output_assembly,
+  const unsigned int input_assembly,
+  const unsigned int config_assembly)
+{
   if (OPENER_CIP_NUM_EXLUSIVE_OWNER_CONNS > connection_number) {
     g_exlusive_owner_connections[connection_number].output_assembly =
-        output_assembly;
+      output_assembly;
     g_exlusive_owner_connections[connection_number].input_assembly =
-        input_assembly;
+      input_assembly;
     g_exlusive_owner_connections[connection_number].config_assembly =
-        config_assembly;
+      config_assembly;
   }
 }
 
 void ConfigureInputOnlyConnectionPoint(const unsigned int connection_number,
                                        const unsigned int output_assembly,
                                        const unsigned int input_assembly,
-                                       const unsigned int config_assembly) {
+                                       const unsigned int config_assembly)
+{
   if (OPENER_CIP_NUM_INPUT_ONLY_CONNS > connection_number) {
     g_input_only_connections[connection_number].output_assembly =
-        output_assembly;
+      output_assembly;
     g_input_only_connections[connection_number].input_assembly = input_assembly;
     g_input_only_connections[connection_number].config_assembly =
-        config_assembly;
+      config_assembly;
   }
 }
 
 void ConfigureListenOnlyConnectionPoint(const unsigned int connection_number,
                                         const unsigned int output_assembly,
                                         const unsigned int input_assembly,
-                                        const unsigned int config_assembly) {
+                                        const unsigned int config_assembly)
+{
   if (OPENER_CIP_NUM_LISTEN_ONLY_CONNS > connection_number) {
     g_listen_only_connections[connection_number].output_assembly =
-        output_assembly;
+      output_assembly;
     g_listen_only_connections[connection_number].input_assembly =
-        input_assembly;
+      input_assembly;
     g_listen_only_connections[connection_number].config_assembly =
-        config_assembly;
+      config_assembly;
   }
 }
 
 ConnectionObject *GetIoConnectionForConnectionData(
-    ConnectionObject *const restrict connection_object, EipUint16 *const extended_error) {
+  ConnectionObject *const restrict connection_object,
+  EipUint16 *const extended_error) {
 
   *extended_error = 0;
 
-  ConnectionObject *io_connection = GetExclusiveOwnerConnection(connection_object,
-                                              extended_error);
+  ConnectionObject *io_connection = GetExclusiveOwnerConnection(
+    connection_object,
+    extended_error);
   if (NULL == io_connection) {
     if (0 == *extended_error) {
       /* we found no connection and don't have an error so try input only next */
@@ -134,11 +147,11 @@ ConnectionObject *GetIoConnectionForConnectionData(
           /* we found no connection and don't have an error so try listen only next */
           io_connection = GetListenOnlyConnection(connection_object,
                                                   extended_error);
-          if ((NULL == io_connection) && (0 == *extended_error)) {
+          if ( (NULL == io_connection) && (0 == *extended_error) ) {
             /* no application connection type was found that suits the given data */
             /* TODO check error code VS */
             *extended_error =
-                kConnectionManagerExtendedStatusCodeInconsistentApplicationPathCombo;
+              kConnectionManagerExtendedStatusCodeInconsistentApplicationPathCombo;
           } else {
             connection_object->instance_type = kConnectionTypeIoListenOnly;
           }
@@ -159,34 +172,39 @@ ConnectionObject *GetIoConnectionForConnectionData(
 }
 
 ConnectionObject *GetExclusiveOwnerConnection(
-    const ConnectionObject *const restrict connection_object, EipUint16 *const extended_error) {
+  const ConnectionObject *const restrict connection_object,
+  EipUint16 *const extended_error) {
   ConnectionObject *exclusive_owner_connection = NULL;
 
   for (int i = 0; i < OPENER_CIP_NUM_EXLUSIVE_OWNER_CONNS; i++) {
-    if ((g_exlusive_owner_connections[i].output_assembly
-        == connection_object->connection_path.connection_point[0])
-        && (g_exlusive_owner_connections[i].input_assembly
-            == connection_object->connection_path.connection_point[1])
-        && (g_exlusive_owner_connections[i].config_assembly
-            == connection_object->connection_path.connection_point[2])) {
+    if ( (g_exlusive_owner_connections[i].output_assembly
+          == connection_object->connection_path.connection_point[0])
+         && (g_exlusive_owner_connections[i].input_assembly
+             == connection_object->connection_path.connection_point[1])
+         && (g_exlusive_owner_connections[i].config_assembly
+             == connection_object->connection_path.connection_point[2]) ) {
 
       /* check if on other connection point with the same output assembly is currently connected */
-      if (NULL
-          != GetConnectedOutputAssembly(
-              connection_object->connection_path.connection_point[0])) {
-        *extended_error = kConnectionManagerExtendedStatusCodeErrorOwnershipConflict;
+      if ( NULL
+           != GetConnectedOutputAssembly(
+             connection_object->connection_path.connection_point[0]) ) {
+        *extended_error =
+          kConnectionManagerExtendedStatusCodeErrorOwnershipConflict;
+        OPENER_TRACE_INFO("Hit an Ownership conflict in appcontype.c");
         break;
       }
       exclusive_owner_connection = &(g_exlusive_owner_connections[i]
-          .connection_data);
+                                     .connection_data);
       break;
     }
   }
   return exclusive_owner_connection;
 }
 
-ConnectionObject *GetInputOnlyConnection(const ConnectionObject *const restrict connection_object,
-                                         EipUint16 *const extended_error) {
+ConnectionObject *GetInputOnlyConnection(
+  const ConnectionObject *const restrict connection_object,
+  EipUint16 *const extended_error)
+{
   ConnectionObject *input_only_connection = NULL; //TODO: This variable has no use
 
   for (int i = 0; i < OPENER_CIP_NUM_INPUT_ONLY_CONNS; i++) {
@@ -195,13 +213,13 @@ ConnectionObject *GetInputOnlyConnection(const ConnectionObject *const restrict
       if (g_input_only_connections[i].input_assembly
           != connection_object->connection_path.connection_point[1]) {
         *extended_error =
-            kConnectionManagerExtendedStatusCodeInvalidProducingApplicationPath;
+          kConnectionManagerExtendedStatusCodeInvalidProducingApplicationPath;
         break;
       }
       if (g_input_only_connections[i].config_assembly
           != connection_object->connection_path.connection_point[2]) {
         *extended_error =
-            kConnectionManagerExtendedStatusCodeInconsistentApplicationPathCombo;
+          kConnectionManagerExtendedStatusCodeInconsistentApplicationPathCombo;
         break;
       }
 
@@ -212,23 +230,25 @@ ConnectionObject *GetInputOnlyConnection(const ConnectionObject *const restrict
         }
       }
       *extended_error =
-          kConnectionManagerExtendedStatusCodeTargetObjectOutOfConnections;
+        kConnectionManagerExtendedStatusCodeTargetObjectOutOfConnections;
       break;
     }
   }
   return input_only_connection;
 }
 
-ConnectionObject *GetListenOnlyConnection(const ConnectionObject *const restrict connection_object,
-                                          EipUint16 *const extended_error) {
+ConnectionObject *GetListenOnlyConnection(
+  const ConnectionObject *const restrict connection_object,
+  EipUint16 *const extended_error)
+{
   ConnectionObject *listen_only_connection = NULL; //TODO: This variable has no use
 
-  if (kForwardOpenConnectionTypeMulticastConnection
-      != (connection_object->t_to_o_network_connection_parameter
-          & kForwardOpenConnectionTypeMulticastConnection)) {
+  if ( kForwardOpenConnectionTypeMulticastConnection
+       != (connection_object->t_to_o_network_connection_parameter
+           & kForwardOpenConnectionTypeMulticastConnection) ) {
     /* a listen only connection has to be a multicast connection. */
     *extended_error =
-        kConnectionManagerExtendedStatusCodeNonListenOnlyConnectionNotOpened; /* maybe not the best error message however there is no suitable definition in the cip spec */
+      kConnectionManagerExtendedStatusCodeNonListenOnlyConnectionNotOpened;   /* maybe not the best error message however there is no suitable definition in the cip spec */
     return NULL;
   }
 
@@ -238,21 +258,21 @@ ConnectionObject *GetListenOnlyConnection(const ConnectionObject *const restrict
       if (g_listen_only_connections[i].input_assembly
           != connection_object->connection_path.connection_point[1]) {
         *extended_error =
-            kConnectionManagerExtendedStatusCodeInvalidProducingApplicationPath;
+          kConnectionManagerExtendedStatusCodeInvalidProducingApplicationPath;
         break;
       }
       if (g_listen_only_connections[i].config_assembly
           != connection_object->connection_path.connection_point[2]) {
         *extended_error =
-            kConnectionManagerExtendedStatusCodeInconsistentApplicationPathCombo;
+          kConnectionManagerExtendedStatusCodeInconsistentApplicationPathCombo;
         break;
       }
 
-      if (NULL
-          == GetExistingProducerMulticastConnection(
-              connection_object->connection_path.connection_point[1])) {
+      if ( NULL
+           == GetExistingProducerMulticastConnection(
+             connection_object->connection_path.connection_point[1]) ) {
         *extended_error =
-            kConnectionManagerExtendedStatusCodeNonListenOnlyConnectionNotOpened;
+          kConnectionManagerExtendedStatusCodeNonListenOnlyConnectionNotOpened;
         break;
       }
 
@@ -263,29 +283,32 @@ ConnectionObject *GetListenOnlyConnection(const ConnectionObject *const restrict
         }
       }
       *extended_error =
-          kConnectionManagerExtendedStatusCodeTargetObjectOutOfConnections;
+        kConnectionManagerExtendedStatusCodeTargetObjectOutOfConnections;
       break;
     }
   }
   return listen_only_connection;
 }
 
-ConnectionObject *GetExistingProducerMulticastConnection(const EipUint32 input_point) {
+ConnectionObject *GetExistingProducerMulticastConnection(
+  const EipUint32 input_point) {
   ConnectionObject *producer_multicast_connection = g_active_connection_list;
 
   while (NULL != producer_multicast_connection) {
-    if ((kConnectionTypeIoExclusiveOwner
-        == producer_multicast_connection->instance_type)
-        || (kConnectionTypeIoInputOnly
-            == producer_multicast_connection->instance_type)) {
-      if ((input_point
-          == producer_multicast_connection->connection_path.connection_point[1])
-          && (kForwardOpenConnectionTypeMulticastConnection
-              == (producer_multicast_connection
-                  ->t_to_o_network_connection_parameter
-                  & kForwardOpenConnectionTypeMulticastConnection))
-          && (kEipInvalidSocket
-              != producer_multicast_connection->socket[kUdpCommuncationDirectionProducing])) {
+    if ( (kConnectionTypeIoExclusiveOwner
+          == producer_multicast_connection->instance_type)
+         || (kConnectionTypeIoInputOnly
+             == producer_multicast_connection->instance_type) ) {
+      if ( (input_point
+            == producer_multicast_connection->connection_path.connection_point[
+              1])
+           && ( kForwardOpenConnectionTypeMulticastConnection
+                == (producer_multicast_connection
+                    ->t_to_o_network_connection_parameter
+                    & kForwardOpenConnectionTypeMulticastConnection) )
+           && (kEipInvalidSocket
+               != producer_multicast_connection->socket[
+                 kUdpCommuncationDirectionProducing]) ) {
         /* we have a connection that produces the same input assembly,
          * is a multicast producer and manages the connection.
          */
@@ -293,29 +316,31 @@ ConnectionObject *GetExistingProducerMulticastConnection(const EipUint32 input_p
       }
     }
     producer_multicast_connection = producer_multicast_connection
-        ->next_connection_object;
+                                    ->next_connection_object;
   }
   return producer_multicast_connection;
 }
 
-ConnectionObject *GetNextNonControlMasterConnection(const EipUint32 input_point) {
+ConnectionObject *GetNextNonControlMasterConnection(const EipUint32 input_point)
+{
   ConnectionObject *next_non_control_master_connection =
-      g_active_connection_list;
+    g_active_connection_list;
 
   while (NULL != next_non_control_master_connection) {
-    if ((kConnectionTypeIoExclusiveOwner
-        == next_non_control_master_connection->instance_type)
-        || (kConnectionTypeIoInputOnly
-            == next_non_control_master_connection->instance_type)) {
-      if ((input_point
-          == next_non_control_master_connection->connection_path
-              .connection_point[1])
-          && (kForwardOpenConnectionTypeMulticastConnection
-              == (next_non_control_master_connection
-                  ->t_to_o_network_connection_parameter
-                  & kForwardOpenConnectionTypeMulticastConnection))
-          && (kEipInvalidSocket
-              == next_non_control_master_connection->socket[kUdpCommuncationDirectionProducing])) {
+    if ( (kConnectionTypeIoExclusiveOwner
+          == next_non_control_master_connection->instance_type)
+         || (kConnectionTypeIoInputOnly
+             == next_non_control_master_connection->instance_type) ) {
+      if ( (input_point
+            == next_non_control_master_connection->connection_path
+            .connection_point[1])
+           && ( kForwardOpenConnectionTypeMulticastConnection
+                == (next_non_control_master_connection
+                    ->t_to_o_network_connection_parameter
+                    & kForwardOpenConnectionTypeMulticastConnection) )
+           && (kEipInvalidSocket
+               == next_non_control_master_connection->socket[
+                 kUdpCommuncationDirectionProducing]) ) {
         /* we have a connection that produces the same input assembly,
          * is a multicast producer and does not manages the connection.
          */
@@ -323,24 +348,26 @@ ConnectionObject *GetNextNonControlMasterConnection(const EipUint32 input_point)
       }
     }
     next_non_control_master_connection = next_non_control_master_connection
-        ->next_connection_object;
+                                         ->next_connection_object;
   }
   return next_non_control_master_connection;
 }
 
 void CloseAllConnectionsForInputWithSameType(const EipUint32 input_point,
-                                             const ConnectionType instance_type) {
+                                             const ConnectionType instance_type)
+{
   ConnectionObject *connection = g_active_connection_list;
 
   while (NULL != connection) {
-    if ((instance_type == connection->instance_type)
-        && (input_point == connection->connection_path.connection_point[1])) {
+    if ( (instance_type == connection->instance_type)
+         && (input_point ==
+             connection->connection_path.connection_point[1]) ) {
       ConnectionObject *connection_to_delete = connection;
       connection = connection->next_connection_object;
       CheckIoConnectionEvent(
-          connection_to_delete->connection_path.connection_point[0],
-          connection_to_delete->connection_path.connection_point[1],
-          kIoConnectionEventClosed);
+        connection_to_delete->connection_path.connection_point[0],
+        connection_to_delete->connection_path.connection_point[1],
+        kIoConnectionEventClosed);
 
       assert(connection_to_delete->connection_close_function != NULL);
       connection_to_delete->connection_close_function(connection_to_delete);
@@ -376,10 +403,10 @@ EipBool8 ConnectionWithSameConfigPointExists(const EipUint32 config_point) {
 }
 
 void InitializeIoConnectionData(void) {
-  memset(g_exlusive_owner_connections, 0,
-  OPENER_CIP_NUM_EXLUSIVE_OWNER_CONNS * sizeof(ExclusiveOwnerConnection));
-  memset(g_input_only_connections, 0,
-  OPENER_CIP_NUM_INPUT_ONLY_CONNS * sizeof(InputOnlyConnection));
-  memset(g_listen_only_connections, 0,
-  OPENER_CIP_NUM_LISTEN_ONLY_CONNS * sizeof(ListenOnlyConnection));
+  memset( g_exlusive_owner_connections, 0,
+          OPENER_CIP_NUM_EXLUSIVE_OWNER_CONNS * sizeof(ExclusiveOwnerConnection) );
+  memset( g_input_only_connections, 0,
+          OPENER_CIP_NUM_INPUT_ONLY_CONNS * sizeof(InputOnlyConnection) );
+  memset( g_listen_only_connections, 0,
+          OPENER_CIP_NUM_LISTEN_ONLY_CONNS * sizeof(ListenOnlyConnection) );
 }

+ 64 - 36
source/src/cip/cipconnectionmanager.c

@@ -25,6 +25,7 @@
 #include "encap.h"
 #include "generic_networkhandler.h"
 #include "cipepath.h"
+#include "cipelectronickey.h"
 
 /* values needed from the CIP identity object */
 extern EipUint16 vendor_id_;
@@ -135,11 +136,8 @@ EipStatus GetConnectionOwner(CipInstance *instance,
                              CipMessageRouterResponse *message_router_response);
 
 EipStatus AssembleForwardOpenResponse(
-  ConnectionObject *connection_object,
-  CipMessageRouterResponse *
-  message_router_response,
-  EipUint8 general_status,
-  EipUint16 extended_status);
+  ConnectionObject *connection_object,CipMessageRouterResponse *
+  message_router_response,EipUint8 general_status,EipUint16 extended_status);
 
 EipStatus AssembleForwardCloseResponse(
   EipUint16 connection_serial_number,
@@ -874,12 +872,8 @@ EipStatus ManageConnections(MilliSeconds elapsed_time) {
  *                -1 .. error
  */
 EipStatus AssembleForwardOpenResponse(
-  ConnectionObject *connection_object,
-  CipMessageRouterResponse *
-  message_router_response,
-  EipUint8 general_status,
-  EipUint16 extended_status)
-{
+  ConnectionObject *connection_object,CipMessageRouterResponse *
+  message_router_response,EipUint8 general_status,EipUint16 extended_status) {
   /* write reply information in CPF struct dependent of pa_status */
   CipCommonPacketFormatData *cip_common_packet_format_data =
     &g_common_packet_format_data_item;
@@ -1016,8 +1010,7 @@ EipStatus AssembleForwardCloseResponse(
   message_router_request,
   CipMessageRouterResponse *
   message_router_response,
-  EipUint16 extended_error_code)
-{
+  EipUint16 extended_error_code)                         {
   /* write reply information in CPF struct dependent of pa_status */
   CipCommonPacketFormatData *common_data_packet_format_data =
     &g_common_packet_format_data_item;
@@ -1220,7 +1213,59 @@ EipUint8 ParseConnectionPath(ConnectionObject *connection_object,
   }
 
   if (remaining_path_size > 0) {
-    /* first electronic key */
+    /* first look if there is an electronic key */
+    if ( kSegmentTypeLogicalSegment == GetPathSegmentType(message) ) {
+      if( kLogicalSegmentLogicalTypeSpecial ==
+          GetPathLogicalSegmentLogicalType(message) ) {
+        if( kLogicalSegmentSpecialTypeLogicalFormatElectronicKey ==
+            GetPathLogicalSegmentSpecialTypeLogicalType(message) ) {
+          if( kElectronicKeySegmentFormatKeyFormat4 ==
+              GetPathLogicalSegmentElectronicKeyFormat(message) ) {
+            /* Check if there is enough data for holding the electronic key segment */
+            if (remaining_path_size < 5) {
+              *extended_error = 0;
+              return kCipErrorNotEnoughData;
+            }
+            /* Electronic key format 4 found */
+            ElectronicKeyFormat4 *electronic_key = ElectronicKeyFormat4New();
+            GetPathLogicalSegmentElectronicKeyFormat4(message, electronic_key);
+            /* logical electronic key found */
+            connection_object->electronic_key.segment_type = 0x34;
+            connection_object->electronic_key.key_format = 0x04;                              //ELECTRONIC_KEY_SEGMENT_KEY_FORMAT_4;
+            connection_object->electronic_key.key_data.vendor_id =
+              ElectronicKeyFormat4GetVendorId(electronic_key);
+            connection_object->electronic_key.key_data.device_type =
+              ElectronicKeyFormat4GetDeviceType(electronic_key);
+            connection_object->electronic_key.key_data.product_code =
+              ElectronicKeyFormat4GetProductCode(electronic_key);
+            connection_object->electronic_key.key_data.major_revision =
+              ElectronicKeyFormat4GetMajorRevision(electronic_key);
+            if( true ==
+                ElectronicKeyFormat4GetMajorRevisionCompatibility(
+                  electronic_key) )
+            {
+              connection_object->electronic_key.key_data.major_revision =
+                connection_object->electronic_key.key_data.major_revision |
+                0x80;
+            }
+            connection_object->electronic_key.key_data.minor_revision =
+              ElectronicKeyFormat4GetMinorRevision(electronic_key);
+            ElectronicKeyFormat4Delete(&electronic_key);
+            message += 10;
+            remaining_path_size -= 5;                                                /*length of the electronic key*/
+            OPENER_TRACE_INFO(
+              "key: ven ID %d, dev type %d, prod code %d, major %d, minor %d\n",
+              connection_object->electronic_key.key_data.vendor_id,
+              connection_object->electronic_key.key_data.device_type,
+              connection_object->electronic_key.key_data.product_code,
+              connection_object->electronic_key.key_data.major_revision,
+              connection_object->electronic_key.key_data.minor_revision);
+          }
+
+        }
+      }
+    }
+
     if (*message == 0x34) {
       if (remaining_path_size < 5) {
         /*there is not enough data for holding the electronic key segment*/
@@ -1228,28 +1273,7 @@ EipUint8 ParseConnectionPath(ConnectionObject *connection_object,
         return kCipErrorNotEnoughData;
       }
 
-      /* logical electronic key found */
-      connection_object->electronic_key.segment_type = 0x34;
-      message++;
-      connection_object->electronic_key.key_format = *message++;
-      connection_object->electronic_key.key_data.vendor_id =
-        GetIntFromMessage(&message);
-      connection_object->electronic_key.key_data.device_type =
-        GetIntFromMessage(&message);
-      connection_object->electronic_key.key_data.product_code =
-        GetIntFromMessage(&message);
-      connection_object->electronic_key.key_data.major_revision =
-        *message++;
-      connection_object->electronic_key.key_data.minor_revision =
-        *message++;
-      remaining_path_size -= 5;                   /*length of the electronic key*/
-      OPENER_TRACE_INFO(
-        "key: ven ID %d, dev type %d, prod code %d, major %d, minor %d\n",
-        connection_object->electronic_key.key_data.vendor_id,
-        connection_object->electronic_key.key_data.device_type,
-        connection_object->electronic_key.key_data.product_code,
-        connection_object->electronic_key.key_data.major_revision,
-        connection_object->electronic_key.key_data.minor_revision);
+
 
       if ( kEipStatusOk
            != CheckElectronicKeyData(
@@ -1422,6 +1446,8 @@ EipUint8 ParseConnectionPath(ConnectionObject *connection_object,
                 remaining_path_size -= (g_config_data_length + 2);
                 message += (g_config_data_length + 2);
                 break;
+              default: OPENER_TRACE_ERR("Not allowed in connection manager");
+                break;
             }
           }
           break;
@@ -1443,6 +1469,8 @@ EipUint8 ParseConnectionPath(ConnectionObject *connection_object,
                     - remaining_path_size;                                                             /*offset in 16Bit words where within the connection path the error happend*/
                   return kCipErrorPathSegmentError;                               /*status code for invalid segment type*/
                 }
+              default: OPENER_TRACE_ERR("Not allowed in connection manager");
+                break;
             }
           }
           break;

+ 12 - 6
source/src/cip/cipelectronickey.h

@@ -52,8 +52,10 @@ CipUint ElectronicKeyFormat4GetVendorId(
  *	@param device_type The device type which shall be inserted into the electronic key
  *	@param electronic_key A format 4 electronic key
  */
-void ElectronicKeyFormat4SetDeviceType(const CipUint device_type,
-                                       ElectronicKeyFormat4 *const electronic_key);
+void ElectronicKeyFormat4SetDeviceType(
+  const CipUint device_type,
+  ElectronicKeyFormat4 *const
+  electronic_key);
 
 /** @brief Gets the device type from a format 4 electronic key
  *
@@ -68,8 +70,10 @@ CipUint ElectronicKeyFormat4GetDeviceType(
  *      @param product_code The product code to be inserted
  *      @param electronic_key The electronic key to be modified
  */
-void ElectronicKeyFormat4SetProductCode(const CipUint product_code,
-                                        ElectronicKeyFormat4 *const electronic_key);
+void ElectronicKeyFormat4SetProductCode(
+  const CipUint product_code,
+  ElectronicKeyFormat4 *const
+  electronic_key);
 
 /** @brief Gets the product code from an format 4 electronic key
  *
@@ -109,8 +113,10 @@ bool ElectronicKeyFormat4GetMajorRevisionCompatibility(
  *      @param minor_revision The minor revision to be set in the electronic key
  *      @param electronic_key The electronic key to be modified
  */
-void ElectronicKeyFormat4SetMinorRevision(const CipUsint minor_revision,
-                                          ElectronicKeyFormat4 *const electronic_key);
+void ElectronicKeyFormat4SetMinorRevision(
+  const CipUsint minor_revision,
+  ElectronicKeyFormat4 *const
+  electronic_key);
 
 /** @brief Gets the minor revision from an format 4 electronic key
  *

+ 299 - 225
source/src/cip/cipioconnection.c

@@ -39,20 +39,21 @@ const int kOpenerEipIoUdpPort = 0x08AE;
  * application connection types.
  */
 EipStatus OpenProducingMulticastConnection(
-    ConnectionObject *connection_object,
-    CipCommonPacketFormatData *common_packet_format_data);
+  ConnectionObject *connection_object,
+  CipCommonPacketFormatData *common_packet_format_data);
 
 EipStatus OpenMulticastConnection(
-    UdpCommuncationDirection direction, ConnectionObject *connection_object,
-    CipCommonPacketFormatData *common_packet_format_data);
+  UdpCommuncationDirection direction,
+  ConnectionObject *connection_object,
+  CipCommonPacketFormatData *common_packet_format_data);
 
 EipStatus OpenConsumingPointToPointConnection(
-    ConnectionObject *const connection_object,
-    CipCommonPacketFormatData *const common_packet_format_data);
+  ConnectionObject *const connection_object,
+  CipCommonPacketFormatData *const common_packet_format_data);
 
 EipStatus OpenProducingPointToPointConnection(
-    ConnectionObject *connection_object,
-    CipCommonPacketFormatData *common_packet_format_data);
+  ConnectionObject *connection_object,
+  CipCommonPacketFormatData *common_packet_format_data);
 
 EipUint16 HandleConfigData(ConnectionObject *connection_object);
 
@@ -72,7 +73,8 @@ void HandleIoConnectionTimeOut(ConnectionObject *connection_object);
 EipStatus SendConnectedData(ConnectionObject *connection_object);
 
 EipStatus HandleReceivedIoConnectionData(ConnectionObject *connection_object,
-                                         const EipUint8 *data, EipUint16 data_length);
+                                         const EipUint8 *data,
+                                         EipUint16 data_length);
 
 /**** Global variables ****/
 EipUint8 *g_config_data_buffer = NULL; /**< buffers for the config data coming with a forward open request. */
@@ -80,20 +82,21 @@ unsigned int g_config_data_length = 0; /**< length of g_config_data_buffer. Init
 
 EipUint32 g_run_idle_state; /**< buffer for holding the run idle information. */
 
-EipUint16 ProcessProductionInhibitTime(ConnectionObject* io_connection_object) {
-  if (kProductionTriggerCyclic
-      == GetProductionTrigger(io_connection_object)) {
-    if (256 == GetProductionInhibitTime(io_connection_object)) {
+EipUint16 ProcessProductionInhibitTime(ConnectionObject *io_connection_object)
+{
+  if ( kProductionTriggerCyclic
+       == GetProductionTrigger(io_connection_object) ) {
+    if ( 256 == GetProductionInhibitTime(io_connection_object) ) {
       /* there was no PIT segment in the connection path; set PIT to one fourth of RPI */
       SetProductionInhibitTime(
-          GetTargetToOriginatorRequestedPackedInterval(io_connection_object)
-              / 4000,
-          io_connection_object);
+        GetTargetToOriginatorRequestedPackedInterval(io_connection_object)
+        / 4000,
+        io_connection_object);
     } else {
       /* If a production inhibit time is provided, it needs to be smaller than the Requested Packet Interval */
-      if (GetProductionInhibitTime(io_connection_object)
-          > (GetTargetToOriginatorRequestedPackedInterval(io_connection_object)
-              / 1000)) {
+      if ( GetProductionInhibitTime(io_connection_object)
+           > (GetTargetToOriginatorRequestedPackedInterval(io_connection_object)
+              / 1000) ) {
         /* see section C-1.4.3.3 */
         return 0x111; /**< RPI not supported. Extended Error code deprecated */
       }
@@ -102,10 +105,12 @@ EipUint16 ProcessProductionInhibitTime(ConnectionObject* io_connection_object) {
   return kConnectionManagerExtendedStatusCodeSuccess;
 }
 
-CipConnectionObjectTransportClass GetConnectionTransportClass(const ConnectionObject *const connection_object) {
+CipConnectionObjectTransportClass GetConnectionTransportClass(
+  const ConnectionObject *const connection_object) {
   const unsigned int kTransportClassMask = 0x0F;
 
-  switch(connection_object->transport_type_class_trigger & kTransportClassMask) {
+  switch(connection_object->transport_type_class_trigger &
+         kTransportClassMask) {
     case 0: return kCipConnectionObjectTransportClass0;
     case 1: return kCipConnectionObjectTransportClass1;
     case 2: return kCipConnectionObjectTransportClass2;
@@ -122,52 +127,55 @@ void SetIoConnectionCallbacks(ConnectionObject *const io_connection_object) {
   io_connection_object->connection_timeout_function = HandleIoConnectionTimeOut;
   io_connection_object->connection_send_data_function = SendConnectedData;
   io_connection_object->connection_receive_data_function =
-      HandleReceivedIoConnectionData;
+    HandleReceivedIoConnectionData;
 }
 
 EipUint16 SetupIoConnectionOriginatorToTargetConnectionPoint(
-    ConnectionObject *const io_connection_object,
-    ConnectionObject *const restrict connection_object) {
-  CipClass* const assembly_class = GetCipClass(kCipAssemblyClassCode);
-  CipInstance* instance = NULL;
-  if (NULL
-      != (instance =
-          GetCipInstance(
-              assembly_class,
-              io_connection_object->connection_path.connection_point[kConnectionPointConsumer]))) {
+  ConnectionObject *const io_connection_object,
+  ConnectionObject *const restrict connection_object)
+{
+  CipClass *const assembly_class = GetCipClass(kCipAssemblyClassCode);
+  CipInstance *instance = NULL;
+  if ( NULL
+       != ( instance =
+              GetCipInstance(
+                assembly_class,
+                io_connection_object->connection_path.connection_point[
+                  kConnectionPointConsumer]) ) ) {
     /* consuming Connection Point is present */
     io_connection_object->consuming_instance = instance;
     io_connection_object->consumed_connection_path_length = 6;
     io_connection_object->consumed_connection_path.path_size = 6;
     io_connection_object->consumed_connection_path.class_id =
-        io_connection_object->connection_path.class_id;
+      io_connection_object->connection_path.class_id;
     io_connection_object->consumed_connection_path.instance_number =
-        io_connection_object->connection_path.connection_point[kConnectionPointConsumer];
+      io_connection_object->connection_path.connection_point[
+        kConnectionPointConsumer];
     io_connection_object->consumed_connection_path.attribute_number = 3;
     int data_size = io_connection_object->consumed_connection_size;
     int diff_size = 0;
 
     /* an assembly object should always have an attribute 3 */
-    CipAttributeStruct* attribute = GetCipAttribute(instance, 3);
+    CipAttributeStruct *attribute = GetCipAttribute(instance, 3);
     OPENER_ASSERT(attribute != NULL);
-    int is_heartbeat = (((CipByteArray*) attribute->data)->length == 0);
-    if (kCipConnectionObjectTransportClass1
-        == GetConnectionTransportClass(io_connection_object)) {
+    int is_heartbeat = ( ( (CipByteArray *) attribute->data )->length == 0 );
+    if ( kCipConnectionObjectTransportClass1
+         == GetConnectionTransportClass(io_connection_object) ) {
       //if ((io_connection_object->transport_type_class_trigger & 0x0F) == 1) {
       /* class 1 connection */
       data_size -= 2; /* remove 16-bit sequence count length */
       diff_size += 2;
     }
-    if ((kOpenerConsumedDataHasRunIdleHeader) && (data_size > 0)
-        && (!is_heartbeat)) {
+    if ( (kOpenerConsumedDataHasRunIdleHeader) && (data_size > 0)
+         && (!is_heartbeat) ) {
       /* we only have an run idle header if it is not an heartbeat connection */
       data_size -= 4; /* remove the 4 bytes needed for run/idle header */
       diff_size += 4;
     }
-    if (((CipByteArray*) attribute->data)->length != data_size) {
+    if ( ( (CipByteArray *) attribute->data )->length != data_size ) {
       /*wrong connection size */
       connection_object->correct_originator_to_target_size =
-          ((CipByteArray*) attribute->data)->length + diff_size;
+        ( (CipByteArray *) attribute->data )->length + diff_size;
       return kConnectionManagerExtendedStatusCodeErrorInvalidOToTConnectionSize;
     }
   } else {
@@ -177,45 +185,49 @@ EipUint16 SetupIoConnectionOriginatorToTargetConnectionPoint(
 }
 
 EipUint16 SetupIoConnectionTargetToOriginatorConnectionPoint(
-    ConnectionObject *const io_connection_object,
-    ConnectionObject *const restrict connection_object) {
+  ConnectionObject *const io_connection_object,
+  ConnectionObject *const restrict connection_object)
+{
   /*setup producer side*/
-  CipClass* const assembly_class = GetCipClass(kCipAssemblyClassCode);
-  CipInstance* instance = NULL;
-  if (0
-      != (instance =
-          GetCipInstance(
-              assembly_class,
-              io_connection_object->connection_path.connection_point[kConnectionPointProducer]))) {
+  CipClass *const assembly_class = GetCipClass(kCipAssemblyClassCode);
+  CipInstance *instance = NULL;
+  if ( 0
+       != ( instance =
+              GetCipInstance(
+                assembly_class,
+                io_connection_object->connection_path.connection_point[
+                  kConnectionPointProducer]) ) ) {
     io_connection_object->producing_instance = instance;
     io_connection_object->produced_connection_path_length = 6;
     io_connection_object->produced_connection_path.path_size = 6;
     io_connection_object->produced_connection_path.class_id =
-        io_connection_object->connection_path.class_id;
+      io_connection_object->connection_path.class_id;
     io_connection_object->produced_connection_path.instance_number =
-        io_connection_object->connection_path.connection_point[kConnectionPointProducer];
+      io_connection_object->connection_path.connection_point[
+        kConnectionPointProducer];
     io_connection_object->produced_connection_path.attribute_number = 3;
     int data_size = io_connection_object->produced_connection_size;
     int diff_size = 0;
     /* an assembly object should always have an attribute 3 */
-    CipAttributeStruct* attribute = GetCipAttribute(instance, 3);
+    CipAttributeStruct *attribute = GetCipAttribute(instance, 3);
     OPENER_ASSERT(attribute != NULL);
-    int is_heartbeat = (((CipByteArray*) attribute->data)->length == 0);
-    if (kCipConnectionObjectTransportClass1 == GetConnectionTransportClass(io_connection_object)) {
+    int is_heartbeat = ( ( (CipByteArray *) attribute->data )->length == 0 );
+    if ( kCipConnectionObjectTransportClass1 ==
+         GetConnectionTransportClass(io_connection_object) ) {
       /* class 1 connection */
       data_size -= 2; /* remove 16-bit sequence count length */
       diff_size += 2;
     }
-    if ((kOpenerProducedDataHasRunIdleHeader) && (data_size > 0)
-        && (!is_heartbeat)) {
+    if ( (kOpenerProducedDataHasRunIdleHeader) && (data_size > 0)
+         && (!is_heartbeat) ) {
       /* we only have an run idle header if it is not an heartbeat connection */
       data_size -= 4; /* remove the 4 bytes needed for run/idle header */
       diff_size += 4;
     }
-    if (((CipByteArray*) attribute->data)->length != data_size) {
+    if ( ( (CipByteArray *) attribute->data )->length != data_size ) {
       /*wrong connection size*/
       connection_object->correct_target_to_originator_size =
-          ((CipByteArray*) attribute->data)->length + diff_size;
+        ( (CipByteArray *) attribute->data )->length + diff_size;
       return kConnectionManagerExtendedStatusCodeErrorInvalidTToOConnectionSize;
     }
   } else {
@@ -235,11 +247,15 @@ EipUint16 SetupIoConnectionTargetToOriginatorConnectionPoint(
  *    - kEipStatusOk ... on success
  *    - On an error the general status code to be put into the response
  */
-EipStatus EstablishIoConnection(ConnectionObject *restrict const connection_object,
-                         EipUint16 *const extended_error) {
+EipStatus EstablishIoConnection(
+  ConnectionObject *restrict const connection_object,
+  EipUint16 *const extended_error)
+{
   EipStatus eip_status = kEipStatusOk;
 
-  ConnectionObject *io_connection_object = GetIoConnectionForConnectionData(connection_object, extended_error);
+  ConnectionObject *io_connection_object = GetIoConnectionForConnectionData(
+    connection_object,
+    extended_error);
   if(NULL == io_connection_object) {
     return kCipErrorConnectionFailure;
   }
@@ -253,14 +269,20 @@ EipStatus EstablishIoConnection(ConnectionObject *restrict const connection_obje
 
   GeneralConnectionConfiguration(io_connection_object);
 
-  ForwardOpenConnectionType originator_to_target_connection_type = GetConnectionType(io_connection_object
-      ->o_to_t_network_connection_parameter);
-  ForwardOpenConnectionType target_to_originator_connection_type = GetConnectionType(io_connection_object
-      ->t_to_o_network_connection_parameter);
+  ForwardOpenConnectionType originator_to_target_connection_type =
+    GetConnectionType(io_connection_object
+                      ->
+                      o_to_t_network_connection_parameter);
+  ForwardOpenConnectionType target_to_originator_connection_type =
+    GetConnectionType(io_connection_object
+                      ->
+                      t_to_o_network_connection_parameter);
 
   /** Already handled by forward open */
-  OPENER_ASSERT(!(originator_to_target_connection_type == kForwardOpenConnectionTypeNull &&
-                target_to_originator_connection_type == kForwardOpenConnectionTypeNull));
+  OPENER_ASSERT( !(originator_to_target_connection_type ==
+                   kForwardOpenConnectionTypeNull &&
+                   target_to_originator_connection_type ==
+                   kForwardOpenConnectionTypeNull) );
 
   io_connection_object->consuming_instance = NULL;
   io_connection_object->consumed_connection_path_length = 0;
@@ -271,7 +293,9 @@ EipStatus EstablishIoConnection(ConnectionObject *restrict const connection_obje
   /* we don't need to check for zero as this is handled in the connection path parsing */
 
   if (originator_to_target_connection_type != kForwardOpenConnectionTypeNull) { /*setup consumer side*/
-    *extended_error = SetupIoConnectionOriginatorToTargetConnectionPoint(io_connection_object, connection_object);
+    *extended_error = SetupIoConnectionOriginatorToTargetConnectionPoint(
+      io_connection_object,
+      connection_object);
     if (kConnectionManagerExtendedStatusCodeSuccess != *extended_error) {
       return kCipErrorConnectionFailure;
     }
@@ -279,8 +303,9 @@ EipStatus EstablishIoConnection(ConnectionObject *restrict const connection_obje
 
 
   if (target_to_originator_connection_type != kForwardOpenConnectionTypeNull) { /*setup producer side*/
-    *extended_error = SetupIoConnectionTargetToOriginatorConnectionPoint(io_connection_object,
-                                                       connection_object);
+    *extended_error = SetupIoConnectionTargetToOriginatorConnectionPoint(
+      io_connection_object,
+      connection_object);
     if (kConnectionManagerExtendedStatusCodeSuccess != *extended_error) {
       return kCipErrorConnectionFailure;
     }
@@ -302,9 +327,9 @@ EipStatus EstablishIoConnection(ConnectionObject *restrict const connection_obje
 
   AddNewActiveConnection(io_connection_object);
   CheckIoConnectionEvent(
-      io_connection_object->connection_path.connection_point[0],
-      io_connection_object->connection_path.connection_point[1],
-      kIoConnectionEventOpened);
+    io_connection_object->connection_path.connection_point[0],
+    io_connection_object->connection_path.connection_point[1],
+    kIoConnectionEventOpened);
   return eip_status;
 }
 
@@ -315,8 +340,9 @@ EipStatus EstablishIoConnection(ConnectionObject *restrict const connection_obje
  * @return kEipStatusOk on success, otherwise kEipStatusError
  */
 EipStatus OpenConsumingPointToPointConnection(
-    ConnectionObject *const connection_object,
-    CipCommonPacketFormatData *const common_packet_format_data) {
+  ConnectionObject *const connection_object,
+  CipCommonPacketFormatData *const common_packet_format_data)
+{
   /*static EIP_UINT16 nUDPPort = 2222; TODO think on improving the udp port assigment for point to point connections */
   int j = 0;
 
@@ -326,12 +352,14 @@ EipStatus OpenConsumingPointToPointConnection(
     j = 1;
   }
 
-  struct sockaddr_in addr = { .sin_family = PF_INET, .sin_addr.s_addr = INADDR_ANY, .sin_port = htons(kOpenerEipIoUdpPort) };
+  struct sockaddr_in addr =
+  { .sin_family = PF_INET, .sin_addr.s_addr = INADDR_ANY, .sin_port = htons(
+      kOpenerEipIoUdpPort) };
 
   int socket = CreateUdpSocket(kUdpCommuncationDirectionConsuming, &addr); /* the address is only needed for bind used if consuming */
   if (socket == kEipInvalidSocket) {
     OPENER_TRACE_ERR(
-        "cannot create UDP socket in OpenPointToPointConnection\n");
+      "cannot create UDP socket in OpenPointToPointConnection\n");
     return kEipStatusError;
   }
 
@@ -341,12 +369,12 @@ EipStatus OpenConsumingPointToPointConnection(
 
   common_packet_format_data->address_info_item[j].length = 16;
   common_packet_format_data->address_info_item[j].type_id =
-      kCipItemIdSocketAddressInfoOriginatorToTarget;
+    kCipItemIdSocketAddressInfoOriginatorToTarget;
 
   common_packet_format_data->address_info_item[j].sin_port = addr.sin_port;
   /*TODO should we add our own address here? */
   common_packet_format_data->address_info_item[j].sin_addr = addr.sin_addr
-      .s_addr;
+                                                             .s_addr;
   memset(common_packet_format_data->address_info_item[j].nasin_zero, 0, 8);
   common_packet_format_data->address_info_item[j].sin_family = htons(AF_INET);
 
@@ -354,8 +382,9 @@ EipStatus OpenConsumingPointToPointConnection(
 }
 
 EipStatus OpenProducingPointToPointConnection(
-    ConnectionObject *connection_object,
-    CipCommonPacketFormatData *common_packet_format_data) {
+  ConnectionObject *connection_object,
+  CipCommonPacketFormatData *common_packet_format_data)
+{
   in_port_t port = htons(kOpenerEipIoUdpPort); /* the default port to be used if no port information is part of the forward open request */
 
   if (kCipItemIdSocketAddressInfoTargetToOriginator
@@ -373,10 +402,10 @@ EipStatus OpenProducingPointToPointConnection(
   connection_object->remote_address.sin_port = port;
 
   int socket = CreateUdpSocket(kUdpCommuncationDirectionProducing,
-                           &connection_object->remote_address); /* the address is only needed for bind used if consuming */
+                               &connection_object->remote_address); /* the address is only needed for bind used if consuming */
   if (socket == kEipInvalidSocket) {
     OPENER_TRACE_ERR(
-        "cannot create UDP socket in OpenPointToPointConnection\n");
+      "cannot create UDP socket in OpenPointToPointConnection\n");
     /* *pa_pnExtendedError = 0x0315; miscellaneous*/
     return kCipErrorConnectionFailure;
   }
@@ -386,11 +415,12 @@ EipStatus OpenProducingPointToPointConnection(
 }
 
 EipStatus OpenProducingMulticastConnection(
-    ConnectionObject *connection_object,
-    CipCommonPacketFormatData *common_packet_format_data) {
+  ConnectionObject *connection_object,
+  CipCommonPacketFormatData *common_packet_format_data)
+{
   ConnectionObject *existing_connection_object =
-      GetExistingProducerMulticastConnection(
-          connection_object->connection_path.connection_point[1]);
+    GetExistingProducerMulticastConnection(
+      connection_object->connection_path.connection_point[1]);
 
   if (NULL == existing_connection_object) { /* we are the first connection producing for the given Input Assembly */
     return OpenMulticastConnection(kUdpCommuncationDirectionProducing,
@@ -398,7 +428,8 @@ EipStatus OpenProducingMulticastConnection(
   } else {
     /* we need to inform our originator on the correct connection id */
     connection_object->cip_produced_connection_id = existing_connection_object
-        ->cip_produced_connection_id;
+                                                    ->
+                                                    cip_produced_connection_id;
   }
 
   /* we have a connection reuse the data and the socket */
@@ -407,7 +438,7 @@ EipStatus OpenProducingMulticastConnection(
   if (g_common_packet_format_data_item.address_info_item[0].type_id == 0) { /* it is not used yet */
     j = 0;
   } else if (g_common_packet_format_data_item.address_info_item[1].type_id
-      == 0) {
+             == 0) {
     j = 1;
   }
 
@@ -416,23 +447,27 @@ EipStatus OpenProducingMulticastConnection(
      * especially in the case of time outs.
      */
     connection_object->socket[kUdpCommuncationDirectionProducing] =
-        existing_connection_object->socket[kUdpCommuncationDirectionProducing];
+      existing_connection_object->socket[kUdpCommuncationDirectionProducing];
     existing_connection_object->socket[kUdpCommuncationDirectionProducing] =
-        kEipInvalidSocket;
+      kEipInvalidSocket;
   } else { /* this connection will not produce the data */
     connection_object->socket[kUdpCommuncationDirectionProducing] =
-        kEipInvalidSocket;
+      kEipInvalidSocket;
   }
 
   common_packet_format_data->address_info_item[j].length = 16;
   common_packet_format_data->address_info_item[j].type_id =
-      kCipItemIdSocketAddressInfoTargetToOriginator;
+    kCipItemIdSocketAddressInfoTargetToOriginator;
   connection_object->remote_address.sin_family = AF_INET;
   connection_object->remote_address.sin_port = common_packet_format_data
-      ->address_info_item[j].sin_port = htons(kOpenerEipIoUdpPort);
+                                               ->address_info_item[j].sin_port
+                                                 = htons(kOpenerEipIoUdpPort);
   connection_object->remote_address.sin_addr.s_addr = common_packet_format_data
-      ->address_info_item[j].sin_addr = g_multicast_configuration
-      .starting_multicast_address;
+                                                      ->address_info_item[j].
+                                                      sin_addr =
+                                                        g_multicast_configuration
+                                                        .
+                                                        starting_multicast_address;
   memset(common_packet_format_data->address_info_item[j].nasin_zero, 0, 8);
   common_packet_format_data->address_info_item[j].sin_family = htons(AF_INET);
 
@@ -447,58 +482,65 @@ EipStatus OpenProducingMulticastConnection(
  * @return kEipStatusOk on success, otherwise kEipStatusError
  */
 EipStatus OpenMulticastConnection(
-    UdpCommuncationDirection direction, ConnectionObject *connection_object,
-    CipCommonPacketFormatData *common_packet_format_data) {
+  UdpCommuncationDirection direction,
+  ConnectionObject *connection_object,
+  CipCommonPacketFormatData *common_packet_format_data)
+{
   int j = -1;
 
   int address_info_item_which_contains_o_to_t = -1;
   int address_info_item_which_contains_t_to_o = -1;
 
   if(kCipItemIdSocketAddressInfoOriginatorToTarget
-            == common_packet_format_data->address_info_item[0].type_id) {
-	  address_info_item_which_contains_o_to_t = 0;
+     == common_packet_format_data->address_info_item[0].type_id) {
+    address_info_item_which_contains_o_to_t = 0;
   } else if(kCipItemIdSocketAddressInfoOriginatorToTarget
-              == common_packet_format_data->address_info_item[1].type_id) {
-	  address_info_item_which_contains_o_to_t = 1;
+            == common_packet_format_data->address_info_item[1].type_id) {
+    address_info_item_which_contains_o_to_t = 1;
   } else {
-	  OPENER_TRACE_INFO("No O->T Sockaddr info available\n");
+    OPENER_TRACE_INFO("No O->T Sockaddr info available\n");
   }
 
   if(kCipItemIdSocketAddressInfoTargetToOriginator
-              == common_packet_format_data->address_info_item[0].type_id) {
-  	  address_info_item_which_contains_t_to_o = 0;
-    } else if(kCipItemIdSocketAddressInfoTargetToOriginator
-                == common_packet_format_data->address_info_item[1].type_id) {
-  	  address_info_item_which_contains_t_to_o = 1;
-    } else {
-    	OPENER_TRACE_INFO("No T->O Sockaddr info available\n");
-    }
+     == common_packet_format_data->address_info_item[0].type_id) {
+    address_info_item_which_contains_t_to_o = 0;
+  } else if(kCipItemIdSocketAddressInfoTargetToOriginator
+            == common_packet_format_data->address_info_item[1].type_id) {
+    address_info_item_which_contains_t_to_o = 1;
+  } else {
+    OPENER_TRACE_INFO("No T->O Sockaddr info available\n");
+  }
 
   if(kUdpCommuncationDirectionConsuming == direction) {
-	  //OPENER_ASSERT(-1 != address_info_item_which_contains_o_to_t);
-	  j = address_info_item_which_contains_o_to_t;
+    //OPENER_ASSERT(-1 != address_info_item_which_contains_o_to_t);
+    j = address_info_item_which_contains_o_to_t;
   }
 
   if(kUdpCommuncationDirectionProducing == direction) {
-  	  //OPENER_ASSERT(-1 != address_info_item_which_contains_o_to_t);
-  	  j = address_info_item_which_contains_t_to_o;
-    }
+    //OPENER_ASSERT(-1 != address_info_item_which_contains_o_to_t);
+    j = address_info_item_which_contains_t_to_o;
+  }
 
   /*****************/
 
   if (-1 == j) {
-	  OPENER_TRACE_ERR("no suitable addr info item available / O->T: %d, T->O: %d, Selector: %d, direction: %d\n",
-			  address_info_item_which_contains_o_to_t,address_info_item_which_contains_t_to_o,j,direction);
-	          return kEipStatusError;
+    OPENER_TRACE_ERR(
+      "no suitable addr info item available / O->T: %d, T->O: %d, Selector: %d, direction: %d\n",
+      address_info_item_which_contains_o_to_t,
+      address_info_item_which_contains_t_to_o,
+      j,
+      direction);
+    return kEipStatusError;
   }
 
-  if (kCipItemIdSocketAddressInfoTargetToOriginator == common_packet_format_data->address_info_item[j].type_id) { /* we are using an unused item initialize it with the default multicast address */
+  if (kCipItemIdSocketAddressInfoTargetToOriginator ==
+      common_packet_format_data->address_info_item[j].type_id) {                                                  /* we are using an unused item initialize it with the default multicast address */
     common_packet_format_data->address_info_item[j].sin_family = htons(
-        AF_INET);
+      AF_INET);
     common_packet_format_data->address_info_item[j].sin_port = htons(
-        kOpenerEipIoUdpPort);
+      kOpenerEipIoUdpPort);
     common_packet_format_data->address_info_item[j].sin_addr =
-        g_multicast_configuration.starting_multicast_address;
+      g_multicast_configuration.starting_multicast_address;
     memset(common_packet_format_data->address_info_item[j].nasin_zero, 0, 8);
     common_packet_format_data->address_info_item[j].length = 16;
   }
@@ -506,18 +548,18 @@ EipStatus OpenMulticastConnection(
   if (htons(AF_INET)
       != common_packet_format_data->address_info_item[j].sin_family) {
     OPENER_TRACE_ERR(
-        "Sockaddr Info Item with wrong sin family value received\n");
+      "Sockaddr Info Item with wrong sin family value received\n");
     return kEipStatusError;
   }
 
   /* allocate an unused sockaddr struct to use */
   struct sockaddr_in socket_address = {0};
   socket_address.sin_family = ntohs(
-      common_packet_format_data->address_info_item[j].sin_family);
+    common_packet_format_data->address_info_item[j].sin_family);
   socket_address.sin_addr.s_addr =
-      common_packet_format_data->address_info_item[j].sin_addr;
+    common_packet_format_data->address_info_item[j].sin_addr;
   socket_address.sin_port = common_packet_format_data->address_info_item[j]
-      .sin_port;
+                            .sin_port;
 
   int socket = CreateUdpSocket(direction, &socket_address); /* the address is only needed for bind used if consuming */
   if (socket == kEipInvalidSocket) {
@@ -528,11 +570,11 @@ EipStatus OpenMulticastConnection(
 
   if (direction == kUdpCommuncationDirectionConsuming) {
     common_packet_format_data->address_info_item[j].type_id =
-        kCipItemIdSocketAddressInfoOriginatorToTarget;
+      kCipItemIdSocketAddressInfoOriginatorToTarget;
     connection_object->originator_address = socket_address;
   } else {
     common_packet_format_data->address_info_item[j].type_id =
-        kCipItemIdSocketAddressInfoTargetToOriginator;
+      kCipItemIdSocketAddressInfoTargetToOriginator;
     connection_object->remote_address = socket_address;
   }
 
@@ -541,37 +583,45 @@ EipStatus OpenMulticastConnection(
 
 EipUint16 HandleConfigData(ConnectionObject *connection_object) {
 
-  CipClass* const assembly_class = GetCipClass(kCipAssemblyClassCode);
+  CipClass *const assembly_class = GetCipClass(kCipAssemblyClassCode);
   EipUint16 connection_manager_status = 0;
   CipInstance *config_instance = GetCipInstance(
-      assembly_class, connection_object->connection_path.connection_point[2]);
+    assembly_class, connection_object->connection_path.connection_point[2]);
 
   if (0 != g_config_data_length) {
-    if (ConnectionWithSameConfigPointExists(
-        connection_object->connection_path.connection_point[kConnectionPointConfig])) { /* there is a connected connection with the same config point
-         * we have to have the same data as already present in the config point*/
-      CipByteArray *attribute_three = (CipByteArray *) GetCipAttribute(config_instance, 3)
-          ->data;
+    if ( ConnectionWithSameConfigPointExists(
+           connection_object->connection_path.connection_point[
+             kConnectionPointConfig]) ) {                                               /* there is a connected connection with the same config point
+                                                                                         * we have to have the same data as already present in the config point*/
+      CipByteArray *attribute_three = (CipByteArray *) GetCipAttribute(
+        config_instance,
+        3)
+                                      ->data;
       if (attribute_three->length != g_config_data_length) {
         connection_manager_status =
-            kConnectionManagerExtendedStatusCodeErrorOwnershipConflict;
+          kConnectionManagerExtendedStatusCodeErrorOwnershipConflict;
+        OPENER_TRACE_INFO(
+          "Hit an Ownership conflict in cipioconnection.c occurrence 1");
       } else {
         /*FIXME check if this is correct */
-        if (memcmp(attribute_three->data, g_config_data_buffer, g_config_data_length)) {
+        if ( memcmp(attribute_three->data, g_config_data_buffer,
+                    g_config_data_length) ) {
           connection_manager_status =
-              kConnectionManagerExtendedStatusCodeErrorOwnershipConflict;
+            kConnectionManagerExtendedStatusCodeErrorOwnershipConflict;
+          OPENER_TRACE_INFO(
+            "Hit an Ownership conflict in cipioconnection.c occurrence 2");
         }
       }
     } else {
       /* put the data on the configuration assembly object with the current
-       design this can be done rather efficiently */
-      if (kEipStatusOk
-          != NotifyAssemblyConnectedDataReceived(config_instance,
-                                                 g_config_data_buffer,
-                                                 g_config_data_length)) {
+         design this can be done rather efficiently */
+      if ( kEipStatusOk
+           != NotifyAssemblyConnectedDataReceived(config_instance,
+                                                  g_config_data_buffer,
+                                                  g_config_data_length) ) {
         OPENER_TRACE_WARN("Configuration data was invalid\n");
         connection_manager_status =
-            kConnectionManagerExtendedStatusCodeInvalidConfigurationApplicationPath;
+          kConnectionManagerExtendedStatusCodeInvalidConfigurationApplicationPath;
       }
     }
   }
@@ -580,80 +630,89 @@ EipUint16 HandleConfigData(ConnectionObject *connection_object) {
 
 void CloseIoConnection(ConnectionObject *connection_object) {
 
-  CheckIoConnectionEvent(connection_object->connection_path.connection_point[kConnectionPointConsumer],
-                    connection_object->connection_path.connection_point[kConnectionPointProducer],
-                    kIoConnectionEventClosed);
-
-  if ((kConnectionTypeIoExclusiveOwner == connection_object->instance_type)
-      || (kConnectionTypeIoInputOnly == connection_object->instance_type)) {
-    if ((kForwardOpenConnectionTypeMulticastConnection
-        == (connection_object->t_to_o_network_connection_parameter
-            & kForwardOpenConnectionTypeMulticastConnection))
-        && (kEipInvalidSocket
-            != connection_object->socket[kUdpCommuncationDirectionProducing])) {
-      ConnectionObject *next_non_control_master_connection = GetNextNonControlMasterConnection(
-          connection_object->connection_path.connection_point[kConnectionPointProducer]);
+  CheckIoConnectionEvent(connection_object->connection_path.connection_point[
+                           kConnectionPointConsumer],
+                         connection_object->connection_path.connection_point[
+                           kConnectionPointProducer],
+                         kIoConnectionEventClosed);
+
+  if ( (kConnectionTypeIoExclusiveOwner == connection_object->instance_type)
+       || (kConnectionTypeIoInputOnly == connection_object->instance_type) ) {
+    if ( ( kForwardOpenConnectionTypeMulticastConnection
+           == (connection_object->t_to_o_network_connection_parameter
+               & kForwardOpenConnectionTypeMulticastConnection) )
+         && (kEipInvalidSocket
+             != connection_object->socket[kUdpCommuncationDirectionProducing]) )
+    {
+      ConnectionObject *next_non_control_master_connection =
+        GetNextNonControlMasterConnection(
+          connection_object->connection_path.connection_point[
+            kConnectionPointProducer]);
       if (NULL != next_non_control_master_connection) {
-        next_non_control_master_connection->socket[kUdpCommuncationDirectionProducing] =
-            connection_object->socket[kUdpCommuncationDirectionProducing];
-        memcpy(&(next_non_control_master_connection->remote_address),
-               &(connection_object->remote_address),
-               sizeof(next_non_control_master_connection->remote_address));
-        next_non_control_master_connection->eip_level_sequence_count_producing =
+        next_non_control_master_connection->socket[
+          kUdpCommuncationDirectionProducing] =
+          connection_object->socket[kUdpCommuncationDirectionProducing];
+        memcpy( &(next_non_control_master_connection->remote_address),
+                &(connection_object->remote_address),
+                sizeof(next_non_control_master_connection->remote_address) );
+        next_non_control_master_connection->eip_level_sequence_count_producing
+          =
             connection_object->eip_level_sequence_count_producing;
         next_non_control_master_connection->sequence_count_producing =
-            connection_object->sequence_count_producing;
+          connection_object->sequence_count_producing;
         connection_object->socket[kUdpCommuncationDirectionProducing] =
-            kEipInvalidSocket;
+          kEipInvalidSocket;
         next_non_control_master_connection->transmission_trigger_timer =
-            connection_object->transmission_trigger_timer;
+          connection_object->transmission_trigger_timer;
       } else { /* this was the last master connection close all listen only connections listening on the port */
         CloseAllConnectionsForInputWithSameType(
-            connection_object->connection_path.connection_point[kConnectionPointProducer],
-            kConnectionTypeIoListenOnly);
+          connection_object->connection_path.connection_point[
+            kConnectionPointProducer],
+          kConnectionTypeIoListenOnly);
       }
     }
   }
 
   CloseCommunicationChannelsAndRemoveFromActiveConnectionsList(
-      connection_object);
+    connection_object);
 }
 
 void HandleIoConnectionTimeOut(ConnectionObject *connection_object) {
   ConnectionObject *next_non_control_master_connection;
   CheckIoConnectionEvent(connection_object->connection_path.connection_point[0],
-                    connection_object->connection_path.connection_point[1],
-                    kIoConnectionEventTimedOut);
+                         connection_object->connection_path.connection_point[1],
+                         kIoConnectionEventTimedOut);
 
-  if (kForwardOpenConnectionTypeMulticastConnection
-      == (connection_object->t_to_o_network_connection_parameter
-          & kForwardOpenConnectionTypeMulticastConnection)) {
+  if ( kForwardOpenConnectionTypeMulticastConnection
+       == (connection_object->t_to_o_network_connection_parameter
+           & kForwardOpenConnectionTypeMulticastConnection) ) {
     switch (connection_object->instance_type) {
       case kConnectionTypeIoExclusiveOwner:
         CloseAllConnectionsForInputWithSameType(
-            connection_object->connection_path.connection_point[1],
-            kConnectionTypeIoInputOnly);
+          connection_object->connection_path.connection_point[1],
+          kConnectionTypeIoInputOnly);
         CloseAllConnectionsForInputWithSameType(
-            connection_object->connection_path.connection_point[1],
-            kConnectionTypeIoListenOnly);
+          connection_object->connection_path.connection_point[1],
+          kConnectionTypeIoListenOnly);
         break;
       case kConnectionTypeIoInputOnly:
         if (kEipInvalidSocket
             != connection_object->socket[kUdpCommuncationDirectionProducing]) { /* we are the controlling input only connection find a new controller*/
           next_non_control_master_connection =
-              GetNextNonControlMasterConnection(
-                  connection_object->connection_path.connection_point[1]);
+            GetNextNonControlMasterConnection(
+              connection_object->connection_path.connection_point[1]);
           if (NULL != next_non_control_master_connection) {
-            next_non_control_master_connection->socket[kUdpCommuncationDirectionProducing] =
-                connection_object->socket[kUdpCommuncationDirectionProducing];
+            next_non_control_master_connection->socket[
+              kUdpCommuncationDirectionProducing] =
+              connection_object->socket[kUdpCommuncationDirectionProducing];
             connection_object->socket[kUdpCommuncationDirectionProducing] =
-                kEipInvalidSocket;
+              kEipInvalidSocket;
             next_non_control_master_connection->transmission_trigger_timer =
-                connection_object->transmission_trigger_timer;
+              connection_object->transmission_trigger_timer;
           } else { /* this was the last master connection close all listen only connections listening on the port */
             CloseAllConnectionsForInputWithSameType(
-                connection_object->connection_path.connection_point[1],
-                kConnectionTypeIoListenOnly);
+              connection_object->connection_path.connection_point[1],
+              kConnectionTypeIoListenOnly);
           }
         }
         break;
@@ -670,33 +729,36 @@ EipStatus SendConnectedData(ConnectionObject *connection_object) {
 
   /* TODO think of adding an own send buffer to each connection object in order to preset up the whole message on connection opening and just change the variable data items e.g., sequence number */
 
-  CipCommonPacketFormatData *common_packet_format_data = &g_common_packet_format_data_item; /* TODO think on adding a CPF data item to the S_CIP_ConnectionObject in order to remove the code here or even better allocate memory in the connection object for storing the message to send and just change the application data*/
+  CipCommonPacketFormatData *common_packet_format_data =
+    &g_common_packet_format_data_item;                                                      /* TODO think on adding a CPF data item to the S_CIP_ConnectionObject in order to remove the code here or even better allocate memory in the connection object for storing the message to send and just change the application data*/
 
   connection_object->eip_level_sequence_count_producing++;
 
   /* assembleCPFData */
   common_packet_format_data->item_count = 2;
-  if ((connection_object->transport_type_class_trigger & 0x0F) != 0) { /* use Sequenced Address Items if not Connection Class 0 */
-    common_packet_format_data->address_item.type_id = kCipItemIdSequencedAddressItem;
+  if ( (connection_object->transport_type_class_trigger & 0x0F) != 0 ) { /* use Sequenced Address Items if not Connection Class 0 */
+    common_packet_format_data->address_item.type_id =
+      kCipItemIdSequencedAddressItem;
     common_packet_format_data->address_item.length = 8;
     common_packet_format_data->address_item.data.sequence_number =
-        connection_object->eip_level_sequence_count_producing;
+      connection_object->eip_level_sequence_count_producing;
   } else {
-    common_packet_format_data->address_item.type_id = kCipItemIdConnectionAddress;
+    common_packet_format_data->address_item.type_id =
+      kCipItemIdConnectionAddress;
     common_packet_format_data->address_item.length = 4;
 
   }
   common_packet_format_data->address_item.data.connection_identifier =
-      connection_object->cip_produced_connection_id;
+    connection_object->cip_produced_connection_id;
 
   common_packet_format_data->data_item.type_id = kCipItemIdConnectedDataItem;
 
   CipByteArray *producing_instance_attributes =
-      (CipByteArray *) connection_object->producing_instance->attributes->data;
+    (CipByteArray *) connection_object->producing_instance->attributes->data;
   common_packet_format_data->data_item.length = 0;
 
   /* notify the application that data will be sent immediately after the call */
-  if (BeforeAssemblyDataSend(connection_object->producing_instance)) {
+  if ( BeforeAssemblyDataSend(connection_object->producing_instance) ) {
     /* the data has changed increase sequence counter */
     connection_object->sequence_count_producing++;
   }
@@ -706,16 +768,17 @@ EipStatus SendConnectedData(ConnectionObject *connection_object) {
   common_packet_format_data->address_info_item[1].type_id = 0;
 
   EipUint16 reply_length = AssembleIOMessage(common_packet_format_data,
-                                   &g_message_data_reply_buffer[0]);
+                                             &g_message_data_reply_buffer[0]);
 
-  EipUint8 *message_data_reply_buffer = &g_message_data_reply_buffer[reply_length - 2];
+  EipUint8 *message_data_reply_buffer =
+    &g_message_data_reply_buffer[reply_length - 2];
   common_packet_format_data->data_item.length = producing_instance_attributes
-      ->length;
+                                                ->length;
   if (kOpenerProducedDataHasRunIdleHeader) {
     common_packet_format_data->data_item.length += 4;
   }
 
-  if ((connection_object->transport_type_class_trigger & 0x0F) == 1) {
+  if ( (connection_object->transport_type_class_trigger & 0x0F) == 1 ) {
     common_packet_format_data->data_item.length += 2;
     AddIntToMessage(common_packet_format_data->data_item.length,
                     &message_data_reply_buffer);
@@ -727,7 +790,7 @@ EipStatus SendConnectedData(ConnectionObject *connection_object) {
   }
 
   if (kOpenerProducedDataHasRunIdleHeader) {
-    AddDintToMessage(g_run_idle_state, &(message_data_reply_buffer));
+    AddDintToMessage( g_run_idle_state, &(message_data_reply_buffer) );
   }
 
   memcpy(message_data_reply_buffer, producing_instance_attributes->data,
@@ -736,19 +799,21 @@ EipStatus SendConnectedData(ConnectionObject *connection_object) {
   reply_length += common_packet_format_data->data_item.length;
 
   return SendUdpData(
-      &connection_object->remote_address,
-      connection_object->socket[kUdpCommuncationDirectionProducing],
-      &g_message_data_reply_buffer[0], reply_length);
+           &connection_object->remote_address,
+           connection_object->socket[kUdpCommuncationDirectionProducing],
+           &g_message_data_reply_buffer[0], reply_length);
 }
 
 EipStatus HandleReceivedIoConnectionData(ConnectionObject *connection_object,
-                                         const EipUint8 *data, EipUint16 data_length) {
+                                         const EipUint8 *data,
+                                         EipUint16 data_length)
+{
 
   /* check class 1 sequence number*/
-  if ((connection_object->transport_type_class_trigger & 0x0F) == 1) {
-    EipUint16 sequence_buffer = GetIntFromMessage(&(data));
-    if (SEQ_LEQ16(sequence_buffer,
-                  connection_object->sequence_count_consuming)) {
+  if ( (connection_object->transport_type_class_trigger & 0x0F) == 1 ) {
+    EipUint16 sequence_buffer = GetIntFromMessage( &(data) );
+    if ( SEQ_LEQ16(sequence_buffer,
+                   connection_object->sequence_count_consuming) ) {
       return kEipStatusOk; /* no new data for the assembly */
     }
     connection_object->sequence_count_consuming = sequence_buffer;
@@ -758,7 +823,7 @@ EipStatus HandleReceivedIoConnectionData(ConnectionObject *connection_object,
   if (data_length > 0) {
     /* we have no heartbeat connection */
     if (kOpenerConsumedDataHasRunIdleHeader) {
-      EipUint32 nRunIdleBuf = GetDintFromMessage(&(data));
+      EipUint32 nRunIdleBuf = GetDintFromMessage( &(data) );
       if (g_run_idle_state != nRunIdleBuf) {
         RunIdleChanged(nRunIdleBuf);
       }
@@ -767,7 +832,8 @@ EipStatus HandleReceivedIoConnectionData(ConnectionObject *connection_object,
     }
 
     if (NotifyAssemblyConnectedDataReceived(
-        connection_object->consuming_instance, (EipUint8 *const)data, data_length) != 0) {
+          connection_object->consuming_instance, (EipUint8 *const)data,
+          data_length) != 0) {
       return kEipStatusError;
     }
   }
@@ -779,16 +845,21 @@ EipStatus OpenCommunicationChannels(ConnectionObject *connection_object) {
   EipStatus eip_status = kEipStatusOk;
   /*get pointer to the CPF data, currently we have just one global instance of the struct. This may change in the future*/
   CipCommonPacketFormatData *common_packet_format_data =
-      &g_common_packet_format_data_item;
+    &g_common_packet_format_data_item;
 
-  ForwardOpenConnectionType originator_to_target_connection_type = GetConnectionType(connection_object
-      ->o_to_t_network_connection_parameter);
+  ForwardOpenConnectionType originator_to_target_connection_type =
+    GetConnectionType(connection_object
+                      ->
+                      o_to_t_network_connection_parameter);
 
-  ForwardOpenConnectionType target_to_originator_connection_type = GetConnectionType(connection_object
-      ->t_to_o_network_connection_parameter);
+  ForwardOpenConnectionType target_to_originator_connection_type =
+    GetConnectionType(connection_object
+                      ->
+                      t_to_o_network_connection_parameter);
 
   /* open a connection "point to point" or "multicast" based on the ConnectionParameter */
-  if (originator_to_target_connection_type == kForwardOpenConnectionTypeMulticastConnection) /* Multicast consuming */
+  if (originator_to_target_connection_type ==
+      kForwardOpenConnectionTypeMulticastConnection)                                         /* Multicast consuming */
   {
     if (OpenMulticastConnection(kUdpCommuncationDirectionConsuming,
                                 connection_object, common_packet_format_data)
@@ -796,7 +867,8 @@ EipStatus OpenCommunicationChannels(ConnectionObject *connection_object) {
       OPENER_TRACE_ERR("error in OpenMulticast Connection\n");
       return kCipErrorConnectionFailure;
     }
-  } else if (originator_to_target_connection_type == kForwardOpenConnectionTypePointToPointConnection) /* Point to Point consuming */
+  } else if (originator_to_target_connection_type ==
+             kForwardOpenConnectionTypePointToPointConnection)                                         /* Point to Point consuming */
   {
     if (OpenConsumingPointToPointConnection(connection_object,
                                             common_packet_format_data)
@@ -806,7 +878,8 @@ EipStatus OpenCommunicationChannels(ConnectionObject *connection_object) {
     }
   }
 
-  if (target_to_originator_connection_type == kForwardOpenConnectionTypeMulticastConnection) /* Multicast producing */
+  if (target_to_originator_connection_type ==
+      kForwardOpenConnectionTypeMulticastConnection)                                         /* Multicast producing */
   {
     if (OpenProducingMulticastConnection(connection_object,
                                          common_packet_format_data)
@@ -814,7 +887,8 @@ EipStatus OpenCommunicationChannels(ConnectionObject *connection_object) {
       OPENER_TRACE_ERR("error in OpenMulticast Connection\n");
       return kCipErrorConnectionFailure;
     }
-  } else if (target_to_originator_connection_type == kForwardOpenConnectionTypePointToPointConnection) /* Point to Point producing */
+  } else if (target_to_originator_connection_type ==
+             kForwardOpenConnectionTypePointToPointConnection)                                         /* Point to Point producing */
   {
 
     if (OpenProducingPointToPointConnection(connection_object,
@@ -828,15 +902,15 @@ EipStatus OpenCommunicationChannels(ConnectionObject *connection_object) {
 }
 
 void CloseCommunicationChannelsAndRemoveFromActiveConnectionsList(
-    ConnectionObject *connection_object) {
+  ConnectionObject *connection_object) {
   IApp_CloseSocket_udp(
-      connection_object->socket[kUdpCommuncationDirectionConsuming]);
+    connection_object->socket[kUdpCommuncationDirectionConsuming]);
   connection_object->socket[kUdpCommuncationDirectionConsuming] =
-      kEipInvalidSocket;
+    kEipInvalidSocket;
   IApp_CloseSocket_udp(
-      connection_object->socket[kUdpCommuncationDirectionProducing]);
+    connection_object->socket[kUdpCommuncationDirectionProducing]);
   connection_object->socket[kUdpCommuncationDirectionProducing] =
-      kEipInvalidSocket;
+    kEipInvalidSocket;
 
   RemoveFromActiveConnections(connection_object);
 }

+ 14 - 0
source/tests/cip/cipelectronickeytest.cpp

@@ -10,6 +10,7 @@
 
 extern "C" {
 
+#include "cipepath.h"
 #include "cipelectronickey.h"
 
 }
@@ -142,3 +143,16 @@ TEST(CipElectronicKey, GetMinorRevision) {
   CipUint product_code = ElectronicKeyFormat4GetMinorRevision(key);
   CHECK_EQUAL(expected_minor_revision, product_code);
 }
+
+TEST(CipElectronicKey, ParseElectronicKeyTest) {
+  /* Size of an electronic key is 1 + 1 + 8 (Segment, Key format, Key) */
+  const unsigned char message[] =
+  {0x34, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x04, 0x05};
+  GetPathLogicalSegmentElectronicKeyFormat4(message, key);
+
+  CHECK_EQUAL( 256, ElectronicKeyFormat4GetVendorId(key) );
+  CHECK_EQUAL( 512, ElectronicKeyFormat4GetDeviceType(key) );
+
+  MEMCMP_EQUAL(message + 2, key, 8);
+
+}

+ 3 - 2
source/tests/cip/cipepathtest.cpp

@@ -285,12 +285,13 @@ TEST(CipEpath, GetPathLogicalSegmentElectronicKeyFormat4) {
 }
 
 TEST(CipEpath, GetLogicalSegmentElectronicKeyFormat4) {
+  /* Size of an electronic key is 1 + 1 + 8 (Segment, Key format, Key) */
   const unsigned char message[] =
-  {0x34,0x04, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
+  {0x34, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x04, 0x05};
   ElectronicKeyFormat4 *electronic_key = ElectronicKeyFormat4New();
   GetPathLogicalSegmentElectronicKeyFormat4(message, electronic_key);
 
-  MEMCMP_EQUAL(message + 2, electronic_key, 6);
+  MEMCMP_EQUAL(message + 2, electronic_key, 8);
 
   ElectronicKeyFormat4Delete(&electronic_key);
 }