Fixing crappy code in coke handler, debug in others
[tpg/opendispense2.git] / src / server / handler_coke.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * handler_coke.c - Coke controller code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  *
10  * NOTES:
11  * - Remember, the coke machine echoes your text back to you!
12  */
13 #include "common.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <regex.h>
20
21 #define READ_TIMEOUT    2       // 2 seconds for ReadChar
22 #define TRACE_COKE      1
23
24 // === IMPORTS ===
25
26 // === PROTOTYPES ===
27  int    Coke_InitHandler();
28  int    Coke_CanDispense(int User, int Item);
29  int    Coke_DoDispense(int User, int Item);
30  int    WaitForColon();
31  int    ReadLine(int len, char *output);
32
33 // === GLOBALS ===
34 tHandler        gCoke_Handler = {
35         "coke",
36         Coke_InitHandler,
37         Coke_CanDispense,
38         Coke_DoDispense
39 };
40 char    *gsCoke_SerialPort = "/dev/ttyS0";
41  int    giCoke_SerialFD;
42 regex_t gCoke_StatusRegex;
43
44 // == CODE ===
45 int Coke_InitHandler()
46 {
47         printf("connecting to coke machine...\n");
48         
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);
52         }
53         
54         CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
55         return 0;
56 }
57
58 int Coke_CanDispense(int UNUSED(User), int Item)
59 {
60         char    tmp[40], *status;
61         regmatch_t      matches[4];
62          int    ret;
63
64         // Sanity please
65         if( Item < 0 || Item > 6 )      return -1;      // -EYOURBAD
66         
67         // Can't dispense if the machine is not connected
68         if( giCoke_SerialFD == -1 )
69                 return -2;
70         
71         #if TRACE_COKE
72         printf("Coke_CanDispense: Flushing\n");
73         #endif
74         
75         
76         // Wait for a prompt
77         ret = 0;
78         while( WaitForColon() && ret < 3 )
79         {
80                 // Flush the input buffer
81                 char    tmpbuf[512];
82                 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
83                 #if TRACE_COKE
84                 printf("Coke_CanDispense: sending 'd7'\n");
85                 #endif
86                 write(giCoke_SerialFD, "d7\r\n", 4);
87                 ret ++;
88         }
89
90         if( !(ret < 3) ) {
91                 fprintf(stderr, "Coke machine timed out\n");
92                 return -2;      // -EMYBAD
93         }
94
95         // TODO: Handle "not ok" response to D7
96         
97         #if TRACE_COKE
98         printf("Coke_CanDispense: sending 's%i'\n", Item);
99         #endif
100         
101         // Ask the coke machine
102         sprintf(tmp, "s%i\r\n", Item);
103         write(giCoke_SerialFD, tmp, 4);
104
105         #if TRACE_COKE
106         printf("Coke_CanDispense: reading response\n");
107         #endif
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' )
113         {
114                 ret = ReadLine(sizeof(tmp)-1, tmp);
115                 printf("ret = %i, tmp = '%s'\n", ret, tmp);
116         }
117
118         // Catch an error       
119         if( ret <= 0 ) {
120                 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
121                 if( ret == -1 ) {
122                         perror("Coke Machine");
123                 }
124                 return -1;
125         }
126         
127         #if TRACE_COKE
128         printf("Coke_CanDispense: wait for the prompt again\n");
129         #endif
130
131         // Eat rest of response
132         WaitForColon();
133
134         // Parse status response
135         ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
136         if( ret ) {
137                 return -1;
138         }
139
140         // Get slot status
141         tmp[ matches[3].rm_eo ] = '\0';
142         status = &tmp[ matches[3].rm_so ];
143
144         printf("Machine responded slot status '%s'\n", status);
145         
146         #if TRACE_COKE
147         printf("Coke_CanDispense: done");
148         #endif
149
150         if( strcmp(status, "full") == 0 )
151                 return 0;
152
153         return 1;
154 }
155
156 /**
157  * \brief Actually do a dispense from the coke machine
158  */
159 int Coke_DoDispense(int UNUSED(User), int Item)
160 {
161         char    tmp[32];
162          int    ret;
163
164         // Sanity please
165         if( Item < 0 || Item > 6 )      return -1;
166
167         // Can't dispense if the machine is not connected
168         if( giCoke_SerialFD == -1 )
169                 return -2;
170         
171         #if TRACE_COKE
172         printf("Coke_DoDispense: flushing input\n");
173         #endif
174         
175         // Wait for prompt
176         ret = 0;
177         while( WaitForColon() && ret < 3 )
178         {
179                 // Flush the input buffer
180                 char    tmpbuf[512];
181                 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
182                 #if TRACE_COKE
183                 printf("Coke_DoDispense: sending 'd7'\n");
184                 #endif
185                 write(Item, "d7\r\n", 4);
186         }
187
188         #if TRACE_COKE
189         printf("Coke_DoDispense: sending 'd%i'\n", Item);
190         #endif
191         // Dispense
192         sprintf(tmp, "d%i\r\n", Item);
193         write(giCoke_SerialFD, tmp, 4);
194         
195         // Read empty lines and echo-backs
196         do {
197                 ret = ReadLine(sizeof(tmp)-1, tmp);
198                 if( ret == -1 ) return -1;
199                 #if TRACE_COKE
200                 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
201                 #endif
202         } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
203
204         WaitForColon(); // Eat up rest of response
205         
206         #if TRACE_COKE
207         printf("Coke_DoDispense: done\n");
208         #endif
209
210         // TODO: Regex
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)
215                 return 0;
216         }
217
218         printf("Machine returned unknown value '%s'\n", tmp);   
219
220         return -1;
221 }
222
223 char ReadChar()
224 {
225         fd_set  readfs;
226         char    ch = 0;
227          int    ret;
228         struct timeval  timeout;
229         
230         timeout.tv_sec = READ_TIMEOUT;
231         timeout.tv_usec = 0;
232         
233         FD_ZERO(&readfs);
234         FD_SET(giCoke_SerialFD, &readfs);
235         
236         ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
237         if( ret == 0 )  return 0;       // Timeout
238         if( ret != 1 ) {
239                 printf("readchar return %i\n", ret);
240                 return 0;
241         }
242         
243         ret = read(giCoke_SerialFD, &ch, 1);
244         if( ret != 1 ) {
245                 printf("ret = %i\n", ret);
246                 return 0;
247         }
248         
249         return ch;
250 }
251
252 int WaitForColon()
253 {
254         fd_set  readfs;
255         char    ch = 0;
256         
257         FD_SET(giCoke_SerialFD, &readfs);
258         
259         while( (ch = ReadChar()) != ':' && ch != 0);
260         
261         if( ch == 0 )   return -1;      // Timeout
262         
263         return 0;
264 }
265
266 int ReadLine(int len, char *output)
267 {
268         char    ch;
269          int    i = 0;
270         
271         for(;;)
272         {
273                 ch = ReadChar();
274                         
275                 if( i < len )
276                         output[i++] = ch;
277                 
278                 if( ch == '\0' ) {
279                         break;
280                 }
281                 if( ch == '\n' || ch == '\r' ) {
282                         if( i < len )
283                                 output[--i] = '\0';
284                         break;
285                 }
286         }
287
288         //printf("ReadLine: output=%s\n", output);
289
290         if( !ch )       return -1;
291         return i;
292 }
293
294

UCC git Repository :: git.ucc.asn.au