ua_server.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. #ifdef UA_MULTITHREADING
  2. #define _LGPL_SOURCE
  3. #include <urcu.h>
  4. #endif
  5. #include "ua_types.h"
  6. #include "ua_server_internal.h"
  7. #include "ua_securechannel_manager.h"
  8. #include "ua_session_manager.h"
  9. #include "ua_util.h"
  10. #include "ua_services.h"
  11. #include "ua_nodeids.h"
  12. const char *UA_LoggerCategoryNames[3] = {"communication", "server", "userland"};
  13. /**********************/
  14. /* Namespace Handling */
  15. /**********************/
  16. static void UA_ExternalNamespace_init(UA_ExternalNamespace *ens) {
  17. ens->index = 0;
  18. UA_String_init(&ens->url);
  19. }
  20. static void UA_ExternalNamespace_deleteMembers(UA_ExternalNamespace *ens) {
  21. UA_String_deleteMembers(&ens->url);
  22. ens->externalNodeStore.destroy(ens->externalNodeStore.ensHandle);
  23. }
  24. /*****************/
  25. /* Configuration */
  26. /*****************/
  27. void UA_Server_addNetworkLayer(UA_Server *server, UA_ServerNetworkLayer networkLayer) {
  28. UA_ServerNetworkLayer *newlayers = UA_realloc(server->nls, sizeof(UA_ServerNetworkLayer)*(server->nlsSize+1));
  29. if(!newlayers) {
  30. UA_LOG_ERROR(server->logger, UA_LOGGERCATEGORY_SERVER, "Networklayer added");
  31. return;
  32. }
  33. server->nls = newlayers;
  34. server->nls[server->nlsSize] = networkLayer;
  35. server->nlsSize++;
  36. UA_LOG_INFO(server->logger, UA_LOGGERCATEGORY_SERVER, "Networklayer added");
  37. }
  38. void UA_Server_setServerCertificate(UA_Server *server, UA_ByteString certificate) {
  39. for(UA_Int32 i=0;i<server->endpointDescriptionsSize;i++)
  40. UA_ByteString_copy(&certificate, &server->endpointDescriptions[i].serverCertificate);
  41. }
  42. void UA_Server_setLogger(UA_Server *server, UA_Logger logger) {
  43. server->logger = logger;
  44. }
  45. /**********/
  46. /* Server */
  47. /**********/
  48. void UA_Server_delete(UA_Server *server) {
  49. // The server needs to be stopped before it can be deleted
  50. // Delete all internal data
  51. UA_ApplicationDescription_deleteMembers(&server->description);
  52. UA_SecureChannelManager_deleteMembers(&server->secureChannelManager);
  53. UA_SessionManager_deleteMembers(&server->sessionManager);
  54. UA_NodeStore_delete(server->nodestore);
  55. UA_ByteString_deleteMembers(&server->serverCertificate);
  56. UA_Array_delete(server->endpointDescriptions, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION], server->endpointDescriptionsSize);
  57. // Delete the timed work
  58. UA_Server_deleteTimedWork(server);
  59. // Delete the network layers
  60. for(UA_Int32 i=0;i<server->nlsSize;i++) {
  61. server->nls[i].free(server->nls[i].nlHandle);
  62. }
  63. UA_free(server->nls);
  64. #ifdef UA_MULTITHREADING
  65. pthread_cond_destroy(&server->dispatchQueue_condition); // so the workers don't spin if the queue is empty
  66. rcu_barrier(); // wait for all scheduled call_rcu work to complete
  67. #endif
  68. UA_free(server);
  69. }
  70. static UA_StatusCode readStatus(const void *handle, UA_Boolean sourceTimeStamp, UA_DataValue *value) {
  71. UA_ServerStatusDataType *status = UA_ServerStatusDataType_new();
  72. status->startTime = ((const UA_Server*)handle)->startTime;
  73. status->currentTime = UA_DateTime_now();
  74. status->state = UA_SERVERSTATE_RUNNING;
  75. UA_String_copycstring("http://www.open62541.org", &status->buildInfo.productUri);
  76. UA_String_copycstring("open62541", &status->buildInfo.manufacturerName);
  77. UA_String_copycstring("open62541 OPC UA Server", &status->buildInfo.productName);
  78. #define STRINGIFY(x) #x //some magic
  79. #define TOSTRING(x) STRINGIFY(x) //some magic
  80. UA_String_copycstring(TOSTRING(OPEN62541_VERSION_MAJOR) "." TOSTRING(OPEN62541_VERSION_MINOR) "." TOSTRING(OPEN62541_VERSION_PATCH), &status->buildInfo.softwareVersion);
  81. UA_String_copycstring("0", &status->buildInfo.buildNumber);
  82. status->buildInfo.buildDate = ((const UA_Server*)handle)->buildDate;
  83. status->secondsTillShutdown = 0;
  84. value->value.type = &UA_TYPES[UA_TYPES_SERVERSTATUSDATATYPE];
  85. value->value.arrayLength = 1;
  86. value->value.dataPtr = status;
  87. value->value.arrayDimensionsSize = -1;
  88. value->value.arrayDimensions = UA_NULL;
  89. value->hasVariant = UA_TRUE;
  90. if(sourceTimeStamp) {
  91. value->hasSourceTimestamp = UA_TRUE;
  92. value->sourceTimestamp = UA_DateTime_now();
  93. }
  94. return UA_STATUSCODE_GOOD;
  95. }
  96. static void releaseStatus(const void *handle, UA_DataValue *value) {
  97. UA_DataValue_deleteMembers(value);
  98. }
  99. static void copyNames(UA_Node *node, char *name) {
  100. UA_QualifiedName_copycstring(name, &node->browseName);
  101. UA_LocalizedText_copycstring(name, &node->displayName);
  102. UA_LocalizedText_copycstring(name, &node->description);
  103. }
  104. static void addDataTypeNode(UA_Server *server, char* name, UA_UInt32 datatypeid, UA_Int32 parent) {
  105. UA_DataTypeNode *datatype = UA_DataTypeNode_new();
  106. copyNames((UA_Node*)datatype, name);
  107. datatype->nodeId.identifier.numeric = datatypeid;
  108. UA_Server_addNode(server, (UA_Node*)datatype,
  109. &UA_EXPANDEDNODEID_STATIC(0, parent),
  110. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  111. }
  112. UA_Server * UA_Server_new(void) {
  113. UA_Server *server = UA_malloc(sizeof(UA_Server));
  114. if(!server)
  115. return UA_NULL;
  116. LIST_INIT(&server->timedWork);
  117. #ifdef UA_MULTITHREADING
  118. rcu_init();
  119. cds_wfcq_init(&server->dispatchQueue_head, &server->dispatchQueue_tail);
  120. server->delayedWork = UA_NULL;
  121. #endif
  122. // logger
  123. server->logger = (UA_Logger){ UA_NULL, UA_NULL, UA_NULL, UA_NULL, UA_NULL, UA_NULL };
  124. // random seed
  125. server->random_seed = (UA_UInt32)UA_DateTime_now();
  126. // networklayers
  127. server->nls = UA_NULL;
  128. server->nlsSize = 0;
  129. UA_ByteString_init(&server->serverCertificate);
  130. // mockup application description
  131. UA_ApplicationDescription_init(&server->description);
  132. UA_String_copycstring("urn:unconfigured:open62541:application", &server->description.productUri);
  133. UA_String_copycstring("http://unconfigured.open62541/applications/", &server->description.applicationUri);
  134. UA_LocalizedText_copycstring("Unconfigured open62541 application", &server->description.applicationName);
  135. server->description.applicationType = UA_APPLICATIONTYPE_SERVER;
  136. server->externalNamespacesSize = 0;
  137. server->externalNamespaces = UA_NULL;
  138. // mockup endpoint description
  139. server->endpointDescriptionsSize = 1;
  140. UA_EndpointDescription *endpoint = UA_EndpointDescription_new(); // todo: check return code
  141. endpoint->securityMode = UA_MESSAGESECURITYMODE_NONE;
  142. UA_String_copycstring("http://opcfoundation.org/UA/SecurityPolicy#None", &endpoint->securityPolicyUri);
  143. UA_String_copycstring("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary", &endpoint->transportProfileUri);
  144. endpoint->userIdentityTokens = UA_malloc(sizeof(UA_UserTokenPolicy));
  145. endpoint->userIdentityTokensSize = 1;
  146. UA_UserTokenPolicy_init(endpoint->userIdentityTokens);
  147. UA_String_copycstring("my-anonymous-policy", &endpoint->userIdentityTokens->policyId); // defined per server
  148. endpoint->userIdentityTokens->tokenType = UA_USERTOKENTYPE_ANONYMOUS;
  149. /* UA_String_copy(endpointUrl, &endpoint->endpointUrl); */
  150. /* /\* The standard says "the HostName specified in the Server Certificate is the */
  151. /* same as the HostName contained in the endpointUrl provided in the */
  152. /* EndpointDescription *\/ */
  153. /* UA_String_copy(&server->serverCertificate, &endpoint->serverCertificate); */
  154. UA_ApplicationDescription_copy(&server->description, &endpoint->server);
  155. server->endpointDescriptions = endpoint;
  156. #define MAXCHANNELCOUNT 100
  157. #define STARTCHANNELID 1
  158. #define TOKENLIFETIME 600000
  159. #define STARTTOKENID 1
  160. UA_SecureChannelManager_init(&server->secureChannelManager, MAXCHANNELCOUNT,
  161. TOKENLIFETIME, STARTCHANNELID, STARTTOKENID);
  162. #define MAXSESSIONCOUNT 1000
  163. #define SESSIONLIFETIME 10000
  164. #define STARTSESSIONID 1
  165. UA_SessionManager_init(&server->sessionManager, MAXSESSIONCOUNT, SESSIONLIFETIME, STARTSESSIONID);
  166. server->nodestore = UA_NodeStore_new();
  167. /**************/
  168. /* References */
  169. /**************/
  170. /* bootstrap by manually inserting "references" and "hassubtype" */
  171. UA_ReferenceTypeNode *references = UA_ReferenceTypeNode_new();
  172. copyNames((UA_Node*)references, "References");
  173. references->nodeId.identifier.numeric = UA_NS0ID_REFERENCES;
  174. references->isAbstract = UA_TRUE;
  175. references->symmetric = UA_TRUE;
  176. // this node has no parent??
  177. UA_NodeStore_insert(server->nodestore, (UA_Node*)references, UA_NULL);
  178. UA_ReferenceTypeNode *hassubtype = UA_ReferenceTypeNode_new();
  179. copyNames((UA_Node*)hassubtype, "HasSubtype");
  180. UA_LocalizedText_copycstring("HasSupertype", &hassubtype->inverseName);
  181. hassubtype->nodeId.identifier.numeric = UA_NS0ID_HASSUBTYPE;
  182. hassubtype->isAbstract = UA_FALSE;
  183. hassubtype->symmetric = UA_FALSE;
  184. UA_NodeStore_insert(server->nodestore, (UA_Node*)hassubtype, UA_NULL);
  185. /* continue adding reference types with normal "addnode" */
  186. UA_ReferenceTypeNode *hierarchicalreferences = UA_ReferenceTypeNode_new();
  187. copyNames((UA_Node*)hierarchicalreferences, "Hierarchicalreferences");
  188. hierarchicalreferences->nodeId.identifier.numeric = UA_NS0ID_HIERARCHICALREFERENCES;
  189. hierarchicalreferences->isAbstract = UA_TRUE;
  190. hierarchicalreferences->symmetric = UA_FALSE;
  191. UA_Server_addNode(server, (UA_Node*)hierarchicalreferences,
  192. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_REFERENCES),
  193. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  194. UA_ReferenceTypeNode *nonhierarchicalreferences = UA_ReferenceTypeNode_new();
  195. copyNames((UA_Node*)nonhierarchicalreferences, "NonHierarchicalReferences");
  196. nonhierarchicalreferences->nodeId.identifier.numeric = UA_NS0ID_NONHIERARCHICALREFERENCES;
  197. nonhierarchicalreferences->isAbstract = UA_TRUE;
  198. nonhierarchicalreferences->symmetric = UA_FALSE;
  199. UA_Server_addNode(server, (UA_Node*)nonhierarchicalreferences,
  200. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_REFERENCES),
  201. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  202. UA_ReferenceTypeNode *haschild = UA_ReferenceTypeNode_new();
  203. copyNames((UA_Node*)haschild, "HasChild");
  204. haschild->nodeId.identifier.numeric = UA_NS0ID_HASCHILD;
  205. haschild->isAbstract = UA_TRUE;
  206. haschild->symmetric = UA_FALSE;
  207. UA_Server_addNode(server, (UA_Node*)haschild,
  208. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_HIERARCHICALREFERENCES),
  209. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  210. UA_ReferenceTypeNode *organizes = UA_ReferenceTypeNode_new();
  211. copyNames((UA_Node*)organizes, "Organizes");
  212. UA_LocalizedText_copycstring("OrganizedBy", &organizes->inverseName);
  213. organizes->nodeId.identifier.numeric = UA_NS0ID_ORGANIZES;
  214. organizes->isAbstract = UA_FALSE;
  215. organizes->symmetric = UA_FALSE;
  216. UA_Server_addNode(server, (UA_Node*)organizes,
  217. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_HIERARCHICALREFERENCES),
  218. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  219. UA_ReferenceTypeNode *haseventsource = UA_ReferenceTypeNode_new();
  220. copyNames((UA_Node*)haseventsource, "HasEventSource");
  221. UA_LocalizedText_copycstring("EventSourceOf", &haseventsource->inverseName);
  222. haseventsource->nodeId.identifier.numeric = UA_NS0ID_HASEVENTSOURCE;
  223. haseventsource->isAbstract = UA_FALSE;
  224. haseventsource->symmetric = UA_FALSE;
  225. UA_Server_addNode(server, (UA_Node*)haseventsource,
  226. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_HIERARCHICALREFERENCES),
  227. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  228. UA_ReferenceTypeNode *hasmodellingrule = UA_ReferenceTypeNode_new();
  229. copyNames((UA_Node*)hasmodellingrule, "HasModellingRule");
  230. UA_LocalizedText_copycstring("ModellingRuleOf", &hasmodellingrule->inverseName);
  231. hasmodellingrule->nodeId.identifier.numeric = UA_NS0ID_HASMODELLINGRULE;
  232. hasmodellingrule->isAbstract = UA_FALSE;
  233. hasmodellingrule->symmetric = UA_FALSE;
  234. UA_Server_addNode(server, (UA_Node*)hasmodellingrule,
  235. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  236. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  237. UA_ReferenceTypeNode *hasencoding = UA_ReferenceTypeNode_new();
  238. copyNames((UA_Node*)hasencoding, "HasEncoding");
  239. UA_LocalizedText_copycstring("EncodingOf", &hasencoding->inverseName);
  240. hasencoding->nodeId.identifier.numeric = UA_NS0ID_HASENCODING;
  241. hasencoding->isAbstract = UA_FALSE;
  242. hasencoding->symmetric = UA_FALSE;
  243. UA_Server_addNode(server, (UA_Node*)hasencoding,
  244. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  245. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  246. UA_ReferenceTypeNode *hasdescription = UA_ReferenceTypeNode_new();
  247. copyNames((UA_Node*)hasdescription, "HasDescription");
  248. UA_LocalizedText_copycstring("DescriptionOf", &hasdescription->inverseName);
  249. hasdescription->nodeId.identifier.numeric = UA_NS0ID_HASDESCRIPTION;
  250. hasdescription->isAbstract = UA_FALSE;
  251. hasdescription->symmetric = UA_FALSE;
  252. UA_Server_addNode(server, (UA_Node*)hasdescription,
  253. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  254. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  255. UA_ReferenceTypeNode *hastypedefinition = UA_ReferenceTypeNode_new();
  256. copyNames((UA_Node*)hastypedefinition, "HasTypeDefinition");
  257. UA_LocalizedText_copycstring("TypeDefinitionOf", &hastypedefinition->inverseName);
  258. hastypedefinition->nodeId.identifier.numeric = UA_NS0ID_HASTYPEDEFINITION;
  259. hastypedefinition->isAbstract = UA_FALSE;
  260. hastypedefinition->symmetric = UA_FALSE;
  261. UA_Server_addNode(server, (UA_Node*)hastypedefinition,
  262. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  263. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  264. UA_ReferenceTypeNode *generatesevent = UA_ReferenceTypeNode_new();
  265. copyNames((UA_Node*)generatesevent, "GeneratesEvent");
  266. UA_LocalizedText_copycstring("GeneratedBy", &generatesevent->inverseName);
  267. generatesevent->nodeId.identifier.numeric = UA_NS0ID_GENERATESEVENT;
  268. generatesevent->isAbstract = UA_FALSE;
  269. generatesevent->symmetric = UA_FALSE;
  270. UA_Server_addNode(server, (UA_Node*)generatesevent,
  271. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  272. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  273. UA_ReferenceTypeNode *aggregates = UA_ReferenceTypeNode_new();
  274. copyNames((UA_Node*)aggregates, "Aggregates");
  275. // Todo: Is there an inverse name?
  276. aggregates->nodeId.identifier.numeric = UA_NS0ID_AGGREGATES;
  277. aggregates->isAbstract = UA_TRUE;
  278. aggregates->symmetric = UA_FALSE;
  279. UA_Server_addNode(server, (UA_Node*)aggregates,
  280. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_HASCHILD),
  281. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  282. // complete bootstrap of hassubtype
  283. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_HASCHILD), UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE),
  284. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  285. UA_ReferenceTypeNode *hasproperty = UA_ReferenceTypeNode_new();
  286. copyNames((UA_Node*)hasproperty, "HasProperty");
  287. UA_LocalizedText_copycstring("PropertyOf", &hasproperty->inverseName);
  288. hasproperty->nodeId.identifier.numeric = UA_NS0ID_HASPROPERTY;
  289. hasproperty->isAbstract = UA_FALSE;
  290. hasproperty->symmetric = UA_FALSE;
  291. UA_Server_addNode(server, (UA_Node*)hasproperty,
  292. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_AGGREGATES),
  293. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  294. UA_ReferenceTypeNode *hascomponent = UA_ReferenceTypeNode_new();
  295. copyNames((UA_Node*)hascomponent, "HasComponent");
  296. UA_LocalizedText_copycstring("ComponentOf", &hascomponent->inverseName);
  297. hascomponent->nodeId.identifier.numeric = UA_NS0ID_HASCOMPONENT;
  298. hascomponent->isAbstract = UA_FALSE;
  299. hascomponent->symmetric = UA_FALSE;
  300. UA_Server_addNode(server, (UA_Node*)hascomponent,
  301. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_AGGREGATES),
  302. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  303. UA_ReferenceTypeNode *hasnotifier = UA_ReferenceTypeNode_new();
  304. copyNames((UA_Node*)hasnotifier, "HasNotifier");
  305. UA_LocalizedText_copycstring("NotifierOf", &hasnotifier->inverseName);
  306. hasnotifier->nodeId.identifier.numeric = UA_NS0ID_HASNOTIFIER;
  307. hasnotifier->isAbstract = UA_FALSE;
  308. hasnotifier->symmetric = UA_FALSE;
  309. UA_Server_addNode(server, (UA_Node*)hasnotifier,
  310. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_HASEVENTSOURCE),
  311. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  312. UA_ReferenceTypeNode *hasorderedcomponent = UA_ReferenceTypeNode_new();
  313. copyNames((UA_Node*)hasorderedcomponent, "HasOrderedComponent");
  314. UA_LocalizedText_copycstring("OrderedComponentOf", &hasorderedcomponent->inverseName);
  315. hasorderedcomponent->nodeId.identifier.numeric = UA_NS0ID_HASORDEREDCOMPONENT;
  316. hasorderedcomponent->isAbstract = UA_FALSE;
  317. hasorderedcomponent->symmetric = UA_FALSE;
  318. UA_Server_addNode(server, (UA_Node*)hasorderedcomponent,
  319. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_HASCOMPONENT),
  320. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  321. UA_ReferenceTypeNode *hasmodelparent = UA_ReferenceTypeNode_new();
  322. copyNames((UA_Node*)hasmodelparent, "HasModelParent");
  323. UA_LocalizedText_copycstring("ModelParentOf", &hasmodelparent->inverseName);
  324. hasmodelparent->nodeId.identifier.numeric = UA_NS0ID_HASMODELPARENT;
  325. hasmodelparent->isAbstract = UA_FALSE;
  326. hasmodelparent->symmetric = UA_FALSE;
  327. UA_Server_addNode(server, (UA_Node*)hasmodelparent,
  328. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  329. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  330. UA_ReferenceTypeNode *fromstate = UA_ReferenceTypeNode_new();
  331. copyNames((UA_Node*)fromstate, "FromState");
  332. UA_LocalizedText_copycstring("ToTransition", &fromstate->inverseName);
  333. fromstate->nodeId.identifier.numeric = UA_NS0ID_FROMSTATE;
  334. fromstate->isAbstract = UA_FALSE;
  335. fromstate->symmetric = UA_FALSE;
  336. UA_Server_addNode(server, (UA_Node*)fromstate,
  337. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  338. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  339. UA_ReferenceTypeNode *tostate = UA_ReferenceTypeNode_new();
  340. copyNames((UA_Node*)tostate, "ToState");
  341. UA_LocalizedText_copycstring("FromTransition", &tostate->inverseName);
  342. tostate->nodeId.identifier.numeric = UA_NS0ID_TOSTATE;
  343. tostate->isAbstract = UA_FALSE;
  344. tostate->symmetric = UA_FALSE;
  345. UA_Server_addNode(server, (UA_Node*)tostate,
  346. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  347. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  348. UA_ReferenceTypeNode *hascause = UA_ReferenceTypeNode_new();
  349. copyNames((UA_Node*)hascause, "HasCause");
  350. UA_LocalizedText_copycstring("MayBeCausedBy", &hascause->inverseName);
  351. hascause->nodeId.identifier.numeric = UA_NS0ID_HASCAUSE;
  352. hascause->isAbstract = UA_FALSE;
  353. hascause->symmetric = UA_FALSE;
  354. UA_Server_addNode(server, (UA_Node*)hascause,
  355. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  356. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  357. UA_ReferenceTypeNode *haseffect = UA_ReferenceTypeNode_new();
  358. copyNames((UA_Node*)haseffect, "HasEffect");
  359. UA_LocalizedText_copycstring("MayBeEffectedBy", &haseffect->inverseName);
  360. haseffect->nodeId.identifier.numeric = UA_NS0ID_HASEFFECT;
  361. haseffect->isAbstract = UA_FALSE;
  362. haseffect->symmetric = UA_FALSE;
  363. UA_Server_addNode(server, (UA_Node*)haseffect,
  364. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_NONHIERARCHICALREFERENCES),
  365. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  366. UA_ReferenceTypeNode *hashistoricalconfiguration = UA_ReferenceTypeNode_new();
  367. copyNames((UA_Node*)hashistoricalconfiguration, "HasHistoricalConfiguration");
  368. UA_LocalizedText_copycstring("HistoricalConfigurationOf", &hashistoricalconfiguration->inverseName);
  369. hashistoricalconfiguration->nodeId.identifier.numeric = UA_NS0ID_HASHISTORICALCONFIGURATION;
  370. hashistoricalconfiguration->isAbstract = UA_FALSE;
  371. hashistoricalconfiguration->symmetric = UA_FALSE;
  372. UA_Server_addNode(server, (UA_Node*)hashistoricalconfiguration,
  373. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_AGGREGATES),
  374. &UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE));
  375. /***********/
  376. /* Objects */
  377. /***********/
  378. UA_ObjectTypeNode *baseObjectType = UA_ObjectTypeNode_new();
  379. baseObjectType->nodeId.identifier.numeric = UA_NS0ID_BASEOBJECTTYPE;
  380. copyNames((UA_Node*)baseObjectType, "BaseObjectType");
  381. UA_NodeStore_insert(server->nodestore, (UA_Node*)baseObjectType, UA_NULL);
  382. UA_ObjectTypeNode *baseDataVarialbeType = UA_ObjectTypeNode_new();
  383. baseDataVarialbeType->nodeId.identifier.numeric = UA_NS0ID_BASEDATAVARIABLETYPE;
  384. copyNames((UA_Node*)baseDataVarialbeType, "BaseDataVariableType");
  385. UA_NodeStore_insert(server->nodestore, (UA_Node*)baseDataVarialbeType, UA_NULL);
  386. UA_ObjectTypeNode *folderType = UA_ObjectTypeNode_new();
  387. folderType->nodeId.identifier.numeric = UA_NS0ID_FOLDERTYPE;
  388. copyNames((UA_Node*)folderType, "FolderType");
  389. UA_NodeStore_insert(server->nodestore, (UA_Node*)folderType, UA_NULL);
  390. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_BASEOBJECTTYPE), UA_NODEID_STATIC(0, UA_NS0ID_HASSUBTYPE),
  391. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_FOLDERTYPE));
  392. UA_ObjectNode *root = UA_ObjectNode_new();
  393. copyNames((UA_Node*)root, "Root");
  394. root->nodeId.identifier.numeric = UA_NS0ID_ROOTFOLDER;
  395. UA_NodeStore_insert(server->nodestore, (UA_Node*)root, UA_NULL);
  396. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_ROOTFOLDER), UA_NODEID_STATIC(0, UA_NS0ID_HASTYPEDEFINITION),
  397. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_FOLDERTYPE));
  398. UA_ObjectNode *objects = UA_ObjectNode_new();
  399. copyNames((UA_Node*)objects, "Objects");
  400. objects->nodeId.identifier.numeric = UA_NS0ID_OBJECTSFOLDER;
  401. UA_Server_addNode(server, (UA_Node*)objects,
  402. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_ROOTFOLDER),
  403. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  404. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_STATIC(0, UA_NS0ID_HASTYPEDEFINITION),
  405. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_FOLDERTYPE));
  406. UA_ObjectNode *types = UA_ObjectNode_new();
  407. copyNames((UA_Node*)types, "Types");
  408. types->nodeId.identifier.numeric = UA_NS0ID_TYPESFOLDER;
  409. UA_Server_addNode(server, (UA_Node*)types,
  410. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_ROOTFOLDER),
  411. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  412. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_TYPESFOLDER), UA_NODEID_STATIC(0, UA_NS0ID_HASTYPEDEFINITION),
  413. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_FOLDERTYPE));
  414. UA_ObjectNode *views = UA_ObjectNode_new();
  415. copyNames((UA_Node*)views, "Views");
  416. views->nodeId.identifier.numeric = UA_NS0ID_VIEWSFOLDER;
  417. UA_Server_addNode(server, (UA_Node*)views,
  418. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_ROOTFOLDER),
  419. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  420. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_VIEWSFOLDER), UA_NODEID_STATIC(0, UA_NS0ID_HASTYPEDEFINITION),
  421. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_FOLDERTYPE));
  422. UA_ObjectNode *servernode = UA_ObjectNode_new();
  423. copyNames((UA_Node*)servernode, "Server");
  424. servernode->nodeId.identifier.numeric = UA_NS0ID_SERVER;
  425. UA_Server_addNode(server, (UA_Node*)servernode,
  426. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  427. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  428. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_SERVER), UA_NODEID_STATIC(0, UA_NS0ID_HASCOMPONENT),
  429. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_SERVER_SERVERCAPABILITIES));
  430. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_SERVER), UA_NODEID_STATIC(0, UA_NS0ID_HASPROPERTY),
  431. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_SERVER_SERVERARRAY));
  432. UA_VariableNode *namespaceArray = UA_VariableNode_new();
  433. copyNames((UA_Node*)namespaceArray, "NamespaceArray");
  434. namespaceArray->nodeId.identifier.numeric = UA_NS0ID_SERVER_NAMESPACEARRAY;
  435. namespaceArray->variableType = UA_VARIABLENODETYPE_VARIANT;
  436. namespaceArray->variable.variant.dataPtr = UA_Array_new(&UA_TYPES[UA_TYPES_STRING], 2);
  437. namespaceArray->variable.variant.arrayLength = 2;
  438. namespaceArray->variable.variant.type = &UA_TYPES[UA_TYPES_STRING];
  439. // Fixme: Insert the external namespaces
  440. UA_String_copycstring("http://opcfoundation.org/UA/",
  441. &((UA_String *)(namespaceArray->variable.variant.dataPtr))[0]);
  442. UA_String_copycstring("urn:myServer:myApplication",
  443. &((UA_String *)(namespaceArray->variable.variant.dataPtr))[1]);
  444. namespaceArray->valueRank = 1;
  445. namespaceArray->minimumSamplingInterval = 1.0;
  446. namespaceArray->historizing = UA_FALSE;
  447. UA_Server_addNode(server, (UA_Node*)namespaceArray,
  448. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_SERVER),
  449. &UA_NODEID_STATIC(0, UA_NS0ID_HASPROPERTY));
  450. UA_VariableNode *serverstatus = UA_VariableNode_new();
  451. copyNames((UA_Node*)serverstatus, "ServerStatus");
  452. serverstatus->nodeId = UA_NODEID_STATIC(0, UA_NS0ID_SERVER_SERVERSTATUS);
  453. serverstatus->variableType = UA_VARIABLENODETYPE_DATASOURCE;
  454. serverstatus->variable.dataSource = (UA_DataSource) {.handle = server, .read = readStatus,
  455. .release = releaseStatus, .write = UA_NULL};
  456. UA_Server_addNode(server, (UA_Node*)serverstatus, &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_SERVER),
  457. &UA_NODEID_STATIC(0, UA_NS0ID_HASCOMPONENT));
  458. UA_VariableNode *state = UA_VariableNode_new();
  459. UA_ServerState *stateEnum = UA_ServerState_new();
  460. *stateEnum = UA_SERVERSTATE_RUNNING;
  461. copyNames((UA_Node*)state, "State");
  462. state->nodeId.identifier.numeric = UA_NS0ID_SERVER_SERVERSTATUS_STATE;
  463. state->variableType = UA_VARIABLENODETYPE_VARIANT;
  464. state->variable.variant.type = &UA_TYPES[UA_TYPES_SERVERSTATE];
  465. state->variable.variant.arrayLength = 1;
  466. state->variable.variant.dataPtr = stateEnum; // points into the other object.
  467. UA_NodeStore_insert(server->nodestore, (UA_Node*)state, UA_NULL);
  468. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_SERVER_SERVERSTATUS), UA_NODEID_STATIC(0, UA_NS0ID_HASCOMPONENT),
  469. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
  470. /**************/
  471. /* Data Types */
  472. /**************/
  473. UA_ObjectNode *datatypes = UA_ObjectNode_new();
  474. copyNames((UA_Node*)datatypes, "DataTypes");
  475. datatypes->nodeId.identifier.numeric = UA_NS0ID_DATATYPESFOLDER;
  476. UA_Server_addNode(server, (UA_Node*)datatypes,
  477. &UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_TYPESFOLDER),
  478. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  479. ADDREFERENCE(UA_NODEID_STATIC(0, UA_NS0ID_DATATYPESFOLDER), UA_NODEID_STATIC(0, UA_NS0ID_HASTYPEDEFINITION),
  480. UA_EXPANDEDNODEID_STATIC(0, UA_NS0ID_FOLDERTYPE));
  481. addDataTypeNode(server, "BaseDataType", UA_NS0ID_BASEDATATYPE, UA_NS0ID_DATATYPESFOLDER);
  482. addDataTypeNode(server, "Boolean", UA_NS0ID_BOOLEAN, UA_NS0ID_BASEDATATYPE);
  483. addDataTypeNode(server, "Number", UA_NS0ID_NUMBER, UA_NS0ID_BASEDATATYPE);
  484. addDataTypeNode(server, "Float", UA_NS0ID_FLOAT, UA_NS0ID_NUMBER);
  485. addDataTypeNode(server, "Double", UA_NS0ID_DOUBLE, UA_NS0ID_NUMBER);
  486. addDataTypeNode(server, "Integer", UA_NS0ID_INTEGER, UA_NS0ID_NUMBER);
  487. addDataTypeNode(server, "SByte", UA_NS0ID_SBYTE, UA_NS0ID_INTEGER);
  488. addDataTypeNode(server, "Int16", UA_NS0ID_INT16, UA_NS0ID_INTEGER);
  489. addDataTypeNode(server, "Int32", UA_NS0ID_INT32, UA_NS0ID_INTEGER);
  490. addDataTypeNode(server, "Int64", UA_NS0ID_INT64, UA_NS0ID_INTEGER);
  491. addDataTypeNode(server, "UInteger", UA_NS0ID_UINTEGER, UA_NS0ID_INTEGER);
  492. addDataTypeNode(server, "Byte", UA_NS0ID_BYTE, UA_NS0ID_UINTEGER);
  493. addDataTypeNode(server, "UInt16", UA_NS0ID_UINT16, UA_NS0ID_UINTEGER);
  494. addDataTypeNode(server, "UInt32", UA_NS0ID_UINT32, UA_NS0ID_UINTEGER);
  495. addDataTypeNode(server, "UInt64", UA_NS0ID_UINT64, UA_NS0ID_UINTEGER);
  496. addDataTypeNode(server, "String", UA_NS0ID_STRING, UA_NS0ID_BASEDATATYPE);
  497. addDataTypeNode(server, "DateTime", UA_NS0ID_DATETIME, UA_NS0ID_BASEDATATYPE);
  498. addDataTypeNode(server, "Guid", UA_NS0ID_GUID, UA_NS0ID_BASEDATATYPE);
  499. addDataTypeNode(server, "ByteString", UA_NS0ID_BYTESTRING, UA_NS0ID_BASEDATATYPE);
  500. addDataTypeNode(server, "XmlElement", UA_NS0ID_XMLELEMENT, UA_NS0ID_BASEDATATYPE);
  501. addDataTypeNode(server, "NodeId", UA_NS0ID_NODEID, UA_NS0ID_BASEDATATYPE);
  502. addDataTypeNode(server, "ExpandedNodeId", UA_NS0ID_EXPANDEDNODEID, UA_NS0ID_BASEDATATYPE);
  503. addDataTypeNode(server, "StatusCode", UA_NS0ID_STATUSCODE, UA_NS0ID_BASEDATATYPE);
  504. addDataTypeNode(server, "QualifiedName", UA_NS0ID_QUALIFIEDNAME, UA_NS0ID_BASEDATATYPE);
  505. addDataTypeNode(server, "LocalizedText", UA_NS0ID_LOCALIZEDTEXT, UA_NS0ID_BASEDATATYPE);
  506. addDataTypeNode(server, "Structure", UA_NS0ID_STRUCTURE, UA_NS0ID_BASEDATATYPE);
  507. addDataTypeNode(server, "DataValue", UA_NS0ID_DATAVALUE, UA_NS0ID_BASEDATATYPE);
  508. addDataTypeNode(server, "DiagnosticInfo", UA_NS0ID_DIAGNOSTICINFO, UA_NS0ID_BASEDATATYPE);
  509. addDataTypeNode(server, "Enumeration", UA_NS0ID_ENUMERATION, UA_NS0ID_BASEDATATYPE);
  510. return server;
  511. }