opcuaServer.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <stdio.h>
  6. #ifndef WIN32
  7. #include <sys/mman.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10. #endif
  11. #include <sys/types.h>
  12. #include <sys/time.h>
  13. #include <fcntl.h>
  14. #include <signal.h>
  15. #include "server/ua_server.h"
  16. #include "logger_stdout.h"
  17. #include "networklayer_tcp.h"
  18. UA_Boolean running = UA_TRUE;
  19. void stopHandler(int sign) {
  20. running = UA_FALSE;
  21. }
  22. void serverCallback(UA_Server *server) {
  23. printf("does whatever servers do\n");
  24. }
  25. UA_ByteString loadCertificate() {
  26. UA_ByteString certificate = UA_STRING_NULL;
  27. FILE *fp = UA_NULL;
  28. //FIXME: a potiential bug of locating the certificate, we need to get the path from the server's config
  29. fp=fopen("localhost.der", "rb");
  30. if(fp == UA_NULL)
  31. return certificate;
  32. fseek(fp, 0, SEEK_END);
  33. certificate.length = ftell(fp);
  34. UA_alloc((void**)&certificate.data, certificate.length*sizeof(UA_Byte));
  35. fseek(fp, 0, SEEK_SET);
  36. fread(certificate.data, sizeof(UA_Byte), certificate.length, fp);
  37. fclose(fp);
  38. return certificate;
  39. }
  40. int main(int argc, char** argv) {
  41. signal(SIGINT, stopHandler); /* catches ctrl-c */
  42. UA_Server server;
  43. UA_String endpointUrl;
  44. UA_String_copycstring("no endpoint url",&endpointUrl);
  45. UA_Server_init(&server, &endpointUrl);
  46. Logger_Stdout_init(&server.logger);
  47. server.serverCertificate = loadCertificate();
  48. NetworklayerTCP* nl;
  49. NetworklayerTCP_new(&nl, UA_ConnectionConfig_standard, 16664);
  50. struct timeval callback_interval = {1, 0}; // 1 second
  51. UA_Int32 retval = NetworkLayerTCP_run(nl, &server, callback_interval,
  52. serverCallback, &running);
  53. NetworklayerTCP_delete(nl);
  54. UA_Server_deleteMembers(&server);
  55. return retval == UA_SUCCESS ? 0 : retval;
  56. }