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