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
91 Coke_int_UpdateSlotStatuses();
94 fprintf(stderr, "Coke machine timed out.\n");
97 AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
102 int Coke_int_GetSlotStatus(char *Buffer, int Slot)
104 regmatch_t matches[4];
108 // Parse status response
109 ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
115 Buffer[ matches[3].rm_eo ] = '\0';
116 status = &Buffer[ matches[3].rm_so ];
118 TRACE("Machine responded slot %i status '%s'\n", Slot, status);
120 if( strcmp(status, "full") == 0 ) {
121 gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable
125 gaCoke_CachedStatus[Slot] = 1; // 1: Empty
131 * \brief Update the status of all coke slots
132 * \note Uses goto to reduce the chance of the lock being kept
134 void Coke_int_UpdateSlotStatuses(void)
139 if( giCoke_SerialFD == -1 ) return ;
141 pthread_mutex_lock(&gCoke_Mutex);
143 while( ReadLine(sizeof tmp, tmp) >= 0 ) ;
145 Writef("d7\r\n"); // Update slot statuses
146 if( WaitForColon() ) goto ret;
150 i = ReadLine(sizeof tmp, tmp); // Read back what we just said
152 TRACE("Eat read failed");
155 } while(tmp[0] != ':' && tmp[1] != 's');
157 for( i = 0; i <= 6; i ++ )
159 // Read until non-blank line
160 while( (len = ReadLine(sizeof tmp, tmp)) == 0 ) ;
162 TRACE("Read failed on slot %i\n", i);
163 goto ret; // I give up :(
165 TRACE("tmp = '%s'\n", tmp);
166 Coke_int_GetSlotStatus(tmp, i);
169 len = ReadLine(sizeof tmp, tmp);
171 TRACE("Read failed on blank line\n");
177 pthread_mutex_unlock(&gCoke_Mutex);
180 int Coke_CanDispense(int UNUSED(User), int Item)
183 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
185 // Can't dispense if the machine is not connected
186 if( giCoke_SerialFD == -1 )
189 return gaCoke_CachedStatus[Item];
193 * \brief Actually do a dispense from the coke machine
195 int Coke_DoDispense(int UNUSED(User), int Item)
201 if( Item < 0 || Item > 6 ) return -1;
203 // Can't dispense if the machine is not connected
204 if( giCoke_SerialFD == -1 )
207 // Make sure there are not two dispenses within n seconds
208 if( time(NULL) - gtCoke_LastDispenseTime < 10 )
210 printf("Wait %li seconds?\n", 10 - (time(NULL) - gtCoke_LastDispenseTime));
211 sleep( 10 - (time(NULL) - gtCoke_LastDispenseTime) );
212 printf("wait done\n");
216 pthread_mutex_lock(&gCoke_Mutex);
218 TRACE("flushing input\n");
223 while( WaitForColon() && ret < 3 )
225 // Flush the input buffer
227 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
228 TRACE("sending 'd7'\n");
234 TRACE("timed out\n");
235 pthread_mutex_unlock(&gCoke_Mutex);
239 TRACE("sending 'd%i'\n", Item);
241 Writef("d%i\r\n", Item);
243 // Read empty lines and echo-backs
245 ret = ReadLine(sizeof(tmp)-1, tmp);
247 pthread_mutex_unlock(&gCoke_Mutex);
250 TRACE("read %i '%s'\n", ret, tmp);
251 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
253 WaitForColon(); // Eat up rest of response
257 // TODO: Regex instead?
258 if( strcmp(tmp, "ok") == 0 ) {
259 // We think dispense worked
260 // - The machine returns 'ok' if an empty slot is dispensed, even if
261 // it doesn't actually try to dispense (no sound)
265 printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp);
269 TRACE("Updating slot status\n");
273 Writef("s%i\r\n", Item);
274 len = ReadLine(sizeof tmp, tmp);
275 if(len == -1) gaCoke_CachedStatus[Item] = -1;
276 Coke_int_GetSlotStatus(tmp, Item);
279 read(giCoke_SerialFD, buf, 512); // Flush
282 gtCoke_LastDispenseTime = time(NULL);
284 // Release and return
285 pthread_mutex_unlock(&gCoke_Mutex);
297 struct timeval timeout;
299 timeout.tv_sec = READ_TIMEOUT;
303 FD_SET(giCoke_SerialFD, &readfs);
305 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
306 if( ret == 0 ) return 0; // Timeout
308 printf("ReadChar: select return %i\n", ret);
312 ret = read(giCoke_SerialFD, &ch, 1);
314 printf("ReadChar: ret != 1 (%i)\n", ret);
321 int Writef(const char *Format, ...)
326 va_start(args, Format);
327 len = vsnprintf(NULL, 0, Format, args);
332 va_start(args, Format);
333 vsnprintf(buf, len+1, Format, args);
337 printf("Writef: %s", buf);
340 return write(giCoke_SerialFD, buf, len);
350 FD_SET(giCoke_SerialFD, &readfs);
352 while( (ch = ReadChar()) != ':' && ch != 0);
354 if( ch == 0 ) return -1; // Timeout
359 int ReadLine(int len, char *output)
374 if( ch == '\n' || ch == '\r' ) {