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
78 Writef("n0 Slot0\r\n");
81 Writef("n1 Slot1\r\n");
83 Writef("n2 Slot2\r\n");
85 Writef("n3 Slot3\r\n");
87 Writef("n4 Slot4\r\n");
89 Writef("n5 Slot5\r\n");
91 Writef("n6 Coke\r\n");
94 Coke_int_UpdateSlotStatuses();
97 fprintf(stderr, "Coke machine timed out.\n");
100 AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
105 int Coke_int_GetSlotStatus(char *Buffer, int Slot)
107 regmatch_t matches[4];
111 // Parse status response
112 ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
118 Buffer[ matches[3].rm_eo ] = '\0';
119 status = &Buffer[ matches[3].rm_so ];
121 TRACE("Machine responded slot %i status '%s'\n", Slot, status);
123 if( strcmp(status, "full") == 0 ) {
124 gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable
128 gaCoke_CachedStatus[Slot] = 1; // 1: Empty
134 * \brief Update the status of all coke slots
135 * \note Uses goto to reduce the chance of the lock being kept
137 void Coke_int_UpdateSlotStatuses(void)
142 if( giCoke_SerialFD == -1 ) return ;
144 pthread_mutex_lock(&gCoke_Mutex);
146 while( ReadLine(sizeof tmp, tmp) >= 0 ) ;
148 Writef("d7\r\n"); // Update slot statuses
149 if( WaitForColon() ) goto ret;
153 i = ReadLine(sizeof tmp, tmp); // Read back what we just said
155 TRACE("Eat read failed");
158 } while(tmp[0] != ':' && tmp[1] != 's');
160 for( i = 0; i <= 6; i ++ )
162 // Read until non-blank line
163 while( (len = ReadLine(sizeof tmp, tmp)) == 0 ) ;
165 TRACE("Read failed on slot %i\n", i);
166 goto ret; // I give up :(
168 TRACE("tmp = '%s'\n", tmp);
169 Coke_int_GetSlotStatus(tmp, i);
172 len = ReadLine(sizeof tmp, tmp);
174 TRACE("Read failed on blank line\n");
180 pthread_mutex_unlock(&gCoke_Mutex);
183 int Coke_CanDispense(int UNUSED(User), int Item)
186 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
188 // Can't dispense if the machine is not connected
189 if( giCoke_SerialFD == -1 )
193 // 2011-10-21: The sensors in slot 1 and 3 a bad, just ignore the sensor result
194 if( Item == 1 || Item == 3 ) return 0;
196 return gaCoke_CachedStatus[Item];
200 * \brief Actually do a dispense from the coke machine
202 int Coke_DoDispense(int UNUSED(User), int Item)
208 if( Item < 0 || Item > 6 ) return -1;
210 // Can't dispense if the machine is not connected
211 if( giCoke_SerialFD == -1 )
214 // Make sure there are not two dispenses within n seconds
215 if( time(NULL) - gtCoke_LastDispenseTime < 10 )
217 printf("Wait %li seconds?\n", 10 - (time(NULL) - gtCoke_LastDispenseTime));
218 sleep( 10 - (time(NULL) - gtCoke_LastDispenseTime) );
219 printf("wait done\n");
223 pthread_mutex_lock(&gCoke_Mutex);
225 TRACE("flushing input\n");
230 while( WaitForColon() && ret < 3 )
232 // Flush the input buffer
234 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
235 TRACE("sending 'd7'\n");
241 TRACE("timed out\n");
242 pthread_mutex_unlock(&gCoke_Mutex);
246 TRACE("sending 'd%i'\n", Item);
248 Writef("d%i\r\n", Item);
250 // Read empty lines and echo-backs
252 ret = ReadLine(sizeof(tmp)-1, tmp);
254 pthread_mutex_unlock(&gCoke_Mutex);
257 TRACE("read %i '%s'\n", ret, tmp);
258 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
260 WaitForColon(); // Eat up rest of response
264 // TODO: Regex instead?
265 if( strcmp(tmp, "ok") == 0 ) {
266 // We think dispense worked
267 // - The machine returns 'ok' if an empty slot is dispensed, even if
268 // it doesn't actually try to dispense (no sound)
272 printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp);
276 TRACE("Updating slot status\n");
280 Writef("s%i\r\n", Item);
281 len = ReadLine(sizeof tmp, tmp);
282 if(len == -1) gaCoke_CachedStatus[Item] = -1;
283 Coke_int_GetSlotStatus(tmp, Item);
286 read(giCoke_SerialFD, buf, 512); // Flush
289 gtCoke_LastDispenseTime = time(NULL);
291 // Release and return
292 pthread_mutex_unlock(&gCoke_Mutex);
304 struct timeval timeout;
306 timeout.tv_sec = READ_TIMEOUT;
310 FD_SET(giCoke_SerialFD, &readfs);
312 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
314 fprintf(stderr, "ReadChar: Timeout of %is expired\n", READ_TIMEOUT);
318 printf("ReadChar: select return %i\n", ret);
322 ret = read(giCoke_SerialFD, &ch, 1);
324 printf("ReadChar: ret != 1 (%i)\n", ret);
331 int Writef(const char *Format, ...)
336 va_start(args, Format);
337 len = vsnprintf(NULL, 0, Format, args);
342 va_start(args, Format);
343 vsnprintf(buf, len+1, Format, args);
347 printf("Writef: %s", buf);
350 return write(giCoke_SerialFD, buf, len);
360 FD_SET(giCoke_SerialFD, &readfs);
362 while( (ch = ReadChar()) != ':' && ch != 0);
364 if( ch == 0 ) return -1; // Timeout
369 int ReadLine(int len, char *output)
384 if( ch == '\n' || ch == '\r' ) {