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.

238 lines
6.8 KiB

7 years ago
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
6 years ago
7 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
6 years ago
7 years ago
​
6 years ago
​
​
​
​
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
​
  1. #include "protocol.h"
  2. #define LOG( X ) m_pLog->Log( "Proto", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "Proto", this, _LOG_ERROR, X )
  4. //Thread Logging
  5. #define SLOG( X ) mThis->m_pLog->Log( "ProtoS", mThis, X )
  6. #define SLOG_ERROR( X ) mThis->m_pLog->Log( "ProtoS", mThis, _LOG_ERROR, X )
  7. protocolParser::protocolParser() : m_mBufferProtectorMutex(){
  8. m_pLog = basicLogCollection::getInstance();
  9. m_pParserBuffer = (U8*) malloc(PROTOCOL_PARSER_BUFFER_SIZE);
  10. m_nParserDataCount = 0;
  11. m_bStatus = 0;
  12. m_pCustomHandler = NULL;
  13. m_bBufferAltered = false;
  14. m_bStatus = 1;
  15. m_bParserThreadStatus = TH_PRE_INIT;
  16. m_eConnectionStatus = CS_UNKNOWN;
  17. };
  18. protocolParser::~protocolParser(){
  19. # ifdef DEBUG
  20. LOG("Destructor Begin");
  21. # endif
  22. if (m_bParserThreadStatus > TH_ARMED ) {
  23. waitParserThreadEnd( true );
  24. };
  25. m_mBufferProtectorMutex.unlock();
  26. if (m_bParserThreadStatus < TH_ENDED ) {
  27. waitParserThreadEnd( true );
  28. };
  29. if( m_pParserBuffer != NULL ) {
  30. free( m_pParserBuffer );
  31. };
  32. # ifdef DEBUG
  33. LOG("Destructor End");
  34. # endif
  35. };
  36. bool protocolParser::create(void){
  37. if (m_pParserBuffer != NULL) {
  38. if (m_bParserThreadStatus == TH_PRE_INIT) {
  39. m_bParserThreadStatus = TH_ARMED;
  40. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  41. int RetVal = pthread_create( &m_sParserThreadHandler, NULL, m_fParserThreadEntryPoint, ( void * ) this );
  42. if( RetVal == -1 ) {
  43. LOG_ERROR("Thread Creation Failed.");
  44. return false;
  45. };
  46. #else
  47. m_hParserThreadHandle = CreateThread(
  48. NULL,
  49. 0,
  50. (LPTHREAD_START_ROUTINE)m_fParserThreadEntryPoint,
  51. (LPVOID)this,
  52. 0,
  53. &m_dwThreadID );
  54. //TODO: Add thread creation check
  55. #endif
  56. } else {
  57. LOG_ERROR("Thread object not ready to be created.");
  58. return false;
  59. };
  60. return true;
  61. } else return false;
  62. };
  63. void protocolParser::setConnectionStatus( CONNECTION_STATUS cStatus ){
  64. if (cStatus == CS_CONNECTED)LOG("Connection Status Changed - Connected!");
  65. if (cStatus == CS_DISCONNECTED)LOG("Connection Status Changed - Disconnected!");
  66. m_eConnectionStatus = cStatus;
  67. };
  68. CONNECTION_STATUS protocolParser::getConnectionStatus( void ){
  69. return m_eConnectionStatus;
  70. };
  71. bool protocolParser::setTransport( basicTransport* Transport, bool check_if_allready ){
  72. if (Transport == NULL) return false;
  73. if( check_if_allready ) {
  74. if (m_oDataTransport != NULL) {
  75. LOG_ERROR("Transport Already Assigned!");
  76. return false;
  77. };
  78. };
  79. m_oDataTransport = Transport;
  80. return m_oDataTransport->addRecipient( this );
  81. };
  82. bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){
  83. bool rv = false;
  84. if( m_pParserBuffer != NULL ) {
  85. m_mBufferProtectorMutex.lock();
  86. if( ( m_nParserDataCount + BufferSize ) < PROTOCOL_PARSER_BUFFER_SIZE ) {
  87. memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
  88. m_nParserDataCount+=BufferSize;
  89. rv = m_bBufferAltered = true;
  90. };
  91. m_mBufferProtectorMutex.unlock();
  92. };
  93. return rv;
  94. };
  95. void protocolParser::dataEraser( U8 *&tPtr ){
  96. if (tPtr == NULL) return;
  97. free(tPtr);
  98. tPtr = NULL;
  99. };
  100. void protocolParser::m_fParserThreadProc(void){
  101. int RetVal;
  102. m_bParserThreadStatus = TH_WORKING;
  103. while(m_bParserThreadStatus < TH_ENDED){
  104. if( m_eConnectionStatus < CS_DISCONNECTED ){
  105. if( ( m_bBufferAltered == false ) && ( m_bParserThreadStatus < TH_ENDED ) ) {
  106. SLEEP(3);
  107. continue;
  108. };
  109. m_mBufferProtectorMutex.lock();
  110. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED)){
  111. #ifdef DUBUG_COMMUNICATION
  112. LOG("Check Pack");
  113. #endif
  114. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  115. #ifdef DUBUG_COMMUNICATION
  116. LOG("Check Pack Done");
  117. #endif
  118. if( RetVal < 0 ){
  119. // Note: RetVal is NEGATIVE!!??
  120. m_nParserDataCount+=RetVal;
  121. memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount );
  122. } else if ( RetVal == 0 ) {
  123. protocolPackage* tVal = NULL;
  124. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  125. if (tVal != NULL) {
  126. #ifdef DUBUG_COMMUNICATION
  127. LOG("Package Handling");
  128. char tBuf[256];
  129. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  130. LOG(tBuf);
  131. #endif
  132. //Move unused data toward buffer's begin to parse at next parse attempt
  133. m_nParserDataCount -= tVal->m_nSize;
  134. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  135. // In Case implementation of packetHandler - handler must return !true!
  136. if( !packetHandler(tVal) ){
  137. m_vPackagePool.push_back( tVal );
  138. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  139. // Delete oldest package in queue
  140. m_vPackagePool.erase(m_vPackagePool.begin());
  141. dataEraser(tVal->m_pData);
  142. free(tVal);
  143. };
  144. } else {
  145. dataEraser(tVal->m_pData);
  146. free(tVal);
  147. };
  148. } else {
  149. // All data consumed in MAVLINK proto but not parsed into message
  150. m_nParserDataCount = 0;
  151. };
  152. } else {
  153. break;
  154. };
  155. };
  156. m_bBufferAltered = false;
  157. m_mBufferProtectorMutex.unlock();
  158. } else {
  159. // Handle Disconnect
  160. m_bParserThreadStatus = TH_ENDED;
  161. };
  162. };
  163. };
  164. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  165. void *protocolParser::m_fParserThreadEntryPoint( void *ptr ){
  166. #else
  167. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint( LPVOID ptr){
  168. #endif
  169. protocolParser *mThis;
  170. mThis = ( protocolParser * ) ptr;
  171. if( mThis->m_bParserThreadStatus == TH_ARMED ) {
  172. mThis->m_fParserThreadProc();
  173. mThis->m_bParserThreadStatus = TH_EXIT;
  174. } else {
  175. SLOG_ERROR("Thread is not ARMED!");
  176. };
  177. #ifdef __linux__
  178. pthread_exit( NULL );
  179. return NULL;
  180. #else
  181. return 0;
  182. #endif
  183. };
  184. void protocolParser::waitParserThreadEnd(bool forced){
  185. if( m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED ) return;
  186. if( forced ) m_bParserThreadStatus = TH_ENDED;
  187. while (m_bParserThreadStatus != TH_EXIT){
  188. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  189. };
  190. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  191. pthread_join(m_sParserThreadHandler, NULL);
  192. #else
  193. WaitForSingleObject(m_hParserThreadHandle, 2000);
  194. CloseHandle(m_hParserThreadHandle);
  195. #endif
  196. };
  197. bool protocolParser::getPackage( U8 **Package, size_t *Size ){
  198. if( this ) {
  199. m_mBufferProtectorMutex.lock();
  200. if( m_vPackagePool.size() ){
  201. protocolPackage *tVal;
  202. tVal = m_vPackagePool.front();
  203. *Package =( U8 * ) malloc( tVal->m_nSize );
  204. if( *Package != NULL && tVal != NULL ) {
  205. memcpy( *Package, tVal->m_pData, tVal->m_nSize );
  206. *Size = tVal->m_nSize;
  207. m_vPackagePool.erase( m_vPackagePool.begin() );
  208. free( tVal );
  209. m_mBufferProtectorMutex.unlock();
  210. return true;
  211. };
  212. };
  213. m_mBufferProtectorMutex.unlock();
  214. };
  215. return false;
  216. };
  217. #undef SLOG
  218. #undef SLOG_ERROR
  219. #undef LOG
  220. #undef LOG_ERROR