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.

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