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.

342 lines
10 KiB

7 years ago
​
​
​
​
​
7 years ago
7 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
7 years ago
​
​
7 years ago
​
​
​
​
​
6 years ago
​
5 years ago
​
​
​
5 years ago
​
​
​
​
5 years ago
​
​
​
6 years ago
7 years ago
​
​
​
​
7 years ago
​
6 years ago
7 years ago
​
​
5 years ago
5 years ago
5 years ago
6 years ago
​
​
3 years ago
​
​
3 years ago
6 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
​
​
​
​
​
​
​
​
6 years ago
​
7 years ago
6 years ago
7 years ago
3 years ago
​
​
7 years ago
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
​
​
​
3 years ago
3 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_oDataTransport = NULL;
  11. m_pParserBuffer = (U8*) malloc(PROTOCOL_PARSER_BUFFER_SIZE);
  12. if( m_pParserBuffer ) {
  13. memset( m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE );
  14. } else LOG_ERROR("Cannot allocate Parser Buffer!");
  15. m_nParserDataCount = 0;
  16. m_bStatus = 0;
  17. m_pCustomHandler = NULL;
  18. m_bBufferAltered = false;
  19. m_bStatus = 1;
  20. #ifdef PROTOCOL_PARSER_IN_THREAD
  21. m_bParserThreadStatus = TH_PRE_INIT;
  22. #endif
  23. };
  24. protocolParser::~protocolParser(){
  25. # ifdef DEBUG_EXTRA
  26. LOG("Destructor Begin");
  27. # endif
  28. m_mBufferProtectorMutex.unlock();
  29. /*
  30. #ifdef PROTOCOL_PARSER_IN_THREAD
  31. if (m_bParserThreadStatus > TH_ARMED ) {
  32. waitParserThreadEnd( true );
  33. };
  34. if (m_bParserThreadStatus < TH_ENDED) {
  35. waitParserThreadEnd(true);
  36. };
  37. #endif
  38. */
  39. if( m_pParserBuffer != NULL ) {
  40. free( m_pParserBuffer );
  41. };
  42. # ifdef DEBUG_EXTRA
  43. LOG("Destructor End");
  44. # endif
  45. };
  46. bool protocolParser::create(void){
  47. if (m_pParserBuffer != NULL) {
  48. #ifdef PROTOCOL_PARSER_IN_THREAD
  49. if (m_bParserThreadStatus == TH_PRE_INIT) {
  50. m_bParserThreadStatus = TH_ARMED;
  51. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  52. int RetVal = pthread_create( &m_sParserThreadHandler, NULL, m_fParserThreadEntryPoint, ( void * ) this );
  53. if( RetVal == -1 ) {
  54. LOG_ERROR("Thread Creation Failed.");
  55. return false;
  56. };
  57. #else
  58. m_hParserThreadHandle = CreateThread(
  59. NULL,
  60. 0,
  61. (LPTHREAD_START_ROUTINE)m_fParserThreadEntryPoint,
  62. (LPVOID)this,
  63. 0,
  64. &m_dwThreadID );
  65. //TODO: Add thread creation check
  66. #endif
  67. } else {
  68. LOG_ERROR("Thread object not ready to be created.");
  69. return false;
  70. };
  71. #endif
  72. return true;
  73. } else return false;
  74. };
  75. void protocolParser::setConnectionStatus( CONNECTION_STATUS cStatus ){
  76. #ifdef DEBUG_EXTRA
  77. if (cStatus == CS_CONNECTED)LOG("Connection Status Changed - Connected!");
  78. if (cStatus == CS_DISCONNECTED)LOG("Connection Status Changed - Disconnected!");
  79. #endif
  80. m_eConnectionStatus = cStatus;
  81. };
  82. CONNECTION_STATUS protocolParser::getConnectionStatus( void ){
  83. return m_eConnectionStatus;
  84. };
  85. bool protocolParser::setTransport( basicTransport* Transport, bool CAE ){
  86. #ifdef DEBUG_EXTRA
  87. LOG("Set Transport Req");
  88. #endif
  89. if( CAE ){
  90. if( m_oDataTransport != NULL ) {
  91. LOG_ERROR("Transport Already Assigned!");
  92. return false;
  93. };
  94. };/* else { //Danger -> If transport Object is deleted the pointer is wrong!
  95. if ( m_oDataTransport != NULL ) {
  96. m_oDataTransport->removeRecipient( this );
  97. };
  98. };*/
  99. m_oDataTransport = Transport;
  100. if( Transport != NULL ){
  101. setConnectionStatus( CS_CONNECTED );
  102. return m_oDataTransport->addRecipient( this );
  103. } else setConnectionStatus( CS_DISCONNECTED );
  104. return true;
  105. };
  106. basicTransport *protocolParser::getCurrentTransport( void ){
  107. LOG("Get Transport Req");
  108. if( m_oDataTransport == NULL ) {
  109. LOG("Transport is NULL");
  110. }
  111. return m_oDataTransport;
  112. };
  113. bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){
  114. bool rv = false;
  115. if( m_pParserBuffer != NULL ) {
  116. #ifdef DEBUG_EXTRA
  117. char ttZZ[513];
  118. sprintf( ttZZ, "New %d bytes to Parse (allredy have in buffer: %d)", BufferSize, m_nParserDataCount );
  119. LOG(ttZZ);
  120. for ( uint8_t i = 0; i < BufferSize; i++ ) sprintf( ttZZ+(i*2), "%02X", Buffer[i] );
  121. LOG(ttZZ);
  122. #endif
  123. m_mBufferProtectorMutex.lock();
  124. if( ( m_nParserDataCount + BufferSize ) < PROTOCOL_PARSER_BUFFER_SIZE ) {
  125. memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
  126. m_nParserDataCount+=BufferSize;
  127. rv = m_bBufferAltered = true;
  128. #ifndef PROTOCOL_PARSER_IN_THREAD
  129. while ( m_nParserDataCount > 0 ){
  130. int RetVal;
  131. #ifdef DUBUG_COMMUNICATION
  132. LOG("Check Pack");
  133. #endif
  134. RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
  135. #ifdef DUBUG_COMMUNICATION
  136. LOG("Check Pack Done");
  137. #endif
  138. if (RetVal < 0) {
  139. // Note: RetVal is NEGATIVE!!??
  140. m_nParserDataCount += RetVal;
  141. memcpy(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
  142. } else if (RetVal == 0) {
  143. protocolPackage* tVal = NULL;
  144. tVal = packetExtractor( m_pParserBuffer, m_nParserDataCount );
  145. if (tVal != NULL) {
  146. #ifdef DUBUG
  147. LOG("Package Accepted");
  148. char tBuf[256];
  149. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  150. LOG(tBuf);
  151. #endif
  152. //Move unused data toward buffer's begin to parse at next parse attempt
  153. m_nParserDataCount -= tVal->m_nSize;
  154. if( m_nParserDataCount > 0 ) {
  155. memcpy( m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount );
  156. } else {
  157. // Clean Buff;
  158. memset( m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
  159. m_nParserDataCount = 0;
  160. };
  161. // In Case implementation of packetHandler - handler must return !true!
  162. if( !packetHandler(tVal) ){
  163. LOG("Handler Not Consumed Data!");
  164. m_vPackagePool.push_back(tVal);
  165. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  166. LOG("Pack Pool Overflow!");
  167. // Delete oldest package in queue
  168. m_vPackagePool.erase(m_vPackagePool.begin());
  169. dataEraser(tVal->m_pData);
  170. free(tVal);
  171. };
  172. } else {
  173. #ifdef DEBUG_EXTRA
  174. LOG("Handler Consumed Data!");
  175. #endif
  176. dataEraser(tVal->m_pData);
  177. free(tVal);
  178. };
  179. } else {
  180. LOG_ERROR("Packet Not Extracted!");
  181. // All data consumed in MAVLINK proto but not parsed into message
  182. m_nParserDataCount = 0;
  183. };
  184. } else {
  185. break;
  186. };
  187. };
  188. m_bBufferAltered = false;
  189. //m_mBufferProtectorMutex.unlock();
  190. #endif
  191. };
  192. m_mBufferProtectorMutex.unlock();
  193. };
  194. return rv;
  195. };
  196. void protocolParser::dataEraser( U8 *&tPtr ){
  197. if (tPtr == NULL) return;
  198. free(tPtr);
  199. tPtr = NULL;
  200. };
  201. #ifdef PROTOCOL_PARSER_IN_THREAD
  202. void protocolParser::m_fParserThreadProc(void){
  203. int RetVal;
  204. m_bParserThreadStatus = TH_WORKING;
  205. while(m_bParserThreadStatus < TH_ENDED){
  206. if( m_eConnectionStatus < CS_DISCONNECTED ){
  207. if( ( m_bBufferAltered == false ) && ( m_bParserThreadStatus < TH_ENDED ) ) {
  208. SLEEP(3);
  209. continue;
  210. };
  211. m_mBufferProtectorMutex.lock();
  212. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED)){
  213. #ifdef DUBUG_COMMUNICATION
  214. LOG("Check Pack");
  215. #endif
  216. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  217. #ifdef DUBUG_COMMUNICATION
  218. LOG("Check Pack Done");
  219. #endif
  220. if( RetVal < 0 ){
  221. // Note: RetVal is NEGATIVE!!??
  222. m_nParserDataCount+=RetVal;
  223. memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount );
  224. } else if ( RetVal == 0 ) {
  225. protocolPackage* tVal = NULL;
  226. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  227. if (tVal != NULL) {
  228. #ifdef DUBUG_COMMUNICATION
  229. LOG("Package Handling");
  230. char tBuf[256];
  231. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  232. LOG(tBuf);
  233. #endif
  234. //Move unused data toward buffer's begin to parse at next parse attempt
  235. m_nParserDataCount -= tVal->m_nSize;
  236. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  237. // In Case implementation of packetHandler - handler must return !true!
  238. if( !packetHandler(tVal) ){
  239. m_vPackagePool.push_back( tVal );
  240. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  241. // Delete oldest package in queue
  242. m_vPackagePool.erase(m_vPackagePool.begin());
  243. dataEraser(tVal->m_pData);
  244. free(tVal);
  245. };
  246. } else {
  247. dataEraser(tVal->m_pData);
  248. free(tVal);
  249. };
  250. } else {
  251. // All data consumed in MAVLINK proto but not parsed into message
  252. m_nParserDataCount = 0;
  253. };
  254. } else {
  255. break;
  256. };
  257. };
  258. m_bBufferAltered = false;
  259. m_mBufferProtectorMutex.unlock();
  260. } else {
  261. // Handle Disconnect
  262. m_bParserThreadStatus = TH_ENDED;
  263. };
  264. };
  265. };
  266. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  267. void *protocolParser::m_fParserThreadEntryPoint( void *ptr ){
  268. #else
  269. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint( LPVOID ptr){
  270. #endif
  271. protocolParser *mThis;
  272. mThis = ( protocolParser * ) ptr;
  273. if( mThis->m_bParserThreadStatus == TH_ARMED ) {
  274. mThis->m_fParserThreadProc();
  275. mThis->m_bParserThreadStatus = TH_EXIT;
  276. } else {
  277. SLOG_ERROR("Thread is not ARMED!");
  278. };
  279. #ifdef __linux__
  280. pthread_exit( NULL );
  281. return NULL;
  282. #else
  283. return 0;
  284. #endif
  285. };
  286. void protocolParser::waitParserThreadEnd(bool forced){
  287. if( m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED ) return;
  288. if( forced ) m_bParserThreadStatus = TH_ENDED;
  289. while (m_bParserThreadStatus != TH_EXIT){
  290. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  291. };
  292. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  293. pthread_join(m_sParserThreadHandler, NULL);
  294. #else
  295. WaitForSingleObject(m_hParserThreadHandle, 2000);
  296. CloseHandle(m_hParserThreadHandle);
  297. #endif
  298. };
  299. #endif //#ifndef PROTOCOL_PARSER_IN_THREAD
  300. bool protocolParser::getPackage( U8 **Package, size_t *Size ){
  301. if( this ) {
  302. m_mBufferProtectorMutex.lock();
  303. if( m_vPackagePool.size() ){
  304. protocolPackage *tVal;
  305. tVal = m_vPackagePool.front();
  306. *Package =( U8 * ) malloc( tVal->m_nSize );
  307. if( *Package != NULL && tVal != NULL ) {
  308. memcpy( *Package, tVal->m_pData, tVal->m_nSize );
  309. *Size = tVal->m_nSize;
  310. m_vPackagePool.erase( m_vPackagePool.begin() );
  311. free( tVal );
  312. m_mBufferProtectorMutex.unlock();
  313. return true;
  314. };
  315. };
  316. m_mBufferProtectorMutex.unlock();
  317. };
  318. return false;
  319. };
  320. #undef SLOG
  321. #undef SLOG_ERROR
  322. #undef LOG
  323. #undef LOG_ERROR