+char *gsDoor_SerialPort; // Set from config in main.c
+sem_t gDoor_UnlockSemaphore;
+pthread_t gDoor_LockThread;
+bool gbDoor_LockThreadStarted;
+
+// === CODE ===
+void* Door_Lock(void* Unused __attribute__((unused)))
+{
+ while(1)
+ {
+ sem_wait(&gDoor_UnlockSemaphore);
+
+ int door_serial_handle = InitSerial(gsDoor_SerialPort, 9600);
+ if(door_serial_handle < 0)
+ {
+ fprintf(stderr, "Unable to open door serial '%s'\n", gsDoor_SerialPort);
+ perror("Opening door port");
+ }
+
+ // Disable local echo
+ {
+ struct termios info;
+ tcgetattr(door_serial_handle, &info);
+ info.c_cflag &= ~CLOCAL;
+ tcsetattr(door_serial_handle, TCSANOW, &info);
+ }
+
+ if(write(door_serial_handle, "\xff\x01\x01", 3) != 3) // Relay ON
+ {
+ fprintf(stderr, "Failed to write Relay ON (unlock) command, errstr: %s", strerror(errno));
+ }
+
+ sleep(DOOR_UNLOCKED_DELAY);
+
+ if(write(door_serial_handle, "\xff\x01\x00", 3) != 3) // Relay OFF
+ {
+ fprintf(stderr, "Failed to write Relay OFF (lock) command, errstr: %s", strerror(errno));
+ }
+
+ close(door_serial_handle);
+ }
+}