From: John Hodge Date: Tue, 23 Nov 2010 03:30:49 +0000 (+0800) Subject: Snack machine handlers X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=51560013d0ab4c823c3d1b28a0d0485cc84b2e40;p=tpg%2Fopendispense2.git Snack machine handlers --- 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; +} + +