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
26 int Coke_InitHandler();
27 int Coke_CanDispense(int User, int Item);
28 int Coke_DoDispense(int User, int Item);
30 int ReadLine(int len, char *output);
33 tHandler gCoke_Handler = {
39 char *gsCoke_SerialPort = "/dev/ttyS0";
41 regex_t gCoke_StatusRegex;
44 int Coke_InitHandler()
46 printf("connecting to coke machine...\n");
48 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
49 if( giCoke_SerialFD == -1 ) {
50 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
53 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
57 int Coke_CanDispense(int UNUSED(User), int Item)
59 char tmp[40], *status;
60 regmatch_t matches[4];
64 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
66 // Can't dispense if the machine is not connected
67 if( giCoke_SerialFD == -1 )
70 // Flush the input buffer
73 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
79 write(giCoke_SerialFD, "d7\r\n", 4);
80 } while( WaitForColon() && ret++ < 3 );
83 fprintf(stderr, "Coke machine timed out\n");
87 // TODO: Handle "not ok" response to D7
89 // Ask the coke machine
90 sprintf(tmp, "s%i\r\n", Item);
91 write(giCoke_SerialFD, tmp, 4);
93 // Read from the machine (ignoring empty lines)
94 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
95 printf("ret = %i, tmp = '%s'\n", ret, tmp);
97 ret = ReadLine(sizeof(tmp)-1, tmp);
98 printf("ret = %i, tmp = '%s'\n", ret, tmp);
103 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
105 perror("Coke Machine");
110 // Eat rest of response
113 // Parse status response
114 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
120 tmp[ matches[3].rm_eo ] = '\0';
121 status = &tmp[ matches[3].rm_so ];
123 printf("Machine responded slot status '%s'\n", status);
125 if( strcmp(status, "full") == 0 )
132 * \brief Actually do a dispense from the coke machine
134 int Coke_DoDispense(int UNUSED(User), int Item)
140 if( Item < 0 || Item > 6 ) return -1;
142 // Can't dispense if the machine is not connected
143 if( giCoke_SerialFD == -1 )
146 // Flush the input buffer
149 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
155 write(Item, "d7\r\n", 4);
156 } while( WaitForColon() && i++ < 3 );
159 sprintf(tmp, "d%i\r\n", Item);
160 write(giCoke_SerialFD, tmp, 4);
163 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == -1 );
164 if( ret == -1 ) return -1;
166 while( tmp[0] == ':' ) {
167 ret = ReadLine(sizeof(tmp)-1, tmp);
168 if( ret == -1 ) return -1;
171 ret = ReadLine(sizeof(tmp)-1, tmp);
172 if( ret == -1 ) return -1;
174 WaitForColon(); // Eat up rest of response
177 if( strcmp(tmp, "ok") == 0 ) {
178 // We think dispense worked
179 // - The machine returns 'ok' if an empty slot is dispensed, even if
180 // it doesn't actually try to dispense (no sound)
184 printf("Machine returned unknown value '%s'\n", tmp);
194 struct timeval timeout;
196 timeout.tv_sec = READ_TIMEOUT;
200 FD_SET(giCoke_SerialFD, &readfs);
202 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
203 if( ret == 0 ) return 0; // Timeout
205 printf("readchar return %i\n", ret);
209 ret = read(giCoke_SerialFD, &ch, 1);
211 printf("ret = %i\n", ret);
223 FD_SET(giCoke_SerialFD, &readfs);
225 while( (ch = ReadChar()) != ':' && ch != 0);
227 if( ch == 0 ) return -1; // Timeout
232 int ReadLine(int len, char *output)
247 if( ch == '\n' || ch == '\r' ) {
254 //printf("ReadLine: output=%s\n", output);