opcuaServerACPLT.c 3.6 KB

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