1341679d5050b963fab8a7b4d4e9a18e83038263
[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 #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
17 // === IMPORTS ===
18
19 // === PROTOTYPES ===
20  int    Door_InitHandler();
21  int    Door_CanDispense(int User, int Item);
22  int    Door_DoDispense(int User, int Item);
23
24 // === GLOBALS ===
25 tHandler        gDoor_Handler = {
26         "door",
27         Door_InitHandler,
28         Door_CanDispense,
29         Door_DoDispense
30 };
31 char    *gsDoor_Password;
32 char    *gsDoor_Command;
33 // int  giDoor_SerialFD;
34
35 // == CODE ===
36 int Door_InitHandler(void)
37 {
38 //      printf("connecting to door...\n");
39 //      giDoor_SerialFD = open(gsDoor_SerialPort, O_RDWR);
40 //      if( giDoor_SerialFD == -1 ) {
41 //              fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsDoor_SerialPort);
42 //      }
43         return 0;
44 }
45
46 /**
47  */
48 int Door_CanDispense(int User, int Item)
49 {
50         // Sanity please
51         if( Item == 0 ) return -1;
52         
53         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
54                 return 1;
55                 
56         gsDoor_Command = malloc(sizeof("llogin door -w ")+strlen(gsDoor_Password));
57         sprintf(gsDoor_Command, "llogin door -w %s", gsDoor_Password);
58         
59         return 0;
60 }
61
62 /**
63  * \brief Actually do a dispense from the coke machine
64  */
65 int Door_DoDispense(int User, int Item)
66 {
67         FILE    *pipe;
68         // Sanity please
69         if( Item != 0 ) return -1;
70         
71         // Check if user is in door
72         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
73                 return 1;
74         
75         // llogin or other
76         pipe = popen(gsDoor_Command, "w");
77         if( !pipe || pipe == (void*)-1 )
78                 return -1;
79         
80         fputs("ATH1F", pipe);
81         sleep(1);
82         fputs("ATH10", pipe);
83         
84         pclose(pipe);
85
86         return 0;
87 }
88
89

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