opcua_transportLayer.c 8.6 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. #define Cmp3Byte(data,pos,a,b,c) (*((Int32*) ((data)+(pos))) & 0xFFFFFF) == (Int32)(((Byte)(a))|((Byte)(b))<<8|((Byte)(c))<<16)
  91. Int32 TL_getPacketType(UA_ByteString *packet, Int32 *pos)
  92. {
  93. Int32 retVal = -1;
  94. printf("TL_getPacketType - entered \n");
  95. printf("TL_getPacketType - pos = %d \n",*pos);
  96. //printf(packet->Data[*pos]);
  97. if(packet->Data[*pos] == 'H' &&
  98. packet->Data[*pos+1] == 'E' &&
  99. packet->Data[*pos+2] == 'L')
  100. {
  101. *pos += 3 * sizeof(Byte);
  102. retVal = packetType_HEL;
  103. }
  104. else if(packet->Data[*pos] == 'A' &&
  105. packet->Data[*pos+1] == 'C' &&
  106. packet->Data[*pos+2] == 'K')
  107. {
  108. *pos += 3 * sizeof(Byte);
  109. retVal = packetType_ACK;
  110. }
  111. else if(packet->Data[*pos] == 'E' &&
  112. packet->Data[*pos+1] == 'R' &&
  113. packet->Data[*pos+2] == 'R')
  114. {
  115. *pos += 3 * sizeof(Byte);
  116. retVal = packetType_ERR;
  117. }
  118. else if(packet->Data[*pos] == 'O' &&
  119. packet->Data[*pos+1] == 'P' &&
  120. packet->Data[*pos+2] == 'N')
  121. {
  122. *pos += 3 * sizeof(Byte);
  123. retVal = packetType_OPN;
  124. }
  125. else if(packet->Data[*pos] == 'C' &&
  126. packet->Data[*pos+1] == 'L' &&
  127. packet->Data[*pos+2] == 'O')
  128. {
  129. *pos += 3 * sizeof(Byte);
  130. retVal = packetType_CLO;
  131. }
  132. else if(packet->Data[*pos] == 'M' &&
  133. packet->Data[*pos+1] == 'S' &&
  134. packet->Data[*pos+2] == 'G')
  135. {
  136. *pos += 3 * sizeof(Byte);
  137. retVal = packetType_MSG;
  138. }
  139. //TODO retVal == -1 -- ERROR no valid message received
  140. return retVal;
  141. }
  142. Int32 TL_process(UA_connection *connection,Int32 packetType, Int32 *pos)
  143. {
  144. Int32 tmpPos = 0;
  145. UA_ByteString tmpMessage;
  146. Byte reserved;
  147. UInt32 messageSize;
  148. printf("TL_process - entered \n");
  149. struct TL_header tmpHeader;
  150. switch(packetType)
  151. {
  152. case packetType_HEL :
  153. if(connection->transportLayer.connectionState == connectionState_CLOSED)
  154. {
  155. printf("TL_process - extracting header information \n");
  156. printf("TL_process - pos = %d \n",*pos);
  157. /* extract information from received header */
  158. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  159. (void*)(&(connection->transportLayer.remoteConf.protocolVersion)));
  160. printf("TL_process - protocolVersion = %d \n",connection->transportLayer.remoteConf.protocolVersion);
  161. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  162. (void*)(&(connection->transportLayer.remoteConf.recvBufferSize)));
  163. printf("TL_process - recvBufferSize = %d \n",connection->transportLayer.remoteConf.recvBufferSize);
  164. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  165. (void*)(&(connection->transportLayer.remoteConf.sendBufferSize)));
  166. printf("TL_process - sendBufferSize = %d \n",connection->transportLayer.remoteConf.sendBufferSize);
  167. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  168. (void*)(&(connection->transportLayer.remoteConf.maxMessageSize)));
  169. printf("TL_process - maxMessageSize = %d \n",connection->transportLayer.remoteConf.maxMessageSize);
  170. decoder_decodeBuiltInDatatype(connection->readData.Data,UINT32,pos,
  171. (void*)(&(connection->transportLayer.remoteConf.maxChunkCount)));
  172. printf("TL_process - maxChunkCount = %d \n",connection->transportLayer.remoteConf.maxChunkCount);
  173. decoder_decodeBuiltInDatatype(connection->readData.Data,STRING,pos,
  174. (void*)(&(connection->transportLayer.endpointURL)));
  175. /* send back acknowledge */
  176. tmpMessage.Data = (Byte*)opcua_malloc(SIZE_OF_ACKNOWLEDGE_MESSAGE);
  177. if(tmpMessage.Data == NULL)
  178. {
  179. printf("TL_process - memory allocation failed \n");
  180. }
  181. tmpMessage.Length = SIZE_OF_ACKNOWLEDGE_MESSAGE;
  182. printf("TL_process - allocated memory \n");
  183. /* ------------------------ Header ------------------------ */
  184. // MessageType
  185. tmpMessage.Data[0] = 'A';
  186. tmpMessage.Data[1] = 'C';
  187. tmpMessage.Data[2] = 'K';
  188. tmpPos += 3;
  189. // Chunk
  190. reserved = 'F';
  191. encoder_encodeBuiltInDatatype(&reserved, BYTE, &tmpPos, tmpMessage.Data);
  192. // MessageSize
  193. messageSize = SIZE_OF_ACKNOWLEDGE_MESSAGE;
  194. encoder_encodeBuiltInDatatype(&messageSize,UINT32, &tmpPos, tmpMessage.Data);
  195. printf("TL_process - Size messageToSend = %d \n",messageSize);
  196. /* ------------------------ Body ------------------------ */
  197. // protocol version
  198. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.protocolVersion),
  199. UINT32, &tmpPos, tmpMessage.Data);
  200. printf("TL_process - localConf.protocolVersion = %d \n",connection->transportLayer.localConf.protocolVersion);
  201. //receive buffer size
  202. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.recvBufferSize),
  203. UINT32, &tmpPos, tmpMessage.Data);
  204. printf("TL_process - localConf.recvBufferSize = %d \n", connection->transportLayer.localConf.recvBufferSize);
  205. //send buffer size
  206. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.sendBufferSize),
  207. UINT32, &tmpPos, tmpMessage.Data);
  208. printf("TL_process - localConf.sendBufferSize = %d \n", connection->transportLayer.localConf.sendBufferSize);
  209. //maximum message size
  210. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.maxMessageSize),
  211. UINT32, &tmpPos, tmpMessage.Data);
  212. printf("TL_process - localConf.maxMessageSize = %d \n", connection->transportLayer.localConf.maxMessageSize);
  213. //maximum chunk count
  214. encoder_encodeBuiltInDatatype(&(connection->transportLayer.localConf.maxChunkCount),
  215. UINT32, &tmpPos, tmpMessage.Data);
  216. printf("TL_process - localConf.maxChunkCount = %d \n", connection->transportLayer.localConf.maxChunkCount);
  217. TL_send(connection, &tmpMessage);
  218. }
  219. else
  220. {
  221. printf("TL_process - wrong connection state \n");
  222. return UA_ERROR_MULTIPLY_HEL;
  223. }
  224. break;
  225. default:
  226. return UA_ERROR;
  227. }
  228. return UA_NO_ERROR;
  229. }
  230. /*
  231. * respond to client request
  232. */
  233. TL_send(UA_connection *connection, UA_ByteString *packet)
  234. {
  235. printf("TL_send - entered \n");
  236. connection->newDataToWrite = 1;
  237. if(packet->Length < connection->transportLayer.remoteConf.maxMessageSize)
  238. {
  239. connection->writeData.Data = packet->Data;
  240. connection->writeData.Length = packet->Length;
  241. printf("TL_send - packet length = %d \n", packet->Length);
  242. }
  243. else
  244. {
  245. printf("TL_send - ERROR: packet size greater than remote buffer size", packet->Length);
  246. //server error
  247. }
  248. }