opcuaServerACPLT.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. ============================================================================
  3. Name : opcuaServer.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description :
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <memory.h> // bzero
  13. #include "opcua.h"
  14. #ifdef LINUX
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <sys/socketvar.h>
  19. void server_init();
  20. void server_run();
  21. #endif
  22. #define PORT 16664
  23. #define MAXMSG 512
  24. #define BUFFER_SIZE 8192
  25. int main(void) {
  26. #ifdef LINUX
  27. server_init();
  28. server_run();
  29. #endif
  30. return EXIT_SUCCESS;
  31. }
  32. #ifdef LINUX
  33. void server_init() {
  34. printf("Starting open62541 demo server on port %d\n", PORT);
  35. }
  36. typedef struct T_Server {
  37. int newDataToWrite;
  38. UA_ByteString writeData;
  39. } Server;
  40. Server server;
  41. void server_writer(UA_TL_connection connection, UA_ByteString* msg) {
  42. UA_ByteString_copy(msg,&server.writeData);
  43. server.newDataToWrite = 1;
  44. }
  45. void server_run() {
  46. UA_TL_connection connection;
  47. connection.connectionState = connectionState_CLOSE;
  48. connection.writerCallback = server_writer;
  49. connection.localConf.maxChunkCount = 1;
  50. connection.localConf.maxMessageSize = BUFFER_SIZE;
  51. connection.localConf.protocolVersion = 0;
  52. connection.localConf.recvBufferSize = BUFFER_SIZE;
  53. connection.localConf.recvBufferSize = BUFFER_SIZE;
  54. UA_ByteString slMessage = { -1, UA_NULL };
  55. int optval = 1;
  56. int sockfd, newsockfd, portno, clilen;
  57. char buffer[BUFFER_SIZE];
  58. struct sockaddr_in serv_addr, cli_addr;
  59. int n;
  60. /* First call to socket() function */
  61. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  62. if (sockfd < 0) {
  63. perror("ERROR opening socket");
  64. exit(1);
  65. }
  66. /* Initialize socket structure */
  67. bzero((void *) &serv_addr, sizeof(serv_addr));
  68. portno = PORT;
  69. serv_addr.sin_family = AF_INET;
  70. serv_addr.sin_addr.s_addr = INADDR_ANY;
  71. serv_addr.sin_port = htons(portno);
  72. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)
  73. == -1) {
  74. perror("setsockopt");
  75. exit(1);
  76. }
  77. /* Now bind the host address using bind() call.*/
  78. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  79. perror("ERROR on binding");
  80. exit(1);
  81. }
  82. /* Now start listening for the clients, here process will
  83. * go in sleep mode and will wait for the incoming connection
  84. */
  85. while (listen(sockfd, 5) != -1) {
  86. clilen = sizeof(cli_addr);
  87. /* Accept actual connection from the client */
  88. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  89. if (newsockfd < 0) {
  90. perror("ERROR on accept");
  91. exit(1);
  92. }
  93. printf("connection accepted\n");
  94. /* communication loop */
  95. while (connection.connectionState != connectionState_CLOSE) {
  96. /* If connection is established then start communicating */
  97. bzero(buffer, BUFFER_SIZE);
  98. n = read(newsockfd, buffer, BUFFER_SIZE);
  99. if (n > 0) {
  100. slMessage.data = buffer;
  101. slMessage.length = n;
  102. UA_ByteString_printx("server_run - received=",slMessage);
  103. TL_process(&connection, &slMessage);
  104. } else if (n < 0) {
  105. perror("ERROR reading from socket1");
  106. exit(1);
  107. }
  108. if (server.newDataToWrite) {
  109. UA_ByteString_printx("Send data:", &server.writeData);
  110. n = write(newsockfd, server.writeData.data,
  111. server.writeData.length);
  112. printf("written %d bytes \n", n);
  113. server.newDataToWrite = 0;
  114. UA_ByteString_deleteMembers(&server.writeData);
  115. server.writeData.data = UA_NULL;
  116. server.writeData.length = 0;
  117. }
  118. }
  119. close(newsockfd);
  120. connection.connectionState = connectionState_CLOSED;
  121. }
  122. }
  123. #endif