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.

458 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
​
​
​
​
​
​
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)
  273. {
  274. return;
  275. }
  276. free(tPtr);
  277. tPtr = NULL;
  278. }
  279. #ifdef PROTOCOL_PARSER_IN_THREAD
  280. void protocolParser::m_fParserThreadProc(void)
  281. {
  282. int RetVal;
  283. m_bParserThreadStatus = TH_WORKING;
  284. while (m_bParserThreadStatus < TH_ENDED)
  285. {
  286. if (m_eConnectionStatus < CS_DISCONNECTED)
  287. {
  288. if ((m_bBufferAltered == false) && (m_bParserThreadStatus < TH_ENDED))
  289. {
  290. SLEEP(3);
  291. continue;
  292. };
  293. m_mBufferProtectorMutex.lock();
  294. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED))
  295. {
  296. #ifdef DEBUG_COMMUNICATION
  297. LOG("Check Pack");
  298. #endif
  299. RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
  300. #ifdef DEBUG_COMMUNICATION
  301. LOG("Check Pack Done");
  302. #endif
  303. if (RetVal < 0)
  304. {
  305. // Note: RetVal is NEGATIVE!!??
  306. m_nParserDataCount += RetVal;
  307. memcpy(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
  308. }
  309. else if (RetVal == 0)
  310. {
  311. protocolPackage * tVal = NULL;
  312. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  313. if (tVal != NULL)
  314. {
  315. #ifdef DEBUG_COMMUNICATION
  316. LOG("Package Handling");
  317. char tBuf[256];
  318. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  319. LOG(tBuf);
  320. #endif
  321. // Move unused data toward buffer's begin to parse at next parse attempt
  322. m_nParserDataCount -= tVal->m_nSize;
  323. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  324. // In Case implementation of packetHandler - handler must return !true!
  325. if (! packetHandler(tVal))
  326. {
  327. m_vPackagePool.push_back(tVal);
  328. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
  329. {
  330. // Delete oldest package in queue
  331. m_vPackagePool.erase(m_vPackagePool.begin());
  332. dataEraser(tVal->m_pData);
  333. free(tVal);
  334. };
  335. }
  336. else
  337. {
  338. dataEraser(tVal->m_pData);
  339. free(tVal);
  340. };
  341. }
  342. else
  343. {
  344. // All data consumed in MAVLINK proto but not parsed into message
  345. m_nParserDataCount = 0;
  346. };
  347. }
  348. else
  349. {
  350. break;
  351. };
  352. };
  353. m_bBufferAltered = false;
  354. m_mBufferProtectorMutex.unlock();
  355. }
  356. else
  357. {
  358. // Handle Disconnect
  359. m_bParserThreadStatus = TH_ENDED;
  360. };
  361. };
  362. }
  363. #if defined(__linux__) || defined(WIN_PTHREAD)
  364. void * protocolParser::m_fParserThreadEntryPoint(void * ptr)
  365. {
  366. #else
  367. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint(LPVOID ptr)
  368. {
  369. #endif
  370. protocolParser * mThis;
  371. mThis = (protocolParser *) ptr;
  372. if (mThis->m_bParserThreadStatus == TH_ARMED)
  373. {
  374. mThis->m_fParserThreadProc();
  375. mThis->m_bParserThreadStatus = TH_EXIT;
  376. }
  377. else
  378. {
  379. SLOG_ERROR("Thread is not ARMED!");
  380. };
  381. #ifdef __linux__
  382. pthread_exit(NULL);
  383. return NULL;
  384. #else
  385. return 0;
  386. #endif
  387. }
  388. void protocolParser::waitParserThreadEnd(bool forced)
  389. {
  390. if (m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED)
  391. {
  392. return;
  393. }
  394. if (forced)
  395. {
  396. m_bParserThreadStatus = TH_ENDED;
  397. }
  398. while (m_bParserThreadStatus != TH_EXIT)
  399. {
  400. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  401. };
  402. #if defined(__linux__) || defined(WIN_PTHREAD)
  403. pthread_join(m_sParserThreadHandler, NULL);
  404. #else
  405. WaitForSingleObject(m_hParserThreadHandle, 2000);
  406. CloseHandle(m_hParserThreadHandle);
  407. #endif
  408. }
  409. #endif // #ifndef PROTOCOL_PARSER_IN_THREAD
  410. bool protocolParser::getPackage(U8 ** Package, size_t * Size)
  411. {
  412. if (this)
  413. {
  414. m_mBufferProtectorMutex.lock();
  415. if (m_vPackagePool.size())
  416. {
  417. protocolPackage * tVal;
  418. tVal = m_vPackagePool.front();
  419. *Package = (U8 *) malloc(tVal->m_nSize);
  420. if (*Package != NULL && tVal != NULL)
  421. {
  422. memcpy(*Package, tVal->m_pData, tVal->m_nSize);
  423. *Size = tVal->m_nSize;
  424. m_vPackagePool.erase(m_vPackagePool.begin());
  425. free(tVal);
  426. m_mBufferProtectorMutex.unlock();
  427. return true;
  428. };
  429. };
  430. m_mBufferProtectorMutex.unlock();
  431. };
  432. return false;
  433. }
  434. // #undef DUBUG_EXTRA
  435. // #undef DUBUG_COMMUNICATION
  436. #undef SLOG
  437. #undef SLOG_ERROR
  438. #undef LOG
  439. #undef LOG_ERROR