5dc4a35b3ca9dd2896bd431094905db16da88783
[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 // === IMPORTS ===
22
23 // === PROTOTYPES ===
24  int    Coke_InitHandler();
25  int    Coke_CanDispense(int User, int Item);
26  int    Coke_DoDispense(int User, int Item);
27  int    WaitForColon();
28  int    ReadLine(int len, char *output);
29
30 // === GLOBALS ===
31 tHandler        gCoke_Handler = {
32         "coke",
33         Coke_InitHandler,
34         Coke_CanDispense,
35         Coke_DoDispense
36 };
37 char    *gsCoke_SerialPort = "/dev/ttyS0";
38  int    giCoke_SerialFD;
39 regex_t gCoke_StatusRegex;
40
41 // == CODE ===
42 int Coke_InitHandler()
43 {
44         printf("connecting to coke machine...\n");
45         
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);
49         }
50         
51         CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
52         return 0;
53 }
54
55 int Coke_CanDispense(int UNUSED(User), int Item)
56 {
57         char    tmp[40], *status;
58         regmatch_t      matches[4];
59          int    ret;
60
61         // Sanity please
62         if( Item < 0 || Item > 6 )      return -1;      // -EYOURBAD
63         
64         // Can't dispense if the machine is not connected
65         if( giCoke_SerialFD == -1 )
66                 return -2;
67         
68         // Wait for a prompt
69         ret = 0;
70         do {
71                 write(giCoke_SerialFD, "d7\r\n", 4);
72         } while( WaitForColon() && ret++ < 3 );
73
74         if( ret == 3 ) {
75                 fprintf(stderr, "Coke machine timed out\n");
76                 return -2;      // -EMYBAD
77         }
78
79         // TODO: Handle "not ok" response to D7
80         
81         // Ask the coke machine
82         sprintf(tmp, "s%i\r\n", Item);
83         write(giCoke_SerialFD, tmp, 4);
84
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);
88         if( tmp[0] == ':' ) {
89                 ret = ReadLine(sizeof(tmp)-1, tmp);
90                 printf("ret = %i, tmp = '%s'\n", ret, tmp);
91         }
92
93         // Catch an error       
94         if( ret <= 0 ) {
95                 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
96                 if( ret == -1 ) {
97                         perror("Coke Machine");
98                 }
99                 return -1;
100         }
101
102         // Eat rest of response
103         WaitForColon();
104
105         // Parse status response
106         ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
107         if( ret ) {
108                 return -1;
109         }
110
111         // Get slot status
112         tmp[ matches[3].rm_eo ] = '\0';
113         status = &tmp[ matches[3].rm_so ];
114
115         printf("Machine responded slot status '%s'\n", status);
116
117         if( strcmp(status, "full") == 0 )
118                 return 0;
119
120         return 1;
121 }
122
123 /**
124  * \brief Actually do a dispense from the coke machine
125  */
126 int Coke_DoDispense(int UNUSED(User), int Item)
127 {
128         char    tmp[32];
129          int    i, ret;
130
131         // Sanity please
132         if( Item < 0 || Item > 6 )      return -1;
133
134         // Can't dispense if the machine is not connected
135         if( giCoke_SerialFD == -1 )
136                 return -2;
137
138         // Wait for prompt
139         i = 0;
140         do {
141                 write(Item, "d7\r\n", 4);
142         } while( WaitForColon() && i++ < 3 );
143
144         // Dispense
145         sprintf(tmp, "d%i\r\n", Item);
146         write(giCoke_SerialFD, tmp, 4);
147         
148         // Read empty lines
149         while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == -1 );
150         if( ret == -1 ) return -1;
151         // Read d%i
152         while( tmp[0] == ':' ) {
153                 ret = ReadLine(sizeof(tmp)-1, tmp);
154                 if( ret == -1 ) return -1;
155         }
156         // Get status
157         ret = ReadLine(sizeof(tmp)-1, tmp);
158         if( ret == -1 ) return -1;
159
160         WaitForColon(); // Eat up rest of response
161
162         // TODO: Regex
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)
167                 return 0;
168         }
169
170         printf("Machine returned unknown value '%s'\n", tmp);   
171
172         return -1;
173 }
174
175 char ReadChar()
176 {
177         fd_set  readfs;
178         char    ch = 0;
179          int    ret;
180         struct timeval  timeout;
181         
182         timeout.tv_sec = 5;     // 5 second timeout
183         timeout.tv_usec = 0;
184         
185         FD_ZERO(&readfs);
186         FD_SET(giCoke_SerialFD, &readfs);
187         
188         ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
189         if( ret == 0 )  return 0;       // Timeout
190         if( ret != 1 ) {
191                 printf("readchar return %i\n", ret);
192                 return 0;
193         }
194         
195         ret = read(giCoke_SerialFD, &ch, 1);
196         if( ret != 1 ) {
197                 printf("ret = %i\n", ret);
198                 return 0;
199         }
200         
201         return ch;
202 }
203
204 int WaitForColon()
205 {
206         fd_set  readfs;
207         char    ch = 0;
208         
209         FD_SET(giCoke_SerialFD, &readfs);
210         
211         while( (ch = ReadChar()) != ':' && ch != 0);
212         
213         if( ch == 0 )   return -1;      // Timeout
214         
215         return 0;
216 }
217
218 int ReadLine(int len, char *output)
219 {
220         char    ch;
221          int    i = 0;
222         
223         for(;;)
224         {
225                 ch = ReadChar();
226                         
227                 if( i < len )
228                         output[i++] = ch;
229                 
230                 if( ch == '\0' ) {
231                         break;
232                 }
233                 if( ch == '\n' || ch == '\r' ) {
234                         if( i < len )
235                                 output[--i] = '\0';
236                         break;
237                 }
238         }
239
240         //printf("ReadLine: output=%s\n", output);
241
242         if( !ch )       return -1;
243         return i;
244 }
245
246

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