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.

340 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
​
​
​
7 years ago
6 years ago
​
​
​
6 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
  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. LOG("Handler Consumed Data!");
  174. dataEraser(tVal->m_pData);
  175. free(tVal);
  176. };
  177. } else {
  178. LOG_ERROR("Packet Not Extracted!");
  179. // All data consumed in MAVLINK proto but not parsed into message
  180. m_nParserDataCount = 0;
  181. };
  182. } else {
  183. break;
  184. };
  185. };
  186. m_bBufferAltered = false;
  187. //m_mBufferProtectorMutex.unlock();
  188. #endif
  189. };
  190. m_mBufferProtectorMutex.unlock();
  191. };
  192. return rv;
  193. };
  194. void protocolParser::dataEraser( U8 *&tPtr ){
  195. if (tPtr == NULL) return;
  196. free(tPtr);
  197. tPtr = NULL;
  198. };
  199. #ifdef PROTOCOL_PARSER_IN_THREAD
  200. void protocolParser::m_fParserThreadProc(void){
  201. int RetVal;
  202. m_bParserThreadStatus = TH_WORKING;
  203. while(m_bParserThreadStatus < TH_ENDED){
  204. if( m_eConnectionStatus < CS_DISCONNECTED ){
  205. if( ( m_bBufferAltered == false ) && ( m_bParserThreadStatus < TH_ENDED ) ) {
  206. SLEEP(3);
  207. continue;
  208. };
  209. m_mBufferProtectorMutex.lock();
  210. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED)){
  211. #ifdef DUBUG_COMMUNICATION
  212. LOG("Check Pack");
  213. #endif
  214. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  215. #ifdef DUBUG_COMMUNICATION
  216. LOG("Check Pack Done");
  217. #endif
  218. if( RetVal < 0 ){
  219. // Note: RetVal is NEGATIVE!!??
  220. m_nParserDataCount+=RetVal;
  221. memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount );
  222. } else if ( RetVal == 0 ) {
  223. protocolPackage* tVal = NULL;
  224. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  225. if (tVal != NULL) {
  226. #ifdef DUBUG_COMMUNICATION
  227. LOG("Package Handling");
  228. char tBuf[256];
  229. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  230. LOG(tBuf);
  231. #endif
  232. //Move unused data toward buffer's begin to parse at next parse attempt
  233. m_nParserDataCount -= tVal->m_nSize;
  234. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  235. // In Case implementation of packetHandler - handler must return !true!
  236. if( !packetHandler(tVal) ){
  237. m_vPackagePool.push_back( tVal );
  238. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  239. // Delete oldest package in queue
  240. m_vPackagePool.erase(m_vPackagePool.begin());
  241. dataEraser(tVal->m_pData);
  242. free(tVal);
  243. };
  244. } else {
  245. dataEraser(tVal->m_pData);
  246. free(tVal);
  247. };
  248. } else {
  249. // All data consumed in MAVLINK proto but not parsed into message
  250. m_nParserDataCount = 0;
  251. };
  252. } else {
  253. break;
  254. };
  255. };
  256. m_bBufferAltered = false;
  257. m_mBufferProtectorMutex.unlock();
  258. } else {
  259. // Handle Disconnect
  260. m_bParserThreadStatus = TH_ENDED;
  261. };
  262. };
  263. };
  264. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  265. void *protocolParser::m_fParserThreadEntryPoint( void *ptr ){
  266. #else
  267. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint( LPVOID ptr){
  268. #endif
  269. protocolParser *mThis;
  270. mThis = ( protocolParser * ) ptr;
  271. if( mThis->m_bParserThreadStatus == TH_ARMED ) {
  272. mThis->m_fParserThreadProc();
  273. mThis->m_bParserThreadStatus = TH_EXIT;
  274. } else {
  275. SLOG_ERROR("Thread is not ARMED!");
  276. };
  277. #ifdef __linux__
  278. pthread_exit( NULL );
  279. return NULL;
  280. #else
  281. return 0;
  282. #endif
  283. };
  284. void protocolParser::waitParserThreadEnd(bool forced){
  285. if( m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED ) return;
  286. if( forced ) m_bParserThreadStatus = TH_ENDED;
  287. while (m_bParserThreadStatus != TH_EXIT){
  288. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  289. };
  290. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  291. pthread_join(m_sParserThreadHandler, NULL);
  292. #else
  293. WaitForSingleObject(m_hParserThreadHandle, 2000);
  294. CloseHandle(m_hParserThreadHandle);
  295. #endif
  296. };
  297. #endif //#ifndef PROTOCOL_PARSER_IN_THREAD
  298. bool protocolParser::getPackage( U8 **Package, size_t *Size ){
  299. if( this ) {
  300. m_mBufferProtectorMutex.lock();
  301. if( m_vPackagePool.size() ){
  302. protocolPackage *tVal;
  303. tVal = m_vPackagePool.front();
  304. *Package =( U8 * ) malloc( tVal->m_nSize );
  305. if( *Package != NULL && tVal != NULL ) {
  306. memcpy( *Package, tVal->m_pData, tVal->m_nSize );
  307. *Size = tVal->m_nSize;
  308. m_vPackagePool.erase( m_vPackagePool.begin() );
  309. free( tVal );
  310. m_mBufferProtectorMutex.unlock();
  311. return true;
  312. };
  313. };
  314. m_mBufferProtectorMutex.unlock();
  315. };
  316. return false;
  317. };
  318. #undef SLOG
  319. #undef SLOG_ERROR
  320. #undef LOG
  321. #undef LOG_ERROR