12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241 |
- #include "ua_util.h"
- #include "ua_client.h"
- #include "ua_types_generated.h"
- #include "ua_nodeids.h"
- #include "ua_securechannel.h"
- #include "ua_types_encoding_binary.h"
- #include "ua_transport_generated.h"
- #include "ua_types_generated_encoding_binary.h"
- #include "ua_transport_generated_encoding_binary.h"
- #include "ua_client_internal.h"
- typedef enum {
- UA_CLIENTSTATE_READY,
- UA_CLIENTSTATE_CONNECTED,
- UA_CLIENTSTATE_ERRORED
- } UA_Client_State;
- struct UA_Client {
- /* State */ //maybe it should be visible to user
- UA_Client_State state;
- /* Connection */
- UA_Connection connection;
- UA_SecureChannel channel;
- UA_String endpointUrl;
- UA_UInt32 requestId;
- /* Session */
- UA_UserTokenPolicy token;
- UA_NodeId sessionId;
- UA_NodeId authenticationToken;
-
- #ifdef ENABLE_SUBSCRIPTIONS
- UA_Int32 monitoredItemHandles;
- LIST_HEAD(UA_ListOfUnacknowledgedNotificationNumbers, UA_Client_NotificationsAckNumber_s) pendingNotificationsAcks;
- LIST_HEAD(UA_ListOfClientSubscriptionItems, UA_Client_Subscription_s) subscriptions;
- #endif
-
- /* Config */
- UA_Logger logger;
- UA_ClientConfig config;
- UA_DateTime scExpiresAt;
- };
- const UA_EXPORT UA_ClientConfig UA_ClientConfig_standard =
- { .timeout = 5 /* ms receive timout */, .secureChannelLifeTime = 30000, .timeToRenewSecureChannel = 2000,
- {.protocolVersion = 0, .sendBufferSize = 65536, .recvBufferSize = 65536,
- .maxMessageSize = 65536, .maxChunkCount = 1}};
- UA_Client * UA_Client_new(UA_ClientConfig config, UA_Logger logger) {
- UA_Client *client = UA_calloc(1, sizeof(UA_Client));
- if(!client)
- return UA_NULL;
- UA_Client_init(client, config, logger);
- return client;
- }
- void UA_Client_reset(UA_Client* client){
- UA_Client_deleteMembers(client);
- UA_Client_init(client, client->config, client->logger);
- }
- void UA_Client_init(UA_Client* client, UA_ClientConfig config, UA_Logger logger){
- client->state = UA_CLIENTSTATE_READY;
- UA_Connection_init(&client->connection);
- UA_SecureChannel_init(&client->channel);
- client->channel.connection = &client->connection;
- UA_String_init(&client->endpointUrl);
- client->requestId = 0;
- UA_NodeId_init(&client->authenticationToken);
- client->logger = logger;
- client->config = config;
- client->scExpiresAt = 0;
- #ifdef ENABLE_SUBSCRIPTIONS
- client->monitoredItemHandles = 0;
- LIST_INIT(&client->pendingNotificationsAcks);
- LIST_INIT(&client->subscriptions);
- #endif
- }
- void UA_Client_deleteMembers(UA_Client* client){
- if(client->state == UA_CLIENTSTATE_READY) //initialized client has no dynamic memory allocated
- return;
- UA_Connection_deleteMembers(&client->connection);
- UA_SecureChannel_deleteMembersCleanup(&client->channel);
- if(client->endpointUrl.data)
- UA_String_deleteMembers(&client->endpointUrl);
- UA_UserTokenPolicy_deleteMembers(&client->token);
- }
- void UA_Client_delete(UA_Client* client){
- if(client->state != UA_CLIENTSTATE_READY)
- UA_Client_deleteMembers(client);
- UA_free(client);
- }
- static UA_StatusCode HelAckHandshake(UA_Client *c) {
- UA_TcpMessageHeader messageHeader;
- messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_HELF;
- UA_TcpHelloMessage hello;
- UA_String_copy(&c->endpointUrl, &hello.endpointUrl); /* must be less than 4096 bytes */
- UA_Connection *conn = &c->connection;
- hello.maxChunkCount = conn->localConf.maxChunkCount;
- hello.maxMessageSize = conn->localConf.maxMessageSize;
- hello.protocolVersion = conn->localConf.protocolVersion;
- hello.receiveBufferSize = conn->localConf.recvBufferSize;
- hello.sendBufferSize = conn->localConf.sendBufferSize;
- UA_ByteString message;
- UA_StatusCode retval = c->connection.getSendBuffer(&c->connection, c->connection.remoteConf.recvBufferSize, &message);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- size_t offset = 8;
- retval |= UA_TcpHelloMessage_encodeBinary(&hello, &message, &offset);
- messageHeader.messageSize = offset;
- offset = 0;
- retval |= UA_TcpMessageHeader_encodeBinary(&messageHeader, &message, &offset);
- UA_TcpHelloMessage_deleteMembers(&hello);
- if(retval != UA_STATUSCODE_GOOD) {
- c->connection.releaseSendBuffer(&c->connection, &message);
- return retval;
- }
- message.length = messageHeader.messageSize;
- retval = c->connection.send(&c->connection, &message);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- UA_ByteString reply;
- UA_ByteString_init(&reply);
- do {
- retval = c->connection.recv(&c->connection, &reply, c->config.timeout);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- } while(!reply.data);
- offset = 0;
- UA_TcpMessageHeader_decodeBinary(&reply, &offset, &messageHeader);
- UA_TcpAcknowledgeMessage ackMessage;
- retval = UA_TcpAcknowledgeMessage_decodeBinary(&reply, &offset, &ackMessage);
- UA_ByteString_deleteMembers(&reply);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- conn->remoteConf.maxChunkCount = ackMessage.maxChunkCount;
- conn->remoteConf.maxMessageSize = ackMessage.maxMessageSize;
- conn->remoteConf.protocolVersion = ackMessage.protocolVersion;
- conn->remoteConf.recvBufferSize = ackMessage.receiveBufferSize;
- conn->remoteConf.sendBufferSize = ackMessage.sendBufferSize;
- conn->state = UA_CONNECTION_ESTABLISHED;
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode SecureChannelHandshake(UA_Client *client, UA_Boolean renew) {
- /* Check if sc is still valid */
- if(renew && client->scExpiresAt - UA_DateTime_now() > client->config.timeToRenewSecureChannel * 10000 ){
- return UA_STATUSCODE_GOOD;
- }
- UA_SecureConversationMessageHeader messageHeader;
- messageHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_OPNF;
- messageHeader.secureChannelId = 0;
- UA_SequenceHeader seqHeader;
- seqHeader.sequenceNumber = ++client->channel.sequenceNumber;
- seqHeader.requestId = ++client->requestId;
- UA_AsymmetricAlgorithmSecurityHeader asymHeader;
- UA_AsymmetricAlgorithmSecurityHeader_init(&asymHeader);
- asymHeader.securityPolicyUri = UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
- /* id of opensecurechannelrequest */
- UA_NodeId requestType = UA_NODEID_NUMERIC(0, UA_NS0ID_OPENSECURECHANNELREQUEST + UA_ENCODINGOFFSET_BINARY);
- UA_OpenSecureChannelRequest opnSecRq;
- UA_OpenSecureChannelRequest_init(&opnSecRq);
- opnSecRq.requestHeader.timestamp = UA_DateTime_now();
- opnSecRq.requestHeader.authenticationToken = client->authenticationToken;
- opnSecRq.requestedLifetime = client->config.secureChannelLifeTime;
- if(renew) {
- opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_RENEW;
- } else {
- opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_ISSUE;
- UA_SecureChannel_generateNonce(&client->channel.clientNonce);
- UA_ByteString_copy(&client->channel.clientNonce, &opnSecRq.clientNonce);
- opnSecRq.securityMode = UA_MESSAGESECURITYMODE_NONE;
- }
- UA_ByteString message;
- UA_Connection *c = &client->connection;
- UA_StatusCode retval = c->getSendBuffer(c, c->remoteConf.recvBufferSize, &message);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
- UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq);
- return retval;
- }
- size_t offset = 12;
- retval = UA_AsymmetricAlgorithmSecurityHeader_encodeBinary(&asymHeader, &message, &offset);
- retval |= UA_SequenceHeader_encodeBinary(&seqHeader, &message, &offset);
- retval |= UA_NodeId_encodeBinary(&requestType, &message, &offset);
- retval |= UA_OpenSecureChannelRequest_encodeBinary(&opnSecRq, &message, &offset);
- messageHeader.messageHeader.messageSize = offset;
- offset = 0;
- retval |= UA_SecureConversationMessageHeader_encodeBinary(&messageHeader, &message, &offset);
- UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
- UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq);
- if(retval != UA_STATUSCODE_GOOD) {
- client->connection.releaseSendBuffer(&client->connection, &message);
- return retval;
- }
- message.length = messageHeader.messageHeader.messageSize;
- retval = client->connection.send(&client->connection, &message);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- UA_ByteString reply;
- UA_ByteString_init(&reply);
- do {
- retval = client->connection.recv(&client->connection, &reply, client->config.timeout);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- } while(!reply.data);
- offset = 0;
- UA_SecureConversationMessageHeader_decodeBinary(&reply, &offset, &messageHeader);
- UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(&reply, &offset, &asymHeader);
- UA_SequenceHeader_decodeBinary(&reply, &offset, &seqHeader);
- UA_NodeId_decodeBinary(&reply, &offset, &requestType);
- UA_NodeId expectedRequest = UA_NODEID_NUMERIC(0, UA_NS0ID_OPENSECURECHANNELRESPONSE +
- UA_ENCODINGOFFSET_BINARY);
- if(!UA_NodeId_equal(&requestType, &expectedRequest)) {
- UA_ByteString_deleteMembers(&reply);
- UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
- UA_NodeId_deleteMembers(&requestType);
- UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT,
- "Reply answers the wrong request. Expected OpenSecureChannelResponse.");
- return UA_STATUSCODE_BADINTERNALERROR;
- }
- UA_OpenSecureChannelResponse response;
- UA_OpenSecureChannelResponse_decodeBinary(&reply, &offset, &response);
- client->scExpiresAt = UA_DateTime_now() + response.securityToken.revisedLifetime * 10000;
- UA_ByteString_deleteMembers(&reply);
- retval = response.responseHeader.serviceResult;
- if(!renew && retval == UA_STATUSCODE_GOOD) {
- UA_ChannelSecurityToken_copy(&response.securityToken, &client->channel.securityToken);
- UA_ByteString_deleteMembers(&client->channel.serverNonce); // if the handshake is repeated
- UA_ByteString_copy(&response.serverNonce, &client->channel.serverNonce);
- }
- UA_OpenSecureChannelResponse_deleteMembers(&response);
- UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
- return retval;
- }
- /** If the request fails, then the response is cast to UA_ResponseHeader (at the beginning of every
- response) and filled with the appropriate error code */
- static void synchronousRequest(UA_Client *client, void *request, const UA_DataType *requestType,
- void *response, const UA_DataType *responseType) {
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
- if(!response)
- return;
- UA_init(response, responseType);
- UA_ResponseHeader *respHeader = (UA_ResponseHeader*)response;
- //make sure we have a valid session
- retval = UA_Client_renewSecureChannel(client);
- if(retval != UA_STATUSCODE_GOOD) {
- respHeader->serviceResult = retval;
- client->state = UA_CLIENTSTATE_ERRORED;
- return;
- }
- /* Copy authenticationToken token to request header */
- typedef struct {
- UA_RequestHeader requestHeader;
- } headerOnlyRequest;
- /* The cast is valid, since all requests start with a requestHeader */
- UA_NodeId_copy(&client->authenticationToken, &((headerOnlyRequest*)request)->requestHeader.authenticationToken);
- /* Send the request */
- UA_UInt32 requestId = ++client->requestId;
- retval = UA_SecureChannel_sendBinaryMessage(&client->channel, requestId,
- request, requestType);
- if(retval) {
- if(retval == UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED)
- respHeader->serviceResult = UA_STATUSCODE_BADREQUESTTOOLARGE;
- else
- respHeader->serviceResult = retval;
- client->state = UA_CLIENTSTATE_ERRORED;
- return;
- }
- /* Retrieve the response */
- // Todo: push this into the generic securechannel implementation for client and server
- UA_ByteString reply;
- UA_ByteString_init(&reply);
- do {
- retval = client->connection.recv(&client->connection, &reply, client->config.timeout);
- if(retval != UA_STATUSCODE_GOOD) {
- respHeader->serviceResult = retval;
- client->state = UA_CLIENTSTATE_ERRORED;
- return;
- }
- } while(!reply.data);
- size_t offset = 0;
- UA_SecureConversationMessageHeader msgHeader;
- retval |= UA_SecureConversationMessageHeader_decodeBinary(&reply, &offset, &msgHeader);
- UA_SymmetricAlgorithmSecurityHeader symHeader;
- retval |= UA_SymmetricAlgorithmSecurityHeader_decodeBinary(&reply, &offset, &symHeader);
- UA_SequenceHeader seqHeader;
- retval |= UA_SequenceHeader_decodeBinary(&reply, &offset, &seqHeader);
- UA_NodeId responseId;
- retval |= UA_NodeId_decodeBinary(&reply, &offset, &responseId);
- UA_NodeId expectedNodeId = UA_NODEID_NUMERIC(0, responseType->typeId.identifier.numeric +
- UA_ENCODINGOFFSET_BINARY);
- if(retval != UA_STATUSCODE_GOOD)
- goto finish;
- /* Todo: we need to demux responses since a publish responses may come at any time */
- if(!UA_NodeId_equal(&responseId, &expectedNodeId) || seqHeader.requestId != requestId) {
- if(responseId.identifier.numeric != UA_NS0ID_SERVICEFAULT + UA_ENCODINGOFFSET_BINARY) {
- UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT,
- "Reply answers the wrong request. Expected ns=%i,i=%i. But retrieved ns=%i,i=%i",
- expectedNodeId.namespaceIndex, expectedNodeId.identifier.numeric,
- responseId.namespaceIndex, responseId.identifier.numeric);
- respHeader->serviceResult = UA_STATUSCODE_BADINTERNALERROR;
- } else
- retval = UA_decodeBinary(&reply, &offset, respHeader, &UA_TYPES[UA_TYPES_SERVICEFAULT]);
- goto finish;
- }
-
- retval = UA_decodeBinary(&reply, &offset, response, responseType);
- if(retval == UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED)
- retval = UA_STATUSCODE_BADRESPONSETOOLARGE;
- finish:
- UA_SymmetricAlgorithmSecurityHeader_deleteMembers(&symHeader);
- UA_ByteString_deleteMembers(&reply);
- if(retval != UA_STATUSCODE_GOOD){
- client->state = UA_CLIENTSTATE_ERRORED;
- respHeader->serviceResult = retval;
- }
- }
- static UA_StatusCode ActivateSession(UA_Client *client) {
- UA_ActivateSessionRequest request;
- UA_ActivateSessionRequest_init(&request);
- request.requestHeader.requestHandle = 2; //TODO: is it a magic number?
- request.requestHeader.authenticationToken = client->authenticationToken;
- request.requestHeader.timestamp = UA_DateTime_now();
- request.requestHeader.timeoutHint = 10000;
- UA_AnonymousIdentityToken identityToken;
- UA_AnonymousIdentityToken_init(&identityToken);
- UA_String_copy(&client->token.policyId, &identityToken.policyId);
- //manual ExtensionObject encoding of the identityToken
- request.userIdentityToken.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
- request.userIdentityToken.typeId = UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN].typeId;
- request.userIdentityToken.typeId.identifier.numeric+=UA_ENCODINGOFFSET_BINARY;
-
- if (identityToken.policyId.length >= 0)
- UA_ByteString_newMembers(&request.userIdentityToken.body, identityToken.policyId.length+4);
- else {
- identityToken.policyId.length = -1;
- UA_ByteString_newMembers(&request.userIdentityToken.body, 4);
- }
-
- size_t offset = 0;
- UA_ByteString_encodeBinary(&identityToken.policyId,&request.userIdentityToken.body,&offset);
- UA_ActivateSessionResponse response;
- synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_ACTIVATESESSIONREQUEST],
- &response, &UA_TYPES[UA_TYPES_ACTIVATESESSIONRESPONSE]);
- UA_AnonymousIdentityToken_deleteMembers(&identityToken);
- UA_ActivateSessionRequest_deleteMembers(&request);
- UA_ActivateSessionResponse_deleteMembers(&response);
- return response.responseHeader.serviceResult; // not deleted
- }
- static UA_StatusCode EndpointsHandshake(UA_Client *client) {
- UA_GetEndpointsRequest request;
- UA_GetEndpointsRequest_init(&request);
- UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
- request.requestHeader.timestamp = UA_DateTime_now();
- request.requestHeader.timeoutHint = 10000;
- UA_String_copy(&client->endpointUrl, &request.endpointUrl);
- request.profileUrisSize = 1;
- request.profileUris = UA_Array_new(&UA_TYPES[UA_TYPES_STRING], request.profileUrisSize);
- *request.profileUris = UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
- UA_GetEndpointsResponse response;
- UA_GetEndpointsResponse_init(&response);
- synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_GETENDPOINTSREQUEST],
- &response, &UA_TYPES[UA_TYPES_GETENDPOINTSRESPONSE]);
- UA_Boolean endpointFound = UA_FALSE;
- UA_Boolean tokenFound = UA_FALSE;
- for(UA_Int32 i=0; i<response.endpointsSize; ++i){
- UA_EndpointDescription* endpoint = &response.endpoints[i];
- /* look out for an endpoint without security */
- if(!UA_String_equal(&endpoint->securityPolicyUri,
- &UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#None")))
- continue;
- endpointFound = UA_TRUE;
- /* endpoint with no security found */
- /* look for a user token policy with an anonymous token */
- for(UA_Int32 j=0; j<endpoint->userIdentityTokensSize; ++j) {
- UA_UserTokenPolicy* userToken = &endpoint->userIdentityTokens[j];
- if(userToken->tokenType != UA_USERTOKENTYPE_ANONYMOUS)
- continue;
- tokenFound = UA_TRUE;
- UA_UserTokenPolicy_copy(userToken, &client->token);
- break;
- }
- }
- UA_GetEndpointsRequest_deleteMembers(&request);
- UA_GetEndpointsResponse_deleteMembers(&response);
- if(!endpointFound){
- UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT, "No suitable endpoint found");
- return UA_STATUSCODE_BADINTERNALERROR;
- }
- if(!tokenFound){
- UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT, "No anonymous token found");
- return UA_STATUSCODE_BADINTERNALERROR;
- }
- return response.responseHeader.serviceResult;
- }
- static UA_StatusCode SessionHandshake(UA_Client *client) {
- UA_CreateSessionRequest request;
- UA_CreateSessionRequest_init(&request);
- // todo: is this needed for all requests?
- UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
- request.requestHeader.timestamp = UA_DateTime_now();
- request.requestHeader.timeoutHint = 10000;
- UA_ByteString_copy(&client->channel.clientNonce, &request.clientNonce);
- request.requestedSessionTimeout = 1200000;
- request.maxResponseMessageSize = UA_INT32_MAX;
- UA_CreateSessionResponse response;
- UA_CreateSessionResponse_init(&response);
- synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_CREATESESSIONREQUEST],
- &response, &UA_TYPES[UA_TYPES_CREATESESSIONRESPONSE]);
- UA_NodeId_copy(&response.authenticationToken, &client->authenticationToken);
- UA_CreateSessionRequest_deleteMembers(&request);
- UA_CreateSessionResponse_deleteMembers(&response);
- return response.responseHeader.serviceResult; // not deleted
- }
- static UA_StatusCode CloseSession(UA_Client *client) {
- UA_CloseSessionRequest request;
- UA_CloseSessionRequest_init(&request);
- request.requestHeader.timestamp = UA_DateTime_now();
- request.requestHeader.timeoutHint = 10000;
- request.deleteSubscriptions = UA_TRUE;
- UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
- UA_CloseSessionResponse response;
- synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_CLOSESESSIONREQUEST],
- &response, &UA_TYPES[UA_TYPES_CLOSESESSIONRESPONSE]);
- UA_CloseSessionRequest_deleteMembers(&request);
- UA_CloseSessionResponse_deleteMembers(&response);
- return response.responseHeader.serviceResult; // not deleted
- }
- static UA_StatusCode CloseSecureChannel(UA_Client *client) {
- UA_SecureChannel *channel = &client->channel;
- UA_CloseSecureChannelRequest request;
- UA_CloseSecureChannelRequest_init(&request);
- request.requestHeader.requestHandle = 1; //TODO: magic number?
- request.requestHeader.timestamp = UA_DateTime_now();
- request.requestHeader.timeoutHint = 10000;
- request.requestHeader.authenticationToken = client->authenticationToken;
- UA_SecureConversationMessageHeader msgHeader;
- msgHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_CLOF;
- msgHeader.secureChannelId = client->channel.securityToken.channelId;
- UA_SymmetricAlgorithmSecurityHeader symHeader;
- symHeader.tokenId = channel->securityToken.tokenId;
-
- UA_SequenceHeader seqHeader;
- seqHeader.sequenceNumber = ++channel->sequenceNumber;
- seqHeader.requestId = ++client->requestId;
- UA_NodeId typeId = UA_NODEID_NUMERIC(0, UA_NS0ID_CLOSESECURECHANNELREQUEST + UA_ENCODINGOFFSET_BINARY);
- UA_ByteString message;
- UA_Connection *c = &client->connection;
- UA_StatusCode retval = c->getSendBuffer(c, c->remoteConf.recvBufferSize, &message);
- if(retval != UA_STATUSCODE_GOOD)
- return retval;
- size_t offset = 12;
- retval |= UA_SymmetricAlgorithmSecurityHeader_encodeBinary(&symHeader, &message, &offset);
- retval |= UA_SequenceHeader_encodeBinary(&seqHeader, &message, &offset);
- retval |= UA_NodeId_encodeBinary(&typeId, &message, &offset);
- retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_CLOSESECURECHANNELREQUEST], &message, &offset);
- msgHeader.messageHeader.messageSize = offset;
- offset = 0;
- retval |= UA_SecureConversationMessageHeader_encodeBinary(&msgHeader, &message, &offset);
- if(retval != UA_STATUSCODE_GOOD) {
- client->connection.releaseSendBuffer(&client->connection, &message);
- return retval;
- }
-
- message.length = msgHeader.messageHeader.messageSize;
- retval = client->connection.send(&client->connection, &message);
- return retval;
- }
- /*************************/
- /* User-Facing Functions */
- /*************************/
- UA_StatusCode UA_Client_connect(UA_Client *client, UA_ConnectClientConnection connectFunc, char *endpointUrl) {
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
- /** make the function more convenient to the end-user **/
- if(client->state == UA_CLIENTSTATE_CONNECTED){
- UA_Client_disconnect(client);
- }
- if(client->state == UA_CLIENTSTATE_ERRORED){
- UA_Client_reset(client);
- }
- client->connection = connectFunc(UA_ConnectionConfig_standard, endpointUrl, client->logger);
- if(client->connection.state != UA_CONNECTION_OPENING){
- retval = UA_STATUSCODE_BADCONNECTIONCLOSED;
- goto cleanup;
- }
- client->endpointUrl = UA_STRING_ALLOC(endpointUrl);
- if(client->endpointUrl.length < 0){
- retval = UA_STATUSCODE_BADOUTOFMEMORY;
- goto cleanup;
- }
- client->connection.localConf = client->config.localConnectionConfig;
- retval = HelAckHandshake(client);
- if(retval == UA_STATUSCODE_GOOD)
- retval = SecureChannelHandshake(client, UA_FALSE);
- if(retval == UA_STATUSCODE_GOOD)
- retval = EndpointsHandshake(client);
- if(retval == UA_STATUSCODE_GOOD)
- retval = SessionHandshake(client);
- if(retval == UA_STATUSCODE_GOOD)
- retval = ActivateSession(client);
- if(retval == UA_STATUSCODE_GOOD){
- client->connection.state = UA_CONNECTION_ESTABLISHED;
- client->state = UA_CLIENTSTATE_CONNECTED;
- }else{
- goto cleanup;
- }
- return retval;
- cleanup:
- client->state = UA_CLIENTSTATE_ERRORED;
- UA_Client_reset(client);
- return retval;
- }
- UA_StatusCode UA_Client_disconnect(UA_Client *client) {
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
- if(client->channel.connection->state == UA_CONNECTION_ESTABLISHED){
- retval = CloseSession(client);
- if(retval == UA_STATUSCODE_GOOD)
- retval = CloseSecureChannel(client);
- }
- UA_Client_reset(client);
- return retval;
- }
- UA_StatusCode UA_Client_renewSecureChannel(UA_Client *client) {
- return SecureChannelHandshake(client, UA_TRUE);
- }
- UA_ReadResponse UA_Client_read(UA_Client *client, UA_ReadRequest *request) {
- UA_ReadResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_READREQUEST], &response,
- &UA_TYPES[UA_TYPES_READRESPONSE]);
- return response;
- }
- UA_WriteResponse UA_Client_write(UA_Client *client, UA_WriteRequest *request) {
- UA_WriteResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_WRITEREQUEST], &response,
- &UA_TYPES[UA_TYPES_WRITERESPONSE]);
- return response;
- }
- UA_BrowseResponse UA_Client_browse(UA_Client *client, UA_BrowseRequest *request) {
- UA_BrowseResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
- &UA_TYPES[UA_TYPES_BROWSERESPONSE]);
- return response;
- }
- UA_BrowseNextResponse UA_Client_browseNext(UA_Client *client, UA_BrowseNextRequest *request) {
- UA_BrowseNextResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_BROWSENEXTREQUEST], &response,
- &UA_TYPES[UA_TYPES_BROWSENEXTRESPONSE]);
- return response;
- }
- UA_TranslateBrowsePathsToNodeIdsResponse
- UA_Client_translateTranslateBrowsePathsToNodeIds(UA_Client *client,
- UA_TranslateBrowsePathsToNodeIdsRequest *request) {
- UA_TranslateBrowsePathsToNodeIdsResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSREQUEST],
- &response, &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE]);
- return response;
- }
- UA_AddNodesResponse UA_Client_addNodes(UA_Client *client, UA_AddNodesRequest *request) {
- UA_AddNodesResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
- &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
- return response;
- }
- UA_AddReferencesResponse UA_Client_addReferences(UA_Client *client, UA_AddReferencesRequest *request) {
- UA_AddReferencesResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_ADDREFERENCESREQUEST],
- &response, &UA_TYPES[UA_TYPES_ADDREFERENCESRESPONSE]);
- return response;
- }
- UA_DeleteNodesResponse UA_Client_deleteNodes(UA_Client *client, UA_DeleteNodesRequest *request) {
- UA_DeleteNodesResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
- &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
- return response;
- }
- UA_DeleteReferencesResponse UA_Client_deleteReferences(UA_Client *client, UA_DeleteReferencesRequest *request) {
- UA_DeleteReferencesResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETEREFERENCESREQUEST],
- &response, &UA_TYPES[UA_TYPES_DELETEREFERENCESRESPONSE]);
- return response;
- }
- #ifdef ENABLE_SUBSCRIPTIONS
- UA_CreateSubscriptionResponse UA_Client_createSubscription(UA_Client *client, UA_CreateSubscriptionRequest *request) {
- UA_CreateSubscriptionResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONREQUEST],
- &response, &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONRESPONSE]);
- return response;
- }
- UA_DeleteSubscriptionsResponse UA_Client_deleteSubscriptions(UA_Client *client, UA_DeleteSubscriptionsRequest *request) {
- UA_DeleteSubscriptionsResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSREQUEST],
- &response, &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSRESPONSE]);
- return response;
- }
- UA_ModifySubscriptionResponse UA_Client_modifySubscription(UA_Client *client, UA_ModifySubscriptionRequest *request) {
- UA_ModifySubscriptionResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONREQUEST],
- &response, &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONRESPONSE]);
- return response;
- }
- UA_CreateMonitoredItemsResponse UA_Client_createMonitoredItems(UA_Client *client, UA_CreateMonitoredItemsRequest *request) {
- UA_CreateMonitoredItemsResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSREQUEST],
- &response, &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSRESPONSE]);
- return response;
- }
- UA_DeleteMonitoredItemsResponse UA_Client_deleteMonitoredItems(UA_Client *client, UA_DeleteMonitoredItemsRequest *request) {
- UA_DeleteMonitoredItemsResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSREQUEST],
- &response, &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSRESPONSE]);
- return response;
- }
- UA_PublishResponse UA_Client_publish(UA_Client *client, UA_PublishRequest *request) {
- UA_PublishResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
- &response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
- return response;
- }
- UA_Int32 UA_Client_newSubscription(UA_Client *client, UA_Int32 publishInterval) {
- UA_Int32 retval;
- UA_CreateSubscriptionRequest aReq;
- UA_CreateSubscriptionResponse aRes;
- UA_CreateSubscriptionRequest_init(&aReq);
- UA_CreateSubscriptionResponse_init(&aRes);
-
- aReq.maxNotificationsPerPublish = 10;
- aReq.priority = 0;
- aReq.publishingEnabled = UA_TRUE;
- aReq.requestedLifetimeCount = 100;
- aReq.requestedMaxKeepAliveCount = 10;
- aReq.requestedPublishingInterval = publishInterval;
-
- aRes = UA_Client_createSubscription(client, &aReq);
-
- if (aRes.responseHeader.serviceResult == UA_STATUSCODE_GOOD) {
- UA_Client_Subscription *newSub = UA_malloc(sizeof(UA_Client_Subscription));
- LIST_INIT(&newSub->MonitoredItems);
-
- newSub->LifeTime = aRes.revisedLifetimeCount;
- newSub->KeepAliveCount = aRes.revisedMaxKeepAliveCount;
- newSub->PublishingInterval = aRes.revisedPublishingInterval;
- newSub->SubscriptionID = aRes.subscriptionId;
- newSub->NotificationsPerPublish = aReq.maxNotificationsPerPublish;
- newSub->Priority = aReq.priority;
- retval = newSub->SubscriptionID;
- LIST_INSERT_HEAD(&(client->subscriptions), newSub, listEntry);
- } else
- retval = 0;
-
- UA_CreateSubscriptionResponse_deleteMembers(&aRes);
- UA_CreateSubscriptionRequest_deleteMembers(&aReq);
- return retval;
- }
- UA_StatusCode UA_Client_removeSubscription(UA_Client *client, UA_UInt32 subscriptionId) {
- UA_Client_Subscription *sub;
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
-
- LIST_FOREACH(sub, &(client->subscriptions), listEntry) {
- if (sub->SubscriptionID == subscriptionId)
- break;
- }
-
- // Problem? We do not have this subscription registeres. Maybe the server should
- // be consulted at this point?
- if (sub == NULL)
- return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
-
- UA_DeleteSubscriptionsRequest request;
- UA_DeleteSubscriptionsResponse response;
- UA_DeleteSubscriptionsRequest_init(&request);
- UA_DeleteSubscriptionsResponse_init(&response);
-
- request.subscriptionIdsSize=1;
- request.subscriptionIds = (UA_UInt32 *) UA_malloc(sizeof(UA_UInt32));
- *(request.subscriptionIds) = sub->SubscriptionID;
-
- UA_Client_MonitoredItem *mon, *tmpmon;
- LIST_FOREACH_SAFE(mon, &(sub->MonitoredItems), listEntry, tmpmon) {
- retval |= UA_Client_unMonitorItemChanges(client, sub->SubscriptionID, mon->MonitoredItemId);
- }
- if (retval != UA_STATUSCODE_GOOD){
- UA_DeleteSubscriptionsRequest_deleteMembers(&request);
- return retval;
- }
-
- response = UA_Client_deleteSubscriptions(client, &request);
-
- if (response.resultsSize > 0)
- retval = response.results[0];
- else
- retval = response.responseHeader.serviceResult;
-
- if (retval == UA_STATUSCODE_GOOD) {
- LIST_REMOVE(sub, listEntry);
- UA_free(sub);
- }
- UA_DeleteSubscriptionsRequest_deleteMembers(&request);
- UA_DeleteSubscriptionsResponse_deleteMembers(&response);
- return retval;
- }
- UA_UInt32 UA_Client_monitorItemChanges(UA_Client *client, UA_UInt32 subscriptionId,
- UA_NodeId nodeId, UA_UInt32 attributeID, void *handlingFunction) {
- UA_Client_Subscription *sub;
- UA_StatusCode retval = 0;
-
- LIST_FOREACH(sub, &(client->subscriptions), listEntry) {
- if (sub->SubscriptionID == subscriptionId)
- break;
- }
-
- // Maybe the same problem as in DeleteSubscription... ask the server?
- if (sub == NULL)
- return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
-
- UA_CreateMonitoredItemsRequest request;
- UA_CreateMonitoredItemsResponse response;
- UA_CreateMonitoredItemsRequest_init(&request);
- UA_CreateMonitoredItemsResponse_init(&response);
- request.subscriptionId = subscriptionId;
- request.itemsToCreateSize = 1;
- request.itemsToCreate = UA_MonitoredItemCreateRequest_new();
- UA_NodeId_copy(&nodeId, &((request.itemsToCreate[0]).itemToMonitor.nodeId));
- (request.itemsToCreate[0]).itemToMonitor.attributeId = attributeID;
- (request.itemsToCreate[0]).monitoringMode = UA_MONITORINGMODE_REPORTING;
- (request.itemsToCreate[0]).requestedParameters.clientHandle = ++(client->monitoredItemHandles);
- (request.itemsToCreate[0]).requestedParameters.samplingInterval = sub->PublishingInterval;
- (request.itemsToCreate[0]).requestedParameters.discardOldest = UA_TRUE;
- (request.itemsToCreate[0]).requestedParameters.queueSize = 1;
- // Filter can be left void for now, only changes are supported (UA_Expert does the same with changeItems)
-
- response = UA_Client_createMonitoredItems(client, &request);
-
- // slight misuse of retval here to check if the deletion was successfull.
- if (response.resultsSize == 0)
- retval = response.responseHeader.serviceResult;
- else
- retval = response.results[0].statusCode;
-
- if (retval == UA_STATUSCODE_GOOD) {
- UA_Client_MonitoredItem *newMon = (UA_Client_MonitoredItem *) UA_malloc(sizeof(UA_Client_MonitoredItem));
- newMon->MonitoringMode = UA_MONITORINGMODE_REPORTING;
- UA_NodeId_copy(&nodeId, &(newMon->monitoredNodeId));
- newMon->AttributeID = attributeID;
- newMon->ClientHandle = client->monitoredItemHandles;
- newMon->SamplingInterval = sub->PublishingInterval;
- newMon->QueueSize = 1;
- newMon->DiscardOldest = UA_TRUE;
- newMon->handler = handlingFunction;
- newMon->MonitoredItemId = response.results[0].monitoredItemId;
-
- LIST_INSERT_HEAD(&(sub->MonitoredItems), newMon, listEntry);
- retval = newMon->MonitoredItemId ;
- }
- else {
- retval = 0;
- }
-
- UA_CreateMonitoredItemsRequest_deleteMembers(&request);
- UA_CreateMonitoredItemsResponse_deleteMembers(&response);
-
- return retval;
- }
- UA_StatusCode UA_Client_unMonitorItemChanges(UA_Client *client, UA_UInt32 subscriptionId, UA_UInt32 monitoredItemId ) {
- UA_Client_Subscription *sub;
- UA_StatusCode retval = 0;
-
- LIST_FOREACH(sub, &(client->subscriptions), listEntry) {
- if (sub->SubscriptionID == subscriptionId)
- break;
- }
- // Maybe the same problem as in DeleteSubscription... ask the server?
- if (sub == NULL)
- return UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
-
- UA_Client_MonitoredItem *mon, *tmpmon;
- LIST_FOREACH_SAFE(mon, &(sub->MonitoredItems), listEntry, tmpmon) {
- if (mon->MonitoredItemId == monitoredItemId)
- break;
- }
- // Also... ask the server?
- if(mon==NULL) {
- return UA_STATUSCODE_BADMONITOREDITEMIDINVALID;
- }
-
- UA_DeleteMonitoredItemsRequest request;
- UA_DeleteMonitoredItemsResponse response;
- UA_DeleteMonitoredItemsRequest_init(&request);
- UA_DeleteMonitoredItemsResponse_init(&response);
-
- request.subscriptionId = sub->SubscriptionID;
- request.monitoredItemIdsSize = 1;
- request.monitoredItemIds = (UA_UInt32 *) UA_malloc(sizeof(UA_UInt32));
- request.monitoredItemIds[0] = mon->MonitoredItemId;
-
- response = UA_Client_deleteMonitoredItems(client, &request);
- if (response.resultsSize > 1)
- retval = response.results[0];
- else
- retval = response.responseHeader.serviceResult;
-
- if (retval == 0) {
- LIST_REMOVE(mon, listEntry);
- UA_NodeId_deleteMembers(&mon->monitoredNodeId);
- UA_free(mon);
- }
-
- UA_DeleteMonitoredItemsRequest_deleteMembers(&request);
- UA_DeleteMonitoredItemsResponse_deleteMembers(&response);
-
- return retval;
- }
- UA_Boolean UA_Client_processPublishRx(UA_Client *client, UA_PublishResponse response) {
- UA_Client_Subscription *sub;
- UA_Client_MonitoredItem *mon;
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
-
- if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
- return UA_FALSE;
-
- // Check if the server has acknowledged any of our ACKS
- // Note that a list of serverside status codes may be send without valid publish data, i.e.
- // during keepalives or no data availability
- UA_Client_NotificationsAckNumber *tmpAck = client->pendingNotificationsAcks.lh_first;
- UA_Client_NotificationsAckNumber *nxtAck = tmpAck;
- for(int i=0; i<response.resultsSize && nxtAck != NULL; i++) {
- tmpAck = nxtAck;
- nxtAck = tmpAck->listEntry.le_next;
- if (response.results[i] == UA_STATUSCODE_GOOD || response.results[i] == UA_STATUSCODE_BADSEQUENCENUMBERINVALID) {
- LIST_REMOVE(tmpAck, listEntry);
- UA_free(tmpAck);
- }
- }
-
- if(response.subscriptionId == 0)
- return UA_FALSE;
-
- LIST_FOREACH(sub, &(client->subscriptions), listEntry) {
- if (sub->SubscriptionID == response.subscriptionId)
- break;
- }
- if (sub == NULL)
- return UA_FALSE;
-
- UA_NotificationMessage msg = response.notificationMessage;
- UA_DataChangeNotification dataChangeNotification;
- size_t decodingOffset = 0;
- for (int k=0; k<msg.notificationDataSize; k++) {
- if (msg.notificationData[k].encoding == UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING) {
- if (msg.notificationData[k].typeId.namespaceIndex == 0 && msg.notificationData[k].typeId.identifier.numeric == 811 ) {
- // This is a dataChangeNotification
- retval |= UA_DataChangeNotification_decodeBinary(&(msg.notificationData[k].body), &decodingOffset, &dataChangeNotification);
- UA_MonitoredItemNotification *mitemNot;
- for(int i=0; i<dataChangeNotification.monitoredItemsSize; i++) {
- mitemNot = &dataChangeNotification.monitoredItems[i];
- // find this client handle
- LIST_FOREACH(mon, &(sub->MonitoredItems), listEntry) {
- if (mon->ClientHandle == mitemNot->clientHandle) {
- mon->handler(mitemNot->clientHandle, &(mitemNot->value));
- break;
- }
- }
- }
- UA_DataChangeNotification_deleteMembers(&dataChangeNotification);
- }
- else if (msg.notificationData[k].typeId.namespaceIndex == 0 && msg.notificationData[k].typeId.identifier.numeric == 820 ) {
- //FIXME: This is a statusChangeNotification (not supported yet)
- continue;
- }
- else if (msg.notificationData[k].typeId.namespaceIndex == 0 && msg.notificationData[k].typeId.identifier.numeric == 916 ) {
- //FIXME: This is an EventNotification
- continue;
- }
- }
- }
-
- // We processed this message, add it to the list of pending acks (but make sure it's not in the list first)
- LIST_FOREACH(tmpAck, &(client->pendingNotificationsAcks), listEntry) {
- if (tmpAck->subAck.sequenceNumber == msg.sequenceNumber &&
- tmpAck->subAck.subscriptionId == response.subscriptionId)
- break;
- }
- if (tmpAck == NULL ){
- tmpAck = (UA_Client_NotificationsAckNumber *) UA_malloc(sizeof(UA_Client_NotificationsAckNumber));
- tmpAck->subAck.sequenceNumber = msg.sequenceNumber;
- tmpAck->subAck.subscriptionId = sub->SubscriptionID;
- tmpAck->listEntry.le_next = UA_NULL;
- tmpAck->listEntry.le_prev = UA_NULL;
- LIST_INSERT_HEAD(&(client->pendingNotificationsAcks), tmpAck, listEntry);
- }
-
- return response.moreNotifications;
- }
- void UA_Client_doPublish(UA_Client *client) {
- UA_PublishRequest request;
- UA_PublishResponse response;
- UA_Client_NotificationsAckNumber *ack;
- UA_Boolean moreNotifications = UA_TRUE;
- int index = 0 ;
-
- do {
- UA_PublishRequest_init(&request);
- UA_PublishResponse_init(&response);
-
- request.subscriptionAcknowledgementsSize = 0;
- LIST_FOREACH(ack, &(client->pendingNotificationsAcks), listEntry) {
- request.subscriptionAcknowledgementsSize++;
- }
- request.subscriptionAcknowledgements = (UA_SubscriptionAcknowledgement *) UA_malloc(sizeof(UA_SubscriptionAcknowledgement)*request.subscriptionAcknowledgementsSize);
-
- index = 0;
- LIST_FOREACH(ack, &(client->pendingNotificationsAcks), listEntry) {
- request.subscriptionAcknowledgements[index].sequenceNumber = ack->subAck.sequenceNumber;
- request.subscriptionAcknowledgements[index].subscriptionId = ack->subAck.subscriptionId;
- index++;
- }
-
- response = UA_Client_publish(client, &request);
- if (response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
- moreNotifications = UA_Client_processPublishRx(client, response);
- else
- moreNotifications = UA_FALSE;
-
- UA_PublishResponse_deleteMembers(&response);
- UA_PublishRequest_deleteMembers(&request);
- } while(moreNotifications == UA_TRUE);
- return;
- }
- #endif
- /**********************************/
- /* User-Facing Macros-Function */
- /**********************************/
- #ifdef ENABLE_METHODCALLS
- UA_CallResponse UA_Client_call(UA_Client *client, UA_CallRequest *request) {
- UA_CallResponse response;
- synchronousRequest(client, request, &UA_TYPES[UA_TYPES_CALLREQUEST],
- &response, &UA_TYPES[UA_TYPES_CALLRESPONSE]);
- return response;
- }
- UA_StatusCode UA_Client_CallServerMethod(UA_Client *client, UA_NodeId objectNodeId, UA_NodeId methodNodeId,
- UA_Int32 inputSize, const UA_Variant *input,
- UA_Int32 *outputSize, UA_Variant **output) {
- UA_CallRequest request;
- UA_CallRequest_init(&request);
-
- request.methodsToCallSize = 1;
- request.methodsToCall = UA_CallMethodRequest_new();
- if(!request.methodsToCall)
- return UA_STATUSCODE_BADOUTOFMEMORY;
- UA_CallMethodRequest *rq = &request.methodsToCall[0];
- UA_NodeId_copy(&methodNodeId, &rq->methodId);
- UA_NodeId_copy(&objectNodeId, &rq->objectId);
- rq->inputArguments = (void*)(uintptr_t)input; // cast const...
- rq->inputArgumentsSize = inputSize;
-
- UA_CallResponse response;
- response = UA_Client_call(client, &request);
- rq->inputArguments = UA_NULL;
- rq->inputArgumentsSize = -1;
- UA_CallRequest_deleteMembers(&request);
- UA_StatusCode retval = response.responseHeader.serviceResult;
- if(response.resultsSize > 0){
- retval |= response.results[0].statusCode;
- if(retval == UA_STATUSCODE_GOOD) {
- *output = response.results[0].outputArguments;
- *outputSize = response.results[0].outputArgumentsSize;
- response.results[0].outputArguments = UA_NULL;
- response.results[0].outputArgumentsSize = -1;
- }
- }
- UA_CallResponse_deleteMembers(&response);
- return retval;
- }
- #endif
- #define ADDNODES_COPYDEFAULTATTRIBUTES(REQUEST,ATTRIBUTES) do { \
- ATTRIBUTES.specifiedAttributes = 0; \
- if(! UA_LocalizedText_copy(&description, &(ATTRIBUTES.description))) \
- ATTRIBUTES.specifiedAttributes |= UA_NODEATTRIBUTESMASK_DESCRIPTION; \
- if(! UA_LocalizedText_copy(&displayName, &(ATTRIBUTES.displayName))) \
- ATTRIBUTES.specifiedAttributes |= UA_NODEATTRIBUTESMASK_DISPLAYNAME; \
- ATTRIBUTES.userWriteMask = userWriteMask; \
- ATTRIBUTES.specifiedAttributes |= UA_NODEATTRIBUTESMASK_USERWRITEMASK; \
- ATTRIBUTES.writeMask = writeMask; \
- ATTRIBUTES.specifiedAttributes |= UA_NODEATTRIBUTESMASK_WRITEMASK; \
- UA_QualifiedName_copy(&browseName, &(REQUEST.nodesToAdd[0].browseName)); \
- UA_ExpandedNodeId_copy(&parentNodeId, &(REQUEST.nodesToAdd[0].parentNodeId)); \
- UA_NodeId_copy(&referenceTypeId, &(REQUEST.nodesToAdd[0].referenceTypeId)); \
- UA_ExpandedNodeId_copy(&typeDefinition, &(REQUEST.nodesToAdd[0].typeDefinition)); \
- UA_ExpandedNodeId_copy(&reqId, &(REQUEST.nodesToAdd[0].requestedNewNodeId )); \
- REQUEST.nodesToAddSize = 1; \
- } while(0)
-
- #define ADDNODES_PACK_AND_SEND(PREQUEST,PATTRIBUTES,PNODETYPE) do { \
- PREQUEST.nodesToAdd[0].nodeAttributes.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING; \
- PREQUEST.nodesToAdd[0].nodeAttributes.typeId = UA_NODEID_NUMERIC(0, UA_NS0ID_##PNODETYPE##ATTRIBUTES + UA_ENCODINGOFFSET_BINARY); \
- size_t encOffset = 0; \
- UA_ByteString_newMembers(&PREQUEST.nodesToAdd[0].nodeAttributes.body, client->connection.remoteConf.maxMessageSize); \
- UA_encodeBinary(&PATTRIBUTES,&UA_TYPES[UA_TYPES_##PNODETYPE##ATTRIBUTES], &(PREQUEST.nodesToAdd[0].nodeAttributes.body), &encOffset); \
- PREQUEST.nodesToAdd[0].nodeAttributes.body.length = encOffset; \
- *(adRes) = UA_Client_addNodes(client, &PREQUEST); \
- UA_AddNodesRequest_deleteMembers(&PREQUEST); \
- } while(0)
-
- /* NodeManagement */
- UA_AddNodesResponse *UA_Client_createObjectNode(UA_Client *client, UA_ExpandedNodeId reqId, UA_QualifiedName browseName, UA_LocalizedText displayName,
- UA_LocalizedText description, UA_ExpandedNodeId parentNodeId, UA_NodeId referenceTypeId,
- UA_UInt32 userWriteMask, UA_UInt32 writeMask, UA_ExpandedNodeId typeDefinition ) {
- UA_AddNodesRequest adReq;
- UA_AddNodesRequest_init(&adReq);
- UA_AddNodesResponse *adRes;
- adRes = UA_AddNodesResponse_new();
- UA_AddNodesResponse_init(adRes);
- UA_ObjectAttributes vAtt;
- UA_ObjectAttributes_init(&vAtt);
- adReq.nodesToAdd = (UA_AddNodesItem *) UA_AddNodesItem_new();
- UA_AddNodesItem_init(adReq.nodesToAdd);
- // Default node properties and attributes
- ADDNODES_COPYDEFAULTATTRIBUTES(adReq, vAtt);
-
- // Specific to objects
- adReq.nodesToAdd[0].nodeClass = UA_NODECLASS_OBJECT;
- vAtt.eventNotifier = 0;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_EVENTNOTIFIER;
- ADDNODES_PACK_AND_SEND(adReq,vAtt,OBJECT);
-
- return adRes;
- }
- UA_AddNodesResponse *UA_Client_createVariableNode(UA_Client *client, UA_ExpandedNodeId reqId, UA_QualifiedName browseName, UA_LocalizedText displayName,
- UA_LocalizedText description, UA_ExpandedNodeId parentNodeId, UA_NodeId referenceTypeId,
- UA_UInt32 userWriteMask, UA_UInt32 writeMask, UA_ExpandedNodeId typeDefinition,
- UA_NodeId dataType, UA_Variant *value) {
- UA_AddNodesRequest adReq;
- UA_AddNodesRequest_init(&adReq);
-
- UA_AddNodesResponse *adRes;
- adRes = UA_AddNodesResponse_new();
- UA_AddNodesResponse_init(adRes);
-
- UA_VariableAttributes vAtt;
- UA_VariableAttributes_init(&vAtt);
- adReq.nodesToAdd = (UA_AddNodesItem *) UA_AddNodesItem_new();
- UA_AddNodesItem_init(adReq.nodesToAdd);
-
- // Default node properties and attributes
- ADDNODES_COPYDEFAULTATTRIBUTES(adReq, vAtt);
-
- // Specific to variables
- adReq.nodesToAdd[0].nodeClass = UA_NODECLASS_VARIABLE;
- vAtt.accessLevel = 0;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_ACCESSLEVEL;
- vAtt.userAccessLevel = 0;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_USERACCESSLEVEL;
- vAtt.minimumSamplingInterval = 100;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_MINIMUMSAMPLINGINTERVAL;
- vAtt.historizing = UA_FALSE;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_HISTORIZING;
-
- if (value != NULL) {
- UA_Variant_copy(value, &(vAtt.value));
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_VALUE;
- vAtt.valueRank = -2;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_VALUERANK;
- // These are defined by the variant
- //vAtt.arrayDimensionsSize = value->arrayDimensionsSize;
- //vAtt.arrayDimensions = NULL;
- }
- UA_NodeId_copy(&dataType, &(vAtt.dataType));
-
- ADDNODES_PACK_AND_SEND(adReq,vAtt,VARIABLE);
-
- return adRes;
- }
- UA_AddNodesResponse *UA_Client_createReferenceTypeNode(UA_Client *client, UA_ExpandedNodeId reqId, UA_QualifiedName browseName, UA_LocalizedText displayName,
- UA_LocalizedText description, UA_ExpandedNodeId parentNodeId, UA_NodeId referenceTypeId,
- UA_UInt32 userWriteMask, UA_UInt32 writeMask, UA_ExpandedNodeId typeDefinition,
- UA_LocalizedText inverseName ) {
- UA_AddNodesRequest adReq;
- UA_AddNodesRequest_init(&adReq);
-
- UA_AddNodesResponse *adRes;
- adRes = UA_AddNodesResponse_new();
- UA_AddNodesResponse_init(adRes);
-
- UA_ReferenceTypeAttributes vAtt;
- UA_ReferenceTypeAttributes_init(&vAtt);
- adReq.nodesToAdd = (UA_AddNodesItem *) UA_AddNodesItem_new();
- UA_AddNodesItem_init(adReq.nodesToAdd);
-
- // Default node properties and attributes
- ADDNODES_COPYDEFAULTATTRIBUTES(adReq, vAtt);
- // Specific to referencetypes
- adReq.nodesToAdd[0].nodeClass = UA_NODECLASS_REFERENCETYPE;
- UA_LocalizedText_copy(&inverseName, &(vAtt.inverseName));
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_INVERSENAME;
- vAtt.symmetric = UA_FALSE;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_SYMMETRIC;
- vAtt.isAbstract = UA_FALSE;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_ISABSTRACT;
-
-
- ADDNODES_PACK_AND_SEND(adReq,vAtt,REFERENCETYPE);
-
- return adRes;
- }
- UA_AddNodesResponse *UA_Client_createObjectTypeNode(UA_Client *client, UA_ExpandedNodeId reqId, UA_QualifiedName browseName, UA_LocalizedText displayName,
- UA_LocalizedText description, UA_ExpandedNodeId parentNodeId, UA_NodeId referenceTypeId,
- UA_UInt32 userWriteMask, UA_UInt32 writeMask, UA_ExpandedNodeId typeDefinition) {
- UA_AddNodesRequest adReq;
- UA_AddNodesRequest_init(&adReq);
-
- UA_AddNodesResponse *adRes;
- adRes = UA_AddNodesResponse_new();
- UA_AddNodesResponse_init(adRes);
-
- UA_ObjectTypeAttributes vAtt;
- UA_ObjectTypeAttributes_init(&vAtt);
- adReq.nodesToAdd = (UA_AddNodesItem *) UA_AddNodesItem_new();
- UA_AddNodesItem_init(adReq.nodesToAdd);
-
- // Default node properties and attributes
- ADDNODES_COPYDEFAULTATTRIBUTES(adReq, vAtt);
-
- // Specific to referencetypes
- adReq.nodesToAdd[0].nodeClass = UA_NODECLASS_OBJECTTYPE;
- vAtt.isAbstract = UA_FALSE;
- vAtt.specifiedAttributes |= UA_NODEATTRIBUTESMASK_ISABSTRACT;
-
-
- ADDNODES_PACK_AND_SEND(adReq,vAtt,OBJECTTYPE);
-
- return adRes;
- }
|