client.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include <ua_client_subscriptions.h>
  4. #include <ua_client_highlevel.h>
  5. #include <ua_config_default.h>
  6. #include <ua_log_stdout.h>
  7. #ifdef UA_ENABLE_SUBSCRIPTIONS
  8. static void
  9. handler_TheAnswerChanged(UA_Client *client, UA_UInt32 subId, void *subContext,
  10. UA_UInt32 monId, void *monContext, UA_DataValue *value) {
  11. printf("The Answer has changed!\n");
  12. }
  13. #endif
  14. static UA_StatusCode
  15. nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, void *handle) {
  16. if(isInverse)
  17. return UA_STATUSCODE_GOOD;
  18. UA_NodeId *parent = (UA_NodeId *)handle;
  19. printf("%d, %d --- %d ---> NodeId %d, %d\n",
  20. parent->namespaceIndex, parent->identifier.numeric,
  21. referenceTypeId.identifier.numeric, childId.namespaceIndex,
  22. childId.identifier.numeric);
  23. return UA_STATUSCODE_GOOD;
  24. }
  25. int main(int argc, char *argv[]) {
  26. UA_Client *client = UA_Client_new();
  27. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  28. /* Listing endpoints */
  29. UA_EndpointDescription* endpointArray = NULL;
  30. size_t endpointArraySize = 0;
  31. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  32. &endpointArraySize, &endpointArray);
  33. if(retval != UA_STATUSCODE_GOOD) {
  34. UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  35. UA_Client_delete(client);
  36. return (int)retval;
  37. }
  38. printf("%i endpoints found\n", (int)endpointArraySize);
  39. for(size_t i=0;i<endpointArraySize;i++) {
  40. printf("URL of endpoint %i is %.*s\n", (int)i,
  41. (int)endpointArray[i].endpointUrl.length,
  42. endpointArray[i].endpointUrl.data);
  43. }
  44. UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  45. /* Connect to a server */
  46. /* anonymous connect would be: retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); */
  47. retval = UA_Client_connect_username(client, "opc.tcp://localhost:4840", "user1", "password");
  48. if(retval != UA_STATUSCODE_GOOD) {
  49. UA_Client_delete(client);
  50. return (int)retval;
  51. }
  52. /* Browse some objects */
  53. printf("Browsing nodes in objects folder:\n");
  54. UA_BrowseRequest bReq;
  55. UA_BrowseRequest_init(&bReq);
  56. bReq.requestedMaxReferencesPerNode = 0;
  57. bReq.nodesToBrowse = UA_BrowseDescription_new();
  58. bReq.nodesToBrowseSize = 1;
  59. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */
  60. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
  61. UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
  62. printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
  63. for(size_t i = 0; i < bResp.resultsSize; ++i) {
  64. for(size_t j = 0; j < bResp.results[i].referencesSize; ++j) {
  65. UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
  66. if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
  67. printf("%-9d %-16d %-16.*s %-16.*s\n", ref->nodeId.nodeId.namespaceIndex,
  68. ref->nodeId.nodeId.identifier.numeric, (int)ref->browseName.name.length,
  69. ref->browseName.name.data, (int)ref->displayName.text.length,
  70. ref->displayName.text.data);
  71. } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
  72. printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->nodeId.nodeId.namespaceIndex,
  73. (int)ref->nodeId.nodeId.identifier.string.length,
  74. ref->nodeId.nodeId.identifier.string.data,
  75. (int)ref->browseName.name.length, ref->browseName.name.data,
  76. (int)ref->displayName.text.length, ref->displayName.text.data);
  77. }
  78. /* TODO: distinguish further types */
  79. }
  80. }
  81. UA_BrowseRequest_clear(&bReq);
  82. UA_BrowseResponse_clear(&bResp);
  83. /* Same thing, this time using the node iterator... */
  84. UA_NodeId *parent = UA_NodeId_new();
  85. *parent = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  86. UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  87. nodeIter, (void *) parent);
  88. UA_NodeId_delete(parent);
  89. #ifdef UA_ENABLE_SUBSCRIPTIONS
  90. /* Create a subscription */
  91. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  92. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  93. NULL, NULL, NULL);
  94. UA_UInt32 subId = response.subscriptionId;
  95. if(response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  96. printf("Create subscription succeeded, id %u\n", subId);
  97. UA_MonitoredItemCreateRequest monRequest =
  98. UA_MonitoredItemCreateRequest_default(UA_NODEID_STRING(1, "the.answer"));
  99. UA_MonitoredItemCreateResult monResponse =
  100. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  101. UA_TIMESTAMPSTORETURN_BOTH,
  102. monRequest, NULL, handler_TheAnswerChanged, NULL);
  103. if(monResponse.statusCode == UA_STATUSCODE_GOOD)
  104. printf("Monitoring 'the.answer', id %u\n", monResponse.monitoredItemId);
  105. /* The first publish request should return the initial value of the variable */
  106. UA_Client_run_iterate(client, 1000);
  107. #endif
  108. /* Read attribute */
  109. UA_Int32 value = 0;
  110. printf("\nReading the value of node (1, \"the.answer\"):\n");
  111. UA_Variant *val = UA_Variant_new();
  112. retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), val);
  113. if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
  114. val->type == &UA_TYPES[UA_TYPES_INT32]) {
  115. value = *(UA_Int32*)val->data;
  116. printf("the value is: %i\n", value);
  117. }
  118. UA_Variant_delete(val);
  119. /* Write node attribute */
  120. value++;
  121. printf("\nWriting a value of node (1, \"the.answer\"):\n");
  122. UA_WriteRequest wReq;
  123. UA_WriteRequest_init(&wReq);
  124. wReq.nodesToWrite = UA_WriteValue_new();
  125. wReq.nodesToWriteSize = 1;
  126. wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  127. wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
  128. wReq.nodesToWrite[0].value.hasValue = true;
  129. wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
  130. wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; /* do not free the integer on deletion */
  131. wReq.nodesToWrite[0].value.value.data = &value;
  132. UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
  133. if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  134. printf("the new value is: %i\n", value);
  135. UA_WriteRequest_clear(&wReq);
  136. UA_WriteResponse_clear(&wResp);
  137. /* Write node attribute (using the highlevel API) */
  138. value++;
  139. UA_Variant *myVariant = UA_Variant_new();
  140. UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
  141. UA_Client_writeValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), myVariant);
  142. UA_Variant_delete(myVariant);
  143. #ifdef UA_ENABLE_SUBSCRIPTIONS
  144. /* Take another look at the.answer */
  145. UA_Client_run_iterate(client, 100);
  146. /* Delete the subscription */
  147. if(UA_Client_Subscriptions_deleteSingle(client, subId) == UA_STATUSCODE_GOOD)
  148. printf("Subscription removed\n");
  149. #endif
  150. #ifdef UA_ENABLE_METHODCALLS
  151. /* Call a remote method */
  152. UA_Variant input;
  153. UA_String argString = UA_STRING("Hello Server");
  154. UA_Variant_init(&input);
  155. UA_Variant_setScalarCopy(&input, &argString, &UA_TYPES[UA_TYPES_STRING]);
  156. size_t outputSize;
  157. UA_Variant *output;
  158. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  159. UA_NODEID_NUMERIC(1, 62541), 1, &input, &outputSize, &output);
  160. if(retval == UA_STATUSCODE_GOOD) {
  161. printf("Method call was successful, and %lu returned values available.\n",
  162. (unsigned long)outputSize);
  163. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  164. } else {
  165. printf("Method call was unsuccessful, and %x returned values available.\n", retval);
  166. }
  167. UA_Variant_clear(&input);
  168. #endif
  169. #ifdef UA_ENABLE_NODEMANAGEMENT
  170. /* Add new nodes*/
  171. /* New ReferenceType */
  172. UA_NodeId ref_id;
  173. UA_ReferenceTypeAttributes ref_attr = UA_ReferenceTypeAttributes_default;
  174. ref_attr.displayName = UA_LOCALIZEDTEXT("en-US", "NewReference");
  175. ref_attr.description = UA_LOCALIZEDTEXT("en-US", "References something that might or might not exist");
  176. ref_attr.inverseName = UA_LOCALIZEDTEXT("en-US", "IsNewlyReferencedBy");
  177. retval = UA_Client_addReferenceTypeNode(client,
  178. UA_NODEID_NUMERIC(1, 12133),
  179. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  180. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  181. UA_QUALIFIEDNAME(1, "NewReference"),
  182. ref_attr, &ref_id);
  183. if(retval == UA_STATUSCODE_GOOD )
  184. printf("Created 'NewReference' with numeric NodeID %u\n", ref_id.identifier.numeric);
  185. /* New ObjectType */
  186. UA_NodeId objt_id;
  187. UA_ObjectTypeAttributes objt_attr = UA_ObjectTypeAttributes_default;
  188. objt_attr.displayName = UA_LOCALIZEDTEXT("en-US", "TheNewObjectType");
  189. objt_attr.description = UA_LOCALIZEDTEXT("en-US", "Put innovative description here");
  190. retval = UA_Client_addObjectTypeNode(client,
  191. UA_NODEID_NUMERIC(1, 12134),
  192. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  193. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  194. UA_QUALIFIEDNAME(1, "NewObjectType"),
  195. objt_attr, &objt_id);
  196. if(retval == UA_STATUSCODE_GOOD)
  197. printf("Created 'NewObjectType' with numeric NodeID %u\n", objt_id.identifier.numeric);
  198. /* New Object */
  199. UA_NodeId obj_id;
  200. UA_ObjectAttributes obj_attr = UA_ObjectAttributes_default;
  201. obj_attr.displayName = UA_LOCALIZEDTEXT("en-US", "TheNewGreatNode");
  202. obj_attr.description = UA_LOCALIZEDTEXT("de-DE", "Hier koennte Ihre Webung stehen!");
  203. retval = UA_Client_addObjectNode(client,
  204. UA_NODEID_NUMERIC(1, 0),
  205. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  206. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  207. UA_QUALIFIEDNAME(1, "TheGreatNode"),
  208. UA_NODEID_NUMERIC(1, 12134),
  209. obj_attr, &obj_id);
  210. if(retval == UA_STATUSCODE_GOOD )
  211. printf("Created 'NewObject' with numeric NodeID %u\n", obj_id.identifier.numeric);
  212. /* New Integer Variable */
  213. UA_NodeId var_id;
  214. UA_VariableAttributes var_attr = UA_VariableAttributes_default;
  215. var_attr.displayName = UA_LOCALIZEDTEXT("en-US", "TheNewVariableNode");
  216. var_attr.description =
  217. UA_LOCALIZEDTEXT("en-US", "This integer is just amazing - it has digits and everything.");
  218. UA_Int32 int_value = 1234;
  219. /* This does not copy the value */
  220. UA_Variant_setScalar(&var_attr.value, &int_value, &UA_TYPES[UA_TYPES_INT32]);
  221. var_attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  222. retval = UA_Client_addVariableNode(client,
  223. UA_NODEID_NUMERIC(1, 0), // Assign new/random NodeID
  224. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  225. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  226. UA_QUALIFIEDNAME(0, "VariableNode"),
  227. UA_NODEID_NULL, // no variable type
  228. var_attr, &var_id);
  229. if(retval == UA_STATUSCODE_GOOD )
  230. printf("Created 'NewVariable' with numeric NodeID %u\n", var_id.identifier.numeric);
  231. #endif
  232. UA_Client_disconnect(client);
  233. UA_Client_delete(client);
  234. return (int) UA_STATUSCODE_GOOD;
  235. }