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

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