client.c 12 KB

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