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.

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