generateSam.c 9.6 KB

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