xml2ns0.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <fcntl.h> // open, O_RDONLY
  2. #include "ua_types.h"
  3. //#include "ua_types_generated.h"
  4. #include "ua_namespace.h"
  5. #include "ua_xml.h"
  6. #include "ua_namespace_xml.h"
  7. #include "ua_types_encoding_xml.h"
  8. #include "util/ua_util.h"
  9. // FIXME: most of the following code should be generated as a template from the ns0-defining xml-files
  10. /** @brief sam (abbr. for server_application_memory) is a flattened memory structure of the UAVariables in ns0 */
  11. struct sam {
  12. UA_ServerStatusDataType serverStatus;
  13. } sam;
  14. char* productName = "xml2ns0";
  15. char* productUri = "http://open62541.org/xml2ns0/";
  16. char* manufacturerName = "open62541";
  17. char* softwareVersion = "0.01";
  18. char* buildNumber = "999-" __DATE__ "-001" ;
  19. #define SAM_ASSIGN_CSTRING(src,dst) do { \
  20. dst.length = strlen(src)-1; \
  21. dst.data = (UA_Byte*) src; \
  22. } while(0)
  23. void sam_attach(Namespace *ns,UA_UInt32 ns0id,UA_Int32 type, void* p) {
  24. UA_NodeId nodeid;
  25. nodeid.namespace = 0; // we cannot access ns->namespaceId;
  26. nodeid.identifier.numeric = ns0id;
  27. nodeid.encodingByte = UA_NODEIDTYPE_FOURBYTE;
  28. const UA_Node* result;
  29. UA_Int32 retval = Namespace_get(ns,&nodeid,&result);
  30. if (retval == UA_SUCCESS) {
  31. if (result->nodeId.identifier.numeric == ns0id) {
  32. if (result->nodeClass == UA_NODECLASS_VARIABLE) {
  33. UA_VariableNode* variable = (UA_VariableNode*) result;
  34. if (variable->dataType.identifier.numeric == UA_.types[type].typeId.identifier.numeric) {
  35. variable->value.arrayLength = 1;
  36. variable->value.data = p;
  37. } else {
  38. UA_alloc((void**)&variable->value.data, sizeof(UA_ExtensionObject));
  39. UA_ExtensionObject* eo = (UA_ExtensionObject*) variable->value.data;
  40. eo->typeId = variable->dataType;
  41. eo->encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  42. // FIXME: This code is valid for ns0 and numeric identifiers only
  43. eo->body.length = UA_.types[UA_ns0ToVTableIndex(&variable->dataType)].memSize;
  44. eo->body.data = p;
  45. }
  46. }
  47. }
  48. Namespace_releaseManagedNode(result);
  49. }
  50. }
  51. /* } else { */
  52. /* perror("Namespace_getWritable returned wrong node class"); */
  53. /* } */
  54. /* } else { */
  55. /* printf("retrieving node={i=%d} returned node={i=%d}\n", ns0id, result->nodeId.identifier.numeric); */
  56. /* } */
  57. /* Namespace_Entry_Lock_release(lock); */
  58. /* } else { */
  59. /* printf("retrieving node={i=%d} returned error code %d\n",ns0id,retval); */
  60. /* } */
  61. void sam_init(Namespace* ns) {
  62. // Initialize the strings
  63. SAM_ASSIGN_CSTRING(productName,sam.serverStatus.buildInfo.productName);
  64. SAM_ASSIGN_CSTRING(productUri,sam.serverStatus.buildInfo.productUri);
  65. SAM_ASSIGN_CSTRING(manufacturerName,sam.serverStatus.buildInfo.manufacturerName);
  66. SAM_ASSIGN_CSTRING(softwareVersion,sam.serverStatus.buildInfo.softwareVersion);
  67. SAM_ASSIGN_CSTRING(buildNumber,sam.serverStatus.buildInfo.buildNumber);
  68. // Attach server application memory to ns0
  69. sam_attach(ns,2256,UA_SERVERSTATUSDATATYPE,&sam.serverStatus); // this is the head of server status!
  70. sam.serverStatus.startTime = sam.serverStatus.currentTime = UA_DateTime_now();
  71. sam_attach(ns,2257,UA_DATETIME, &sam.serverStatus.startTime);
  72. sam_attach(ns,2258,UA_DATETIME, &sam.serverStatus.currentTime);
  73. sam_attach(ns,2259,UA_SERVERSTATE, &sam.serverStatus.state);
  74. sam_attach(ns,2260,UA_BUILDINFO, &sam.serverStatus.buildInfo); // start of build Info
  75. sam_attach(ns,2261,UA_STRING, &sam.serverStatus.buildInfo.productName);
  76. sam_attach(ns,2262,UA_STRING, &sam.serverStatus.buildInfo.productUri);
  77. sam_attach(ns,2263,UA_STRING, &sam.serverStatus.buildInfo.manufacturerName);
  78. sam_attach(ns,2264,UA_STRING, &sam.serverStatus.buildInfo.softwareVersion);
  79. sam_attach(ns,2265,UA_STRING, &sam.serverStatus.buildInfo.buildNumber);
  80. sam_attach(ns,2266,UA_DATETIME, &sam.serverStatus.buildInfo.buildDate);
  81. sam_attach(ns,2992,UA_UINT32, &sam.serverStatus.secondsTillShutdown);
  82. sam_attach(ns,2993,UA_LOCALIZEDTEXT,&sam.serverStatus.shutdownReason);
  83. }
  84. UA_Int32 Namespace_getNumberOfComponents(Namespace const * ns, UA_NodeId const * id, UA_Int32* number) {
  85. UA_Int32 retval = UA_SUCCESS;
  86. UA_Node const * node;
  87. if ((retval = Namespace_get(ns,id,&node)) != UA_SUCCESS)
  88. return retval;
  89. if (node == UA_NULL)
  90. return UA_ERR_INVALID_VALUE;
  91. UA_Int32 i, n;
  92. for (i = 0, n = 0; i < node->referencesSize; i++ ) {
  93. if (node->references[i].referenceTypeId.identifier.numeric == 47 && node->references[i].isInverse != UA_TRUE) {
  94. n++;
  95. }
  96. }
  97. Namespace_releaseManagedNode(node);
  98. *number = n;
  99. return retval;
  100. }
  101. UA_Int32 Namespace_getComponent(Namespace const * ns, UA_NodeId const * id, UA_Int32 idx, UA_NodeId** result) {
  102. UA_Int32 retval = UA_SUCCESS;
  103. UA_Node const * node;
  104. if ((retval = Namespace_get(ns,id,&node)) != UA_SUCCESS)
  105. return retval;
  106. UA_Int32 i, n;
  107. for (i = 0, n = 0; i < node->referencesSize; i++ ) {
  108. if (node->references[i].referenceTypeId.identifier.numeric == 47 && node->references[i].isInverse != UA_TRUE) {
  109. n++;
  110. if (n == idx) {
  111. *result = &(node->references[i].targetId.nodeId);
  112. Namespace_releaseManagedNode(node);
  113. return retval;
  114. }
  115. }
  116. }
  117. Namespace_releaseManagedNode(node);
  118. return UA_ERR_INVALID_VALUE;
  119. }
  120. UA_Int32 UAX_NodeId_encodeBinaryByMetaData(Namespace const * ns, UA_NodeId const * id, UA_ByteString *dst, UA_UInt32* offset) {
  121. UA_Int32 i, retval = UA_SUCCESS;
  122. if (UA_NodeId_isBasicType(id)) {
  123. UA_Node const * result;
  124. if ((retval = Namespace_get(ns,id,&result)) == UA_SUCCESS) {
  125. UA_Variant_encodeBinary(&((UA_VariableNode *) result)->value,dst,offset);
  126. Namespace_releaseManagedNode(result);
  127. }
  128. } else {
  129. UA_Int32 nComp = 0;
  130. if ((retval = Namespace_getNumberOfComponents(ns,id,&nComp)) == UA_SUCCESS) {
  131. for (i=0; i < nComp; i++) {
  132. UA_NodeId* comp = UA_NULL;
  133. Namespace_getComponent(ns,id,i,&comp);
  134. UAX_NodeId_encodeBinaryByMetaData(ns,comp, dst, offset);
  135. }
  136. }
  137. }
  138. return retval;
  139. }
  140. UA_Int32 UAX_NodeId_encodeBinary(Namespace const * ns, UA_NodeId const * id, UA_ByteString *dst, UA_UInt32* offset) {
  141. UA_Int32 retval = UA_SUCCESS;
  142. UA_Node const * node;
  143. if ((retval = Namespace_get(ns,id,&node)) == UA_SUCCESS) {
  144. if (node->nodeClass == UA_NODECLASS_VARIABLE) {
  145. retval = UA_Variant_encodeBinary(&((UA_VariableNode*) node)->value,dst,offset);
  146. }
  147. Namespace_releaseManagedNode(node);
  148. }
  149. return retval;
  150. }
  151. int main() {
  152. Namespace* ns;
  153. UA_Int32 retval;
  154. retval = Namespace_loadFromFile(&ns, 0, "ROOT", UA_NULL);
  155. sam_init(ns);
  156. // Namespace_iterate(ns, print_node);
  157. // encoding buffer
  158. char buf[1024];
  159. UA_ByteString buffer = { 1024, (UA_Byte*) buf };
  160. UA_UInt32 pos = 0;
  161. UA_NodeId nodeid;
  162. nodeid.encodingByte = UA_NODEIDTYPE_FOURBYTE;
  163. nodeid.namespace = 0;
  164. nodeid.identifier.numeric = 2256; // ServerStatus
  165. UA_Int32 i=0;
  166. retval=UA_SUCCESS;
  167. UA_DateTime tStart = UA_DateTime_now();
  168. // encoding takes roundabout 10 µs on my virtual machine with -O0, so 1E5 takes a second
  169. for (i=0;i<1E5 && retval == UA_SUCCESS;i++) {
  170. pos = 0;
  171. sam.serverStatus.currentTime = UA_DateTime_now();
  172. retval |= UAX_NodeId_encodeBinary(ns,&nodeid,&buffer,&pos);
  173. }
  174. UA_DateTime tEnd = UA_DateTime_now();
  175. // tStart, tEnd count in 100 ns steps, so 10 steps = 1 µs
  176. UA_Double tDelta = ( tEnd - tStart ) / ( 10.0 * i );
  177. printf("encode server node %d times, retval=%d: time/enc=%f us, enc/s=%f\n",i, retval, tDelta, 1.0E6 / tDelta);
  178. DBG(buffer.length=pos);
  179. DBG(UA_ByteString_printx(", result=", &buffer));
  180. // Design alternative two : use meta data
  181. // pos = 0;
  182. // buffer.length = 1024;
  183. // UAX_NodeId_encodeBinary(n.ns,&nodeid,&pos,&buffer);
  184. // buffer.length = pos;
  185. // UA_ByteString_printx("namespace based encoder result=", &buffer);
  186. return 0;
  187. }