5729f06af4f9d17f596ce2dd9e2fb81e32f2430c
[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         ret = 0;
65         do {
66                 write(giCoke_SerialFD, "d7\r\n", 4);
67         } while( WaitForColon() && ret++ < 3 );
68
69         if( ret == 3 ) {
70                 fprintf(stderr, "Coke machine timed out\n");
71                 return -2;      // -EMYBAD
72         }
73
74         // TODO: Handle "not ok" response to D7
75         
76         // Ask the coke machine
77         sprintf(tmp, "s%i\r\n", Item);
78         write(giCoke_SerialFD, tmp, 4);
79
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);
83         if( tmp[0] == ':' ) {
84                 ret = ReadLine(sizeof(tmp)-1, tmp);
85                 printf("ret = %i, tmp = '%s'\n", ret, tmp);
86         }
87
88         // Catch an error       
89         if( ret <= 0 ) {
90                 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
91                 if( ret == -1 ) {
92                         perror("Coke Machine");
93                 }
94                 return -1;
95         }
96
97         // Eat rest of response
98         WaitForColon();
99
100         // Parse status response
101         ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
102         if( ret ) {
103                 return -1;
104         }
105
106         // Get slot status
107         tmp[ matches[3].rm_eo ] = '\0';
108         status = &tmp[ matches[3].rm_so ];
109
110         printf("Machine responded slot status '%s'\n", status);
111
112         if( strcmp(status, "full") == 0 )
113                 return 0;
114
115         return 1;
116 }
117
118 /**
119  * \brief Actually do a dispense from the coke machine
120  */
121 int Coke_DoDispense(int UNUSED(User), int Item)
122 {
123         char    tmp[32];
124          int    i, ret;
125
126         // Sanity please
127         if( Item < 0 || Item > 6 )      return -1;
128
129         // Wait for prompt
130         i = 0;
131         do {
132                 write(Item, "d7\r\n", 4);
133         } while( WaitForColon() && i++ < 3 );
134
135         // Dispense
136         sprintf(tmp, "d%i\r\n", Item);
137         write(giCoke_SerialFD, tmp, 4);
138         
139         // Read empty lines
140         while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == -1 );
141         if( ret == -1 ) return -1;
142         // Read d%i
143         while( tmp[0] == ':' ) {
144                 ret = ReadLine(sizeof(tmp)-1, tmp);
145                 if( ret == -1 ) return -1;
146         }
147         // Get status
148         ret = ReadLine(sizeof(tmp)-1, tmp);
149         if( ret == -1 ) return -1;
150
151         WaitForColon(); // Eat up rest of response
152
153         // TODO: Regex
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)
158                 return 0;
159         }
160
161         printf("Machine returned unknown value '%s'\n", tmp);   
162
163         return -1;
164 }
165
166 char ReadChar()
167 {
168         fd_set  readfs;
169         char    ch = 0;
170          int    ret;
171         struct timeval  timeout;
172         
173         timeout.tv_sec = 5;     // 5 second timeout
174         timeout.tv_usec = 0;
175         
176         FD_ZERO(&readfs);
177         FD_SET(giCoke_SerialFD, &readfs);
178         
179         ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
180         if( ret == 0 )  return 0;       // Timeout
181         if( ret != 1 ) {
182                 printf("readchar return %i\n", ret);
183                 return 0;
184         }
185         
186         ret = read(giCoke_SerialFD, &ch, 1);
187         if( ret != 1 ) {
188                 printf("ret = %i\n", ret);
189                 return 0;
190         }
191         
192         return ch;
193 }
194
195 int WaitForColon()
196 {
197         fd_set  readfs;
198         char    ch = 0;
199         
200         FD_SET(giCoke_SerialFD, &readfs);
201         
202         while( (ch = ReadChar()) != ':' && ch != 0);
203         
204         if( ch == 0 )   return -1;      // Timeout
205         
206         return 0;
207 }
208
209 int ReadLine(int len, char *output)
210 {
211         char    ch;
212          int    i = 0;
213         
214         for(;;)
215         {
216                 ch = ReadChar();
217                         
218                 if( i < len )
219                         output[i++] = ch;
220                 
221                 if( ch == '\0' ) {
222                         break;
223                 }
224                 if( ch == '\n' || ch == '\r' ) {
225                         if( i < len )
226                                 output[--i] = '\0';
227                         break;
228                 }
229         }
230
231         //printf("ReadLine: output=%s\n", output);
232
233         if( !ch )       return -1;
234         return i;
235 }
236
237

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