Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef _MAV2DJI_MODULEBUF_H_
00010 #define _MAV2DJI_MODULEBUF_H_
00011
00012
00013 #include <new>
00014 #include <string>
00015 #include <mutex>
00016
00017 namespace dji2mav{
00018
00019 class ModuleBuf {
00020 public:
00021 ModuleBuf(uint16_t bufSize) {
00022 DJI2MAV_DEBUG("Going to construct ModuleBuf with bufSize "
00023 "%u...", bufSize);
00024
00025 m_bufSize = bufSize;
00026 m_head = 0;
00027 m_bufUsedAmount = 0;
00028
00029 try {
00030 m_buf = new uint8_t[m_bufSize];
00031 memset(m_buf, 0, sizeof(uint8_t) * m_bufSize);
00032 } catch(std::bad_alloc& m) {
00033 DJI2MAV_FATAL( "Failed to alloc memory for moduleBuf! "
00034 "Exception: %s!", m.what() );
00035 exit(EXIT_FAILURE);
00036 }
00037
00038 DJI2MAV_DEBUG("...finish constructing ModuleBuf.");
00039
00040 }
00041
00042
00043 ~ModuleBuf() {
00044 if(NULL != m_buf) {
00045 delete []m_buf;
00046 m_buf = NULL;
00047 }
00048 }
00049
00050
00055 inline uint16_t getBufSize() {
00056 return m_bufSize;
00057 }
00058
00059
00064 inline uint16_t getBufUsedAmount() {
00065 return m_bufUsedAmount;
00066 }
00067
00068
00075 bool writeBuf(const uint8_t *src, uint16_t len) {
00076
00077 if( m_bufUsedAmount + len > m_bufSize ) {
00078 return false;
00079 } else {
00080 m_bufMutex.lock();
00081 uint16_t tail = (m_head + m_bufUsedAmount) % m_bufSize;
00082 if(len + tail < m_bufSize) {
00083 memcpy(m_buf + tail, src, len);
00084 } else {
00085 uint16_t bSize = m_bufSize - tail;
00086 memcpy(m_buf + tail, src, bSize);
00087 memcpy(m_buf, src + bSize, len - bSize);
00088 }
00089 m_bufUsedAmount += len;
00090 m_bufMutex.unlock();
00091 return true;
00092 }
00093
00094 }
00095
00096
00103 bool readBuf(uint8_t *dest, uint16_t len) {
00104
00105 if( len > m_bufUsedAmount ) {
00106 return false;
00107 } else {
00108 m_bufMutex.lock();
00109 if(m_head + len < m_bufSize) {
00110 memcpy(dest, m_buf + m_head, len);
00111 m_head += len;
00112 } else {
00113 memcpy(dest, m_buf + m_head, m_bufSize - m_head);
00114 memcpy(dest + m_bufSize - m_head, m_buf,
00115 len + m_head - m_bufSize);
00116 m_head -= m_bufSize - len;
00117 }
00118 m_bufUsedAmount -= len;
00119 m_bufMutex.unlock();
00120 return true;
00121 }
00122 }
00123
00124
00125
00126
00127
00128 void display() {
00129 DJI2MAV_INFO("In ModuleBuf head: %u, used: %u, buf: ",
00130 m_head, m_bufUsedAmount);
00131 for(int i = 0; i < m_bufSize; ++i) {
00132 printf("%02x", m_buf[i]);
00133 }
00134 printf("\n");
00135 DJI2MAV_INFO("--- End of display ---");
00136 }
00137
00138
00142 void clear() {
00143 DJI2MAV_DEBUG("Going to clear the buffer with used amount "
00144 "%u...", m_bufUsedAmount);
00145 m_bufMutex.lock();
00146 m_bufUsedAmount = 0;
00147 m_bufMutex.unlock();
00148 DJI2MAV_DEBUG("...finish clearing the buffer. Head point to "
00149 "%u.", m_head);
00150 }
00151
00152
00153 private:
00154 uint8_t *m_buf;
00155 uint16_t m_bufSize;
00156 uint16_t m_head;
00157 uint16_t m_bufUsedAmount;
00158 std::mutex m_bufMutex;
00159 };
00160
00161 }
00162
00163 #endif