client.c 12 KB

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