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.

361 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
​
​
2 years ago
​
​
2 years ago
6 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
​
​
​
​
2 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 ) LOG("Transport is NULL");
  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_COMMUNICATION
  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. //if( BufferSize ){
  126. memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
  127. m_nParserDataCount+=BufferSize;
  128. rv = m_bBufferAltered = true;
  129. //};
  130. #ifndef PROTOCOL_PARSER_IN_THREAD
  131. while ( m_nParserDataCount > 0 ){
  132. int RetVal;
  133. #ifdef DEBUG_COMMUNICATION
  134. LOG("Check Pack");
  135. #endif
  136. RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
  137. #ifdef DEBUG_COMMUNICATION
  138. LOG("Check Pack Done");
  139. #endif
  140. if( RetVal < 0 ){
  141. //LOG("Clear 1b");
  142. //sprintf( ttZZ, "%d bytes to Eat", m_nParserDataCount );
  143. //LOG(ttZZ);
  144. // Note: RetVal is NEGATIVE!!??
  145. m_nParserDataCount += RetVal;
  146. memcpy( m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount );
  147. //m_nParserDataCount--;
  148. //if( m_nParserDataCount ) memcpy( m_pParserBuffer, m_pParserBuffer+1, m_nParserDataCount );
  149. } else if (RetVal == 0) {
  150. protocolPackage* tVal = NULL;
  151. tVal = packetExtractor( m_pParserBuffer, m_nParserDataCount );
  152. if (tVal != NULL) {
  153. #ifdef DEBUG_COMMUNICATION
  154. LOG("Package Accepted");
  155. char tBuf[256];
  156. sprintf(tBuf, "Pack Size: %d from %d bytes TOT %lld", tVal->m_nSize, m_nParserDataCount, BufferSize );
  157. LOG(tBuf);
  158. #endif
  159. //Move unused data toward buffer's begin to parse at next parse attempt
  160. m_nParserDataCount -= tVal->m_nSize;
  161. if( m_nParserDataCount > 0 ) {
  162. memcpy( m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount );
  163. } else {
  164. // Clean Buff;
  165. memset( m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
  166. m_nParserDataCount = 0;
  167. };
  168. // In Case implementation of packetHandler - handler must return !true!
  169. if( !packetHandler(tVal) ){
  170. #ifdef DEBUG_COMMUNICATION
  171. LOG("Handler Not Consumed Data!");
  172. #endif
  173. m_vPackagePool.push_back(tVal);
  174. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  175. #ifdef DEBUG_COMMUNICATION
  176. LOG_ERROR("Pack Pool Overflow!");
  177. #endif
  178. // Delete oldest package in queue
  179. m_vPackagePool.erase(m_vPackagePool.begin());
  180. dataEraser(tVal->m_pData);
  181. free(tVal);
  182. };
  183. } else {
  184. #ifdef DEBUG_COMMUNICATION
  185. LOG("Handler Consumed Data!");
  186. #endif
  187. dataEraser(tVal->m_pData);
  188. free(tVal);
  189. };
  190. } else {
  191. #ifdef DEBUG_COMMUNICATION
  192. LOG_ERROR("Packet Not Extracted!");
  193. #endif
  194. // All data consumed in MAVLINK proto but not parsed into message
  195. m_nParserDataCount = 0;
  196. };
  197. } else {
  198. #ifdef DEBUG_COMMUNICATION
  199. LOG("Harvest More!");
  200. #endif
  201. break;
  202. };
  203. };
  204. m_bBufferAltered = false;
  205. //m_mBufferProtectorMutex.unlock();
  206. #endif
  207. };
  208. m_mBufferProtectorMutex.unlock();
  209. };
  210. return rv;
  211. };
  212. void protocolParser::dataEraser( U8 *&tPtr ){
  213. if (tPtr == NULL) return;
  214. free(tPtr);
  215. tPtr = NULL;
  216. };
  217. #ifdef PROTOCOL_PARSER_IN_THREAD
  218. void protocolParser::m_fParserThreadProc(void){
  219. int RetVal;
  220. m_bParserThreadStatus = TH_WORKING;
  221. while(m_bParserThreadStatus < TH_ENDED){
  222. if( m_eConnectionStatus < CS_DISCONNECTED ){
  223. if( ( m_bBufferAltered == false ) && ( m_bParserThreadStatus < TH_ENDED ) ) {
  224. SLEEP(3);
  225. continue;
  226. };
  227. m_mBufferProtectorMutex.lock();
  228. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED)){
  229. #ifdef DEBUG_COMMUNICATION
  230. LOG("Check Pack");
  231. #endif
  232. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  233. #ifdef DEBUG_COMMUNICATION
  234. LOG("Check Pack Done");
  235. #endif
  236. if( RetVal < 0 ){
  237. // Note: RetVal is NEGATIVE!!??
  238. m_nParserDataCount+=RetVal;
  239. memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount );
  240. } else if ( RetVal == 0 ) {
  241. protocolPackage* tVal = NULL;
  242. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  243. if (tVal != NULL) {
  244. #ifdef DEBUG_COMMUNICATION
  245. LOG("Package Handling");
  246. char tBuf[256];
  247. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  248. LOG(tBuf);
  249. #endif
  250. //Move unused data toward buffer's begin to parse at next parse attempt
  251. m_nParserDataCount -= tVal->m_nSize;
  252. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  253. // In Case implementation of packetHandler - handler must return !true!
  254. if( !packetHandler(tVal) ){
  255. m_vPackagePool.push_back( tVal );
  256. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  257. // Delete oldest package in queue
  258. m_vPackagePool.erase(m_vPackagePool.begin());
  259. dataEraser(tVal->m_pData);
  260. free(tVal);
  261. };
  262. } else {
  263. dataEraser(tVal->m_pData);
  264. free(tVal);
  265. };
  266. } else {
  267. // All data consumed in MAVLINK proto but not parsed into message
  268. m_nParserDataCount = 0;
  269. };
  270. } else {
  271. break;
  272. };
  273. };
  274. m_bBufferAltered = false;
  275. m_mBufferProtectorMutex.unlock();
  276. } else {
  277. // Handle Disconnect
  278. m_bParserThreadStatus = TH_ENDED;
  279. };
  280. };
  281. };
  282. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  283. void *protocolParser::m_fParserThreadEntryPoint( void *ptr ){
  284. #else
  285. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint( LPVOID ptr){
  286. #endif
  287. protocolParser *mThis;
  288. mThis = ( protocolParser * ) ptr;
  289. if( mThis->m_bParserThreadStatus == TH_ARMED ) {
  290. mThis->m_fParserThreadProc();
  291. mThis->m_bParserThreadStatus = TH_EXIT;
  292. } else {
  293. SLOG_ERROR("Thread is not ARMED!");
  294. };
  295. #ifdef __linux__
  296. pthread_exit( NULL );
  297. return NULL;
  298. #else
  299. return 0;
  300. #endif
  301. };
  302. void protocolParser::waitParserThreadEnd(bool forced){
  303. if( m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED ) return;
  304. if( forced ) m_bParserThreadStatus = TH_ENDED;
  305. while (m_bParserThreadStatus != TH_EXIT){
  306. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  307. };
  308. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  309. pthread_join(m_sParserThreadHandler, NULL);
  310. #else
  311. WaitForSingleObject(m_hParserThreadHandle, 2000);
  312. CloseHandle(m_hParserThreadHandle);
  313. #endif
  314. };
  315. #endif //#ifndef PROTOCOL_PARSER_IN_THREAD
  316. bool protocolParser::getPackage( U8 **Package, size_t *Size ){
  317. if( this ) {
  318. m_mBufferProtectorMutex.lock();
  319. if( m_vPackagePool.size() ){
  320. protocolPackage *tVal;
  321. tVal = m_vPackagePool.front();
  322. *Package =( U8 * ) malloc( tVal->m_nSize );
  323. if( *Package != NULL && tVal != NULL ) {
  324. memcpy( *Package, tVal->m_pData, tVal->m_nSize );
  325. *Size = tVal->m_nSize;
  326. m_vPackagePool.erase( m_vPackagePool.begin() );
  327. free( tVal );
  328. m_mBufferProtectorMutex.unlock();
  329. return true;
  330. };
  331. };
  332. m_mBufferProtectorMutex.unlock();
  333. };
  334. return false;
  335. };
  336. //#undef DUBUG_EXTRA
  337. //#undef DUBUG_COMMUNICATION
  338. #undef SLOG
  339. #undef SLOG_ERROR
  340. #undef LOG
  341. #undef LOG_ERROR