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