Переглянути джерело

convert To BuildInTypes has been extended.
ConvertToRequestHeader has been created.

MaximilianBauer 11 роки тому
батько
коміт
77eec0f2d3

+ 60 - 19
OPCUAServer/src/opcua_binaryEncDec.c

@@ -6,7 +6,9 @@
  */
 
 #include "opcua_binaryEncDec.h"
+#include "opcua_types.h"
 
+const char *TEST_PASSED = "PASSED";
 
 /*
  * convert byte array to Byte
@@ -77,6 +79,12 @@ Int64 convertToInt64(char* buf, int pos)
 	return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
 }
 
+Int64 convertToInt64_test(char* buf, int pos)
+{
+
+	printf("");
+}
+
 UA_String convertToUAString(char* buf, int pos)
 {
 	UA_String tmpUAString;
@@ -92,30 +100,63 @@ UA_Guid convertToUAGuid(char* buf, int pos)
 	UInt32 i = 0;
 	for(i = 1; i <= 4; i++)
 	{
-		tmpUAGuid.Data1[i] = convertToUInt32(*buf, pos+counter);
+		tmpUAGuid.Data1[i] = convertToUInt32(buf, pos+counter);
 		counter += sizeof(UInt32);
 	}
 	for(i = 1; i <= 2; i++)
 	{
-		tmpUAGuid.Data2[i] = convertToUInt16(*buf, pos+counter);
+		tmpUAGuid.Data2[i] = convertToUInt16(buf, pos+counter);
 		counter += sizeof(UInt16);
 	}
 	for(i = 1; i <= 2; i++)
 	{
-		tmpUAGuid.Data3[i] = convertToUInt16(*buf, pos+counter);
+		tmpUAGuid.Data3[i] = convertToUInt16(buf, pos+counter);
 		counter += sizeof(UInt16);
 	}
 	for(i = 1; i <= 8; i++)
 	{
-		tmpUAGuid.Data4[i] = convertToByte(*buf, pos+counter);
+		tmpUAGuid.Data4[i] = convertToByte(buf, pos+counter);
 		counter += sizeof(Byte);
 	}
 	return tmpUAGuid;
 }
 
+UA_ByteString convertToUAByteString(char* buf, int pos){
+	UA_ByteString tmpUAByteString;
+	int counter = sizeof(Int32);
+	int i = 0;
+
+	tmpUAByteString.Length = convertToInt32(buf, pos);
+	Byte byteStringData[tmpUAByteString.Length];
+
+	if(tmpUAByteString.Length == -1){
+		return tmpUAByteString;
+	}else{
+		for(i = 0; i < tmpUAByteString.Length; i++)
+		{
+			byteStringData[i] = convertToByte(buf, pos+counter);
+			counter += sizeof(Byte);
+		}
+	}
+
+	tmpUAByteString.Data = byteStringData;
+
+	return tmpUAByteString;
+}
+
+UA_DateTime convertToUADateTime(char* buf, int pos){
+	UA_DateTime tmpUADateTime;
+	tmpUADateTime = convertToInt64(buf, pos);
+	return tmpUADateTime;
+}
+
+UA_StatusCode convertToUAStatusCode(char* buf, int pos){
+	return convertToUInt32(buf, pos);
+}
+
 UA_NodeId convertToUANodeId(char* buf, int pos){
 	UA_NodeId tmpUANodeId;
-	tmpUANodeId.EncodingByte = convertToInt32(*buf, 0);
+	tmpUANodeId.EncodingByte = convertToInt32(buf, 0);
 	int counter = sizeof(UInt32);
 
 	UA_NodeIdEncodingValuesType encodingType = tmpUANodeId.EncodingByte;
@@ -124,48 +165,48 @@ UA_NodeId convertToUANodeId(char* buf, int pos){
 	{
 		case NIEVT_TWO_BYTE:
 		{
-			tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
+			tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
 			counter += sizeof(UInt16);
 			break;
 		}
 		case NIEVT_FOUR_BYTE:
 		{
-			tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
+			tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
 			counter += sizeof(Int64);
 			break;
 		}
 		case NIEVT_NUMERIC:
 		{
-			tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
+			tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
 			counter += sizeof(UInt32);
 			break;
 		}
 		case NIEVT_STRING:
 		{
-			tmpUANodeId.Identifier.String = convertToUAString(*buf, counter);
+			tmpUANodeId.Identifier.String = convertToUAString(buf, counter);
 			counter += sizeof(sizeof(UInt32) + tmpUANodeId.Identifier.String.Length*sizeof(char));
 			break;
 		}
 		case NIEVT_GUID:
 		{
-			UA_Guid tempGuid = convertToUAGuid(*buf, counter);
-			tmpUANodeId.Identifier.Guid = &tempGuid;
+			tmpUANodeId.Identifier.Guid = convertToUAGuid(buf, counter);
 			counter += sizeof(UA_Guid);
 			break;
 		}
 		case NIEVT_BYTESTRING:
 		{
-			//ToDo
+			tmpUANodeId.Identifier.OPAQUE = convertToUAByteString(buf, counter);
+			//If Length == -1 then the ByteString is null
+			if(tmpUANodeId.Identifier.OPAQUE.Length == -1)
+			{
+				counter += sizeof(Int32);
+			}else{
+				counter += sizeof(Int32)+sizeof(Byte)*tmpUANodeId.Identifier.OPAQUE.Length;
+			}
 			break;
 		}
-		case NIEVT_NAMESPACE_URI_FLAG:
-		{
+		default:
 			break;
-		}
-		case NIEVT_SERVERINDEX_FLAG:
-		{
-			break;
-		}
 	}
 
 	return tmpUANodeId;

+ 71 - 71
OPCUAServer/src/opcua_builtInDatatypes.h

@@ -18,31 +18,31 @@
 
 typedef enum _UA_BuiltInDataTypes
 {
-	BOOLEAN = 1,
-	SBYTE = 2,
-	BYTE = 3,
-	INT16 = 4,
-	UINT16 = 5,
-	INT32 = 6,
-	UINT32 = 7,
-	INT64 = 8,
-	UINT64 = 9,
-	FLOAT = 10,
-	DOUBLE = 11,
-	STRING = 12,
+	BOOLEAN = 	1,
+	SBYTE = 	2,
+	BYTE = 		3,
+	INT16 = 	4,
+	UINT16 = 	5,
+	INT32 = 	6,
+	UINT32 = 	7,
+	INT64 = 	8,
+	UINT64 = 	9,
+	FLOAT = 	10,
+	DOUBLE = 	11,
+	STRING = 	12,
 	DATE_TIME = 13,
-	GUID = 14,
-	BYTE_STRING = 15,
-	XML_ELEMENT = 16,
-	NODE_ID = 17,
-	EXPANDED_NODE_ID = 18,
-	STATUS_CODE = 19,
-	QUALIFIED_NAME = 20,
-	LOCALIZED_TEXT = 21,
-	EXTENSION_OBJECT = 22,
-	DATA_VALUE = 23,
-	VARIAN = 24,
-	DIAGNOSTIC_INFO = 25
+	GUID = 		14,
+	BYTE_STRING = 		15,
+	XML_ELEMENT = 		16,
+	NODE_ID = 			17,
+	EXPANDED_NODE_ID = 	18,
+	STATUS_CODE = 		19,
+	QUALIFIED_NAME = 	20,
+	LOCALIZED_TEXT = 	21,
+	EXTENSION_OBJECT = 	22,
+	DATA_VALUE = 		23,
+	VARIAN = 			24,
+	DIAGNOSTIC_INFO = 	25
 }
 UA_BuiltInDataTypes;
 
@@ -174,14 +174,14 @@ typedef enum _UA_NodeIdEncodingValuesType
 {
 	// Some Values are called the same as previous Enumerations so we need
 	// names that are unique
-	NIEVT_TWO_BYTE = 0, 			//Hex 0x00
-	NIEVT_FOUR_BYTE = 1, 			//Hex 0x01
-	NIEVT_NUMERIC = 2, 				//Hex 0x02
-	NIEVT_STRING = 3, 				//Hex 0x03
-	NIEVT_GUID = 4, 				//Hex 0x04
-	NIEVT_BYTESTRING = 5, 			//Hex 0x05
-	NIEVT_NAMESPACE_URI_FLAG = 128, //Hex 0x80 Is only for ExpandedNodeId required
-	NIEVT_SERVERINDEX_FLAG = 64 	//Hex 0x40 Is only for ExpandedNodeId required
+	NIEVT_TWO_BYTE = 	0x00,
+	NIEVT_FOUR_BYTE = 	0x01,
+	NIEVT_NUMERIC = 	0x02,
+	NIEVT_STRING = 		0x03,
+	NIEVT_GUID = 		0x04,
+	NIEVT_BYTESTRING = 	0x05,
+	NIEVT_NAMESPACE_URI_FLAG = 	0x80, 	//Is only for ExpandedNodeId required
+	NIEVT_SERVERINDEX_FLAG = 	0x40 	//Is only for ExpandedNodeId required
 }
 UA_NodeIdEncodingValuesType;
 
@@ -262,13 +262,13 @@ typedef enum _UA_DiagnosticInfoEncodingMaskType
 {
 	// Some Values are called the same as previouse Enumerations so we need
 	//names that are unique
-	DIEMT_SYMBOLIC_ID = 1, 		//Hex 0x01
-	DIEMT_NAMESPACE = 2, 			//Hex 0x02
-	DIEMT_LOCALIZED_TEXT = 4, 		//Hex 0x04
-	DIEMT_LOCATE = 8, 			//Hex 0x08
-	DIEMT_ADDITIONAL_INFO = 16, 		//Hex 0x10
-	DIEMT_INNER_STATUS_CODE = 32, 	//Hex 0x20
-	DIEMT_INNER_DIAGNOSTIC_INFO = 64 	//Hex 0x40
+	DIEMT_SYMBOLIC_ID = 			0x01,
+	DIEMT_NAMESPACE = 				0x02,
+	DIEMT_LOCALIZED_TEXT = 			0x04,
+	DIEMT_LOCATE = 					0x08,
+	DIEMT_ADDITIONAL_INFO = 		0x10,
+	DIEMT_INNER_STATUS_CODE = 		0x20,
+	DIEMT_INNER_DIAGNOSTIC_INFO = 	0x40
 }
 UA_DiagnosticInfoEncodingMaskType;
 
@@ -304,8 +304,8 @@ UA_LocalizedText;
 
 typedef enum _UA_LocalizedTextEncodingMaskType
 {
-	LTEMT_SYMBOLIC_ID = 1, 		//Hex 0x01
-	LTEMT_NAMESPACE = 2 			//Hex 0x02
+	LTEMT_SYMBOLIC_ID = 0x01,
+	LTEMT_NAMESPACE = 	0x02
 }
 UA_LocalizedTextEncodingMaskType;
 
@@ -327,9 +327,9 @@ UA_ExtensionObject;
 
 typedef enum _UA_ExtensionObjectEncodingMaskType
 {
-	NO_BODY_IS_ENCODED = 0,		//Hex 0x00
-	BODY_IS_BYTE_STRING = 1,	//Hex 0x01
-	BODY_IS_XML_ELEMENT = 2		//Hex 0x02
+	NO_BODY_IS_ENCODED = 	0x00,
+	BODY_IS_BYTE_STRING = 	0x01,
+	BODY_IS_XML_ELEMENT = 	0x02
 }
 UA_ExtensionObjectEncodingMaskType;
 
@@ -428,35 +428,35 @@ UA_Variant;
 typedef enum _UA_VariantTypeEncodingMaskType
 {
 	//Bytes 0:5	HEX 0x00 - 0x20
-	VTEMT_BOOLEAN = 1,
-	VTEMT_SBYTE = 2,
-	VTEMT_BYTE = 3,
-	VTEMT_INT16 = 4,
-	VTEMT_UINT16 = 5,
-	VTEMT_INT32 = 6,
-	VTEMT_UINT32 = 7,
-	VTEMT_INT64 = 8,
-	VTEMT_UINT64 = 9,
-	VTEMT_FLOAT = 10,
-	VTEMT_DOUBLE = 11,
-	VTEMT_STRING = 12,
-	VTEMT_DATE_TIME = 13,
-	VTEMT_GUID = 14,
-	VTEMT_BYTE_STRING = 15,
-	VTEMT_XML_ELEMENT = 16,
-	VTEMT_NODE_ID = 17,
-	VTEMT_EXPANDED_NODE_ID = 18,
-	VTEMT_STATUS_CODE = 19,
-	VTEMT_QUALIFIED_NAME = 20,
-	VTEMT_LOCALIZED_TEXT = 21,
-	VTEMT_EXTENSION_OBJECT = 22,
-	VTEMT_DATA_VALUE = 23,
-	VTEMT_VARIANT = 24,
-	VTEMT_DIAGNOSTIC_INFO = 25,
+	VTEMT_BOOLEAN = 			1,
+	VTEMT_SBYTE = 				2,
+	VTEMT_BYTE = 				3,
+	VTEMT_INT16 = 				4,
+	VTEMT_UINT16 = 				5,
+	VTEMT_INT32 = 				6,
+	VTEMT_UINT32 = 				7,
+	VTEMT_INT64 = 				8,
+	VTEMT_UINT64 = 				9,
+	VTEMT_FLOAT = 				10,
+	VTEMT_DOUBLE = 				11,
+	VTEMT_STRING = 				12,
+	VTEMT_DATE_TIME = 			13,
+	VTEMT_GUID = 				14,
+	VTEMT_BYTE_STRING = 		15,
+	VTEMT_XML_ELEMENT = 		16,
+	VTEMT_NODE_ID = 			17,
+	VTEMT_EXPANDED_NODE_ID = 	18,
+	VTEMT_STATUS_CODE = 		19,
+	VTEMT_QUALIFIED_NAME = 		20,
+	VTEMT_LOCALIZED_TEXT = 		21,
+	VTEMT_EXTENSION_OBJECT = 	22,
+	VTEMT_DATA_VALUE = 			23,
+	VTEMT_VARIANT = 			24,
+	VTEMT_DIAGNOSTIC_INFO = 	25,
 	//Byte 6
-	VTEMT_ARRAY_DIMENSIONS_ENCODED = 64,	//HEX 0x40
+	VTEMT_ARRAY_DIMENSIONS_ENCODED = 	0x40,
 	//Byte 7
-	VTEMT_ARRAY_VALUE_ENCODED = 128	//HEX 0x80
+	VTEMT_ARRAY_VALUE_ENCODED = 		0x80,
 }
 UA_VariantTypeEncodingMaskType;
 

+ 74 - 7
OPCUAServer/src/opcua_encodingLayer.c

@@ -8,12 +8,58 @@
 #include "opcua_binaryEncDec.h"
 #include "opcua_types.h"
 
-T_RequestHeader encodeRequestHeader(char* buf){
+/**
+* IntegerId
+* Part: 4
+* Chapter: 7.13
+* Page: 118
+*/
+T_IntegerId convertToTIntegerId(char* buf, int pos){
+	return convertToUInt32(buf, pos);
+}
+
+/**
+* DiagnosticInfo
+* Part: 4
+* Chapter: 7.9
+* Page: 116
+*/
+T_DiagnosticInfo convertToTDiagnosticInfo(char* buf, int pos){
+	T_DiagnosticInfo tmpTDiagnosticInfo;
+	int counter = 0;
+	tmpTDiagnosticInfo.namespaceUri = convertToInt32(buf, counter);
+	counter += sizeof(tmpTDiagnosticInfo.namespaceUri);
+	tmpTDiagnosticInfo.symbolicId = convertToInt32(buf, counter);
+	counter += sizeof(tmpTDiagnosticInfo.symbolicId);
+	tmpTDiagnosticInfo.locale = convertToInt32(buf, counter);
+	counter += sizeof(tmpTDiagnosticInfo.locale);
+	tmpTDiagnosticInfo.localizesText = convertToInt32(buf, counter);
+	counter += sizeof(tmpTDiagnosticInfo.localizesText);
+	tmpTDiagnosticInfo.additionalInfo = convertToUAString(buf, counter);
+	counter += sizeof(tmpTDiagnosticInfo.additionalInfo);
+	tmpTDiagnosticInfo.innerStatusCode = convertToUAStatusCode(buf, pos);
+	counter += sizeof(tmpTDiagnosticInfo.innerStatusCode);
+
+	//If the Flag InnerDiagnosticInfo is set, then the DiagnosticInfo will be encoded
+	if((tmpTDiagnosticInfo.innerStatusCode & DIEMT_INNER_DIAGNOSTIC_INFO) == 1){
+		tmpTDiagnosticInfo.innerDiagnosticInfo = convertToTDiagnosticInfo(buf, counter);
+	}
+
+	return tmpTDiagnosticInfo;
+}
+
+/**
+* RequestHeader
+* Part: 4
+* Chapter: 7.26
+* Page: 132
+*/
+T_RequestHeader encodeTRequestHeader(char* buf){
 	T_RequestHeader tmpRequestHeader;
 	int counter = 0 ;
 	//ToDo: counter needs the length of the buffer,
 	//		strings have in this type just the size of the pointer not of the content
-	tmpRequestHeader.authenticationToken = convertToUANodeId(*buf, counter);
+	tmpRequestHeader.authenticationToken = convertToUANodeId(buf, counter);
 	if(tmpRequestHeader.authenticationToken.EncodingByte ==  NIEVT_STRING){
 		counter = sizeof(tmpRequestHeader.authenticationToken.EncodingByte) +
 				sizeof(tmpRequestHeader.authenticationToken.Namespace) +
@@ -22,17 +68,38 @@ T_RequestHeader encodeRequestHeader(char* buf){
 	}else{
 		counter = sizeof(tmpRequestHeader.authenticationToken);
 	}
-	tmpRequestHeader.timestamp = convertToUADateTime(*buf, counter);
+	tmpRequestHeader.timestamp = convertToUADateTime(buf, counter);
 	counter += sizeof(tmpRequestHeader.timestamp);
-	tmpRequestHeader.requestHandle = convertToInt64(*buf, counter);
+	tmpRequestHeader.requestHandle = convertToTIntegerId(buf, counter);
 	counter += sizeof(tmpRequestHeader.requestHandle);
-	tmpRequestHeader.returnDiagnostics = convertToUInt32(*buf, counter);
+	tmpRequestHeader.returnDiagnostics = convertToUInt32(buf, counter);
 	counter += sizeof(tmpRequestHeader.returnDiagnostics);
-	tmpRequestHeader.auditEntryId = convertToUAString(*buf, counter);
+	tmpRequestHeader.auditEntryId = convertToUAString(buf, counter);
 	counter += sizeof(tmpRequestHeader.auditEntryId);
-	tmpRequestHeader.timeoutHint = convertToUInt32(*buf, counter);
+	tmpRequestHeader.timeoutHint = convertToUInt32(buf, counter);
 	counter += sizeof(tmpRequestHeader.timeoutHint);
 	// AdditionalHeader will stay empty, need to be changed if there is relevant information
 
 	return tmpRequestHeader;
 }
+
+/**
+* ResponseHeader
+* Part: 4
+* Chapter: 7.27
+* Page: 133
+*/
+T_ResponseHeader encodeTResponseHeader(char* buf){
+	T_ResponseHeader tmpResponseHeader;
+	int counter = 0;
+	tmpResponseHeader.timestamp = convertToUADateTime(buf, counter);
+	counter += sizeof(tmpResponseHeader.timestamp);
+	tmpResponseHeader.requestHandle = convertToTIntegerId(buf, counter);
+	counter += sizeof(tmpResponseHeader.requestHandle);
+	tmpResponseHeader.serviceResult = convertToUAStatusCode(buf, counter);
+	counter += sizeof(tmpResponseHeader.serviceResult);
+	tmpResponseHeader.serviceDiagnostics = convertToTDiagnosticInfo(buf, counter);
+
+
+	return tmpResponseHeader;
+}

+ 2 - 2
OPCUAServer/src/opcua_types.h

@@ -165,14 +165,14 @@ T_DataValue;
 */
 typedef struct _T_DiagnosticInfo
 {
-//ToDo	struct ???? identifier;				//ToDo: what kind of strcuture?
+	//ToDo	struct ???? identifier;	identifier is only needed for encoding????
 	Int32 namespaceUri;
 	Int32 symbolicId;
 	Int32 locale;
 	Int32 localizesText;
 	UA_String additionalInfo;
 	UA_StatusCode innerStatusCode;
-//ToDo	T_DiagnosticInfo innerDiagnosticInfo;
+	struct _T_DiagnosticInfo* innerDiagnosticInfo;
 }
 T_DiagnosticInfo;