client.c 12 KB

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