opcua_transportLayer.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * opcua_transportLayer.c
  3. *
  4. * Created on: Dec 19, 2013
  5. * Author: opcua
  6. */
  7. #include "opcua_transportLayer.h"
  8. /*
  9. * send acknowledge to the client
  10. */
  11. void TL_sendACK(UA_connection *connection)
  12. {
  13. //get memory for message
  14. //
  15. //build message
  16. //connection->transportLayer.serverBuffers.maxChunkCount;
  17. //call send function
  18. }
  19. void TL_open(UA_connection *connection, AD_RawMessage *rawMessage)
  20. {
  21. switch(connection->transportLayer.connectionState)
  22. {
  23. connectionState_CLOSED :
  24. {
  25. TL_processHELMessage(connection,rawMessage);
  26. TL_sendACK(connection);
  27. connection->transportLayer.connectionState = connectionState_ESTABLISHED;
  28. break;
  29. }
  30. connectionState_OPENING :
  31. {
  32. // TL_sendACK(connection);
  33. // connection->transportLayer.connectionState = connectionState_ESTABLISHED;
  34. break;
  35. }
  36. connectionState_ESTABLISHED :
  37. {
  38. break;
  39. }
  40. }
  41. }
  42. /*
  43. void TL_receive(UA_connection *connection, AD_RawMessage *TL_message)
  44. {
  45. UInt32 bufferSize = connection->transportLayer.serverBuffers.recvBufferSize = 8192;
  46. UInt32 length = 0;
  47. AD_RawMessage tmpRawMessage;
  48. struct TL_header tmpHeader;
  49. //allocate memory for the message
  50. //TODO filter double Hello Messages -> generate error message as response
  51. //TODO build a file which handles the memory allocation
  52. tmpRawMessage.message = (char *)malloc(bufferSize);
  53. if (tmpRawMessage.message != NULL)
  54. {
  55. length = tcp_recv(connection, tmpRawMessage.message, bufferSize);
  56. }
  57. tmpRawMessage.length = length;
  58. if(tmpRawMessage.length > 0)
  59. {
  60. TL_getMessageHeader(connection, &tmpHeader);
  61. switch(tmpHeader->MessageType)
  62. {
  63. TL_OPN,TL_MSG,TL_CLO :
  64. {
  65. break;
  66. }
  67. TL_HEL :
  68. {
  69. break;
  70. }
  71. TL_ACK :
  72. {
  73. break;
  74. }
  75. }
  76. //check in which state the connection is
  77. }
  78. }
  79. */
  80. void TL_getMessageHeader_test()
  81. {
  82. Byte data[] = {0x48,0x45,0x4c,0x46,0x56,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x01,0x88,0x13,0x00,0x00,0x36,0x00,0x00,0x00,0x6f,0x70,0x63,0x2e,0x74,0x63,0x70,0x3a,0x2f,0x2f,0x43,0x61,0x6e,0x6f,0x70,0x75,0x73,0x2e,0x70,0x6c,0x74,0x2e,0x72,0x77,0x74,0x68,0x2d,0x61,0x61,0x63,0x68,0x65,0x6e,0x2e,0x64,0x65,0x3a,0x31,0x36,0x36,0x36,0x34,0x2f,0x34,0x43,0x45,0x55,0x41,0x53,0x65,0x72,0x76,0x65,0x72};
  83. AD_RawMessage rawMessage;
  84. rawMessage.message = data;
  85. rawMessage.length = 86;
  86. struct TL_messageBodyHEL HELmessage;
  87. struct TL_header header;
  88. printf("TL_getMessageHeader_test");
  89. TL_getMessageHeader(&header, &rawMessage);
  90. if(header.MessageSize == 86 &&
  91. header.MessageType == TL_HEL &&
  92. header.Reserved == 0x46)
  93. {
  94. printf(" - passed \n");
  95. }
  96. else
  97. {
  98. printf(" - failed \n");
  99. }
  100. }
  101. /*
  102. * get the message header
  103. */
  104. void TL_getMessageHeader(struct TL_header *header, AD_RawMessage *rawMessage)
  105. {
  106. int pos = 0;
  107. if(rawMessage->message[0] == 'H' &&
  108. rawMessage->message[1] == 'E' &&
  109. rawMessage->message[2] == 'L')
  110. {
  111. header->MessageType = TL_HEL;
  112. }
  113. else if(rawMessage->message[0] == 'A' &&
  114. rawMessage->message[1] == 'C' &&
  115. rawMessage->message[2] == 'K')
  116. {
  117. header->MessageType = TL_ACK;
  118. }
  119. else if(rawMessage->message[0] == 'E' &&
  120. rawMessage->message[1] == 'R' &&
  121. rawMessage->message[2] == 'R')
  122. {
  123. header->MessageType = TL_ERR;
  124. }
  125. else if(rawMessage->message[0] == 'O' &&
  126. rawMessage->message[1] == 'P' &&
  127. rawMessage->message[2] == 'N')
  128. {
  129. header->MessageType = TL_OPN;
  130. }
  131. else if(rawMessage->message[0] == 'C' &&
  132. rawMessage->message[1] == 'L' &&
  133. rawMessage->message[2] == 'O')
  134. {
  135. header->MessageType = TL_CLO;
  136. }
  137. else if(rawMessage->message[0] == 'M' &&
  138. rawMessage->message[1] == 'S' &&
  139. rawMessage->message[2] == 'G')
  140. {
  141. header->MessageType = TL_MSG;
  142. }
  143. else
  144. {
  145. //TODO ERROR no valid message received
  146. }
  147. pos = pos + TL_MESSAGE_TYPE_LEN;
  148. header->Reserved = convertToByte(rawMessage,pos);
  149. pos = pos + TL_RESERVED_LEN;
  150. header->MessageSize = convertToUInt32(rawMessage,pos);
  151. }
  152. Int32 TL_getPacketType(AD_RawMessage *rawMessage)
  153. {
  154. if(rawMessage->message[0] == 'H' &&
  155. rawMessage->message[1] == 'E' &&
  156. rawMessage->message[2] == 'L')
  157. {
  158. return packetType_HEL;
  159. }
  160. else if(rawMessage->message[0] == 'A' &&
  161. rawMessage->message[1] == 'C' &&
  162. rawMessage->message[2] == 'K')
  163. {
  164. return packetType_ACK;
  165. }
  166. else if(rawMessage->message[0] == 'E' &&
  167. rawMessage->message[1] == 'R' &&
  168. rawMessage->message[2] == 'R')
  169. {
  170. return packetType_ERR;
  171. }
  172. else if(rawMessage->message[0] == 'O' &&
  173. rawMessage->message[1] == 'P' &&
  174. rawMessage->message[2] == 'N')
  175. {
  176. return packetType_OPN;
  177. }
  178. else if(rawMessage->message[0] == 'C' &&
  179. rawMessage->message[1] == 'L' &&
  180. rawMessage->message[2] == 'O')
  181. {
  182. return packetType_CLO;
  183. }
  184. else if(rawMessage->message[0] == 'M' &&
  185. rawMessage->message[1] == 'S' &&
  186. rawMessage->message[2] == 'G')
  187. {
  188. return packetType_MSG;
  189. }
  190. else
  191. {
  192. return -1;//TODO ERROR no valid message received
  193. }
  194. }
  195. void TL_processHELMessage_test()
  196. {
  197. Byte data[] = {0x48,0x45,0x4c,0x46,0x56,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x01,0x88,0x13,0x00,0x00,0x36,0x00,0x00,0x00,0x6f,0x70,0x63,0x2e,0x74,0x63,0x70,0x3a,0x2f,0x2f,0x43,0x61,0x6e,0x6f,0x70,0x75,0x73,0x2e,0x70,0x6c,0x74,0x2e,0x72,0x77,0x74,0x68,0x2d,0x61,0x61,0x63,0x68,0x65,0x6e,0x2e,0x64,0x65,0x3a,0x31,0x36,0x36,0x36,0x34,0x2f,0x34,0x43,0x45,0x55,0x41,0x53,0x65,0x72,0x76,0x65,0x72};
  198. UA_connection con;
  199. AD_RawMessage rawMessage;
  200. rawMessage.message = data;
  201. rawMessage.length = 86;
  202. struct TL_messageBodyHEL HELmessage;
  203. struct TL_header header;
  204. printf("TL_getHELMessage_test");
  205. header.MessageSize = 86;
  206. header.MessageType = TL_HEL; // HEL message
  207. header.Reserved = 0x46; // F
  208. TL_processHELMessage(&con, &rawMessage);
  209. if(con.transportLayer.clientBuffers.protocolVersion == 0 &&
  210. con.transportLayer.clientBuffers.recvBufferSize == 65536 &&
  211. con.transportLayer.clientBuffers.sendBufferSize == 65536 &&
  212. con.transportLayer.clientBuffers.maxMessageSize == 16777216 &&
  213. con.transportLayer.clientBuffers.maxChunkCount == 5000)
  214. {
  215. printf(" - passed \n");
  216. }
  217. else
  218. {
  219. printf(" - failed \n");
  220. }
  221. }
  222. /*
  223. * gets the TL_messageBody
  224. */
  225. void TL_processHELMessage(UA_connection *connection, AD_RawMessage *rawMessage)
  226. {
  227. UInt32 pos = TL_HEADER_LENGTH;
  228. struct TL_header tmpHeader;
  229. connection->transportLayer.clientBuffers.protocolVersion =
  230. convertToUInt32(rawMessage,pos);
  231. pos = pos + sizeof(UInt32);
  232. connection->transportLayer.clientBuffers.recvBufferSize =
  233. convertToUInt32(rawMessage,pos);
  234. pos = pos + sizeof(UInt32);
  235. connection->transportLayer.clientBuffers.sendBufferSize =
  236. convertToUInt32(rawMessage,pos);
  237. pos = pos + sizeof(UInt32);
  238. connection->transportLayer.clientBuffers.maxMessageSize =
  239. convertToUInt32(rawMessage,pos);
  240. pos = pos + sizeof(UInt32);
  241. connection->transportLayer.clientBuffers.maxChunkCount =
  242. convertToUInt32(rawMessage,pos);
  243. pos = pos + sizeof(UInt32);
  244. connection->transportLayer.endpointURL.Data = (rawMessage->message[pos]);
  245. connection->transportLayer.endpointURL.Length = tmpHeader.MessageSize - pos;
  246. }
  247. /*
  248. * respond to client request
  249. */
  250. TL_send(AD_RawMessage *rawMessage)
  251. {
  252. //call tcp function or callback
  253. }