Door controller code (well, the start of it)
[tpg/opendispense2.git] / src / server / handler_snack.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  * - Dispense Server
5  *
6  * handler_snack.c - Snack machine code
7  *
8  * This file is licenced under the 3-clause BSD Licence. See the file
9  * COPYING for full details.
10  */
11 #include "common.h"
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <regex.h>
18
19 // === IMPORTS ===
20
21 // === PROTOTYPES ===
22  int    Snack_InitHandler();
23  int    Snack_CanDispense(int User, int Item);
24  int    Snack_DoDispense(int User, int Item);
25
26 // === GLOBALS ===
27 tHandler        gSnack_Handler = {
28         "snack",
29         Snack_InitHandler,
30         Snack_CanDispense,
31         Snack_DoDispense
32 };
33 char    *gsSnack_SerialPort = "/dev/ttyS1";
34  int    giSnack_SerialFD;
35 regex_t gSnack_ResponseRegex;
36
37 // == CODE ===
38 int Snack_InitHandler()
39 {
40         giSnack_SerialFD = open(gsSnack_SerialPort, O_RDWR);
41         regcomp(&gSnack_ResponseRegex, "^(\\d\\d\\d)(.*)$", REG_EXTENDED);
42         return 0;
43 }
44
45 int Snack_CanDispense(int User, int Item)
46 {
47         // Sanity please
48         if( Item < 0 || Item > 99 )     return -1;
49         
50         // Hmm... could we implement slot statuses?
51         
52         return 1;
53 }
54
55 /**
56  * \brief Actually do a dispense from the coke machine
57  */
58 int Snack_DoDispense(int User, int Item)
59 {
60         char    tmp[32];
61         regmatch_t      matches[4];
62
63         // Sanity please
64         if( Item < 0 || Item > 99 )     return -1;
65
66         // Dispense
67         sprintf(tmp, "V%02i\n", Item);
68         write(giSnack_SerialFD, tmp, 2);
69
70         // Get status
71         read(giSnack_SerialFD, tmp, sizeof(tmp)-1);
72         regexec(&gSnack_ResponseRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
73
74         return 0;
75 }

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