client.c 10 KB

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