generateSam.c 9.6 KB

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