Преглед на файлове

Combines EncaplulateIpAddress and EncapsulateIpAddressCommonPacketFormat, as they differed only in appliying htons to the port

capxilinx преди 10 години
родител
ревизия
d5111f601b

+ 10 - 7
source/src/enet_encap/cpf.c

@@ -15,7 +15,6 @@
 #include "cipconnectionmanager.h"
 #include "trace.h"
 
-
 CipCommonPacketFormatData g_common_packet_format_data_item; /**< CPF global data items */
 
 int NotifyCommonPacketFormat(EncapsulationData *receive_data,
@@ -29,8 +28,9 @@ int NotifyCommonPacketFormat(EncapsulationData *receive_data,
     OPENER_TRACE_ERR("notifyCPF: error from createCPFstructure\n");
   } else {
     return_value = kEipStatusOk; /* In cases of errors we normally need to send an error response */
-    if (g_common_packet_format_data_item.address_item.type_id == kCipItemIdNullAddress) /* check if NullAddressItem received, otherwise it is no unconnected message and should not be here*/
-    { /* found null address item*/
+    if (g_common_packet_format_data_item.address_item.type_id
+        == kCipItemIdNullAddress) /* check if NullAddressItem received, otherwise it is no unconnected message and should not be here*/
+        { /* found null address item*/
       if (g_common_packet_format_data_item.data_item.type_id
           == kCipItemIdUnconnectedMessage) { /* unconnected data item received*/
         return_value = NotifyMR(
@@ -224,7 +224,8 @@ int AssembleLinearMessage(
   AddIntToMessage(common_packet_format_data_item->item_count, &message); /* item count */
   size += 2;
   /* process Address Item */
-  if (common_packet_format_data_item->address_item.type_id == kCipItemIdNullAddress) { /* null address item -> address length set to 0 */
+  if (common_packet_format_data_item->address_item.type_id
+      == kCipItemIdNullAddress) { /* null address item -> address length set to 0 */
     AddIntToMessage(kCipItemIdNullAddress, &message);
     AddIntToMessage(0, &message);
     size += 4;
@@ -293,7 +294,8 @@ int AssembleLinearMessage(
       message++;
       *message = message_router_response->size_of_additional_status;
       message++;
-      for (int i = 0; i < message_router_response->size_of_additional_status; i++)
+      for (int i = 0; i < message_router_response->size_of_additional_status;
+          i++)
         AddIntToMessage(message_router_response->additional_status[i],
                         &message);
 
@@ -306,7 +308,8 @@ int AssembleLinearMessage(
                       &message);
       AddIntToMessage(common_packet_format_data_item->data_item.length,
                       &message);
-      for (int i = 0; i < common_packet_format_data_item->data_item.length; i++) {
+      for (int i = 0; i < common_packet_format_data_item->data_item.length;
+          i++) {
         *message = (EipUint8) *(common_packet_format_data_item->data_item.data
             + i);
         message++;
@@ -331,7 +334,7 @@ int AssembleLinearMessage(
             common_packet_format_data_item->address_info_item[j].length,
             &message);
 
-        EncapsulateIpAddressCommonPaketFormat(
+        EncapsulateIpAddress(
             common_packet_format_data_item->address_info_item[j].sin_port,
             common_packet_format_data_item->address_info_item[j].sin_addr,
             message);

+ 1 - 1
source/src/enet_encap/encap.c

@@ -365,7 +365,7 @@ int EncapsulateListIdentyResponseMessage(const EipByte *const communication_buff
 
   AddIntToMessage(kSupportedProtocolVersion, &communication_buffer_runner);
 
-  EncapsulateIpAddress(kOpenerEthernetPort,
+  EncapsulateIpAddress(htons(kOpenerEthernetPort),
                        interface_configuration_.ip_address,
                        communication_buffer_runner);
   communication_buffer_runner += 8;

+ 2 - 38
source/src/enet_encap/endianconv.c

@@ -3,6 +3,8 @@
  * All rights reserved. 
  *
  ******************************************************************************/
+#include <sys/socket.h>
+
 #include "endianconv.h"
 
 OpenerEndianess g_opener_platform_endianess = kOpenerEndianessUnknown;
@@ -105,45 +107,7 @@ void AddLintToMessage(EipUint64 data, EipUint8 **buffer) {
 
 #endif
 
-/**
- * @brief Encapsulates the IP address and port into the package
- *
- * @param port IP Port
- * @param address IP Address
- * @param communication_buffer Buffer for constructing the message
- */
 void EncapsulateIpAddress(EipUint16 port, EipUint32 address,
-                          EipByte *communication_buffer) {
-  if (kOpENerEndianessLittle == g_opener_platform_endianess) {
-    AddIntToMessage(htons(AF_INET), &communication_buffer);
-    AddIntToMessage(htons(port), &communication_buffer);
-    AddDintToMessage(address, &communication_buffer);
-  } else {
-    if (kOpENerEndianessBig == g_opener_platform_endianess) {
-      communication_buffer[0] = (unsigned char) (AF_INET >> 8);
-      communication_buffer[1] = (unsigned char) AF_INET;
-      communication_buffer += 2;
-
-      communication_buffer[0] = (unsigned char) (port >> 8);
-      communication_buffer[1] = (unsigned char) port;
-      communication_buffer += 2;
-
-      communication_buffer[3] = (unsigned char) address;
-      communication_buffer[2] = (unsigned char) (address >> 8);
-      communication_buffer[1] = (unsigned char) (address >> 16);
-      communication_buffer[0] = (unsigned char) (address >> 24);
-    }
-  }
-}
-
-/**
- * @brief encapsulates the IP address and port into the package (Common Paket Format variant)
- *
- * @param port IP port
- * @param address IP address
- * @param communication_buffer buffer for constructing the message
- */
-void EncapsulateIpAddressCommonPaketFormat(EipUint16 port, EipUint32 address,
                                            EipByte *communication_buffer) {
   if (kOpENerEndianessLittle == g_opener_platform_endianess) {
     AddIntToMessage(htons(AF_INET), &communication_buffer);

+ 8 - 5
source/src/enet_encap/endianconv.h

@@ -58,12 +58,15 @@ void AddLintToMessage(EipUint64 pa_unData, EipUint8 **buffer);
 
 #endif
 
-void EncapsulateIpAddress(EipUint16 port, EipUint32 address,
-                          EipByte *communication_buffer);
-
-/** @brief Encapsulate the sockaddr information as necessary for the Common Paket Format data items
+/** @brief Encapsulate the sockaddr information as necessary for the Common Packet Format data items
+ *
+ * Converts and adds the provided port and IP address into an common packet format message
+ *
+ * @param port Port of the socket, has to be provided in big-endian
+ * @param address IP address of the socket, has to be provided in big-endian
+ * @param communcation_buffer The message buffer for sending the message
  */
-void EncapsulateIpAddressCommonPaketFormat(EipUint16 port, EipUint32 address,
+void EncapsulateIpAddress(EipUint16 port, EipUint32 address,
                                            EipByte *communication_buffer);
 
 /** Identify if we are running on a big or little endian system and set

+ 2 - 2
source/tests/enet_encap/endianconvtest.cpp

@@ -96,13 +96,13 @@ TEST(EndianConversion, AddLintToMessage) {
   POINTERS_EQUAL(message + 8, message_pointer)
 }
 
-TEST(EndianConversion, EncapsulateIpAddressCommonPaketFormat) {
+TEST(EndianConversion, EncapsulateIpAddress) {
   CipOctet ip_message[8];
   CipOctet *ip_message_ponter = ip_message;
 
   DetermineEndianess();
 
-  EncapsulateIpAddressCommonPaketFormat(0xAF12, 0x25E0C459, ip_message_ponter);
+  EncapsulateIpAddress(0xAF12, 0x25E0C459, ip_message_ponter);
 
   BYTES_EQUAL(AF_INET >> 8, ip_message[0]);
   BYTES_EQUAL(AF_INET, ip_message[1]);