Jelajahi Sumber

ciptypes.c added, function GetCipDataTypeLength added

micsat 4 tahun lalu
induk
melakukan
010e0ddeba
3 mengubah file dengan 104 tambahan dan 2 penghapusan
  1. 1 1
      source/src/cip/CMakeLists.txt
  2. 95 0
      source/src/cip/ciptypes.c
  3. 8 1
      source/src/cip/ciptypes.h

+ 1 - 1
source/src/cip/CMakeLists.txt

@@ -12,7 +12,7 @@ opener_common_includes()
 #######################################
 opener_platform_support("INCLUDES")
 
-set( CIP_SRC appcontype.c cipassembly.c cipclass3connection.c cipcommon.c cipconnectionobject.c cipconnectionmanager.c cipdlr.c ciperror.h cipethernetlink.c cipidentity.c cipioconnection.c cipmessagerouter.c ciptcpipinterface.c ciptypes.h cipepath.c cipelectronickey.c cipstring.c cipqos.c )
+set( CIP_SRC appcontype.c cipassembly.c cipclass3connection.c cipcommon.c cipconnectionobject.c cipconnectionmanager.c cipdlr.c ciperror.h cipethernetlink.c cipidentity.c cipioconnection.c cipmessagerouter.c ciptcpipinterface.c ciptypes.h cipepath.c cipelectronickey.c cipstring.c cipqos.c ciptypes.c)
 
 add_library( CIP ${CIP_SRC} )
 

+ 95 - 0
source/src/cip/ciptypes.c

@@ -0,0 +1,95 @@
+/*******************************************************************************
+ * Copyright (c) 2009, Rockwell Automation, Inc.
+ * All rights reserved.
+ *
+ ******************************************************************************/
+#include <ciptypes.h>
+#include <endianconv.h>
+#include <trace.h>
+
+/* functions*/
+int GetCipDataTypeLength(EipUint8 type, const EipUint8 *data) {
+
+	int length = 0;
+
+	switch (type) {
+		case kCipBool:
+		case kCipSint:
+		case kCipUsint:
+		case kCipByte:
+			length = 1;
+			break;
+
+		case kCipInt:
+		case kCipUint:
+		case kCipWord:
+		case kCipUsintUsint:
+		case kCipItime:
+		case kCipDate:
+		case kCipEngUnit:
+			length = 2;
+			break;
+
+		case kCipDint:
+		case kCipUdint:
+		case kCipDword:
+		case kCipStime:
+		case kCipFtime:
+		case kCipTime:
+		case kCipReal:
+		case kCipTimeOfDay:
+			length = 4;
+			break;
+
+		case kCipLint:
+		case kCipUlint:
+		case kCipLreal:
+		case kCipLword:
+		case kCipLtime:
+			length = 8;
+			break;
+
+		case kCip6Usint:
+			length = 6;
+			break;
+
+		case kCipString:
+		case kCipString2:
+		case kCipStringN:
+			if(NULL != data){
+				length = GetIntFromMessage(&data) + 2; // string length + 2 bytes length indicator
+			}
+			break;
+
+		case kCipShortString:
+			if(NULL != data){
+				length = GetSintFromMessage(&data) + 1; // string length + 1 byte length indicator
+			}
+			break;
+
+		case kCipEpath:
+			if(NULL != data){
+				length = GetIntFromMessage(&data) + 2; // path size + 2 bytes path size indicator
+			}
+			break;
+
+		case kCipByteArray:
+			if (NULL != data) {
+				CipByteArray *byte_array = (CipByteArray*) data;
+				length = byte_array->length;
+			}
+			break;
+
+		default:
+			OPENER_TRACE_ERR("GetCipDataTypeLength ERROR\n");
+			return 0;
+
+		/* TODO: missing data types:
+		 * kCipAny
+		 * kCipDateAndTime
+		 * kCipStringI
+		 * kCipMemberList
+		 */
+	}
+	return length;
+}

+ 8 - 1
source/src/cip/ciptypes.h

@@ -45,7 +45,7 @@ typedef enum cip_data_types {
                              length indicator */
   kCipTime = 0xDB, /**< Duration in milli-seconds; range of DINT */
   kCipEpath = 0xDC, /**< CIP path segments*/
-  kCipEngUnit = 0xDD, /**< Engineering Units*/
+  kCipEngUnit = 0xDD, /**< Engineering Units, range of UINT*/
   /* definition of some CIP structs */
   /* need to be validated in IEC 61131-3 subclause 2.3.3 */
   /* TODO: Check these codes */
@@ -60,6 +60,13 @@ typedef enum cip_data_types {
   kCipStringI
 } CipDataType;
 
+/** @brief returns the size of CIP data types in bytes
+ * @param type CIP data type
+ * @param data use data pointer if data length is variable, else set NULL
+ * @return size of CIP data type in bytes
+ * */
+int GetCipDataTypeLength(EipUint8 type, const EipUint8 *data);
+
 /** @brief Definition of CIP service codes
  *
  * An Enum with all CIP service codes. Common services codes range from 0x01 to