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

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