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.
11 * - Remember, the coke machine echoes your text back to you!
22 #define READ_TIMEOUT 2 // 2 seconds for ReadChar
28 int Coke_InitHandler();
29 int Coke_CanDispense(int User, int Item);
30 int Coke_DoDispense(int User, int Item);
31 int Writef(const char *Format, ...);
33 int ReadLine(int len, char *output);
36 tHandler gCoke_Handler = {
42 char *gsCoke_SerialPort = "/dev/ttyS0";
44 regex_t gCoke_StatusRegex;
47 int Coke_InitHandler()
49 printf("connecting to coke machine...\n");
51 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
52 if( giCoke_SerialFD == -1 ) {
53 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
56 // Reset the slot names.
57 // - Dunno why this is needed, but the machine plays silly
76 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
80 int Coke_CanDispense(int UNUSED(User), int Item)
82 char tmp[40], *status;
83 regmatch_t matches[4];
87 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
89 // Can't dispense if the machine is not connected
90 if( giCoke_SerialFD == -1 )
94 printf("Coke_CanDispense: Flushing\n");
100 while( WaitForColon() && ret < 3 )
102 // Flush the input buffer
104 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
106 printf("Coke_CanDispense: sending 'd7'\n");
111 // Check for a timeout error
113 fprintf(stderr, "Coke machine timed out\n");
114 return -2; // -EMYBAD
117 // You need to do a 'd7' before reading the status
118 // - Otherwise it sometimes reports a full slot as empty
119 // [TPG] (2011-02-19)
126 // TODO: Handle "not ok" response to D7
129 printf("Coke_CanDispense: sending 's%i'\n", Item);
132 // Ask the coke machine
133 Writef("s%i\r\n", Item);
136 printf("Coke_CanDispense: reading response\n");
138 // Read from the machine (ignoring empty lines)
139 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
141 printf("ret = %i, tmp = '%s'\n", ret, tmp);
143 // Read back-echoed lines
144 while( tmp[0] == ':' || tmp[1] != 'l' )
146 ret = ReadLine(sizeof(tmp)-1, tmp);
147 printf("ret = %i, tmp = '%s'\n", ret, tmp);
152 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
154 perror("Coke Machine");
160 printf("Coke_CanDispense: wait for the prompt again\n");
163 // Eat rest of response
166 // Parse status response
167 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
173 tmp[ matches[3].rm_eo ] = '\0';
174 status = &tmp[ matches[3].rm_so ];
177 printf("Coke_CanDispense: Machine responded slot status '%s'\n", status);
180 if( strcmp(status, "full") == 0 )
187 * \brief Actually do a dispense from the coke machine
189 int Coke_DoDispense(int UNUSED(User), int Item)
195 if( Item < 0 || Item > 6 ) return -1;
197 // Can't dispense if the machine is not connected
198 if( giCoke_SerialFD == -1 )
202 printf("Coke_DoDispense: flushing input\n");
207 while( WaitForColon() && ret < 3 )
209 // Flush the input buffer
211 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
213 printf("Coke_DoDispense: sending 'd7'\n");
219 printf("Coke_DoDispense: sending 'd%i'\n", Item);
222 Writef("d%i\r\n", Item);
224 // Read empty lines and echo-backs
226 ret = ReadLine(sizeof(tmp)-1, tmp);
227 if( ret == -1 ) return -1;
229 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
231 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
233 WaitForColon(); // Eat up rest of response
236 printf("Coke_DoDispense: done\n");
240 if( strcmp(tmp, "ok") == 0 ) {
241 // We think dispense worked
242 // - The machine returns 'ok' if an empty slot is dispensed, even if
243 // it doesn't actually try to dispense (no sound)
247 printf("Machine returned unknown value '%s'\n", tmp);
257 struct timeval timeout;
259 timeout.tv_sec = READ_TIMEOUT;
263 FD_SET(giCoke_SerialFD, &readfs);
265 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
266 if( ret == 0 ) return 0; // Timeout
268 printf("readchar return %i\n", ret);
272 ret = read(giCoke_SerialFD, &ch, 1);
274 printf("ret = %i\n", ret);
281 int Writef(const char *Format, ...)
286 va_start(args, Format);
287 len = vsnprintf(NULL, 0, Format, args);
292 va_start(args, Format);
293 vsnprintf(buf, len+1, Format, args);
297 printf("Writef: %s", buf);
300 return write(giCoke_SerialFD, buf, len);
310 FD_SET(giCoke_SerialFD, &readfs);
312 while( (ch = ReadChar()) != ':' && ch != 0);
314 if( ch == 0 ) return -1; // Timeout
319 int ReadLine(int len, char *output)
334 if( ch == '\n' || ch == '\r' ) {
341 //printf("ReadLine: output=%s\n", output);