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!
23 #define READ_TIMEOUT 2 // 2 seconds for ReadChar
29 int Coke_InitHandler();
30 int Coke_int_GetSlotStatus(char *Buffer, int Slot);
31 void Coke_int_UpdateSlotStatuses(void);
32 int Coke_CanDispense(int User, int Item);
33 int Coke_DoDispense(int User, int Item);
34 int Writef(const char *Format, ...);
36 int ReadLine(int len, char *output);
39 tHandler gCoke_Handler = {
45 char *gsCoke_SerialPort = "/dev/ttyS0";
47 regex_t gCoke_StatusRegex;
48 int gaCoke_CachedStatus[7];
49 pthread_mutex_t gCoke_Mutex = PTHREAD_MUTEX_INITIALIZER;
52 int Coke_InitHandler()
54 printf("connecting to coke machine...\n");
56 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
57 if( giCoke_SerialFD == -1 ) {
58 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
61 // Reset the slot names.
62 // - Dunno why this is needed, but the machine plays silly
79 Coke_int_UpdateSlotStatuses();
83 AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
85 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
89 int Coke_int_GetSlotStatus(char *Buffer, int Slot)
91 regmatch_t matches[4];
95 // Parse status response
96 ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
102 Buffer[ matches[3].rm_eo ] = '\0';
103 status = &Buffer[ matches[3].rm_so ];
106 printf("Coke_CanDispense: Machine responded slot status '%s'\n", status);
109 if( strcmp(status, "full") == 0 ) {
110 gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable
114 gaCoke_CachedStatus[Slot] = 1; // 1: Empty
119 void Coke_int_UpdateSlotStatuses(void)
125 pthread_mutex_lock(&gCoke_Mutex);
127 if( WaitForColon() ) return ;
128 Writef("d7\r\n"); // Update slot statuses
131 ReadLine(sizeof tmp, tmp); // Read back what we just said
133 for( i = 0; i <= 6; i ++ )
135 len = ReadLine(sizeof tmp, tmp);
136 if( len == -1 ) return ; // I give up :(
137 Coke_int_GetSlotStatus(tmp, i);
139 pthread_mutex_unlock(&gCoke_Mutex);
142 int Coke_CanDispense(int UNUSED(User), int Item)
144 // Disabled in favor of caching
146 char tmp[40], *status;
147 regmatch_t matches[4];
151 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
153 // Can't dispense if the machine is not connected
154 if( giCoke_SerialFD == -1 )
158 printf("Coke_CanDispense: Flushing\n");
164 while( WaitForColon() && ret < 3 )
166 // Flush the input buffer
168 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
170 printf("Coke_CanDispense: sending 'd7'\n");
175 // Check for a timeout error
177 fprintf(stderr, "Coke machine timed out\n");
178 return -2; // -EMYBAD
181 // You need to do a 'd7' before reading the status
182 // - Otherwise it sometimes reports a full slot as empty
183 // [TPG] (2011-02-19)
190 // TODO: Handle "not ok" response to D7
193 printf("Coke_CanDispense: sending 's%i'\n", Item);
196 // Ask the coke machine
197 Writef("s%i\r\n", Item);
200 printf("Coke_CanDispense: reading response\n");
202 // Read from the machine (ignoring empty lines)
203 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
205 printf("ret = %i, tmp = '%s'\n", ret, tmp);
207 // Read back-echoed lines
208 while( tmp[0] == ':' || tmp[1] != 'l' )
210 ret = ReadLine(sizeof(tmp)-1, tmp);
211 if( ret == -1 ) return -1;
212 printf("ret = %i, tmp = '%s'\n", ret, tmp);
217 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
219 perror("Coke Machine");
225 printf("Coke_CanDispense: wait for the prompt again\n");
228 // Eat rest of response
231 return Coke_GetSlotStatus(tmp, Item);
234 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
236 // Can't dispense if the machine is not connected
237 if( giCoke_SerialFD == -1 )
240 return gaCoke_CachedStatus[Item];
245 * \brief Actually do a dispense from the coke machine
247 int Coke_DoDispense(int UNUSED(User), int Item)
253 if( Item < 0 || Item > 6 ) return -1;
255 // Can't dispense if the machine is not connected
256 if( giCoke_SerialFD == -1 )
260 pthread_mutex_lock(&gCoke_Mutex);
263 printf("Coke_DoDispense: flushing input\n");
268 while( WaitForColon() && ret < 3 )
270 // Flush the input buffer
272 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
274 printf("Coke_DoDispense: sending 'd7'\n");
280 printf("Coke_DoDispense: sending 'd%i'\n", Item);
283 Writef("d%i\r\n", Item);
285 // Read empty lines and echo-backs
287 ret = ReadLine(sizeof(tmp)-1, tmp);
289 pthread_mutex_unlock(&gCoke_Mutex);
293 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
295 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
297 WaitForColon(); // Eat up rest of response
300 printf("Coke_DoDispense: done\n");
304 if( strcmp(tmp, "ok") == 0 ) {
305 // We think dispense worked
306 // - The machine returns 'ok' if an empty slot is dispensed, even if
307 // it doesn't actually try to dispense (no sound)
311 printf("Machine returned unknown value '%s'\n", tmp);
316 Writef("s%i\r\n", Item);
317 len = ReadLine(sizeof tmp, tmp);
318 if(len == -1) gaCoke_CachedStatus[Item] = -1;
319 Coke_int_GetSlotStatus(tmp, Item);
322 pthread_mutex_unlock(&gCoke_Mutex);
332 struct timeval timeout;
334 timeout.tv_sec = READ_TIMEOUT;
338 FD_SET(giCoke_SerialFD, &readfs);
340 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
341 if( ret == 0 ) return 0; // Timeout
343 printf("readchar return %i\n", ret);
347 ret = read(giCoke_SerialFD, &ch, 1);
349 printf("ret = %i\n", ret);
356 int Writef(const char *Format, ...)
361 va_start(args, Format);
362 len = vsnprintf(NULL, 0, Format, args);
367 va_start(args, Format);
368 vsnprintf(buf, len+1, Format, args);
372 printf("Writef: %s", buf);
375 return write(giCoke_SerialFD, buf, len);
385 FD_SET(giCoke_SerialFD, &readfs);
387 while( (ch = ReadChar()) != ':' && ch != 0);
389 if( ch == 0 ) return -1; // Timeout
394 int ReadLine(int len, char *output)
409 if( ch == '\n' || ch == '\r' ) {
416 //printf("ReadLine: output=%s\n", output);