client.c 12 KB

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