소스 검색

printing of all UA types as C-structs

Julius Pfrommer 10 년 전
부모
커밋
001e61ea9a
7개의 변경된 파일335개의 추가작업 그리고 87개의 파일을 삭제
  1. 9 12
      src/ua_application.c
  2. 0 5
      src/ua_namespace.c
  3. 5 2
      src/ua_namespace.h
  4. 278 55
      src/ua_types.c
  5. 18 5
      src/ua_types.h
  6. 23 8
      tools/generate_builtin.py
  7. 2 0
      tools/generate_namespace.py

+ 9 - 12
src/ua_application.c

@@ -48,8 +48,8 @@ void appMockup_init() {
 	UA_Array_new((void**)&v->value.data, 2, &UA_.types[UA_STRING]);
 	v->value.vt = &UA_.types[UA_STRING];
 	v->value.arrayLength = 2;
-	UA_String_copycstring("http://opcfoundation.org/UA/",((UA_String **)(((v)->value).data))[0]);
-	UA_String_copycstring("http://localhost:16664/open62541/",((UA_String **)(((v)->value).data))[1]);
+	UA_String_copycstring("http://opcfoundation.org/UA/",&((UA_String *)((v->value).data))[0]);
+	UA_String_copycstring("http://localhost:16664/open62541/",&((UA_String *)(((v)->value).data))[1]);
 	v->dataType.encodingByte = UA_NODEIDTYPE_FOURBYTE;
 	v->dataType.identifier.numeric = UA_STRING_NS0;
 	v->valueRank = 1;
@@ -59,16 +59,13 @@ void appMockup_init() {
 	Namespace_insert(ns0,np);
 
 #if defined(DEBUG) && defined(VERBOSE)
-	uint32_t i, j;
-	for (i=0, j=0; i < ns0->size && j < ns0->count; i++) {
-		// Namespace_Entry is opaque. The content cannot be accessed from here.
-		/* if (ns0->entries[i] != UA_NULL) { */
-		/* 	printf("appMockup_init - entries[%d]={",i); */
-		/* 	UA_NodeId_printf("nodeId=",&(ns0->entries[i].node->nodeId)); */
-		/* 	UA_String_printf(",browseName=",&(ns0->entries[i].node->browseName.name)); */
-		/* 	j++; */
-		/* 	printf("}\n"); */
-		/* } */
+	uint32_t i;
+	for (i=0;i < ns0->size;i++) {
+		if (ns0->entries[i].node != UA_NULL) {
+			printf("appMockup_init - entries[%d]={",i);
+			UA_Node_print(ns0->entries[i].node, stdout);
+			printf("}\n");
+		}
 	}
 #endif
 }

+ 0 - 5
src/ua_namespace.c

@@ -6,11 +6,6 @@
 /* Internal (not exported) functionality */
 /*****************************************/
 
-struct Namespace_Entry {
-	UA_UInt64 status;	/* 2 bits status | 14 bits checkout count | 48 bits timestamp */
-	const UA_Node *node;	/* Nodes are immutable. It is not recommended to change nodes in place */
-};
-
 struct Namespace_Entry_Lock {
 	Namespace_Entry *entry;
 };

+ 5 - 2
src/ua_namespace.h

@@ -13,8 +13,11 @@
 
 /** @brief Namespace entries point to an UA_Node. But the actual data structure
 	is opaque outside of ua_namespace.c */
-struct Namespace_Entry;
-typedef struct Namespace_Entry Namespace_Entry;
+
+typedef struct Namespace_Entry {
+	UA_UInt64 status;	/* 2 bits status | 14 bits checkout count | 48 bits timestamp */
+	const UA_Node *node;	/* Nodes are immutable. It is not recommended to change nodes in place */
+} Namespace_Entry;
 
 /** @brief Namespace datastructure. It mainly serves as a hashmap to UA_Nodes. */
 typedef struct Namespace {

+ 278 - 55
src/ua_types.c

@@ -24,47 +24,92 @@ UA_TYPE_DELETE_DEFAULT(UA_Boolean)
 UA_TYPE_DELETEMEMBERS_NOACTION(UA_Boolean)
 UA_TYPE_NEW_DEFAULT(UA_Boolean)
 UA_TYPE_COPY_DEFAULT(UA_Boolean)
+void UA_Boolean_print(const UA_Boolean *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	if(*p) fprintf(stream, "UA_TRUE");
+	else fprintf(stream, "UA_FALSE");
+}
 
 /* SByte */
 UA_TYPE_DEFAULT(UA_SByte)
+void UA_SByte_print(const UA_SByte *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	UA_SByte x = *p;
+	fprintf(stream, "%s%x\n", x<0?"-":"", x<0?-x:x);
+}
 
 /* Byte */
 UA_TYPE_DEFAULT(UA_Byte)
+void UA_Byte_print(const UA_Byte *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%x", *p);
+}
 
 /* Int16 */
 UA_TYPE_DEFAULT(UA_Int16)
+void UA_Int16_print(const UA_Int16 *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%d", *p);
+}
 
 /* UInt16 */
 UA_TYPE_DEFAULT(UA_UInt16)
+void UA_UInt16_print(const UA_UInt16 *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%u", *p);
+}
 
 /* Int32 */
 UA_TYPE_DEFAULT(UA_Int32)
+void UA_Int32_print(const UA_Int32 *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%d", *p);
+}
 
 /* UInt32 */
 UA_TYPE_DEFAULT(UA_UInt32)
+void UA_UInt32_print(const UA_UInt32 *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%u", *p);
+}
 
 /* Int64 */
 UA_TYPE_DEFAULT(UA_Int64)
+void UA_Int64_print(const UA_Int64 *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%ld", *p);
+}
 
 /* UInt64 */
 UA_TYPE_DEFAULT(UA_UInt64)
+void UA_UInt64_print(const UA_UInt64 *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%lu", *p);
+}
 
 /* Float */
-UA_TYPE_DELETE_DEFAULT(UA_Float)
-UA_TYPE_DELETEMEMBERS_NOACTION(UA_Float)
-UA_Int32 UA_Float_init(UA_Float *p) {
-	if(p == UA_NULL) return UA_ERROR;
-	*p = (UA_Float)0.0;
-	return UA_SUCCESS;
+UA_TYPE_DEFAULT(UA_Float)
+void UA_Float_print(const UA_Float *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%f", *p);
 }
-UA_TYPE_NEW_DEFAULT(UA_Float)
-UA_TYPE_COPY_DEFAULT(UA_Float)
 
 /* Double */
 UA_TYPE_DEFAULT(UA_Double)
+void UA_Double_print(const UA_Double *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "%f", *p);
+}
 
 /* String */
 UA_TYPE_NEW_DEFAULT(UA_String)
+UA_Int32 UA_String_init(UA_String *p) {
+	if(p == UA_NULL) return UA_ERROR;
+	p->length = -1;
+	p->data   = UA_NULL;
+	return UA_SUCCESS;
+}
+
 UA_TYPE_DELETE_DEFAULT(UA_String)
 UA_Int32 UA_String_deleteMembers(UA_String *p) {
 	UA_Int32 retval = UA_SUCCESS;
@@ -75,6 +120,7 @@ UA_Int32 UA_String_deleteMembers(UA_String *p) {
 	}
 	return retval;
 }
+
 UA_Int32 UA_String_copy(UA_String const *src, UA_String *dst) {
 	if(src == UA_NULL || dst == UA_NULL) return UA_ERROR;
 	UA_Int32 retval = UA_SUCCESS;
@@ -89,6 +135,12 @@ UA_Int32 UA_String_copy(UA_String const *src, UA_String *dst) {
 	}
 	return retval;
 }
+
+void UA_String_print(const UA_String *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_String){%d, \"%.*s\"}", p->length, p->length, p->data);
+}
+
 UA_Int32 UA_String_copycstring(char const *src, UA_String *dst) {
 	UA_Int32 retval = UA_SUCCESS;
 	dst->length = strlen(src);
@@ -128,13 +180,6 @@ UA_Int32 UA_String_copyprintf(char const *fmt, UA_String *dst, ...) {
 	return retval;
 }
 
-UA_Int32 UA_String_init(UA_String *p) {
-	if(p == UA_NULL) return UA_ERROR;
-	p->length = -1;
-	p->data   = UA_NULL;
-	return UA_SUCCESS;
-}
-
 UA_Int32 UA_String_equal(const UA_String *string1, const UA_String *string2) {
 	UA_Int32 retval;
 	if(string1->length == 0 && string2->length == 0)
@@ -263,6 +308,12 @@ UA_Int32 UA_Guid_copy(UA_Guid const *src, UA_Guid *dst) {
 	return retval;
 }
 
+void UA_Guid_print(const UA_Guid *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_Guid){%u, %u %u {%x,%x,%x,%x,%x,%x,%x,%x}}", p->data1, p->data2, p->data3, p->data4[0],
+			p->data4[1], p->data4[2], p->data4[3], p->data4[4], p->data4[5], p->data4[6], p->data4[7]);
+}
+
 /* ByteString */
 UA_TYPE_AS(UA_ByteString, UA_String)
 UA_Int32 UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2) {
@@ -283,8 +334,7 @@ void UA_ByteString_printx_hex(char *label, const UA_ByteString *string) {
 
 UA_Byte       UA_Byte_securityPoliceNoneData[] = "http://opcfoundation.org/UA/SecurityPolicy#None";
 // sizeof()-1 : discard the implicit null-terminator of the c-char-string
-UA_ByteString UA_ByteString_securityPoliceNone =
-{ sizeof(UA_Byte_securityPoliceNoneData)-1, UA_Byte_securityPoliceNoneData };
+UA_ByteString UA_ByteString_securityPoliceNone = { sizeof(UA_Byte_securityPoliceNoneData)-1, UA_Byte_securityPoliceNoneData };
 
 UA_Int32 UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length) {
 	UA_Int32 retval = UA_SUCCESS;
@@ -301,6 +351,43 @@ UA_Int32 UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length) {
 UA_TYPE_AS(UA_XmlElement, UA_ByteString)
 
 /* NodeId */
+UA_Int32 UA_NodeId_init(UA_NodeId *p) {
+	if(p == UA_NULL) return UA_ERROR;
+	p->encodingByte = UA_NODEIDTYPE_TWOBYTE;
+	p->namespace    = 0;
+	memset(&p->identifier, 0, sizeof(p->identifier));
+	return UA_SUCCESS;
+}
+
+UA_TYPE_NEW_DEFAULT(UA_NodeId)
+UA_Int32 UA_NodeId_copy(UA_NodeId const *src, UA_NodeId *dst) {
+	if(src == UA_NULL || dst == UA_NULL) return UA_ERROR;
+	UA_Int32 retval = UA_SUCCESS;
+	retval |= UA_Byte_copy(&src->encodingByte, &dst->encodingByte);
+
+	switch(src->encodingByte & UA_NODEIDTYPE_MASK) {
+	case UA_NODEIDTYPE_TWOBYTE:
+	case UA_NODEIDTYPE_FOURBYTE:
+	case UA_NODEIDTYPE_NUMERIC:
+		// nothing to do
+		retval |= UA_UInt16_copy(&src->namespace, &dst->namespace);
+		retval |= UA_UInt32_copy(&src->identifier.numeric, &dst->identifier.numeric);
+		break;
+
+	case UA_NODEIDTYPE_STRING: // Table 6, second entry
+		retval |= UA_String_copy(&src->identifier.string, &dst->identifier.string);
+		break;
+
+	case UA_NODEIDTYPE_GUID: // Table 6, third entry
+		retval |= UA_Guid_copy(&src->identifier.guid, &dst->identifier.guid);
+		break;
+
+	case UA_NODEIDTYPE_BYTESTRING: // Table 6, "OPAQUE"
+		retval |= UA_ByteString_copy(&src->identifier.byteString, &dst->identifier.byteString);
+		break;
+	}
+	return retval;
+}
 UA_Boolean UA_NodeId_isBasicType(UA_NodeId const *id) {
 	return (id->namespace == 0 && id->identifier.numeric <= UA_DIAGNOSTICINFO);
 }
@@ -331,6 +418,54 @@ UA_Int32 UA_NodeId_deleteMembers(UA_NodeId *p) {
 	return retval;
 }
 
+void UA_NodeId_print(const UA_NodeId *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_NodeId){");
+	switch(p->encodingByte & UA_NODEIDTYPE_MASK) {
+	case UA_NODEIDTYPE_TWOBYTE:
+		fprintf(stream, "UA_NODEIDTYPE_TWOBYTE");
+		break;
+	case UA_NODEIDTYPE_FOURBYTE:
+		fprintf(stream, "UA_NODEIDTYPE_FOURBYTE");
+	case UA_NODEIDTYPE_NUMERIC:
+		fprintf(stream, "UA_NODEIDTYPE_NUMERIC");
+		break;
+	case UA_NODEIDTYPE_STRING:
+		fprintf(stream, "UA_NODEIDTYPE_STRING");
+		break;
+	case UA_NODEIDTYPE_BYTESTRING:
+		fprintf(stream, "UA_NODEIDTYPE_BYTESTRING");
+		break;
+	case UA_NODEIDTYPE_GUID:
+		fprintf(stream, "UA_NODEIDTYPE_GUID");
+		break;
+	default:
+		fprintf(stream, "ERROR");
+		break;
+	}
+	fprintf(stream,",%u,", p->namespace);
+	switch(p->encodingByte & UA_NODEIDTYPE_MASK) {
+	case UA_NODEIDTYPE_TWOBYTE:
+	case UA_NODEIDTYPE_FOURBYTE:
+	case UA_NODEIDTYPE_NUMERIC:
+		fprintf(stream, ".identifier.numeric=%u", p->identifier.numeric);
+		break;
+	case UA_NODEIDTYPE_STRING:
+		fprintf(stream, ".identifier.string=%.*s", p->identifier.string.length, p->identifier.string.data);
+	case UA_NODEIDTYPE_BYTESTRING:
+		fprintf(stream, ".identifier.byteString=%.*s", p->identifier.byteString.length, p->identifier.byteString.data);
+		break;
+	case UA_NODEIDTYPE_GUID:
+		fprintf(stream, ".identifer.guid=");
+		UA_Guid_print(&p->identifier.guid, stream);
+		break;
+	default:
+		fprintf(stream, "ERROR");
+		break;
+	}
+	fprintf(stream, "}");
+}
+
 void UA_NodeId_printf(char *label, const UA_NodeId *node) {
 	UA_Int32 l;
 
@@ -390,44 +525,6 @@ UA_Int32 UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2) {
 	return UA_NOT_EQUAL;
 }
 
-UA_Int32 UA_NodeId_init(UA_NodeId *p) {
-	if(p == UA_NULL) return UA_ERROR;
-	p->encodingByte = UA_NODEIDTYPE_TWOBYTE;
-	p->namespace    = 0;
-	memset(&p->identifier, 0, sizeof(p->identifier));
-	return UA_SUCCESS;
-}
-
-UA_TYPE_NEW_DEFAULT(UA_NodeId)
-UA_Int32 UA_NodeId_copy(UA_NodeId const *src, UA_NodeId *dst) {
-	if(src == UA_NULL || dst == UA_NULL) return UA_ERROR;
-	UA_Int32 retval = UA_SUCCESS;
-	retval |= UA_Byte_copy(&src->encodingByte, &dst->encodingByte);
-
-	switch(src->encodingByte & UA_NODEIDTYPE_MASK) {
-	case UA_NODEIDTYPE_TWOBYTE:
-	case UA_NODEIDTYPE_FOURBYTE:
-	case UA_NODEIDTYPE_NUMERIC:
-		// nothing to do
-		retval |= UA_UInt16_copy(&src->namespace, &dst->namespace);
-		retval |= UA_UInt32_copy(&src->identifier.numeric, &dst->identifier.numeric);
-		break;
-
-	case UA_NODEIDTYPE_STRING: // Table 6, second entry
-		retval |= UA_String_copy(&src->identifier.string, &dst->identifier.string);
-		break;
-
-	case UA_NODEIDTYPE_GUID: // Table 6, third entry
-		retval |= UA_Guid_copy(&src->identifier.guid, &dst->identifier.guid);
-		break;
-
-	case UA_NODEIDTYPE_BYTESTRING: // Table 6, "OPAQUE"
-		retval |= UA_ByteString_copy(&src->identifier.byteString, &dst->identifier.byteString);
-		break;
-	}
-	return retval;
-}
-
 UA_Boolean UA_NodeId_isNull(const UA_NodeId *p) {
 	switch(p->encodingByte & UA_NODEIDTYPE_MASK) {
 	case UA_NODEIDTYPE_TWOBYTE:
@@ -486,6 +583,17 @@ UA_Int32 UA_ExpandedNodeId_copy(UA_ExpandedNodeId const *src, UA_ExpandedNodeId
 	return retval;
 }
 
+void UA_ExpandedNodeId_print(const UA_ExpandedNodeId *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_ExpandedNodeId){");
+	UA_NodeId_print(&p->nodeId, stream);
+	fprintf(stream, ",");
+	UA_String_print(&p->namespaceUri, stream);
+	fprintf(stream, ",");
+	UA_UInt32_print(&p->serverIndex, stream);
+	fprintf(stream, "}");
+}
+
 UA_Boolean UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p) {
 	return UA_NodeId_isNull(&p->nodeId);
 }
@@ -519,6 +627,15 @@ UA_Int32 UA_QualifiedName_copy(UA_QualifiedName const *src, UA_QualifiedName *ds
 
 }
 
+void UA_QualifiedName_print(const UA_QualifiedName *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_QualifiedName){");
+	UA_UInt16_print(&p->namespaceIndex, stream);
+	fprintf(stream, ",");
+	UA_String_print(&p->name, stream);
+	fprintf(stream, "}");
+}
+
 void UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn) {
 	printf("%s {NamespaceIndex=%u, Length=%d, Data=%.*s}\n", label, qn->namespaceIndex,
 		   qn->name.length, qn->name.length, (char *)qn->name.data);
@@ -564,6 +681,15 @@ UA_Int32 UA_LocalizedText_copy(UA_LocalizedText const *src, UA_LocalizedText *ds
 	return retval;
 }
 
+void UA_LocalizedText_print(const UA_LocalizedText *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_LocalizedText){");
+	UA_String_print(&p->locale, stream);
+	fprintf(stream, ",");
+	UA_String_print(&p->text, stream);
+	fprintf(stream, "}");
+}
+
 /* ExtensionObject */
 UA_TYPE_DELETE_DEFAULT(UA_ExtensionObject)
 UA_Int32 UA_ExtensionObject_deleteMembers(UA_ExtensionObject *p) {
@@ -592,6 +718,21 @@ UA_Int32 UA_ExtensionObject_copy(UA_ExtensionObject const *src, UA_ExtensionObje
 	return retval;
 }
 
+void UA_ExtensionObject_print(const UA_ExtensionObject *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_ExtensionObject){");
+	UA_NodeId_print(&p->typeId, stream);
+	if(p->encoding == UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING){
+		fprintf(stream, ",UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING,");
+	} else if(p->encoding == UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML) {
+		fprintf(stream, ",UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML,");
+	} else {
+		fprintf(stream, ",UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED,");
+	}
+	UA_ByteString_print(&p->body, stream);
+	fprintf(stream, "}");
+}
+
 /* DataValue */
 UA_TYPE_DELETE_DEFAULT(UA_DataValue)
 UA_Int32 UA_DataValue_deleteMembers(UA_DataValue *p) {
@@ -627,6 +768,25 @@ UA_Int32 UA_DataValue_copy(UA_DataValue const *src, UA_DataValue *dst) {
 	return retval;
 }
 
+void UA_DataValue_print(const UA_DataValue *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_DataValue){");
+	UA_Byte_print(&p->encodingMask, stream);
+	fprintf(stream, ",");
+	UA_Variant_print(&p->value, stream);
+	fprintf(stream, ",");
+	UA_StatusCode_print(&p->status, stream);
+	fprintf(stream, ",");
+	UA_DateTime_print(&p->sourceTimestamp, stream);
+	fprintf(stream, ",");
+	UA_Int16_print(&p->sourcePicoseconds, stream);
+	fprintf(stream, ",");
+	UA_DateTime_print(&p->serverTimestamp, stream);
+	fprintf(stream, ",");
+	UA_Int16_print(&p->serverPicoseconds, stream);
+	fprintf(stream, "}");
+}
+
 /* Variant */
 UA_TYPE_DELETE_DEFAULT(UA_Variant)
 UA_Int32 UA_Variant_deleteMembers(UA_Variant *p) {
@@ -673,6 +833,26 @@ UA_Int32 UA_Variant_copy(UA_Variant const *src, UA_Variant *dst) {
 	return retval;
 }
 
+void UA_Variant_print(const UA_Variant *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	UA_UInt32 ns0id = UA_ns0ToVTableIndex(&p->vt->typeId);
+	fprintf(stream, "(UA_Variant){/*%s*/", p->vt->name);
+	if(p->vt == &UA_.types[ns0id])
+		fprintf(stream, "UA_.types[%d]", ns0id);
+	else if(p->vt == &UA_borrowed_.types[ns0id])
+		fprintf(stream, "UA_borrowed_.types[%d]", ns0id);
+	else
+		fprintf(stream, "ERROR (not a builtin type)");
+	UA_Int32_print(&p->arrayLength, stream);
+	fprintf(stream, ",");
+	UA_Array_print(p->data, p->arrayLength, p->vt, stream);
+	fprintf(stream, ",");
+	UA_Int32_print(&p->arrayDimensionsLength, stream);
+	fprintf(stream, ",");
+	UA_Array_print(p->arrayDimensions, p->arrayDimensionsLength, &UA_.types[UA_INT32], stream);
+	fprintf(stream, "}");
+}
+
 UA_Int32 UA_Variant_copySetValue(UA_Variant *v, UA_VTable_Entry *vt, const void *value) {
 	if(v == UA_NULL || vt == UA_NULL || value == UA_NULL)
 		return UA_ERROR;
@@ -762,6 +942,32 @@ UA_Int32 UA_DiagnosticInfo_copy(UA_DiagnosticInfo const *src, UA_DiagnosticInfo
 	return retval;
 }
 
+void UA_DiagnosticInfo_print(const UA_DiagnosticInfo *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_DiagnosticInfo){");
+	UA_Byte_print(&p->encodingMask, stream);
+	fprintf(stream, ",");
+	UA_Int32_print(&p->symbolicId, stream);
+	fprintf(stream, ",");
+	UA_Int32_print(&p->namespaceUri, stream);
+	fprintf(stream, ",");
+	UA_Int32_print(&p->localizedText, stream);
+	fprintf(stream, ",");
+	UA_Int32_print(&p->locale, stream);
+	fprintf(stream, ",");
+	UA_String_print(&p->additionalInfo, stream);
+	fprintf(stream, ",");
+	UA_StatusCode_print(&p->innerStatusCode, stream);
+	fprintf(stream, ",");
+	if(p->innerDiagnosticInfo != UA_NULL) {
+		fprintf(stream, "&");
+		UA_DiagnosticInfo_print(p->innerDiagnosticInfo, stream);
+	} else {
+		fprintf(stream, "UA_NULL");
+	}
+	fprintf(stream, "}");
+}
+
 /* InvalidType */
 UA_Int32 UA_InvalidType_free(UA_InvalidType *p) {
 	return UA_ERR_INVALID_VALUE;
@@ -787,6 +993,11 @@ UA_Int32 UA_InvalidType_new(UA_InvalidType **p) {
 	return UA_ERR_INVALID_VALUE;
 }
 
+void UA_InvalidType_print(const UA_InvalidType *p, FILE *stream) {
+	if(p == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(UA_InvalidType){ERROR (invalid type)}");
+}
+
 /*********/
 /* Array */
 /*********/
@@ -879,3 +1090,15 @@ UA_Int32 UA_Array_copy(const void *src, UA_Int32 noElements, UA_VTable_Entry *vt
 
 	return retval;
 }
+
+void UA_Array_print(const void *p, UA_Int32 noElements, UA_VTable_Entry *vt, FILE *stream) {
+	if(p == UA_NULL || vt == UA_NULL || stream == UA_NULL) return;
+	fprintf(stream, "(%s){", vt->name);
+	char *cp = (char *)p; // so compilers allow pointer arithmetic
+	UA_UInt32 memSize = vt->memSize;
+	for(UA_Int32 i=0;i < noElements;i++) {
+		vt->print(cp, stream);
+		fprintf(stream, ",");
+		cp += memSize;
+	}
+}

+ 18 - 5
src/ua_types.h

@@ -1,6 +1,7 @@
 #ifndef UA_TYPES_H_
 #define UA_TYPES_H_
 
+#include <stdio.h>
 #include <stdint.h>
 
 /**
@@ -25,8 +26,8 @@
  * - <type>_delete: Frees the memory where the datatype was stored.
  *
  * - <type>_deleteMembers: Frees the memory of dynamically sized members (e.g. a
-     string) of a datatype. This is useful when the datatype was allocated on
-     the stack, whereas the dynamically sized members is heap-allocated.
+ *   string) of a datatype. This is useful when the datatype was allocated on
+ *   the stack, whereas the dynamically sized members is heap-allocated.
  *
  * @{
  */
@@ -166,7 +167,10 @@ enum UA_ExtensionObject_EncodingMaskType_enum {
 struct UA_VTable_Entry; // forwards declaration
 typedef struct UA_VTable_Entry UA_VTable_Entry;
 
-/** @brief A union of all of the types specified above. */
+/** @brief A union of all of the types specified above.
+ *
+ * Variants store (arrays of) built-in types. If you want to store a more
+ * complex (or self-defined) type, you have to use an UA_ExtensionObject.*/
 typedef struct UA_Variant {
 	UA_VTable_Entry *vt;          // internal entry into vTable
 	UA_Int32  arrayLength;        // total number of elements
@@ -236,7 +240,8 @@ typedef void UA_InvalidType;
     UA_Int32 TYPE##_init(TYPE * p);          \
     UA_Int32 TYPE##_delete(TYPE * p);        \
     UA_Int32 TYPE##_deleteMembers(TYPE * p); \
-    UA_Int32 TYPE##_copy(const TYPE *src, TYPE *dst);
+    UA_Int32 TYPE##_copy(const TYPE *src, TYPE *dst); \
+	void TYPE##_print(const TYPE *p, FILE *stream);
 
 /* Functions for all types */
 UA_TYPE_PROTOTYPES(UA_Boolean)
@@ -336,6 +341,7 @@ UA_Int32 UA_Array_delete(void *p, UA_Int32 noElements, UA_VTable_Entry *vt);
 
 /* @brief The destination array is allocated according to noElements. */
 UA_Int32 UA_Array_copy(const void *src, UA_Int32 noElements, UA_VTable_Entry *vt, void **dst);
+void UA_Array_print(const void *p, UA_Int32 noElements, UA_VTable_Entry *vt, FILE *stream);
 
 /**********/
 /* VTable */
@@ -367,6 +373,7 @@ struct UA_VTable_Entry {
 	UA_Int32   (*copy)(void const *src, void *dst);
 	UA_Int32   (*delete)(void *p);
 	UA_Int32   (*deleteMembers)(void *p);
+	void       (*print)(const void *p, FILE *stream);
 	UA_UInt32  memSize;       // size of the struct only in memory (no dynamic components)
 	UA_Boolean dynMembers;    // does the type contain members that are dynamically
 	                          // allocated (strings, ..)? Otherwise, the size on
@@ -442,12 +449,18 @@ typedef struct UA_VTable {
 		return TYPE_AS##_copy((TYPE_AS *)src, (TYPE_AS *)dst); \
 	}
 
+#define UA_TYPE_PRINT_AS(TYPE, TYPE_AS)                        \
+    void TYPE##_print(TYPE const *p, FILE *stream) {		   \
+		TYPE_AS##_print((TYPE_AS *)p, stream);				   \
+	}
+
 #define UA_TYPE_AS(TYPE, TYPE_AS)           \
     UA_TYPE_NEW_DEFAULT(TYPE)               \
     UA_TYPE_INIT_AS(TYPE, TYPE_AS)          \
     UA_TYPE_DELETE_AS(TYPE, TYPE_AS)        \
     UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
-    UA_TYPE_COPY_AS(TYPE, TYPE_AS)
+    UA_TYPE_COPY_AS(TYPE, TYPE_AS)			\
+	UA_TYPE_PRINT_AS(TYPE, TYPE_AS)
 
 /// @} /* end of group */
 

+ 23 - 8
tools/generate_builtin.py

@@ -144,8 +144,8 @@ def createStructured(element):
 
     # 4) CalcSizeBinary
     printc('''UA_Int32 %(name)s_calcSizeBinary(%(name)s const * ptr) {
-    \tif(ptr==UA_NULL) return sizeof(%(name)s);
-    \treturn 0''')
+    if(ptr==UA_NULL) return sizeof(%(name)s);
+    return 0''')
     has_fixed_size = True
     for n,t in membermap.iteritems():
         if t in fixed_size:
@@ -163,7 +163,7 @@ def createStructured(element):
 
     # 5) EncodeBinary
     printc('''UA_Int32 %(name)s_encodeBinary(%(name)s const * src, UA_ByteString* dst, UA_UInt32 *offset) {
-    \tUA_Int32 retval = UA_SUCCESS;''')
+    UA_Int32 retval = UA_SUCCESS;''')
     for n,t in membermap.iteritems():
         if t.find("*") != -1:
             printc("\tretval |= UA_Array_encodeBinary(src->%(n)s,src->%(n)sSize,&UA_.types[" +
@@ -174,7 +174,7 @@ def createStructured(element):
 
     # 6) DecodeBinary
     printc('''UA_Int32 %(name)s_decodeBinary(UA_ByteString const * src, UA_UInt32 *offset, %(name)s * dst) {
-    \tUA_Int32 retval = UA_SUCCESS;''')
+    UA_Int32 retval = UA_SUCCESS;''')
     printc('\t'+name+'_init(dst);')
     for n,t in membermap.iteritems():
         if t.find("*") != -1:
@@ -200,7 +200,7 @@ def createStructured(element):
 	
     # 9) DeleteMembers
     printc('''UA_Int32 %(name)s_deleteMembers(%(name)s *p) {
-    \tUA_Int32 retval = UA_SUCCESS;''')
+    UA_Int32 retval = UA_SUCCESS;''')
     for n,t in membermap.iteritems():
         if not t in fixed_size: # dynamic size on the wire
             if t.find("*") != -1:
@@ -212,7 +212,7 @@ def createStructured(element):
 
     # 10) Init
     printc('''UA_Int32 %(name)s_init(%(name)s *p) {
-    \tUA_Int32 retval = UA_SUCCESS;''')
+    UA_Int32 retval = UA_SUCCESS;''')
     for n,t in membermap.iteritems():
         if t.find("*") != -1:
             printc('\tp->%(n)sSize = 0;')
@@ -226,8 +226,8 @@ def createStructured(element):
 
     # 12) Copy
     printc('''UA_Int32 %(name)s_copy(const %(name)s *src,%(name)s *dst) {
-    \tif(src == UA_NULL || dst == UA_NULL) return UA_ERROR;
-    \tUA_Int32 retval = UA_SUCCESS;''')
+    if(src == UA_NULL || dst == UA_NULL) return UA_ERROR;
+    UA_Int32 retval = UA_SUCCESS;''')
     printc("\tmemcpy(dst, src, sizeof(%(name)s));")
     for n,t in membermap.iteritems():
         if t.find("*") != -1:
@@ -238,6 +238,21 @@ def createStructured(element):
         if not t in fixed_size: # there are members of variable size    
             printc('\tretval |= %(t)s_copy(&src->%(n)s,&dst->%(n)s);')
     printc("\treturn retval;\n}\n")
+
+    # 13) Print
+    printc('''void %(name)s_print(const %(name)s *p, FILE *stream) {
+    fprintf(stream, "(%(name)s){");''')
+    for i,(n,t) in enumerate(membermap.iteritems()):
+        if t.find("*") != -1:
+            printc('\tUA_Int32_print(&p->%(n)sSize, stream);')
+            printc("\tUA_Array_print(p->%(n)s, p->%(n)sSize, &UA_.types[" + t[0:t.find("*")].upper()+"], stream);")
+        else:
+            printc('\t%(t)s_print(&p->%(n)s,stream);')
+        if i == len(membermap)-1:
+            continue
+	printc('\tfprintf(stream, ",");')
+    printc('''\tfprintf(stream, "}");\n}\n''')
+    
 	
 printh('''/**
  * @file '''+sys.argv[2]+'''.h

+ 2 - 0
tools/generate_namespace.py

@@ -150,6 +150,7 @@ for row in rows:
           ",.copy=(UA_Int32(*)(void const * ,void*))%(name)s_copy" +
           ",.delete=(UA_Int32(*)(void *))%(name)s_delete" +
           ",.deleteMembers=(UA_Int32(*)(void *))%(name)s_deleteMembers" +
+          ",.print=(void(*)(const void *, FILE *))%(name)s_print" +
           ",.memSize=" + ("sizeof(%(name)s)" if (name != "UA_InvalidType") else "0") +
           ",.dynMembers=" + ("UA_FALSE" if (name in fixed_size) else "UA_TRUE") +
           ",.encodings={{.calcSize=(UA_calcSize)%(name)s_calcSizeBinary" +
@@ -183,6 +184,7 @@ for row in rows:
           ",.copy=(UA_Int32(*)(void const * ,void*))%(name)s_copy" +
           ",.delete=(UA_Int32(*)(void *))phantom_delete" +
           ",.deleteMembers=(UA_Int32(*)(void *))phantom_delete" +
+          ",.print=(void(*)(const void *, FILE *))%(name)s_print" +
           ",.memSize=" + ("sizeof(%(name)s)" if (name != "UA_InvalidType") else "0") +
           ",.dynMembers=" + ("UA_FALSE" if (name in fixed_size) else "UA_TRUE") +
           ",.encodings={{.calcSize=(UA_calcSize)" + name +"_calcSizeBinary" +