client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifdef UA_NO_AMALGAMATION
  2. # include "ua_types.h"
  3. # include "ua_client.h"
  4. # include "ua_nodeids.h"
  5. # include "networklayer_tcp.h"
  6. # include "logger_stdout.h"
  7. # include "ua_types_encoding_binary.h"
  8. #else
  9. # include "open62541.h"
  10. # include <string.h>
  11. # include <stdlib.h>
  12. #endif
  13. #include <stdio.h>
  14. void handler_TheAnswerChanged(UA_UInt32 handle, UA_DataValue *value);
  15. void handler_TheAnswerChanged(UA_UInt32 handle, UA_DataValue *value) {
  16. printf("The Answer has changed!\n");
  17. return;
  18. }
  19. UA_StatusCode nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId);
  20. UA_StatusCode nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId) {
  21. printf("References ns=%d;i=%d using i=%d ", childId.namespaceIndex, childId.identifier.numeric, referenceTypeId.identifier.numeric);
  22. if (isInverse == UA_TRUE) {
  23. printf(" (inverse)");
  24. }
  25. printf("\n");
  26. return UA_STATUSCODE_GOOD;
  27. }
  28. int main(int argc, char *argv[]) {
  29. UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new());
  30. UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect,
  31. "opc.tcp://localhost:16664");
  32. if(retval != UA_STATUSCODE_GOOD) {
  33. UA_Client_delete(client);
  34. return retval;
  35. }
  36. // Browse some objects
  37. printf("Browsing nodes in objects folder:\n");
  38. UA_BrowseRequest bReq;
  39. UA_BrowseRequest_init(&bReq);
  40. bReq.requestedMaxReferencesPerNode = 0;
  41. bReq.nodesToBrowse = UA_BrowseDescription_new();
  42. bReq.nodesToBrowseSize = 1;
  43. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); //browse objects folder
  44. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; //return everything
  45. UA_BrowseResponse bResp = UA_Client_browse(client, &bReq);
  46. printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
  47. for (int i = 0; i < bResp.resultsSize; ++i) {
  48. for (int j = 0; j < bResp.results[i].referencesSize; ++j) {
  49. UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
  50. if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
  51. printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  52. ref->nodeId.nodeId.identifier.numeric, ref->browseName.name.length,
  53. ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  54. } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
  55. printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  56. ref->nodeId.nodeId.identifier.string.length, ref->nodeId.nodeId.identifier.string.data,
  57. ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length,
  58. ref->displayName.text.data);
  59. }
  60. //TODO: distinguish further types
  61. }
  62. }
  63. UA_BrowseRequest_deleteMembers(&bReq);
  64. UA_BrowseResponse_deleteMembers(&bResp);
  65. #ifdef ENABLE_SUBSCRIPTIONS
  66. // Create a subscription with interval 0 (immediate)...
  67. UA_Int32 subId = UA_Client_newSubscription(client, 0);
  68. if (subId)
  69. printf("Create subscription succeeded, id %u\n", subId);
  70. // .. and monitor TheAnswer
  71. UA_NodeId monitorThis;
  72. monitorThis = UA_NODEID_STRING_ALLOC(1, "the.answer");
  73. UA_UInt32 monId = UA_Client_monitorItemChanges(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE, &handler_TheAnswerChanged );
  74. if (monId)
  75. printf("Monitoring 'the.answer', id %u\n", subId);
  76. UA_NodeId_deleteMembers(&monitorThis);
  77. // First Publish always generates data (current value) and call out handler.
  78. UA_Client_doPublish(client);
  79. // This should not generate anything
  80. UA_Client_doPublish(client);
  81. #endif
  82. UA_Int32 value = 0;
  83. // Read node's value
  84. printf("\nReading the value of node (1, \"the.answer\"):\n");
  85. UA_ReadRequest rReq;
  86. UA_ReadRequest_init(&rReq);
  87. rReq.nodesToRead = UA_ReadValueId_new();
  88. rReq.nodesToReadSize = 1;
  89. rReq.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  90. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  91. UA_ReadResponse rResp = UA_Client_read(client, &rReq);
  92. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  93. rResp.resultsSize > 0 && rResp.results[0].hasValue &&
  94. UA_Variant_isScalar(&rResp.results[0].value) &&
  95. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) {
  96. value = *(UA_Int32*)rResp.results[0].value.data;
  97. printf("the value is: %i\n", value);
  98. }
  99. UA_ReadRequest_deleteMembers(&rReq);
  100. UA_ReadResponse_deleteMembers(&rResp);
  101. value++;
  102. // Write node's value
  103. printf("\nWriting a value of node (1, \"the.answer\"):\n");
  104. UA_WriteRequest wReq;
  105. UA_WriteRequest_init(&wReq);
  106. wReq.nodesToWrite = UA_WriteValue_new();
  107. wReq.nodesToWriteSize = 1;
  108. wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  109. wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
  110. wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
  111. wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
  112. wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; //do not free the integer on deletion
  113. wReq.nodesToWrite[0].value.value.data = &value;
  114. UA_WriteResponse wResp = UA_Client_write(client, &wReq);
  115. if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  116. printf("the new value is: %i\n", value);
  117. UA_WriteRequest_deleteMembers(&wReq);
  118. UA_WriteResponse_deleteMembers(&wResp);
  119. #ifdef ENABLE_SUBSCRIPTIONS
  120. // Take another look at the.answer... this should call the handler.
  121. UA_Client_doPublish(client);
  122. // Delete our subscription (which also unmonitors all items)
  123. if(!UA_Client_removeSubscription(client, subId))
  124. printf("Subscription removed\n");
  125. #endif
  126. #ifdef ENABLE_METHODCALLS
  127. /* Note: This example requires Namespace 0 Node 11489 (ServerType -> GetMonitoredItems)
  128. FIXME: Provide a namespace 0 independant example on the server side
  129. */
  130. UA_Variant input;
  131. UA_String argString = UA_STRING("Hello Server");
  132. UA_Variant_init(&input);
  133. UA_Variant_setScalarCopy(&input, &argString, &UA_TYPES[UA_TYPES_STRING]);
  134. UA_Int32 outputSize;
  135. UA_Variant *output;
  136. retval = UA_Client_CallServerMethod(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  137. UA_NODEID_NUMERIC(1, 62541), 1, &input, &outputSize, &output);
  138. if(retval == UA_STATUSCODE_GOOD) {
  139. printf("Method call was successfull, and %i returned values available.\n", outputSize);
  140. UA_Array_delete(output, &UA_TYPES[UA_TYPES_VARIANT], outputSize);
  141. } else {
  142. printf("Method call was unsuccessfull, and %x returned values available.\n", retval);
  143. }
  144. UA_Variant_deleteMembers(&input);
  145. #endif
  146. #ifdef ENABLE_ADDNODES
  147. /* Create a new object type node */
  148. // New ReferenceType
  149. UA_StatusCode addRes;
  150. UA_NodeId retNodeId;
  151. addRes = UA_Client_addReferenceTypeNode(client,
  152. UA_NODEID_NUMERIC(1, 12133), // Assign this NodeId (will fail if client is called multiple times)
  153. UA_QUALIFIEDNAME(0, "NewReference"),
  154. UA_LOCALIZEDTEXT("en_US", "TheNewReference"),
  155. UA_LOCALIZEDTEXT("en_US", "References something that might or might not exist."),
  156. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  157. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  158. (UA_UInt32) 0, (UA_UInt32) 0,
  159. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  160. UA_LOCALIZEDTEXT("en_US", "IsNewlyReferencedBy"),
  161. &retNodeId);
  162. if (addRes == UA_STATUSCODE_GOOD ) {
  163. printf("Created 'NewReference' with numeric NodeID %u\n", retNodeId.identifier.numeric );
  164. }
  165. // New ObjectType
  166. addRes = UA_Client_addObjectTypeNode(client,
  167. UA_NODEID_NUMERIC(1, 12134), // Assign this NodeId (will fail if client is called multiple times)
  168. UA_QUALIFIEDNAME(0, "NewObjectType"),
  169. UA_LOCALIZEDTEXT("en_US", "TheNewObjectType"),
  170. UA_LOCALIZEDTEXT("en_US", "Put innovative description here."),
  171. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  172. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  173. (UA_UInt32) 0, (UA_UInt32) 0,
  174. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  175. UA_FALSE,
  176. &retNodeId);
  177. if (addRes == UA_STATUSCODE_GOOD ) {
  178. printf("Created 'NewObjectType' with numeric NodeID %u\n", retNodeId.identifier.numeric );
  179. }
  180. // New Object
  181. addRes = UA_Client_addObjectNode(client,
  182. UA_NODEID_NUMERIC(1, 0), // Assign new/random NodeID
  183. UA_QUALIFIEDNAME(0, "TheNewGreatNodeBrowseName"),
  184. UA_LOCALIZEDTEXT("en_US", "TheNewGreatNode"),
  185. UA_LOCALIZEDTEXT("de_DE", "Hier koennte Ihre Webung stehen!"),
  186. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  187. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  188. (UA_UInt32) 0, (UA_UInt32) 0,
  189. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  190. &retNodeId);
  191. if (addRes == UA_STATUSCODE_GOOD ) {
  192. printf("Created 'NewObject' with numeric NodeID %u\n", retNodeId.identifier.numeric );
  193. }
  194. // New Integer Variable
  195. UA_Variant *theValue = UA_Variant_new();
  196. UA_Int32 *theValueDate = UA_Int32_new();
  197. *theValueDate = 1234;
  198. theValue->type = &UA_TYPES[UA_TYPES_INT32];
  199. theValue->data = theValueDate;
  200. addRes = UA_Client_addVariableNode(client,
  201. UA_NODEID_NUMERIC(1, 0), // Assign new/random NodeID
  202. UA_QUALIFIEDNAME(0, "VariableNode"),
  203. UA_LOCALIZEDTEXT("en_US", "TheNewVariableNode"),
  204. UA_LOCALIZEDTEXT("en_US", "This integer is just amazing - it has digits and everything."),
  205. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  206. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  207. (UA_UInt32) 0, (UA_UInt32) 0,
  208. theValue,
  209. &retNodeId);
  210. if (addRes == UA_STATUSCODE_GOOD ) {
  211. printf("Created 'NewVariable' with numeric NodeID %u\n", retNodeId.identifier.numeric );
  212. }
  213. free(theValue);
  214. /* Done creating a new node*/
  215. #endif
  216. UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), nodeIter);
  217. UA_Client_disconnect(client);
  218. UA_Client_delete(client);
  219. return UA_STATUSCODE_GOOD;
  220. }