123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- * opcua_binaryEncDec.c
- *
- * Created on: Dec 18, 2013
- * Author: opcua
- */
- #include "opcua_binaryEncDec.h"
- /*
- * convert byte array to Byte
- */
- Byte convertToByte(char* buf, int pos)
- {
- return (Byte)buf[pos];
- }
- /*
- * convert byte array to UInt16
- */
- UInt16 convertToUInt16(char* buf, int pos)
- {
- Byte t1 = buf[pos];
- Int32 t2 = (UInt16)(buf[pos+1] << 8);
- return t1 + t2;
- }
- /*
- * convert byte array to Int32
- */
- Int32 convertToInt32(char* buf, int pos)
- {
- SByte t1 = buf[pos];
- Int32 t2 = (UInt32)(buf[pos+1] << 8);
- Int32 t3 = (UInt32)(buf[pos+2] << 16);
- Int32 t4 = (UInt32)(buf[pos+3] << 24);
- return t1 + t2 + t3 + t4;
- }
- /*
- * convert byte array to UInt32
- */
- UInt32 convertToUInt32(char* buf, int pos)
- {
- Byte t1 = buf[pos];
- UInt32 t2 = (UInt32)(buf[pos+1] << 8);
- UInt32 t3 = (UInt32)(buf[pos+2] << 16);
- UInt32 t4 = (UInt32)(buf[pos+3] << 24);
- return t1 + t2 + t3 + t4;
- }
- void convertUInt32ToByteArray(UInt32 value,char *buf,int pos)
- {
- buf[pos] = (char)(value && 0xFF);
- buf[pos + 1] = (char)((value >> 8) && 0xFF);
- buf[pos + 2] = (char)((value >> 16) && 0xFF);
- buf[pos + 3] = (char)((value >> 24) && 0xFF);
- }
- /*
- * convert byte array to Int64
- */
- Int64 convertToInt64(char* buf, int pos)
- {
- SByte t1 = buf[pos];
- UInt64 t2 = (UInt64)(buf[pos+1] << 8);
- UInt64 t3 = (UInt64)(buf[pos+2] << 16);
- UInt64 t4 = (UInt64)(buf[pos+3] << 24);
- UInt64 t5 = (UInt64)(buf[pos+4] << 32);
- UInt64 t6 = (UInt64)(buf[pos+5] << 40);
- UInt64 t7 = (UInt64)(buf[pos+6] << 48);
- UInt64 t8 = (UInt64)(buf[pos+7] << 56);
- return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
- }
- UA_String convertToUAString(char* buf, int pos)
- {
- UA_String tmpUAString;
- tmpUAString.Length = convertToInt32(buf,pos);
- tmpUAString.Data = &(buf[sizeof(UInt32)]);
- return tmpUAString;
- }
- UA_Guid convertToUAGuid(char* buf, int pos)
- {
- UA_Guid tmpUAGuid;
- int counter = 0;
- UInt32 i = 0;
- for(i = 1; i <= 4; i++)
- {
- tmpUAGuid.Data1[i] = convertToUInt32(*buf, pos+counter);
- counter += sizeof(UInt32);
- }
- for(i = 1; i <= 2; i++)
- {
- tmpUAGuid.Data2[i] = convertToUInt16(*buf, pos+counter);
- counter += sizeof(UInt16);
- }
- for(i = 1; i <= 2; i++)
- {
- tmpUAGuid.Data3[i] = convertToUInt16(*buf, pos+counter);
- counter += sizeof(UInt16);
- }
- for(i = 1; i <= 8; i++)
- {
- tmpUAGuid.Data4[i] = convertToByte(*buf, pos+counter);
- counter += sizeof(Byte);
- }
- return tmpUAGuid;
- }
- UA_NodeId convertToUANodeId(char* buf, int pos){
- UA_NodeId tmpUANodeId;
- tmpUANodeId.EncodingByte = convertToInt32(*buf, 0);
- int counter = sizeof(UInt32);
- UA_NodeIdEncodingValuesType encodingType = tmpUANodeId.EncodingByte;
- switch(encodingType)
- {
- case NIEVT_TWO_BYTE:
- {
- tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
- counter += sizeof(UInt16);
- break;
- }
- case NIEVT_FOUR_BYTE:
- {
- tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
- counter += sizeof(Int64);
- break;
- }
- case NIEVT_NUMERIC:
- {
- tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
- counter += sizeof(UInt32);
- break;
- }
- case NIEVT_STRING:
- {
- 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;
- counter += sizeof(UA_Guid);
- break;
- }
- case NIEVT_BYTESTRING:
- {
- //ToDo
- break;
- }
- case NIEVT_NAMESPACE_URI_FLAG:
- {
- break;
- }
- case NIEVT_SERVERINDEX_FLAG:
- {
- break;
- }
- }
- return tmpUANodeId;
- }
|