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 )
71 write(giCoke_SerialFD, "d7\r\n", 4);
72 } while( WaitForColon() && ret++ < 3 );
75 fprintf(stderr, "Coke machine timed out\n");
79 // TODO: Handle "not ok" response to D7
81 // Ask the coke machine
82 sprintf(tmp, "s%i\r\n", Item);
83 write(giCoke_SerialFD, tmp, 4);
85 // Read from the machine (ignoring empty lines)
86 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
87 printf("ret = %i, tmp = '%s'\n", ret, tmp);
89 ret = ReadLine(sizeof(tmp)-1, tmp);
90 printf("ret = %i, tmp = '%s'\n", ret, tmp);
95 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
97 perror("Coke Machine");
102 // Eat rest of response
105 // Parse status response
106 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
112 tmp[ matches[3].rm_eo ] = '\0';
113 status = &tmp[ matches[3].rm_so ];
115 printf("Machine responded slot status '%s'\n", status);
117 if( strcmp(status, "full") == 0 )
124 * \brief Actually do a dispense from the coke machine
126 int Coke_DoDispense(int UNUSED(User), int Item)
132 if( Item < 0 || Item > 6 ) return -1;
134 // Can't dispense if the machine is not connected
135 if( giCoke_SerialFD == -1 )
141 write(Item, "d7\r\n", 4);
142 } while( WaitForColon() && i++ < 3 );
145 sprintf(tmp, "d%i\r\n", Item);
146 write(giCoke_SerialFD, tmp, 4);
149 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == -1 );
150 if( ret == -1 ) return -1;
152 while( tmp[0] == ':' ) {
153 ret = ReadLine(sizeof(tmp)-1, tmp);
154 if( ret == -1 ) return -1;
157 ret = ReadLine(sizeof(tmp)-1, tmp);
158 if( ret == -1 ) return -1;
160 WaitForColon(); // Eat up rest of response
163 if( strcmp(tmp, "ok") == 0 ) {
164 // We think dispense worked
165 // - The machine returns 'ok' if an empty slot is dispensed, even if
166 // it doesn't actually try to dispense (no sound)
170 printf("Machine returned unknown value '%s'\n", tmp);
180 struct timeval timeout;
182 timeout.tv_sec = 5; // 5 second timeout
186 FD_SET(giCoke_SerialFD, &readfs);
188 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
189 if( ret == 0 ) return 0; // Timeout
191 printf("readchar return %i\n", ret);
195 ret = read(giCoke_SerialFD, &ch, 1);
197 printf("ret = %i\n", ret);
209 FD_SET(giCoke_SerialFD, &readfs);
211 while( (ch = ReadChar()) != ':' && ch != 0);
213 if( ch == 0 ) return -1; // Timeout
218 int ReadLine(int len, char *output)
233 if( ch == '\n' || ch == '\r' ) {
240 //printf("ReadLine: output=%s\n", output);