3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * handler_doror.c - Door Relay code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
24 #define DOOR_UNLOCKED_DELAY 5 // Time in seconds before the door re-locks
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);
35 tHandler gDoor_Handler = {
41 char *gsDoor_SerialPort = "/dev/ttyS3";
44 int Door_InitHandler(void)
51 int Door_CanDispense(int User, int Item)
54 printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
57 if( Item != 0 ) return -1;
59 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
62 printf("Door_CanDispense: User %i not in door\n", User);
68 printf("Door_CanDispense: User %i can open the door\n", User);
75 * \brief Actually do a dispense from the coke machine
77 int Door_DoDispense(int User, int Item)
79 int door_serial_handle;
82 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
86 if( Item != 0 ) return -1;
88 // Check if user is in door
89 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
92 printf("Door_CanDispense: User %i not in door\n", User);
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");
106 tcgetattr(door_serial_handle, &info);
107 // info.c_iflag &= ~IGNCR; // Ignore \r
108 info.c_cflag &= ~CLOCAL;
109 tcsetattr(door_serial_handle, TCSANOW, &info);
112 writes(door_serial_handle, "\r\r");
115 if( writes(door_serial_handle, "ATH1\r") ) {
116 fprintf(stderr, "Unable to open door (sending ATH1)\n");
117 perror("Sending ATH1");
121 // Wait before re-locking
122 sleep(DOOR_UNLOCKED_DELAY);
124 if( writes(door_serial_handle, "ATH0\r") ) {
125 fprintf(stderr, "Oh, hell! Door not re-locking, big error (sending ATH0 failed)\n");
126 perror("Sending ATH0");
130 close(door_serial_handle);
133 printf("Door_DoDispense: User %i opened door\n", User);
139 int writes(int fd, const char *str)
141 int len = strlen(str);
143 if( len != write(fd, str, len) )