summaryrefslogtreecommitdiffstats
path: root/net_arm.c
blob: e2cd6db2c9a5c29b36ea64d00dd10cd5f2fd0b25 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "util.h"
#include "sia.h"
#include "arm.h"




static int usage (const char *name)
{
  fprintf (stderr, "Usage:\n");
  fprintf (stderr, "%s -h host [-p port] [-z zone] [-U|-S|-P|-R|-B|-F|-T|-A|-D]\n", name);
  fprintf (stderr, "\n");
  fprintf (stderr, "    -U Unset     system/zone\n");
  fprintf (stderr, "    -S Set       system/zone\n");
  fprintf (stderr, "    -P Part set  system/zone\n");
  fprintf (stderr, "    -R Reset     system/zone\n");
  fprintf (stderr, "    -B aBort set system/zone\n");
  fprintf (stderr, "    -F Force set system/zone\n");
  fprintf (stderr, "    -T sTate     system/zone\n");
  fprintf (stderr, "    -A Alarm     system/zone\n");
  fprintf (stderr, "    -D reaDy     system/zone\n");


  return -1;
}


int main (int argc, char *argv[])
{
  SIA_Block b;
  char cmd[SIA_MAX_DATA_LENGTH];
  unsigned opt;
  int zone = -1;
  int action = -1;
  const char *host = NULL;
  unsigned port = 10005;
  int fd;

  while ((opt = getopt (argc, argv, "h:p:z:USPRBFTAD")) != -1) {
    switch (opt) {
    case 'h':
      host = optarg;
      break;

    case 'p':
      port = atoi (optarg);
      break;

    case 'z':
      zone = atoi (optarg);
      break;

    case 'U':
      action = 0;
      break;

    case 'S':
      action = 1;
      break;

    case 'P':
      action = 2;
      break;

    case 'R':
      action = 3;
      break;

    case 'B':
      action = 4;
      break;

    case 'F':
      action = 5;
      break;

    case 'T':
      action = 6;
      break;

    case 'A':
      action = 7;
      break;

    case 'D':
      action = 8;
      break;

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

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

  fd = open_tcp_client (host, port);

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



  if (action == -1) {
    if (show_state (fd))
      return -1;

    return 0;
  }


  if (zone != -1)
    sprintf (cmd, "SA%d*%d", zone, action);
  else
    sprintf (cmd, "SA*%d", action);


  if (auth_cmd_with_ack (fd, SIA_FN_CONTROL, cmd, strlen (cmd), &b)) {
    printf ("Command failed\n");
    return -1;
  }

  switch (b.function) {
  case SIA_FN_ACKNOLEDGE:
    printf ("Panel acks command\n");
    return 0;

  case SIA_FN_REJECT:
    printf ("Panel nacks command\n");
    /*fall through */
  }

  return -1;
}