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
27 # define TRACE(v...) do{printf("%s: ",__func__);printf(v);}while(0)
35 int Coke_InitHandler();
36 int Coke_int_GetSlotStatus(char *Buffer, int Slot);
37 void Coke_int_UpdateSlotStatuses(void);
38 int Coke_CanDispense(int User, int Item);
39 int Coke_DoDispense(int User, int Item);
40 int Writef(const char *Format, ...);
42 int ReadLine(int len, char *output);
45 tHandler gCoke_Handler = {
51 char *gsCoke_SerialPort = "/dev/ttyS0";
53 regex_t gCoke_StatusRegex;
54 int gaCoke_CachedStatus[7];
55 pthread_mutex_t gCoke_Mutex = PTHREAD_MUTEX_INITIALIZER;
58 int Coke_InitHandler()
60 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
62 printf("connecting to coke machine...\n");
64 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
65 if( giCoke_SerialFD == -1 ) {
66 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
70 for( i = 0; i < 7; i ++ )
71 gaCoke_CachedStatus[i] = -1;
72 // Reset the slot names.
73 // - Dunno why this is needed, but the machine plays silly
90 Coke_int_UpdateSlotStatuses();
94 AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
99 int Coke_int_GetSlotStatus(char *Buffer, int Slot)
101 regmatch_t matches[4];
105 // Parse status response
106 ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
112 Buffer[ matches[3].rm_eo ] = '\0';
113 status = &Buffer[ matches[3].rm_so ];
115 TRACE("Machine responded slot %i status '%s'\n", Slot, status);
117 if( strcmp(status, "full") == 0 ) {
118 gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable
122 gaCoke_CachedStatus[Slot] = 1; // 1: Empty
128 * \brief Update the status of all coke slots
129 * \note Uses goto to reduce the chance of the lock being kept
131 void Coke_int_UpdateSlotStatuses(void)
136 if( giCoke_SerialFD == -1 ) return ;
138 pthread_mutex_lock(&gCoke_Mutex);
140 while( ReadLine(sizeof tmp, tmp) >= 0 ) ;
142 Writef("d7\r\n"); // Update slot statuses
143 if( WaitForColon() ) goto ret;
147 i = ReadLine(sizeof tmp, tmp); // Read back what we just said
149 TRACE("Eat read failed");
152 } while(tmp[0] != ':' && tmp[1] != 's');
154 for( i = 0; i <= 6; i ++ )
156 len = ReadLine(sizeof tmp, tmp);
158 TRACE("Read failed on slot %i\n", i);
159 goto ret; // I give up :(
161 TRACE("tmp = '%s'\n", tmp);
162 Coke_int_GetSlotStatus(tmp, i);
165 len = ReadLine(sizeof tmp, tmp);
167 TRACE("Read failed on blank line\n");
173 pthread_mutex_unlock(&gCoke_Mutex);
176 int Coke_CanDispense(int UNUSED(User), int Item)
179 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
181 // Can't dispense if the machine is not connected
182 if( giCoke_SerialFD == -1 )
185 return gaCoke_CachedStatus[Item];
189 * \brief Actually do a dispense from the coke machine
191 int Coke_DoDispense(int UNUSED(User), int Item)
197 if( Item < 0 || Item > 6 ) return -1;
199 // Can't dispense if the machine is not connected
200 if( giCoke_SerialFD == -1 )
204 pthread_mutex_lock(&gCoke_Mutex);
206 TRACE("flushing input\n");
211 while( ReadLine(512, buf) != -1 );
216 while( WaitForColon() && ret < 3 )
218 // Flush the input buffer
220 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
221 TRACE("sending 'd7'\n");
227 TRACE("timed out\n");
228 pthread_mutex_unlock(&gCoke_Mutex);
232 TRACE("sending 'd%i'\n", Item);
234 Writef("d%i\r\n", Item);
236 // Read empty lines and echo-backs
238 ret = ReadLine(sizeof(tmp)-1, tmp);
240 pthread_mutex_unlock(&gCoke_Mutex);
243 TRACE("read %i '%s'\n", ret, tmp);
244 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
246 WaitForColon(); // Eat up rest of response
250 // TODO: Regex instead?
251 if( strcmp(tmp, "ok") == 0 ) {
252 // We think dispense worked
253 // - The machine returns 'ok' if an empty slot is dispensed, even if
254 // it doesn't actually try to dispense (no sound)
258 printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp);
262 TRACE("Updating slot status\n");
265 Writef("s%i\r\n", Item);
266 len = ReadLine(sizeof tmp, tmp);
267 if(len == -1) gaCoke_CachedStatus[Item] = -1;
268 Coke_int_GetSlotStatus(tmp, Item);
271 read(giCoke_SerialFD, buf, 512); // Flush
274 // Release and return
275 pthread_mutex_unlock(&gCoke_Mutex);
287 struct timeval timeout;
289 timeout.tv_sec = READ_TIMEOUT;
293 FD_SET(giCoke_SerialFD, &readfs);
295 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
296 if( ret == 0 ) return 0; // Timeout
298 printf("ReadChar: select return %i\n", ret);
302 ret = read(giCoke_SerialFD, &ch, 1);
304 printf("ReadChar: ret != 1 (%i)\n", ret);
311 int Writef(const char *Format, ...)
316 va_start(args, Format);
317 len = vsnprintf(NULL, 0, Format, args);
322 va_start(args, Format);
323 vsnprintf(buf, len+1, Format, args);
327 printf("Writef: %s", buf);
330 return write(giCoke_SerialFD, buf, len);
340 FD_SET(giCoke_SerialFD, &readfs);
342 while( (ch = ReadChar()) != ':' && ch != 0);
344 if( ch == 0 ) return -1; // Timeout
349 int ReadLine(int len, char *output)
364 if( ch == '\n' || ch == '\r' ) {