Просмотр исходного кода

Refactors memory allocation, adds tests

CapXilinx 9 лет назад
Родитель
Сommit
d15198db4b

+ 11 - 0
source/src/cip/cipelectronickey.c

@@ -4,6 +4,8 @@
  *
  ******************************************************************************/
 
+#include <stdlib.h>
+
 #include "cipelectronickey.h"
 
 typedef struct electronic_key_format_4 {
@@ -14,6 +16,15 @@ typedef struct electronic_key_format_4 {
   CipUsint minor_revision;
 } ElectronicKeyFormat4;
 
+ElectronicKeyFormat4 *ElectronicKeyFormat4New() {
+  return (ElectronicKeyFormat4*)calloc(1, sizeof(ElectronicKeyFormat4));
+}
+
+void ElectronicKeyFormat4Delete(ElectronicKeyFormat4 **key) {
+  free(*key);
+  *key = NULL;
+}
+
 void SetElectronicKeyFormat4VendorId(const CipUint vendor_id, ElectronicKeyFormat4 *const electronic_key) {
   electronic_key->vendor_id = vendor_id;
 }

+ 3 - 0
source/src/cip/cipelectronickey.h

@@ -13,6 +13,9 @@
 
 typedef struct electronic_key_format_4 ElectronicKeyFormat4;
 
+ElectronicKeyFormat4 *ElectronicKeyFormat4New();
+void ElectronicKeyFormat4Delete(ElectronicKeyFormat4 **key);
+
 void SetElectronicKeyFormat4VendorId(const CipUint vendor_id, ElectronicKeyFormat4 *const electronic_key);
 CipUint GetElectronicKeyFormat4VendorId(const ElectronicKeyFormat4 *const electronic_key);
 

+ 12 - 18
source/src/cip/cipepath.c

@@ -57,12 +57,6 @@ const unsigned int kPortSegmentExtendedPort = 15; /**< Reserved port segment por
 
 #define ELECTRONIC_KEY_SEGMENT_KEY_FORMAT_4_MESSAGE_VALUE 0x04
 
-typedef enum {
-  kDataSegmentSubtypeReserved,
-  kDataSegmentSubtypeSimpleData,
-  kDataSegmentSubtypeANSIExtendedSymbol
-} DataSegmentSubtype;
-
 #define DATA_SEGMENT_SUBTYPE_SIMPLE_DATA_MESSAGE_VALUE 0x00
 #define DATA_SEGMENT_SUBTYPE_ANSI_EXTENDED_SYMBOL_MESSAGE_VALUE 0x11
 
@@ -307,21 +301,18 @@ ElectronicKeySegmentFormat GetPathLogicalSegmentElectronicKeyFormat(const unsign
   return result;
 }
 
-ElectronicKeyFormat4 *GetPathLogicalSegmentElectronicKeyFormat4(const unsigned char *const cip_path) {
+void GetPathLogicalSegmentElectronicKeyFormat4(const unsigned char *const cip_path, ElectronicKeyFormat4 *key) {
 //  OPENER_ASSERT(kElectronicKeySegmentFormatKeyFormat4 ==
 //      GetPathLogicalSegmentElectronicKeyFormat(cip_path), "Not electronic key format 4!\n");
   OPENER_ASSERT(kElectronicKeySegmentFormatKeyFormat4 ==
         GetPathLogicalSegmentElectronicKeyFormat(cip_path));
 
-  const char *message_runner = (const char *)cip_path;
-  ElectronicKeyFormat4 *result = calloc(1, sizeof(ElectronicKeySegmentFormat));
-  SetElectronicKeyFormat4VendorId(GetIntFromMessage(&message_runner), result);
-  SetElectronicKeyFormat4DeviceType(GetIntFromMessage(&message_runner), result);
-  SetElectronicKeyFormat4ProductCode(GetIntFromMessage(&message_runner), result);
-  SetElectronicKeyFormat4MajorRevisionCompatibility(GetSintFromMessage(&message_runner), result);
-  SetElectronicKeyFormat4MinorRevision(GetSintFromMessage(&message_runner), result);
-
-  return result;
+  const char *message_runner = (const char *)(cip_path + 2);
+  SetElectronicKeyFormat4VendorId(GetIntFromMessage(&message_runner), key);
+  SetElectronicKeyFormat4DeviceType(GetIntFromMessage(&message_runner), key);
+  SetElectronicKeyFormat4ProductCode(GetIntFromMessage(&message_runner), key);
+  SetElectronicKeyFormat4MajorRevisionCompatibility(GetSintFromMessage(&message_runner), key);
+  SetElectronicKeyFormat4MinorRevision(GetSintFromMessage(&message_runner), key);
 }
 
 /*** Logical Segment ***/
@@ -335,6 +326,7 @@ ElectronicKeyFormat4 *GetPathLogicalSegmentElectronicKeyFormat4(const unsigned c
  *  @return The Network Segment subtype of the EPath
  */
 NetworkSegmentSubType GetPathNetworkSegmentSubtype(const unsigned char *const cip_path) {
+  OPENER_ASSERT(kSegmentTypeNetworkSegment == GetPathSegmentType(cip_path));
   const unsigned int kSubtypeMask = 0x1F;
   const unsigned int subtype = (*cip_path) & kSubtypeMask;
   NetworkSegmentSubType result = kNetworkSegmentSubtypeReserved;
@@ -349,6 +341,8 @@ NetworkSegmentSubType GetPathNetworkSegmentSubtype(const unsigned char *const ci
       result = kNetworkSegmentSubtypeSafetySegment; break;
     case NETWORK_SEGMENT_SUBTYPE_PRODUCTION_INHIBIT_TIME_IN_MICROSECONDS_MESSAGE_VALUE:
       result = kNetworkSegmentSubtypeProductionInhibitTimeInMicroseconds; break;
+    case NETWORK_SEGMENT_SUBTYPE_EXTENDED_NETWORK_MESSAGE_VALUE:
+          result = kNetworkSegmentSubtypeExtendedNetworkSegment; break;
     default: result = kNetworkSegmentSubtypeReserved; break;
   }
 
@@ -386,7 +380,7 @@ CipUdint GetPathNetworkSegmentProductionInhibitTimeInMicroseconds(const unsigned
   OPENER_ASSERT(kNetworkSegmentSubtypeProductionInhibitTimeInMicroseconds == GetPathNetworkSegmentSubtype(cip_path));
   OPENER_ASSERT(2 == *(cip_path + 1));
 
-  const char *message_runner = cip_path;
+  const unsigned char *message_runner = cip_path + 2;
   return GetDintFromMessage(&message_runner);
 }
 
@@ -427,7 +421,7 @@ CipUsint GetPathDataSegmentSimpleDataWordLength(const unsigned char *const cip_p
   OPENER_ASSERT(
       kDataSegmentSubtypeSimpleData == GetPathDataSegmentSubtype(cip_path));
 
-  const char *message_runner = cip_path;
+  const unsigned char *message_runner = cip_path + 1;
   return GetSintFromMessage(&message_runner);
 }
 

+ 17 - 1
source/src/cip/cipepath.h

@@ -66,6 +66,12 @@ typedef enum {
   kElectronicKeySegmentFormatKeyFormat4
 } ElectronicKeySegmentFormat;
 
+typedef enum {
+  kDataSegmentSubtypeReserved,
+  kDataSegmentSubtypeSimpleData,
+  kDataSegmentSubtypeANSIExtendedSymbol
+} DataSegmentSubtype;
+
 SegmentType GetPathSegmentType(const unsigned char *const cip_path);
 
 void SetPathSegmentType(SegmentType segment_type, unsigned char *const cip_path);
@@ -95,6 +101,16 @@ LogicalSegmentSpecialTypeLogicalFormat GetPathLogicalSegmentSpecialTypeLogicalTy
 
 ElectronicKeySegmentFormat GetPathLogicalSegmentElectronicKeyFormat(const unsigned char *const cip_path);
 
-ElectronicKeyFormat4 *GetPathLogicalSegmentElectronicKeyFormat4(const unsigned char *const cip_path);
+void GetPathLogicalSegmentElectronicKeyFormat4(const unsigned char *const cip_path, ElectronicKeyFormat4 *key);
+
+NetworkSegmentSubType GetPathNetworkSegmentSubtype(const unsigned char *const cip_path);
+
+CipUsint GetPathNetworkSegmentProductionInhibitTimeInMilliseconds(const unsigned char *const cip_path);
+
+CipUdint GetPathNetworkSegmentProductionInhibitTimeInMicroseconds(const unsigned char *const cip_path);
+
+DataSegmentSubtype GetPathDataSegmentSubtype(const unsigned char *const cip_path);
+
+CipUsint GetPathDataSegmentSimpleDataWordLength(const unsigned char *const cip_path);
 
 #endif /* SRC_CIP_CIPEPATH_H_ */

+ 8 - 3
source/src/utils/random.c

@@ -10,7 +10,12 @@
 #include <stdlib.h>
 
 Random *RandomNew(SetSeed set_seed, GetNextUInt32 get_next_uint32) {
-  Random *out = malloc(sizeof(Random));
-  *out = (Random ) { .set_seed = set_seed, .get_next_uint32 = get_next_uint32 };
-  return out;
+  Random *random = malloc(sizeof(Random));
+  *random = (Random ) { .set_seed = set_seed, .get_next_uint32 = get_next_uint32 };
+  return random;
+}
+
+void RandomDelete(Random **random) {
+  free(*random);
+  *random = NULL;
 }

+ 3 - 1
source/src/utils/random.h

@@ -19,6 +19,8 @@ typedef struct {
   GetNextUInt32 get_next_uint32; /**< Function pointer to GetNextUInt32 function */
 } Random;
 
-Random* RandomNew(SetSeed, GetNextUInt32);
+Random *RandomNew(SetSeed, GetNextUInt32);
+
+void RandomDelete(Random **random);
 
 #endif /* OPENER_RANDOM_H_ */

+ 1 - 1
source/src/utils/xorshiftrandom.h

@@ -17,7 +17,7 @@
 
 /**
  * @brief Sets the initial seed for the XOR shift pseudo-random algorithm
- * @param pa_nSeed The initial seed value
+ * @param seed The initial seed value
  */
 void SetXorShiftSeed(uint32_t seed);
 

+ 2 - 0
source/tests/cip/CMakeLists.txt

@@ -6,3 +6,5 @@ set( CipTestSrc cipepathtest.cpp )
 include_directories( ${SRC_DIR}/cip )
 
 add_library( CipTest ${CipTestSrc} )
+
+target_link_libraries( CipTest gcov ${CPPUTEST_LIBRARY} ${CPPUTESTEXT_LIBRARY} )

+ 84 - 4
source/tests/cip/cipepathtest.cpp

@@ -258,9 +258,89 @@ TEST(CipEpath, GetPathLogicalSegmentElectronicKeyFormat4) {
 }
 
 TEST(CipEpath, GetLogicalSegmentElectronicKeyFormat4) {
-  const unsigned char message[] = {0x34,0x04};
-  ElectronicKeyFormat4 *electronic_key = GetPathLogicalSegmentElectronicKeyFormat4(message);
+  const unsigned char message[] = {0x34,0x04, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
+  ElectronicKeyFormat4 *electronic_key = ElectronicKeyFormat4New();
+  GetPathLogicalSegmentElectronicKeyFormat4(message, electronic_key);
 
-  //TODO: check on how to free in CppuTest
-  //free(electronic_key);
+  MEMCMP_EQUAL(message + 2, electronic_key, 6);
+
+  ElectronicKeyFormat4Delete(&electronic_key);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentSubtypeReserved) {
+  const unsigned char message[] = {0x40};
+  NetworkSegmentSubType sub_type = GetPathNetworkSegmentSubtype(message);
+  CHECK_EQUAL(kNetworkSegmentSubtypeReserved, sub_type);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentSubtypeScheduled) {
+  const unsigned char message[] = {0x41};
+  NetworkSegmentSubType sub_type = GetPathNetworkSegmentSubtype(message);
+  CHECK_EQUAL(kNetworkSegmentSubtypeScheduleSegment, sub_type);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentSubtypeFixedTag) {
+  const unsigned char message[] = {0x42};
+  NetworkSegmentSubType sub_type = GetPathNetworkSegmentSubtype(message);
+  CHECK_EQUAL(kNetworkSegmentSubtypeFixedTagSegment, sub_type);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentSubtypeProductionInhibitTimerInMilliseconds) {
+  const unsigned char message[] = {0x43};
+  NetworkSegmentSubType sub_type = GetPathNetworkSegmentSubtype(message);
+  CHECK_EQUAL(kNetworkSegmentSubtypeProductionInhibitTimeInMilliseconds, sub_type);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentSubtypeSafety) {
+  const unsigned char message[] = {0x44};
+  NetworkSegmentSubType sub_type = GetPathNetworkSegmentSubtype(message);
+  CHECK_EQUAL(kNetworkSegmentSubtypeSafetySegment, sub_type);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentSubtypeProductionInhibitTimerInMicroseconds) {
+  const unsigned char message[] = {0x50};
+  NetworkSegmentSubType sub_type = GetPathNetworkSegmentSubtype(message);
+  CHECK_EQUAL(kNetworkSegmentSubtypeProductionInhibitTimeInMicroseconds, sub_type);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentSubtypeExtendedNetwork) {
+  const unsigned char message[] = {0x5F};
+  NetworkSegmentSubType sub_type = GetPathNetworkSegmentSubtype(message);
+  CHECK_EQUAL(kNetworkSegmentSubtypeExtendedNetworkSegment, sub_type);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentProductionInhibitTimeInMilliseconds) {
+  const unsigned char message[] = {0x43, 0xFF};
+  CipUsint time_im_milliseconds = GetPathNetworkSegmentProductionInhibitTimeInMilliseconds(message);
+  CHECK_EQUAL(255, time_im_milliseconds);
+}
+
+TEST(CipEpath, GetPathNetworkSegmentProductionInhibitTimeInMicroseconds) {
+  const unsigned char message[] = {0x50, 0x02, 0x00, 0x00, 0x00, 0xFF};
+  CipUdint time_in_microseconds = GetPathNetworkSegmentProductionInhibitTimeInMicroseconds(message);
+  CHECK_EQUAL(4278190080, time_in_microseconds);
+}
+
+TEST(CipEpath, GetPathDataSegmentSubtypeReserverd) {
+  const unsigned char message[] = {0x81};
+  DataSegmentSubtype type = GetPathDataSegmentSubtype(message);
+  CHECK_EQUAL(kDataSegmentSubtypeReserved, type);
+}
+
+TEST(CipEpath, GetPathDataSegmentSubtypeSimpleData) {
+  const unsigned char message[] = {0x80};
+  DataSegmentSubtype type = GetPathDataSegmentSubtype(message);
+  CHECK_EQUAL(kDataSegmentSubtypeSimpleData, type);
+}
+
+TEST(CipEpath, GetPathDataSegmentSubtypeANSIExtendedSymbol) {
+  const unsigned char message[] = {0x91};
+  DataSegmentSubtype type = GetPathDataSegmentSubtype(message);
+  CHECK_EQUAL(kDataSegmentSubtypeANSIExtendedSymbol, type);
+}
+
+TEST(CipEpath, GetPathDataSegmentSimpleDataWordLength) {
+  const unsigned char message[] = {0x80, 0x25};
+  CipUsint length = GetPathDataSegmentSimpleDataWordLength(message);
+  CHECK_EQUAL(37, length);
 }

+ 2 - 0
source/tests/utils/CMakeLists.txt

@@ -6,3 +6,5 @@ set( UtilsTestSrc randomTests.cpp xorshiftrandomtests.cpp)
 include_directories( ${SRC_DIR}/utils )
 
 add_library( UtilsTest ${UtilsTestSrc} )
+
+target_link_libraries( UtilsTest gcov ${CPPUTEST_LIBRARY} ${CPPUTESTEXT_LIBRARY} )

+ 2 - 1
source/tests/utils/randomTests.cpp

@@ -20,9 +20,10 @@ TEST_GROUP(RandomClass)
 
 TEST(RandomClass, CreateXOrShiftObject)
 {
-	Random* pRandom;
+	Random* pRandom = NULL;
 	uint32_t nResult = 0;
 	pRandom = RandomNew(SetXorShiftSeed, NextXorShiftUint32);
 	POINTERS_EQUAL(SetXorShiftSeed, pRandom->set_seed);
 	POINTERS_EQUAL(NextXorShiftUint32, pRandom->get_next_uint32);
+	RandomDelete(&pRandom);
 }