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 User, int Item)
57 char tmp[40], *status;
58 regmatch_t matches[4];
62 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
66 write(giCoke_SerialFD, "d7\r\n", 4);
67 } while( WaitForColon() && ret++ < 3 );
70 fprintf(stderr, "Coke machine timed out\n");
74 // TODO: Handle "not ok" response to D7
76 // Ask the coke machine
77 sprintf(tmp, "s%i\r\n", Item);
78 write(giCoke_SerialFD, tmp, 4);
80 // Read from the machine (ignoring empty lines)
81 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
82 printf("ret = %i, tmp = '%s'\n", ret, tmp);
84 ret = ReadLine(sizeof(tmp)-1, tmp);
85 printf("ret = %i, tmp = '%s'\n", ret, tmp);
90 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
92 perror("Coke Machine");
97 // Eat rest of response
100 // Parse status response
101 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
107 tmp[ matches[3].rm_eo ] = '\0';
108 status = &tmp[ matches[3].rm_so ];
110 printf("Machine responded slot status '%s'\n", status);
112 if( strcmp(status, "full") == 0 )
119 * \brief Actually do a dispense from the coke machine
121 int Coke_DoDispense(int User, int Item)
127 if( Item < 0 || Item > 6 ) return -1;
132 write(Item, "d7\r\n", 4);
133 } while( WaitForColon() && i++ < 3 );
136 sprintf(tmp, "d%i\r\n", Item);
137 write(giCoke_SerialFD, tmp, 4);
140 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == -1 );
141 if( ret == -1 ) return -1;
143 while( tmp[0] == ':' ) {
144 ret = ReadLine(sizeof(tmp)-1, tmp);
145 if( ret == -1 ) return -1;
148 ret = ReadLine(sizeof(tmp)-1, tmp);
149 if( ret == -1 ) return -1;
151 WaitForColon(); // Eat up rest of response
154 if( strcmp(tmp, "ok") == 0 ) {
155 // We think dispense worked
156 // - The machine returns 'ok' if an empty slot is dispensed, even if
157 // it doesn't actually try to dispense (no sound)
161 printf("Machine returned unknown value '%s'\n", tmp);
171 struct timeval timeout;
173 timeout.tv_sec = 5; // 5 second timeout
177 FD_SET(giCoke_SerialFD, &readfs);
179 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
180 if( ret == 0 ) return 0; // Timeout
182 printf("readchar return %i\n", ret);
186 ret = read(giCoke_SerialFD, &ch, 1);
188 printf("ret = %i\n", ret);
200 FD_SET(giCoke_SerialFD, &readfs);
202 while( (ch = ReadChar()) != ':' && ch != 0);
204 if( ch == 0 ) return -1; // Timeout
209 int ReadLine(int len, char *output)
224 if( ch == '\n' || ch == '\r' ) {
231 //printf("ReadLine: output=%s\n", output);