X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fserver%2Fhandler_door.c;h=190769aa739f03ed764d7d9e32077a9ddca51b20;hb=0b5755205d388b5704c040c21285c0b7ab58825e;hp=62fb8f960b9c4c2974a63abc12553cb98937b538;hpb=74b6a69bdc5a9f96d5781939167ae5af0e038375;p=tpg%2Fopendispense2.git diff --git a/src/server/handler_door.c b/src/server/handler_door.c index 62fb8f9..190769a 100644 --- a/src/server/handler_door.c +++ b/src/server/handler_door.c @@ -2,7 +2,7 @@ * OpenDispense 2 * UCC (University [of WA] Computer Club) Electronic Accounting System * - * handler_doror.c - Door Relay code + * handler_door.c - Door Relay code * * This file is licenced under the 3-clause BSD Licence. See the file * COPYING for full details. @@ -18,7 +18,6 @@ #include #include #include -#include #include #define DOOR_UNLOCKED_DELAY 5 // Time in seconds before the door re-locks @@ -29,6 +28,8 @@ int Door_InitHandler(); int Door_CanDispense(int User, int Item); int Door_DoDispense(int User, int Item); + int writes(int fd, const char *str); +char *ReadStatus(int FD); // === GLOBALS === tHandler gDoor_Handler = { @@ -37,7 +38,7 @@ tHandler gDoor_Handler = { Door_CanDispense, Door_DoDispense }; -char *gsDoor_SerialPort = "/dev/ttyS3"; +char *gsDoor_SerialPort; // Set from config in main.c // == CODE === int Door_InitHandler(void) @@ -93,22 +94,33 @@ int Door_DoDispense(int User, int Item) return 1; } - door_serial_handle = InitSerial(gsDoor_SerialPort, 9600); - - if( write(door_serial_handle, "ATH1\n", 5) != 5 ) { - fprintf(stderr, "Unable to open door (sending ATH1)\n"); - perror("Sending ATH1"); + door_serial_handle = InitSerial(gsDoor_SerialPort, 115200); + if(door_serial_handle < 0) { + fprintf(stderr, "Unable to open door serial '%s'\n", gsDoor_SerialPort); + perror("Opening door port"); return -1; } - - // Wait before re-locking - sleep(DOOR_UNLOCKED_DELAY); - if( write(door_serial_handle, "ATH0\n", 5) != 5 ) { - fprintf(stderr, "Oh, hell! Door not re-locking, big error (sending ATH0 failed)\n"); - perror("Sending ATH0"); + // Disable local echo + { + struct termios info; + tcgetattr(door_serial_handle, &info); + info.c_cflag &= ~CLOCAL; + tcsetattr(door_serial_handle, TCSANOW, &info); + } + +// flush(door_serial_handle); + + writes(door_serial_handle, "4;"); + +#if 0 + char *status = ReadStatus(door_serial_handle); + if( !status ) return -1; + if( strcmp(status, "Opening door") != 0 ) { + fprintf(stderr, "Unknown/unexpected door status '%s'\n", status); return -1; } +#endif close(door_serial_handle); @@ -119,3 +131,35 @@ int Door_DoDispense(int User, int Item) return 0; } +int writes(int fd, const char *str) +{ + int len = strlen(str); + + if( len != write(fd, str, len) ) + { + return 1; + } + return 0; +} + +char *ReadStatus(int FD) +{ + char tmpbuf[32]; + int len; + len = read(FD, tmpbuf, sizeof(tmpbuf)-1); + tmpbuf[len] = 0; + char *msg = strchr(tmpbuf, ','); + if( !msg ) { + fprintf(stderr, "Door returned malformed data (no ',')\n"); + return NULL; + } + msg ++; + char *end = strchr(tmpbuf, ';'); + if( !end ) { + fprintf(stderr, "Door returned malformed data (no ';')\n"); + return NULL; + } + *end = '\0'; + + return strdup(msg); +}