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
74 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
78 int Coke_CanDispense(int UNUSED(User), int Item)
80 char tmp[40], *status;
81 regmatch_t matches[4];
85 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
87 // Can't dispense if the machine is not connected
88 if( giCoke_SerialFD == -1 )
92 printf("Coke_CanDispense: Flushing\n");
98 while( WaitForColon() && ret < 3 )
100 // Flush the input buffer
102 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
104 printf("Coke_CanDispense: sending 'd7'\n");
109 // Check for a timeout error
111 fprintf(stderr, "Coke machine timed out\n");
112 return -2; // -EMYBAD
115 // You need to do a 'd7' before reading the status
116 // - Otherwise it sometimes reports a full slot as empty
117 // [TPG] (2011-02-19)
124 // TODO: Handle "not ok" response to D7
127 printf("Coke_CanDispense: sending 's%i'\n", Item);
130 // Ask the coke machine
131 Writef("s%i\r\n", Item);
134 printf("Coke_CanDispense: reading response\n");
136 // Read from the machine (ignoring empty lines)
137 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
139 printf("ret = %i, tmp = '%s'\n", ret, tmp);
141 // Read back-echoed lines
142 while( tmp[0] == ':' || tmp[1] != 'l' )
144 ret = ReadLine(sizeof(tmp)-1, tmp);
145 printf("ret = %i, tmp = '%s'\n", ret, tmp);
150 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
152 perror("Coke Machine");
158 printf("Coke_CanDispense: wait for the prompt again\n");
161 // Eat rest of response
164 // Parse status response
165 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
171 tmp[ matches[3].rm_eo ] = '\0';
172 status = &tmp[ matches[3].rm_so ];
175 printf("Coke_CanDispense: Machine responded slot status '%s'\n", status);
178 if( strcmp(status, "full") == 0 )
185 * \brief Actually do a dispense from the coke machine
187 int Coke_DoDispense(int UNUSED(User), int Item)
193 if( Item < 0 || Item > 6 ) return -1;
195 // Can't dispense if the machine is not connected
196 if( giCoke_SerialFD == -1 )
200 printf("Coke_DoDispense: flushing input\n");
205 while( WaitForColon() && ret < 3 )
207 // Flush the input buffer
209 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
211 printf("Coke_DoDispense: sending 'd7'\n");
217 printf("Coke_DoDispense: sending 'd%i'\n", Item);
220 Writef("d%i\r\n", Item);
222 // Read empty lines and echo-backs
224 ret = ReadLine(sizeof(tmp)-1, tmp);
225 if( ret == -1 ) return -1;
227 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
229 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
231 WaitForColon(); // Eat up rest of response
234 printf("Coke_DoDispense: done\n");
238 if( strcmp(tmp, "ok") == 0 ) {
239 // We think dispense worked
240 // - The machine returns 'ok' if an empty slot is dispensed, even if
241 // it doesn't actually try to dispense (no sound)
245 printf("Machine returned unknown value '%s'\n", tmp);
255 struct timeval timeout;
257 timeout.tv_sec = READ_TIMEOUT;
261 FD_SET(giCoke_SerialFD, &readfs);
263 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
264 if( ret == 0 ) return 0; // Timeout
266 printf("readchar return %i\n", ret);
270 ret = read(giCoke_SerialFD, &ch, 1);
272 printf("ret = %i\n", ret);
279 int Writef(const char *Format, ...)
284 va_start(args, Format);
285 len = vsnprintf(NULL, 0, Format, args);
290 va_start(args, Format);
291 vsnprintf(buf, len+1, Format, args);
295 printf("Writef: %s", buf);
298 return write(giCoke_SerialFD, buf, len);
308 FD_SET(giCoke_SerialFD, &readfs);
310 while( (ch = ReadChar()) != ':' && ch != 0);
312 if( ch == 0 ) return -1; // Timeout
317 int ReadLine(int len, char *output)
332 if( ch == '\n' || ch == '\r' ) {
339 //printf("ReadLine: output=%s\n", output);