client.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. static void
  6. handler_TheAnswerChanged(UA_UInt32 monId, UA_DataValue *value, void *context) {
  7. printf("The Answer has changed!\n");
  8. }
  9. static UA_StatusCode
  10. nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, void *handle) {
  11. if(isInverse)
  12. return UA_STATUSCODE_GOOD;
  13. UA_NodeId *parent = (UA_NodeId *)handle;
  14. printf("%d, %d --- %d ---> NodeId %d, %d\n",
  15. parent->namespaceIndex, parent->identifier.numeric,
  16. referenceTypeId.identifier.numeric, childId.namespaceIndex,
  17. childId.identifier.numeric);
  18. return UA_STATUSCODE_GOOD;
  19. }
  20. int main(int argc, char *argv[]) {
  21. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  22. /* Listing endpoints */
  23. UA_EndpointDescription* endpointArray = NULL;
  24. size_t endpointArraySize = 0;
  25. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:16664",
  26. &endpointArraySize, &endpointArray);
  27. if(retval != UA_STATUSCODE_GOOD) {
  28. UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  29. UA_Client_delete(client);
  30. return (int)retval;
  31. }
  32. printf("%i endpoints found\n", (int)endpointArraySize);
  33. for(size_t i=0;i<endpointArraySize;i++){
  34. printf("URL of endpoint %i is %.*s\n", (int)i,
  35. (int)endpointArray[i].endpointUrl.length,
  36. endpointArray[i].endpointUrl.data);
  37. }
  38. UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  39. /* Connect to a server */
  40. /* anonymous connect would be: retval = UA_Client_connect(client, "opc.tcp://localhost:16664"); */
  41. retval = UA_Client_connect_username(client, "opc.tcp://localhost:16664", "user1", "password");
  42. if(retval != UA_STATUSCODE_GOOD) {
  43. UA_Client_delete(client);
  44. return (int)retval;
  45. }
  46. /* Browse some objects */
  47. printf("Browsing nodes in objects folder:\n");
  48. UA_BrowseRequest bReq;
  49. UA_BrowseRequest_init(&bReq);
  50. bReq.requestedMaxReferencesPerNode = 0;
  51. bReq.nodesToBrowse = UA_BrowseDescription_new();
  52. bReq.nodesToBrowseSize = 1;
  53. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */
  54. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
  55. UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
  56. printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
  57. for (size_t i = 0; i < bResp.resultsSize; ++i) {
  58. for (size_t j = 0; j < bResp.results[i].referencesSize; ++j) {
  59. UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
  60. if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
  61. printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  62. ref->nodeId.nodeId.identifier.numeric, (int)ref->browseName.name.length,
  63. ref->browseName.name.data, (int)ref->displayName.text.length,
  64. ref->displayName.text.data);
  65. } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
  66. printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  67. (int)ref->nodeId.nodeId.identifier.string.length,
  68. 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. /* Same thing, this time using the node iterator... */
  78. UA_NodeId *parent = UA_NodeId_new();
  79. *parent = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  80. UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  81. nodeIter, (void *) parent);
  82. UA_NodeId_delete(parent);
  83. #ifdef UA_ENABLE_SUBSCRIPTIONS
  84. /* Create a subscription */
  85. UA_UInt32 subId = 0;
  86. UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_standard, &subId);
  87. if(subId)
  88. printf("Create subscription succeeded, id %u\n", subId);
  89. /* Add a MonitoredItem */
  90. UA_NodeId monitorThis = UA_NODEID_STRING(1, "the.answer");
  91. UA_UInt32 monId = 0;
  92. UA_Client_Subscriptions_addMonitoredItem(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE,
  93. &handler_TheAnswerChanged, NULL, &monId);
  94. if (monId)
  95. printf("Monitoring 'the.answer', id %u\n", subId);
  96. /* The first publish request should return the initial value of the variable */
  97. UA_Client_Subscriptions_manuallySendPublishRequest(client);
  98. #endif
  99. /* Read attribute */
  100. UA_Int32 value = 0;
  101. printf("\nReading the value of node (1, \"the.answer\"):\n");
  102. UA_Variant *val = UA_Variant_new();
  103. retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), val);
  104. if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
  105. val->type == &UA_TYPES[UA_TYPES_INT32]) {
  106. value = *(UA_Int32*)val->data;
  107. printf("the value is: %i\n", value);
  108. }
  109. UA_Variant_delete(val);
  110. /* Write node attribute */
  111. value++;
  112. printf("\nWriting a value of node (1, \"the.answer\"):\n");
  113. UA_WriteRequest wReq;
  114. UA_WriteRequest_init(&wReq);
  115. wReq.nodesToWrite = UA_WriteValue_new();
  116. wReq.nodesToWriteSize = 1;
  117. wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  118. wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
  119. wReq.nodesToWrite[0].value.hasValue = true;
  120. wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
  121. wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; /* do not free the integer on deletion */
  122. wReq.nodesToWrite[0].value.value.data = &value;
  123. UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
  124. if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  125. printf("the new value is: %i\n", value);
  126. UA_WriteRequest_deleteMembers(&wReq);
  127. UA_WriteResponse_deleteMembers(&wResp);
  128. /* Write node attribute (using the highlevel API) */
  129. value++;
  130. UA_Variant *myVariant = UA_Variant_new();
  131. UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
  132. UA_Client_writeValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), myVariant);
  133. UA_Variant_delete(myVariant);
  134. #ifdef UA_ENABLE_SUBSCRIPTIONS
  135. /* Take another look at the.answer */
  136. UA_Client_Subscriptions_manuallySendPublishRequest(client);
  137. /* Delete the subscription */
  138. if(!UA_Client_Subscriptions_remove(client, subId))
  139. printf("Subscription removed\n");
  140. #endif
  141. #ifdef UA_ENABLE_METHODCALLS
  142. /* Call a remote method */
  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 %lu returned values available.\n",
  153. (unsigned long)outputSize);
  154. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  155. } else {
  156. printf("Method call was unsuccessfull, and %x returned values available.\n", retval);
  157. }
  158. UA_Variant_deleteMembers(&input);
  159. #endif
  160. #ifdef UA_ENABLE_NODEMANAGEMENT
  161. /* Add new nodes*/
  162. /* New ReferenceType */
  163. UA_NodeId ref_id;
  164. UA_ReferenceTypeAttributes ref_attr;
  165. UA_ReferenceTypeAttributes_init(&ref_attr);
  166. ref_attr.displayName = UA_LOCALIZEDTEXT("en_US", "NewReference");
  167. ref_attr.description = UA_LOCALIZEDTEXT("en_US", "References something that might or might not exist");
  168. ref_attr.inverseName = UA_LOCALIZEDTEXT("en_US", "IsNewlyReferencedBy");
  169. retval = UA_Client_addReferenceTypeNode(client,
  170. UA_NODEID_NUMERIC(1, 12133),
  171. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  172. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  173. UA_QUALIFIEDNAME(1, "NewReference"),
  174. ref_attr, &ref_id);
  175. if(retval == UA_STATUSCODE_GOOD )
  176. printf("Created 'NewReference' with numeric NodeID %u\n", ref_id.identifier.numeric);
  177. /* New ObjectType */
  178. UA_NodeId objt_id;
  179. UA_ObjectTypeAttributes objt_attr;
  180. UA_ObjectTypeAttributes_init(&objt_attr);
  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;
  194. UA_ObjectAttributes_init(&obj_attr);
  195. obj_attr.displayName = UA_LOCALIZEDTEXT("en_US", "TheNewGreatNode");
  196. obj_attr.description = UA_LOCALIZEDTEXT("de_DE", "Hier koennte Ihre Webung stehen!");
  197. retval = UA_Client_addObjectNode(client,
  198. UA_NODEID_NUMERIC(1, 0),
  199. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  200. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  201. UA_QUALIFIEDNAME(1, "TheGreatNode"),
  202. UA_NODEID_NUMERIC(1, 12134),
  203. obj_attr, &obj_id);
  204. if(retval == UA_STATUSCODE_GOOD )
  205. printf("Created 'NewObject' with numeric NodeID %u\n", obj_id.identifier.numeric);
  206. /* New Integer Variable */
  207. UA_NodeId var_id;
  208. UA_VariableAttributes var_attr;
  209. UA_VariableAttributes_init(&var_attr);
  210. var_attr.displayName = UA_LOCALIZEDTEXT("en_US", "TheNewVariableNode");
  211. var_attr.description =
  212. UA_LOCALIZEDTEXT("en_US", "This integer is just amazing - it has digits and everything.");
  213. UA_Int32 int_value = 1234;
  214. /* This does not copy the value */
  215. UA_Variant_setScalar(&var_attr.value, &int_value, &UA_TYPES[UA_TYPES_INT32]);
  216. var_attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  217. retval = UA_Client_addVariableNode(client,
  218. UA_NODEID_NUMERIC(1, 0), // Assign new/random NodeID
  219. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  220. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  221. UA_QUALIFIEDNAME(0, "VariableNode"),
  222. UA_NODEID_NULL, // no variable type
  223. var_attr, &var_id);
  224. if(retval == UA_STATUSCODE_GOOD )
  225. printf("Created 'NewVariable' with numeric NodeID %u\n", var_id.identifier.numeric);
  226. #endif
  227. UA_Client_disconnect(client);
  228. UA_Client_delete(client);
  229. return (int) UA_STATUSCODE_GOOD;
  230. }