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

cleanup, add gettimeofday for WIN32

Julius Pfrommer 10 роки тому
батько
коміт
ff343a4955
3 змінених файлів з 38 додано та 20 видалено
  1. 0 1
      src/ua_namespace.c
  2. 16 0
      src/ua_types.c
  3. 22 19
      src/ua_types_encoding_binary.c

+ 0 - 1
src/ua_namespace.c

@@ -255,7 +255,6 @@ UA_Int32 Namespace_new(Namespace ** result, UA_UInt32 namespaceId) {
 	return UA_SUCCESS;
 }
 
-
 UA_Int32 Namespace_delete(Namespace * ns) {
 	UA_UInt32 size = ns->size;
 	const UA_Node **entries = ns->entries;

+ 16 - 0
src/ua_types.c

@@ -275,6 +275,22 @@ UA_TYPE_AS(UA_DateTime, UA_Int64)
 #define HUNDRED_NANOSEC_PER_USEC 10LL
 #define HUNDRED_NANOSEC_PER_SEC (HUNDRED_NANOSEC_PER_USEC * 1000000LL)
 
+#ifdef WIN32
+static const unsigned __int64 epoch = 116444736000000000;
+int gettimeofday(struct timeval * tp, struct timezone * tzp) {
+	FILETIME ft;
+	SYSTEMTIME st;
+	ULARGE_INTEGER ul;
+	GetSystemTime(&st);
+	SystemTimeToFileTime(&st, &ft);
+	ul.LowPart = ft.dwLowDateTime;
+	ul.HighPart = ft.dwHighDateTime;
+	tp->tv_sec = (ul.QuadPart - epoch) / 10000000L;
+	tp->tv_usec = st.wMilliseconds * 1000;
+	return 0;
+}
+#endif
+
 // IEC 62541-6 §5.2.2.5  A DateTime value shall be encoded as a 64-bit signed integer
 // which represents the number of 100 nanosecond intervals since January 1, 1601 (UTC).
 UA_DateTime UA_DateTime_now() {

+ 22 - 19
src/ua_types_encoding_binary.c

@@ -10,12 +10,12 @@ static INLINE UA_Boolean is_builtin(UA_NodeId *typeid) {
 /*********/
 
 UA_Int32 UA_Array_calcSizeBinary(UA_Int32 nElements, UA_VTable_Entry *vt, const void *data) {
-	if(vt == UA_NULL){
+	if(vt == UA_NULL)
 		return 0; // do not return error as the result will be used to allocate memory
-	}
-	if(data == UA_NULL){ //NULL Arrays are encoded as length = -1
+
+	if(data == UA_NULL) //NULL Arrays are encoded as length = -1
 		return sizeof(UA_Int32);
-	}
+
 	UA_Int32  length     = sizeof(UA_Int32);
 	UA_UInt32 memSize    = vt->memSize;
 	const UA_Byte *cdata = (const UA_Byte *)data;
@@ -26,14 +26,13 @@ UA_Int32 UA_Array_calcSizeBinary(UA_Int32 nElements, UA_VTable_Entry *vt, const
 	return length;
 }
 
-static UA_Int32 UA_Array_calcSizeBinary_asExtensionObject(UA_Int32 nElements, UA_VTable_Entry *vt,
-														   const void *data) {
-	if(vt == UA_NULL){
+static UA_Int32 UA_Array_calcSizeBinary_asExtensionObject(UA_Int32 nElements, UA_VTable_Entry *vt, const void *data) {
+	if(vt == UA_NULL)
 		return 0; // do not return error as the result will be used to allocate memory
-	}
-	if(data == UA_NULL){ //NULL Arrays are encoded as length = -1
+	
+	if(data == UA_NULL) //NULL Arrays are encoded as length = -1
 		return sizeof(UA_Int32);
-	}
+	
 	UA_Int32  length     = sizeof(UA_Int32);
 	UA_UInt32 memSize    = vt->memSize;
 	UA_Boolean isBuiltin = is_builtin(&vt->typeId);
@@ -47,14 +46,14 @@ static UA_Int32 UA_Array_calcSizeBinary_asExtensionObject(UA_Int32 nElements, UA
 	return length;
 }
 
-UA_Int32 UA_Array_encodeBinary(const void *src, UA_Int32 noElements, UA_VTable_Entry *vt, UA_ByteString *dst,
-                               UA_UInt32 *offset) {
+UA_Int32 UA_Array_encodeBinary(const void *src, UA_Int32 noElements, UA_VTable_Entry *vt, UA_ByteString *dst, UA_UInt32 *offset) {
 	UA_Int32 retval = UA_SUCCESS;
 	if(vt == UA_NULL || dst == UA_NULL || offset == UA_NULL || ((src == UA_NULL) && (noElements > 0)))
 		return UA_ERROR;
 
 	//Null Arrays are encoded with length = -1 // part 6 - §5.24
-	if(noElements < -1) noElements = -1;
+	if(noElements < -1)
+		noElements = -1;
 
 	retval = UA_Int32_encodeBinary(&noElements, dst, offset);
 	const UA_Byte *csrc = (const UA_Byte *)src;
@@ -315,8 +314,12 @@ UA_Int32 UA_String_calcSizeBinary(UA_String const *string) {
 }
 UA_Int32 UA_String_encodeBinary(UA_String const *src, UA_ByteString *dst, UA_UInt32 *offset) {
 	UA_Int32 retval = UA_SUCCESS;
-	if(src == UA_NULL) return UA_ERR_INVALID_VALUE;
-	if((UA_Int32)(*offset+ UA_String_calcSizeBinary(src)) > dst->length) return UA_ERR_INVALID_VALUE;
+	if(src == UA_NULL)
+		return UA_ERR_INVALID_VALUE;
+
+	if((UA_Int32)(*offset+ UA_String_calcSizeBinary(src)) > dst->length)
+		return UA_ERR_INVALID_VALUE;
+
 	retval |= UA_Int32_encodeBinary(&src->length, dst, offset);
 	if(src->length > 0) {
 		retval  |= UA_memcpy(&dst->data[*offset], src->data, src->length);
@@ -328,8 +331,10 @@ UA_Int32 UA_String_decodeBinary(UA_ByteString const *src, UA_UInt32 *offset, UA_
 	UA_Int32 retval = UA_SUCCESS;
 	UA_String_init(dst);
 	retval |= UA_Int32_decodeBinary(src, offset, &dst->length);
+
 	if(dst->length > (UA_Int32)(src->length - *offset))
 		retval = UA_ERR_INVALID_VALUE;
+
 	if(retval != UA_SUCCESS || dst->length <= 0) {
 		dst->length = -1;
 		dst->data   = UA_NULL;
@@ -347,10 +352,7 @@ UA_TYPE_BINARY_ENCODING_AS(UA_DateTime, UA_Int64)
 
 /* Guid */
 UA_Int32 UA_Guid_calcSizeBinary(UA_Guid const *p) {
-	if(p == UA_NULL)
-		return sizeof(UA_Guid);
-	else
-		return 16;
+	return 16;
 }
 
 UA_TYPE_ENCODEBINARY(UA_Guid,
@@ -522,6 +524,7 @@ UA_TYPE_ENCODEBINARY(UA_ExpandedNodeId,
                      if(src->nodeId.encodingByte & UA_NODEIDTYPE_SERVERINDEX_FLAG)
 						 retval |= UA_UInt32_encodeBinary(&src->serverIndex, dst, offset);
                      )
+
 UA_Int32 UA_ExpandedNodeId_decodeBinary(UA_ByteString const *src, UA_UInt32 *offset, UA_ExpandedNodeId *dst) {
 	UA_UInt32 retval = UA_SUCCESS;
 	UA_ExpandedNodeId_init(dst);