Cleaning up client, cleaning coke code
[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         WaitForColon();
75
76         ret = ReadLine(sizeof(tmp)-1, tmp);
77         printf("ret = %i, tmp = '%s'\n", ret, tmp);
78         
79         if( ret <= 0 ) {
80                 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
81                 if( ret == -1 ) {
82                         perror("Coke Machine");
83                 }
84                 return -1;
85         }
86         ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
87         if( ret ) {
88                 return -1;
89         }
90
91         tmp[ matches[3].rm_eo ] = '\0';
92         status = &tmp[ matches[3].rm_so ];
93
94         printf("Machine responded slot status '%s'\n", status);
95
96         if( strcmp(status, "full") == 0 )
97                 return 0;
98
99         return 1;
100 }
101
102 /**
103  * \brief Actually do a dispense from the coke machine
104  */
105 int Coke_DoDispense(int User, int Item)
106 {
107         char    tmp[32], *status;
108         regmatch_t      matches[4];
109
110         // Sanity please
111         if( Item < 0 || Item > 6 )      return -1;
112
113         WaitForColon();
114
115         // Dispense
116         sprintf(tmp, "d%i\r\n", Item);
117         write(giCoke_SerialFD, tmp, 4);
118         
119         WaitForColon();
120
121         // Get status
122         ReadLine(sizeof(tmp)-1, tmp);
123         
124         tmp[ matches[3].rm_eo ] = '\0';
125         status = &tmp[ matches[3].rm_so ];
126
127         printf("Machine responded slot status '%s'\n", status);
128
129         return 0;
130 }
131
132 char ReadChar()
133 {
134         fd_set  readfs;
135         char    ch = 0;
136          int    ret;
137         struct timeval  timeout;
138         
139         timeout.tv_sec = 5;     // 5 second timeout
140         timeout.tv_usec = 0;
141         
142         FD_ZERO(&readfs);
143         FD_SET(giCoke_SerialFD, &readfs);
144         
145         ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
146         if( ret == 0 )  return 0;       // Timeout
147         if( ret != 1 ) {
148                 printf("readchar return %i\n", ret);
149                 return 0;
150         }
151         
152         ret = read(giCoke_SerialFD, &ch, 1);
153         if( ret != 1 ) {
154                 printf("ret = %i\n", ret);
155                 return 0;
156         }
157         
158         return ch;
159 }
160
161 int WaitForColon()
162 {
163         fd_set  readfs;
164         char    ch = 0;
165         
166         FD_SET(giCoke_SerialFD, &readfs);
167         
168         while( (ch = ReadChar()) != ':' && ch != 0);
169         
170         if( ch == 0 )   return -1;      // Timeout
171         
172         return 0;
173 }
174
175 int ReadLine(int len, char *output)
176 {
177         char    ch;
178          int    i = 0;
179         
180         for(;;)
181         {
182                 ch = ReadChar();
183                 
184                 
185                 if( i < len )
186                         output[i++] = ch;
187                 
188                 if( ch == '\0' ) {
189                         return -1;
190                 }
191                 if( ch == '\n' || ch == '\r' ) {
192                         if( i < len )
193                                 output[--i] = '\0';
194                         return i;
195                 }
196         }
197 }
198
199

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