Snack machine handlers
authorJohn Hodge <[email protected]>
Tue, 23 Nov 2010 03:30:49 +0000 (11:30 +0800)
committerJohn Hodge <[email protected]>
Tue, 23 Nov 2010 03:30:49 +0000 (11:30 +0800)
items.cfg
src/server/Makefile
src/server/handler_snack.c [new file with mode: 0644]

index b101b7e..21e09ef 100644 (file)
--- 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
index 34e55f6..10e64eb 100644 (file)
@@ -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 (file)
index 0000000..826851e
--- /dev/null
@@ -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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <regex.h>
+
+// === 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;
+}
+
+

UCC git Repository :: git.ucc.asn.au