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.

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