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( WaitForColon() && ret < 3 )
213 // Flush the input buffer
215 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
216 TRACE("sending 'd7'\n");
222 TRACE("timed out\n");
223 pthread_mutex_unlock(&gCoke_Mutex);
227 TRACE("sending 'd%i'\n", Item);
229 Writef("d%i\r\n", Item);
231 // Read empty lines and echo-backs
233 ret = ReadLine(sizeof(tmp)-1, tmp);
235 pthread_mutex_unlock(&gCoke_Mutex);
238 TRACE("read %i '%s'\n", ret, tmp);
239 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
241 WaitForColon(); // Eat up rest of response
245 // TODO: Regex instead?
246 if( strcmp(tmp, "ok") == 0 ) {
247 // We think dispense worked
248 // - The machine returns 'ok' if an empty slot is dispensed, even if
249 // it doesn't actually try to dispense (no sound)
253 printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp);
257 TRACE("Updating slot status\n");
261 Writef("s%i\r\n", Item);
262 len = ReadLine(sizeof tmp, tmp);
263 if(len == -1) gaCoke_CachedStatus[Item] = -1;
264 Coke_int_GetSlotStatus(tmp, Item);
267 read(giCoke_SerialFD, buf, 512); // Flush
270 // Release and return
271 pthread_mutex_unlock(&gCoke_Mutex);
283 struct timeval timeout;
285 timeout.tv_sec = READ_TIMEOUT;
289 FD_SET(giCoke_SerialFD, &readfs);
291 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
292 if( ret == 0 ) return 0; // Timeout
294 printf("ReadChar: select return %i\n", ret);
298 ret = read(giCoke_SerialFD, &ch, 1);
300 printf("ReadChar: ret != 1 (%i)\n", ret);
307 int Writef(const char *Format, ...)
312 va_start(args, Format);
313 len = vsnprintf(NULL, 0, Format, args);
318 va_start(args, Format);
319 vsnprintf(buf, len+1, Format, args);
323 printf("Writef: %s", buf);
326 return write(giCoke_SerialFD, buf, len);
336 FD_SET(giCoke_SerialFD, &readfs);
338 while( (ch = ReadChar()) != ':' && ch != 0);
340 if( ch == 0 ) return -1; // Timeout
345 int ReadLine(int len, char *output)
360 if( ch == '\n' || ch == '\r' ) {