From 252c222c4200921c495b2d1812132e69682fe42f Mon Sep 17 00:00:00 2001 From: hama Date: Sat, 18 Jun 2016 00:09:07 +0200 Subject: [PATCH] implementation of trace+getbuffer --- Server/client.cpp | 14 ++++++------ Server/gpioprocessor.cpp | 49 ++++++++++++++++++++++++++++++++-------- Server/tcpserver.cpp | 2 +- util/cudynbuffer.cpp | 8 +++---- util/dynbuffer.cpp | 8 +++---- util/dynbuffer.hpp | 18 ++++++++++++--- 6 files changed, 71 insertions(+), 28 deletions(-) diff --git a/Server/client.cpp b/Server/client.cpp index e030206..8450d75 100644 --- a/Server/client.cpp +++ b/Server/client.cpp @@ -41,15 +41,15 @@ static void prepareBlink(PinNumber pin, int count, int high, int low, DynBuffer& buffer.appendAsLE(high * 1000, 4); buffer.appendAsLE(low * 1000, 4); } -static void prepareTrace(PinNumber pin, int count, int delay){ +static void prepareTrace(PinNumber pin, int count, int delay, DynBuffer& buffer){ printf("trace: pin: %d count: %d delay: %d\n", pin, count, delay); buffer.clear().append("TRAC"); buffer.appendAsLE((int) pin, 1); buffer.appendAsLE(count, 4); buffer.appendAsLE(delay, 4); } -static void prepareGetBuffer(const char* bufferName){ - printf("getbuffer: %s\n", buffername); +static void prepareGetBuffer(const char* bufferName, DynBuffer& buffer){ + printf("getbuffer: %s\n", bufferName); buffer.clear().append("GETB"); buffer.append(bufferName); } @@ -145,16 +145,16 @@ int main (int argc, char **argv) { else if ( (pin = pinNameToNumber(argv[2])) == pinUndef) usage("unknown pin:", argv[2]); count = 100; - if (argc > 2 && (count = toNumber(argv[2], length)) == 0) + if (argc > 3 && (count = toNumber(argv[3], length)) == 0) usage("invalid count", argv[2]); int delay = 1000; - if (argc > 3 && (count = toNumber(argv[3], length)) == 0) + if (argc > 4 && (count = toNumber(argv[4], length)) == 0) usage("invalid delay", argv[3]); - prepareTrace(pin, count, delay); + prepareTrace(pin, count, delay, buffer); } else if (strcmp(command, "getbuffer") == 0){ if (argc < 3) usage("missing buffername", NULL); - prepareGetBuffer(argv[2]); + prepareGetBuffer(argv[2], buffer); } else { usage("unknown command:", command); } diff --git a/Server/gpioprocessor.cpp b/Server/gpioprocessor.cpp index 1e90ea4..a9e43ec 100644 --- a/Server/gpioprocessor.cpp +++ b/Server/gpioprocessor.cpp @@ -8,8 +8,10 @@ GPIOProcessor::GPIOProcessor(Announcer* logger, bool simulation) : TcpProcessor(), m_pins(), m_logger(logger), - m_pool(128, logger) + m_pool(128, logger), + m_buffers() { + memset(m_buffers, 0, sizeof m_buffers); } /** * Destructor. @@ -55,14 +57,17 @@ void GPIOProcessor::blink(DynBuffer& buffer){ * OUT: answer for the client */ void GPIOProcessor::getBuffer(DynBuffer& buffer){ - char name; - if (buffer.length() != 4 + 2 || buffer[4] != '#' || buffer[5] < 'A' || (name = buffer[5]) > 'Z'){ + int name; + if (buffer.length() != 4 + 2 || buffer.at(4) != '#' || buffer.at(5) < 'A' || (name = buffer.at(5)) > 'Z'){ DynBuffer name(buffer.str() + 4); buffer.set("ERROR wrong buffername: ").append(name); } else if(m_buffers[name] == NULL) { buffer.set("ERROR unknown buffername: #").append(name); } else { - buffer.set("OK ").append(*m_buffers[name]); + int ix = name - 'A'; + buffer.set("OK ").append(*m_buffers[ix]); + delete m_buffers[ix]; + m_buffers[ix] = NULL; } } /** @@ -136,8 +141,11 @@ TcpProcessor::State GPIOProcessor::process(DynBuffer& buffer){ blink(buffer); } else if (buffer.startsWith("MELO", 4)){ melody(buffer); - } else if (buffer.startsWith("PWM ", 4)){ pulseWidthModulation(buffer); + } else if (buffer.startsWith("TRAC", 4)){ + trace(buffer); + } else if (buffer.startsWith("GETB", 4)){ + getBuffer(buffer); } else { rc = stIgnored; } @@ -149,12 +157,35 @@ TcpProcessor::State GPIOProcessor::process(DynBuffer& buffer){ * * Syntax: TRAC[pin][count][delay]
* [pin]: 1 byte. Numbering like PinNumber
- * [count]: 2 byte
- * [duration]: 4 byte little endian, number of microseconds
+ * [count]: 4 byte
+ * [delay]: 4 byte little endian, number of microseconds
* * @param buffer IN: contains the command describing a melody to play
* OUT: answer for the client */ -void TcpProcessor::trace(DynBuffer& buffer){ - +void GPIOProcessor::trace(DynBuffer& buffer){ + if (buffer.length() != 4 + 1 + 4 + 4){ + buffer.clear().appendFormatted("ERR invalid buffer length: %d / 9", + buffer.length()); + } else { + PinNumber pin = (PinNumber) buffer.at(4); + int count = buffer.valueOfLE(4, 4 + 1); + int delay = buffer.valueOfLE(4, 4 + 1 + 4); + printf("Pin: %d count: %d delay: %d\n", pin, count, delay); + char name = 0; + int ix; + for (ix = 0; ix < 26; ix++){ + if (m_buffers[ix] == NULL){ + name = 'A' + ix; + break; + } + } + if (name == 0){ + buffer.set("ERR too few buffers: 26"); + } else { + m_buffers[ix] = new DynBuffer(count); + new TraceInputTimer(pin, delay, count, m_buffers[ix], this, m_logger, &m_pool); + buffer.set("OK #").append(name); + } + } } diff --git a/Server/tcpserver.cpp b/Server/tcpserver.cpp index 8a92e1c..1433bb8 100644 --- a/Server/tcpserver.cpp +++ b/Server/tcpserver.cpp @@ -86,7 +86,7 @@ TcpProcessor::State TcpServer::process(DynBuffer& buffer){ rc = stStop; buffer.clear(); } else if (buffer.startsWith("TIME", 4)){ - buffer.resize(64); + buffer.ensureSize(64); struct tm* tm_info; struct timeval tv; diff --git a/util/cudynbuffer.cpp b/util/cudynbuffer.cpp index aa90fce..d5ae03c 100644 --- a/util/cudynbuffer.cpp +++ b/util/cudynbuffer.cpp @@ -16,7 +16,7 @@ public: testAppendLittleEndian(); testAt(); testBufferClearLength(); - testResize(); + testEnsureSize(); testSet(); testSetLength(); testStartsWith(); @@ -167,16 +167,16 @@ public: checkE(0, buf.length()); checkT(buf.length() <= buf.size()); } - void testResize(){ + void testEnsureSize(){ DynBuffer buf(2, 2); checkE(2, buf.size()); checkE(0, buf.length()); // blocksize is 2: - buf.resize(3); + buf.ensureSize(3); checkE(4, buf.size()); checkE(0, buf.length()); // reserve more than blocksize: - buf.resize(7); + buf.ensureSize(7); checkE(7, buf.size()); checkE(0, buf.length()); } diff --git a/util/dynbuffer.cpp b/util/dynbuffer.cpp index 6dd1773..44d52da 100644 --- a/util/dynbuffer.cpp +++ b/util/dynbuffer.cpp @@ -88,7 +88,7 @@ DynBuffer& DynBuffer::operator =(const DynBuffer& source){ DynBuffer& DynBuffer::append(const char* string, size_t length){ if (length == 0) length = strlen(string); - resize(m_length + length); + ensureSize(m_length + length); memcpy(m_buffer + m_length, string, length); m_length += length; m_buffer[m_length] = '\0'; @@ -130,7 +130,7 @@ DynBuffer& DynBuffer::appendInt(int value, const char* format){ * @return *this (for chaining) */ DynBuffer& DynBuffer::appendAsLE(int value, size_t size){ - resize(m_length + size); + ensureSize(m_length + size); while(size-- > 0){ m_buffer[m_length++] = (char) value; value >>= 8; @@ -144,8 +144,8 @@ DynBuffer& DynBuffer::appendAsLE(int value, size_t size){ * @param size the wanted size (not including the implicite trailing '\0') * @return *this (for chaining) */ -DynBuffer& DynBuffer::resize(size_t size){ - if (m_length + size > m_size){ +DynBuffer& DynBuffer::ensureSize(size_t size){ + if (size > m_size){ m_size = m_size + m_blocksize; if (size > m_size) m_size = size; diff --git a/util/dynbuffer.hpp b/util/dynbuffer.hpp index ea7e39a..5416d6f 100644 --- a/util/dynbuffer.hpp +++ b/util/dynbuffer.hpp @@ -19,6 +19,18 @@ public: inline DynBuffer& append(const DynBuffer& source){ return append(source.str(), source.length()); } + /** + * Appends a character to the buffer. + * + * @param cc the character to append + * @return the instance (for chaining) + */ + inline DynBuffer& append(char cc){ + if (m_length >= m_size - 1) + ensureSize(m_length + 1); + m_buffer[m_length ++] = cc; + return *this; + } DynBuffer& appendFormatted(const char* format, ...); DynBuffer& appendInt(int value, const char* format = "%d"); DynBuffer& appendAsLE(int value, size_t size = 1); @@ -49,7 +61,7 @@ public: */ inline char* buffer(size_t neededSize = 0){ if (neededSize > 0) - resize(neededSize); + ensureSize(neededSize); return m_buffer; } /** @@ -62,6 +74,7 @@ public: m_buffer[0] = '\0'; return *this; } + DynBuffer& ensureSize(size_t size); /** * Returns the current buffer length. * @@ -70,7 +83,6 @@ public: inline size_t length() const { return m_length; } - DynBuffer& resize(size_t length); /** * Replace the buffer contents with a given string. * @@ -99,7 +111,7 @@ public: */ inline DynBuffer& setLength(int length){ if (length > (int) m_size) - resize(length); + ensureSize(length); m_buffer[m_length = length] = '\0'; return *this; } -- 2.39.5