Fixed behavior in coke (maybe)
[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 #include "common.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <regex.h>
17
18 // === IMPORTS ===
19
20 // === PROTOTYPES ===
21  int    Coke_InitHandler();
22  int    Coke_CanDispense(int User, int Item);
23  int    Coke_DoDispense(int User, int Item);
24  int    WaitForColon();
25  int    ReadLine(int len, char *output);
26
27 // === GLOBALS ===
28 tHandler        gCoke_Handler = {
29         "coke",
30         Coke_InitHandler,
31         Coke_CanDispense,
32         Coke_DoDispense
33 };
34 char    *gsCoke_SerialPort = "/dev/ttyS0";
35  int    giCoke_SerialFD;
36 regex_t gCoke_StatusRegex;
37
38 // == CODE ===
39 int Coke_InitHandler()
40 {
41         printf("connecting to coke machine...\n");
42         
43         giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
44         if( giCoke_SerialFD == -1 ) {
45                 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
46         }
47         
48         CompileRegex(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
49         return 0;
50 }
51
52 int Coke_CanDispense(int User, int Item)
53 {
54         char    tmp[32], *status;
55         regmatch_t      matches[4];
56          int    ret;
57
58         // Sanity please
59         if( Item < 0 || Item > 6 )      return -1;      // -EYOURBAD
60         
61         write(giCoke_SerialFD, "d7\r\n", 4);
62         write(giCoke_SerialFD, "d7\r\n", 4);
63         write(giCoke_SerialFD, "d7\r\n", 4);
64         
65         if( WaitForColon() ) {
66                 fprintf(stderr, "Coke machine timed out\n");
67                 return -2;      // -EMYBAD
68         }
69         
70         // Ask the coke machine
71         sprintf(tmp, "s%i\r\n", Item);
72         write(giCoke_SerialFD, tmp, 4);
73
74         ret = ReadLine(sizeof(tmp)-1, tmp);
75         printf("ret = %i, tmp = '%s'\n", ret, tmp);
76         
77         if( ret <= 0 ) {
78                 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
79                 if( ret == -1 ) {
80                         perror("Coke Machine");
81                 }
82                 return -1;
83         }
84         ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
85         if( ret ) {
86                 return -1;
87         }
88
89         tmp[ matches[3].rm_eo ] = '\0';
90         status = &tmp[ matches[3].rm_so ];
91
92         printf("Machine responded slot status '%s'\n", status);
93
94         if( strcmp(status, "full") == 0 )
95                 return 0;
96
97         return 1;
98 }
99
100 /**
101  * \brief Actually do a dispense from the coke machine
102  */
103 int Coke_DoDispense(int User, int Item)
104 {
105         char    tmp[32], *status;
106         regmatch_t      matches[4];
107
108         // Sanity please
109         if( Item < 0 || Item > 6 )      return -1;
110
111         WaitForColon();
112
113         // Dispense
114         sprintf(tmp, "d%i\r\n", Item);
115         write(giCoke_SerialFD, tmp, 4);
116         
117         WaitForColon();
118
119         // Get status
120         ReadLine(sizeof(tmp)-1, tmp);
121         
122         tmp[ matches[3].rm_eo ] = '\0';
123         status = &tmp[ matches[3].rm_so ];
124
125         printf("Machine responded slot status '%s'\n", status);
126
127         return 0;
128 }
129
130 char ReadChar()
131 {
132         fd_set  readfs;
133         char    ch = 0;
134          int    ret;
135         struct timeval  timeout;
136         
137         timeout.tv_sec = 5;     // 5 second timeout
138         timeout.tv_usec = 0;
139         
140         FD_ZERO(&readfs);
141         FD_SET(giCoke_SerialFD, &readfs);
142         
143         ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
144         if( ret == 0 )  return 0;       // Timeout
145         if( ret != 1 ) {
146                 printf("readchar return %i\n", ret);
147                 return 0;
148         }
149         
150         ret = read(giCoke_SerialFD, &ch, 1);
151         if( ret != 1 ) {
152                 printf("ret = %i\n", ret);
153                 return 0;
154         }
155         
156         return ch;
157 }
158
159 int WaitForColon()
160 {
161         fd_set  readfs;
162         char    ch = 0;
163         
164         FD_SET(giCoke_SerialFD, &readfs);
165         
166         while( (ch = ReadChar()) != ':' && ch != 0);
167         
168         if( ch == 0 )   return -1;      // Timeout
169         
170         return 0;
171 }
172
173 int ReadLine(int len, char *output)
174 {
175         char    ch;
176          int    i = 0;
177         
178         for(;;)
179         {
180                 ch = ReadChar();
181                 
182                 
183                 if( i < len )
184                         output[i++] = ch;
185                 
186                 if( ch == '\0' ) {
187                         return -1;
188                 }
189                 if( ch == '\n' || ch == '\r' ) {
190                         if( i < len )
191                                 output[--i] = '\0';
192                         return i;
193                 }
194         }
195 }
196
197

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