Changed door code to use direct serial instead of llogin
[tpg/opendispense2.git] / src / server / handler_door.c
index 9942526..62fb8f9 100644 (file)
@@ -7,16 +7,21 @@
  * 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 <sys/wait.h>
+#include <pty.h>
 
-#define        DEBUG   1
-
-#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 ===
 
@@ -32,12 +37,11 @@ tHandler    gDoor_Handler = {
        Door_CanDispense,
        Door_DoDispense
 };
-char   *gsDoor_Password = "";
-// int giDoor_SerialFD;
+char   *gsDoor_SerialPort = "/dev/ttyS3";
 
 // == CODE ===
 int Door_InitHandler(void)
-{      
+{
        return 0;
 }
 
@@ -71,8 +75,7 @@ int Door_CanDispense(int User, int Item)
  */
 int Door_DoDispense(int User, int Item)
 {
-       FILE    *pipe;
-       char    buf[512];       // Buffer flush location - the sewer :)
+        int    door_serial_handle;
        
        #if DEBUG
        printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
@@ -90,33 +93,24 @@ int Door_DoDispense(int User, int Item)
                return 1;
        }
        
-       // llogin or other
-       pipe = popen("llogin door -w -", "w");
-       if( !pipe || pipe == (void*)-1 ) {
-               #if DEBUG
-               printf("Door_DoDispense: llogin failure\n");
-               #endif
+       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");
                return -1;
        }
-       if( fread(buf, 512, 1, pipe) == 0 )     return -1;      // Flush!
-       
-       // Send password
-       fputs(gsDoor_Password, pipe);
-       fputs("\n", pipe);
-       if( fread(buf, 512, 1, pipe) == 0 )     return -1;      // Flush!
-       
-       // ATH1 - Unlock door
-       fputs("ATH1\n", pipe);
-       if( fread(buf, 512, 1, pipe) == 0 )     return -1;      // Flush!
        
        // Wait before re-locking
        sleep(DOOR_UNLOCKED_DELAY);
 
-       // Re-lock the door
-       fputs("ATH0\n", pipe);
-       if( fread(buf, 512, 1, pipe) == 0 )     return -1;      // Flush!
-       
-       pclose(pipe);
+       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");
+               return -1;
+       }
+
+       close(door_serial_handle);
        
        #if DEBUG
        printf("Door_DoDispense: User %i opened door\n", User);
@@ -125,4 +119,3 @@ int Door_DoDispense(int User, int Item)
        return 0;
 }
 
-

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