client.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #ifdef UA_AMALGAMATE
  2. # include "open62541.h"
  3. # include <string.h>
  4. # include <stdlib.h>
  5. #else
  6. # include "ua_types.h"
  7. # include "ua_client.h"
  8. # include "ua_nodeids.h"
  9. # include "networklayer_tcp.h"
  10. # include "logger_stdout.h"
  11. # include "ua_types_encoding_binary.h"
  12. #endif
  13. #include <stdio.h>
  14. int main(int argc, char *argv[]) {
  15. UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new());
  16. UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect,
  17. "opc.tcp://localhost:16664");
  18. if(retval != UA_STATUSCODE_GOOD) {
  19. UA_Client_delete(client);
  20. return retval;
  21. }
  22. // Browse some objects
  23. printf("Browsing nodes in objects folder:\n");
  24. UA_BrowseRequest bReq;
  25. UA_BrowseRequest_init(&bReq);
  26. bReq.requestedMaxReferencesPerNode = 0;
  27. bReq.nodesToBrowse = UA_BrowseDescription_new();
  28. bReq.nodesToBrowseSize = 1;
  29. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); //browse objects folder
  30. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; //return everything
  31. UA_BrowseResponse bResp = UA_Client_browse(client, &bReq);
  32. printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
  33. for (int i = 0; i < bResp.resultsSize; ++i) {
  34. for (int j = 0; j < bResp.results[i].referencesSize; ++j) {
  35. UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
  36. if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
  37. printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  38. ref->nodeId.nodeId.identifier.numeric, ref->browseName.name.length,
  39. ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  40. } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
  41. printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  42. ref->nodeId.nodeId.identifier.string.length, ref->nodeId.nodeId.identifier.string.data,
  43. ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length,
  44. ref->displayName.text.data);
  45. }
  46. //TODO: distinguish further types
  47. }
  48. }
  49. UA_BrowseRequest_deleteMembers(&bReq);
  50. UA_BrowseResponse_deleteMembers(&bResp);
  51. UA_Int32 value = 0;
  52. // Read node's value
  53. printf("\nReading the value of node (1, \"the.answer\"):\n");
  54. UA_ReadRequest rReq;
  55. UA_ReadRequest_init(&rReq);
  56. rReq.nodesToRead = UA_ReadValueId_new();
  57. rReq.nodesToReadSize = 1;
  58. rReq.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  59. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  60. UA_ReadResponse rResp = UA_Client_read(client, &rReq);
  61. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  62. rResp.resultsSize > 0 && rResp.results[0].hasValue &&
  63. UA_Variant_isScalar(&rResp.results[0].value) &&
  64. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) {
  65. value = *(UA_Int32*)rResp.results[0].value.data;
  66. printf("the value is: %i\n", value);
  67. }
  68. UA_ReadRequest_deleteMembers(&rReq);
  69. UA_ReadResponse_deleteMembers(&rResp);
  70. value++;
  71. // Write node's value
  72. printf("\nWriting a value of node (1, \"the.answer\"):\n");
  73. UA_WriteRequest wReq;
  74. UA_WriteRequest_init(&wReq);
  75. wReq.nodesToWrite = UA_WriteValue_new();
  76. wReq.nodesToWriteSize = 1;
  77. wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  78. wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
  79. wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
  80. wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
  81. wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; //do not free the integer on deletion
  82. wReq.nodesToWrite[0].value.value.data = &value;
  83. UA_WriteResponse wResp = UA_Client_write(client, &wReq);
  84. if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  85. printf("the new value is: %i\n", value);
  86. UA_WriteRequest_deleteMembers(&wReq);
  87. UA_WriteResponse_deleteMembers(&wResp);
  88. #ifdef ENABLE_ADDNODES
  89. /* Create a new object type node */
  90. // New ReferenceType
  91. UA_AddNodesResponse *adResp = UA_Client_createReferenceTypeNode(client,
  92. UA_EXPANDEDNODEID_NUMERIC(1, 12133), // Assign this NodeId (will fail if client is called multiple times)
  93. UA_QUALIFIEDNAME(0, "NewReference"),
  94. UA_LOCALIZEDTEXT("en_US", "TheNewReference"),
  95. UA_LOCALIZEDTEXT("en_US", "References something that might or might not exist."),
  96. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  97. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  98. (UA_UInt32) 0, (UA_UInt32) 0,
  99. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  100. UA_LOCALIZEDTEXT("en_US", "IsNewlyReferencedBy"));
  101. if (adResp->resultsSize > 0 && adResp->results[0].statusCode == UA_STATUSCODE_GOOD ) {
  102. printf("Created 'NewReference' with numeric NodeID %u\n", adResp->results[0].addedNodeId.identifier.numeric );
  103. }
  104. UA_AddNodesResponse_deleteMembers(adResp);
  105. free(adResp);
  106. // New ObjectType
  107. adResp = UA_Client_createObjectTypeNode(client,
  108. UA_EXPANDEDNODEID_NUMERIC(1, 12134), // Assign this NodeId (will fail if client is called multiple times)
  109. UA_QUALIFIEDNAME(0, "NewObjectType"),
  110. UA_LOCALIZEDTEXT("en_US", "TheNewObjectType"),
  111. UA_LOCALIZEDTEXT("en_US", "Put innovative description here."),
  112. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  113. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  114. (UA_UInt32) 0, (UA_UInt32) 0,
  115. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER));
  116. if (adResp->resultsSize > 0 && adResp->results[0].statusCode == UA_STATUSCODE_GOOD ) {
  117. printf("Created 'NewObjectType' with numeric NodeID %u\n", adResp->results[0].addedNodeId.identifier.numeric );
  118. }
  119. // New Object
  120. adResp = UA_Client_createObjectNode(client,
  121. UA_EXPANDEDNODEID_NUMERIC(1, 0), // Assign new/random NodeID
  122. UA_QUALIFIEDNAME(0, "TheNewGreatNodeBrowseName"),
  123. UA_LOCALIZEDTEXT("en_US", "TheNewGreatNode"),
  124. UA_LOCALIZEDTEXT("de_DE", "Hier koennte Ihre Webung stehen!"),
  125. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  126. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  127. (UA_UInt32) 0, (UA_UInt32) 0,
  128. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER));
  129. if (adResp->resultsSize > 0 && adResp->results[0].statusCode == UA_STATUSCODE_GOOD ) {
  130. printf("Created 'NewObject' with numeric NodeID %u\n", adResp->results[0].addedNodeId.identifier.numeric );
  131. }
  132. UA_AddNodesResponse_deleteMembers(adResp);
  133. free(adResp);
  134. // New Integer Variable
  135. UA_Variant *theValue = UA_Variant_new();
  136. UA_Int32 *theValueDate = UA_Int32_new();
  137. *theValueDate = 1234;
  138. theValue->type = &UA_TYPES[UA_TYPES_INT32];
  139. theValue->data = theValueDate;
  140. adResp = UA_Client_createVariableNode(client,
  141. UA_EXPANDEDNODEID_NUMERIC(1, 0), // Assign new/random NodeID
  142. UA_QUALIFIEDNAME(0, "VariableNode"),
  143. UA_LOCALIZEDTEXT("en_US", "TheNewVariableNode"),
  144. UA_LOCALIZEDTEXT("en_US", "This integer is just amazing - it has digits and everything."),
  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_NODEID_NUMERIC(0, UA_NS0ID_INT32),
  150. theValue);
  151. if (adResp->resultsSize > 0 && adResp->results[0].statusCode == UA_STATUSCODE_GOOD ) {
  152. printf("Created 'NewVariable' with numeric NodeID %u\n", adResp->results[0].addedNodeId.identifier.numeric );
  153. }
  154. UA_AddNodesResponse_deleteMembers(adResp);
  155. free(adResp);
  156. free(theValue);
  157. /* Done creating a new node*/
  158. #endif
  159. UA_Client_disconnect(client);
  160. UA_Client_delete(client);
  161. return UA_STATUSCODE_GOOD;
  162. }