generateSam.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * xml2ns0.c
  3. *
  4. * Created on: 21.04.2014
  5. * Author: mrt
  6. */
  7. #include "ua_xml.h"
  8. #include <ctype.h> // tolower
  9. /** @brief we need a variable global to the module to make it possible for the visitors to access the namespace */
  10. static Namespace* theNamespace;
  11. UA_Int32 UA_Node_getParent(const UA_Node* node, const UA_Node** parent) {
  12. UA_Int32 i = 0;
  13. DBG(printf("// UA_Node_getParent - node={i=%d}",UA_NodeId_getIdentifier(&(node->nodeId))));
  14. // FIXME: the data model is crap! we should have a single memory location which holds the parent NodeId
  15. for (; i < node->referencesSize; i++ ) {
  16. UA_Int32 refId = UA_NodeId_getIdentifier(&(node->references[i]->referenceTypeId));
  17. UA_Int32 isInverse = node->references[i]->isInverse;
  18. if (isInverse && (refId == 47 || refId == 46)) {
  19. Namespace_Entry_Lock* lock = UA_NULL;
  20. UA_Int32 retval;
  21. retval = Namespace_get(theNamespace, &(node->references[i]->targetId.nodeId),parent,&lock);
  22. Namespace_Entry_Lock_release(lock);
  23. if (retval == UA_SUCCESS) {
  24. DBG(printf(" has parent={i=%d}\n",UA_NodeId_getIdentifier(&((*parent)->nodeId))));
  25. } else {
  26. DBG(printf(" has non-existing parent={i=%d}\n",UA_NodeId_getIdentifier(&(node->references[i]->targetId.nodeId))));
  27. }
  28. return retval;
  29. }
  30. }
  31. // there is no parent, we are root
  32. DBG(printf(" is root\n"));
  33. *parent = UA_NULL;
  34. return UA_SUCCESS;
  35. }
  36. /** @brief recurse down to root and return root node */
  37. UA_Int32 UA_Node_getRoot(const UA_Node* node, const UA_Node** root) {
  38. UA_Int32 retval = UA_SUCCESS;
  39. const UA_Node* parent = UA_NULL;
  40. if ( (retval = UA_Node_getParent(node,&parent)) == UA_SUCCESS ) {
  41. if (parent != UA_NULL) { // recurse down to root node
  42. retval = UA_Node_getRoot(parent,root);
  43. } else { // node is root, terminate recursion
  44. *root = node;
  45. }
  46. }
  47. return retval;
  48. }
  49. /** @brief check if VariableNode needs a memory object. This is the
  50. * case if the parent is of type object and the root is type object
  51. **/
  52. _Bool UA_VariableNode_needsObject(const UA_VariableNode* node) {
  53. const UA_Node* parent = UA_NULL;
  54. if ( UA_Node_getParent((UA_Node*)node,&parent) == UA_SUCCESS ) {
  55. if (parent == UA_NULL)
  56. return UA_TRUE;
  57. if (parent->nodeClass == UA_NODECLASS_OBJECT ) {
  58. const UA_Node* root;
  59. if (UA_Node_getRoot(parent,&root) == UA_SUCCESS)
  60. if (root == UA_NULL || root->nodeClass == UA_NODECLASS_OBJECT )
  61. return UA_TRUE;
  62. }
  63. }
  64. return UA_FALSE;
  65. }
  66. /** @brief recurse down to root and get full qualified name */
  67. UA_Int32 UA_Node_getPath(const UA_Node* node, UA_list_List* list) {
  68. UA_Int32 retval = UA_SUCCESS;
  69. const UA_Node* parent = UA_NULL;
  70. if ( (retval = UA_Node_getParent(node,&parent)) == UA_SUCCESS ) {
  71. if (parent != UA_NULL) {
  72. // recurse down to root node
  73. UA_Int32 retval = UA_Node_getPath(parent,list);
  74. // and add our own name when we come back
  75. if (retval == UA_SUCCESS) {
  76. UA_list_addPayloadToBack(list,(void*)&(node->browseName.name));
  77. DBG(printf("// UA_Node_getPath - add id={i=%d},class=%d",UA_NodeId_getIdentifier(&(node->nodeId)),node->nodeClass));
  78. DBG(UA_String_printf(",name=",&(node->browseName.name)));
  79. }
  80. } else {
  81. // node is root, terminate recursion by adding own name
  82. UA_list_addPayloadToBack(list,(void*)&node->browseName.name);
  83. DBG(printf("// UA_Node_getPath - add id={i=%d},class=%d",UA_NodeId_getIdentifier(&(node->nodeId)),node->nodeClass));
  84. DBG(UA_String_printf(",name=",&(node->browseName.name)));
  85. }
  86. }
  87. return retval;
  88. }
  89. /** @brief some macros to lowercase the first character without copying around */
  90. #define F_cls "%c%.*s"
  91. #define LC_cls(str) tolower((str).data[0]), (str).length-1, &((str).data[1])
  92. void listPrintName(void * payload) {
  93. UA_ByteString* name = (UA_ByteString*) payload;
  94. if (name->length > 0) {
  95. printf("_" F_cls, LC_cls(*name));
  96. }
  97. }
  98. /** @brief declares all the top level objects in the server's application memory */
  99. void sam_declareAttribute(UA_Node const * node) {
  100. if (node->nodeClass == UA_NODECLASS_VARIABLE && UA_VariableNode_needsObject((UA_VariableNode*)node)) {
  101. UA_list_List list; UA_list_init(&list);
  102. UA_Int32 retval = UA_Node_getPath(node,&list);
  103. if (retval == UA_SUCCESS) {
  104. UA_VariableNode* vn = (UA_VariableNode*) node;
  105. printf("\t%s ", UA_[UA_ns0ToVTableIndex(vn->dataType.identifier.numeric)].name);
  106. UA_list_iteratePayload(&list,listPrintName);
  107. printf("; // i=%d\n", node->nodeId.identifier.numeric);
  108. } else {
  109. printf("// could not determine path for i=%d\n",node->nodeId.identifier.numeric);
  110. }
  111. UA_list_destroy(&list,UA_NULL);
  112. }
  113. }
  114. /** @brief declares all the buffers for string variables
  115. * FIXME: shall traverse down to the root object and create a unique name such as cstr_serverState_buildInfo_version
  116. */
  117. void sam_declareBuffer(UA_Node const * node) {
  118. if (node != UA_NULL && node->nodeClass == UA_NODECLASS_VARIABLE) {
  119. UA_VariableNode* vn = (UA_VariableNode*) node;
  120. switch (vn->dataType.identifier.numeric) {
  121. case UA_BYTESTRING_NS0:
  122. case UA_STRING_NS0:
  123. case UA_LOCALIZEDTEXT_NS0:
  124. case UA_QUALIFIEDNAME_NS0:
  125. printf("UA_Byte cstr_" F_cls "[] = \"\"\n",LC_cls(vn->browseName.name));
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. }
  132. /** @brief assigns the c-strings to the ua type strings.
  133. * FIXME: traverse down to top level objects and create a unique name such as cstr_serverState_buildInfo_version
  134. */
  135. void sam_assignBuffer(UA_Node const * node) {
  136. if (node != UA_NULL && node->nodeClass == UA_NODECLASS_VARIABLE) {
  137. UA_VariableNode* vn = (UA_VariableNode*) node;
  138. switch (vn->dataType.identifier.numeric) {
  139. case UA_BYTESTRING_NS0:
  140. case UA_STRING_NS0:
  141. printf("\tSAM_ASSIGN_CSTRING(cstr_" F_cls ",sam." F_cls ");\n",LC_cls(vn->browseName.name),LC_cls(vn->browseName.name));
  142. break;
  143. case UA_LOCALIZEDTEXT_NS0:
  144. printf("\tSAM_ASSIGN_CSTRING(cstr_" F_cls ",sam." F_cls ".text);\n",LC_cls(vn->browseName.name),LC_cls(vn->browseName.name));
  145. break;
  146. case UA_QUALIFIEDNAME_NS0:
  147. printf("\tSAM_ASSIGN_CSTRING(cstr_" F_cls ",sam." F_cls ".name);\n",LC_cls(vn->browseName.name),LC_cls(vn->browseName.name));
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. }
  154. void sam_attachToNamespace(UA_Node const * node) {
  155. if (node != UA_NULL && node->nodeClass == UA_NODECLASS_VARIABLE) {
  156. UA_VariableNode* vn = (UA_VariableNode*) node;
  157. printf("\tsam_attach(ns,%d,%s,&sam." F_cls ");\n",node->nodeId.identifier.numeric,UA_[UA_ns0ToVTableIndex(vn->dataType.identifier.numeric)].name, LC_cls(vn->browseName.name));
  158. }
  159. }
  160. UA_Int32 Namespace_getNumberOfComponents(Namespace const * ns, UA_NodeId const * id, UA_Int32* number) {
  161. UA_Int32 retval = UA_SUCCESS;
  162. UA_Node const * node;
  163. if ((retval = Namespace_get(ns,id,&node,UA_NULL)) != UA_SUCCESS)
  164. return retval;
  165. if (node == UA_NULL)
  166. return UA_ERR_INVALID_VALUE;
  167. UA_Int32 i, n;
  168. for (i = 0, n = 0; i < node->referencesSize; i++ ) {
  169. if (node->references[i]->referenceTypeId.identifier.numeric == 47 && node->references[i]->isInverse != UA_TRUE) {
  170. n++;
  171. }
  172. }
  173. *number = n;
  174. return retval;
  175. }
  176. UA_Int32 Namespace_getComponent(Namespace const * ns, UA_NodeId const * id, UA_Int32 idx, UA_NodeId** result) {
  177. UA_Int32 retval = UA_SUCCESS;
  178. UA_Node const * node;
  179. if ((retval = Namespace_get(ns,id,&node,UA_NULL)) != UA_SUCCESS)
  180. return retval;
  181. UA_Int32 i, n;
  182. for (i = 0, n = 0; i < node->referencesSize; i++ ) {
  183. if (node->references[i]->referenceTypeId.identifier.numeric == 47 && node->references[i]->isInverse != UA_TRUE) {
  184. n++;
  185. if (n == idx) {
  186. *result = &(node->references[i]->targetId.nodeId);
  187. return retval;
  188. }
  189. }
  190. }
  191. return UA_ERR_INVALID_VALUE;
  192. }
  193. UA_Int32 UAX_NodeId_encodeBinaryByMetaData(Namespace const * ns, UA_NodeId const * id, UA_Int32* pos, UA_ByteString *dst) {
  194. UA_Int32 i, retval = UA_SUCCESS;
  195. if (UA_NodeId_isBasicType(id)) {
  196. UA_Node const * result;
  197. Namespace_Entry_Lock* lock;
  198. if ((retval = Namespace_get(ns,id,&result,&lock)) == UA_SUCCESS)
  199. UA_Variant_encodeBinary(&((UA_VariableNode *) result)->value,pos,dst);
  200. } else {
  201. UA_Int32 nComp = 0;
  202. if ((retval = Namespace_getNumberOfComponents(ns,id,&nComp)) == UA_SUCCESS) {
  203. for (i=0; i < nComp; i++) {
  204. UA_NodeId* comp = UA_NULL;
  205. Namespace_getComponent(ns,id,i,&comp);
  206. UAX_NodeId_encodeBinaryByMetaData(ns,comp, pos, dst);
  207. }
  208. }
  209. }
  210. return retval;
  211. }
  212. UA_Int32 UAX_NodeId_encodeBinary(Namespace const * ns, UA_NodeId const * id, UA_Int32* pos, UA_ByteString *dst) {
  213. UA_Int32 retval = UA_SUCCESS;
  214. UA_Node const * node;
  215. Namespace_Entry_Lock* lock;
  216. if ((retval = Namespace_get(ns,id,&node,&lock)) == UA_SUCCESS) {
  217. if (node->nodeClass == UA_NODECLASS_VARIABLE) {
  218. retval = UA_Variant_encodeBinary(&((UA_VariableNode*) node)->value,pos,dst);
  219. }
  220. Namespace_Entry_Lock_release(lock);
  221. }
  222. return retval;
  223. }
  224. /** @ brief poor man's text template processor
  225. * for p in patterns: print p.s, iterate over namespace with p.v */
  226. typedef struct pattern {
  227. char* s;
  228. Namespace_nodeVisitor v;
  229. } pattern;
  230. pattern p[] = {
  231. { "/** server application memory - generated but manually adapted */\n",UA_NULL },
  232. { "#define SAM_ASSIGN_CSTRING(src,dst) do { dst.length = strlen(src)-1; dst.data = (UA_Byte*) src; } while(0)\n",UA_NULL },
  233. { "struct sam {\n", sam_declareAttribute },
  234. { "} sam;\n", UA_NULL },
  235. { UA_NULL, sam_declareBuffer },
  236. { "void sam_init(Namespace* ns) {\n", sam_assignBuffer },
  237. { UA_NULL, sam_attachToNamespace },
  238. { "}\n", UA_NULL },
  239. {UA_NULL, UA_NULL} // terminal node : both elements UA_NULL
  240. };
  241. int main(int argc, char** argv) {
  242. if (argc != 2) {
  243. printf("usage: %s filename\n",argv[0]);
  244. } else {
  245. Namespace* ns;
  246. if (Namespace_loadFromFile(&ns,0,"ROOT",argv[1]) != UA_SUCCESS) {
  247. printf("error loading file {%s}\n", argv[1]);
  248. } else {
  249. theNamespace = ns;
  250. for (pattern* pi = &p[0]; pi->s != UA_NULL || pi->v != UA_NULL; ++pi) {
  251. if (pi->s) {
  252. printf("%s",pi->s);
  253. }
  254. if (pi->v) {
  255. Namespace_iterate(ns, pi->v);
  256. }
  257. }
  258. // FIXME: crashes with a seg fault
  259. // Namespace_delete(ns);
  260. }
  261. }
  262. return 0;
  263. }