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 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
56 printf("connecting to coke machine...\n");
58 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
59 if( giCoke_SerialFD == -1 ) {
60 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
63 // Reset the slot names.
64 // - Dunno why this is needed, but the machine plays silly
81 Coke_int_UpdateSlotStatuses();
85 AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
90 int Coke_int_GetSlotStatus(char *Buffer, int Slot)
92 regmatch_t matches[4];
96 // Parse status response
97 ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
103 Buffer[ matches[3].rm_eo ] = '\0';
104 status = &Buffer[ matches[3].rm_so ];
107 printf("Coke_CanDispense: Machine responded slot status '%s'\n", status);
110 if( strcmp(status, "full") == 0 ) {
111 gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable
115 gaCoke_CachedStatus[Slot] = 1; // 1: Empty
121 * \brief Update the status of all coke slots
122 * \note Uses goto to reduce the chance of the lock being kept
124 void Coke_int_UpdateSlotStatuses(void)
129 if( giCoke_SerialFD == -1 ) return ;
131 pthread_mutex_lock(&gCoke_Mutex);
135 printf("Coke_int_UpdateSlotStatuses: send d7\n");
137 Writef("d7\r\n"); // Update slot statuses
138 if( WaitForColon() ) goto ret;
140 printf("Coke_int_UpdateSlotStatuses: send s\n");
144 i = ReadLine(sizeof tmp, tmp); // Read back what we just said
145 if( i == -1 ) goto ret;
146 } while(tmp[0] == ':' || tmp[0] == 's');
148 for( i = 0; i <= 6; i ++ )
150 len = ReadLine(sizeof tmp, tmp);
153 printf("Coke_int_UpdateSlotStatuses: Read failed on slot %i\n", i);
155 goto ret; // I give up :(
158 printf("Coke_int_UpdateSlotStatuses: tmp = '%s'\n", tmp);
160 Coke_int_GetSlotStatus(tmp, i);
164 pthread_mutex_unlock(&gCoke_Mutex);
167 int Coke_CanDispense(int UNUSED(User), int Item)
170 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
172 // Can't dispense if the machine is not connected
173 if( giCoke_SerialFD == -1 )
176 return gaCoke_CachedStatus[Item];
180 * \brief Actually do a dispense from the coke machine
182 int Coke_DoDispense(int UNUSED(User), int Item)
188 if( Item < 0 || Item > 6 ) return -1;
190 // Can't dispense if the machine is not connected
191 if( giCoke_SerialFD == -1 )
195 pthread_mutex_lock(&gCoke_Mutex);
198 printf("Coke_DoDispense: flushing input\n");
203 while( WaitForColon() && ret < 3 )
205 // Flush the input buffer
207 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
209 printf("Coke_DoDispense: sending 'd7'\n");
217 printf("Coke_DoDispense: timed out\n");
219 pthread_mutex_unlock(&gCoke_Mutex);
224 printf("Coke_DoDispense: sending 'd%i'\n", Item);
227 Writef("d%i\r\n", Item);
229 // Read empty lines and echo-backs
231 ret = ReadLine(sizeof(tmp)-1, tmp);
233 pthread_mutex_unlock(&gCoke_Mutex);
237 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
239 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
241 WaitForColon(); // Eat up rest of response
244 printf("Coke_DoDispense: done\n");
247 // TODO: Regex instead?
248 if( strcmp(tmp, "ok") == 0 ) {
249 // We think dispense worked
250 // - The machine returns 'ok' if an empty slot is dispensed, even if
251 // it doesn't actually try to dispense (no sound)
255 printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp);
260 printf("Coke_DoDispense: Updating slot status\n");
263 Writef("s%i\r\n", Item);
264 len = ReadLine(sizeof tmp, tmp);
265 if(len == -1) gaCoke_CachedStatus[Item] = -1;
266 Coke_int_GetSlotStatus(tmp, Item);
268 // Release and return
269 pthread_mutex_unlock(&gCoke_Mutex);
279 struct timeval timeout;
281 timeout.tv_sec = READ_TIMEOUT;
285 FD_SET(giCoke_SerialFD, &readfs);
287 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
288 if( ret == 0 ) return 0; // Timeout
290 printf("readchar return %i\n", ret);
294 ret = read(giCoke_SerialFD, &ch, 1);
296 printf("ret = %i\n", ret);
303 int Writef(const char *Format, ...)
308 va_start(args, Format);
309 len = vsnprintf(NULL, 0, Format, args);
314 va_start(args, Format);
315 vsnprintf(buf, len+1, Format, args);
319 printf("Writef: %s", buf);
322 return write(giCoke_SerialFD, buf, len);
332 FD_SET(giCoke_SerialFD, &readfs);
334 while( (ch = ReadChar()) != ':' && ch != 0);
336 if( ch == 0 ) return -1; // Timeout
341 int ReadLine(int len, char *output)
356 if( ch == '\n' || ch == '\r' ) {
363 //printf("ReadLine: output=%s\n", output);