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!
21 #define READ_TIMEOUT 2 // 2 seconds for ReadChar
27 int Coke_InitHandler();
28 int Coke_CanDispense(int User, int Item);
29 int Coke_DoDispense(int User, int Item);
31 int ReadLine(int len, char *output);
34 tHandler gCoke_Handler = {
40 char *gsCoke_SerialPort = "/dev/ttyS0";
42 regex_t gCoke_StatusRegex;
45 int Coke_InitHandler()
47 printf("connecting to coke machine...\n");
49 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
50 if( giCoke_SerialFD == -1 ) {
51 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
54 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
58 int Coke_CanDispense(int UNUSED(User), int Item)
60 char tmp[40], *status;
61 regmatch_t matches[4];
65 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
67 // Can't dispense if the machine is not connected
68 if( giCoke_SerialFD == -1 )
72 printf("Coke_CanDispense: Flushing\n");
78 while( WaitForColon() && ret < 3 )
80 // Flush the input buffer
82 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
84 printf("Coke_CanDispense: sending 'd7'\n");
86 write(giCoke_SerialFD, "d7\r\n", 4);
91 fprintf(stderr, "Coke machine timed out\n");
95 // TODO: Handle "not ok" response to D7
98 printf("Coke_CanDispense: sending 's%i'\n", Item);
101 // Ask the coke machine
102 sprintf(tmp, "s%i\r\n", Item);
103 write(giCoke_SerialFD, tmp, 4);
106 printf("Coke_CanDispense: reading response\n");
108 // Read from the machine (ignoring empty lines)
109 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
110 printf("ret = %i, tmp = '%s'\n", ret, tmp);
111 // Read back-echoed lines
112 while( tmp[0] == ':' || tmp[1] != 'l' )
114 ret = ReadLine(sizeof(tmp)-1, tmp);
115 printf("ret = %i, tmp = '%s'\n", ret, tmp);
120 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
122 perror("Coke Machine");
128 printf("Coke_CanDispense: wait for the prompt again\n");
131 // Eat rest of response
134 // Parse status response
135 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
141 tmp[ matches[3].rm_eo ] = '\0';
142 status = &tmp[ matches[3].rm_so ];
144 printf("Machine responded slot status '%s'\n", status);
147 printf("Coke_CanDispense: done");
150 if( strcmp(status, "full") == 0 )
157 * \brief Actually do a dispense from the coke machine
159 int Coke_DoDispense(int UNUSED(User), int Item)
165 if( Item < 0 || Item > 6 ) return -1;
167 // Can't dispense if the machine is not connected
168 if( giCoke_SerialFD == -1 )
172 printf("Coke_DoDispense: flushing input\n");
177 while( WaitForColon() && ret < 3 )
179 // Flush the input buffer
181 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
183 printf("Coke_DoDispense: sending 'd7'\n");
185 write(Item, "d7\r\n", 4);
189 printf("Coke_DoDispense: sending 'd%i'\n", Item);
192 sprintf(tmp, "d%i\r\n", Item);
193 write(giCoke_SerialFD, tmp, 4);
195 // Read empty lines and echo-backs
197 ret = ReadLine(sizeof(tmp)-1, tmp);
198 if( ret == -1 ) return -1;
200 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
202 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
204 WaitForColon(); // Eat up rest of response
207 printf("Coke_DoDispense: done\n");
211 if( strcmp(tmp, "ok") == 0 ) {
212 // We think dispense worked
213 // - The machine returns 'ok' if an empty slot is dispensed, even if
214 // it doesn't actually try to dispense (no sound)
218 printf("Machine returned unknown value '%s'\n", tmp);
228 struct timeval timeout;
230 timeout.tv_sec = READ_TIMEOUT;
234 FD_SET(giCoke_SerialFD, &readfs);
236 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
237 if( ret == 0 ) return 0; // Timeout
239 printf("readchar return %i\n", ret);
243 ret = read(giCoke_SerialFD, &ch, 1);
245 printf("ret = %i\n", ret);
257 FD_SET(giCoke_SerialFD, &readfs);
259 while( (ch = ReadChar()) != ':' && ch != 0);
261 if( ch == 0 ) return -1; // Timeout
266 int ReadLine(int len, char *output)
281 if( ch == '\n' || ch == '\r' ) {
288 //printf("ReadLine: output=%s\n", output);