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.

448 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. } else if (RetVal == 0) {
  192. protocolPackage * tVal = NULL;
  193. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  194. if (tVal != NULL)
  195. {
  196. #ifdef DEBUG_COMMUNICATION
  197. LOG("Package Accepted");
  198. char tBuf[256];
  199. sprintf(tBuf, "Pack Size: %d from %d bytes TOT %lld", tVal->m_nSize, m_nParserDataCount, BufferSize);
  200. LOG(tBuf);
  201. #endif
  202. // Move unused data toward buffer's begin to parse at next parse attempt
  203. m_nParserDataCount -= tVal->m_nSize;
  204. if (m_nParserDataCount > 0)
  205. {
  206. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  207. }
  208. else
  209. {
  210. // Clean Buff;
  211. memset(m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
  212. m_nParserDataCount = 0;
  213. };
  214. // In Case implementation of packetHandler - handler must return !true!
  215. if (! packetHandler(tVal))
  216. {
  217. #ifdef DEBUG_COMMUNICATION
  218. LOG("Handler Not Consumed Data!");
  219. #endif
  220. m_vPackagePool.push_back(tVal);
  221. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
  222. {
  223. #ifdef DEBUG_COMMUNICATION
  224. LOG_ERROR("Pack Pool Overflow!");
  225. #endif
  226. // Delete oldest package in queue
  227. m_vPackagePool.erase(m_vPackagePool.begin());
  228. dataEraser(tVal->m_pData);
  229. free(tVal);
  230. };
  231. }
  232. else
  233. {
  234. #ifdef DEBUG_COMMUNICATION
  235. LOG("Handler Consumed Data!");
  236. #endif
  237. dataEraser(tVal->m_pData);
  238. free(tVal);
  239. };
  240. }
  241. else
  242. {
  243. #ifdef DEBUG_COMMUNICATION
  244. LOG_ERROR("Packet Not Extracted!");
  245. #endif
  246. // All data consumed in MAVLINK proto but not parsed into message
  247. m_nParserDataCount = 0;
  248. };
  249. }
  250. else
  251. {
  252. #ifdef DEBUG_COMMUNICATION
  253. LOG("Harvest More!");
  254. #endif
  255. break;
  256. };
  257. };
  258. m_bBufferAltered = false;
  259. // m_mBufferProtectorMutex.unlock();
  260. #endif
  261. };
  262. m_mBufferProtectorMutex.unlock();
  263. };
  264. return rv;
  265. }
  266. void protocolParser::dataEraser(U8 *& tPtr)
  267. {
  268. if (tPtr == NULL) return;
  269. free(tPtr);
  270. tPtr = NULL;
  271. }
  272. #ifdef PROTOCOL_PARSER_IN_THREAD
  273. void protocolParser::m_fParserThreadProc(void)
  274. {
  275. int RetVal;
  276. m_bParserThreadStatus = TH_WORKING;
  277. while (m_bParserThreadStatus < TH_ENDED)
  278. {
  279. if (m_eConnectionStatus < CS_DISCONNECTED)
  280. {
  281. if ((m_bBufferAltered == false) && (m_bParserThreadStatus < TH_ENDED))
  282. {
  283. SLEEP(3);
  284. continue;
  285. };
  286. m_mBufferProtectorMutex.lock();
  287. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED))
  288. {
  289. #ifdef DEBUG_COMMUNICATION
  290. LOG("Check Pack");
  291. #endif
  292. RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
  293. #ifdef DEBUG_COMMUNICATION
  294. LOG("Check Pack Done");
  295. #endif
  296. if (RetVal < 0)
  297. {
  298. // Note: RetVal is NEGATIVE!!??
  299. m_nParserDataCount += RetVal;
  300. memcpy(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
  301. }
  302. else if (RetVal == 0)
  303. {
  304. protocolPackage * tVal = NULL;
  305. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  306. if (tVal != NULL)
  307. {
  308. #ifdef DEBUG_COMMUNICATION
  309. LOG("Package Handling");
  310. char tBuf[256];
  311. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  312. LOG(tBuf);
  313. #endif
  314. // Move unused data toward buffer's begin to parse at next parse attempt
  315. m_nParserDataCount -= tVal->m_nSize;
  316. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  317. // In Case implementation of packetHandler - handler must return !true!
  318. if (! packetHandler(tVal))
  319. {
  320. m_vPackagePool.push_back(tVal);
  321. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
  322. {
  323. // Delete oldest package in queue
  324. m_vPackagePool.erase(m_vPackagePool.begin());
  325. dataEraser(tVal->m_pData);
  326. free(tVal);
  327. };
  328. }
  329. else
  330. {
  331. dataEraser(tVal->m_pData);
  332. free(tVal);
  333. };
  334. }
  335. else
  336. {
  337. // All data consumed in MAVLINK proto but not parsed into message
  338. m_nParserDataCount = 0;
  339. };
  340. }
  341. else
  342. {
  343. break;
  344. };
  345. };
  346. m_bBufferAltered = false;
  347. m_mBufferProtectorMutex.unlock();
  348. }
  349. else
  350. {
  351. // Handle Disconnect
  352. m_bParserThreadStatus = TH_ENDED;
  353. };
  354. };
  355. }
  356. #if defined(__linux__) || defined(WIN_PTHREAD)
  357. void * protocolParser::m_fParserThreadEntryPoint(void * ptr)
  358. {
  359. #else
  360. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint(LPVOID ptr)
  361. {
  362. #endif
  363. protocolParser * mThis;
  364. mThis = (protocolParser *) ptr;
  365. if (mThis->m_bParserThreadStatus == TH_ARMED)
  366. {
  367. mThis->m_fParserThreadProc();
  368. mThis->m_bParserThreadStatus = TH_EXIT;
  369. }
  370. else
  371. {
  372. SLOG_ERROR("Thread is not ARMED!");
  373. };
  374. #ifdef __linux__
  375. pthread_exit(NULL);
  376. return NULL;
  377. #else
  378. return 0;
  379. #endif
  380. }
  381. void protocolParser::waitParserThreadEnd(bool forced)
  382. {
  383. if (m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED)
  384. {
  385. return;
  386. }
  387. if (forced)
  388. {
  389. m_bParserThreadStatus = TH_ENDED;
  390. }
  391. while (m_bParserThreadStatus != TH_EXIT)
  392. {
  393. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  394. };
  395. #if defined(__linux__) || defined(WIN_PTHREAD)
  396. pthread_join(m_sParserThreadHandler, NULL);
  397. #else
  398. WaitForSingleObject(m_hParserThreadHandle, 2000);
  399. CloseHandle(m_hParserThreadHandle);
  400. #endif
  401. }
  402. #endif // #ifndef PROTOCOL_PARSER_IN_THREAD
  403. bool protocolParser::getPackage(U8 ** Package, size_t * Size)
  404. {
  405. m_mBufferProtectorMutex.lock();
  406. if (m_vPackagePool.size())
  407. {
  408. protocolPackage * tVal;
  409. tVal = m_vPackagePool.front();
  410. *Package = (U8 *) malloc(tVal->m_nSize);
  411. if (*Package != NULL && tVal != NULL)
  412. {
  413. memcpy(*Package, tVal->m_pData, tVal->m_nSize);
  414. *Size = tVal->m_nSize;
  415. m_vPackagePool.erase(m_vPackagePool.begin());
  416. free(tVal);
  417. m_mBufferProtectorMutex.unlock();
  418. return true;
  419. };
  420. };
  421. m_mBufferProtectorMutex.unlock();
  422. return false;
  423. }
  424. // #undef DUBUG_EXTRA
  425. // #undef DUBUG_COMMUNICATION
  426. #undef SLOG
  427. #undef SLOG_ERROR
  428. #undef LOG
  429. #undef LOG_ERROR