client.c 13 KB

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