]> gitweb.hamatoma.de Git - cpidjinn/commitdiff
implementation of trace+getbuffer
authorhama <hama@siduction.net>
Fri, 17 Jun 2016 22:09:07 +0000 (00:09 +0200)
committerhama <hama@siduction.net>
Fri, 17 Jun 2016 22:09:07 +0000 (00:09 +0200)
Server/client.cpp
Server/gpioprocessor.cpp
Server/tcpserver.cpp
util/cudynbuffer.cpp
util/dynbuffer.cpp
util/dynbuffer.hpp

index e030206b07e3d9c786e96f6d913d966fcd9970a1..8450d75d49a04a6218bfaadc7d32f8ccd48b476c 100644 (file)
@@ -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);
        }
index 1e90ea4eb7a331dd0d48af258e04cc379ae37c1d..a9e43eca70245481fd97684adee0b9b0a40c5d50 100644 (file)
@@ -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]<br>
  * [pin]: 1 byte. Numbering like <i>PinNumber</i><br>
- * [count]: 2 byte<br>
- * [duration]: 4 byte little endian, number of microseconds<br>
+ * [count]: 4 byte<br>
+ * [delay]: 4 byte little endian, number of microseconds<br>
  *
  * @param buffer       IN: contains the command describing a melody to play<br>
  *                                     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);
+               }
+       }
 }
index 8a92e1c12dcf4894d983a5b5213ba543eddf6fca..1433bb8a97f936a13ab444ca9a52d458df3b59b7 100644 (file)
@@ -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;
 
index aa90fcebba39c10fcc540a4d65bb0254c4812c4b..d5ae03c1a3e9dad3115ecfaeb81e5d311a74f4fd 100644 (file)
@@ -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());
        }
index 6dd177360616ef2ff0828139dfe95389131bacc9..44d52da22c9d30b2e81cd5f3c27728ef5804107f 100644 (file)
@@ -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                     <i>*this</i> (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                     <i>*this</i> (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;
index ea7e39a42a0d8433b35c6ca706c65ed37b94d469..5416d6f84ae7dcfc18eb8a7d4b031059becad294 100644 (file)
@@ -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;
     }