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;
56 time_t gtCoke_LastDispenseTime;
59 int Coke_InitHandler()
61 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
63 printf("connecting to coke machine...\n");
65 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
66 if( giCoke_SerialFD == -1 ) {
67 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
71 for( i = 0; i < 7; i ++ )
72 gaCoke_CachedStatus[i] = -1;
73 // Reset the slot names.
74 // - Dunno why this is needed, but the machine plays silly
91 Coke_int_UpdateSlotStatuses();
95 AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
100 int Coke_int_GetSlotStatus(char *Buffer, int Slot)
102 regmatch_t matches[4];
106 // Parse status response
107 ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
113 Buffer[ matches[3].rm_eo ] = '\0';
114 status = &Buffer[ matches[3].rm_so ];
116 TRACE("Machine responded slot %i status '%s'\n", Slot, status);
118 if( strcmp(status, "full") == 0 ) {
119 gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable
123 gaCoke_CachedStatus[Slot] = 1; // 1: Empty
129 * \brief Update the status of all coke slots
130 * \note Uses goto to reduce the chance of the lock being kept
132 void Coke_int_UpdateSlotStatuses(void)
137 if( giCoke_SerialFD == -1 ) return ;
139 pthread_mutex_lock(&gCoke_Mutex);
141 while( ReadLine(sizeof tmp, tmp) >= 0 ) ;
143 Writef("d7\r\n"); // Update slot statuses
144 if( WaitForColon() ) goto ret;
148 i = ReadLine(sizeof tmp, tmp); // Read back what we just said
150 TRACE("Eat read failed");
153 } while(tmp[0] != ':' && tmp[1] != 's');
155 for( i = 0; i <= 6; i ++ )
157 len = ReadLine(sizeof tmp, tmp);
159 TRACE("Read failed on slot %i\n", i);
160 goto ret; // I give up :(
162 TRACE("tmp = '%s'\n", tmp);
163 Coke_int_GetSlotStatus(tmp, i);
166 len = ReadLine(sizeof tmp, tmp);
168 TRACE("Read failed on blank line\n");
174 pthread_mutex_unlock(&gCoke_Mutex);
177 int Coke_CanDispense(int UNUSED(User), int Item)
180 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
182 // Can't dispense if the machine is not connected
183 if( giCoke_SerialFD == -1 )
186 return gaCoke_CachedStatus[Item];
190 * \brief Actually do a dispense from the coke machine
192 int Coke_DoDispense(int UNUSED(User), int Item)
198 if( Item < 0 || Item > 6 ) return -1;
200 // Can't dispense if the machine is not connected
201 if( giCoke_SerialFD == -1 )
204 // Make sure there are not two dispenses within n seconds
205 if( time(NULL) - gtCoke_LastDispenseTime < 10 )
207 printf("Wait %li seconds?\n", 10 - (time(NULL) - gtCoke_LastDispenseTime));
208 sleep( 10 - (time(NULL) - gtCoke_LastDispenseTime) );
209 printf("wait done\n");
213 pthread_mutex_lock(&gCoke_Mutex);
215 TRACE("flushing input\n");
220 while( WaitForColon() && ret < 3 )
222 // Flush the input buffer
224 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
225 TRACE("sending 'd7'\n");
231 TRACE("timed out\n");
232 pthread_mutex_unlock(&gCoke_Mutex);
236 TRACE("sending 'd%i'\n", Item);
238 Writef("d%i\r\n", Item);
240 // Read empty lines and echo-backs
242 ret = ReadLine(sizeof(tmp)-1, tmp);
244 pthread_mutex_unlock(&gCoke_Mutex);
247 TRACE("read %i '%s'\n", ret, tmp);
248 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
250 WaitForColon(); // Eat up rest of response
254 // TODO: Regex instead?
255 if( strcmp(tmp, "ok") == 0 ) {
256 // We think dispense worked
257 // - The machine returns 'ok' if an empty slot is dispensed, even if
258 // it doesn't actually try to dispense (no sound)
262 printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp);
266 TRACE("Updating slot status\n");
270 Writef("s%i\r\n", Item);
271 len = ReadLine(sizeof tmp, tmp);
272 if(len == -1) gaCoke_CachedStatus[Item] = -1;
273 Coke_int_GetSlotStatus(tmp, Item);
276 read(giCoke_SerialFD, buf, 512); // Flush
279 gtCoke_LastDispenseTime = time(NULL);
281 // Release and return
282 pthread_mutex_unlock(&gCoke_Mutex);
294 struct timeval timeout;
296 timeout.tv_sec = READ_TIMEOUT;
300 FD_SET(giCoke_SerialFD, &readfs);
302 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
303 if( ret == 0 ) return 0; // Timeout
305 printf("ReadChar: select return %i\n", ret);
309 ret = read(giCoke_SerialFD, &ch, 1);
311 printf("ReadChar: ret != 1 (%i)\n", ret);
318 int Writef(const char *Format, ...)
323 va_start(args, Format);
324 len = vsnprintf(NULL, 0, Format, args);
329 va_start(args, Format);
330 vsnprintf(buf, len+1, Format, args);
334 printf("Writef: %s", buf);
337 return write(giCoke_SerialFD, buf, len);
347 FD_SET(giCoke_SerialFD, &readfs);
349 while( (ch = ReadChar()) != ':' && ch != 0);
351 if( ch == 0 ) return -1; // Timeout
356 int ReadLine(int len, char *output)
371 if( ch == '\n' || ch == '\r' ) {