Fixed door for direct serial
[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  int    writes(int fd, const char *str);
33
34 // === GLOBALS ===
35 tHandler        gDoor_Handler = {
36         "door",
37         Door_InitHandler,
38         Door_CanDispense,
39         Door_DoDispense
40 };
41 char    *gsDoor_SerialPort = "/dev/ttyS3";
42
43 // == CODE ===
44 int Door_InitHandler(void)
45 {
46         return 0;
47 }
48
49 /**
50  */
51 int Door_CanDispense(int User, int Item)
52 {
53         #if DEBUG
54         printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
55         #endif
56         // Sanity please
57         if( Item != 0 ) return -1;
58         
59         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
60         {
61                 #if DEBUG
62                 printf("Door_CanDispense: User %i not in door\n", User);
63                 #endif
64                 return 1;
65         }
66         
67         #if DEBUG
68         printf("Door_CanDispense: User %i can open the door\n", User);
69         #endif
70         
71         return 0;
72 }
73
74 /**
75  * \brief Actually do a dispense from the coke machine
76  */
77 int Door_DoDispense(int User, int Item)
78 {
79          int    door_serial_handle;
80         
81         #if DEBUG
82         printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
83         #endif
84         
85         // Sanity please
86         if( Item != 0 ) return -1;
87         
88         // Check if user is in door
89         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
90         {
91                 #if DEBUG
92                 printf("Door_CanDispense: User %i not in door\n", User);
93                 #endif
94                 return 1;
95         }
96         
97         door_serial_handle = InitSerial(gsDoor_SerialPort, 1200);
98         if(door_serial_handle < 0) {
99                 fprintf(stderr, "Unable to open door serial '%s'\n", gsDoor_SerialPort);
100                 perror("Opening door port");
101                 return -1;
102         }
103
104         {
105                 struct termios  info;
106                 tcgetattr(door_serial_handle, &info);
107                 info.c_iflag &= ~IGNCR; // Ignore \r
108                 tcsetattr(door_serial_handle, TCSANOW, &info);
109         }
110
111         if( writes(door_serial_handle, "\r\nATH1\r\n") ) {
112                 fprintf(stderr, "Unable to open door (sending ATH1)\n");
113                 perror("Sending ATH1");
114                 return -1;
115         }
116         
117         // Wait before re-locking
118         sleep(DOOR_UNLOCKED_DELAY);
119
120         if( writes(door_serial_handle, "\r\nATH0\r\n") ) {
121                 fprintf(stderr, "Oh, hell! Door not re-locking, big error (sending ATH0 failed)\n");
122                 perror("Sending ATH0");
123                 return -1;
124         }
125
126         close(door_serial_handle);
127         
128         #if DEBUG
129         printf("Door_DoDispense: User %i opened door\n", User);
130         #endif
131
132         return 0;
133 }
134
135 int writes(int fd, const char *str)
136 {
137          int    len = strlen(str);
138         
139         if( len != write(fd, str, len) )
140         {
141                 return 1;
142         }
143         return 0;
144 }
145

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