From 51560013d0ab4c823c3d1b28a0d0485cc84b2e40 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 23 Nov 2010 11:30:49 +0800 Subject: [PATCH] Snack machine handlers --- items.cfg | 4 ++ src/server/Makefile | 2 +- src/server/handler_snack.c | 75 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/server/handler_snack.c diff --git a/items.cfg b/items.cfg index b101b7e..21e09ef 100644 --- a/items.cfg +++ b/items.cfg @@ -13,3 +13,7 @@ coke 6 96 Coke # Pseudo items pseudo 0 128 clue # clue.flac pseudo 1 10 laserprint # print 10 pages + +# Snack machine +snack 13 128 Smiths Salt & Vinegar +snack 33 128 Smiths Original diff --git a/src/server/Makefile b/src/server/Makefile index 34e55f6..10e64eb 100644 --- a/src/server/Makefile +++ b/src/server/Makefile @@ -3,7 +3,7 @@ OBJ := main.o server.o logging.o OBJ += dispense.o itemdb.o -OBJ += handler_coke.o +OBJ += handler_coke.o handler_snack.o BIN := ../../dispsrv LINKFLAGS := -g ../../cokebank.so diff --git a/src/server/handler_snack.c b/src/server/handler_snack.c new file mode 100644 index 0000000..826851e --- /dev/null +++ b/src/server/handler_snack.c @@ -0,0 +1,75 @@ +/* + * OpenDispense 2 + * UCC (University [of WA] Computer Club) Electronic Accounting System + * - Dispense Server + * + * handler_snack.c - Snack machine code + * + * This file is licenced under the 3-clause BSD Licence. See the file + * COPYING for full details. + */ +#include "common.h" +#include +#include +#include +#include +#include +#include + +// === IMPORTS === + +// === PROTOTYPES === + int Snack_InitHandler(); + int Snack_CanDispense(int User, int Item); + int Snack_DoDispense(int User, int Item); + +// === GLOBALS === +tHandler gSnack_Handler = { + "snack", + Snack_InitHandler, + Snack_CanDispense, + Snack_DoDispense +}; +char *gsSnack_SerialPort = "/dev/ttyS1"; + int giSnack_SerialFD; +regex_t gSnack_ResponseRegex; + +// == CODE === +int Snack_InitHandler() +{ + giSnack_SerialFD = open(gsSnack_SerialPort, O_RDWR); + regcomp(&gSnack_ResponseRegex, "^(\\d\\d\\d)(.*)$", REG_EXTENDED); + return 0; +} + +int Snack_CanDispense(int User, int Item) +{ + // Sanity please + if( Item < 0 || Item > 99 ) return -1; + + return 1; +} + +/** + * \brief Actually do a dispense from the coke machine + */ +int Snack_DoDispense(int User, int Item) +{ + char tmp[32]; + regmatch_t matches[4]; + + // Sanity please + if( Item < 0 || Item > 99 ) return -1; + + // Dispense + sprintf(tmp, "V%02i\n", Item); + write(giSnack_SerialFD, tmp, 2); + + // Get status + read(giSnack_SerialFD, tmp, sizeof(tmp)-1); + regexec(&gSnack_ResponseRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0); + + return 0; +} + + -- 2.20.1