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.

363 lines
11 KiB

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