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.

478 lines
12 KiB

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