summaryrefslogtreecommitdiffstats
path: root/net_rx.c
blob: 7bcc01ea7c1233c3345cb13f5e30eb1a2dd7cc6d (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <syslog.h>

#include "util.h"
#include "sia.h"

extern int new_block (int fd, SIA_Block *b, int log, const char *email, const char *state_file, const char *mqtt_host);
extern void periodic_task (void);
extern void dump_state (const char *);

static int usage (const char *name)
{
  fprintf (stderr, "Usage:\n");
  fprintf (stderr, "%s [ -l ] [ -p listen_port ] [ -e email address] [ -s state_file ] [ -m mqtt_host ]\n\n", name);
  fprintf (stderr, "listen_port defaults to 10002\n");

  return 1;
}


#define TICK 5

int main (int argc, char *argv[])
{
  int port = 10002;
  unsigned opt;
  int fd;
  int afd = -1;
  SIA_Block b;
  struct timeval tv = {0};
  int log = 0;

  unsigned char buf[SIA_MAX_BLOCK_LENGTH];
  unsigned ptr = 0;

  const char *email = NULL;
  const char *state_file = NULL;
  const char *mqtt_host = NULL;


  while ((opt = getopt (argc, argv, "p:le:s:m:")) != -1) {
    switch (opt) {
    case 'p':
      port = atoi (optarg);
      break;

    case 'l':
      log++;
      break;

    case 'e':
      email = optarg;
      break;

    case 's':
      state_file = optarg;
      break;

    case 'm':
      mqtt_host = optarg;
      break;

    default: /* '?' */
      return usage (argv[0]);
    }
  }

  if (log)
    openlog ("net_rx", 0, LOG_USER);

  fd = open_tcp_server (port);

  if (fd < 0) {
    perror ("open socket");
    return -1;
  }

  set_blocking (fd);

  if (state_file)
    dump_state (state_file);

  // Epically budget TCP server

  tv.tv_sec = TICK;

  for (;;) {
    fd_set rfds;
    int rc;

    FD_ZERO (&rfds);
    FD_SET (fd, &rfds);

    if (afd > 0)
      FD_SET (afd, &rfds);

    rc = select (afd > fd ? afd + 1 : fd + 1, &rfds, NULL, NULL, &tv);

    if (!rc) periodic_task();

    if (!tv.tv_sec && !tv.tv_usec)
      tv.tv_sec = TICK;

    if (FD_ISSET (fd, &rfds)) {
      if (afd > 0) close (afd);

      afd = accept (fd, NULL, NULL);
      ptr = 0;
    }

    if ((afd > 0) && (FD_ISSET (afd, &rfds))) {

      if (read (afd, &buf[ptr], 1) != 1) {
        close (afd);
        afd = -1;
        ptr = 0;
        continue;
      }

      ptr++;


      switch (sia_parse (buf, ptr, &b)) {
      case -1: /*Parse errror, form here is top drop connexion*/
        close (afd);
        afd = -1;
        break;

      case 0: /*More data needed*/
        break;

      default: /*Valid block */
        new_block (afd, &b, log, email, state_file, mqtt_host);
        ptr = 0;
        break;
      }

      if (ptr == SIA_MAX_BLOCK_LENGTH) {
        close (afd);
        afd = -1;
      }

    }

  }
}