From af2339434d35ee2a40eb99af84e8571dc59dfc62 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Wed, 23 Oct 2019 21:25:26 +1100 Subject: Initial commit --- client/Makefile | 23 ++++++++++++++++++++++ client/main.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 client/Makefile create mode 100644 client/main.c (limited to 'client') diff --git a/client/Makefile b/client/Makefile new file mode 100644 index 0000000..f2b6ca3 --- /dev/null +++ b/client/Makefile @@ -0,0 +1,23 @@ +TARGET = $(shell basename $(CURDIR)) + +SRCS = $(wildcard *.c) +OBJS = $(SRCS:.c=.o) + +CC = gcc + +all: build + +build: $(TARGET) + +clean: + rm -f $(OBJS) + rm -f $(TARGET) + +run: $(TARGET) + $(realpath $(TARGET)) + +$(TARGET): $(OBJS) + $(CC) -o $(TARGET) $(OBJS) + +.c.o: + $(CC) -c -o $*.o $^ diff --git a/client/main.c b/client/main.c new file mode 100644 index 0000000..8ea731b --- /dev/null +++ b/client/main.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define PORT 2319 + +int main(int argc, char **argv) { + int ret; + char c, last; + ssize_t in; + + int fd; + int sfd; + + char buf[256]; + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT); + addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); + + sfd = socket(AF_INET, SOCK_DGRAM, 0); + if(sfd < 0) { + perror("Failed to open socket"); + exit(EXIT_FAILURE); + } + + ret = bind(sfd, (struct sockaddr*) &addr, sizeof(addr)); + if(ret < 0) { + perror("Failed to bind socket"); + exit(EXIT_FAILURE); + } + + for(;;) { + in = read(sfd, buf, sizeof(buf) - 1); + if(in < 0) { + perror("Failed to read from serial port"); + exit(EXIT_FAILURE); + } + buf[in] = 0; + + ret = sscanf(buf, "%c->%c\n", &last, &c); + if(ret < 2) + continue; + + if(last == 'L' && c == 'H') + system("/home/jake/.bin/scripts/reed open"); + + if(last == 'H' && c == 'L') + system("/home/jake/.bin/scripts/reed close"); + } + + return 0; +} -- cgit v1.3