opcua_transportLayer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * opcua_transportLayer.c
  3. *
  4. * Created on: Dec 19, 2013
  5. * Author: opcua
  6. */
  7. #include "opcua_transportLayer.h"
  8. UA_Int32 TL_initConnectionObject(UA_connection *connection)
  9. {
  10. connection->newDataToRead = 0;
  11. connection->readData.data = UA_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. UA_Int32 TL_check(UA_connection *connection)
  21. {
  22. UA_Int32 position = 4;
  23. UA_Int32 messageLength = 0;
  24. printf("TL_check - entered \n");
  25. UA_ByteString_printf("received data:",&(connection->readData));
  26. UA_Int32_decode(connection->readData.data,&position,&messageLength);
  27. printf("TL_check - messageLength = %d \n",messageLength);
  28. if (messageLength != -1 && messageLength == connection->readData.length &&
  29. messageLength < (UA_Int32) connection->transportLayer.localConf.maxMessageSize)
  30. {
  31. printf("TL_check - no error \n");
  32. return UA_NO_ERROR;
  33. }
  34. printf("TL_check - length error \n");
  35. return UA_ERROR;
  36. }
  37. UA_Int32 TL_receive(UA_connection *connection, UA_ByteString *packet)
  38. {
  39. UA_Int32 retval = UA_SUCCESS;
  40. UA_Int32 pos = 0;
  41. UA_OPCUATcpMessageHeader *tcpMessageHeader;
  42. UA_alloc((void**)&tcpMessageHeader,UA_OPCUATcpMessageHeader_calcSize(UA_NULL));
  43. printf("TL_receive - entered \n");
  44. packet->data = NULL;
  45. packet->length = 0;
  46. UA_OPCUATcpMessageHeader_decode(connection->readData.data, &pos,tcpMessageHeader);
  47. if(TL_check(connection) == UA_NO_ERROR)
  48. {
  49. printf("TL_receive - no error \n");
  50. printf("TL_receive - connection->readData.length %d \n",connection->readData.length);
  51. printf("TL_receive - MessageType = %d \n",tcpMessageHeader->messageType);
  52. switch(tcpMessageHeader->messageType)
  53. {
  54. case UA_MESSAGETYPE_MSG:
  55. case UA_MESSAGETYPE_OPN:
  56. case UA_MESSAGETYPE_CLO:
  57. {
  58. packet->data = connection->readData.data;
  59. packet->length = connection->readData.length;
  60. printf("TL_receive - received MSG or OPN or CLO message\n");
  61. break;
  62. }
  63. case UA_MESSAGETYPE_HEL:
  64. case UA_MESSAGETYPE_ACK:
  65. {
  66. printf("TL_receive - received HEL or ACK message\n");
  67. TL_process(connection, tcpMessageHeader->messageType, &pos);
  68. break;
  69. }
  70. case UA_MESSAGETYPE_ERR:
  71. {
  72. printf("TL_receive - received ERR message\n");
  73. //TODO ERROR HANDLING
  74. retval = UA_ERROR_RCV_ERROR;
  75. break;
  76. }
  77. }
  78. }
  79. else
  80. {
  81. //length error: send error message to communication partner
  82. //TL_send()
  83. }
  84. // Clean Up
  85. UA_OPCUATcpMessageHeader_delete(tcpMessageHeader);
  86. return retval;
  87. }
  88. #define Cmp3Byte(data,pos,a,b,c) (*((Int32*) ((data)+(pos))) & 0xFFFFFF) == (Int32)(((Byte)(a))|((Byte)(b))<<8|((Byte)(c))<<16)
  89. UA_Int32 TL_process(UA_connection *connection,UA_Int32 packetType, UA_Int32 *pos)
  90. {
  91. UA_Int32 tmpPos = 0;
  92. UA_ByteString tmpMessage;
  93. UA_OPCUATcpHelloMessage *helloMessage;
  94. UA_OPCUATcpAcknowledgeMessage *ackMessage;
  95. UA_OPCUATcpMessageHeader *ackHeader;
  96. printf("TL_process - entered \n");
  97. switch(packetType)
  98. {
  99. case UA_MESSAGETYPE_HEL :
  100. if(connection->transportLayer.connectionState == connectionState_CLOSED)
  101. {
  102. printf("TL_process - extracting header information \n");
  103. printf("TL_process - pos = %d \n",*pos);
  104. UA_alloc((void**)(&helloMessage),UA_OPCUATcpHelloMessage_calcSize(UA_NULL));
  105. UA_OPCUATcpHelloMessage_decode(connection->readData.data,pos,helloMessage);
  106. /* extract information from received header */
  107. //UA_UInt32_decode(connection->readData.data,pos,(&(connection->transportLayer.remoteConf.protocolVersion)));
  108. connection->transportLayer.remoteConf.protocolVersion = helloMessage->protocolVersion;
  109. printf("TL_process - protocolVersion = %d \n",connection->transportLayer.remoteConf.protocolVersion);
  110. connection->transportLayer.remoteConf.recvBufferSize = helloMessage->receiveBufferSize;
  111. printf("TL_process - recvBufferSize = %d \n",connection->transportLayer.remoteConf.recvBufferSize);
  112. connection->transportLayer.remoteConf.sendBufferSize = helloMessage->sendBufferSize;
  113. printf("TL_process - sendBufferSize = %d \n",connection->transportLayer.remoteConf.sendBufferSize);
  114. connection->transportLayer.remoteConf.maxMessageSize = helloMessage->maxMessageSize;
  115. printf("TL_process - maxMessageSize = %d \n",connection->transportLayer.remoteConf.maxMessageSize);
  116. connection->transportLayer.remoteConf.maxChunkCount = helloMessage->maxChunkCount;
  117. printf("TL_process - maxChunkCount = %d \n",connection->transportLayer.remoteConf.maxChunkCount);
  118. UA_String_copy(&(helloMessage->endpointUrl), &(connection->transportLayer.endpointURL));
  119. UA_OPCUATcpHelloMessage_delete(helloMessage);
  120. /* send back acknowledge */
  121. //memory for message
  122. UA_alloc((void**)&(ackMessage),UA_OPCUATcpAcknowledgeMessage_calcSize(UA_NULL));
  123. ackMessage->protocolVersion = connection->transportLayer.localConf.protocolVersion;
  124. ackMessage->receiveBufferSize = connection->transportLayer.localConf.recvBufferSize;
  125. ackMessage->maxMessageSize = connection->transportLayer.localConf.maxMessageSize;
  126. ackMessage->maxChunkCount = connection->transportLayer.localConf.maxChunkCount;
  127. //memory for header
  128. UA_alloc((void**)&(ackHeader),UA_OPCUATcpMessageHeader_calcSize(UA_NULL));
  129. ackHeader->messageType = UA_MESSAGETYPE_ACK;
  130. ackHeader->isFinal = 'F';
  131. ackHeader->messageSize = UA_OPCUATcpAcknowledgeMessage_calcSize(ackMessage) +
  132. UA_OPCUATcpMessageHeader_calcSize(ackHeader);
  133. //allocate memory in stream
  134. UA_alloc((void**)&(tmpMessage.data),ackHeader->messageSize);
  135. tmpMessage.length = ackHeader->messageSize;
  136. //encode header and message
  137. UA_OPCUATcpMessageHeader_encode(ackHeader,&tmpPos,tmpMessage.data);
  138. UA_OPCUATcpAcknowledgeMessage_encode(ackMessage,&tmpPos,tmpMessage.data);
  139. printf("TL_process - Size messageToSend = %d \n",ackHeader->messageSize);
  140. UA_OPCUATcpMessageHeader_delete(ackHeader);
  141. UA_OPCUATcpAcknowledgeMessage_delete(ackMessage);
  142. /* ------------------------ Body ------------------------ */
  143. // protocol version
  144. printf("TL_process - localConf.protocolVersion = %d \n",connection->transportLayer.localConf.protocolVersion);
  145. //receive buffer size
  146. printf("TL_process - localConf.recvBufferSize = %d \n", connection->transportLayer.localConf.recvBufferSize);
  147. //send buffer size
  148. printf("TL_process - localConf.sendBufferSize = %d \n", connection->transportLayer.localConf.sendBufferSize);
  149. //maximum message size
  150. printf("TL_process - localConf.maxMessageSize = %d \n", connection->transportLayer.localConf.maxMessageSize);
  151. //maximum chunk count
  152. printf("TL_process - localConf.maxChunkCount = %d \n", connection->transportLayer.localConf.maxChunkCount);
  153. UA_ByteString_printf("encoded data",&tmpMessage);
  154. TL_send(connection, &tmpMessage);
  155. // do not delete tmpMessage - this is the responsibility of the send thread
  156. }
  157. else
  158. {
  159. printf("TL_process - wrong connection state \n");
  160. return UA_ERROR_MULTIPLY_HEL;
  161. }
  162. break;
  163. default:
  164. return UA_ERROR;
  165. }
  166. return UA_SUCCESS;
  167. }
  168. /*
  169. * respond to client request
  170. */
  171. UA_Int32 TL_send(UA_connection* connection, UA_ByteString* packet)
  172. {
  173. printf("TL_send - entered \n");
  174. connection->newDataToWrite = 1;
  175. if(packet->length != -1 && packet->length < (UA_Int32) connection->transportLayer.remoteConf.maxMessageSize)
  176. {
  177. connection->writeData.data = packet->data;
  178. connection->writeData.length = packet->length;
  179. printf("TL_send - packet length = %d \n", packet->length);
  180. }
  181. else
  182. {
  183. printf("TL_send - ERROR: packet size greater than remote buffer size");
  184. //server error
  185. }
  186. return UA_SUCCESS;
  187. }