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.

457 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
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day 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
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day 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
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
7 years ago
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
7 years ago
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day 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. // Release packages that were never fetched from the pool
  50. for (packagePoll::iterator idx = m_vPackagePool.begin(); idx != m_vPackagePool.end(); idx++)
  51. {
  52. if (*idx)
  53. {
  54. dataEraser((*idx)->m_pData);
  55. free(*idx);
  56. };
  57. };
  58. m_vPackagePool.clear();
  59. if (m_pParserBuffer != NULL)
  60. {
  61. free(m_pParserBuffer);
  62. };
  63. #ifdef DEBUG_EXTRA
  64. LOG("Destructor End");
  65. #endif
  66. }
  67. bool protocolParser::create(void)
  68. {
  69. if (m_pParserBuffer != NULL)
  70. {
  71. #ifdef PROTOCOL_PARSER_IN_THREAD
  72. if (m_bParserThreadStatus == TH_PRE_INIT)
  73. {
  74. m_bParserThreadStatus = TH_ARMED;
  75. #if defined(__linux__) || defined(WIN_PTHREAD)
  76. int RetVal = pthread_create(&m_sParserThreadHandler, NULL, m_fParserThreadEntryPoint, (void *) this);
  77. if (RetVal == -1)
  78. {
  79. LOG_ERROR("Thread Creation Failed.");
  80. return false;
  81. };
  82. #else
  83. m_hParserThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fParserThreadEntryPoint, (LPVOID) this, 0, &m_dwThreadID);
  84. // TODO: Add thread creation check
  85. #endif
  86. }
  87. else
  88. {
  89. LOG_ERROR("Thread object not ready to be created.");
  90. return false;
  91. };
  92. #endif
  93. return true;
  94. }
  95. else
  96. {
  97. return false;
  98. }
  99. }
  100. void protocolParser::setConnectionStatus(CONNECTION_STATUS cStatus)
  101. {
  102. #ifdef DEBUG_EXTRA
  103. if (cStatus == CS_CONNECTED)
  104. {
  105. LOG("Connection Status Changed - Connected!");
  106. }
  107. if (cStatus == CS_DISCONNECTED)
  108. {
  109. LOG("Connection Status Changed - Disconnected!");
  110. }
  111. #endif
  112. m_eConnectionStatus = cStatus;
  113. }
  114. CONNECTION_STATUS protocolParser::getConnectionStatus(void)
  115. {
  116. return m_eConnectionStatus;
  117. }
  118. bool protocolParser::setTransport(basicTransport * Transport, bool CAE)
  119. {
  120. #ifdef DEBUG_EXTRA
  121. LOG("Set Transport Req");
  122. #endif
  123. if (CAE)
  124. {
  125. if (m_oDataTransport != NULL)
  126. {
  127. LOG_ERROR("Transport Already Assigned!");
  128. return false;
  129. };
  130. }
  131. else
  132. { // Danger -> If transport Object is deleted the pointer is wrong!
  133. if (m_oDataTransport != NULL)
  134. {
  135. m_oDataTransport->removeRecipient(this);
  136. };
  137. };
  138. m_oDataTransport = Transport;
  139. if (Transport != NULL)
  140. {
  141. setConnectionStatus(CS_CONNECTED);
  142. return m_oDataTransport->addRecipient(this);
  143. }
  144. else
  145. {
  146. setConnectionStatus(CS_DISCONNECTED);
  147. }
  148. return true;
  149. }
  150. basicTransport * protocolParser::getCurrentTransport(void)
  151. {
  152. LOG("Get Transport Req");
  153. if (m_oDataTransport == NULL)
  154. {
  155. LOG("Transport is NULL");
  156. }
  157. return m_oDataTransport;
  158. }
  159. //#define DEBUG_COMMUNICATION
  160. bool protocolParser::parse(U8 * Buffer, size_t BufferSize)
  161. {
  162. bool rv = false;
  163. if (m_pParserBuffer != NULL)
  164. {
  165. #ifdef DEBUG_COMMUNICATION
  166. char ttZZ[1024];
  167. sprintf(ttZZ, "New %d bytes to Parse (allredy have in buffer: %d)", BufferSize, m_nParserDataCount);
  168. LOG(ttZZ);
  169. for (uint8_t i = 0; i < BufferSize; i++) sprintf(ttZZ + (i * 2), "%02X", Buffer[i]);
  170. LOG(ttZZ);
  171. #endif
  172. m_mBufferProtectorMutex.lock();
  173. if ((m_nParserDataCount + BufferSize) < PROTOCOL_PARSER_BUFFER_SIZE)
  174. {
  175. // if( BufferSize ){
  176. memcpy(m_pParserBuffer + m_nParserDataCount, Buffer, BufferSize);
  177. m_nParserDataCount += BufferSize;
  178. #ifdef DEBUG_COMMUNICATION
  179. for (uint8_t i = 0; i < m_nParserDataCount; i++) sprintf(ttZZ + (i * 2), "%02X", m_pParserBuffer[i]);
  180. LOG(ttZZ);
  181. #endif
  182. m_bBufferAltered = true;
  183. //};
  184. #ifndef PROTOCOL_PARSER_IN_THREAD
  185. while (m_nParserDataCount > 0)
  186. {
  187. int RetVal;
  188. #ifdef DEBUG_COMMUNICATION
  189. LOG("Check Pack");
  190. #endif
  191. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  192. #ifdef DEBUG_COMMUNICATION
  193. LOG("Check Pack Done");
  194. #endif
  195. if (RetVal < 0){
  196. #ifdef DEBUG_COMMUNICATION
  197. LOG("Clear 1b");
  198. sprintf( ttZZ, "%d bytes to Eat", m_nParserDataCount );
  199. LOG(ttZZ);
  200. #endif
  201. // Note: RetVal is NEGATIVE!!??
  202. m_nParserDataCount += RetVal;
  203. memmove(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
  204. } else if (RetVal == 0) {
  205. protocolPackage * tVal = NULL;
  206. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  207. if (tVal != NULL){
  208. #ifdef DEBUG_COMMUNICATION
  209. LOG("Package Accepted");
  210. char tBuf[256];
  211. sprintf(tBuf, "Pack Size: %d from %d bytes TOT %lld", tVal->m_nSize, m_nParserDataCount, BufferSize);
  212. LOG(tBuf);
  213. #endif
  214. // Move unused data toward buffer's begin to parse at next parse attempt
  215. m_nParserDataCount -= tVal->m_nSize;
  216. if (m_nParserDataCount > 0){
  217. memmove(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  218. } else {
  219. // Clean Buff;
  220. memset(m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
  221. m_nParserDataCount = 0;
  222. };
  223. // In Case implementation of packetHandler - handler must return !true!
  224. if (! packetHandler(tVal)) {
  225. #ifdef DEBUG_COMMUNICATION
  226. LOG("Handler Not Consumed Data!");
  227. #endif
  228. m_vPackagePool.push_back(tVal);
  229. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
  230. {
  231. #ifdef DEBUG_COMMUNICATION
  232. LOG_ERROR("Pack Pool Overflow!");
  233. #endif
  234. // Delete oldest package in queue (free the removed one, not the newly pooled one)
  235. protocolPackage * tOld = m_vPackagePool.front();
  236. m_vPackagePool.erase(m_vPackagePool.begin());
  237. dataEraser(tOld->m_pData);
  238. free(tOld);
  239. };
  240. } else {
  241. #ifdef DEBUG_COMMUNICATION
  242. LOG("Handler Consumed Data!");
  243. #endif
  244. dataEraser(tVal->m_pData);
  245. free(tVal);
  246. };
  247. rv = true; // Package Extracted From Data
  248. } else {
  249. #ifdef DEBUG_COMMUNICATION
  250. LOG_ERROR("Packet Not Extracted!");
  251. #endif
  252. // All data consumed in MAVLINK proto but not parsed into message
  253. m_nParserDataCount = 0;
  254. };
  255. } else {
  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. //#undef DEBUG_COMMUNICATION
  271. void protocolParser::dataEraser(U8 *& tPtr)
  272. {
  273. if (tPtr == NULL) return;
  274. free(tPtr);
  275. tPtr = NULL;
  276. }
  277. #ifdef PROTOCOL_PARSER_IN_THREAD
  278. void protocolParser::m_fParserThreadProc(void)
  279. {
  280. int RetVal;
  281. m_bParserThreadStatus = TH_WORKING;
  282. while (m_bParserThreadStatus < TH_ENDED)
  283. {
  284. if (m_eConnectionStatus < CS_DISCONNECTED)
  285. {
  286. if ((m_bBufferAltered == false) && (m_bParserThreadStatus < TH_ENDED))
  287. {
  288. SLEEP(3);
  289. continue;
  290. };
  291. m_mBufferProtectorMutex.lock();
  292. while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED))
  293. {
  294. #ifdef DEBUG_COMMUNICATION
  295. LOG("Check Pack");
  296. #endif
  297. RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
  298. #ifdef DEBUG_COMMUNICATION
  299. LOG("Check Pack Done");
  300. #endif
  301. if (RetVal < 0)
  302. {
  303. // Note: RetVal is NEGATIVE!!??
  304. m_nParserDataCount += RetVal;
  305. memmove(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
  306. }
  307. else if (RetVal == 0)
  308. {
  309. protocolPackage * tVal = NULL;
  310. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  311. if (tVal != NULL)
  312. {
  313. #ifdef DEBUG_COMMUNICATION
  314. LOG("Package Handling");
  315. char tBuf[256];
  316. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  317. LOG(tBuf);
  318. #endif
  319. // Move unused data toward buffer's begin to parse at next parse attempt
  320. m_nParserDataCount -= tVal->m_nSize;
  321. memmove(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  322. // In Case implementation of packetHandler - handler must return !true!
  323. if (! packetHandler(tVal))
  324. {
  325. m_vPackagePool.push_back(tVal);
  326. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
  327. {
  328. // Delete oldest package in queue (free the removed one, not the newly pooled one)
  329. protocolPackage * tOld = m_vPackagePool.front();
  330. m_vPackagePool.erase(m_vPackagePool.begin());
  331. dataEraser(tOld->m_pData);
  332. free(tOld);
  333. };
  334. }
  335. else
  336. {
  337. dataEraser(tVal->m_pData);
  338. free(tVal);
  339. };
  340. }
  341. else
  342. {
  343. // All data consumed in MAVLINK proto but not parsed into message
  344. m_nParserDataCount = 0;
  345. };
  346. }
  347. else
  348. {
  349. break;
  350. };
  351. };
  352. m_bBufferAltered = false;
  353. m_mBufferProtectorMutex.unlock();
  354. }
  355. else
  356. {
  357. // Handle Disconnect
  358. m_bParserThreadStatus = TH_ENDED;
  359. };
  360. };
  361. }
  362. #if defined(__linux__) || defined(WIN_PTHREAD)
  363. void * protocolParser::m_fParserThreadEntryPoint(void * ptr)
  364. {
  365. #else
  366. DWORD WINAPI protocolParser::m_fParserThreadEntryPoint(LPVOID ptr)
  367. {
  368. #endif
  369. protocolParser * mThis;
  370. mThis = (protocolParser *) ptr;
  371. if (mThis->m_bParserThreadStatus == TH_ARMED)
  372. {
  373. mThis->m_fParserThreadProc();
  374. mThis->m_bParserThreadStatus = TH_EXIT;
  375. }
  376. else
  377. {
  378. SLOG_ERROR("Thread is not ARMED!");
  379. };
  380. #ifdef __linux__
  381. pthread_exit(NULL);
  382. return NULL;
  383. #else
  384. return 0;
  385. #endif
  386. }
  387. void protocolParser::waitParserThreadEnd(bool forced)
  388. {
  389. if (m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED)
  390. {
  391. return;
  392. }
  393. if (forced)
  394. {
  395. m_bParserThreadStatus = TH_ENDED;
  396. }
  397. while (m_bParserThreadStatus != TH_EXIT)
  398. {
  399. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  400. };
  401. #if defined(__linux__) || defined(WIN_PTHREAD)
  402. pthread_join(m_sParserThreadHandler, NULL);
  403. #else
  404. WaitForSingleObject(m_hParserThreadHandle, 2000);
  405. CloseHandle(m_hParserThreadHandle);
  406. #endif
  407. }
  408. #endif // #ifndef PROTOCOL_PARSER_IN_THREAD
  409. bool protocolParser::getPackage(U8 ** Package, size_t * Size)
  410. {
  411. m_mBufferProtectorMutex.lock();
  412. if (m_vPackagePool.size())
  413. {
  414. protocolPackage * tVal;
  415. tVal = m_vPackagePool.front();
  416. *Package = (U8 *) malloc(tVal->m_nSize);
  417. if (*Package != NULL && tVal != NULL)
  418. {
  419. memcpy(*Package, tVal->m_pData, tVal->m_nSize);
  420. *Size = tVal->m_nSize;
  421. m_vPackagePool.erase(m_vPackagePool.begin());
  422. dataEraser(tVal->m_pData);
  423. free(tVal);
  424. m_mBufferProtectorMutex.unlock();
  425. return true;
  426. };
  427. };
  428. m_mBufferProtectorMutex.unlock();
  429. return false;
  430. }
  431. // #undef DUBUG_EXTRA
  432. // #undef DUBUG_COMMUNICATION
  433. #undef SLOG
  434. #undef SLOG_ERROR
  435. #undef LOG
  436. #undef LOG_ERROR