opcua_transportLayer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * opcua_transportLayer.c
  3. *
  4. * Created on: Dec 19, 2013
  5. * Author: opcua
  6. */
  7. #include "opcua_transportLayer.h"
  8. Int32 TL_initConnectionObject(UA_connection *connection)
  9. {
  10. connection->newDataToRead = 0;
  11. connection->readData.Data = NULL;
  12. connection->readData.Length = 0;
  13. connection->transportLayer.connectionState = connectionState_CLOSED;
  14. connection->transportLayer.localConf.maxChunkCount = 1;
  15. connection->transportLayer.localConf.maxMessageSize = 16384;
  16. connection->transportLayer.localConf.sendBufferSize = 8192;
  17. connection->transportLayer.localConf.recvBufferSize = 8192;
  18. return UA_NO_ERROR;
  19. }
  20. Int32 TL_check(UA_connection *connection)
  21. {
  22. Int32 position = 4;
  23. UInt32 messageLength = 0;
  24. printf("TL_check - entered \n");
  25. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,&position,&messageLength);
  26. printf("TL_check - messageLength = %d \n",messageLength);
  27. if (messageLength == connection->readData.Length &&
  28. messageLength < (connection->transportLayer.localConf.maxMessageSize))
  29. {
  30. printf("TL_check - no error \n");
  31. return UA_NO_ERROR;
  32. }
  33. printf("TL_check - length error \n");
  34. return UA_ERROR;
  35. }
  36. Int32 TL_receive(UA_connection *connection, UA_ByteString *packet)
  37. {
  38. UInt32 length = 0;
  39. Int32 pos = 0;
  40. AD_RawMessage tmpRawMessage;
  41. struct TL_header tmpHeader;
  42. printf("TL_receive - entered \n");
  43. packet->Data = NULL;
  44. packet->Length = 0;
  45. if(TL_check(connection) == UA_NO_ERROR)
  46. {
  47. printf("TL_receive - no error \n");
  48. printf("TL_receive - connection->readData.Length %d \n",connection->readData.Length);
  49. Int32 packetType = TL_getPacketType(&(connection->readData),&pos);
  50. //is final chunk or not
  51. //TODO process chunks
  52. pos += sizeof(Byte);
  53. //save the message size if needed
  54. pos += sizeof(UInt32);
  55. printf("TL_receive - packetType = %d \n",packetType);
  56. switch(packetType)
  57. {
  58. case packetType_MSG:
  59. case packetType_OPN:
  60. case packetType_CLO:
  61. {
  62. packet->Data = connection->readData.Data;
  63. packet->Length = connection->readData.Length;
  64. printf("TL_receive - received MSG or OPN or CLO message\n");
  65. break;
  66. }
  67. case packetType_HEL:
  68. case packetType_ACK:
  69. {
  70. printf("TL_receive - received HEL or ACK message\n");
  71. TL_process(connection, packetType, &pos);
  72. break;
  73. }
  74. case packetType_ERR:
  75. {
  76. printf("TL_receive - received ERR message\n");
  77. //TODO ERROR HANDLING
  78. return UA_ERROR_RCV_ERROR;
  79. break;
  80. }
  81. }
  82. }
  83. else
  84. {
  85. //length error: send error message to communication partner
  86. //TL_send()
  87. }
  88. return UA_NO_ERROR;
  89. }
  90. Int32 TL_getPacketType(UA_ByteString *packet, Int32 *pos)
  91. {
  92. printf("TL_getPacketType - entered \n");
  93. printf("TL_getPacketType - pos = %d \n",*pos);
  94. //printf(packet->Data[*pos]);
  95. if(packet->Data[*pos] == 'H' &&
  96. packet->Data[*pos+1] == 'E' &&
  97. packet->Data[*pos+2] == 'L')
  98. {
  99. *pos += 3 * sizeof(Byte);
  100. return packetType_HEL;
  101. }
  102. else if(packet->Data[*pos] == 'A' &&
  103. packet->Data[*pos+1] == 'C' &&
  104. packet->Data[*pos+2] == 'K')
  105. {
  106. *pos += 3 * sizeof(Byte);
  107. return packetType_ACK;
  108. }
  109. else if(packet->Data[*pos] == 'E' &&
  110. packet->Data[*pos+1] == 'R' &&
  111. packet->Data[*pos+2] == 'R')
  112. {
  113. *pos += 3 * sizeof(Byte);
  114. return packetType_ERR;
  115. }
  116. else if(packet->Data[*pos] == 'O' &&
  117. packet->Data[*pos+1] == 'P' &&
  118. packet->Data[*pos+2] == 'N')
  119. {
  120. *pos += 3 * sizeof(Byte);
  121. return packetType_OPN;
  122. }
  123. else if(packet->Data[*pos] == 'C' &&
  124. packet->Data[*pos+1] == 'L' &&
  125. packet->Data[*pos+2] == 'O')
  126. {
  127. *pos += 3 * sizeof(Byte);
  128. return packetType_CLO;
  129. }
  130. else if(packet->Data[*pos] == 'M' &&
  131. packet->Data[*pos+1] == 'S' &&
  132. packet->Data[*pos+2] == 'G')
  133. {
  134. *pos += 3 * sizeof(Byte);
  135. return packetType_MSG;
  136. }
  137. return -1;//TODO ERROR no valid message received
  138. }
  139. Int32 TL_process(UA_connection *connection,Int32 packetType, Int32 *pos)
  140. {
  141. Int32 tmpPos = 0;
  142. UA_ByteString tmpMessage;
  143. Byte reserved;
  144. UInt32 messageSize;
  145. printf("TL_process - entered \n");
  146. struct TL_header tmpHeader;
  147. switch(packetType)
  148. {
  149. case packetType_HEL :
  150. if(connection->transportLayer.connectionState == connectionState_CLOSED)
  151. {
  152. printf("TL_process - extracting header information \n");
  153. printf("TL_process - pos = %d \n",*pos);
  154. /* extract information from received header */
  155. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  156. (void*)(&(connection->transportLayer.remoteConf.protocolVersion)));
  157. printf("TL_process - protocolVersion = %d \n",connection->transportLayer.remoteConf.protocolVersion);
  158. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  159. (void*)(&(connection->transportLayer.remoteConf.recvBufferSize)));
  160. printf("TL_process - recvBufferSize = %d \n",connection->transportLayer.remoteConf.recvBufferSize);
  161. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  162. (void*)(&(connection->transportLayer.remoteConf.sendBufferSize)));
  163. printf("TL_process - sendBufferSize = %d \n",connection->transportLayer.remoteConf.sendBufferSize);
  164. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  165. (void*)(&(connection->transportLayer.remoteConf.maxMessageSize)));
  166. printf("TL_process - maxMessageSize = %d \n",connection->transportLayer.remoteConf.maxMessageSize);
  167. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  168. (void*)(&(connection->transportLayer.remoteConf.maxChunkCount)));
  169. printf("TL_process - maxChunkCount = %d \n",connection->transportLayer.remoteConf.maxChunkCount);
  170. decoder_decodeBuiltInDatatype(connection->readData.Data,STRING,pos,
  171. (void*)(&(connection->transportLayer.endpointURL)));
  172. /* send back acknowledge */
  173. tmpMessage.Data = (Byte*)opcua_malloc(SIZE_OF_ACKNOWLEDGE_MESSAGE);
  174. if(tmpMessage.Data == NULL)
  175. {
  176. printf("TL_process - memory allocation failed \n");
  177. }
  178. tmpMessage.Length = SIZE_OF_ACKNOWLEDGE_MESSAGE;
  179. printf("TL_process - allocated memory \n");
  180. /* ------------------------ Header ------------------------ */
  181. // MessageType
  182. tmpMessage.Data[0] = 'A';
  183. tmpMessage.Data[1] = 'C';
  184. tmpMessage.Data[2] = 'K';
  185. tmpPos += 3;
  186. // Chunk
  187. reserved = 'F';
  188. encoder_encodeBuiltInDatatype(&reserved, BYTE, &tmpPos, tmpMessage.Data);
  189. // MessageSize
  190. messageSize = SIZE_OF_ACKNOWLEDGE_MESSAGE;
  191. encoder_encodeBuiltInDatatype(&messageSize,UINT32, &tmpPos, tmpMessage.Data);
  192. printf("TL_process - Size messageToSend = %d \n",messageSize);
  193. /* ------------------------ Body ------------------------ */
  194. // protocol version
  195. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.protocolVersion),
  196. UINT32, &tmpPos, tmpMessage.Data);
  197. printf("TL_process - localConf.protocolVersion = %d \n",connection->transportLayer.localConf.protocolVersion);
  198. //receive buffer size
  199. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.recvBufferSize),
  200. UINT32, &tmpPos, tmpMessage.Data);
  201. printf("TL_process - localConf.recvBufferSize = %d \n", connection->transportLayer.localConf.recvBufferSize);
  202. //send buffer size
  203. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.sendBufferSize),
  204. UINT32, &tmpPos, tmpMessage.Data);
  205. printf("TL_process - localConf.sendBufferSize = %d \n", connection->transportLayer.localConf.sendBufferSize);
  206. //maximum message size
  207. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.maxMessageSize),
  208. UINT32, &tmpPos, tmpMessage.Data);
  209. printf("TL_process - localConf.maxMessageSize = %d \n", connection->transportLayer.localConf.maxMessageSize);
  210. //maximum chunk count
  211. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.maxChunkCount),
  212. UINT32, &tmpPos, tmpMessage.Data);
  213. printf("TL_process - localConf.maxChunkCount = %d \n", connection->transportLayer.localConf.maxChunkCount);
  214. TL_send(connection, &tmpMessage);
  215. }
  216. else
  217. {
  218. printf("TL_process - wrong connection state \n");
  219. return UA_ERROR_MULTIPLY_HEL;
  220. }
  221. break;
  222. default:
  223. return UA_ERROR;
  224. }
  225. return UA_NO_ERROR;
  226. }
  227. /*
  228. * respond to client request
  229. */
  230. TL_send(UA_connection *connection, UA_ByteString *packet)
  231. {
  232. printf("TL_send - entered \n");
  233. connection->newDataToWrite = 1;
  234. if(packet->Length < connection->transportLayer.remoteConf.maxMessageSize)
  235. {
  236. connection->writeData.Data = packet->Data;
  237. connection->writeData.Length = packet->Length;
  238. printf("TL_send - packet length = %d \n", packet->Length);
  239. }
  240. else
  241. {
  242. printf("TL_send - ERROR: packet size greater than remote buffer size", packet->Length);
  243. //server error
  244. }
  245. }