summaryrefslogtreecommitdiff
path: root/client/main.c
blob: 8ea731bc234ad8b825c97e309647ea711d5e4d3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#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;
}