3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * handler_coke.c - Coke controller code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
21 int Coke_InitHandler();
22 int Coke_CanDispense(int User, int Item);
23 int Coke_DoDispense(int User, int Item);
26 tHandler gCoke_Handler = {
32 char *gsCoke_SerialPort = "/dev/ttyS0";
34 regex_t gCoke_StatusRegex;
37 int Coke_InitHandler()
39 printf("connecting to coke machine...\n");
40 giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR);
41 if( giCoke_SerialFD == -1 ) {
42 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
44 CompileRegex(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
48 int Coke_CanDispense(int User, int Item)
50 char tmp[32], *status;
51 regmatch_t matches[4];
55 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
57 // Ask the coke machine
58 sprintf(tmp, "s%i\n", Item);
59 write(giCoke_SerialFD, tmp, 2);
63 ret = read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
64 //printf("ret = %i\n", ret);
66 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
69 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
74 tmp[ matches[3].rm_eo ] = '\0';
75 status = &tmp[ matches[3].rm_so ];
77 printf("Machine responded slot status '%s'\n", status);
79 if( strcmp(status, "full") == 0 )
86 * \brief Actually do a dispense from the coke machine
88 int Coke_DoDispense(int User, int Item)
90 char tmp[32], *status;
91 regmatch_t matches[4];
94 if( Item < 0 || Item > 6 ) return -1;
97 sprintf(tmp, "d%i\n", Item);
98 write(giCoke_SerialFD, tmp, 2);
101 read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
102 regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
104 tmp[ matches[3].rm_eo ] = '\0';
105 status = &tmp[ matches[3].rm_so ];
107 printf("Machine responded slot status '%s'\n", status);