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);
}
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);
}
TcpProcessor(),
m_pins(),
m_logger(logger),
- m_pool(128, logger)
+ m_pool(128, logger),
+ m_buffers()
{
+ memset(m_buffers, 0, sizeof m_buffers);
}
/**
* Destructor.
* 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;
}
}
/**
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;
}
*
* 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);
+ }
+ }
}
rc = stStop;
buffer.clear();
} else if (buffer.startsWith("TIME", 4)){
- buffer.resize(64);
+ buffer.ensureSize(64);
struct tm* tm_info;
struct timeval tv;
testAppendLittleEndian();
testAt();
testBufferClearLength();
- testResize();
+ testEnsureSize();
testSet();
testSetLength();
testStartsWith();
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());
}
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';
* @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;
* @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;
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);
*/
inline char* buffer(size_t neededSize = 0){
if (neededSize > 0)
- resize(neededSize);
+ ensureSize(neededSize);
return m_buffer;
}
/**
m_buffer[0] = '\0';
return *this;
}
+ DynBuffer& ensureSize(size_t size);
/**
* Returns the current buffer length.
*
inline size_t length() const {
return m_length;
}
- DynBuffer& resize(size_t length);
/**
* Replace the buffer contents with a given string.
*
*/
inline DynBuffer& setLength(int length){
if (length > (int) m_size)
- resize(length);
+ ensureSize(length);
m_buffer[m_length = length] = '\0';
return *this;
}