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.

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