client.c 12 KB

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