Bläddra i källkod

various changes in decoding/encoding functions
started to encode/decode arrays (variant type)

FlorianPalm 11 år sedan
förälder
incheckning
ca48adaff8

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 461 - 145
OPCUAServer/src/opcua_binaryEncDec.c


+ 20 - 0
OPCUAServer/src/opcua_binaryEncDec.h

@@ -19,6 +19,26 @@
 
 #define MAX_PICO_SECONDS 9999
 //functions
+/**
+ *
+ * @param srcBuf
+ * @param type
+ * @param pos
+ * @param dstStructure
+ * @return
+ */
+Int32 decoder_decodeBuiltInDatatype(char *srcBuf, Int32 type, Int32 *pos,
+		void *dstStructure);
+/**
+ *
+ * @param data
+ * @param type
+ * @param pos
+ * @param dstBuf
+ * @return
+ */
+Int32 encoder_encodeBuiltInDatatype(void *data, Int32 type, Int32 *pos, char *dstBuf);
+
 /**
  *
  * @param buf

+ 286 - 32
OPCUAServer/src/opcua_builtInDatatypes.h

@@ -16,7 +16,7 @@
 *
 */
 
-typedef enum _UA_BuiltInDataTypes
+typedef enum
 {
 	BOOLEAN = 	1,
 	SBYTE = 	2,
@@ -47,6 +47,70 @@ typedef enum _UA_BuiltInDataTypes
 UA_BuiltInDataTypes;
 
 
+
+
+typedef enum
+{
+	BOOLEAN_ARRAY = 	129,
+	SBYTE_ARRAY = 	130,
+	BYTE_ARRAY = 		131,
+	INT16_ARRAY = 	132,
+	UINT16_ARRAY = 	133,
+	INT32_ARRAY = 	134,
+	UINT32_ARRAY = 	135,
+	INT64_ARRAY = 	136,
+	UINT64_ARRAY = 	137,
+	FLOAT_ARRAY = 	138,
+	DOUBLE_ARRAY = 	139,
+	STRING_ARRAY = 	140,
+	DATE_TIME_ARRAY = 141,
+	GUID_ARRAY = 		142,
+	BYTE_STRING_ARRAY = 143,
+	XML_ELEMENT_ARRAY = 144,
+	NODE_ID_ARRAY = 	145,
+	EXPANDED_NODE_ID_ARRAY = 145,
+	STATUS_CODE_ARRAY = 146,
+	QUALIFIED_NAME_ARRAY = 147,
+	LOCALIZED_TEXT_ARRAY = 	148,
+	EXTENSION_OBJECT_ARRAY = 149,
+	DATA_VALUE_ARRAY = 150,
+	VARIANT_ARRAY = 151,
+	DIAGNOSTIC_INFO_ARRAY = 152
+}
+UA_BuiltInDataTypes_Array;
+
+typedef enum
+{
+	BOOLEAN_MATRIX = 193,
+	SBYTE_MATRIX = 	194,
+	BYTE_MATRIX = 	195,
+	INT16_MATRIX = 	196,
+	UINT16_MATRIX = 197,
+	INT32_MATRIX = 	198,
+	UINT32_MATRIX = 199,
+	INT64_MATRIX =  200,
+	UINT64_MATRIX = 201,
+	FLOAT_MATRIX = 	202,
+	DOUBLE_MATRIX = 203,
+	STRING_MATRIX = 204,
+	DATE_TIME_MATRIX = 205,
+	GUID_MATRIX = 206,
+	BYTE_STRING_MATRIX = 207,
+	XML_ELEMENT_MATRIX = 208,
+	NODE_ID_MATRIX = 209,
+	EXPANDED_NODE_ID_MATRIX = 210,
+	STATUS_CODE_MATRIX = 211,
+	QUALIFIED_NAME_MATRIX = 212,
+	LOCALIZED_TEXT_MATRIX = 213,
+	EXTENSION_OBJECT_MATRIX = 214,
+	DATA_VALUE_MATRIX = 215,
+	VARIANT_MATRIX = 216,
+	DIAGNOSTIC_INFO_MATRIX = 217
+}
+UA_BuiltInDataTypes_Array;
+
+
+
 /**
 * BasicBuiltInDatatypes
 * Part: 6
@@ -117,12 +181,6 @@ typedef struct _UA_ByteString
 UA_ByteString;
 
 
-typedef struct
-{
-	Int32 Length;
-	Int32 *Data;
-}IntegerString;
-
 /* GuidType
 * Part: 6
 * Chapter: 5.2.2.6
@@ -342,33 +400,227 @@ UA_ExtensionObjectEncodingMaskType;
 */
 struct _UA_DataValue;
 struct _UA_Variant;
+
+
+typedef struct
+{
+	Int32 *data;
+	Int32 length;
+}IntegerString;
+
+typedef struct
+{
+	Int32 *data;
+	Int32 arrayLength;
+	IntegerString dimensions;
+}Int32_Array;
+
+
+// Array types of builtInDatatypes
+typedef struct
+{
+	SByte *data;
+	Int32 arrayLength;
+	IntegerString dimensions;
+}SBYte_Array;
+
+typedef struct
+{
+	Boolean *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}Boolean_Array;
+
+typedef struct
+{
+	SByte *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}SByte_Array;
+
+typedef struct
+{
+	Byte *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}Byte_Array;
+
+typedef struct
+{
+	Int16 *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}Int16_Array;
+
+typedef struct
+{
+	UInt16 *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}UInt16_Array;
+
+
+typedef struct
+{
+	UInt32 *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}UInt32_Array
+
+typedef struct
+{
+	Int64 *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}Int64_Array;
+
+typedef struct
+{
+	UInt64 *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}UInt64_Array;
+typedef struct
+{
+	Float *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}Float_Array;
+
+typedef struct
+{
+	Double *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}Double_Array;
+
+typedef struct
+{
+	UA_String *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}String_Array;
+
+typedef struct
+{
+	UA_DateTime *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}DateTime_Array;
+
+typedef struct
+{
+	UA_Guid *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}Guid_Array;
+
+typedef struct
+{
+	UA_ByteString *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}ByteString_Array;
+
+typedef struct
+{
+	UA_XmlElement *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}XmlElement_Array;
+
+typedef struct
+{
+	UA_NodeId *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}NodeId_Array;
+
+typedef struct
+{
+	UA_ExpandedNodeId *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}ExpandedNodeId_Array;
+
+
+typedef struct
+{
+	UA_StatusCode *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}StatusCode_Array;
+
+typedef struct
+{
+	UA_QualifiedName *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}QualifiedName_Array;
+
+typedef struct
+{
+	UA_LocalizedText *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}LocalizedText_Array;
+
+typedef struct
+{
+	UA_ExtensionObject *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}ExtensionObject_Array;
+
+typedef struct
+{
+	UA_DataValue *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}DataValue_Array;
+
+typedef struct
+{
+	UA_Variant *data;
+	Int32 arrayLength;
+	IntegerString  dimensions;
+}Variant_Array;
+
+typedef struct
+{
+	UA_DiagnosticInfo *data;
+	Int32 arrayLength;
+	IntegerString dimensions;
+}DiagnosticInfo_Array;
+
+
 typedef union _UA_VariantArrayUnion
 {
-    void*              Array;
-    Boolean*           BooleanArray;
-    SByte*             SByteArray;
-    Byte*              ByteArray;
-    Int16*             Int16Array;
-    UInt16*            UInt16Array;
-    Int32*             Int32Array;
-    UInt32*            UInt32Array;
-    Int64*             Int64Array;
-    UInt64*            UInt64Array;
-    Float*             FloatArray;
-    Double*            DoubleArray;
-    UA_String*            StringArray;
-    UA_DateTime*          DateTimeArray;
-    UA_Guid*              GuidArray;
-    UA_ByteString*        ByteStringArray;
-    UA_ByteString*        XmlElementArray;
-    UA_NodeId*            NodeIdArray;
-    UA_ExpandedNodeId*    ExpandedNodeIdArray;
-    UA_StatusCode*        StatusCodeArray;
-    UA_QualifiedName*     QualifiedNameArray;
-    UA_LocalizedText*     LocalizedTextArray;
-    UA_ExtensionObject*   ExtensionObjectArray;
-    struct _UA_DataValue* DataValueArray;
-    struct _UA_Variant*   VariantArray;
+    void*         Array;
+    Boolean_Array BooleanArray;
+    SByte_Array   SByteArray;
+    Byte_Array    ByteArray;
+    Int16_Array   Int16Array;
+    UInt16_Array  UInt16Array;
+    Int32_Array   Int32Array;
+    UInt32_Array  UInt32Array;
+    Int64_Array   Int64Array;
+    UInt64_Array  UInt64Array;
+    Float_Array   FloatArray;
+    Double_Array            DoubleArray;
+    String_Array            StringArray;
+    DateTime_Array          DateTimeArray;
+    Guid_Array              GuidArray;
+    ByteString_Array        ByteStringArray;
+    ByteString_Array        XmlElementArray;
+    NodeId_Array            NodeIdArray;
+    ExpandedNodeId_Array    ExpandedNodeIdArray;
+    StatusCode_Array        StatusCodeArray;
+    QualifiedName_Array     QualifiedNameArray;
+    LocalizedText_Array     LocalizedTextArray;
+    ExtensionObject_Array   ExtensionObjectArray;
+    DataValue_Array DataValueArray;
+    Variant_Array   VariantArray;
 }
 UA_VariantArrayUnion;
 
@@ -490,4 +742,6 @@ typedef UInt32 IntegerId;
 */
 typedef double UA_Duration;
 
+
+
 #endif /* OPCUA_BUILTINDATATYPES_H_ */

+ 19 - 27
OPCUAServer/src/opcua_transportLayer.c

@@ -64,15 +64,16 @@ void TL_open(UA_connection *connection, AD_RawMessage *rawMessage)
 Int32 TL_checkMessage(UA_connection *connection, AD_RawMessage *TL_messsage)
 {
 	Int32 position = 4;
+	UInt32 messageLength = 0;
 	TL_getPacketType(TL_messsage);
+	decoder_decodeBuiltInDatatype(TL_messsage->message,UINT32,&position,&messageLength);
 
-	Int32 messageLen = decodeUInt32(TL_messsage->message, &position);
-	if (messageLen == TL_messsage->length &&
-		messageLen < (connection->transportLayer.localConf.maxMessageSize))
+	if (messageLength == TL_messsage->length &&
+			messageLength < (connection->transportLayer.localConf.maxMessageSize))
 	{
-		return 1;
+		return UA_ERROR;
 	}
-	return 0;
+	return UA_NO_ERROR;
 }
 void TL_receive(UA_connection *connection, AD_RawMessage *TL_message)
 {
@@ -185,10 +186,8 @@ void TL_getMessageHeader(struct TL_header *header, AD_RawMessage *rawMessage)
 
 	pos = pos + TL_MESSAGE_TYPE_LEN;
 
-	header->Reserved = decodeByte(rawMessage->message,&pos);
-	pos = pos + TL_RESERVED_LEN;
-	header->MessageSize = decodeUInt32(rawMessage->message,&pos);
-
+	decoder_decodeBuiltInDatatype(rawMessage->message,BYTE,&pos,&(header->Reserved));
+	decoder_decodeBuiltInDatatype(rawMessage->message,UINT32,&pos,&(header->MessageSize));
 }
 Int32 TL_getPacketType(AD_RawMessage *rawMessage)
 {
@@ -267,9 +266,6 @@ void TL_processHELMessage_test()
 		printf(" - failed \n");
 	}
 
-
-
-
 }
 /*
  * gets the TL_messageBody
@@ -279,24 +275,20 @@ void TL_processHELMessage(UA_connection *connection, AD_RawMessage *rawMessage)
 	UInt32 pos = TL_HEADER_LENGTH;
 	struct TL_header tmpHeader;
 
-	connection->transportLayer.remoteConf.protocolVersion =
-			decodeUInt32(rawMessage->message,&pos);
-	pos = pos + sizeof(UInt32);
+	decoder_decodeBuiltInDatatype(rawMessage->message,UINT32,pos,
+			(void*)&(connection->transportLayer.remoteConf.protocolVersion));
+
+	decoder_decodeBuiltInDatatype(rawMessage->message,UINT32,pos,
+			(void*)&(connection->transportLayer.remoteConf.recvBufferSize));
 
-	connection->transportLayer.remoteConf.recvBufferSize =
-			decodeUInt32(rawMessage->message,&pos);
-	pos = pos +  sizeof(UInt32);
+	decoder_decodeBuiltInDatatype(rawMessage->message,UINT32,pos,
+			(void*)&(connection->transportLayer.remoteConf.sendBufferSize));
 
-	connection->transportLayer.remoteConf.sendBufferSize =
-			decodeUInt32(rawMessage->message,&pos);
-	pos = pos +  sizeof(UInt32);
-	connection->transportLayer.remoteConf.maxMessageSize =
-			decodeUInt32(rawMessage->message,&pos);
-	pos = pos +  sizeof(UInt32);
+	decoder_decodeBuiltInDatatype(rawMessage->message,UINT32,pos,
+			(void*)&(connection->transportLayer.remoteConf.maxMessageSize));
 
-	connection->transportLayer.remoteConf.maxChunkCount =
-			decodeUInt32(rawMessage->message,&pos);
-	pos = pos +  sizeof(UInt32);
+	decoder_decodeBuiltInDatatype(rawMessage->message,UINT32,pos,
+			(void*)&(connection->transportLayer.remoteConf.maxChunkCount));
 
 	connection->transportLayer.endpointURL.Data = &(rawMessage->message[pos]);
 	connection->transportLayer.endpointURL.Length = tmpHeader.MessageSize - pos;

+ 1 - 1
OPCUAServer/src/opcua_types.h

@@ -13,7 +13,7 @@
 
 #define UA_NO_ERROR 0
 #define UA_ERROR 1
-#define NULL (void*)0
+
 typedef enum _T_ApplicationType
 {
 	ApplicationType_SERVER_0,

+ 20 - 9
OPCUAServer/tests/check_stack.c

@@ -92,7 +92,8 @@ START_TEST(decodeInt16_test)
 	//encodeUInt16(testUInt16, &position, &rawMessage);
 
 	Int32 p = 0;
-	Int16 val = decodeInt16(rawMessage.message,&p);
+	Int16 val;
+	decoder_decodeBuiltInDatatype(rawMessage.message,INT16,&p,&val);
 	ck_assert_int_eq(val,1);
 	//ck_assert_int_eq(p, 2);
 	//ck_assert_int_eq(rawMessage.message[0], 0xAB);
@@ -116,7 +117,8 @@ START_TEST(encodeInt16_test)
 
 	ck_assert_int_eq(position, 2);
 	Int32 p = 0;
-	Int16 val = decodeInt16(rawMessage.message,&p);
+	Int16 val;
+	decoder_decodeBuiltInDatatype(rawMessage.message, INT16, &p, &val);
 	ck_assert_int_eq(val,testUInt16);
 	//ck_assert_int_eq(rawMessage.message[0], 0xAB);
 
@@ -138,7 +140,9 @@ START_TEST(decodeUInt16_test)
 	//encodeUInt16(testUInt16, &position, &rawMessage);
 
 	Int32 p = 0;
-	UInt16 val = decodeUInt16(rawMessage.message,&p);
+	UInt16 val;
+	decoder_decodeBuiltInDatatype(rawMessage.message,UINT16,&p,&val);
+
 	ck_assert_int_eq(val,1);
 	//ck_assert_int_eq(p, 2);
 	//ck_assert_int_eq(rawMessage.message[0], 0xAB);
@@ -162,7 +166,8 @@ START_TEST(encodeUInt16_test)
 
 	ck_assert_int_eq(position, 2);
 	Int32 p = 0;
-	UInt16 val = decodeUInt16(rawMessage.message,&p);
+	UInt16 val;
+	decoder_decodeBuiltInDatatype(rawMessage.message, UINT16, &p, &val);
 	ck_assert_int_eq(val,testUInt16);
 	//ck_assert_int_eq(rawMessage.message[0], 0xAB);
 
@@ -181,7 +186,8 @@ START_TEST(decodeUInt32_test)
 	rawMessage.length = 4;
 
 	Int32 p = 0;
-	UInt32 val = decodeUInt32(rawMessage.message,&p);
+	UInt32 val;
+	decoder_decodeBuiltInDatatype(rawMessage.message, UINT32, &p, &val);
 	ck_assert_uint_eq(val,255);
 
 }
@@ -221,7 +227,8 @@ START_TEST(decodeInt32_test)
 
 
 	Int32 p = 0;
-	Int32 val = decodeInt32(rawMessage.message,&p);
+	Int32 val;
+	decoder_decodeBuiltInDatatype(rawMessage.message, INT32, &p, &val);
 	ck_assert_int_eq(val,1000000000);
 }
 END_TEST
@@ -245,7 +252,8 @@ START_TEST(decodeUInt64_test)
 	rawMessage.length = 8;
 
 	Int32 p = 0;
-	UInt64 val = decodeUInt64(rawMessage.message,&p);
+	UInt64 val;
+	decoder_decodeBuiltInDatatype(rawMessage.message, UINT64, &p, &val);
 	ck_assert_uint_eq(val, expectedVal);
 }
 END_TEST
@@ -287,7 +295,8 @@ START_TEST(decodeInt64_test)
 	rawMessage.length = 8;
 
 	Int32 p = 0;
-	Int64 val = decodeInt64(rawMessage.message,&p);
+	Int64 val;
+	decoder_decodeBuiltInDatatype(rawMessage.message, INT64, &p, &val);
 	ck_assert_uint_eq(val, expectedVal);
 }
 END_TEST
@@ -324,7 +333,9 @@ START_TEST(decodeFloat_test)
 	char buf[4] = {0x00,0x00,0xD0,0xC0};
 
 
-	Float calcVal = decodeFloat(buf,&pos);
+	Float calcVal;
+
+	decoder_decodeBuiltInDatatype(buf, FLOAT, &pos, &calcVal);
 	//val should be -6.5
 
 	Int32 val = (calcVal > -6.501 && calcVal < -6.499);