opcuaServer.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <errno.h> // errno, EINTR
  16. #include "server/ua_server.h"
  17. #include "logger_stdout.h"
  18. #include "networklayer_tcp.h"
  19. UA_Boolean running = UA_TRUE;
  20. void stopHandler(int sign) {
  21. running = UA_FALSE;
  22. }
  23. void serverCallback(UA_Server *server) {
  24. printf("does whatever servers do\n");
  25. }
  26. UA_ByteString loadCertificate() {
  27. UA_ByteString certificate = UA_STRING_NULL;
  28. FILE *fp = UA_NULL;
  29. //FIXME: a potiential bug of locating the certificate, we need to get the path from the server's config
  30. fp=fopen("localhost.der", "rb");
  31. if(!fp) {
  32. errno = 0; // otherwise we think sth went wrong on the tcp socket level
  33. return certificate;
  34. }
  35. fseek(fp, 0, SEEK_END);
  36. certificate.length = ftell(fp);
  37. UA_alloc((void**)&certificate.data, certificate.length*sizeof(UA_Byte));
  38. fseek(fp, 0, SEEK_SET);
  39. if(fread(certificate.data, sizeof(UA_Byte), certificate.length, fp) < (size_t)certificate.length)
  40. UA_ByteString_deleteMembers(&certificate); // error reading the cert
  41. fclose(fp);
  42. return certificate;
  43. }
  44. int main(int argc, char** argv) {
  45. signal(SIGINT, stopHandler); /* catches ctrl-c */
  46. UA_Server server;
  47. UA_String endpointUrl;
  48. UA_String_copycstring("no endpoint url",&endpointUrl);
  49. UA_Server_init(&server, &endpointUrl);
  50. Logger_Stdout_init(&server.logger);
  51. server.serverCertificate = loadCertificate();
  52. NetworklayerTCP* nl;
  53. NetworklayerTCP_new(&nl, UA_ConnectionConfig_standard, 16664);
  54. struct timeval callback_interval = {1, 0}; // 1 second
  55. UA_Int32 retval = NetworkLayerTCP_run(nl, &server, callback_interval,
  56. serverCallback, &running);
  57. NetworklayerTCP_delete(nl);
  58. UA_Server_deleteMembers(&server);
  59. return retval == UA_SUCCESS ? 0 : retval;
  60. }