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.

452 lines
10 KiB

7 years ago
​
​
​
​
​
7 years ago
​
2 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
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
6 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
7 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
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. // #define DUBUG_EXTRA
  8. // #define DUBUG_COMMUNICATION
  9. protocolParser::protocolParser() :
  10. m_mBufferProtectorMutex()
  11. {
  12. m_pLog = basicLogCollection::getInstance();
  13. m_eConnectionStatus = CS_UNKNOWN;
  14. m_oDataTransport = NULL;
  15. m_pParserBuffer = (U8 *) malloc(PROTOCOL_PARSER_BUFFER_SIZE);
  16. if (m_pParserBuffer)
  17. {
  18. memset(m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
  19. }
  20. else
  21. {
  22. LOG_ERROR("Cannot allocate Parser Buffer!");
  23. }
  24. m_nParserDataCount = 0;
  25. m_bStatus = 0;
  26. m_pCustomHandler = NULL;
  27. m_bBufferAltered = false;
  28. m_bStatus = 1;
  29. #ifdef PROTOCOL_PARSER_IN_THREAD
  30. m_bParserThreadStatus = TH_PRE_INIT;
  31. #endif
  32. }
  33. protocolParser::~protocolParser()
  34. {
  35. #ifdef DEBUG_EXTRA
  36. LOG("Destructor Begin");
  37. #endif
  38. m_mBufferProtectorMutex.unlock();
  39. /*
  40. #ifdef PROTOCOL_PARSER_IN_THREAD
  41. if (m_bParserThreadStatus > TH_ARMED ) {
  42. waitParserThreadEnd( true );
  43. };
  44. if (m_bParserThreadStatus < TH_ENDED) {
  45. waitParserThreadEnd(true);
  46. };
  47. #endif
  48. */
  49. if (m_pParserBuffer != NULL)
  50. {
  51. free(m_pParserBuffer);
  52. };
  53. #ifdef DEBUG_EXTRA
  54. LOG("Destructor End");
  55. #endif
  56. }
  57. bool protocolParser::create(void)
  58. {
  59. if (m_pParserBuffer != NULL)
  60. {
  61. #ifdef PROTOCOL_PARSER_IN_THREAD
  62. if (m_bParserThreadStatus == TH_PRE_INIT)
  63. {
  64. m_bParserThreadStatus = TH_ARMED;
  65. #if defined(__linux__) || defined(WIN_PTHREAD)
  66. int RetVal = pthread_create(&m_sParserThreadHandler, NULL, m_fParserThreadEntryPoint, (void *) this);
  67. if (RetVal == -1)
  68. {
  69. LOG_ERROR("Thread Creation Failed.");
  70. return false;
  71. };
  72. #else
  73. m_hParserThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fParserThreadEntryPoint, (LPVOID) this, 0, &m_dwThreadID);
  74. // TODO: Add thread creation check
  75. #endif
  76. }
  77. else
  78. {
  79. LOG_ERROR("Thread object not ready to be created.");
  80. return false;
  81. };
  82. #endif
  83. return true;
  84. }
  85. else
  86. {
  87. return false;
  88. }
  89. }
  90. void protocolParser::setConnectionStatus(CONNECTION_STATUS cStatus)
  91. {
  92. #ifdef DEBUG_EXTRA
  93. if (cStatus == CS_CONNECTED)
  94. {
  95. LOG("Connection Status Changed - Connected!");
  96. }
  97. if (cStatus == CS_DISCONNECTED)
  98. {
  99. LOG("Connection Status Changed - Disconnected!");
  100. }
  101. #endif
  102. m_eConnectionStatus = cStatus;
  103. }
  104. CONNECTION_STATUS protocolParser::getConnectionStatus(void)
  105. {
  106. return m_eConnectionStatus;
  107. }
  108. bool protocolParser::setTransport(basicTransport * Transport, bool CAE)
  109. {
  110. #ifdef DEBUG_EXTRA
  111. LOG("Set Transport Req");
  112. #endif
  113. if (CAE)
  114. {
  115. if (m_oDataTransport != NULL)
  116. {
  117. LOG_ERROR("Transport Already Assigned!");
  118. return false;
  119. };
  120. }
  121. else
  122. { // Danger -> If transport Object is deleted the pointer is wrong!
  123. if (m_oDataTransport != NULL)
  124. {
  125. m_oDataTransport->removeRecipient(this);
  126. };
  127. };
  128. m_oDataTransport = Transport;
  129. if (Transport != NULL)
  130. {
  131. setConnectionStatus(CS_CONNECTED);
  132. return m_oDataTransport->addRecipient(this);
  133. }
  134. else
  135. {
  136. setConnectionStatus(CS_DISCONNECTED);
  137. }
  138. return true;
  139. }
  140. basicTransport * protocolParser::getCurrentTransport(void)
  141. {
  142. LOG("Get Transport Req");
  143. if (m_oDataTransport == NULL)
  144. {
  145. LOG("Transport is NULL");
  146. }
  147. return m_oDataTransport;
  148. }
  149. bool protocolParser::parse(U8 * Buffer, size_t BufferSize)
  150. {
  151. bool rv = false;
  152. if (m_pParserBuffer != NULL)
  153. {
  154. #ifdef DEBUG_COMMUNICATION
  155. char ttZZ[513];
  156. sprintf(ttZZ, "New %d bytes to Parse (allredy have in buffer: %d)", BufferSize, m_nParserDataCount);
  157. LOG(ttZZ);
  158. for (uint8_t i = 0; i < BufferSize; i++)
  159. {
  160. sprintf(ttZZ + (i * 2), "%02X", Buffer[i]);
  161. }
  162. LOG(ttZZ);
  163. #endif
  164. m_mBufferProtectorMutex.lock();
  165. if ((m_nParserDataCount + BufferSize) < PROTOCOL_PARSER_BUFFER_SIZE)
  166. {
  167. // if( BufferSize ){
  168. memcpy(m_pParserBuffer + m_nParserDataCount, Buffer, BufferSize);
  169. m_nParserDataCount += BufferSize;
  170. rv = m_bBufferAltered = true;
  171. //};
  172. #ifndef PROTOCOL_PARSER_IN_THREAD
  173. while (m_nParserDataCount > 0)
  174. {
  175. int RetVal;
  176. #ifdef DEBUG_COMMUNICATION
  177. LOG("Check Pack");
  178. #endif
  179. RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
  180. #ifdef DEBUG_COMMUNICATION
  181. LOG("Check Pack Done");
  182. #endif
  183. if (RetVal < 0)
  184. {
  185. // LOG("Clear 1b");
  186. // sprintf( ttZZ, "%d bytes to Eat", m_nParserDataCount );
  187. // LOG(ttZZ);
  188. // Note: RetVal is NEGATIVE!!??
  189. m_nParserDataCount += RetVal;
  190. memcpy(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
  191. // m_nParserDataCount--;
  192. // if( m_nParserDataCount ) memcpy( m_pParserBuffer, m_pParserBuffer+1, m_nParserDataCount );
  193. }
  194. else if (RetVal == 0)
  195. {
  196. protocolPackage * tVal = NULL;
  197. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  198. if (tVal != NULL)
  199. {
  200. #ifdef DEBUG_COMMUNICATION
  201. LOG("Package Accepted");
  202. char tBuf[256];
  203. sprintf(tBuf, "Pack Size: %d from %d bytes TOT %lld", tVal->m_nSize, m_nParserDataCount, BufferSize);
  204. LOG(tBuf);
  205. #endif
  206. // Move unused data toward buffer's begin to parse at next parse attempt
  207. m_nParserDataCount -= tVal->m_nSize;
  208. if (m_nParserDataCount > 0)
  209. {
  210. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  211. }
  212. else
  213. {
  214. // Clean Buff;
  215. memset(m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
  216. m_nParserDataCount = 0;
  217. };
  218. // In Case implementation of packetHandler - handler must return !true!
  219. if (! packetHandler(tVal))
  220. {
  221. #ifdef DEBUG_COMMUNICATION
  222. LOG("Handler Not Consumed Data!");
  223. #endif
  224. m_vPackagePool.push_back(tVal);
  225. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
  226. {
  227. #ifdef DEBUG_COMMUNICATION
  228. LOG_ERROR("Pack Pool Overflow!");
  229. #endif
  230. // Delete oldest package in queue
  231. m_vPackagePool.erase(m_vPackagePool.begin());
  232. dataEraser(tVal->m_pData);
  233. free(tVal);
  234. };
  235. }
  236. else
  237. {
  238. #ifdef DEBUG_COMMUNICATION
  239. LOG("Handler Consumed Data!");
  240. #endif
  241. dataEraser(tVal->m_pData);
  242. free(tVal);
  243. };
  244. }
  245. else
  246. {
  247. #ifdef DEBUG_COMMUNICATION
  248. LOG_ERROR("Packet Not Extracted!");
  249. #endif
  250. // All data consumed in MAVLINK proto but not parsed into message
  251. m_nParserDataCount = 0;
  252. };
  253. }
  254. else
  255. {
  256. #ifdef DEBUG_COMMUNICATION
  257. LOG("Harvest More!");
  258. #endif
  259. break;
  260. };
  261. };
  262. m_bBufferAltered = false;
  263. // m_mBufferProtectorMutex.unlock();
  264. #endif
  265. };
  266. m_mBufferProtectorMutex.unlock();
  267. };
  268. return rv;
  269. }
  270. void protocolParser::dataEraser(U8 *& tPtr)
  271. {
  272. if (tPtr == NULL) return;
  273. free(tPtr);
  274. tPtr = NULL;
  275. }
  276. #ifdef PROTOCOL_PARSER_IN_THREAD
  277. void protocolParser::m_fParserThreadProc(void)
  278. {
  279. int RetVal;
  280. m_bParserThreadStatus = TH_WORKING;
  281. while (m_bParserThreadStatus < TH_ENDED)
  282. {
  283. if (m_eConnectionStatus < CS_DISCONNECTED)
  284. {
  285. if ((m_bBufferAltered == false) && (m_bParserThreadStatus < TH_ENDED))
  286. {
  287. SLEEP(3);
  288. continue;
  289. };
  290. m_mBufferProtectorMutex.lock();
  291. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED))
  292. {
  293. #ifdef DEBUG_COMMUNICATION
  294. LOG("Check Pack");
  295. #endif
  296. RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
  297. #ifdef DEBUG_COMMUNICATION
  298. LOG("Check Pack Done");
  299. #endif
  300. if (RetVal < 0)
  301. {
  302. // Note: RetVal is NEGATIVE!!??
  303. m_nParserDataCount += RetVal;
  304. memcpy(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
  305. }
  306. else if (RetVal == 0)
  307. {
  308. protocolPackage * tVal = NULL;
  309. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  310. if (tVal != NULL)
  311. {
  312. #ifdef DEBUG_COMMUNICATION
  313. LOG("Package Handling");
  314. char tBuf[256];
  315. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  316. LOG(tBuf);
  317. #endif
  318. // Move unused data toward buffer's begin to parse at next parse attempt
  319. m_nParserDataCount -= tVal->m_nSize;
  320. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  321. // In Case implementation of packetHandler - handler must return !true!
  322. if (! packetHandler(tVal))
  323. {
  324. m_vPackagePool.push_back(tVal);
  325. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
  326. {
  327. // Delete oldest package in queue
  328. m_vPackagePool.erase(m_vPackagePool.begin());
  329. dataEraser(tVal->m_pData);
  330. free(tVal);
  331. };
  332. }
  333. else
  334. {
  335. dataEraser(tVal->m_pData);
  336. free(tVal);
  337. };
  338. }
  339. else
  340. {
  341. // All data consumed in MAVLINK proto but not parsed into message
  342. m_nParserDataCount = 0;
  343. };
  344. }
  345. else
  346. {
  347. break;
  348. };
  349. };
  350. m_bBufferAltered = false;
  351. m_mBufferProtectorMutex.unlock();
  352. }
  353. else
  354. {
  355. // Handle Disconnect
  356. m_bParserThreadStatus = TH_ENDED;
  357. };
  358. };
  359. }
  360. #if defined(__linux__) || defined(WIN_PTHREAD)
  361. void * protocolParser::m_fParserThreadEntryPoint(void * ptr)
  362. {
  363. #else
  364. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint(LPVOID ptr)
  365. {
  366. #endif
  367. protocolParser * mThis;
  368. mThis = (protocolParser *) ptr;
  369. if (mThis->m_bParserThreadStatus == TH_ARMED)
  370. {
  371. mThis->m_fParserThreadProc();
  372. mThis->m_bParserThreadStatus = TH_EXIT;
  373. }
  374. else
  375. {
  376. SLOG_ERROR("Thread is not ARMED!");
  377. };
  378. #ifdef __linux__
  379. pthread_exit(NULL);
  380. return NULL;
  381. #else
  382. return 0;
  383. #endif
  384. }
  385. void protocolParser::waitParserThreadEnd(bool forced)
  386. {
  387. if (m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED)
  388. {
  389. return;
  390. }
  391. if (forced)
  392. {
  393. m_bParserThreadStatus = TH_ENDED;
  394. }
  395. while (m_bParserThreadStatus != TH_EXIT)
  396. {
  397. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  398. };
  399. #if defined(__linux__) || defined(WIN_PTHREAD)
  400. pthread_join(m_sParserThreadHandler, NULL);
  401. #else
  402. WaitForSingleObject(m_hParserThreadHandle, 2000);
  403. CloseHandle(m_hParserThreadHandle);
  404. #endif
  405. }
  406. #endif // #ifndef PROTOCOL_PARSER_IN_THREAD
  407. bool protocolParser::getPackage(U8 ** Package, size_t * Size)
  408. {
  409. m_mBufferProtectorMutex.lock();
  410. if (m_vPackagePool.size())
  411. {
  412. protocolPackage * tVal;
  413. tVal = m_vPackagePool.front();
  414. *Package = (U8 *) malloc(tVal->m_nSize);
  415. if (*Package != NULL && tVal != NULL)
  416. {
  417. memcpy(*Package, tVal->m_pData, tVal->m_nSize);
  418. *Size = tVal->m_nSize;
  419. m_vPackagePool.erase(m_vPackagePool.begin());
  420. free(tVal);
  421. m_mBufferProtectorMutex.unlock();
  422. return true;
  423. };
  424. };
  425. m_mBufferProtectorMutex.unlock();
  426. return false;
  427. }
  428. // #undef DUBUG_EXTRA
  429. // #undef DUBUG_COMMUNICATION
  430. #undef SLOG
  431. #undef SLOG_ERROR
  432. #undef LOG
  433. #undef LOG_ERROR