client_connect.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <open62541/client_config_default.h>
  4. #include <open62541/client_highlevel.h>
  5. #include <open62541/client_subscriptions.h>
  6. #include <open62541/plugin/log_stdout.h>
  7. #include "common.h"
  8. static void usage(void) {
  9. printf("Usage: client [-username name] [-password password] ");
  10. #ifdef UA_ENABLE_ENCRYPTION
  11. printf("[-cert certfile.der] [-key keyfile.der] "
  12. "[-securityMode <0-3>] [-securityPolicy policyUri] ");
  13. #endif
  14. printf("opc.tcp://<host>:<port>\n");
  15. }
  16. int main(int argc, char *argv[]) {
  17. UA_String securityPolicyUri = UA_STRING_NULL;
  18. UA_MessageSecurityMode securityMode = UA_MESSAGESECURITYMODE_INVALID; /* allow everything */
  19. char *serverurl = NULL;
  20. char *username = NULL;
  21. char *password = NULL;
  22. #ifdef UA_ENABLE_ENCRYPTION
  23. char *certfile = NULL;
  24. char *keyfile = NULL;
  25. #endif
  26. /* At least one argument is required for the server uri */
  27. if(argc <= 1) {
  28. usage();
  29. return EXIT_FAILURE;
  30. }
  31. /* Parse the arguments */
  32. for(int argpos = 1; argpos < argc; argpos++) {
  33. if(strcmp(argv[argpos], "--help") == 0 ||
  34. strcmp(argv[argpos], "-h") == 0) {
  35. usage();
  36. return EXIT_SUCCESS;
  37. }
  38. if(argpos + 1 == argc) {
  39. serverurl = argv[argpos];
  40. continue;
  41. }
  42. if(strcmp(argv[argpos], "-username") == 0) {
  43. argpos++;
  44. username = argv[argpos];
  45. continue;
  46. }
  47. if(strcmp(argv[argpos], "-password") == 0) {
  48. argpos++;
  49. password = argv[argpos];
  50. continue;
  51. }
  52. #ifdef UA_ENABLE_ENCRYPTION
  53. if(strcmp(argv[argpos], "-cert") == 0) {
  54. argpos++;
  55. certfile = argv[argpos];
  56. continue;
  57. }
  58. if(strcmp(argv[argpos], "-key") == 0) {
  59. argpos++;
  60. keyfile = argv[argpos];
  61. continue;
  62. }
  63. if(strcmp(argv[argpos], "-securityMode") == 0) {
  64. argpos++;
  65. if(sscanf(argv[argpos], "%i", (int*)&securityMode) != 1) {
  66. usage();
  67. return EXIT_FAILURE;
  68. }
  69. continue;
  70. }
  71. if(strcmp(argv[argpos], "-securityPolicy") == 0) {
  72. argpos++;
  73. securityPolicyUri = UA_STRING(argv[argpos]);
  74. continue;
  75. }
  76. #endif
  77. usage();
  78. return EXIT_FAILURE;
  79. }
  80. /* Create the server and set its config */
  81. UA_Client *client = UA_Client_new();
  82. UA_ClientConfig *cc = UA_Client_getConfig(client);
  83. #ifdef UA_ENABLE_ENCRYPTION
  84. if(certfile) {
  85. UA_ByteString certificate = loadFile(certfile);
  86. UA_ByteString privateKey = loadFile(keyfile);
  87. UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
  88. NULL, 0, NULL, 0);
  89. UA_ByteString_clear(&certificate);
  90. UA_ByteString_clear(&privateKey);
  91. } else {
  92. UA_ClientConfig_setDefault(cc);
  93. }
  94. #else
  95. UA_ClientConfig_setDefault(cc);
  96. #endif
  97. cc->securityMode = securityMode;
  98. cc->securityPolicyUri = securityPolicyUri;
  99. /* Connect to the server */
  100. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  101. if(username)
  102. retval = UA_Client_connect_username(client, serverurl, username, password);
  103. else
  104. retval = UA_Client_connect(client, serverurl);
  105. if(retval != UA_STATUSCODE_GOOD) {
  106. UA_Client_delete(client);
  107. return EXIT_FAILURE;
  108. }
  109. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Connected!");
  110. /* Read the server-time */
  111. UA_Variant value;
  112. UA_Variant_init(&value);
  113. UA_Client_readValueAttribute(client,
  114. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME),
  115. &value);
  116. if(UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  117. UA_DateTimeStruct dts = UA_DateTime_toStruct(*(UA_DateTime *)value.data);
  118. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  119. "The server date is: %02u-%02u-%04u %02u:%02u:%02u.%03u",
  120. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  121. }
  122. UA_Variant_clear(&value);
  123. /* Clean up */
  124. UA_Client_disconnect(client);
  125. UA_Client_delete(client);
  126. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  127. }