You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
3.8 KiB

7 years ago
6 years ago
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
7 years ago
7 years ago
7 years ago
​
7 years ago
7 years ago
​
​
7 years ago
​
​
  1. #include "transportSerial.h"
  2. #include "../../data/protocol/protocol.h"
  3. #define LOG(X) m_pLog->Log("TSER", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("TSER", this, _LOG_ERROR, X)
  5. transportSerial::transportSerial() :
  6. basicTransport(),
  7. basicDualThread()
  8. {
  9. m_pLog = basicLogCollection::getInstance();
  10. m_nInputBufferSize = -1;
  11. m_nOutputBufferSize = -1;
  12. m_pInputBuffer = (U8 *) malloc(MAXIMAL_SERIAL_IO_BUFFER_LENGTH);
  13. if (m_pInputBuffer != NULL)
  14. {
  15. m_nInputBufferSize = 0;
  16. m_pOutputBuffer = (U8 *) malloc(MAXIMAL_SERIAL_IO_BUFFER_LENGTH);
  17. if (m_pOutputBuffer != NULL)
  18. {
  19. m_nOutputBufferSize = 0;
  20. // For Select IO Mode
  21. pipe(m_iControlPipeFD);
  22. FD_ZERO(&m_sSenderSet);
  23. FD_ZERO(&m_sReaderSet);
  24. FD_SET(m_iControlPipeFD[0], &m_sSenderSet);
  25. m_nSenderSetMax = m_iControlPipeFD[0];
  26. FD_SET(m_iControlPipeFD[1], &m_sReaderSet);
  27. m_nReaderSetMax = m_iControlPipeFD[1];
  28. };
  29. };
  30. }
  31. transportSerial::~transportSerial()
  32. {
  33. _close();
  34. if (m_pInputBuffer != NULL)
  35. {
  36. free(m_pInputBuffer);
  37. }
  38. if (m_pOutputBuffer != NULL)
  39. {
  40. free(m_pOutputBuffer);
  41. }
  42. }
  43. bool transportSerial::create(void)
  44. {
  45. m_bInitComplete = init();
  46. if (m_bInitComplete)
  47. {
  48. setConnectionStatus(CS_CONNECTED);
  49. }
  50. return m_bInitComplete;
  51. }
  52. // void *transportSerial::senderProc( void *param ){
  53. void transportSerial::m_fThreadAProc(void)
  54. {
  55. m_bThreadAStatus = TH_WORKING;
  56. uint32_t nSleepTime = 1;
  57. #ifdef DEBUG_COMMUNICATION
  58. LOG("Output Thread Started ");
  59. #endif
  60. while (m_bThreadAStatus < TH_ENDED)
  61. {
  62. m_mBufferProtectorMutex.lock();
  63. if (m_nOutputBufferSize > 0)
  64. {
  65. int index;
  66. int RetVal;
  67. index = 0;
  68. #ifdef DEBUG_COMMUNICATION
  69. LOG("Buffer ready to Send");
  70. #endif
  71. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED))
  72. {
  73. if (m_mIsBusy.try_lock())
  74. {
  75. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  76. m_mIsBusy.unlock();
  77. if (RetVal > 0)
  78. {
  79. index += RetVal;
  80. }
  81. if (RetVal < 0)
  82. {
  83. setConnectionStatus(CS_DISCONNECTED);
  84. m_bThreadAStatus = TH_ENDED;
  85. };
  86. }
  87. else
  88. {
  89. SLEEP(3);
  90. }
  91. };
  92. #ifdef DEBUG_COMMUNICATION
  93. LOG("Buffer is Sent");
  94. #endif
  95. m_nOutputBufferSize = 0;
  96. nSleepTime = 1;
  97. }
  98. else
  99. {
  100. nSleepTime = 10;
  101. };
  102. m_mBufferProtectorMutex.unlock();
  103. SLEEP(nSleepTime);
  104. };
  105. }
  106. // void *transportSerial::receiverProc( void *param ){
  107. void transportSerial::m_fThreadBProc(void)
  108. {
  109. m_bThreadBStatus = TH_WORKING;
  110. uint32_t nSleepTime = 1;
  111. #ifdef DEBUG_COMMUNICATION
  112. LOG("Input Thread Started");
  113. #endif
  114. while (m_bThreadBStatus < TH_ENDED)
  115. {
  116. if (m_mIsBusy.try_lock())
  117. {
  118. int retVal = recptBlock(m_pInputBuffer, &m_nInputBufferSize);
  119. m_mIsBusy.unlock();
  120. if (retVal < 0)
  121. {
  122. setConnectionStatus(CS_DISCONNECTED);
  123. m_bThreadBStatus = TH_ENDED;
  124. continue;
  125. }
  126. else
  127. {
  128. if (m_nInputBufferSize > 0 && m_bThreadBStatus == TH_WORKING)
  129. {
  130. m_mQueueProtectorMutex.lock();
  131. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++)
  132. {
  133. if (m_bThreadBStatus == TH_WORKING)
  134. {
  135. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  136. }
  137. };
  138. m_mQueueProtectorMutex.unlock();
  139. nSleepTime = 1;
  140. }
  141. else
  142. {
  143. nSleepTime = 10;
  144. if (m_bThreadBStatus != TH_WORKING)
  145. {
  146. continue;
  147. }
  148. };
  149. };
  150. SLEEP(nSleepTime);
  151. }
  152. else
  153. {
  154. SLEEP(3);
  155. }
  156. };
  157. }
  158. bool transportSerial::sendData(unsigned char * Data, int DataSize)
  159. {
  160. m_mBufferProtectorMutex.lock();
  161. if (m_nOutputBufferSize + DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH)
  162. {
  163. m_mBufferProtectorMutex.unlock();
  164. LOG_ERROR("Data Too Long");
  165. return false;
  166. };
  167. #ifdef DEBUG_COMMUNICATION
  168. LOG("Output Data Set");
  169. #endif
  170. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  171. m_nOutputBufferSize += DataSize;
  172. m_mBufferProtectorMutex.unlock();
  173. return true;
  174. }
  175. #undef LOG
  176. #undef LOG_ERROR