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.

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