client.c 13 KB

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