ua_application.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "ua_application.h"
  2. #include "ua_namespace.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. UA_indexedList_List nsMockup;
  6. Application appMockup = {
  7. ( UA_ApplicationDescription*) UA_NULL,
  8. &nsMockup
  9. };
  10. UA_Node* create_node_ns0(UA_Int32 class, UA_Int32 nodeClass, UA_Int32 const id, char const * qn, char const * dn, char const * desc) {
  11. UA_Node* n; UA_.types[class].new((void **)&n);
  12. n->nodeId.encodingByte = UA_NODEIDTYPE_FOURBYTE;
  13. n->nodeId.namespace = 0;
  14. n->nodeId.identifier.numeric = id;
  15. UA_String_copycstring(qn,&(n->browseName.name));
  16. UA_String_copycstring(dn,&n->displayName.text);
  17. UA_String_copycstring(desc,&n->description.text);
  18. n->nodeClass = nodeClass;
  19. return n;
  20. }
  21. #define C2UA_STRING(s) (UA_String) { sizeof(s)-1, (UA_Byte*) s }
  22. void appMockup_init() {
  23. // create namespaces
  24. // TODO: A table that maps the namespaceUris to Ids
  25. Namespace* ns0;
  26. Namespace_new(&ns0, 100, 0); //C2UA_STRING("http://opcfoundation.org/UA/"));
  27. Namespace* local;
  28. Namespace_new(&local, 100, 1); //C2UA_STRING("http://localhost:16664/open62541/"));
  29. // add to list of namespaces
  30. UA_indexedList_init(appMockup.namespaces);
  31. UA_indexedList_addValueToFront(appMockup.namespaces,0,ns0);
  32. UA_indexedList_addValueToFront(appMockup.namespaces,1,local);
  33. /***************/
  34. /* Namespace 0 */
  35. /***************/
  36. // ReferenceTypes
  37. UA_NodeId RefTypeId_Organizes = NS0NODEID(35);
  38. /* UA_NodeId RefTypeId_HasEventSource = NS0NODEID(36); */
  39. /* UA_NodeId RefTypeId_HasModellingRule = NS0NODEID(37); */
  40. /* UA_NodeId RefTypeId_HasEncoding = NS0NODEID(38); */
  41. /* UA_NodeId RefTypeId_HasDescription = NS0NODEID(39); */
  42. UA_NodeId RefTypeId_HasTypeDefinition = NS0NODEID(40);
  43. /* UA_NodeId RefTypeId_HasSubtype = NS0NODEID(45); */
  44. UA_NodeId RefTypeId_HasProperty = NS0NODEID(46);
  45. UA_NodeId RefTypeId_HasComponent = NS0NODEID(47);
  46. /* UA_NodeId RefTypeId_HasNotifier = NS0NODEID(48); */
  47. // ObjectTypes (Ids only)
  48. UA_ExpandedNodeId ObjTypeId_FolderType = NS0EXPANDEDNODEID(61);
  49. // Objects (Ids only)
  50. UA_ExpandedNodeId ObjId_ObjectsFolder = NS0EXPANDEDNODEID(85);
  51. UA_ExpandedNodeId ObjId_TypesFolder = NS0EXPANDEDNODEID(86);
  52. UA_ExpandedNodeId ObjId_ViewsFolder = NS0EXPANDEDNODEID(87);
  53. UA_ExpandedNodeId ObjId_Server = NS0EXPANDEDNODEID(2253);
  54. UA_ExpandedNodeId ObjId_ServerArray = NS0EXPANDEDNODEID(2254);
  55. UA_ExpandedNodeId ObjId_NamespaceArray = NS0EXPANDEDNODEID(2255);
  56. UA_ExpandedNodeId ObjId_ServerStatus = NS0EXPANDEDNODEID(2256);
  57. UA_ExpandedNodeId ObjId_ServerCapabilities = NS0EXPANDEDNODEID(2268);
  58. UA_ExpandedNodeId ObjId_State = NS0EXPANDEDNODEID(2259);
  59. // FolderType
  60. UA_ObjectNode *folderType;
  61. UA_ObjectNode_new(&folderType);
  62. folderType->nodeId = NS0NODEID(61);
  63. folderType->nodeClass = UA_NODECLASS_OBJECT; // I should not have to set this manually
  64. folderType->browseName = (UA_QualifiedName){0, {10, "FolderType"}};
  65. folderType->displayName = (UA_LocalizedText){{2,"EN"},{10, "FolderType"}};
  66. folderType->description = (UA_LocalizedText){{2,"EN"},{10, "FolderType"}};
  67. folderType->referencesSize = 0;
  68. folderType->references = UA_NULL;
  69. Namespace_insert(ns0,(UA_Node*)folderType);
  70. // Root
  71. UA_ObjectNode *root;
  72. UA_ObjectNode_new(&root);
  73. root->nodeId = NS0NODEID(84);
  74. root->nodeClass = UA_NODECLASS_OBJECT; // I should not have to set this manually
  75. root->browseName = (UA_QualifiedName){0, {4, "Root"}};
  76. root->displayName = (UA_LocalizedText){{2,"EN"},{4, "Root"}};
  77. root->description = (UA_LocalizedText){{2,"EN"},{4, "Root"}};
  78. root->referencesSize = 4;
  79. UA_alloc((void**) &root->references, sizeof(UA_ReferenceNode)*4);
  80. UA_ReferenceNode *static_nodes = (UA_ReferenceNode[4]){
  81. {RefTypeId_HasTypeDefinition, UA_FALSE, ObjTypeId_FolderType},
  82. {RefTypeId_Organizes, UA_FALSE, ObjId_ObjectsFolder},
  83. {RefTypeId_Organizes, UA_FALSE, ObjId_TypesFolder},
  84. {RefTypeId_Organizes, UA_FALSE, ObjId_ViewsFolder}};
  85. UA_memcpy(root->references, static_nodes, sizeof(UA_ReferenceNode)*4);
  86. Namespace_insert(ns0,(UA_Node*)root);
  87. // Objects
  88. UA_ObjectNode *objects;
  89. UA_ObjectNode_new(&objects);
  90. objects->nodeId = ObjId_ObjectsFolder.nodeId;
  91. objects->nodeClass = UA_NODECLASS_OBJECT;
  92. objects->browseName = (UA_QualifiedName){0, {7, "Objects"}};
  93. objects->displayName = (UA_LocalizedText){{2,"EN"},{7, "Objects"}};
  94. objects->description = (UA_LocalizedText){{2,"EN"},{7, "Objects"}};
  95. objects->referencesSize = 2;
  96. objects->references = (UA_ReferenceNode[2]){
  97. {RefTypeId_HasTypeDefinition, UA_FALSE, ObjTypeId_FolderType},
  98. {RefTypeId_Organizes, UA_FALSE, ObjId_Server}};
  99. Namespace_insert(ns0,(UA_Node*)objects);
  100. // Views
  101. UA_ObjectNode *views;
  102. UA_ObjectNode_new(&views);
  103. views->nodeId = ObjId_ViewsFolder.nodeId;
  104. views->nodeClass = UA_NODECLASS_OBJECT;
  105. views->browseName = (UA_QualifiedName){0, {5, "Views"}};
  106. views->displayName = (UA_LocalizedText){{2,"EN"},{5, "Views"}};
  107. views->description = (UA_LocalizedText){{2,"EN"},{5, "Views"}};
  108. views->referencesSize = 1;
  109. views->references = (UA_ReferenceNode[1]){
  110. {RefTypeId_HasTypeDefinition, UA_FALSE, ObjTypeId_FolderType}};
  111. Namespace_insert(ns0,(UA_Node*)views);
  112. // Server
  113. UA_ObjectNode *server;
  114. UA_ObjectNode_new(&server);
  115. server->nodeId = ObjId_Server.nodeId;
  116. server->nodeClass = UA_NODECLASS_OBJECT;
  117. server->browseName = (UA_QualifiedName){0, {6, "Server"}};
  118. server->displayName = (UA_LocalizedText){{2,"EN"},{6, "Server"}};
  119. server->description = (UA_LocalizedText){{2,"EN"},{6, "Server"}};
  120. server->referencesSize = 0;
  121. server->references = (UA_ReferenceNode[4]){
  122. {RefTypeId_HasComponent, UA_FALSE, ObjId_ServerCapabilities},
  123. {RefTypeId_HasComponent, UA_FALSE, ObjId_NamespaceArray},
  124. {RefTypeId_HasProperty, UA_FALSE, ObjId_ServerStatus},
  125. {RefTypeId_HasProperty, UA_FALSE, ObjId_ServerArray}};
  126. Namespace_insert(ns0,(UA_Node*)server);
  127. // NamespaceArray
  128. UA_VariableNode *namespaceArray;
  129. UA_VariableNode_new(&namespaceArray);
  130. namespaceArray->nodeId = ObjId_NamespaceArray.nodeId;
  131. namespaceArray->nodeClass = UA_NODECLASS_VARIABLE; //FIXME: this should go into _new?
  132. namespaceArray->browseName = (UA_QualifiedName){0, {13, "NamespaceArray"}};
  133. namespaceArray->displayName = (UA_LocalizedText){{2,"EN"},{13, "NamespaceArray"}};
  134. namespaceArray->description = (UA_LocalizedText){{2,"EN"},{13, "NamespaceArray"}};
  135. //FIXME: can we avoid new here?
  136. UA_Array_new((void**)&namespaceArray->value.data, 2, &UA_.types[UA_STRING]);
  137. namespaceArray->value.vt = &UA_.types[UA_STRING];
  138. namespaceArray->value.arrayLength = 2;
  139. UA_String_copycstring("http://opcfoundation.org/UA/",&((UA_String *)((namespaceArray->value).data))[0]);
  140. UA_String_copycstring("http://localhost:16664/open62541/",&((UA_String *)(((namespaceArray)->value).data))[1]);
  141. namespaceArray->arrayDimensionsSize = 1;
  142. UA_Int32 dimensions = 2;
  143. namespaceArray->arrayDimensions = &dimensions;
  144. namespaceArray->dataType = NS0NODEID(UA_STRING_NS0);
  145. namespaceArray->valueRank = 1;
  146. namespaceArray->minimumSamplingInterval = 1.0;
  147. namespaceArray->historizing = UA_FALSE;
  148. Namespace_insert(ns0,(UA_Node*)namespaceArray);
  149. // ServerStatus
  150. UA_VariableNode *serverstatus;
  151. UA_VariableNode_new(&serverstatus);
  152. serverstatus->nodeId = ObjId_ServerStatus.nodeId;
  153. serverstatus->nodeClass = UA_NODECLASS_VARIABLE;
  154. serverstatus->browseName = (UA_QualifiedName){0, {12, "ServerStatus"}};
  155. serverstatus->displayName = (UA_LocalizedText){{2,"EN"},{12, "ServerStatus"}};
  156. serverstatus->description = (UA_LocalizedText){{2,"EN"},{12, "ServerStatus"}};
  157. UA_ServerStatusDataType *status;
  158. UA_ServerStatusDataType_new(&status);
  159. status->startTime = UA_DateTime_now();
  160. status->currentTime = UA_DateTime_now();
  161. status->state = UA_SERVERSTATE_RUNNING;
  162. status->buildInfo = (UA_BuildInfo){{13,"open62541.org"}, {9,"open62541"}, {9,"open62541"},
  163. {3, "0.0"}, {3, "0.0"}, UA_DateTime_now()};
  164. status->secondsTillShutdown = 99999999;
  165. status->shutdownReason = (UA_LocalizedText){{2,"EN"},{7, "because"}};
  166. serverstatus->value.vt = &UA_.types[UA_SERVERSTATUSDATATYPE]; // gets encoded as an extensionobject
  167. serverstatus->value.arrayLength = 1;
  168. serverstatus->value.data = status;
  169. Namespace_insert(ns0,(UA_Node*)serverstatus);
  170. // State (Component of ServerStatus)
  171. UA_VariableNode *state;
  172. UA_VariableNode_new(&state);
  173. state->nodeId = ObjId_State.nodeId;
  174. state->nodeClass = UA_NODECLASS_VARIABLE;
  175. state->browseName = (UA_QualifiedName){0, {5, "State"}};
  176. state->displayName = (UA_LocalizedText){{2,"EN"},{5, "State"}};
  177. state->description = (UA_LocalizedText){{2,"EN"},{5, "State"}};
  178. state->value.vt = &UA_borrowed_.types[UA_SERVERSTATE];
  179. state->value.arrayLength = 1;
  180. state->value.data = &status->state; // points into the other object.
  181. state->referencesSize = 0;
  182. state->references = UA_NULL;
  183. Namespace_insert(ns0,(UA_Node*)state);
  184. //TODO: free(namespaceArray->value.data) later or forget it
  185. /* UA_VariableNode* v = (UA_VariableNode*)np; */
  186. /* UA_Array_new((void**)&v->value.data, 2, &UA_.types[UA_STRING]); */
  187. /* v->value.vt = &UA_.types[UA_STRING]; */
  188. /* v->value.arrayLength = 2; */
  189. /* UA_String_copycstring("http://opcfoundation.org/UA/",&((UA_String *)((v->value).data))[0]); */
  190. /* UA_String_copycstring("http://localhost:16664/open62541/",&((UA_String *)(((v)->value).data))[1]); */
  191. /* v->dataType.encodingByte = UA_NODEIDTYPE_FOURBYTE; */
  192. /* v->dataType.identifier.numeric = UA_STRING_NS0; */
  193. /* v->valueRank = 1; */
  194. /* v->minimumSamplingInterval = 1.0; */
  195. /* v->historizing = UA_FALSE; */
  196. /* Namespace_insert(ns0,np); */
  197. /*TEST */
  198. const UA_Node * foundNode1;
  199. const UA_Node * foundNode2;
  200. Namespace_Entry_Lock *lock1;
  201. Namespace_get(ns0, &root->nodeId,
  202. &foundNode1, &lock1);
  203. Namespace_get(ns0, &foundNode1->references[0].targetId.nodeId,
  204. &foundNode2, &lock1);
  205. /*TEST END*/
  206. /*******************/
  207. /* Namespace local */
  208. /*******************/
  209. #if defined(DEBUG) && defined(VERBOSE)
  210. uint32_t i;
  211. for (i=0;i < ns0->size;i++) {
  212. if (ns0->entries[i].node != UA_NULL) {
  213. printf("appMockup_init - entries[%d]={",i);
  214. UA_Node_print(ns0->entries[i].node, stdout);
  215. printf("}\n");
  216. }
  217. }
  218. #endif
  219. }