3 * UCC (University [of WA] Computer Club) Electronic Accounting System
6 * handler_snack.c - Snack machine code
8 * This file is licenced under the 3-clause BSD Licence. See the file
9 * COPYING for full details.
22 int Snack_InitHandler();
23 int Snack_CanDispense(int User, int Item);
24 int Snack_DoDispense(int User, int Item);
27 tHandler gSnack_Handler = {
33 char *gsSnack_SerialPort = "/dev/ttyS1";
35 regex_t gSnack_ResponseRegex;
38 int Snack_InitHandler()
40 giSnack_SerialFD = open(gsSnack_SerialPort, O_RDWR);
41 regcomp(&gSnack_ResponseRegex, "^(\\d\\d\\d)(.*)$", REG_EXTENDED);
45 int Snack_CanDispense(int User, int Item)
48 if( Item < 0 || Item > 99 ) return -1;
50 // Hmm... could we implement slot statuses?
56 * \brief Actually do a dispense from the coke machine
58 int Snack_DoDispense(int User, int Item)
61 regmatch_t matches[4];
64 if( Item < 0 || Item > 99 ) return -1;
67 sprintf(tmp, "V%02i\n", Item);
68 write(giSnack_SerialFD, tmp, 2);
71 read(giSnack_SerialFD, tmp, sizeof(tmp)-1);
72 regexec(&gSnack_ResponseRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);