Server - Moved to new door control (via 115200 USB serial)
[tpg/opendispense2.git] / src / server / handler_door.c
index 978c184..190769a 100644 (file)
@@ -2,19 +2,25 @@
  * 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.
  */
+#define        DEBUG   1
+#define        USE_POPEN       0
+
 #include "common.h"
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <signal.h>
+#include <unistd.h>
+#include <pty.h>
 
-#define DOOR_UNLOCKED_DELAY    10      // 10 seconds before it re-locks
+#define DOOR_UNLOCKED_DELAY    5       // Time in seconds before the door re-locks
 
 // === IMPORTS ===
 
@@ -22,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 = {
@@ -30,16 +38,11 @@ tHandler    gDoor_Handler = {
        Door_CanDispense,
        Door_DoDispense
 };
-char   *gsDoor_Password;
-char   *gsDoor_Command;
-// int giDoor_SerialFD;
+char   *gsDoor_SerialPort;     // Set from config in main.c
 
 // == CODE ===
 int Door_InitHandler(void)
-{              
-       gsDoor_Command = malloc(sizeof("llogin door -w ")+strlen(gsDoor_Password));
-       sprintf(gsDoor_Command, "llogin door -w %s", gsDoor_Password);
-       
+{
        return 0;
 }
 
@@ -47,11 +50,23 @@ int Door_InitHandler(void)
  */
 int Door_CanDispense(int User, int Item)
 {
+       #if DEBUG
+       printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
+       #endif
        // Sanity please
-       if( Item == 0 ) return -1;
+       if( Item != 0 ) return -1;
        
        if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
+       {
+               #if DEBUG
+               printf("Door_CanDispense: User %i not in door\n", User);
+               #endif
                return 1;
+       }
+       
+       #if DEBUG
+       printf("Door_CanDispense: User %i can open the door\n", User);
+       #endif
        
        return 0;
 }
@@ -61,28 +76,90 @@ int Door_CanDispense(int User, int Item)
  */
 int Door_DoDispense(int User, int Item)
 {
-       FILE    *pipe;
+        int    door_serial_handle;
+       
+       #if DEBUG
+       printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
+       #endif
+       
        // Sanity please
        if( Item != 0 ) return -1;
        
        // Check if user is in door
        if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
+       {
+               #if DEBUG
+               printf("Door_CanDispense: User %i not in door\n", User);
+               #endif
                return 1;
+       }
        
-       // llogin or other
-       //pipe = popen(gsDoor_Command, "w");
-       pipe = popen("llogin door -w -", "w");
-       if( !pipe || pipe == (void*)-1 )
+       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;
+       }
+
+       // 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);
        
-       fputs(gsDoor_Password, pipe);   fputs("\n", pipe);
-       fputs("ATH1\n", pipe);
-       sleep(DOOR_UNLOCKED_DELAY);
-       fputs("ATH0\n", pipe);
-       
-       pclose(pipe);
+       #if DEBUG
+       printf("Door_DoDispense: User %i opened door\n", User);
+       #endif
 
        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);
+}

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