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.

247 lines
7.1 KiB

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