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.

316 lines
9.4 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
7 years ago
​
6 years ago
7 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. LOG("C0");
  16. m_nParserDataCount = 0;
  17. m_bStatus = 0;
  18. m_pCustomHandler = NULL;
  19. m_bBufferAltered = false;
  20. m_bStatus = 1;
  21. #ifdef PROTOCOL_PARSER_IN_THREAD
  22. m_bParserThreadStatus = TH_PRE_INIT;
  23. #endif
  24. };
  25. protocolParser::~protocolParser(){
  26. # ifdef DEBUG
  27. LOG("Destructor Begin");
  28. # endif
  29. m_mBufferProtectorMutex.unlock();
  30. /*
  31. #ifdef PROTOCOL_PARSER_IN_THREAD
  32. if (m_bParserThreadStatus > TH_ARMED ) {
  33. waitParserThreadEnd( true );
  34. };
  35. if (m_bParserThreadStatus < TH_ENDED) {
  36. waitParserThreadEnd(true);
  37. };
  38. #endif
  39. */
  40. if( m_pParserBuffer != NULL ) {
  41. free( m_pParserBuffer );
  42. };
  43. # ifdef DEBUG
  44. LOG("Destructor End");
  45. # endif
  46. };
  47. bool protocolParser::create(void){
  48. if (m_pParserBuffer != NULL) {
  49. #ifdef PROTOCOL_PARSER_IN_THREAD
  50. if (m_bParserThreadStatus == TH_PRE_INIT) {
  51. m_bParserThreadStatus = TH_ARMED;
  52. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  53. int RetVal = pthread_create( &m_sParserThreadHandler, NULL, m_fParserThreadEntryPoint, ( void * ) this );
  54. if( RetVal == -1 ) {
  55. LOG_ERROR("Thread Creation Failed.");
  56. return false;
  57. };
  58. #else
  59. m_hParserThreadHandle = CreateThread(
  60. NULL,
  61. 0,
  62. (LPTHREAD_START_ROUTINE)m_fParserThreadEntryPoint,
  63. (LPVOID)this,
  64. 0,
  65. &m_dwThreadID );
  66. //TODO: Add thread creation check
  67. #endif
  68. } else {
  69. LOG_ERROR("Thread object not ready to be created.");
  70. return false;
  71. };
  72. #endif
  73. return true;
  74. } else return false;
  75. };
  76. void protocolParser::setConnectionStatus( CONNECTION_STATUS cStatus ){
  77. #ifdef DEBUG
  78. if (cStatus == CS_CONNECTED)LOG("Connection Status Changed - Connected!");
  79. if (cStatus == CS_DISCONNECTED)LOG("Connection Status Changed - Disconnected!");
  80. #endif
  81. m_eConnectionStatus = cStatus;
  82. };
  83. CONNECTION_STATUS protocolParser::getConnectionStatus( void ){
  84. return m_eConnectionStatus;
  85. };
  86. bool protocolParser::setTransport( basicTransport* Transport, bool check_if_allready ){
  87. if (Transport == NULL) return false;
  88. if( check_if_allready ) {
  89. if (m_oDataTransport != NULL) {
  90. LOG_ERROR("Transport Already Assigned!");
  91. return false;
  92. };
  93. };
  94. m_oDataTransport = Transport;
  95. return m_oDataTransport->addRecipient( this );
  96. };
  97. bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){
  98. bool rv = false;
  99. if( m_pParserBuffer != NULL ) {
  100. #ifdef DUBUG_COMMUNICATION
  101. char ttZZ[256];
  102. sprintf( ttZZ, "New %d bytes to Parse (allredy have in buffer: %d)", BufferSize, m_nParserDataCount );
  103. LOG(ttZZ);
  104. #endif
  105. m_mBufferProtectorMutex.lock();
  106. if( ( m_nParserDataCount + BufferSize ) < PROTOCOL_PARSER_BUFFER_SIZE ) {
  107. memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
  108. m_nParserDataCount+=BufferSize;
  109. rv = m_bBufferAltered = true;
  110. #ifndef PROTOCOL_PARSER_IN_THREAD
  111. while ( m_nParserDataCount > 0 ){
  112. int RetVal;
  113. #ifdef DUBUG_COMMUNICATION
  114. LOG("Check Pack");
  115. #endif
  116. RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
  117. #ifdef DUBUG_COMMUNICATION
  118. LOG("Check Pack Done");
  119. #endif
  120. if (RetVal < 0) {
  121. // Note: RetVal is NEGATIVE!!??
  122. m_nParserDataCount += RetVal;
  123. memcpy(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
  124. } else if (RetVal == 0) {
  125. protocolPackage* tVal = NULL;
  126. tVal = packetExtractor( m_pParserBuffer, m_nParserDataCount );
  127. if (tVal != NULL) {
  128. #ifdef DUBUG_COMMUNICATION
  129. LOG("Package Handling");
  130. char tBuf[256];
  131. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  132. LOG(tBuf);
  133. #endif
  134. //Move unused data toward buffer's begin to parse at next parse attempt
  135. m_nParserDataCount -= tVal->m_nSize;
  136. if( m_nParserDataCount > 0 ) {
  137. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  138. } else {
  139. // Clean Buff;
  140. memset( m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
  141. m_nParserDataCount = 0;
  142. };
  143. // In Case implementation of packetHandler - handler must return !true!
  144. if (!packetHandler(tVal)) {
  145. m_vPackagePool.push_back(tVal);
  146. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  147. // Delete oldest package in queue
  148. m_vPackagePool.erase(m_vPackagePool.begin());
  149. dataEraser(tVal->m_pData);
  150. free(tVal);
  151. };
  152. } else {
  153. dataEraser(tVal->m_pData);
  154. free(tVal);
  155. };
  156. } else {
  157. // All data consumed in MAVLINK proto but not parsed into message
  158. m_nParserDataCount = 0;
  159. };
  160. } else {
  161. break;
  162. };
  163. };
  164. m_bBufferAltered = false;
  165. //m_mBufferProtectorMutex.unlock();
  166. #endif
  167. };
  168. m_mBufferProtectorMutex.unlock();
  169. };
  170. return rv;
  171. };
  172. void protocolParser::dataEraser( U8 *&tPtr ){
  173. if (tPtr == NULL) return;
  174. free(tPtr);
  175. tPtr = NULL;
  176. };
  177. #ifdef PROTOCOL_PARSER_IN_THREAD
  178. void protocolParser::m_fParserThreadProc(void){
  179. int RetVal;
  180. m_bParserThreadStatus = TH_WORKING;
  181. while(m_bParserThreadStatus < TH_ENDED){
  182. if( m_eConnectionStatus < CS_DISCONNECTED ){
  183. if( ( m_bBufferAltered == false ) && ( m_bParserThreadStatus < TH_ENDED ) ) {
  184. SLEEP(3);
  185. continue;
  186. };
  187. m_mBufferProtectorMutex.lock();
  188. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED)){
  189. #ifdef DUBUG_COMMUNICATION
  190. LOG("Check Pack");
  191. #endif
  192. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  193. #ifdef DUBUG_COMMUNICATION
  194. LOG("Check Pack Done");
  195. #endif
  196. if( RetVal < 0 ){
  197. // Note: RetVal is NEGATIVE!!??
  198. m_nParserDataCount+=RetVal;
  199. memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount );
  200. } else if ( RetVal == 0 ) {
  201. protocolPackage* tVal = NULL;
  202. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  203. if (tVal != NULL) {
  204. #ifdef DUBUG_COMMUNICATION
  205. LOG("Package Handling");
  206. char tBuf[256];
  207. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  208. LOG(tBuf);
  209. #endif
  210. //Move unused data toward buffer's begin to parse at next parse attempt
  211. m_nParserDataCount -= tVal->m_nSize;
  212. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  213. // In Case implementation of packetHandler - handler must return !true!
  214. if( !packetHandler(tVal) ){
  215. m_vPackagePool.push_back( tVal );
  216. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  217. // Delete oldest package in queue
  218. m_vPackagePool.erase(m_vPackagePool.begin());
  219. dataEraser(tVal->m_pData);
  220. free(tVal);
  221. };
  222. } else {
  223. dataEraser(tVal->m_pData);
  224. free(tVal);
  225. };
  226. } else {
  227. // All data consumed in MAVLINK proto but not parsed into message
  228. m_nParserDataCount = 0;
  229. };
  230. } else {
  231. break;
  232. };
  233. };
  234. m_bBufferAltered = false;
  235. m_mBufferProtectorMutex.unlock();
  236. } else {
  237. // Handle Disconnect
  238. m_bParserThreadStatus = TH_ENDED;
  239. };
  240. };
  241. };
  242. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  243. void *protocolParser::m_fParserThreadEntryPoint( void *ptr ){
  244. #else
  245. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint( LPVOID ptr){
  246. #endif
  247. protocolParser *mThis;
  248. mThis = ( protocolParser * ) ptr;
  249. if( mThis->m_bParserThreadStatus == TH_ARMED ) {
  250. mThis->m_fParserThreadProc();
  251. mThis->m_bParserThreadStatus = TH_EXIT;
  252. } else {
  253. SLOG_ERROR("Thread is not ARMED!");
  254. };
  255. #ifdef __linux__
  256. pthread_exit( NULL );
  257. return NULL;
  258. #else
  259. return 0;
  260. #endif
  261. };
  262. void protocolParser::waitParserThreadEnd(bool forced){
  263. if( m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED ) return;
  264. if( forced ) m_bParserThreadStatus = TH_ENDED;
  265. while (m_bParserThreadStatus != TH_EXIT){
  266. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  267. };
  268. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  269. pthread_join(m_sParserThreadHandler, NULL);
  270. #else
  271. WaitForSingleObject(m_hParserThreadHandle, 2000);
  272. CloseHandle(m_hParserThreadHandle);
  273. #endif
  274. };
  275. #endif //#ifndef PROTOCOL_PARSER_IN_THREAD
  276. bool protocolParser::getPackage( U8 **Package, size_t *Size ){
  277. if( this ) {
  278. m_mBufferProtectorMutex.lock();
  279. if( m_vPackagePool.size() ){
  280. protocolPackage *tVal;
  281. tVal = m_vPackagePool.front();
  282. *Package =( U8 * ) malloc( tVal->m_nSize );
  283. if( *Package != NULL && tVal != NULL ) {
  284. memcpy( *Package, tVal->m_pData, tVal->m_nSize );
  285. *Size = tVal->m_nSize;
  286. m_vPackagePool.erase( m_vPackagePool.begin() );
  287. free( tVal );
  288. m_mBufferProtectorMutex.unlock();
  289. return true;
  290. };
  291. };
  292. m_mBufferProtectorMutex.unlock();
  293. };
  294. return false;
  295. };
  296. #undef SLOG
  297. #undef SLOG_ERROR
  298. #undef LOG
  299. #undef LOG_ERROR