ua_services_call.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2015 (c) Chris Iatrou
  6. * Copyright 2015-2017 (c) Florian Palm
  7. * Copyright 2015-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  8. * Copyright 2015-2016 (c) Sten Grüner
  9. * Copyright 2015 (c) Oleksiy Vasylyev
  10. * Copyright 2016 (c) LEvertz
  11. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  12. * Copyright 2017 (c) Julian Grothoff
  13. */
  14. #include "ua_services.h"
  15. #include "ua_server_internal.h"
  16. #ifdef UA_ENABLE_METHODCALLS /* conditional compilation */
  17. static const UA_VariableNode *
  18. getArgumentsVariableNode(UA_Server *server, const UA_MethodNode *ofMethod,
  19. UA_String withBrowseName) {
  20. UA_NodeId hasProperty = UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY);
  21. for(size_t i = 0; i < ofMethod->referencesSize; ++i) {
  22. UA_NodeReferenceKind *rk = &ofMethod->references[i];
  23. if(rk->isInverse != false)
  24. continue;
  25. if(!UA_NodeId_equal(&hasProperty, &rk->referenceTypeId))
  26. continue;
  27. for(size_t j = 0; j < rk->targetIdsSize; ++j) {
  28. const UA_Node *refTarget =
  29. server->config.nodestore.getNode(server->config.nodestore.context,
  30. &rk->targetIds[j].nodeId);
  31. if(!refTarget)
  32. continue;
  33. if(refTarget->nodeClass == UA_NODECLASS_VARIABLE &&
  34. refTarget->browseName.namespaceIndex == 0 &&
  35. UA_String_equal(&withBrowseName, &refTarget->browseName.name)) {
  36. return (const UA_VariableNode*)refTarget;
  37. }
  38. server->config.nodestore.releaseNode(server->config.nodestore.context,
  39. refTarget);
  40. }
  41. }
  42. return NULL;
  43. }
  44. /* inputArgumentResults has the length request->inputArgumentsSize */
  45. static UA_StatusCode
  46. typeCheckArguments(UA_Server *server, UA_Session *session,
  47. const UA_VariableNode *argRequirements, size_t argsSize,
  48. UA_Variant *args, UA_StatusCode *inputArgumentResults) {
  49. /* Verify that we have a Variant containing UA_Argument (scalar or array) in
  50. * the "InputArguments" node */
  51. if(argRequirements->valueSource != UA_VALUESOURCE_DATA)
  52. return UA_STATUSCODE_BADINTERNALERROR;
  53. if(!argRequirements->value.data.value.hasValue)
  54. return UA_STATUSCODE_BADINTERNALERROR;
  55. if(argRequirements->value.data.value.value.type != &UA_TYPES[UA_TYPES_ARGUMENT])
  56. return UA_STATUSCODE_BADINTERNALERROR;
  57. /* Verify the number of arguments. A scalar argument value is interpreted as
  58. * an array of length 1. */
  59. size_t argReqsSize = argRequirements->value.data.value.value.arrayLength;
  60. if(UA_Variant_isScalar(&argRequirements->value.data.value.value))
  61. argReqsSize = 1;
  62. if(argReqsSize > argsSize)
  63. return UA_STATUSCODE_BADARGUMENTSMISSING;
  64. if(argReqsSize < argsSize)
  65. return UA_STATUSCODE_BADTOOMANYARGUMENTS;
  66. /* Type-check every argument against the definition */
  67. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  68. UA_Argument *argReqs = (UA_Argument*)argRequirements->value.data.value.value.data;
  69. for(size_t i = 0; i < argReqsSize; ++i) {
  70. if(!compatibleValue(server, session, &argReqs[i].dataType, argReqs[i].valueRank,
  71. argReqs[i].arrayDimensionsSize, argReqs[i].arrayDimensions,
  72. &args[i], NULL)) {
  73. inputArgumentResults[i] = UA_STATUSCODE_BADTYPEMISMATCH;
  74. retval = UA_STATUSCODE_BADINVALIDARGUMENT;
  75. }
  76. }
  77. return retval;
  78. }
  79. /* inputArgumentResults has the length request->inputArgumentsSize */
  80. static UA_StatusCode
  81. validMethodArguments(UA_Server *server, UA_Session *session, const UA_MethodNode *method,
  82. const UA_CallMethodRequest *request,
  83. UA_StatusCode *inputArgumentResults) {
  84. /* Get the input arguments node */
  85. const UA_VariableNode *inputArguments =
  86. getArgumentsVariableNode(server, method, UA_STRING("InputArguments"));
  87. if(!inputArguments) {
  88. if(request->inputArgumentsSize > 0)
  89. return UA_STATUSCODE_BADTOOMANYARGUMENTS;
  90. return UA_STATUSCODE_GOOD;
  91. }
  92. /* Verify the request */
  93. UA_StatusCode retval = typeCheckArguments(server, session, inputArguments,
  94. request->inputArgumentsSize,
  95. request->inputArguments,
  96. inputArgumentResults);
  97. /* Release the input arguments node */
  98. server->config.nodestore.releaseNode(server->config.nodestore.context,
  99. (const UA_Node*)inputArguments);
  100. return retval;
  101. }
  102. static const UA_NodeId hasComponentNodeId = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASCOMPONENT}};
  103. static const UA_NodeId hasSubTypeNodeId = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASSUBTYPE}};
  104. static void
  105. callWithMethodAndObject(UA_Server *server, UA_Session *session,
  106. const UA_CallMethodRequest *request, UA_CallMethodResult *result,
  107. const UA_MethodNode *method, const UA_ObjectNode *object) {
  108. /* Verify the object's NodeClass */
  109. if(object->nodeClass != UA_NODECLASS_OBJECT &&
  110. object->nodeClass != UA_NODECLASS_OBJECTTYPE) {
  111. result->statusCode = UA_STATUSCODE_BADNODECLASSINVALID;
  112. return;
  113. }
  114. /* Verify the method's NodeClass */
  115. if(method->nodeClass != UA_NODECLASS_METHOD) {
  116. result->statusCode = UA_STATUSCODE_BADNODECLASSINVALID;
  117. return;
  118. }
  119. /* Is there a method to execute? */
  120. if(!method->method) {
  121. result->statusCode = UA_STATUSCODE_BADINTERNALERROR;
  122. return;
  123. }
  124. /* Verify method/object relations. Object must have a hasComponent or a
  125. * subtype of hasComponent reference to the method node. Therefore, check
  126. * every reference between the parent object and the method node if there is
  127. * a hasComponent (or subtype) reference */
  128. UA_Boolean found = false;
  129. for(size_t i = 0; i < object->referencesSize && !found; ++i) {
  130. UA_NodeReferenceKind *rk = &object->references[i];
  131. if(rk->isInverse)
  132. continue;
  133. if(!isNodeInTree(&server->config.nodestore, &rk->referenceTypeId,
  134. &hasComponentNodeId, &hasSubTypeNodeId, 1))
  135. continue;
  136. for(size_t j = 0; j < rk->targetIdsSize; ++j) {
  137. if(UA_NodeId_equal(&rk->targetIds[j].nodeId, &request->methodId)) {
  138. found = true;
  139. break;
  140. }
  141. }
  142. }
  143. if(!found) {
  144. result->statusCode = UA_STATUSCODE_BADMETHODINVALID;
  145. return;
  146. }
  147. /* Verify access rights */
  148. UA_Boolean executable = method->executable;
  149. if(session != &server->adminSession)
  150. executable = executable &&
  151. server->config.accessControl.getUserExecutableOnObject(server,
  152. &server->config.accessControl, &session->sessionId,
  153. session->sessionHandle, &request->methodId, method->context,
  154. &request->objectId, object->context);
  155. if(!executable) {
  156. result->statusCode = UA_STATUSCODE_BADNOTEXECUTABLE;
  157. return;
  158. }
  159. /* Allocate the inputArgumentResults array */
  160. result->inputArgumentResults = (UA_StatusCode*)
  161. UA_Array_new(request->inputArgumentsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  162. if(!result->inputArgumentResults) {
  163. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  164. return;
  165. }
  166. result->inputArgumentResultsSize = request->inputArgumentsSize;
  167. /* Verify Input Arguments */
  168. result->statusCode = validMethodArguments(server, session, method, request, result->inputArgumentResults);
  169. /* Return inputArgumentResults only for BADINVALIDARGUMENT */
  170. if(result->statusCode != UA_STATUSCODE_BADINVALIDARGUMENT) {
  171. UA_Array_delete(result->inputArgumentResults, result->inputArgumentResultsSize,
  172. &UA_TYPES[UA_TYPES_STATUSCODE]);
  173. result->inputArgumentResults = NULL;
  174. result->inputArgumentResultsSize = 0;
  175. }
  176. /* Error during type-checking? */
  177. if(result->statusCode != UA_STATUSCODE_GOOD)
  178. return;
  179. /* Get the output arguments node */
  180. const UA_VariableNode *outputArguments =
  181. getArgumentsVariableNode(server, method, UA_STRING("OutputArguments"));
  182. /* Allocate the output arguments array */
  183. size_t outputArgsSize = 0;
  184. if(outputArguments)
  185. outputArgsSize = outputArguments->value.data.value.value.arrayLength;
  186. result->outputArguments = (UA_Variant*)
  187. UA_Array_new(outputArgsSize, &UA_TYPES[UA_TYPES_VARIANT]);
  188. if(!result->outputArguments) {
  189. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  190. return;
  191. }
  192. result->outputArgumentsSize = outputArgsSize;
  193. /* Release the output arguments node */
  194. server->config.nodestore.releaseNode(server->config.nodestore.context,
  195. (const UA_Node*)outputArguments);
  196. /* Call the method */
  197. result->statusCode = method->method(server, &session->sessionId, session->sessionHandle,
  198. &method->nodeId, method->context,
  199. &object->nodeId, object->context,
  200. request->inputArgumentsSize, request->inputArguments,
  201. result->outputArgumentsSize, result->outputArguments);
  202. /* TODO: Verify Output matches the argument definition */
  203. }
  204. static void
  205. Operation_CallMethod(UA_Server *server, UA_Session *session, void *context,
  206. const UA_CallMethodRequest *request, UA_CallMethodResult *result) {
  207. /* Get the method node */
  208. const UA_MethodNode *method = (const UA_MethodNode*)
  209. server->config.nodestore.getNode(server->config.nodestore.context,
  210. &request->methodId);
  211. if(!method) {
  212. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  213. return;
  214. }
  215. /* Get the object node */
  216. const UA_ObjectNode *object = (const UA_ObjectNode*)
  217. server->config.nodestore.getNode(server->config.nodestore.context,
  218. &request->objectId);
  219. if(!object) {
  220. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  221. server->config.nodestore.releaseNode(server->config.nodestore.context,
  222. (const UA_Node*)method);
  223. return;
  224. }
  225. /* Continue with method and object as context */
  226. callWithMethodAndObject(server, session, request, result, method, object);
  227. /* Release the method and object node */
  228. server->config.nodestore.releaseNode(server->config.nodestore.context,
  229. (const UA_Node*)method);
  230. server->config.nodestore.releaseNode(server->config.nodestore.context,
  231. (const UA_Node*)object);
  232. }
  233. void Service_Call(UA_Server *server, UA_Session *session,
  234. const UA_CallRequest *request,
  235. UA_CallResponse *response) {
  236. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  237. "Processing CallRequest");
  238. if(server->config.maxNodesPerMethodCall != 0 &&
  239. request->methodsToCallSize > server->config.maxNodesPerMethodCall) {
  240. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  241. return;
  242. }
  243. response->responseHeader.serviceResult =
  244. UA_Server_processServiceOperations(server, session, (UA_ServiceOperation)Operation_CallMethod, NULL,
  245. &request->methodsToCallSize, &UA_TYPES[UA_TYPES_CALLMETHODREQUEST],
  246. &response->resultsSize, &UA_TYPES[UA_TYPES_CALLMETHODRESULT]);
  247. }
  248. UA_CallMethodResult UA_EXPORT
  249. UA_Server_call(UA_Server *server, const UA_CallMethodRequest *request) {
  250. UA_CallMethodResult result;
  251. UA_CallMethodResult_init(&result);
  252. Operation_CallMethod(server, &server->adminSession, NULL, request, &result);
  253. return result;
  254. }
  255. #endif /* UA_ENABLE_METHODCALLS */