opcuaServer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "../../src/opcua_binaryEncDec.h"
  13. #include "../../src/opcua_builtInDatatypes.h"
  14. #include "../../src/opcua_transportLayer.h"
  15. #include "../../src/opcua_types.h"
  16. #ifdef LINUX
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. void server_init();
  20. void server_run();
  21. #endif
  22. int main(void)
  23. {
  24. #ifdef LINUX
  25. server_init();
  26. server_run();
  27. #endif
  28. return EXIT_SUCCESS;
  29. }
  30. #ifdef LINUX
  31. void server_init()
  32. {
  33. puts("starting demo Server");
  34. //call listen
  35. }
  36. void server_run()
  37. {
  38. int server_state = 0;
  39. int recv_data = 0;
  40. int send_data = 1;
  41. int new_client = 2;
  42. int new_request = 3;
  43. char buf[8192];
  44. struct sockaddr_in self;
  45. int sockfd;
  46. int clientfd;
  47. struct sockaddr_in client_addr;
  48. int addrlen=sizeof(client_addr);
  49. Int32 valreal = 0;
  50. Int32 valcalc = 0;
  51. UA_DiagnosticInfo diagnosticInfo;
  52. diagnosticInfo.EncodingMask |= 0x01 | 0x02 | 0x04 | 0x04 | 0x08 | 0x10 | 0x10;
  53. diagnosticInfo.SymbolicId = 30;
  54. diagnosticInfo.NamespaceUri = 25;
  55. diagnosticInfo.LocalizedText = 22;
  56. diagnosticInfo.AdditionalInfo.Data = "OPCUA";
  57. diagnosticInfo.AdditionalInfo.Length = 5;
  58. if (diagnosticInfo_calcSize(&diagnosticInfo) > 10)
  59. {
  60. //---Create streaming socket---
  61. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  62. {
  63. puts("socket error");
  64. }
  65. }
  66. bzero(&self, sizeof(self));
  67. self.sin_family = AF_INET;
  68. self.sin_port = htons(4840);
  69. self.sin_addr.s_addr = htonl(INADDR_ANY);
  70. if( bind(sockfd,(struct sockaddr *)&self , sizeof(self)) < 0)
  71. {
  72. //Fehler bei bind()
  73. }
  74. //---Make it a "listening socket"---
  75. if ( listen(sockfd, 1) != 0 )
  76. {
  77. puts("listen error");
  78. }
  79. clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
  80. server_state = 0;
  81. while(1)
  82. {
  83. //call recv (nonblocking)
  84. //call TL_getPacketType
  85. //if newData
  86. //
  87. UA_connection connection;
  88. AD_RawMessage *rawMessage;
  89. rawMessage->message = buf;
  90. rawMessage->length = 0;
  91. switch(server_state)
  92. {
  93. recv_data :
  94. {
  95. //call receive function
  96. rawMessage->length = recv(sockfd,buf,8192,0);
  97. if(rawMessage->length > 0)
  98. {
  99. server_state = new_client;
  100. }
  101. break;
  102. }
  103. send_data :
  104. {
  105. //call send function
  106. break;
  107. }
  108. new_client :
  109. {
  110. if(connection.transportLayer.connectionState != connectionState_ESTABLISHED)
  111. {
  112. TL_open(connection,rawMessage);
  113. }
  114. // else
  115. // {
  116. // SL_open(connection,rawMessage);
  117. //
  118. // }
  119. }
  120. new_request :
  121. {
  122. break;
  123. }
  124. }
  125. //if newClient
  126. //TL_processHELMessage(&connection,);
  127. //--------
  128. //call listen
  129. }
  130. }
  131. #endif