]> git.itanic.dy.fi Git - log-plotter/commitdiff
Initial commit
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 29 Sep 2013 19:05:52 +0000 (22:05 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 29 Sep 2013 19:24:32 +0000 (22:24 +0300)
Add a very very simple version of the "plotter". It does no plotting,
it only opens the serial device with correct baud rate, reads out the
data at correct baud rate (correct for an iCharger 206B charer) and
prints out exactly what it read. Dumb as hell but works.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
baud.c [new file with mode: 0644]
baud.h [new file with mode: 0644]
main.c [new file with mode: 0644]
mkcompile_h [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..bd05bb0
--- /dev/null
@@ -0,0 +1,7 @@
+*.d
+*.o
+*~
+log-plotter
+version.h
+.*
+!.gitignore
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..bcea3eb
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,47 @@
+CC=gcc
+LD=ld
+CFLAGS=-Wall -O2 -g -fPIC
+
+LOG-PLOTTER_OBJS = baud.o main.o
+
+ALL_OBJS = $(LOG-PLOTTER_OBJS)
+ALL_DEBS = $(patsubst %.o,.%.o.d,$(ALL_OBJS))
+
+ifeq ($(V),1)
+       Q               =
+       QUIET_CC        =
+       QUIET_LINK      =
+else
+       Q               = @
+       QUIET_CC        = @echo "       CC " $@;
+       QUIET_LINK      = @echo "     LINK " $@;
+endif
+
+default: log-plotter
+
+log-plotter: $(LOG-PLOTTER_OBJS)
+       $(QUIET_LINK)$(CC) -o log-plotter $(LOG-PLOTTER_OBJS)
+
+clean:
+       rm -vf log-plotter *~ *.o .*.d .version version.h .compiler_check*
+
+%.o: %.c .compiler_check
+       $(QUIET_CC)$(CC) -MMD -MF .$@.d $(CFLAGS) -c $< -o $@
+       $(Q)cp .$@.d .$@.P; \
+            sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
+                -e '/^$$/ d' -e 's/$$/ :/' < .$@.d >> .$@.P; \
+            mv .$@.P .$@.d
+
+version.h .compiler_check: FORCE
+       $(Q)./mkcompile_h "$(CC) $(CFLAGS)"
+
+FORCE:
+
+
+TAGS:
+       @echo -e "\tTAGS\t"
+       @etags *.[ch]
+
+.PHONY: all clean TAGS
+
+-include $(ALL_DEBS)
diff --git a/baud.c b/baud.c
new file mode 100644 (file)
index 0000000..be2194e
--- /dev/null
+++ b/baud.c
@@ -0,0 +1,67 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <linux/termios.h>
+#include <unistd.h>
+
+/*
+ * HACK: declare the ioctl function by hand...
+ *
+ * We cannot include <sys/ioctl.h> as it will eventually include
+ * <termios.h>, which will conflict with <linux/temios.h>. As we
+ * really need the latter one AND the ioctl, just declare it by hand
+ * here so that we get to use it...
+ */
+int ioctl(int d, int request, ...);
+
+/**
+ * Open a serial device and adjust the baud rate to give (arbitrary)
+ * baud rate. The actual baud rate is stored back to the @baud
+ * variable. This might differ from the requested one as the
+ * underlying hardware may not support all possible baud
+ * rates.
+ *
+ * Returns the file descriptor that was opened.
+ */
+int open_at_baud(const char *device, int *baud)
+{
+       struct termios2 tios;
+       int fd;
+
+       fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
+
+       if (fd == -1) {
+               fprintf(stderr, "error opening %s: %m", device);
+               return fd;
+       }
+
+       if (ioctl(fd, TCGETS2, &tios)) {
+               perror("TCGETS2");
+               goto err;
+       }
+
+       tios.c_cflag &= ~CBAUD;
+       tios.c_cflag |= BOTHER;
+       tios.c_ispeed = *baud;
+       tios.c_ospeed = *baud;
+
+       if (ioctl(fd, TCSETS2, &tios)) {
+               perror("TCSETS2");
+               goto err;
+       }
+
+       if (ioctl(fd, TCGETS2, &tios)) {
+               perror("TCGETS2");
+               goto err;
+       }
+
+       *baud = tios.c_ospeed;
+
+       return fd;
+err:
+       close(fd);
+
+       return -1;
+}
diff --git a/baud.h b/baud.h
new file mode 100644 (file)
index 0000000..bf905cc
--- /dev/null
+++ b/baud.h
@@ -0,0 +1,6 @@
+#ifndef _BAUD_H
+#define _BAUD_H
+
+int open_at_baud(const char *device, int *baud);
+
+#endif
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..b4a4c8f
--- /dev/null
+++ b/main.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <unistd.h>
+
+#include "baud.h"
+
+int main(int argc, char *argv[])
+{
+       int fd, baud, ret;
+       char *device;
+       char buf[256];
+
+       if (argc < 2) {
+               printf("Usage: %s SERIAL_DEVICE\n", argv[0]);
+               return 1;
+       }
+
+       baud = 128000;
+       device = argv[1];
+
+       fd = open_at_baud(device, &baud);
+       if (fd < 0)
+               return 1;
+
+       if (baud != 128000) {
+               printf("Failed to set baudrate to 128000, only got %d\n", baud);
+               close(fd);
+               return 1;
+       }
+
+       while (1) {
+               ret = read(fd, buf, sizeof(buf));
+               if (read < 0) {
+                       perror("read");
+                       break;
+               }
+
+               if (read == 0)
+                       break;
+
+               ret = write(1, buf, ret);
+               if (read < 0) {
+                       perror("write");
+                       break;
+               }
+       }
+
+       close(fd);
+       return 0;
+}
diff --git a/mkcompile_h b/mkcompile_h
new file mode 100755 (executable)
index 0000000..e3b246e
--- /dev/null
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+VERSION_TMP=.version
+VERSION_TARGET=version.h
+
+VERSION_STR=$(git show | grep ^commit | sed -e s.commit\ .. | cut -b -10)
+
+COMPILER_STR="$1"
+COMPILER_STR_TARGET=.compiler_check
+COMPILER_STR_TMP=$COMPILER_CHECK.tmp
+
+echo > $VERSION_TMP
+echo "#ifndef _VERSION_H_" >> $VERSION_TMP
+echo "#define _VERSION_H_" >> $VERSION_TMP
+echo >> $VERSION_TMP
+echo "/* Autogenerated version.h file */" >> $VERSION_TMP
+echo >> $VERSION_TMP
+echo "#define LOG-PLOTTER_VERSION \"$VERSION_STR\"" >> $VERSION_TMP
+echo >> $VERSION_TMP
+echo "#endif" >> $VERSION_TMP
+
+if [ -f $VERSION_TARGET ] ; then
+       cmp -s $VERSION_TMP $VERSION_TARGET || cp $VERSION_TMP $VERSION_TARGET
+else
+       cp $VERSION_TMP $VERSION_TARGET
+fi
+
+echo $COMPILER_STR > $COMPILER_STR_TMP
+
+if [ -f $COMPILER_STR_TARGET ] ; then
+       cmp -s $COMPILER_STR_TMP $COMPILER_STR_TARGET || \
+           cp $COMPILER_STR_TMP $COMPILER_STR_TARGET
+else
+       cp $COMPILER_STR_TMP $COMPILER_STR_TARGET
+fi
+