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

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