summaryrefslogtreecommitdiffstats
path: root/serial_rx.c
blob: d481417df05a0a86ba0228a5a8d36638d19b3aa0 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>

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





void msg (char *account, char *event, char *ascii)
{
  printf ("%s %64s %s\n", account, event, ascii);
}


static int new_block (int fd, SIA_Block *b)
{
  static int have_ascii_messages = 0; /*SIA level 3 doesn't have ascii, SIA level 4 does */

  static char account[SIA_MAX_DATA_LENGTH + 1];
  static char event[SIA_MAX_DATA_LENGTH + 1];
  static char ascii[SIA_MAX_DATA_LENGTH + 1];

  unsigned len = sia_data_length (b);

  if (sia_ack_if_needed (fd, b))
    return -1;

  switch (b->function) {
  case SIA_FN_ACCOUNT_ID:
    memcpy (account, b->data, len);
    account[len] = 0;
    break;

  case SIA_FN_NEW_EVENT:
  case SIA_FN_OLD_EVENT:
    memcpy (event, b->data, len);
    event[len] = 0;

    if (!have_ascii_messages) msg (account, event, "");

    break;

  case SIA_FN_ASCII:
    have_ascii_messages = 1;
    memcpy (ascii, b->data, len);
    ascii[len] = 0;

    msg (account, event, ascii);
    break;
  }

  return 0;
}


static int usage (const char *name)
{
  fprintf (stderr, "Usage:\n");
  fprintf (stderr, "%s [ -b baud ] -p serial_device\n\n", name);
  fprintf (stderr, "baud defaults to 9600\n");

  return 1;
}


int main (int argc, char *argv[])
{
  const char *port;
  int baud = 9600;
  unsigned opt;
  int fd;
  SIA_Block b;
  ssize_t red;

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


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

    case 'b':
      baud = atoi (optarg);
      break;

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



  if (!port)
    return usage (argv[0]);

  fd = open_tty (port, baud);

  set_blocking (fd);

  for (;;) {
    red = read (fd, &buf[ptr], 1);

    if ((red < 0) || (red > 1)) {
      perror ("read");
      return 1;
    }

    if (!red) continue;

    ptr++;

    switch (sia_parse (buf, ptr, &b)) {
    case -1: /*Parse errror, flush and carry on */
      tcflush (fd, TCIFLUSH);
      ptr = 0;
      break;

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

    default: /*Valid block */
      new_block (fd, &b);
      ptr = 0;
      break;
    }

    if (ptr == SIA_MAX_BLOCK_LENGTH) {
      tcflush (fd, TCIFLUSH);
      ptr = 0;
    }

  }

}