diff --git a/radio/Formatter.cpp b/radio/Formatter.cpp index e2cba94..334ab6b 100644 --- a/radio/Formatter.cpp +++ b/radio/Formatter.cpp @@ -2,6 +2,8 @@ #include #include #include +#include + #include #include #include "Formatter.hpp" @@ -27,46 +29,24 @@ Formatter::~Formatter() { } -void Formatter::idle() -{ - if (m_dataCount) - { - // Commit partial DATA frame - commit(FRAME_TYPE_DATA, m_dataCount); - m_dataCount = 0; - } - else - { - // IDLE frame not full - if (m_idleCount < FRAME_NUM_BYTES_PER_FRAME) - { - m_frame.data[m_idleCount++] = 0; - } - - if (m_idleCount == FRAME_NUM_BYTES_PER_FRAME) - { - // Commit full IDLE frame of size FRAME_NUM_BYTES_PER_FRAME - commit(FRAME_TYPE_IDLE, m_idleCount); - m_idleCount = 0; - } - } -} - -void Formatter::process(uint8_t data) +void Formatter::process_data(uint8_t *pData, uint32_t len) { if (m_idleCount) { - // Commit partial IDLE frame + // Idle in progress: Commit partial IDLE frame commit(FRAME_TYPE_IDLE, m_idleCount); m_idleCount = 0; } - else + + size_t remain = len; + while(remain) { - // DATA frame not full - if (m_dataCount < FRAME_NUM_BYTES_PER_FRAME) - { - m_frame.data[m_dataCount++] = data; - } + size_t frame_remain = FRAME_NUM_BYTES_PER_FRAME - m_dataCount; + size_t to_write = std::min(remain, frame_remain); + memcpy(&m_frame.data[m_dataCount], pData, to_write); + m_dataCount += to_write; + remain -= to_write; + pData += to_write; if (m_dataCount == FRAME_NUM_BYTES_PER_FRAME) { @@ -77,15 +57,44 @@ void Formatter::process(uint8_t data) } } +void Formatter::process_idle(uint32_t len) +{ + if (m_dataCount) + { + // Commit partial DATA frame + commit(FRAME_TYPE_DATA, m_dataCount); + m_dataCount = 0; + } + + size_t remain = len; + while(remain) + { + size_t frame_remain = FRAME_NUM_BYTES_PER_FRAME - m_dataCount; + size_t to_write = std::min(remain, frame_remain); + memset(&m_frame.data[m_dataCount], 0, to_write); + m_dataCount += to_write; + remain -= to_write; + + if (m_idleCount == FRAME_NUM_BYTES_PER_FRAME) + { + // Commit full IDLE frame of size FRAME_NUM_BYTES_PER_FRAME + commit(FRAME_TYPE_IDLE, m_idleCount); + m_idleCount = 0; + } + } + +} void Formatter::process(void *pData, uint32_t len) { - uint8_t *pByte; - - pByte = (uint8_t*)pData; - - while(len--) + if (pData) { - process(*(pByte++)); + // Data available: Process data + process_data((uint8_t*)pData, len); + } + else + { + // Data not available: Process Idle + process_idle(len); } } diff --git a/radio/Formatter.hpp b/radio/Formatter.hpp index 57c5443..091bd6d 100644 --- a/radio/Formatter.hpp +++ b/radio/Formatter.hpp @@ -20,8 +20,6 @@ class Formatter public: Formatter(IFormatter *pFrameHandler); ~Formatter(); - void idle(); - void process(uint8_t data); void process(void *pData, uint32_t len); frame_statistics_t getStats(); void resetStats(); @@ -33,7 +31,8 @@ private: uint32_t m_dataCount; uint32_t m_idleCount; frame_statistics_t m_frameStats; - + void process_idle(uint32_t len); + void process_data(uint8_t *pData, uint32_t len); void commit(frame_type frameType, uint32_t m_dataCount); }; diff --git a/radio/Frame.hpp b/radio/Frame.hpp index d1d1fdc..81e956c 100644 --- a/radio/Frame.hpp +++ b/radio/Frame.hpp @@ -2,6 +2,7 @@ #ifndef _RADIO_FRAME_HPP_ #define _RADIO_FRAME_HPP_ +#include #include #include #include "FrameDefs.hpp" @@ -66,21 +67,25 @@ public: void process() { - uint32_t i; + { + uint8_t data[12]; + for (uint32_t i=0; i < sizeof(data); i++) + { + data[i] = i; + } + m_formatter.process(data, sizeof(data)); + } - for (i=0; i < 12; i++) + m_formatter.process(nullptr, 100); { - m_formatter.process((uint8_t)i); + uint8_t data[100]; + for (uint32_t i=0; i < sizeof(data); i++) + { + data[i] = i; + } + m_formatter.process(data, sizeof(data)); } - for (i=0; i < 100; i++) - { - m_formatter.idle(); - } - for (i=0; i < 100; i++) - { - m_formatter.process((uint8_t)i); - } - m_formatter.idle(); + m_formatter.process(nullptr, 1); } private: