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!
21 #define READ_TIMEOUT 2 // 2 seconds for ReadChar
27 int Coke_InitHandler();
28 int Coke_CanDispense(int User, int Item);
29 int Coke_DoDispense(int User, int Item);
30 int Writef(const char *Format, ...);
32 int ReadLine(int len, char *output);
35 tHandler gCoke_Handler = {
41 char *gsCoke_SerialPort = "/dev/ttyS0";
43 regex_t gCoke_StatusRegex;
46 int Coke_InitHandler()
48 printf("connecting to coke machine...\n");
50 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
51 if( giCoke_SerialFD == -1 ) {
52 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
55 // Reset the slot names.
56 // - Dunno why this is needed, but the machine plays silly
73 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
77 int Coke_CanDispense(int UNUSED(User), int Item)
79 char tmp[40], *status;
80 regmatch_t matches[4];
84 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
86 // Can't dispense if the machine is not connected
87 if( giCoke_SerialFD == -1 )
91 printf("Coke_CanDispense: Flushing\n");
97 while( WaitForColon() && ret < 3 )
99 // Flush the input buffer
101 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
103 printf("Coke_CanDispense: sending 'd7'\n");
108 // Check for a timeout error
110 fprintf(stderr, "Coke machine timed out\n");
111 return -2; // -EMYBAD
114 // You need to do a 'd7' before reading the status
115 // - Otherwise it sometimes reports a full slot as empty
116 // [TPG] (2011-02-19)
123 // TODO: Handle "not ok" response to D7
126 printf("Coke_CanDispense: sending 's%i'\n", Item);
129 // Ask the coke machine
130 Writef("s%i\r\n", Item);
133 printf("Coke_CanDispense: reading response\n");
135 // Read from the machine (ignoring empty lines)
136 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
138 printf("ret = %i, tmp = '%s'\n", ret, tmp);
140 // Read back-echoed lines
141 while( tmp[0] == ':' || tmp[1] != 'l' )
143 ret = ReadLine(sizeof(tmp)-1, tmp);
144 printf("ret = %i, tmp = '%s'\n", ret, tmp);
149 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
151 perror("Coke Machine");
157 printf("Coke_CanDispense: wait for the prompt again\n");
160 // Eat rest of response
163 // Parse status response
164 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
170 tmp[ matches[3].rm_eo ] = '\0';
171 status = &tmp[ matches[3].rm_so ];
174 printf("Coke_CanDispense: Machine responded slot status '%s'\n", status);
177 if( strcmp(status, "full") == 0 )
184 * \brief Actually do a dispense from the coke machine
186 int Coke_DoDispense(int UNUSED(User), int Item)
192 if( Item < 0 || Item > 6 ) return -1;
194 // Can't dispense if the machine is not connected
195 if( giCoke_SerialFD == -1 )
199 printf("Coke_DoDispense: flushing input\n");
204 while( WaitForColon() && ret < 3 )
206 // Flush the input buffer
208 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
210 printf("Coke_DoDispense: sending 'd7'\n");
216 printf("Coke_DoDispense: sending 'd%i'\n", Item);
219 Writef("d%i\r\n", Item);
221 // Read empty lines and echo-backs
223 ret = ReadLine(sizeof(tmp)-1, tmp);
224 if( ret == -1 ) return -1;
226 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
228 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
230 WaitForColon(); // Eat up rest of response
233 printf("Coke_DoDispense: done\n");
237 if( strcmp(tmp, "ok") == 0 ) {
238 // We think dispense worked
239 // - The machine returns 'ok' if an empty slot is dispensed, even if
240 // it doesn't actually try to dispense (no sound)
244 printf("Machine returned unknown value '%s'\n", tmp);
254 struct timeval timeout;
256 timeout.tv_sec = READ_TIMEOUT;
260 FD_SET(giCoke_SerialFD, &readfs);
262 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
263 if( ret == 0 ) return 0; // Timeout
265 printf("readchar return %i\n", ret);
269 ret = read(giCoke_SerialFD, &ch, 1);
271 printf("ret = %i\n", ret);
278 int Writef(const char *Format, ...)
283 va_start(args, Format);
284 len = vsnprintf(NULL, 0, Format, args);
289 va_start(args, Format);
290 vsnprintf(buf, len+1, Format, args);
294 printf("Writef: %s", buf);
297 return write(giCoke_SerialFD, buf, len);
307 FD_SET(giCoke_SerialFD, &readfs);
309 while( (ch = ReadChar()) != ':' && ch != 0);
311 if( ch == 0 ) return -1; // Timeout
316 int ReadLine(int len, char *output)
331 if( ch == '\n' || ch == '\r' ) {
338 //printf("ReadLine: output=%s\n", output);