c937756583c9bac0a7bbf1f9b65d03aface5d3f2
[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
25 // === GLOBALS ===
26 tHandler        gCoke_Handler = {
27         "coke",
28         Coke_InitHandler,
29         Coke_CanDispense,
30         Coke_DoDispense
31 };
32 char    *gsCoke_SerialPort = "/dev/ttyS0";
33  int    giCoke_SerialFD;
34 regex_t gCoke_StatusRegex;
35
36 // == CODE ===
37 int Coke_InitHandler()
38 {
39         printf("connecting to coke machine...\n");
40         
41         giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
42         if( giCoke_SerialFD == -1 ) {
43                 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
44         }
45         
46         CompileRegex(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
47         return 0;
48 }
49
50 int Coke_CanDispense(int User, int Item)
51 {
52         char    tmp[32], *status;
53         regmatch_t      matches[4];
54          int    ret;
55
56         // Sanity please
57         if( Item < 0 || Item > 6 )      return -1;      // -EYOURBAD
58         
59         // Ask the coke machine
60         sprintf(tmp, "s%i\n", Item);
61         write(giCoke_SerialFD, tmp, 2);
62
63         // Wait a little
64         sleep(250);
65
66         // Read the response
67         tmp[0] = '\0';
68         ret = read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
69         //printf("ret = %i\n", ret);
70         if( ret <= 0 ) {
71                 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
72                 return -1;
73         }
74         ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
75         if( ret ) {
76                 return -1;
77         }
78
79         tmp[ matches[3].rm_eo ] = '\0';
80         status = &tmp[ matches[3].rm_so ];
81
82         printf("Machine responded slot status '%s'\n", status);
83
84         if( strcmp(status, "full") == 0 )
85                 return 0;
86
87         return 1;
88 }
89
90 /**
91  * \brief Actually do a dispense from the coke machine
92  */
93 int Coke_DoDispense(int User, int Item)
94 {
95         char    tmp[32], *status;
96         regmatch_t      matches[4];
97
98         // Sanity please
99         if( Item < 0 || Item > 6 )      return -1;
100
101         // Dispense
102         sprintf(tmp, "d%i\n", Item);
103         write(giCoke_SerialFD, tmp, 2);
104         
105         // Wait a little
106         sleep(250);
107
108         // Get status
109         read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
110         regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
111         
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         return 0;
118 }
119
120

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