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.

297 lines
8.7 KiB

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