xml2ns0.c 7.7 KB

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