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.

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