ua_services_method.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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->refTargetsSize; ++j) {
  28. const UA_Node *refTarget =
  29. UA_Nodestore_getNode(server->nsCtx, &rk->refTargets[j].target.nodeId);
  30. if(!refTarget)
  31. continue;
  32. if(refTarget->nodeClass == UA_NODECLASS_VARIABLE &&
  33. refTarget->browseName.namespaceIndex == 0 &&
  34. UA_String_equal(&withBrowseName, &refTarget->browseName.name)) {
  35. return (const UA_VariableNode*)refTarget;
  36. }
  37. UA_Nodestore_releaseNode(server->nsCtx, refTarget);
  38. }
  39. }
  40. return NULL;
  41. }
  42. /* inputArgumentResults has the length request->inputArgumentsSize */
  43. static UA_StatusCode
  44. typeCheckArguments(UA_Server *server, UA_Session *session,
  45. const UA_VariableNode *argRequirements, size_t argsSize,
  46. UA_Variant *args, UA_StatusCode *inputArgumentResults) {
  47. /* Verify that we have a Variant containing UA_Argument (scalar or array) in
  48. * the "InputArguments" node */
  49. if(argRequirements->valueSource != UA_VALUESOURCE_DATA)
  50. return UA_STATUSCODE_BADINTERNALERROR;
  51. if(!argRequirements->value.data.value.hasValue)
  52. return UA_STATUSCODE_BADINTERNALERROR;
  53. if(argRequirements->value.data.value.value.type != &UA_TYPES[UA_TYPES_ARGUMENT])
  54. return UA_STATUSCODE_BADINTERNALERROR;
  55. /* Verify the number of arguments. A scalar argument value is interpreted as
  56. * an array of length 1. */
  57. size_t argReqsSize = argRequirements->value.data.value.value.arrayLength;
  58. if(UA_Variant_isScalar(&argRequirements->value.data.value.value))
  59. argReqsSize = 1;
  60. if(argReqsSize > argsSize)
  61. return UA_STATUSCODE_BADARGUMENTSMISSING;
  62. if(argReqsSize < argsSize)
  63. return UA_STATUSCODE_BADTOOMANYARGUMENTS;
  64. /* Type-check every argument against the definition */
  65. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  66. UA_Argument *argReqs = (UA_Argument*)argRequirements->value.data.value.value.data;
  67. for(size_t i = 0; i < argReqsSize; ++i) {
  68. if(!compatibleValue(server, session, &argReqs[i].dataType, argReqs[i].valueRank,
  69. argReqs[i].arrayDimensionsSize, argReqs[i].arrayDimensions,
  70. &args[i], NULL)) {
  71. inputArgumentResults[i] = UA_STATUSCODE_BADTYPEMISMATCH;
  72. retval = UA_STATUSCODE_BADINVALIDARGUMENT;
  73. }
  74. }
  75. return retval;
  76. }
  77. /* inputArgumentResults has the length request->inputArgumentsSize */
  78. static UA_StatusCode
  79. validMethodArguments(UA_Server *server, UA_Session *session, const UA_MethodNode *method,
  80. const UA_CallMethodRequest *request,
  81. UA_StatusCode *inputArgumentResults) {
  82. /* Get the input arguments node */
  83. const UA_VariableNode *inputArguments =
  84. getArgumentsVariableNode(server, method, UA_STRING("InputArguments"));
  85. if(!inputArguments) {
  86. if(request->inputArgumentsSize > 0)
  87. return UA_STATUSCODE_BADTOOMANYARGUMENTS;
  88. return UA_STATUSCODE_GOOD;
  89. }
  90. /* Verify the request */
  91. UA_StatusCode retval = typeCheckArguments(server, session, inputArguments,
  92. request->inputArgumentsSize,
  93. request->inputArguments,
  94. inputArgumentResults);
  95. /* Release the input arguments node */
  96. UA_Nodestore_releaseNode(server->nsCtx, (const UA_Node*)inputArguments);
  97. return retval;
  98. }
  99. static const UA_NodeId hasComponentNodeId = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASCOMPONENT}};
  100. static const UA_NodeId hasSubTypeNodeId = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASSUBTYPE}};
  101. static void
  102. callWithMethodAndObject(UA_Server *server, UA_Session *session,
  103. const UA_CallMethodRequest *request, UA_CallMethodResult *result,
  104. const UA_MethodNode *method, const UA_ObjectNode *object) {
  105. /* Verify the object's NodeClass */
  106. if(object->nodeClass != UA_NODECLASS_OBJECT &&
  107. object->nodeClass != UA_NODECLASS_OBJECTTYPE) {
  108. result->statusCode = UA_STATUSCODE_BADNODECLASSINVALID;
  109. return;
  110. }
  111. /* Verify the method's NodeClass */
  112. if(method->nodeClass != UA_NODECLASS_METHOD) {
  113. result->statusCode = UA_STATUSCODE_BADNODECLASSINVALID;
  114. return;
  115. }
  116. /* Is there a method to execute? */
  117. if(!method->method) {
  118. result->statusCode = UA_STATUSCODE_BADINTERNALERROR;
  119. return;
  120. }
  121. /* Verify method/object relations. Object must have a hasComponent or a
  122. * subtype of hasComponent reference to the method node. Therefore, check
  123. * every reference between the parent object and the method node if there is
  124. * a hasComponent (or subtype) reference */
  125. UA_Boolean found = false;
  126. for(size_t i = 0; i < object->referencesSize && !found; ++i) {
  127. UA_NodeReferenceKind *rk = &object->references[i];
  128. if(rk->isInverse)
  129. continue;
  130. if(!isNodeInTree(server->nsCtx, &rk->referenceTypeId,
  131. &hasComponentNodeId, &hasSubTypeNodeId, 1))
  132. continue;
  133. for(size_t j = 0; j < rk->refTargetsSize; ++j) {
  134. if(UA_NodeId_equal(&rk->refTargets[j].target.nodeId, &request->methodId)) {
  135. found = true;
  136. break;
  137. }
  138. }
  139. }
  140. if(!found) {
  141. result->statusCode = UA_STATUSCODE_BADMETHODINVALID;
  142. return;
  143. }
  144. /* Verify access rights */
  145. UA_Boolean executable = method->executable;
  146. if(session != &server->adminSession)
  147. executable = executable &&
  148. server->config.accessControl.getUserExecutableOnObject(server,
  149. &server->config.accessControl, &session->sessionId,
  150. session->sessionHandle, &request->methodId, method->context,
  151. &request->objectId, object->context);
  152. if(!executable) {
  153. result->statusCode = UA_STATUSCODE_BADNOTEXECUTABLE;
  154. return;
  155. }
  156. /* Allocate the inputArgumentResults array */
  157. result->inputArgumentResults = (UA_StatusCode*)
  158. UA_Array_new(request->inputArgumentsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  159. if(!result->inputArgumentResults) {
  160. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  161. return;
  162. }
  163. result->inputArgumentResultsSize = request->inputArgumentsSize;
  164. /* Verify Input Arguments */
  165. result->statusCode = validMethodArguments(server, session, method, request, result->inputArgumentResults);
  166. /* Return inputArgumentResults only for BADINVALIDARGUMENT */
  167. if(result->statusCode != UA_STATUSCODE_BADINVALIDARGUMENT) {
  168. UA_Array_delete(result->inputArgumentResults, result->inputArgumentResultsSize,
  169. &UA_TYPES[UA_TYPES_STATUSCODE]);
  170. result->inputArgumentResults = NULL;
  171. result->inputArgumentResultsSize = 0;
  172. }
  173. /* Error during type-checking? */
  174. if(result->statusCode != UA_STATUSCODE_GOOD)
  175. return;
  176. /* Get the output arguments node */
  177. const UA_VariableNode *outputArguments =
  178. getArgumentsVariableNode(server, method, UA_STRING("OutputArguments"));
  179. /* Allocate the output arguments array */
  180. size_t outputArgsSize = 0;
  181. if(outputArguments)
  182. outputArgsSize = outputArguments->value.data.value.value.arrayLength;
  183. result->outputArguments = (UA_Variant*)
  184. UA_Array_new(outputArgsSize, &UA_TYPES[UA_TYPES_VARIANT]);
  185. if(!result->outputArguments) {
  186. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  187. return;
  188. }
  189. result->outputArgumentsSize = outputArgsSize;
  190. /* Release the output arguments node */
  191. UA_Nodestore_releaseNode(server->nsCtx, (const UA_Node*)outputArguments);
  192. /* Call the method */
  193. result->statusCode = method->method(server, &session->sessionId, session->sessionHandle,
  194. &method->nodeId, method->context,
  195. &object->nodeId, object->context,
  196. request->inputArgumentsSize, request->inputArguments,
  197. result->outputArgumentsSize, result->outputArguments);
  198. /* TODO: Verify Output matches the argument definition */
  199. }
  200. static void
  201. Operation_CallMethod(UA_Server *server, UA_Session *session, void *context,
  202. const UA_CallMethodRequest *request, UA_CallMethodResult *result) {
  203. /* Get the method node */
  204. const UA_MethodNode *method = (const UA_MethodNode*)
  205. UA_Nodestore_getNode(server->nsCtx, &request->methodId);
  206. if(!method) {
  207. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  208. return;
  209. }
  210. /* Get the object node */
  211. const UA_ObjectNode *object = (const UA_ObjectNode*)
  212. UA_Nodestore_getNode(server->nsCtx, &request->objectId);
  213. if(!object) {
  214. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  215. UA_Nodestore_releaseNode(server->nsCtx, (const UA_Node*)method);
  216. return;
  217. }
  218. /* Continue with method and object as context */
  219. callWithMethodAndObject(server, session, request, result, method, object);
  220. /* Release the method and object node */
  221. UA_Nodestore_releaseNode(server->nsCtx, (const UA_Node*)method);
  222. UA_Nodestore_releaseNode(server->nsCtx, (const UA_Node*)object);
  223. }
  224. void Service_Call(UA_Server *server, UA_Session *session,
  225. const UA_CallRequest *request,
  226. UA_CallResponse *response) {
  227. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  228. "Processing CallRequest");
  229. if(server->config.maxNodesPerMethodCall != 0 &&
  230. request->methodsToCallSize > server->config.maxNodesPerMethodCall) {
  231. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  232. return;
  233. }
  234. response->responseHeader.serviceResult =
  235. UA_Server_processServiceOperations(server, session, (UA_ServiceOperation)Operation_CallMethod, NULL,
  236. &request->methodsToCallSize, &UA_TYPES[UA_TYPES_CALLMETHODREQUEST],
  237. &response->resultsSize, &UA_TYPES[UA_TYPES_CALLMETHODRESULT]);
  238. }
  239. UA_CallMethodResult UA_EXPORT
  240. UA_Server_call(UA_Server *server, const UA_CallMethodRequest *request) {
  241. UA_CallMethodResult result;
  242. UA_CallMethodResult_init(&result);
  243. Operation_CallMethod(server, &server->adminSession, NULL, request, &result);
  244. return result;
  245. }
  246. #endif /* UA_ENABLE_METHODCALLS */