ua_client.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. #include "ua_types_generated.h"
  2. #include "ua_client.h"
  3. #include "ua_nodeids.h"
  4. #include "ua_securechannel.h"
  5. #include "ua_types_encoding_binary.h"
  6. #include "ua_transport_generated.h"
  7. #include "ua_client_internal.h"
  8. struct UA_Client {
  9. /* Connection */
  10. UA_Connection connection;
  11. UA_SecureChannel channel;
  12. UA_String endpointUrl;
  13. UA_UInt32 requestId;
  14. /* Session */
  15. UA_UserTokenPolicy token;
  16. UA_NodeId sessionId;
  17. UA_NodeId authenticationToken;
  18. #ifdef ENABLE_SUBSCRIPTIONS
  19. UA_Int32 monitoredItemHandles;
  20. LIST_HEAD(UA_ListOfUnacknowledgedNotificationNumbers, UA_Client_NotificationsAckNumber_s) pendingNotificationsAcks;
  21. LIST_HEAD(UA_ListOfClientSubscriptionItems, UA_Client_Subscription_s) subscriptions;
  22. #endif
  23. /* Config */
  24. UA_Logger logger;
  25. UA_ClientConfig config;
  26. UA_DateTime scExpiresAt;
  27. };
  28. const UA_EXPORT UA_ClientConfig UA_ClientConfig_standard =
  29. { 5 /* ms receive timout */, 30000, 2000,
  30. {.protocolVersion = 0, .sendBufferSize = 65536, .recvBufferSize = 65536,
  31. .maxMessageSize = 65536, .maxChunkCount = 1}};
  32. UA_Client * UA_Client_new(UA_ClientConfig config, UA_Logger logger) {
  33. UA_Client *client = UA_calloc(1, sizeof(UA_Client));
  34. if(!client)
  35. return UA_NULL;
  36. UA_Connection_init(&client->connection);
  37. UA_SecureChannel_init(&client->channel);
  38. client->channel.connection = &client->connection;
  39. UA_String_init(&client->endpointUrl);
  40. client->requestId = 0;
  41. UA_NodeId_init(&client->authenticationToken);
  42. client->logger = logger;
  43. client->config = config;
  44. client->scExpiresAt = 0;
  45. #ifdef ENABLE_SUBSCRIPTIONS
  46. client->monitoredItemHandles = 0;
  47. LIST_INIT(&client->pendingNotificationsAcks);
  48. LIST_INIT(&client->subscriptions);
  49. #endif
  50. return client;
  51. }
  52. void UA_Client_delete(UA_Client* client){
  53. UA_Connection_deleteMembers(&client->connection);
  54. UA_SecureChannel_deleteMembersCleanup(&client->channel);
  55. UA_String_deleteMembers(&client->endpointUrl);
  56. UA_UserTokenPolicy_deleteMembers(&client->token);
  57. free(client);
  58. }
  59. static UA_StatusCode HelAckHandshake(UA_Client *c) {
  60. UA_TcpMessageHeader messageHeader;
  61. messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_HELF;
  62. UA_TcpHelloMessage hello;
  63. UA_String_copy(&c->endpointUrl, &hello.endpointUrl); /* must be less than 4096 bytes */
  64. UA_Connection *conn = &c->connection;
  65. hello.maxChunkCount = conn->localConf.maxChunkCount;
  66. hello.maxMessageSize = conn->localConf.maxMessageSize;
  67. hello.protocolVersion = conn->localConf.protocolVersion;
  68. hello.receiveBufferSize = conn->localConf.recvBufferSize;
  69. hello.sendBufferSize = conn->localConf.sendBufferSize;
  70. UA_ByteString message;
  71. UA_StatusCode retval = c->connection.getBuffer(&c->connection, &message);
  72. if(retval != UA_STATUSCODE_GOOD)
  73. return retval;
  74. size_t offset = 8;
  75. retval |= UA_TcpHelloMessage_encodeBinary(&hello, &message, &offset);
  76. messageHeader.messageSize = offset;
  77. offset = 0;
  78. retval |= UA_TcpMessageHeader_encodeBinary(&messageHeader, &message, &offset);
  79. UA_TcpHelloMessage_deleteMembers(&hello);
  80. if(retval != UA_STATUSCODE_GOOD) {
  81. c->connection.releaseBuffer(&c->connection, &message);
  82. return retval;
  83. }
  84. retval = c->connection.write(&c->connection, &message, messageHeader.messageSize);
  85. if(retval != UA_STATUSCODE_GOOD) {
  86. c->connection.releaseBuffer(&c->connection, &message);
  87. return retval;
  88. }
  89. UA_ByteString reply;
  90. UA_ByteString_init(&reply);
  91. do {
  92. retval = c->connection.recv(&c->connection, &reply, c->config.timeout);
  93. if(retval != UA_STATUSCODE_GOOD)
  94. return retval;
  95. } while(!reply.data);
  96. offset = 0;
  97. UA_TcpMessageHeader_decodeBinary(&reply, &offset, &messageHeader);
  98. UA_TcpAcknowledgeMessage ackMessage;
  99. retval = UA_TcpAcknowledgeMessage_decodeBinary(&reply, &offset, &ackMessage);
  100. UA_ByteString_deleteMembers(&reply);
  101. if(retval != UA_STATUSCODE_GOOD)
  102. return retval;
  103. conn->remoteConf.maxChunkCount = ackMessage.maxChunkCount;
  104. conn->remoteConf.maxMessageSize = ackMessage.maxMessageSize;
  105. conn->remoteConf.protocolVersion = ackMessage.protocolVersion;
  106. conn->remoteConf.recvBufferSize = ackMessage.receiveBufferSize;
  107. conn->remoteConf.sendBufferSize = ackMessage.sendBufferSize;
  108. conn->state = UA_CONNECTION_ESTABLISHED;
  109. return UA_STATUSCODE_GOOD;
  110. }
  111. static UA_StatusCode SecureChannelHandshake(UA_Client *client, UA_Boolean renew) {
  112. UA_SecureConversationMessageHeader messageHeader;
  113. messageHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_OPNF;
  114. messageHeader.secureChannelId = 0;
  115. UA_SequenceHeader seqHeader;
  116. seqHeader.sequenceNumber = ++client->channel.sequenceNumber;
  117. seqHeader.requestId = ++client->requestId;
  118. UA_AsymmetricAlgorithmSecurityHeader asymHeader;
  119. UA_AsymmetricAlgorithmSecurityHeader_init(&asymHeader);
  120. asymHeader.securityPolicyUri = UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
  121. /* id of opensecurechannelrequest */
  122. UA_NodeId requestType = UA_NODEID_NUMERIC(0, UA_NS0ID_OPENSECURECHANNELREQUEST + UA_ENCODINGOFFSET_BINARY);
  123. UA_OpenSecureChannelRequest opnSecRq;
  124. UA_OpenSecureChannelRequest_init(&opnSecRq);
  125. opnSecRq.requestHeader.timestamp = UA_DateTime_now();
  126. opnSecRq.requestHeader.authenticationToken = client->authenticationToken;
  127. opnSecRq.requestedLifetime = client->config.secureChannelLifeTime;
  128. if(renew) {
  129. opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_RENEW;
  130. } else {
  131. opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_ISSUE;
  132. UA_SecureChannel_generateNonce(&client->channel.clientNonce);
  133. UA_ByteString_copy(&client->channel.clientNonce, &opnSecRq.clientNonce);
  134. opnSecRq.securityMode = UA_MESSAGESECURITYMODE_NONE;
  135. }
  136. UA_ByteString message;
  137. UA_StatusCode retval = client->connection.getBuffer(&client->connection, &message);
  138. if(retval != UA_STATUSCODE_GOOD) {
  139. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  140. UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq);
  141. return retval;
  142. }
  143. size_t offset = 12;
  144. retval = UA_AsymmetricAlgorithmSecurityHeader_encodeBinary(&asymHeader, &message, &offset);
  145. retval |= UA_SequenceHeader_encodeBinary(&seqHeader, &message, &offset);
  146. retval |= UA_NodeId_encodeBinary(&requestType, &message, &offset);
  147. retval |= UA_OpenSecureChannelRequest_encodeBinary(&opnSecRq, &message, &offset);
  148. messageHeader.messageHeader.messageSize = offset;
  149. offset = 0;
  150. retval |= UA_SecureConversationMessageHeader_encodeBinary(&messageHeader, &message, &offset);
  151. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  152. UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq);
  153. if(retval != UA_STATUSCODE_GOOD) {
  154. client->connection.releaseBuffer(&client->connection, &message);
  155. return retval;
  156. }
  157. retval = client->connection.write(&client->connection, &message, messageHeader.messageHeader.messageSize);
  158. if(retval != UA_STATUSCODE_GOOD) {
  159. client->connection.releaseBuffer(&client->connection, &message);
  160. return retval;
  161. }
  162. UA_ByteString reply;
  163. UA_ByteString_init(&reply);
  164. do {
  165. retval = client->connection.recv(&client->connection, &reply, client->config.timeout);
  166. if(retval != UA_STATUSCODE_GOOD)
  167. return retval;
  168. } while(!reply.data);
  169. offset = 0;
  170. UA_SecureConversationMessageHeader_decodeBinary(&reply, &offset, &messageHeader);
  171. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(&reply, &offset, &asymHeader);
  172. UA_SequenceHeader_decodeBinary(&reply, &offset, &seqHeader);
  173. UA_NodeId_decodeBinary(&reply, &offset, &requestType);
  174. UA_NodeId expectedRequest = UA_NODEID_NUMERIC(0, UA_NS0ID_OPENSECURECHANNELRESPONSE +
  175. UA_ENCODINGOFFSET_BINARY);
  176. if(!UA_NodeId_equal(&requestType, &expectedRequest)) {
  177. UA_ByteString_deleteMembers(&reply);
  178. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  179. UA_NodeId_deleteMembers(&requestType);
  180. UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT,
  181. "Reply answers the wrong request. Expected OpenSecureChannelResponse.");
  182. return UA_STATUSCODE_BADINTERNALERROR;
  183. }
  184. UA_OpenSecureChannelResponse response;
  185. UA_OpenSecureChannelResponse_decodeBinary(&reply, &offset, &response);
  186. client->scExpiresAt = UA_DateTime_now() + response.securityToken.revisedLifetime * 10000;
  187. UA_ByteString_deleteMembers(&reply);
  188. retval = response.responseHeader.serviceResult;
  189. if(!renew && retval == UA_STATUSCODE_GOOD) {
  190. UA_ChannelSecurityToken_copy(&response.securityToken, &client->channel.securityToken);
  191. UA_ByteString_deleteMembers(&client->channel.serverNonce); // if the handshake is repeated
  192. UA_ByteString_copy(&response.serverNonce, &client->channel.serverNonce);
  193. }
  194. UA_OpenSecureChannelResponse_deleteMembers(&response);
  195. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  196. return retval;
  197. }
  198. /** If the request fails, then the response is cast to UA_ResponseHeader (at the beginning of every
  199. response) and filled with the appropriate error code */
  200. static void synchronousRequest(UA_Client *client, void *request, const UA_DataType *requestType,
  201. void *response, const UA_DataType *responseType) {
  202. /* Check if sc needs to be renewed */
  203. if(client->scExpiresAt - UA_DateTime_now() <= client->config.timeToRenewSecureChannel * 10000 )
  204. UA_Client_renewSecureChannel(client);
  205. /* Copy authenticationToken token to request header */
  206. typedef struct {
  207. UA_RequestHeader requestHeader;
  208. } headerOnlyRequest;
  209. /* The cast is valid, since all requests start with a requestHeader */
  210. UA_NodeId_copy(&client->authenticationToken, &((headerOnlyRequest*)request)->requestHeader.authenticationToken);
  211. if(!response)
  212. return;
  213. UA_init(response, responseType);
  214. /* Send the request */
  215. UA_UInt32 requestId = ++client->requestId;
  216. UA_StatusCode retval = UA_SecureChannel_sendBinaryMessage(&client->channel, requestId,
  217. request, requestType);
  218. UA_ResponseHeader *respHeader = (UA_ResponseHeader*)response;
  219. if(retval) {
  220. if(retval == UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED)
  221. respHeader->serviceResult = UA_STATUSCODE_BADREQUESTTOOLARGE;
  222. else
  223. respHeader->serviceResult = retval;
  224. return;
  225. }
  226. /* Retrieve the response */
  227. // Todo: push this into the generic securechannel implementation for client and server
  228. UA_ByteString reply;
  229. UA_ByteString_init(&reply);
  230. do {
  231. retval = client->connection.recv(&client->connection, &reply, client->config.timeout);
  232. if(retval != UA_STATUSCODE_GOOD) {
  233. respHeader->serviceResult = retval;
  234. return;
  235. }
  236. } while(!reply.data);
  237. size_t offset = 0;
  238. UA_SecureConversationMessageHeader msgHeader;
  239. retval |= UA_SecureConversationMessageHeader_decodeBinary(&reply, &offset, &msgHeader);
  240. UA_SymmetricAlgorithmSecurityHeader symHeader;
  241. retval |= UA_SymmetricAlgorithmSecurityHeader_decodeBinary(&reply, &offset, &symHeader);
  242. UA_SequenceHeader seqHeader;
  243. retval |= UA_SequenceHeader_decodeBinary(&reply, &offset, &seqHeader);
  244. UA_NodeId responseId;
  245. retval |= UA_NodeId_decodeBinary(&reply, &offset, &responseId);
  246. UA_NodeId expectedNodeId = UA_NODEID_NUMERIC(0, responseType->typeId.identifier.numeric +
  247. UA_ENCODINGOFFSET_BINARY);
  248. if(retval != UA_STATUSCODE_GOOD)
  249. goto finish;
  250. /* Todo: we need to demux responses since a publish responses may come at any time */
  251. if(!UA_NodeId_equal(&responseId, &expectedNodeId) || seqHeader.requestId != requestId) {
  252. if(responseId.identifier.numeric != UA_NS0ID_SERVICEFAULT + UA_ENCODINGOFFSET_BINARY) {
  253. UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT,
  254. "Reply answers the wrong request. Expected ns=%i,i=%i. But retrieved ns=%i,i=%i",
  255. expectedNodeId.namespaceIndex, expectedNodeId.identifier.numeric,
  256. responseId.namespaceIndex, responseId.identifier.numeric);
  257. respHeader->serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  258. } else
  259. retval = UA_decodeBinary(&reply, &offset, respHeader, &UA_TYPES[UA_TYPES_SERVICEFAULT]);
  260. goto finish;
  261. }
  262. retval = UA_decodeBinary(&reply, &offset, response, responseType);
  263. if(retval == UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED)
  264. retval = UA_STATUSCODE_BADRESPONSETOOLARGE;
  265. finish:
  266. UA_SymmetricAlgorithmSecurityHeader_deleteMembers(&symHeader);
  267. UA_ByteString_deleteMembers(&reply);
  268. if(retval != UA_STATUSCODE_GOOD)
  269. respHeader->serviceResult = retval;
  270. }
  271. static UA_StatusCode ActivateSession(UA_Client *client) {
  272. UA_ActivateSessionRequest request;
  273. UA_ActivateSessionRequest_init(&request);
  274. request.requestHeader.requestHandle = 2; //TODO: is it a magic number?
  275. request.requestHeader.authenticationToken = client->authenticationToken;
  276. request.requestHeader.timestamp = UA_DateTime_now();
  277. request.requestHeader.timeoutHint = 10000;
  278. UA_AnonymousIdentityToken identityToken;
  279. UA_AnonymousIdentityToken_init(&identityToken);
  280. UA_String_copy(&client->token.policyId, &identityToken.policyId);
  281. //manual ExtensionObject encoding of the identityToken
  282. request.userIdentityToken.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  283. request.userIdentityToken.typeId = UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN].typeId;
  284. request.userIdentityToken.typeId.identifier.numeric+=UA_ENCODINGOFFSET_BINARY;
  285. UA_ByteString_newMembers(&request.userIdentityToken.body, identityToken.policyId.length+4);
  286. size_t offset = 0;
  287. UA_ByteString_encodeBinary(&identityToken.policyId,&request.userIdentityToken.body,&offset);
  288. UA_ActivateSessionResponse response;
  289. synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_ACTIVATESESSIONREQUEST],
  290. &response, &UA_TYPES[UA_TYPES_ACTIVATESESSIONRESPONSE]);
  291. UA_AnonymousIdentityToken_deleteMembers(&identityToken);
  292. UA_ActivateSessionRequest_deleteMembers(&request);
  293. UA_ActivateSessionResponse_deleteMembers(&response);
  294. return response.responseHeader.serviceResult; // not deleted
  295. }
  296. static UA_StatusCode EndpointsHandshake(UA_Client *client) {
  297. UA_GetEndpointsRequest request;
  298. UA_GetEndpointsRequest_init(&request);
  299. UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
  300. request.requestHeader.timestamp = UA_DateTime_now();
  301. request.requestHeader.timeoutHint = 10000;
  302. UA_String_copy(&client->endpointUrl, &request.endpointUrl);
  303. request.profileUrisSize = 1;
  304. request.profileUris = UA_Array_new(&UA_TYPES[UA_TYPES_STRING], request.profileUrisSize);
  305. *request.profileUris = UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  306. UA_GetEndpointsResponse response;
  307. UA_GetEndpointsResponse_init(&response);
  308. synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_GETENDPOINTSREQUEST],
  309. &response, &UA_TYPES[UA_TYPES_GETENDPOINTSRESPONSE]);
  310. UA_Boolean endpointFound = UA_FALSE;
  311. UA_Boolean tokenFound = UA_FALSE;
  312. for(UA_Int32 i=0; i<response.endpointsSize; ++i){
  313. UA_EndpointDescription* endpoint = &response.endpoints[i];
  314. /* look out for an endpoint without security */
  315. if(!UA_String_equal(&endpoint->securityPolicyUri,
  316. &UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#None")))
  317. continue;
  318. endpointFound = UA_TRUE;
  319. /* endpoint with no security found */
  320. /* look for a user token policy with an anonymous token */
  321. for(UA_Int32 j=0; j<endpoint->userIdentityTokensSize; ++j) {
  322. UA_UserTokenPolicy* userToken = &endpoint->userIdentityTokens[j];
  323. if(userToken->tokenType != UA_USERTOKENTYPE_ANONYMOUS)
  324. continue;
  325. tokenFound = UA_TRUE;
  326. UA_UserTokenPolicy_copy(userToken, &client->token);
  327. break;
  328. }
  329. }
  330. UA_GetEndpointsRequest_deleteMembers(&request);
  331. UA_GetEndpointsResponse_deleteMembers(&response);
  332. if(!endpointFound){
  333. UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT, "No suitable endpoint found");
  334. return UA_STATUSCODE_BADINTERNALERROR;
  335. }
  336. if(!tokenFound){
  337. UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT, "No anonymous token found");
  338. return UA_STATUSCODE_BADINTERNALERROR;
  339. }
  340. return response.responseHeader.serviceResult;
  341. }
  342. static UA_StatusCode SessionHandshake(UA_Client *client) {
  343. UA_CreateSessionRequest request;
  344. UA_CreateSessionRequest_init(&request);
  345. // todo: is this needed for all requests?
  346. UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
  347. request.requestHeader.timestamp = UA_DateTime_now();
  348. request.requestHeader.timeoutHint = 10000;
  349. UA_ByteString_copy(&client->channel.clientNonce, &request.clientNonce);
  350. request.requestedSessionTimeout = 1200000;
  351. request.maxResponseMessageSize = UA_INT32_MAX;
  352. UA_CreateSessionResponse response;
  353. UA_CreateSessionResponse_init(&response);
  354. synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_CREATESESSIONREQUEST],
  355. &response, &UA_TYPES[UA_TYPES_CREATESESSIONRESPONSE]);
  356. UA_NodeId_copy(&response.authenticationToken, &client->authenticationToken);
  357. UA_CreateSessionRequest_deleteMembers(&request);
  358. UA_CreateSessionResponse_deleteMembers(&response);
  359. return response.responseHeader.serviceResult; // not deleted
  360. }
  361. static UA_StatusCode CloseSession(UA_Client *client) {
  362. UA_CloseSessionRequest request;
  363. UA_CloseSessionRequest_init(&request);
  364. request.requestHeader.timestamp = UA_DateTime_now();
  365. request.requestHeader.timeoutHint = 10000;
  366. request.deleteSubscriptions = UA_TRUE;
  367. UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
  368. UA_CreateSessionResponse response;
  369. synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_CLOSESESSIONREQUEST],
  370. &response, &UA_TYPES[UA_TYPES_CLOSESESSIONRESPONSE]);
  371. UA_CloseSessionRequest_deleteMembers(&request);
  372. UA_CloseSessionResponse_deleteMembers(&response);
  373. return response.responseHeader.serviceResult; // not deleted
  374. }
  375. static UA_StatusCode CloseSecureChannel(UA_Client *client) {
  376. UA_SecureChannel *channel = &client->channel;
  377. UA_CloseSecureChannelRequest request;
  378. UA_CloseSecureChannelRequest_init(&request);
  379. request.requestHeader.requestHandle = 1; //TODO: magic number?
  380. request.requestHeader.timestamp = UA_DateTime_now();
  381. request.requestHeader.timeoutHint = 10000;
  382. request.requestHeader.authenticationToken = client->authenticationToken;
  383. UA_SecureConversationMessageHeader msgHeader;
  384. msgHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_CLOF;
  385. msgHeader.secureChannelId = client->channel.securityToken.channelId;
  386. UA_SymmetricAlgorithmSecurityHeader symHeader;
  387. symHeader.tokenId = channel->securityToken.tokenId;
  388. UA_SequenceHeader seqHeader;
  389. seqHeader.sequenceNumber = ++channel->sequenceNumber;
  390. seqHeader.requestId = ++client->requestId;
  391. UA_NodeId typeId = UA_NODEID_NUMERIC(0, UA_NS0ID_CLOSESECURECHANNELREQUEST + UA_ENCODINGOFFSET_BINARY);
  392. UA_ByteString message;
  393. UA_StatusCode retval = client->connection.getBuffer(&client->connection, &message);
  394. if(retval != UA_STATUSCODE_GOOD)
  395. return retval;
  396. size_t offset = 12;
  397. retval |= UA_SymmetricAlgorithmSecurityHeader_encodeBinary(&symHeader, &message, &offset);
  398. retval |= UA_SequenceHeader_encodeBinary(&seqHeader, &message, &offset);
  399. retval |= UA_NodeId_encodeBinary(&typeId, &message, &offset);
  400. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_CLOSESECURECHANNELREQUEST], &message, &offset);
  401. msgHeader.messageHeader.messageSize = offset;
  402. offset = 0;
  403. retval |= UA_SecureConversationMessageHeader_encodeBinary(&msgHeader, &message, &offset);
  404. if(retval != UA_STATUSCODE_GOOD) {
  405. client->connection.releaseBuffer(&client->connection, &message);
  406. return retval;
  407. }
  408. retval = client->connection.write(&client->connection, &message, msgHeader.messageHeader.messageSize);
  409. if(retval != UA_STATUSCODE_GOOD)
  410. client->connection.releaseBuffer(&client->connection, &message);
  411. return retval;
  412. }
  413. /*************************/
  414. /* User-Facing Functions */
  415. /*************************/
  416. UA_StatusCode UA_Client_connect(UA_Client *client, UA_ConnectClientConnection connectFunc, char *endpointUrl) {
  417. client->connection = connectFunc(UA_ConnectionConfig_standard, endpointUrl, &client->logger);
  418. if(client->connection.state != UA_CONNECTION_OPENING)
  419. return UA_STATUSCODE_BADCONNECTIONCLOSED;
  420. client->endpointUrl = UA_STRING_ALLOC(endpointUrl);
  421. if(client->endpointUrl.length < 0)
  422. return UA_STATUSCODE_BADOUTOFMEMORY;
  423. client->connection.localConf = client->config.localConnectionConfig;
  424. UA_StatusCode retval = HelAckHandshake(client);
  425. if(retval == UA_STATUSCODE_GOOD)
  426. retval = SecureChannelHandshake(client, UA_FALSE);
  427. if(retval == UA_STATUSCODE_GOOD)
  428. retval = EndpointsHandshake(client);
  429. if(retval == UA_STATUSCODE_GOOD)
  430. retval = SessionHandshake(client);
  431. if(retval == UA_STATUSCODE_GOOD)
  432. retval = ActivateSession(client);
  433. if(retval == UA_STATUSCODE_GOOD)
  434. client->connection.state = UA_CONNECTION_ESTABLISHED;
  435. return retval;
  436. }
  437. UA_StatusCode UA_Client_disconnect(UA_Client *client) {
  438. UA_StatusCode retval;
  439. if(client->channel.connection->state != UA_CONNECTION_ESTABLISHED)
  440. return UA_STATUSCODE_GOOD;
  441. retval = CloseSession(client);
  442. if(retval == UA_STATUSCODE_GOOD)
  443. retval = CloseSecureChannel(client);
  444. return retval;
  445. }
  446. UA_StatusCode UA_Client_renewSecureChannel(UA_Client *client) {
  447. return SecureChannelHandshake(client, UA_TRUE);
  448. }
  449. UA_ReadResponse UA_Client_read(UA_Client *client, UA_ReadRequest *request) {
  450. UA_ReadResponse response;
  451. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_READREQUEST], &response,
  452. &UA_TYPES[UA_TYPES_READRESPONSE]);
  453. return response;
  454. }
  455. UA_WriteResponse UA_Client_write(UA_Client *client, UA_WriteRequest *request) {
  456. UA_WriteResponse response;
  457. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_WRITEREQUEST], &response,
  458. &UA_TYPES[UA_TYPES_WRITERESPONSE]);
  459. return response;
  460. }
  461. UA_BrowseResponse UA_Client_browse(UA_Client *client, UA_BrowseRequest *request) {
  462. UA_BrowseResponse response;
  463. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
  464. &UA_TYPES[UA_TYPES_BROWSERESPONSE]);
  465. return response;
  466. }
  467. UA_BrowseNextResponse UA_Client_browseNext(UA_Client *client, UA_BrowseNextRequest *request) {
  468. UA_BrowseNextResponse response;
  469. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_BROWSENEXTREQUEST], &response,
  470. &UA_TYPES[UA_TYPES_BROWSENEXTRESPONSE]);
  471. return response;
  472. }
  473. UA_TranslateBrowsePathsToNodeIdsResponse
  474. UA_Client_translateTranslateBrowsePathsToNodeIds(UA_Client *client,
  475. UA_TranslateBrowsePathsToNodeIdsRequest *request) {
  476. UA_TranslateBrowsePathsToNodeIdsResponse response;
  477. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSREQUEST],
  478. &response, &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE]);
  479. return response;
  480. }
  481. UA_AddNodesResponse UA_Client_addNodes(UA_Client *client, UA_AddNodesRequest *request) {
  482. UA_AddNodesResponse response;
  483. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  484. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  485. return response;
  486. }
  487. UA_AddReferencesResponse UA_Client_addReferences(UA_Client *client, UA_AddReferencesRequest *request) {
  488. UA_AddReferencesResponse response;
  489. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_ADDREFERENCESREQUEST],
  490. &response, &UA_TYPES[UA_TYPES_ADDREFERENCESRESPONSE]);
  491. return response;
  492. }
  493. UA_DeleteNodesResponse UA_Client_deleteNodes(UA_Client *client, UA_DeleteNodesRequest *request) {
  494. UA_DeleteNodesResponse response;
  495. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  496. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  497. return response;
  498. }
  499. UA_DeleteReferencesResponse UA_Client_deleteReferences(UA_Client *client, UA_DeleteReferencesRequest *request) {
  500. UA_DeleteReferencesResponse response;
  501. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETEREFERENCESREQUEST],
  502. &response, &UA_TYPES[UA_TYPES_DELETEREFERENCESRESPONSE]);
  503. return response;
  504. }
  505. #ifdef ENABLE_SUBSCRIPTIONS
  506. UA_CreateSubscriptionResponse UA_Client_createSubscription(UA_Client *client, UA_CreateSubscriptionRequest *request) {
  507. UA_CreateSubscriptionResponse response;
  508. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONREQUEST],
  509. &response, &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONRESPONSE]);
  510. return response;
  511. }
  512. UA_DeleteSubscriptionsResponse UA_Client_deleteSubscriptions(UA_Client *client, UA_DeleteSubscriptionsRequest *request) {
  513. UA_DeleteSubscriptionsResponse response;
  514. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSREQUEST],
  515. &response, &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSRESPONSE]);
  516. return response;
  517. }
  518. UA_ModifySubscriptionResponse UA_Client_modifySubscription(UA_Client *client, UA_ModifySubscriptionRequest *request) {
  519. UA_ModifySubscriptionResponse response;
  520. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONREQUEST],
  521. &response, &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONRESPONSE]);
  522. return response;
  523. }
  524. UA_CreateMonitoredItemsResponse UA_Client_createMonitoredItems(UA_Client *client, UA_CreateMonitoredItemsRequest *request) {
  525. UA_CreateMonitoredItemsResponse response;
  526. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSREQUEST],
  527. &response, &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSRESPONSE]);
  528. return response;
  529. }
  530. UA_DeleteMonitoredItemsResponse UA_Client_deleteMonitoredItems(UA_Client *client, UA_DeleteMonitoredItemsRequest *request) {
  531. UA_DeleteMonitoredItemsResponse response;
  532. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSREQUEST],
  533. &response, &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSRESPONSE]);
  534. return response;
  535. }
  536. UA_PublishResponse UA_Client_publish(UA_Client *client, UA_PublishRequest *request) {
  537. UA_PublishResponse response;
  538. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  539. &response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  540. return response;
  541. }
  542. UA_Int32 UA_Client_newSubscription(UA_Client *client, UA_Int32 publishInterval) {
  543. UA_Int32 retval;
  544. UA_CreateSubscriptionRequest aReq;
  545. UA_CreateSubscriptionResponse aRes;
  546. UA_CreateSubscriptionRequest_init(&aReq);
  547. UA_CreateSubscriptionResponse_init(&aRes);
  548. aReq.maxNotificationsPerPublish = 10;
  549. aReq.priority = 0;
  550. aReq.publishingEnabled = UA_TRUE;
  551. aReq.requestedLifetimeCount = 100;
  552. aReq.requestedMaxKeepAliveCount = 10;
  553. aReq.requestedPublishingInterval = publishInterval;
  554. aRes = UA_Client_createSubscription(client, &aReq);
  555. if (aRes.responseHeader.serviceResult == UA_STATUSCODE_GOOD) {
  556. UA_Client_Subscription *newSub = UA_malloc(sizeof(UA_Client_Subscription));
  557. LIST_INIT(&newSub->MonitoredItems);
  558. newSub->LifeTime = aRes.revisedLifetimeCount;
  559. newSub->KeepAliveCount = aRes.revisedMaxKeepAliveCount;
  560. newSub->PublishingInterval = aRes.revisedPublishingInterval;
  561. newSub->SubscriptionID = aRes.subscriptionId;
  562. newSub->NotificationsPerPublish = aReq.maxNotificationsPerPublish;
  563. newSub->Priority = aReq.priority;
  564. retval = newSub->SubscriptionID;
  565. LIST_INSERT_HEAD(&(client->subscriptions), newSub, listEntry);
  566. } else
  567. retval = 0;
  568. UA_CreateSubscriptionResponse_deleteMembers(&aRes);
  569. UA_CreateSubscriptionRequest_deleteMembers(&aReq);
  570. return retval;
  571. }
  572. UA_StatusCode UA_Client_removeSubscription(UA_Client *client, UA_UInt32 subscriptionId) {
  573. UA_Client_Subscription *sub;
  574. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  575. LIST_FOREACH(sub, &(client->subscriptions), listEntry) {
  576. if (sub->SubscriptionID == subscriptionId)
  577. break;
  578. }
  579. // Problem? We do not have this subscription registeres. Maybe the server should
  580. // be consulted at this point?
  581. if (sub == NULL)
  582. return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  583. UA_DeleteSubscriptionsRequest request;
  584. UA_DeleteSubscriptionsResponse response;
  585. UA_DeleteSubscriptionsRequest_init(&request);
  586. UA_DeleteSubscriptionsResponse_init(&response);
  587. request.subscriptionIdsSize=1;
  588. request.subscriptionIds = (UA_UInt32 *) UA_malloc(sizeof(UA_UInt32));
  589. *(request.subscriptionIds) = sub->SubscriptionID;
  590. UA_Client_MonitoredItem *mon;
  591. LIST_FOREACH(mon, &(sub->MonitoredItems), listEntry) {
  592. retval |= UA_Client_unMonitorItemChanges(client, sub->SubscriptionID, mon->MonitoredItemId);
  593. }
  594. if (retval != UA_STATUSCODE_GOOD)
  595. return retval;
  596. response = UA_Client_deleteSubscriptions(client, &request);
  597. if (response.resultsSize > 0)
  598. retval = response.results[0];
  599. else
  600. retval = response.responseHeader.serviceResult;
  601. if (retval == UA_STATUSCODE_GOOD) {
  602. LIST_REMOVE(sub, listEntry);
  603. UA_free(sub);
  604. }
  605. UA_DeleteSubscriptionsRequest_deleteMembers(&request);
  606. UA_DeleteSubscriptionsResponse_deleteMembers(&response);
  607. return retval;
  608. }
  609. UA_UInt32 UA_Client_monitorItemChanges(UA_Client *client, UA_UInt32 subscriptionId,
  610. UA_NodeId nodeId, UA_UInt32 attributeID, void *handlingFunction) {
  611. UA_Client_Subscription *sub;
  612. UA_StatusCode retval = 0;
  613. LIST_FOREACH(sub, &(client->subscriptions), listEntry) {
  614. if (sub->SubscriptionID == subscriptionId)
  615. break;
  616. }
  617. // Maybe the same problem as in DeleteSubscription... ask the server?
  618. if (sub == NULL)
  619. return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  620. UA_CreateMonitoredItemsRequest request;
  621. UA_CreateMonitoredItemsResponse response;
  622. UA_CreateMonitoredItemsRequest_init(&request);
  623. UA_CreateMonitoredItemsResponse_init(&response);
  624. request.subscriptionId = subscriptionId;
  625. request.itemsToCreateSize = 1;
  626. request.itemsToCreate = UA_MonitoredItemCreateRequest_new();
  627. UA_NodeId_copy(&nodeId, &((request.itemsToCreate[0]).itemToMonitor.nodeId));
  628. (request.itemsToCreate[0]).itemToMonitor.attributeId = attributeID;
  629. (request.itemsToCreate[0]).monitoringMode = UA_MONITORINGMODE_REPORTING;
  630. (request.itemsToCreate[0]).requestedParameters.clientHandle = ++(client->monitoredItemHandles);
  631. (request.itemsToCreate[0]).requestedParameters.samplingInterval = sub->PublishingInterval;
  632. (request.itemsToCreate[0]).requestedParameters.discardOldest = UA_TRUE;
  633. (request.itemsToCreate[0]).requestedParameters.queueSize = 1;
  634. // Filter can be left void for now, only changes are supported (UA_Expert does the same with changeItems)
  635. response = UA_Client_createMonitoredItems(client, &request);
  636. // slight misuse of retval here to check if the deletion was successfull.
  637. if (response.resultsSize == 0)
  638. retval = response.responseHeader.serviceResult;
  639. else
  640. retval = response.results[0].statusCode;
  641. if (retval == UA_STATUSCODE_GOOD) {
  642. UA_Client_MonitoredItem *newMon = (UA_Client_MonitoredItem *) UA_malloc(sizeof(UA_Client_MonitoredItem));
  643. newMon->MonitoringMode = UA_MONITORINGMODE_REPORTING;
  644. UA_NodeId_copy(&nodeId, &(newMon->monitoredNodeId));
  645. newMon->AttributeID = attributeID;
  646. newMon->ClientHandle = client->monitoredItemHandles;
  647. newMon->SamplingInterval = sub->PublishingInterval;
  648. newMon->QueueSize = 1;
  649. newMon->DiscardOldest = UA_TRUE;
  650. newMon->handler = handlingFunction;
  651. newMon->MonitoredItemId = response.results[0].monitoredItemId;
  652. LIST_INSERT_HEAD(&(sub->MonitoredItems), newMon, listEntry);
  653. retval = newMon->MonitoredItemId ;
  654. }
  655. else {
  656. retval = 0;
  657. }
  658. UA_CreateMonitoredItemsRequest_deleteMembers(&request);
  659. UA_CreateMonitoredItemsResponse_deleteMembers(&response);
  660. return retval;
  661. }
  662. UA_StatusCode UA_Client_unMonitorItemChanges(UA_Client *client, UA_UInt32 subscriptionId, UA_UInt32 monitoredItemId ) {
  663. UA_Client_Subscription *sub;
  664. UA_StatusCode retval = 0;
  665. LIST_FOREACH(sub, &(client->subscriptions), listEntry) {
  666. if (sub->SubscriptionID == subscriptionId)
  667. break;
  668. }
  669. // Maybe the same problem as in DeleteSubscription... ask the server?
  670. if (sub == NULL)
  671. return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  672. UA_Client_MonitoredItem *mon;
  673. LIST_FOREACH(mon, &(sub->MonitoredItems), listEntry) {
  674. if (mon->MonitoredItemId == monitoredItemId)
  675. break;
  676. }
  677. // Also... ask the server?
  678. if(mon==NULL) {
  679. return UA_STATUSCODE_BADMONITOREDITEMIDINVALID;
  680. }
  681. UA_DeleteMonitoredItemsRequest request;
  682. UA_DeleteMonitoredItemsResponse response;
  683. UA_DeleteMonitoredItemsRequest_init(&request);
  684. UA_DeleteMonitoredItemsResponse_init(&response);
  685. request.subscriptionId = sub->SubscriptionID;
  686. request.monitoredItemIdsSize = 1;
  687. request.monitoredItemIds = (UA_UInt32 *) UA_malloc(sizeof(UA_UInt32));
  688. request.monitoredItemIds[0] = mon->MonitoredItemId;
  689. response = UA_Client_deleteMonitoredItems(client, &request);
  690. if (response.resultsSize > 1)
  691. retval = response.results[0];
  692. else
  693. retval = response.responseHeader.serviceResult;
  694. if (retval == 0) {
  695. LIST_REMOVE(mon, listEntry);
  696. UA_free(mon);
  697. }
  698. UA_DeleteMonitoredItemsRequest_deleteMembers(&request);
  699. UA_DeleteMonitoredItemsResponse_deleteMembers(&response);
  700. return retval;
  701. }
  702. UA_Boolean UA_Client_processPublishRx(UA_Client *client, UA_PublishResponse response) {
  703. UA_Client_Subscription *sub;
  704. UA_Client_MonitoredItem *mon;
  705. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  706. if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  707. return UA_FALSE;
  708. // Check if the server has acknowledged any of our ACKS
  709. // Note that a list of serverside status codes may be send without valid publish data, i.e.
  710. // during keepalives or no data availability
  711. UA_Client_NotificationsAckNumber *tmpAck = client->pendingNotificationsAcks.lh_first;
  712. UA_Client_NotificationsAckNumber *nxtAck = tmpAck;
  713. for(int i=0; i<response.resultsSize && nxtAck != NULL; i++) {
  714. tmpAck = nxtAck;
  715. nxtAck = tmpAck->listEntry.le_next;
  716. if (response.results[i] == UA_STATUSCODE_GOOD) {
  717. LIST_REMOVE(tmpAck, listEntry);
  718. UA_free(tmpAck);
  719. }
  720. }
  721. if(response.subscriptionId == 0)
  722. return UA_FALSE;
  723. LIST_FOREACH(sub, &(client->subscriptions), listEntry) {
  724. if (sub->SubscriptionID == response.subscriptionId)
  725. break;
  726. }
  727. if (sub == NULL)
  728. return UA_FALSE;
  729. UA_NotificationMessage msg = response.notificationMessage;
  730. UA_DataChangeNotification dataChangeNotification;
  731. size_t decodingOffset = 0;
  732. for (int k=0; k<msg.notificationDataSize; k++) {
  733. if (msg.notificationData[k].encoding == UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING) {
  734. if (msg.notificationData[k].typeId.namespaceIndex == 0 && msg.notificationData[k].typeId.identifier.numeric == 811 ) {
  735. // This is a dataChangeNotification
  736. retval |= UA_DataChangeNotification_decodeBinary(&(msg.notificationData[k].body), &decodingOffset, &dataChangeNotification);
  737. UA_MonitoredItemNotification *mitemNot;
  738. for(int i=0; i<dataChangeNotification.monitoredItemsSize; i++) {
  739. mitemNot = &dataChangeNotification.monitoredItems[i];
  740. // find this client handle
  741. LIST_FOREACH(mon, &(sub->MonitoredItems), listEntry) {
  742. if (mon->ClientHandle == mitemNot->clientHandle) {
  743. mon->handler(mitemNot->clientHandle, &(mitemNot->value));
  744. break;
  745. }
  746. }
  747. }
  748. }
  749. else if (msg.notificationData[k].typeId.namespaceIndex == 0 && msg.notificationData[k].typeId.identifier.numeric == 820 ) {
  750. //FIXME: This is a statusChangeNotification (not supported yet)
  751. continue;
  752. }
  753. else if (msg.notificationData[k].typeId.namespaceIndex == 0 && msg.notificationData[k].typeId.identifier.numeric == 916 ) {
  754. //FIXME: This is an EventNotification
  755. continue;
  756. }
  757. }
  758. }
  759. // We processed this message, add it to the list of pending acks (but make sure it's not in the list first)
  760. LIST_FOREACH(tmpAck, &(client->pendingNotificationsAcks), listEntry) {
  761. if (tmpAck->subAck.sequenceNumber == msg.sequenceNumber &&
  762. tmpAck->subAck.subscriptionId == response.subscriptionId)
  763. break;
  764. }
  765. if (tmpAck == NULL ){
  766. tmpAck = (UA_Client_NotificationsAckNumber *) malloc(sizeof(UA_Client_NotificationsAckNumber));
  767. tmpAck->subAck.sequenceNumber = msg.sequenceNumber;
  768. tmpAck->subAck.subscriptionId = sub->SubscriptionID;
  769. LIST_INSERT_HEAD(&(client->pendingNotificationsAcks), tmpAck, listEntry);
  770. }
  771. return response.moreNotifications;
  772. }
  773. void UA_Client_doPublish(UA_Client *client) {
  774. UA_PublishRequest request;
  775. UA_PublishResponse response;
  776. UA_Client_NotificationsAckNumber *ack;
  777. UA_Boolean moreNotifications = UA_TRUE;
  778. int index = 0 ;
  779. do {
  780. UA_PublishRequest_init(&request);
  781. UA_PublishResponse_init(&response);
  782. request.subscriptionAcknowledgementsSize = 0;
  783. LIST_FOREACH(ack, &(client->pendingNotificationsAcks), listEntry) {
  784. request.subscriptionAcknowledgementsSize++;
  785. }
  786. request.subscriptionAcknowledgements = (UA_SubscriptionAcknowledgement *) UA_malloc(sizeof(UA_SubscriptionAcknowledgement)*request.subscriptionAcknowledgementsSize);
  787. index = 0;
  788. LIST_FOREACH(ack, &(client->pendingNotificationsAcks), listEntry) {
  789. ack = client->pendingNotificationsAcks.lh_first;
  790. request.subscriptionAcknowledgements[index].sequenceNumber = ack->subAck.sequenceNumber;
  791. request.subscriptionAcknowledgements[index].subscriptionId = ack->subAck.subscriptionId;
  792. index++;
  793. }
  794. response = UA_Client_publish(client, &request);
  795. if (response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  796. moreNotifications = UA_Client_processPublishRx(client, response);
  797. else
  798. moreNotifications = UA_FALSE;
  799. UA_PublishResponse_deleteMembers(&response);
  800. UA_PublishRequest_deleteMembers(&request);
  801. } while(moreNotifications == UA_TRUE);
  802. return;
  803. }
  804. #endif
  805. /**********************************/
  806. /* User-Facing Macros-Function */
  807. /**********************************/
  808. #ifdef ENABLE_METHODCALLS
  809. UA_CallResponse UA_Client_call(UA_Client *client, UA_CallRequest *request) {
  810. UA_CallResponse response;
  811. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_CALLREQUEST],
  812. &response, &UA_TYPES[UA_TYPES_CALLRESPONSE]);
  813. return response;
  814. }
  815. UA_StatusCode UA_Client_CallServerMethod(UA_Client *client, UA_NodeId objectNodeId, UA_NodeId methodNodeId,
  816. UA_Int32 inputSize, const UA_Variant *input,
  817. UA_Int32 *outputSize, UA_Variant **output) {
  818. UA_CallRequest request;
  819. UA_CallRequest_init(&request);
  820. request.methodsToCallSize = 1;
  821. request.methodsToCall = UA_CallMethodRequest_new();
  822. if(!request.methodsToCall)
  823. return UA_STATUSCODE_BADOUTOFMEMORY;
  824. UA_CallMethodRequest *rq = &request.methodsToCall[0];
  825. UA_NodeId_copy(&methodNodeId, &rq->methodId);
  826. UA_NodeId_copy(&objectNodeId, &rq->objectId);
  827. rq->inputArguments = (void*)(uintptr_t)input; // cast const...
  828. rq->inputArgumentsSize = inputSize;
  829. UA_CallResponse response;
  830. response = UA_Client_call(client, &request);
  831. rq->inputArguments = UA_NULL;
  832. rq->inputArgumentsSize = -1;
  833. UA_CallRequest_deleteMembers(&request);
  834. UA_StatusCode retval = response.responseHeader.serviceResult;
  835. retval |= response.results[0].statusCode;
  836. if(retval == UA_STATUSCODE_GOOD) {
  837. *output = response.results[0].outputArguments;
  838. *outputSize = response.results[0].outputArgumentsSize;
  839. response.results[0].outputArguments = UA_NULL;
  840. response.results[0].outputArgumentsSize = -1;
  841. }
  842. UA_CallResponse_deleteMembers(&response);
  843. return retval;
  844. }
  845. #endif
  846. #define ADDNODES_COPYDEFAULTATTRIBUTES(REQUEST,ATTRIBUTES) do { \
  847. ATTRIBUTES.specifiedAttributes = 0; \
  848. if(! UA_LocalizedText_copy(&description, &(ATTRIBUTES.description))) \
  849. ATTRIBUTES.specifiedAttributes |= UA_NODEATTRIBUTESMASK_DESCRIPTION; \
  850. if(! UA_LocalizedText_copy(&displayName, &(ATTRIBUTES.displayName))) \
  851. ATTRIBUTES.specifiedAttributes |= UA_NODEATTRIBUTESMASK_DISPLAYNAME; \
  852. ATTRIBUTES.userWriteMask = userWriteMask; \
  853. ATTRIBUTES.specifiedAttributes |= UA_NODEATTRIBUTESMASK_USERWRITEMASK; \
  854. ATTRIBUTES.writeMask = writeMask; \
  855. ATTRIBUTES.specifiedAttributes |= UA_NODEATTRIBUTESMASK_WRITEMASK; \
  856. UA_QualifiedName_copy(&browseName, &(REQUEST.nodesToAdd[0].browseName)); \
  857. UA_ExpandedNodeId_copy(&parentNodeId, &(REQUEST.nodesToAdd[0].parentNodeId)); \
  858. UA_NodeId_copy(&referenceTypeId, &(REQUEST.nodesToAdd[0].referenceTypeId)); \
  859. UA_ExpandedNodeId_copy(&typeDefinition, &(REQUEST.nodesToAdd[0].typeDefinition)); \
  860. UA_ExpandedNodeId reqExpNodeId; \
  861. UA_ExpandedNodeId_init(&reqExpNodeId); \
  862. UA_NodeId_copy(&reqId, &reqExpNodeId.nodeId); \
  863. UA_ExpandedNodeId_copy(&reqExpNodeId, &(REQUEST.nodesToAdd[0].requestedNewNodeId )); \
  864. REQUEST.nodesToAddSize = 1; \
  865. } while(0)
  866. #define ADDNODES_PACK_AND_SEND(PREQUEST,PATTRIBUTES,PNODETYPE) do { \
  867. PREQUEST.nodesToAdd[0].nodeAttributes.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING; \
  868. PREQUEST.nodesToAdd[0].nodeAttributes.typeId = UA_NODEID_NUMERIC(0, UA_NS0ID_##PNODETYPE##ATTRIBUTES + UA_ENCODINGOFFSET_BINARY); \
  869. size_t encOffset = 0; \
  870. UA_ByteString_newMembers(&PREQUEST.nodesToAdd[0].nodeAttributes.body, client->connection.remoteConf.maxMessageSize); \
  871. UA_encodeBinary(&PATTRIBUTES,&UA_TYPES[UA_TYPES_##PNODETYPE##ATTRIBUTES], &(PREQUEST.nodesToAdd[0].nodeAttributes.body), &encOffset); \
  872. PREQUEST.nodesToAdd[0].nodeAttributes.body.length = encOffset; \
  873. *(adRes) = UA_Client_addNodes(client, &PREQUEST); \
  874. UA_AddNodesRequest_deleteMembers(&PREQUEST); \
  875. } while(0)
  876. /* NodeManagement */
  877. UA_StatusCode UA_Client_addObjectNode(UA_Client *client, UA_NodeId reqId, UA_QualifiedName browseName, UA_LocalizedText displayName,
  878. UA_LocalizedText description, UA_ExpandedNodeId parentNodeId, UA_NodeId referenceTypeId,
  879. UA_UInt32 userWriteMask, UA_UInt32 writeMask, UA_ExpandedNodeId typeDefinition, UA_NodeId *createdNodeId) {
  880. UA_AddNodesRequest adReq;
  881. UA_StatusCode retval;
  882. UA_AddNodesRequest_init(&adReq);
  883. UA_AddNodesResponse *adRes;
  884. adRes = UA_AddNodesResponse_new();
  885. UA_AddNodesResponse_init(adRes);
  886. UA_ObjectAttributes vAtt;
  887. UA_ObjectAttributes_init(&vAtt);
  888. adReq.nodesToAdd = (UA_AddNodesItem *) UA_AddNodesItem_new();
  889. UA_AddNodesItem_init(adReq.nodesToAdd);
  890. // Default node properties and attributes
  891. ADDNODES_COPYDEFAULTATTRIBUTES(adReq, vAtt);
  892. // Specific to objects
  893. adReq.nodesToAdd[0].nodeClass = UA_NODECLASS_OBJECT;
  894. vAtt.eventNotifier = 0;
  895. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_EVENTNOTIFIER;
  896. ADDNODES_PACK_AND_SEND(adReq,vAtt,OBJECT);
  897. if(adRes->responseHeader.serviceResult != UA_STATUSCODE_GOOD || adRes->resultsSize == 0)
  898. retval = adRes->responseHeader.serviceResult;
  899. retval = adRes->results[0].statusCode;
  900. if(createdNodeId != NULL)
  901. UA_NodeId_copy(&adRes->results[0].addedNodeId, createdNodeId);
  902. UA_AddNodesResponse_deleteMembers(adRes);
  903. return retval;
  904. }
  905. UA_StatusCode UA_Client_addVariableNode(UA_Client *client, UA_NodeId reqId, UA_QualifiedName browseName, UA_LocalizedText displayName,
  906. UA_LocalizedText description, UA_ExpandedNodeId parentNodeId, UA_NodeId referenceTypeId,
  907. UA_UInt32 userWriteMask, UA_UInt32 writeMask, UA_Variant *value, UA_NodeId *createdNodeId) {
  908. UA_AddNodesRequest adReq;
  909. UA_StatusCode retval;
  910. UA_AddNodesRequest_init(&adReq);
  911. UA_AddNodesResponse *adRes;
  912. adRes = UA_AddNodesResponse_new();
  913. UA_AddNodesResponse_init(adRes);
  914. UA_VariableAttributes vAtt;
  915. UA_VariableAttributes_init(&vAtt);
  916. adReq.nodesToAdd = (UA_AddNodesItem *) UA_AddNodesItem_new();
  917. UA_AddNodesItem_init(adReq.nodesToAdd);
  918. UA_ExpandedNodeId typeDefinition;
  919. UA_ExpandedNodeId_init(&typeDefinition);
  920. if (value != UA_NULL)
  921. UA_NodeId_copy(&value->type->typeId, &typeDefinition.nodeId);
  922. // Default node properties and attributes
  923. ADDNODES_COPYDEFAULTATTRIBUTES(adReq, vAtt);
  924. // Specific to variables
  925. adReq.nodesToAdd[0].nodeClass = UA_NODECLASS_VARIABLE;
  926. vAtt.accessLevel = 0;
  927. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_ACCESSLEVEL;
  928. vAtt.userAccessLevel = 0;
  929. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_USERACCESSLEVEL;
  930. vAtt.minimumSamplingInterval = 100;
  931. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_MINIMUMSAMPLINGINTERVAL;
  932. vAtt.historizing = UA_FALSE;
  933. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_HISTORIZING;
  934. if (value != NULL) {
  935. UA_Variant_copy(value, &(vAtt.value));
  936. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_VALUE;
  937. vAtt.valueRank = -2;
  938. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_VALUERANK;
  939. // These are defined by the variant
  940. //vAtt.arrayDimensionsSize = value->arrayDimensionsSize;
  941. //vAtt.arrayDimensions = NULL;
  942. }
  943. UA_NodeId_copy(&value->type->typeId, &(vAtt.dataType));
  944. ADDNODES_PACK_AND_SEND(adReq,vAtt,VARIABLE);
  945. if(adRes->responseHeader.serviceResult != UA_STATUSCODE_GOOD || adRes->resultsSize == 0)
  946. retval = adRes->responseHeader.serviceResult;
  947. retval = adRes->results[0].statusCode;
  948. if(createdNodeId != NULL)
  949. UA_NodeId_copy(&adRes->results[0].addedNodeId, createdNodeId);
  950. UA_AddNodesResponse_deleteMembers(adRes);
  951. return retval;
  952. }
  953. UA_StatusCode UA_Client_addReferenceTypeNode( UA_Client *client, UA_NodeId reqId, UA_QualifiedName browseName, UA_LocalizedText displayName,
  954. UA_LocalizedText description, UA_ExpandedNodeId parentNodeId, UA_NodeId referenceTypeId,
  955. UA_UInt32 userWriteMask, UA_UInt32 writeMask, UA_ExpandedNodeId typeDefinition,
  956. UA_LocalizedText inverseName, UA_NodeId *createdNodeId ) {
  957. UA_AddNodesRequest adReq;
  958. UA_StatusCode retval;
  959. UA_AddNodesRequest_init(&adReq);
  960. UA_AddNodesResponse *adRes;
  961. adRes = UA_AddNodesResponse_new();
  962. UA_AddNodesResponse_init(adRes);
  963. UA_ReferenceTypeAttributes vAtt;
  964. UA_ReferenceTypeAttributes_init(&vAtt);
  965. adReq.nodesToAdd = (UA_AddNodesItem *) UA_AddNodesItem_new();
  966. UA_AddNodesItem_init(adReq.nodesToAdd);
  967. // Default node properties and attributes
  968. ADDNODES_COPYDEFAULTATTRIBUTES(adReq, vAtt);
  969. // Specific to referencetypes
  970. adReq.nodesToAdd[0].nodeClass = UA_NODECLASS_REFERENCETYPE;
  971. UA_LocalizedText_copy(&inverseName, &(vAtt.inverseName));
  972. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_INVERSENAME;
  973. vAtt.symmetric = UA_FALSE;
  974. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_SYMMETRIC;
  975. vAtt.isAbstract = UA_FALSE;
  976. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_ISABSTRACT;
  977. ADDNODES_PACK_AND_SEND(adReq,vAtt,REFERENCETYPE);
  978. if(adRes->responseHeader.serviceResult != UA_STATUSCODE_GOOD || adRes->resultsSize == 0)
  979. retval = adRes->responseHeader.serviceResult;
  980. retval = adRes->results[0].statusCode;
  981. if(createdNodeId != NULL)
  982. UA_NodeId_copy(&adRes->results[0].addedNodeId, createdNodeId);
  983. UA_AddNodesResponse_deleteMembers(adRes);
  984. return retval;
  985. }
  986. UA_StatusCode UA_Client_addObjectTypeNode(UA_Client *client, UA_NodeId reqId, UA_QualifiedName browseName, UA_LocalizedText displayName,
  987. UA_LocalizedText description, UA_ExpandedNodeId parentNodeId, UA_NodeId referenceTypeId,
  988. UA_UInt32 userWriteMask, UA_UInt32 writeMask, UA_ExpandedNodeId typeDefinition, UA_Boolean isAbstract,
  989. UA_NodeId *createdNodeId) {
  990. UA_AddNodesRequest adReq;
  991. UA_StatusCode retval;
  992. UA_AddNodesRequest_init(&adReq);
  993. UA_AddNodesResponse *adRes;
  994. adRes = UA_AddNodesResponse_new();
  995. UA_AddNodesResponse_init(adRes);
  996. UA_ObjectTypeAttributes vAtt;
  997. UA_ObjectTypeAttributes_init(&vAtt);
  998. adReq.nodesToAdd = (UA_AddNodesItem *) UA_AddNodesItem_new();
  999. UA_AddNodesItem_init(adReq.nodesToAdd);
  1000. // Default node properties and attributes
  1001. ADDNODES_COPYDEFAULTATTRIBUTES(adReq, vAtt);
  1002. // Specific to referencetypes
  1003. adReq.nodesToAdd[0].nodeClass = UA_NODECLASS_OBJECTTYPE;
  1004. vAtt.isAbstract = isAbstract;
  1005. vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_ISABSTRACT;
  1006. ADDNODES_PACK_AND_SEND(adReq,vAtt,OBJECTTYPE);
  1007. if(adRes->responseHeader.serviceResult != UA_STATUSCODE_GOOD || adRes->resultsSize == 0)
  1008. retval = adRes->responseHeader.serviceResult;
  1009. retval = adRes->results[0].statusCode;
  1010. if(createdNodeId != NULL)
  1011. UA_NodeId_copy(&adRes->results[0].addedNodeId, createdNodeId);
  1012. UA_AddNodesResponse_deleteMembers(adRes);
  1013. return retval;
  1014. }
  1015. UA_StatusCode
  1016. UA_Client_forEachChildNodeCall(UA_Client *client, UA_NodeId parentNodeId, UA_NodeIteratorCallback callback) {
  1017. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1018. UA_BrowseRequest brq;
  1019. UA_BrowseRequest_init(&brq);
  1020. UA_BrowseResponse brs;
  1021. UA_BrowseResponse_init(&brs);
  1022. brq.nodesToBrowseSize = 1;
  1023. brq.requestedMaxReferencesPerNode = 0;
  1024. brq.nodesToBrowse = UA_BrowseDescription_new();
  1025. brq.nodesToBrowse[0].browseDirection = UA_BROWSEDIRECTION_BOTH;
  1026. brq.nodesToBrowse[0].includeSubtypes = UA_TRUE;
  1027. UA_NodeId_copy(&parentNodeId, &brq.nodesToBrowse[0].nodeId);
  1028. brq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL;
  1029. brs = UA_Client_browse(client, &brq);
  1030. UA_BrowseRequest_deleteMembers(&brq);
  1031. if (brs.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  1032. UA_BrowseResponse_deleteMembers(&brq);
  1033. return brs.responseHeader.serviceResult;
  1034. }
  1035. if (brs.resultsSize < 1) {
  1036. UA_BrowseResponse_deleteMembers(&brq);
  1037. return UA_STATUSCODE_GOOD;
  1038. }
  1039. if (brs.results[0].statusCode != UA_STATUSCODE_GOOD) {
  1040. UA_BrowseResponse_deleteMembers(&brq);
  1041. return brs.results[0].statusCode;
  1042. }
  1043. UA_Boolean isInverse;
  1044. UA_NodeId *childId = UA_NodeId_new();
  1045. UA_NodeId *refTypeId = UA_NodeId_new();
  1046. for (int i = 0; i < brs.results[0].referencesSize; i++) {
  1047. isInverse = UA_FALSE;
  1048. if (brs.results[0].references[i].isForward == UA_FALSE)
  1049. isInverse = UA_TRUE;
  1050. UA_NodeId_copy(&brs.results[0].references[i].nodeId.nodeId, childId);
  1051. UA_NodeId_copy(&brs.results[0].references[i].referenceTypeId, refTypeId);
  1052. //UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId
  1053. callback(*childId, isInverse, *refTypeId);
  1054. UA_NodeId_deleteMembers(childId);
  1055. UA_NodeId_deleteMembers(refTypeId);
  1056. }
  1057. UA_NodeId_delete(childId);
  1058. UA_NodeId_delete(refTypeId);
  1059. UA_BrowseResponse_deleteMembers(&brs);
  1060. return retval;
  1061. }
  1062. #include "server/ua_nodes.h"
  1063. UA_StatusCode
  1064. UA_Client_copyBaseAttributes(UA_Client *client, UA_ReadResponse *readResponseSrc, void *dst) {
  1065. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1066. UA_Node *target = (UA_Node *) dst;
  1067. if (readResponseSrc->results[0].value.data != UA_NULL)
  1068. retval |= UA_NodeId_copy((UA_NodeId *) readResponseSrc->results[0].value.data, &target->nodeId);
  1069. if (readResponseSrc->results[1].value.data != UA_NULL)
  1070. retval |= UA_NodeClass_copy((UA_NodeClass *) readResponseSrc->results[1].value.data, &target->nodeClass);
  1071. if (readResponseSrc->results[2].value.data != UA_NULL)
  1072. retval |= UA_QualifiedName_copy((UA_QualifiedName *) readResponseSrc->results[2].value.data, &target->browseName);
  1073. if (readResponseSrc->results[3].value.data != UA_NULL)
  1074. retval |= UA_LocalizedText_copy((UA_LocalizedText *) readResponseSrc->results[3].value.data, &target->displayName);
  1075. if (readResponseSrc->results[4].value.data != UA_NULL)
  1076. retval |= UA_LocalizedText_copy((UA_LocalizedText *) readResponseSrc->results[4].value.data, &target->description);
  1077. if (readResponseSrc->results[5].value.data != UA_NULL)
  1078. retval |= UA_UInt32_copy((UA_UInt32 *) readResponseSrc->results[5].value.data, &target->writeMask);
  1079. if (readResponseSrc->results[6].value.data != UA_NULL)
  1080. retval |= UA_UInt32_copy((UA_UInt32 *) readResponseSrc->results[6].value.data, &target->userWriteMask);
  1081. target->referencesSize = 0;
  1082. target->references = UA_NULL;
  1083. UA_BrowseRequest *brq = UA_BrowseRequest_new();
  1084. UA_BrowseResponse brs;
  1085. UA_BrowseResult_init(&brs);
  1086. brq->nodesToBrowseSize = 1;
  1087. brq->requestedMaxReferencesPerNode = 0;
  1088. brq->nodesToBrowse = UA_BrowseDescription_new();
  1089. brq->nodesToBrowse[0].browseDirection = UA_BROWSEDIRECTION_BOTH;
  1090. brq->nodesToBrowse[0].includeSubtypes = UA_TRUE;
  1091. UA_NodeId_copy((UA_NodeId *) readResponseSrc->results[0].value.data, &brq->nodesToBrowse[0].nodeId);
  1092. brq->nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL;
  1093. brs = UA_Client_browse(client, brq);
  1094. UA_BrowseRequest_delete(brq);
  1095. if (brs.responseHeader.serviceResult!= UA_STATUSCODE_GOOD || brs.resultsSize != 1) {
  1096. UA_BrowseResponse_deleteMembers(&brs);
  1097. return retval;
  1098. }
  1099. if (brs.results[0].statusCode != UA_STATUSCODE_GOOD) {
  1100. UA_BrowseResponse_deleteMembers(&brs);
  1101. return retval;
  1102. }
  1103. /* typedef struct {
  1104. U A_NodeId referenceTypeId; *
  1105. UA_Boolean isInverse;
  1106. UA_ExpandedNodeId targetId; */
  1107. target->referencesSize = brs.results[0].referencesSize;
  1108. target->references = (UA_ReferenceNode *) UA_malloc(sizeof(UA_ReferenceNode) * brs.results[0].referencesSize);
  1109. for (int i=0; i<brs.results[0].referencesSize; i++) {
  1110. target->references[i].isInverse = UA_FALSE;
  1111. if (brs.results[0].references->isForward == UA_FALSE)
  1112. target->references[i].isInverse = UA_TRUE;
  1113. UA_NodeId_copy(&brs.results[0].references->nodeId.nodeId, &target->references[i].referenceTypeId);
  1114. UA_ExpandedNodeId_init(&target->references[i].targetId);
  1115. UA_NodeId_copy(&brs.results[0].references->referenceTypeId, &target->references[i].targetId.nodeId);
  1116. }
  1117. UA_BrowseResponse_deleteMembers(&brs);
  1118. return retval;
  1119. }
  1120. UA_StatusCode
  1121. UA_Client_appendObjectNodeAttributes(UA_Client *client, void *dst) {
  1122. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1123. UA_ObjectNode *target = (UA_ObjectNode *) dst;
  1124. UA_ReadRequest *rrq = UA_ReadRequest_new();
  1125. UA_ReadResponse rrs;
  1126. /* Read node attributes:
  1127. UA_ATTRIBUTEID_EVENTNOTIFIER = 12
  1128. */
  1129. rrq->nodesToReadSize = 1;
  1130. rrq->nodesToRead = (UA_ReadValueId*) UA_malloc(sizeof(UA_ReadValueId) * rrq->nodesToReadSize);
  1131. for(int i = 0; i < rrq->nodesToReadSize; i++) {
  1132. UA_ReadValueId_init(&rrq->nodesToRead[i]);
  1133. rrq->nodesToRead[i].attributeId = (UA_UInt32 ) i+12;
  1134. UA_NodeId_copy(&target->nodeId, &rrq->nodesToRead[i].nodeId);
  1135. }
  1136. rrs = UA_Client_read(client, rrq);
  1137. UA_ReadRequest_delete(rrq);
  1138. if (rrs.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  1139. return rrs.responseHeader.serviceResult;
  1140. if (rrs.resultsSize != rrq->nodesToReadSize)
  1141. return rrs.responseHeader.serviceResult;
  1142. if (rrs.results[0].value.data != NULL)
  1143. UA_Byte_copy((UA_Byte *) rrs.results[0].value.data, &target->eventNotifier);
  1144. UA_ReadResponse_deleteMembers(&rrs);
  1145. return retval;
  1146. }
  1147. UA_StatusCode
  1148. UA_Client_appendObjectTypeNodeAttributes(UA_Client *client, void *dst) {
  1149. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1150. UA_ObjectTypeNode *target = (UA_ObjectTypeNode *) dst;
  1151. UA_ReadRequest *rrq = UA_ReadRequest_new();
  1152. UA_ReadResponse rrs;
  1153. /* Read node attributes:
  1154. * UA_ATTRIBUTEID_ISABSTRACT = 8,
  1155. */
  1156. rrq->nodesToReadSize = 1;
  1157. rrq->nodesToRead = (UA_ReadValueId*) UA_malloc(sizeof(UA_ReadValueId) * rrq->nodesToReadSize);
  1158. for(int i = 0; i < rrq->nodesToReadSize; i++) {
  1159. UA_ReadValueId_init(&rrq->nodesToRead[i]);
  1160. rrq->nodesToRead[i].attributeId = (UA_UInt32 ) i+8;
  1161. UA_NodeId_copy(&target->nodeId, &rrq->nodesToRead[i].nodeId);
  1162. }
  1163. rrs = UA_Client_read(client, rrq);
  1164. UA_ReadRequest_delete(rrq);
  1165. if (rrs.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  1166. return rrs.responseHeader.serviceResult;
  1167. if (rrs.resultsSize != rrq->nodesToReadSize)
  1168. return rrs.responseHeader.serviceResult;
  1169. if (rrs.results[0].value.data != NULL)
  1170. UA_Boolean_copy((UA_Boolean *) rrs.results[0].value.data, &target->isAbstract);
  1171. UA_ReadResponse_deleteMembers(&rrs);
  1172. return retval;
  1173. }
  1174. UA_StatusCode
  1175. UA_Client_appendVariableNodeAttributes(UA_Client *client, void *dst) {
  1176. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1177. UA_VariableNode *target = (UA_VariableNode *) dst;
  1178. UA_ReadRequest *rrq = UA_ReadRequest_new();
  1179. UA_ReadResponse rrs;
  1180. /* Read node attributes:
  1181. UA_ATTRIBUTEID_VALUE = 13,
  1182. UA_ATTRIBUTEID_DATATYPE = 14, // Req. but not used (is in variant)
  1183. UA_ATTRIBUTEID_ARRAYDIMENSIONS = 16, // Req. but not used (is in variant)
  1184. UA_ATTRIBUTEID_VALUERANK = 15,
  1185. UA_ATTRIBUTEID_ACCESSLEVEL = 17,
  1186. UA_ATTRIBUTEID_USERACCESSLEVEL = 18,
  1187. UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL = 19,
  1188. UA_ATTRIBUTEID_HISTORIZING = 20,
  1189. */
  1190. rrq->nodesToReadSize = 8;
  1191. rrq->nodesToRead = (UA_ReadValueId*) UA_malloc(sizeof(UA_ReadValueId) * rrq->nodesToReadSize);
  1192. for(int i = 0; i < rrq->nodesToReadSize; i++) {
  1193. UA_ReadValueId_init(&rrq->nodesToRead[i]);
  1194. rrq->nodesToRead[i].attributeId = (UA_UInt32 ) i+13;
  1195. UA_NodeId_copy(&target->nodeId, &rrq->nodesToRead[i].nodeId);
  1196. }
  1197. rrs = UA_Client_read(client, rrq);
  1198. UA_ReadRequest_delete(rrq);
  1199. if (rrs.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  1200. return rrs.responseHeader.serviceResult;
  1201. if (rrs.resultsSize != rrq->nodesToReadSize)
  1202. return rrs.responseHeader.serviceResult;
  1203. if (rrs.results[0].value.data != NULL)
  1204. UA_Variant_copy((UA_Variant *) &rrs.results[0].value, &target->value.variant);
  1205. if (rrs.results[3].value.data != NULL)
  1206. UA_Int32_copy((UA_Int32 *) rrs.results[3].value.data, &target->valueRank);
  1207. if (rrs.results[4].value.data != NULL)
  1208. UA_Byte_copy((UA_Byte *) rrs.results[4].value.data, &target->accessLevel);
  1209. if (rrs.results[5].value.data != NULL)
  1210. UA_Byte_copy((UA_Byte *) rrs.results[5].value.data, &target->userAccessLevel);
  1211. if (rrs.results[6].value.data != NULL)
  1212. UA_Double_copy((UA_Double *) rrs.results[6].value.data, &target->minimumSamplingInterval);
  1213. if (rrs.results[7].value.data != NULL)
  1214. UA_Boolean_copy((UA_Boolean *) rrs.results[7].value.data, &target->historizing);
  1215. UA_ReadResponse_deleteMembers(&rrs);
  1216. return retval;
  1217. }
  1218. UA_StatusCode
  1219. UA_Client_appendVariableTypeNodeAttributes(UA_Client *client, void *dst) {
  1220. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1221. UA_VariableTypeNode *target = (UA_VariableTypeNode *) dst;
  1222. UA_ReadRequest *rrq = UA_ReadRequest_new();
  1223. UA_ReadResponse rrs;
  1224. /* Read node attributes:
  1225. UA_ATTRIBUTEID_ISABSTRACT = 8,
  1226. UA_ATTRIBUTEID_VALUE = 13,
  1227. UA_ATTRIBUTEID_DATATYPE = 14, // Req. but not used (is in variant)
  1228. UA_ATTRIBUTEID_ARRAYDIMENSIONS = 16, // Req. but not used (is in variant)
  1229. UA_ATTRIBUTEID_VALUERANK = 15,
  1230. */
  1231. rrq->nodesToReadSize = 8;
  1232. rrq->nodesToRead = (UA_ReadValueId*) UA_malloc(sizeof(UA_ReadValueId) * rrq->nodesToReadSize);
  1233. for(int i = 0; i < rrq->nodesToReadSize; i++) {
  1234. UA_ReadValueId_init(&rrq->nodesToRead[i]);
  1235. rrq->nodesToRead[i].attributeId = (UA_UInt32 ) i+13;
  1236. UA_NodeId_copy(&target->nodeId, &rrq->nodesToRead[i].nodeId);
  1237. }
  1238. rrq->nodesToRead[0].attributeId = UA_ATTRIBUTEID_ISABSTRACT;
  1239. rrs = UA_Client_read(client, rrq);
  1240. UA_ReadRequest_delete(rrq);
  1241. if (rrs.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  1242. return rrs.responseHeader.serviceResult;
  1243. if (rrs.resultsSize != rrq->nodesToReadSize)
  1244. return rrs.responseHeader.serviceResult;
  1245. if (rrs.results[0].value.data != NULL)
  1246. UA_Boolean_copy((UA_Boolean *) rrs.results[0].value.data, &target->isAbstract);
  1247. if (rrs.results[1].value.data != NULL)
  1248. UA_Variant_copy((UA_Variant *) &rrs.results[1].value, &target->value.variant);
  1249. if (rrs.results[2].value.data != NULL)
  1250. UA_Int32_copy((UA_Int32 *) rrs.results[2].value.data, &target->valueRank);
  1251. UA_ReadResponse_deleteMembers(&rrs);
  1252. return retval;
  1253. }
  1254. UA_StatusCode
  1255. UA_Client_appendReferenceTypeNodeAttributes(UA_Client *client, void *dst) {
  1256. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1257. UA_ReferenceTypeNode *target = (UA_ReferenceTypeNode *) dst;
  1258. UA_ReadRequest *rrq = UA_ReadRequest_new();
  1259. UA_ReadResponse rrs;
  1260. /* Read node attributes:
  1261. UA_ATTRIBUTEID_SYMMETRIC = 9,
  1262. UA_ATTRIBUTEID_INVERSENAME = 10,
  1263. * UA_ATTRIBUTEID_ISABSTRACT = 8,
  1264. */
  1265. rrq->nodesToReadSize = 3;
  1266. rrq->nodesToRead = (UA_ReadValueId*) UA_malloc(sizeof(UA_ReadValueId) * rrq->nodesToReadSize);
  1267. for(int i = 0; i < rrq->nodesToReadSize; i++) {
  1268. UA_ReadValueId_init(&rrq->nodesToRead[i]);
  1269. rrq->nodesToRead[i].attributeId = (UA_UInt32 ) i+8;
  1270. UA_NodeId_copy(&target->nodeId, &rrq->nodesToRead[i].nodeId);
  1271. }
  1272. rrs = UA_Client_read(client, rrq);
  1273. UA_ReadRequest_delete(rrq);
  1274. if (rrs.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  1275. return rrs.responseHeader.serviceResult;
  1276. if (rrs.resultsSize != rrq->nodesToReadSize)
  1277. return rrs.responseHeader.serviceResult;
  1278. if (rrs.results[0].value.data != NULL)
  1279. UA_Boolean_copy((UA_Boolean *) rrs.results[0].value.data, &target->isAbstract);
  1280. if (rrs.results[1].value.data != NULL)
  1281. UA_Boolean_copy((UA_Boolean *) rrs.results[1].value.data, &target->symmetric);
  1282. if (rrs.results[2].value.data != NULL)
  1283. UA_LocalizedText_copy((UA_LocalizedText *) rrs.results[2].value.data, &target->inverseName);
  1284. UA_ReadResponse_deleteMembers(&rrs);
  1285. return retval;
  1286. return retval;
  1287. }
  1288. UA_StatusCode
  1289. UA_Client_appendViewNodeAttributes(UA_Client *client, void *dst) {
  1290. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1291. UA_ViewNode *target = (UA_ViewNode *) dst;
  1292. UA_ReadRequest *rrq = UA_ReadRequest_new();
  1293. UA_ReadResponse rrs;
  1294. /* Read node attributes:
  1295. UA_ATTRIBUTEID_CONTAINSNOLOOPS = 11,
  1296. UA_ATTRIBUTEID_EVENTNOTIFIER = 12,
  1297. */
  1298. rrq->nodesToReadSize = 2;
  1299. rrq->nodesToRead = (UA_ReadValueId*) UA_malloc(sizeof(UA_ReadValueId) * rrq->nodesToReadSize);
  1300. for(int i = 0; i < rrq->nodesToReadSize; i++) {
  1301. UA_ReadValueId_init(&rrq->nodesToRead[i]);
  1302. rrq->nodesToRead[i].attributeId = (UA_UInt32 ) i+11;
  1303. UA_NodeId_copy(&target->nodeId, &rrq->nodesToRead[i].nodeId);
  1304. }
  1305. rrs = UA_Client_read(client, rrq);
  1306. UA_ReadRequest_delete(rrq);
  1307. if (rrs.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  1308. return rrs.responseHeader.serviceResult;
  1309. if (rrs.resultsSize != rrq->nodesToReadSize)
  1310. return rrs.responseHeader.serviceResult;
  1311. if (rrs.results[0].value.data != NULL)
  1312. UA_Boolean_copy((UA_Boolean *) rrs.results[0].value.data, &target->containsNoLoops);
  1313. if (rrs.results[1].value.data != NULL)
  1314. UA_Byte_copy((UA_Byte *) rrs.results[1].value.data, &target->eventNotifier);
  1315. UA_ReadResponse_deleteMembers(&rrs);
  1316. return retval;
  1317. return retval;
  1318. }
  1319. UA_StatusCode
  1320. UA_Client_appendDataTypeNodeAttributes(UA_Client *client, void *dst) {
  1321. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1322. UA_DataTypeNode *target = (UA_DataTypeNode *) dst;
  1323. UA_ReadRequest *rrq = UA_ReadRequest_new();
  1324. UA_ReadResponse rrs;
  1325. /* Read node attributes:
  1326. * UA_ATTRIBUTEID_ISABSTRACT = 8,
  1327. */
  1328. rrq->nodesToReadSize = 1;
  1329. rrq->nodesToRead = (UA_ReadValueId*) UA_malloc(sizeof(UA_ReadValueId) * rrq->nodesToReadSize);
  1330. for(int i = 0; i < rrq->nodesToReadSize; i++) {
  1331. UA_ReadValueId_init(&rrq->nodesToRead[i]);
  1332. rrq->nodesToRead[i].attributeId = (UA_UInt32 ) i+8;
  1333. UA_NodeId_copy(&target->nodeId, &rrq->nodesToRead[i].nodeId);
  1334. }
  1335. rrs = UA_Client_read(client, rrq);
  1336. UA_ReadRequest_delete(rrq);
  1337. if (rrs.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  1338. return rrs.responseHeader.serviceResult;
  1339. if (rrs.resultsSize != rrq->nodesToReadSize)
  1340. return rrs.responseHeader.serviceResult;
  1341. if (rrs.results[0].value.data != NULL)
  1342. UA_Boolean_copy((UA_Boolean *) rrs.results[0].value.data, &target->isAbstract);
  1343. UA_ReadResponse_deleteMembers(&rrs);
  1344. return retval;
  1345. }
  1346. UA_StatusCode
  1347. UA_Client_getNodeCopy(UA_Client *client, UA_NodeId nodeId, void **copyInto) {
  1348. UA_ReadRequest *rrq = UA_ReadRequest_new();
  1349. UA_ReadResponse rrs;
  1350. *copyInto = UA_NULL;
  1351. /* Read default node attributes:
  1352. UA_ATTRIBUTEID_NODEID = 1,
  1353. UA_ATTRIBUTEID_NODECLASS = 2,
  1354. UA_ATTRIBUTEID_BROWSENAME = 3,
  1355. UA_ATTRIBUTEID_DISPLAYNAME = 4,
  1356. UA_ATTRIBUTEID_DESCRIPTION = 5,
  1357. UA_ATTRIBUTEID_WRITEMASK = 6,
  1358. UA_ATTRIBUTEID_USERWRITEMASK = 7,
  1359. */
  1360. rrq->nodesToReadSize = 7;
  1361. rrq->nodesToRead = (UA_ReadValueId*) UA_malloc(sizeof(UA_ReadValueId) * rrq->nodesToReadSize);
  1362. for(int i = 0; i < rrq->nodesToReadSize; i++) {
  1363. UA_ReadValueId_init(&rrq->nodesToRead[i]);
  1364. rrq->nodesToRead[i].attributeId = (UA_UInt32 ) i+1;
  1365. UA_NodeId_copy(&nodeId, &rrq->nodesToRead[i].nodeId);
  1366. }
  1367. rrs = UA_Client_read(client, rrq);
  1368. if (rrs.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  1369. return rrs.responseHeader.serviceResult;
  1370. if (rrs.resultsSize != rrq->nodesToReadSize)
  1371. return rrs.responseHeader.serviceResult;
  1372. UA_ReadRequest_delete(rrq);
  1373. UA_UInt32 *nodeClass = rrs.results[1].value.data;
  1374. switch(*nodeClass) {
  1375. case UA_NODECLASS_OBJECT:
  1376. *copyInto = UA_ObjectNode_new();
  1377. UA_Client_copyBaseAttributes(client, &rrs, *copyInto);
  1378. break;
  1379. case UA_NODECLASS_OBJECTTYPE:
  1380. *copyInto = UA_ObjectTypeNode_new();
  1381. UA_Client_copyBaseAttributes(client, &rrs, *copyInto);
  1382. break;
  1383. case UA_NODECLASS_VARIABLE:
  1384. *copyInto = UA_VariableNode_new();
  1385. UA_Client_copyBaseAttributes(client, &rrs, *copyInto);
  1386. UA_Client_appendVariableNodeAttributes(client, *copyInto);
  1387. break;
  1388. case UA_NODECLASS_VARIABLETYPE:
  1389. *copyInto = UA_VariableTypeNode_new();
  1390. UA_Client_copyBaseAttributes(client, &rrs, *copyInto);
  1391. break;
  1392. case UA_NODECLASS_REFERENCETYPE:
  1393. *copyInto = UA_ReferenceTypeNode_new();
  1394. UA_Client_copyBaseAttributes(client, &rrs, *copyInto);
  1395. break;
  1396. case UA_NODECLASS_METHOD:
  1397. *copyInto = UA_MethodNode_new();
  1398. UA_Client_copyBaseAttributes(client, &rrs, *copyInto);
  1399. break;
  1400. case UA_NODECLASS_VIEW:
  1401. *copyInto = UA_ViewNode_new();
  1402. UA_Client_copyBaseAttributes(client, &rrs, *copyInto);
  1403. break;
  1404. case UA_NODECLASS_DATATYPE:
  1405. *copyInto = UA_DataTypeNode_new();
  1406. UA_Client_copyBaseAttributes(client, &rrs, *copyInto);
  1407. break;
  1408. default:
  1409. UA_ReadRequest_delete(rrq);
  1410. UA_ReadResponse_deleteMembers(&rrs);
  1411. return UA_STATUSCODE_BADNODECLASSINVALID;
  1412. }
  1413. /*
  1414. UA_ATTRIBUTEID_ISABSTRACT = 8,
  1415. UA_ATTRIBUTEID_SYMMETRIC = 9,
  1416. UA_ATTRIBUTEID_INVERSENAME = 10,
  1417. UA_ATTRIBUTEID_CONTAINSNOLOOPS = 11,
  1418. UA_ATTRIBUTEID_EVENTNOTIFIER = 12,
  1419. UA_ATTRIBUTEID_EXECUTABLE = 21,
  1420. UA_ATTRIBUTEID_USEREXECUTABLE = 22
  1421. */
  1422. UA_ReadResponse_deleteMembers(&rrs);
  1423. return 0;
  1424. }