summaryrefslogtreecommitdiff
path: root/instr-client.c
diff options
context:
space:
mode:
Diffstat (limited to 'instr-client.c')
-rw-r--r--instr-client.c45
1 files changed, 44 insertions, 1 deletions
diff --git a/instr-client.c b/instr-client.c
index c894bee..990abc2 100644
--- a/instr-client.c
+++ b/instr-client.c
@@ -3,6 +3,9 @@
#include <string.h>
#include <stdlib.h>
#include <strings.h>
+#include <sys/time.h>
+#include <glib.h>
+#include <glib/gprintf.h>
#include "response.h"
#include "version.h"
@@ -45,6 +48,8 @@ static gboolean Matches(char *src, char* dst, gssize size)
return TRUE;
}
+#define TIMEOUT 100000 // 100000 useconds, 100ms
+#define ONESECONDINUSEC 1000000
//GIOChannel callback
static gboolean
@@ -57,6 +62,43 @@ stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data)
return TRUE;
}
+ // Ignore second \n if two are sent in rapid succession.
+ // This happens when the serial input is terminated with \r\n,
+ // which getty translates to \n\n. The IO handler splits this
+ // into 2 input lines, so we can't use a simple regex to
+ // replace it.
+
+ static struct timeval last = { 0, 0 }; // the time this last ran
+ static struct timeval now; // the time now
+ gettimeofday(&now, NULL );
+
+ if (last.tv_sec != 0) { // shouldn't be zero if we have been in here before
+ if (strcmp(tmp, "\n") == 0) { // is this a newline all on it's own?
+ if (last.tv_sec == now.tv_sec) {
+ // if we're in the same second we can just check the usec field
+ if (now.tv_usec - last.tv_usec < TIMEOUT) {
+ // ignore this \n
+ return TRUE;
+ }
+ }
+ // - check that last and now sec fields are only one second apart
+ // - check that the time elapsed in this second is smaller than the
+ // timeout, if it isn't we don't need to check the other second
+ // - check that there is less of the last second remaining than the timeout
+ else if (now.tv_sec - last.tv_sec == 1 && now.tv_usec < TIMEOUT && (ONESECONDINUSEC - last.tv_usec) < TIMEOUT) {
+ // and then check the the time left in the last second
+ // plus the time in this second is less than the timeout
+ if ((ONESECONDINUSEC - last.tv_usec) + now.tv_usec < TIMEOUT) {
+ // ignore this \n
+ return TRUE;
+ }
+ }
+ }
+ }
+
+ last = now;
+
+
//test if we have an exit word
int idx=0;
for(idx=0; idx<sizeof(exitTokens)/sizeof(exitTokens[0]); idx++) {
@@ -76,6 +118,7 @@ stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data)
GError* error = NULL;
written = g_pollable_output_stream_write_nonblocking(out, tmp, size, NULL, &error);
+
if(error != NULL && error->message) {
g_print("Got error: %s\n", error->message);
}
@@ -132,7 +175,7 @@ int main(int argc, char** argv)
g_object_unref(client);
return -1;
}
- g_printf("Welcome to InstrumentShell v%s\n\n",FW_VERSION);
+ g_printf("\r\nWelcome! Avtech Electrosystems Ltd. - Firmware v%s\r\n\r\n",FW_VERSION);
g_print("> ");
//register Pollable sources