From 0a2c6b377209b8bf29fa53fa1a2c6ebf13a7c570 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Wed, 16 May 2012 21:32:42 +0200 Subject: code refactor + changed status led meaning --- Makefile | 2 +- db.vala | 26 ++++++---- device.vala | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.vala | 99 ++++++++++++-------------------------- serial.vala | 111 ------------------------------------------ 5 files changed, 205 insertions(+), 189 deletions(-) create mode 100644 device.vala delete mode 100644 serial.vala diff --git a/Makefile b/Makefile index 1d993f3..0c33e42 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PREFIX=/usr/local -barcode-scanner: main.vala serial.vala web.vala db.vala +barcode-scanner: main.vala device.vala web.vala db.vala valac-0.16 --output $@ --pkg posix --pkg linux --pkg libsoup-2.4 --pkg sqlite3 $^ install: barcode-scanner diff --git a/db.vala b/db.vala index 6467e6c..5679eb7 100644 --- a/db.vala +++ b/db.vala @@ -25,23 +25,31 @@ public class Database { } } - public void login(uint64 id) { + public bool login(uint64 id) { this.user = id; + return true; } - public void logout() { + public bool logout() { this.user = 0; + return true; } - public void buy(uint64 article) { - this.insert_stmt.reset(); - this.insert_stmt.bind_text(1, "%llu".printf(user)); - this.insert_stmt.bind_text(2, "%llu".printf(article)); + public bool buy(uint64 article) { + if(is_logged_in()) { + this.insert_stmt.reset(); + this.insert_stmt.bind_text(1, "%llu".printf(user)); + this.insert_stmt.bind_text(2, "%llu".printf(article)); - int rc = this.insert_stmt.step(); + int rc = this.insert_stmt.step(); - if(rc != Sqlite.DONE) - error("[interner Fehler: %d]".printf(rc)); + if(rc != Sqlite.DONE) + error("[interner Fehler: %d]".printf(rc)); + else + return true; + } else { + return false; + } } public string get_product_name(uint64 article) { diff --git a/device.vala b/device.vala new file mode 100644 index 0000000..ee0967b --- /dev/null +++ b/device.vala @@ -0,0 +1,156 @@ +public class Device { + private Posix.termios newtio; + private Posix.termios restoretio; + public int fd=-1; + public int byterate; + + public Device(string device, int rate, int bits, int stopbits) { + Posix.speed_t baudrate = Posix.B9600; + + fd = Posix.open(device, Posix.O_RDWR /*| Posix.O_NONBLOCK*/); + + if(fd < 0) { + fd = -1; + stderr.printf("Could not open device!\n"); + } + + Posix.tcflush(fd, Posix.TCIOFLUSH); + + Posix.tcgetattr(fd, out restoretio); + + /* apply settings */ + switch(rate) { + case 300: + baudrate = Posix.B300; + break; + case 600: + baudrate = Posix.B600; + break; + case 1200: + baudrate = Posix.B1200; + break; + case 2400: + baudrate = Posix.B2400; + break; + case 4800: + baudrate = Posix.B4800; + break; + case 9600: + baudrate = Posix.B9600; + break; + case 19200: + baudrate = Posix.B19200; + break; + case 38400: + baudrate = Posix.B38400; + break; + case 57600: + baudrate = Posix.B57600; + break; + case 115200: + baudrate = Posix.B115200; + break; + case 230400: + baudrate = Posix.B230400; + break; + default: + /* not supported */ + rate = 9600; + break; + } + + Posix.cfsetospeed(ref newtio, baudrate); + Posix.cfsetispeed(ref newtio, baudrate); + + switch(bits) { + case 5: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS5; + break; + case 6: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS6; + break; + case 7: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS7; + break; + case 8: + default: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS8; + break; + } + + newtio.c_cflag |= Posix.CLOCAL | Posix.CREAD; + + newtio.c_cflag &= ~(Posix.PARENB | Posix.PARODD); + + /* TODO: parity */ + + newtio.c_cflag &= ~Linux.Termios.CRTSCTS; + + if(stopbits == 2) + newtio.c_cflag |= Posix.CSTOPB; + else + newtio.c_cflag &= ~Posix.CSTOPB; + + newtio.c_iflag = Posix.IGNBRK; + + newtio.c_lflag = 0; + newtio.c_oflag = 0; + + newtio.c_cc[Posix.VTIME]=1; + newtio.c_cc[Posix.VMIN]=1; + + newtio.c_lflag &= ~(Posix.ECHONL|Posix.NOFLSH); + + int mcs=0; + Posix.ioctl(fd, Linux.Termios.TIOCMGET, out mcs); + mcs |= Linux.Termios.TIOCM_RTS; + Posix.ioctl(fd, Linux.Termios.TIOCMSET, out mcs); + + Posix.tcsetattr(fd, Posix.TCSANOW, newtio); + + this.byterate = rate/bits; + } + + private ssize_t read(void *buf, size_t count) { + return Posix.read(fd, buf, count); + } + + private ssize_t write(void *buf, size_t count) { + ssize_t size = Posix.write(fd, buf, count); + Posix.tcflush(fd, Posix.TCOFLUSH); + return size; + } + + public string receive() { + char[] detected = {}; + char buf[64]; + + int size = (int) this.read(buf, 64); + + if(size <= 0) + error("serial device lost.\n"); + + for(int i = 0; i < size; i++) { + if(buf[i] != '\r' && buf[i] != '\n') { + detected += (char) buf[i]; + } else { + if(detected.length > 0) { + detected += '\0'; + return (string) detected; + } + } + } + + return ""; + } + + /** + * @param duration duration of the blink in 0.1 seconds + */ + public void blink(uint duration) { + uint size = byterate/10 * duration; + var msg = new uint8[size]; + Posix.memset(msg, 0xFF, msg.length); + this.write(msg, msg.length); +} +} diff --git a/main.vala b/main.vala index 9ffd108..5886b11 100644 --- a/main.vala +++ b/main.vala @@ -1,5 +1,5 @@ -public Serial s; -public Database w; +public Device dev; +public Database db; public static int main(string[] args) { if(args.length < 2) { @@ -7,85 +7,48 @@ public static int main(string[] args) { return 1; } - s = new Serial(args[1], 9600, 8, 1); - w = new Database("shop.db"); - - char[] detected = {}; + dev = new Device(args[1], 9600, 8, 1); + db = new Database("shop.db"); while(true) { - uint8 buf[64]; - uint8 buf2[250] = { - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - - int size = (int) Posix.read(s.fd, buf, 64); - - if(size <= 0) { - stderr.printf("serial device lost.\n"); - return 1; - } - - for(int i = 0; i < size; i++) - if(buf[i] != '\r' && buf[i] != '\n') { - detected += (char) buf[i]; - } else { - if(detected.length > 0) { - detected += '\0'; - - /* use led as feedback */ - Posix.write(s.fd, buf2, 250); - Posix.write(s.fd, buf2, 250); - Posix.write(s.fd, buf2, 250); - Posix.write(s.fd, buf2, 250); - Posix.tcflush(s.fd, Posix.TCOFLUSH); - - interpret((string) detected); - } - detected = {}; - } + string message = dev.receive(); + if(interpret((string) message)) + dev.blink(10); } } -public static void interpret(string data) { +public static bool interpret(string data) { if(data.has_prefix("USER ")) { string str_id = data.substring(5); uint64 id = uint64.parse(str_id); - if(w.is_logged_in()) { - stdout.printf("logout\n"); - w.logout(); + /* check if data has valid format */ + if(data != "USER %llu".printf(id)) { + stdout.printf("ungültige Benutzernummer!\n"); + return false; } - else { - stdout.printf("login: %llu\n".printf(id)); - w.login(id); + + if(db.is_logged_in()) { + stdout.printf("Logout\n"); + return db.logout(); + } else { + stdout.printf("Login: %llu\n".printf(id)); + return db.login(id); } } else { uint64 id = uint64.parse(data); - w.buy(id); - stdout.printf("gekaufter Artikel: %s\n", w.get_product_name(id)); + /* check if data has valid format */ + if(data != "%llu".printf(id)) { + stdout.printf("ungültiges Produkt!\n"); + return false; + } + + if(db.buy(id)) { + stdout.printf("gekaufter Artikel: %s\n", db.get_product_name(id)); + return true; + } + + return false; } } diff --git a/serial.vala b/serial.vala deleted file mode 100644 index acdcf41..0000000 --- a/serial.vala +++ /dev/null @@ -1,111 +0,0 @@ -public class Serial { - private Posix.termios newtio; - private Posix.termios restoretio; - public int fd=-1; - - public signal void new_data(uchar[] data); - - public Serial(string device, int rate, int bits, int stopbits) { - Posix.speed_t baudrate = Posix.B9600; - - fd = Posix.open(device, Posix.O_RDWR /*| Posix.O_NONBLOCK*/); - - if(fd < 0) { - fd = -1; - stderr.printf("Could not open device!\n"); - } - - Posix.tcflush(fd, Posix.TCIOFLUSH); - - Posix.tcgetattr(fd, out restoretio); - - /* apply settings */ - switch(rate) { - case 300: - baudrate = Posix.B300; - break; - case 600: - baudrate = Posix.B600; - break; - case 1200: - baudrate = Posix.B1200; - break; - case 2400: - baudrate = Posix.B2400; - break; - case 4800: - baudrate = Posix.B4800; - break; - case 9600: - baudrate = Posix.B9600; - break; - case 19200: - baudrate = Posix.B19200; - break; - case 38400: - baudrate = Posix.B38400; - break; - case 57600: - baudrate = Posix.B57600; - break; - case 115200: - baudrate = Posix.B115200; - break; - case 230400: - baudrate = Posix.B230400; - break; - default: - /* not supported */ - break; - } - - Posix.cfsetospeed(ref newtio, baudrate); - Posix.cfsetispeed(ref newtio, baudrate); - - switch(bits) { - case 5: - newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS5; - break; - case 6: - newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS6; - break; - case 7: - newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS7; - break; - case 8: - default: - newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS8; - break; - } - - newtio.c_cflag |= Posix.CLOCAL | Posix.CREAD; - - newtio.c_cflag &= ~(Posix.PARENB | Posix.PARODD); - - /* TODO: parity */ - - newtio.c_cflag &= ~Linux.Termios.CRTSCTS; - - if(stopbits == 2) - newtio.c_cflag |= Posix.CSTOPB; - else - newtio.c_cflag &= ~Posix.CSTOPB; - - newtio.c_iflag = Posix.IGNBRK; - - newtio.c_lflag = 0; - newtio.c_oflag = 0; - - newtio.c_cc[Posix.VTIME]=1; - newtio.c_cc[Posix.VMIN]=1; - - newtio.c_lflag &= ~(Posix.ECHONL|Posix.NOFLSH); - - int mcs=0; - Posix.ioctl(fd, Linux.Termios.TIOCMGET, out mcs); - mcs |= Linux.Termios.TIOCM_RTS; - Posix.ioctl(fd, Linux.Termios.TIOCMSET, out mcs); - - Posix.tcsetattr(fd, Posix.TCSANOW, newtio); - } -} -- cgit v1.2.3