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
29 int Coke_InitHandler();
30 int Coke_int_GetSlotStatus(char *Buffer, int Slot);
31 void Coke_int_UpdateSlotStatuses(void);
32 int Coke_CanDispense(int User, int Item);
33 int Coke_DoDispense(int User, int Item);
34 int Writef(const char *Format, ...);
36 int ReadLine(int len, char *output);
39 tHandler gCoke_Handler = {
45 char *gsCoke_SerialPort = "/dev/ttyS0";
47 regex_t gCoke_StatusRegex;
48 int gaCoke_CachedStatus[7];
49 pthread_mutex_t gCoke_Mutex = PTHREAD_MUTEX_INITIALIZER;
52 int Coke_InitHandler()
54 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
56 printf("connecting to coke machine...\n");
58 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
59 if( giCoke_SerialFD == -1 ) {
60 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
64 for( i = 0; i < 7; i ++ )
65 gaCoke_CachedStatus[i] = -1;
66 // Reset the slot names.
67 // - Dunno why this is needed, but the machine plays silly
84 Coke_int_UpdateSlotStatuses();
88 AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
93 int Coke_int_GetSlotStatus(char *Buffer, int Slot)
95 regmatch_t matches[4];
99 // Parse status response
100 ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
106 Buffer[ matches[3].rm_eo ] = '\0';
107 status = &Buffer[ matches[3].rm_so ];
110 printf("Coke_CanDispense: Machine responded slot status '%s'\n", status);
113 if( strcmp(status, "full") == 0 ) {
114 gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable
118 gaCoke_CachedStatus[Slot] = 1; // 1: Empty
124 * \brief Update the status of all coke slots
125 * \note Uses goto to reduce the chance of the lock being kept
127 void Coke_int_UpdateSlotStatuses(void)
132 if( giCoke_SerialFD == -1 ) return ;
134 pthread_mutex_lock(&gCoke_Mutex);
138 printf("Coke_int_UpdateSlotStatuses: send d7\n");
140 Writef("d7\r\n"); // Update slot statuses
141 if( WaitForColon() ) goto ret;
143 printf("Coke_int_UpdateSlotStatuses: send s\n");
147 i = ReadLine(sizeof tmp, tmp); // Read back what we just said
148 if( i == -1 ) goto ret;
149 } while(tmp[0] == ':' || tmp[0] == 's');
151 for( i = 0; i <= 6; i ++ )
153 len = ReadLine(sizeof tmp, tmp);
156 printf("Coke_int_UpdateSlotStatuses: Read failed on slot %i\n", i);
158 goto ret; // I give up :(
161 printf("Coke_int_UpdateSlotStatuses: tmp = '%s'\n", tmp);
163 Coke_int_GetSlotStatus(tmp, i);
167 pthread_mutex_unlock(&gCoke_Mutex);
170 int Coke_CanDispense(int UNUSED(User), int Item)
173 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
175 // Can't dispense if the machine is not connected
176 if( giCoke_SerialFD == -1 )
179 return gaCoke_CachedStatus[Item];
183 * \brief Actually do a dispense from the coke machine
185 int Coke_DoDispense(int UNUSED(User), int Item)
191 if( Item < 0 || Item > 6 ) return -1;
193 // Can't dispense if the machine is not connected
194 if( giCoke_SerialFD == -1 )
198 pthread_mutex_lock(&gCoke_Mutex);
201 printf("Coke_DoDispense: flushing input\n");
206 while( WaitForColon() && ret < 3 )
208 // Flush the input buffer
210 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
212 printf("Coke_DoDispense: sending 'd7'\n");
220 printf("Coke_DoDispense: timed out\n");
222 pthread_mutex_unlock(&gCoke_Mutex);
227 printf("Coke_DoDispense: sending 'd%i'\n", Item);
230 Writef("d%i\r\n", Item);
232 // Read empty lines and echo-backs
234 ret = ReadLine(sizeof(tmp)-1, tmp);
236 pthread_mutex_unlock(&gCoke_Mutex);
240 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
242 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
244 WaitForColon(); // Eat up rest of response
247 printf("Coke_DoDispense: done\n");
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);
263 printf("Coke_DoDispense: Updating slot status\n");
266 Writef("s%i\r\n", Item);
267 len = ReadLine(sizeof tmp, tmp);
268 if(len == -1) gaCoke_CachedStatus[Item] = -1;
269 Coke_int_GetSlotStatus(tmp, Item);
271 // Release and return
272 pthread_mutex_unlock(&gCoke_Mutex);
282 struct timeval timeout;
284 timeout.tv_sec = READ_TIMEOUT;
288 FD_SET(giCoke_SerialFD, &readfs);
290 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
291 if( ret == 0 ) return 0; // Timeout
293 printf("readchar return %i\n", ret);
297 ret = read(giCoke_SerialFD, &ch, 1);
299 printf("ret = %i\n", ret);
306 int Writef(const char *Format, ...)
311 va_start(args, Format);
312 len = vsnprintf(NULL, 0, Format, args);
317 va_start(args, Format);
318 vsnprintf(buf, len+1, Format, args);
322 printf("Writef: %s", buf);
325 return write(giCoke_SerialFD, buf, len);
335 FD_SET(giCoke_SerialFD, &readfs);
337 while( (ch = ReadChar()) != ':' && ch != 0);
339 if( ch == 0 ) return -1; // Timeout
344 int ReadLine(int len, char *output)
359 if( ch == '\n' || ch == '\r' ) {
366 //printf("ReadLine: output=%s\n", output);