Browse Source

encodeUInt16 has been added. (with test)

MaximilianBauer 11 years ago
parent
commit
b53c0cb573
2 changed files with 29 additions and 8 deletions
  1. 8 1
      OPCUAServer/src/opcua_binaryEncDec.c
  2. 21 7
      OPCUAServer/tests/check_stack.c

+ 8 - 1
OPCUAServer/src/opcua_binaryEncDec.c

@@ -15,7 +15,6 @@ Byte convertToByte(const char *buf, Int32 *pos)
 	return (Byte) buf[(*pos)-1];
 
 }
-
 void encodeByte(Byte encodeByte, Int32 *pos, AD_RawMessage *dstBuf)
 {
 	dstBuf->message[*pos] = encodeByte;
@@ -32,6 +31,14 @@ UInt16 convertToUInt16(const char* buf, Int32 *pos)
 	*pos += 2;
 	return t1 + t2;
 }
+void encodeUInt16(UInt16 encodeUInt16, Int32 *pos, AD_RawMessage *dstBuf)
+{
+	dstBuf->message[*pos] = encodeUInt16;
+	dstBuf->message[*pos +1 ] = encodeUInt16 >>8;
+	*pos = (*pos) + sizeof(UInt16);
+	dstBuf->length = dstBuf->length + sizeof(UInt16);
+}
+
 Int16 convertToInt16(const char* buf, Int32 *pos)
 {
 

+ 21 - 7
OPCUAServer/tests/check_stack.c

@@ -50,20 +50,34 @@ START_TEST(test_decodeRequestHeader_validParameter)
 }
 END_TEST
 
-START_TEST(test_binaryEncDec_encodeByte)
+START_TEST(test_binaryEncDec_encoding)
 {
+
+	AD_RawMessage rawMessage;
+	Int32 position = 0;
+	//EncodeByte
+		char testChar = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+		rawMessage.message = &testChar;
 		Byte testByte = 0x08;
-		char testChar;
-		AD_RawMessage rawMessage;
 		rawMessage.length = 0;
-		rawMessage.message = &testChar;
-
-		Int32 position = 0;
+		position = 0;
 		encodeByte(testByte, &position, &rawMessage);
 
 		ck_assert_int_eq(rawMessage.message[0], 0x08);
 		ck_assert_int_eq(rawMessage.length, 1);
 		ck_assert_int_eq(position, 1);
+	//EncodeUInt16
+		char testChar1 = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+		rawMessage.message = &testChar1;
+		UInt16 testUInt16 = 0XABCD;
+		rawMessage.length = 0;
+		position = 0;
+		encodeUInt16(testUInt16, &position, &rawMessage);
+
+		ck_assert_int_eq(rawMessage.length, 2);
+		ck_assert_int_eq(position, 2);
+		ck_assert_int_eq((Byte) rawMessage.message[1], 0xAB);
+		ck_assert_int_eq((Byte) rawMessage.message[0], 0xCD);
 }
 END_TEST
 
@@ -81,7 +95,7 @@ Suite* TL_testSuite_encode(void)
 {
 	Suite *s = suite_create("encoding");
 	TCase *tc_core = tcase_create("Core");
-	tcase_add_test(tc_core,test_binaryEncDec_encodeByte);
+	tcase_add_test(tc_core,test_binaryEncDec_encoding);
 	suite_add_tcase(s,tc_core);
 	return s;
 }