client.c 13 KB

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