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 on '%s'\n", gsCoke_SerialPort);
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
76 Writef("n0 Slot0\r\n");
79 Writef("n1 Slot1\r\n");
81 Writef("n2 Slot2\r\n");
83 Writef("n3 Slot3\r\n");
85 Writef("n4 Slot4\r\n");
87 Writef("n5 Slot5\r\n");
89 Writef("n6 Coke\r\n");
92 Coke_int_UpdateSlotStatuses();
95 fprintf(stderr, "Coke machine timed out.\n");
98 AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
103 int Coke_int_GetSlotStatus(char *Buffer, int Slot)
105 regmatch_t matches[4];
109 // Parse status response
110 ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
116 Buffer[ matches[3].rm_eo ] = '\0';
117 status = &Buffer[ matches[3].rm_so ];
119 TRACE("Machine responded slot %i status '%s'\n", Slot, status);
121 if( strcmp(status, "full") == 0 ) {
122 gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable
126 gaCoke_CachedStatus[Slot] = 1; // 1: Empty
132 * \brief Update the status of all coke slots
133 * \note Uses goto to reduce the chance of the lock being kept
135 void Coke_int_UpdateSlotStatuses(void)
140 if( giCoke_SerialFD == -1 ) return ;
142 pthread_mutex_lock(&gCoke_Mutex);
144 while( ReadLine(sizeof tmp, tmp) >= 0 ) ;
146 Writef("d7\r\n"); // Update slot statuses
147 if( WaitForColon() ) goto ret;
151 i = ReadLine(sizeof tmp, tmp); // Read back what we just said
153 TRACE("Eat read failed");
156 } while(tmp[0] != ':' && tmp[1] != 's');
158 for( i = 0; i <= 6; i ++ )
160 // Read until non-blank line
161 while( (len = ReadLine(sizeof tmp, tmp)) == 0 ) ;
163 TRACE("Read failed on slot %i\n", i);
164 goto ret; // I give up :(
166 TRACE("tmp = '%s'\n", tmp);
167 Coke_int_GetSlotStatus(tmp, i);
170 len = ReadLine(sizeof tmp, tmp);
172 TRACE("Read failed on blank line\n");
178 pthread_mutex_unlock(&gCoke_Mutex);
181 int Coke_CanDispense(int UNUSED(User), int Item)
184 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
186 // Can't dispense if the machine is not connected
187 if( giCoke_SerialFD == -1 )
190 return gaCoke_CachedStatus[Item];
194 * \brief Actually do a dispense from the coke machine
196 int Coke_DoDispense(int UNUSED(User), int Item)
202 if( Item < 0 || Item > 6 ) return -1;
204 // Can't dispense if the machine is not connected
205 if( giCoke_SerialFD == -1 )
208 // Make sure there are not two dispenses within n seconds
209 if( time(NULL) - gtCoke_LastDispenseTime < 10 )
211 printf("Wait %li seconds?\n", 10 - (time(NULL) - gtCoke_LastDispenseTime));
212 sleep( 10 - (time(NULL) - gtCoke_LastDispenseTime) );
213 printf("wait done\n");
217 pthread_mutex_lock(&gCoke_Mutex);
219 TRACE("flushing input\n");
224 while( WaitForColon() && ret < 3 )
226 // Flush the input buffer
228 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
229 TRACE("sending 'd7'\n");
235 TRACE("timed out\n");
236 pthread_mutex_unlock(&gCoke_Mutex);
240 TRACE("sending 'd%i'\n", Item);
242 Writef("d%i\r\n", Item);
244 // Read empty lines and echo-backs
246 ret = ReadLine(sizeof(tmp)-1, tmp);
248 pthread_mutex_unlock(&gCoke_Mutex);
251 TRACE("read %i '%s'\n", ret, tmp);
252 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
254 WaitForColon(); // Eat up rest of response
258 // TODO: Regex instead?
259 if( strcmp(tmp, "ok") == 0 ) {
260 // We think dispense worked
261 // - The machine returns 'ok' if an empty slot is dispensed, even if
262 // it doesn't actually try to dispense (no sound)
266 printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp);
270 TRACE("Updating slot status\n");
274 Writef("s%i\r\n", Item);
275 len = ReadLine(sizeof tmp, tmp);
276 if(len == -1) gaCoke_CachedStatus[Item] = -1;
277 Coke_int_GetSlotStatus(tmp, Item);
280 read(giCoke_SerialFD, buf, 512); // Flush
283 gtCoke_LastDispenseTime = time(NULL);
285 // Release and return
286 pthread_mutex_unlock(&gCoke_Mutex);
298 struct timeval timeout;
300 timeout.tv_sec = READ_TIMEOUT;
304 FD_SET(giCoke_SerialFD, &readfs);
306 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
307 if( ret == 0 ) return 0; // Timeout
309 printf("ReadChar: select return %i\n", ret);
313 ret = read(giCoke_SerialFD, &ch, 1);
315 printf("ReadChar: ret != 1 (%i)\n", ret);
322 int Writef(const char *Format, ...)
327 va_start(args, Format);
328 len = vsnprintf(NULL, 0, Format, args);
333 va_start(args, Format);
334 vsnprintf(buf, len+1, Format, args);
338 printf("Writef: %s", buf);
341 return write(giCoke_SerialFD, buf, len);
351 FD_SET(giCoke_SerialFD, &readfs);
353 while( (ch = ReadChar()) != ':' && ch != 0);
355 if( ch == 0 ) return -1; // Timeout
360 int ReadLine(int len, char *output)
375 if( ch == '\n' || ch == '\r' ) {