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!
24 int Coke_InitHandler();
25 int Coke_CanDispense(int User, int Item);
26 int Coke_DoDispense(int User, int Item);
28 int ReadLine(int len, char *output);
31 tHandler gCoke_Handler = {
37 char *gsCoke_SerialPort = "/dev/ttyS0";
39 regex_t gCoke_StatusRegex;
42 int Coke_InitHandler()
44 printf("connecting to coke machine...\n");
46 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
47 if( giCoke_SerialFD == -1 ) {
48 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
51 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
55 int Coke_CanDispense(int UNUSED(User), int Item)
57 char tmp[40], *status;
58 regmatch_t matches[4];
62 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
64 // Can't dispense if the machine is not connected
65 if( giCoke_SerialFD == -1 )
68 // Flush the input buffer
71 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
77 write(giCoke_SerialFD, "d7\r\n", 4);
78 } while( WaitForColon() && ret++ < 3 );
81 fprintf(stderr, "Coke machine timed out\n");
85 // TODO: Handle "not ok" response to D7
87 // Ask the coke machine
88 sprintf(tmp, "s%i\r\n", Item);
89 write(giCoke_SerialFD, tmp, 4);
91 // Read from the machine (ignoring empty lines)
92 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
93 printf("ret = %i, tmp = '%s'\n", ret, tmp);
95 ret = ReadLine(sizeof(tmp)-1, tmp);
96 printf("ret = %i, tmp = '%s'\n", ret, tmp);
101 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
103 perror("Coke Machine");
108 // Eat rest of response
111 // Parse status response
112 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
118 tmp[ matches[3].rm_eo ] = '\0';
119 status = &tmp[ matches[3].rm_so ];
121 printf("Machine responded slot status '%s'\n", status);
123 if( strcmp(status, "full") == 0 )
130 * \brief Actually do a dispense from the coke machine
132 int Coke_DoDispense(int UNUSED(User), int Item)
138 if( Item < 0 || Item > 6 ) return -1;
140 // Can't dispense if the machine is not connected
141 if( giCoke_SerialFD == -1 )
144 // Flush the input buffer
147 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
153 write(Item, "d7\r\n", 4);
154 } while( WaitForColon() && i++ < 3 );
157 sprintf(tmp, "d%i\r\n", Item);
158 write(giCoke_SerialFD, tmp, 4);
161 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == -1 );
162 if( ret == -1 ) return -1;
164 while( tmp[0] == ':' ) {
165 ret = ReadLine(sizeof(tmp)-1, tmp);
166 if( ret == -1 ) return -1;
169 ret = ReadLine(sizeof(tmp)-1, tmp);
170 if( ret == -1 ) return -1;
172 WaitForColon(); // Eat up rest of response
175 if( strcmp(tmp, "ok") == 0 ) {
176 // We think dispense worked
177 // - The machine returns 'ok' if an empty slot is dispensed, even if
178 // it doesn't actually try to dispense (no sound)
182 printf("Machine returned unknown value '%s'\n", tmp);
192 struct timeval timeout;
194 timeout.tv_sec = 5; // 5 second timeout
198 FD_SET(giCoke_SerialFD, &readfs);
200 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
201 if( ret == 0 ) return 0; // Timeout
203 printf("readchar return %i\n", ret);
207 ret = read(giCoke_SerialFD, &ch, 1);
209 printf("ret = %i\n", ret);
221 FD_SET(giCoke_SerialFD, &readfs);
223 while( (ch = ReadChar()) != ':' && ch != 0);
225 if( ch == 0 ) return -1; // Timeout
230 int ReadLine(int len, char *output)
245 if( ch == '\n' || ch == '\r' ) {
252 //printf("ReadLine: output=%s\n", output);