client.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_UInt32 monId, UA_DataValue *value, void *context) {
  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_deleteMembers(&bReq);
  78. UA_BrowseResponse_deleteMembers(&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_UInt32 subId = 0;
  88. UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  89. if(subId)
  90. printf("Create subscription succeeded, id %u\n", subId);
  91. /* Add a MonitoredItem */
  92. UA_NodeId monitorThis = UA_NODEID_STRING(1, "the.answer");
  93. UA_UInt32 monId = 0;
  94. UA_Client_Subscriptions_addMonitoredItem(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE,
  95. &handler_TheAnswerChanged, NULL, &monId, 250);
  96. if (monId)
  97. printf("Monitoring 'the.answer', id %u\n", subId);
  98. /* The first publish request should return the initial value of the variable */
  99. UA_Client_Subscriptions_manuallySendPublishRequest(client);
  100. #endif
  101. /* Read attribute */
  102. UA_Int32 value = 0;
  103. printf("\nReading the value of node (1, \"the.answer\"):\n");
  104. UA_Variant *val = UA_Variant_new();
  105. retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), val);
  106. if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
  107. val->type == &UA_TYPES[UA_TYPES_INT32]) {
  108. value = *(UA_Int32*)val->data;
  109. printf("the value is: %i\n", value);
  110. }
  111. UA_Variant_delete(val);
  112. /* Write node attribute */
  113. value++;
  114. printf("\nWriting a value of node (1, \"the.answer\"):\n");
  115. UA_WriteRequest wReq;
  116. UA_WriteRequest_init(&wReq);
  117. wReq.nodesToWrite = UA_WriteValue_new();
  118. wReq.nodesToWriteSize = 1;
  119. wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  120. wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
  121. wReq.nodesToWrite[0].value.hasValue = true;
  122. wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
  123. wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; /* do not free the integer on deletion */
  124. wReq.nodesToWrite[0].value.value.data = &value;
  125. UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
  126. if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  127. printf("the new value is: %i\n", value);
  128. UA_WriteRequest_deleteMembers(&wReq);
  129. UA_WriteResponse_deleteMembers(&wResp);
  130. /* Write node attribute (using the highlevel API) */
  131. value++;
  132. UA_Variant *myVariant = UA_Variant_new();
  133. UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
  134. UA_Client_writeValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), myVariant);
  135. UA_Variant_delete(myVariant);
  136. #ifdef UA_ENABLE_SUBSCRIPTIONS
  137. /* Take another look at the.answer */
  138. UA_Client_Subscriptions_manuallySendPublishRequest(client);
  139. /* Delete the subscription */
  140. if(!UA_Client_Subscriptions_remove(client, subId))
  141. printf("Subscription removed\n");
  142. #endif
  143. #ifdef UA_ENABLE_METHODCALLS
  144. /* Call a remote method */
  145. UA_Variant input;
  146. UA_String argString = UA_STRING("Hello Server");
  147. UA_Variant_init(&input);
  148. UA_Variant_setScalarCopy(&input, &argString, &UA_TYPES[UA_TYPES_STRING]);
  149. size_t outputSize;
  150. UA_Variant *output;
  151. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  152. UA_NODEID_NUMERIC(1, 62541), 1, &input, &outputSize, &output);
  153. if(retval == UA_STATUSCODE_GOOD) {
  154. printf("Method call was successfull, and %lu returned values available.\n",
  155. (unsigned long)outputSize);
  156. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  157. } else {
  158. printf("Method call was unsuccessfull, and %x returned values available.\n", retval);
  159. }
  160. UA_Variant_deleteMembers(&input);
  161. #endif
  162. #ifdef UA_ENABLE_NODEMANAGEMENT
  163. /* Add new nodes*/
  164. /* New ReferenceType */
  165. UA_NodeId ref_id;
  166. UA_ReferenceTypeAttributes ref_attr = UA_ReferenceTypeAttributes_default;
  167. ref_attr.displayName = UA_LOCALIZEDTEXT("en-US", "NewReference");
  168. ref_attr.description = UA_LOCALIZEDTEXT("en-US", "References something that might or might not exist");
  169. ref_attr.inverseName = UA_LOCALIZEDTEXT("en-US", "IsNewlyReferencedBy");
  170. retval = UA_Client_addReferenceTypeNode(client,
  171. UA_NODEID_NUMERIC(1, 12133),
  172. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  173. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  174. UA_QUALIFIEDNAME(1, "NewReference"),
  175. ref_attr, &ref_id);
  176. if(retval == UA_STATUSCODE_GOOD )
  177. printf("Created 'NewReference' with numeric NodeID %u\n", ref_id.identifier.numeric);
  178. /* New ObjectType */
  179. UA_NodeId objt_id;
  180. UA_ObjectTypeAttributes objt_attr = UA_ObjectTypeAttributes_default;
  181. objt_attr.displayName = UA_LOCALIZEDTEXT("en-US", "TheNewObjectType");
  182. objt_attr.description = UA_LOCALIZEDTEXT("en-US", "Put innovative description here");
  183. retval = UA_Client_addObjectTypeNode(client,
  184. UA_NODEID_NUMERIC(1, 12134),
  185. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  186. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  187. UA_QUALIFIEDNAME(1, "NewObjectType"),
  188. objt_attr, &objt_id);
  189. if(retval == UA_STATUSCODE_GOOD)
  190. printf("Created 'NewObjectType' with numeric NodeID %u\n", objt_id.identifier.numeric);
  191. /* New Object */
  192. UA_NodeId obj_id;
  193. UA_ObjectAttributes obj_attr = UA_ObjectAttributes_default;
  194. obj_attr.displayName = UA_LOCALIZEDTEXT("en-US", "TheNewGreatNode");
  195. obj_attr.description = UA_LOCALIZEDTEXT("de-DE", "Hier koennte Ihre Webung stehen!");
  196. retval = UA_Client_addObjectNode(client,
  197. UA_NODEID_NUMERIC(1, 0),
  198. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  199. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  200. UA_QUALIFIEDNAME(1, "TheGreatNode"),
  201. UA_NODEID_NUMERIC(1, 12134),
  202. obj_attr, &obj_id);
  203. if(retval == UA_STATUSCODE_GOOD )
  204. printf("Created 'NewObject' with numeric NodeID %u\n", obj_id.identifier.numeric);
  205. /* New Integer Variable */
  206. UA_NodeId var_id;
  207. UA_VariableAttributes var_attr = UA_VariableAttributes_default;
  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. }