3359ed16fa11952151269a5bcaa39f3b990d8a09
[tpg/opendispense2.git] / src / server / handler_door.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * handler_doror.c - Door Relay code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  */
10 #define DEBUG   1
11 #define USE_POPEN       0
12
13 #include "common.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <signal.h>
20 #include <unistd.h>
21 #include <sys/wait.h>
22 #include <pty.h>
23
24 #define DOOR_UNLOCKED_DELAY     5       // Time in seconds before the door re-locks
25
26 // === IMPORTS ===
27
28 // === PROTOTYPES ===
29  int    Door_InitHandler();
30  int    Door_CanDispense(int User, int Item);
31  int    Door_DoDispense(int User, int Item);
32
33 // === GLOBALS ===
34 tHandler        gDoor_Handler = {
35         "door",
36         Door_InitHandler,
37         Door_CanDispense,
38         Door_DoDispense
39 };
40 char    *gsDoor_SerialPort = "/dev/ttyS3";
41
42 // == CODE ===
43 int Door_InitHandler(void)
44 {
45         return 0;
46 }
47
48 /**
49  */
50 int Door_CanDispense(int User, int Item)
51 {
52         #if DEBUG
53         printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
54         #endif
55         // Sanity please
56         if( Item != 0 ) return -1;
57         
58         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
59         {
60                 #if DEBUG
61                 printf("Door_CanDispense: User %i not in door\n", User);
62                 #endif
63                 return 1;
64         }
65         
66         #if DEBUG
67         printf("Door_CanDispense: User %i can open the door\n", User);
68         #endif
69         
70         return 0;
71 }
72
73 /**
74  * \brief Actually do a dispense from the coke machine
75  */
76 int Door_DoDispense(int User, int Item)
77 {
78          int    door_serial_handle;
79         
80         #if DEBUG
81         printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
82         #endif
83         
84         // Sanity please
85         if( Item != 0 ) return -1;
86         
87         // Check if user is in door
88         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
89         {
90                 #if DEBUG
91                 printf("Door_CanDispense: User %i not in door\n", User);
92                 #endif
93                 return 1;
94         }
95         
96         door_serial_handle = InitSerial(gsDoor_SerialPort, 1200);
97
98         if( write(door_serial_handle, "ATH1\n", 5) != 5 ) {
99                 fprintf(stderr, "Unable to open door (sending ATH1)\n");
100                 perror("Sending ATH1");
101                 return -1;
102         }
103         
104         // Wait before re-locking
105         sleep(DOOR_UNLOCKED_DELAY);
106
107         if( write(door_serial_handle, "ATH0\n", 5) != 5 ) {
108                 fprintf(stderr, "Oh, hell! Door not re-locking, big error (sending ATH0 failed)\n");
109                 perror("Sending ATH0");
110                 return -1;
111         }
112
113         close(door_serial_handle);
114         
115         #if DEBUG
116         printf("Door_DoDispense: User %i opened door\n", User);
117         #endif
118
119         return 0;
120 }
121

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