From 62bc1af6c6a1201db551e1ec523e757415464fd5 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 25 Oct 2020 13:26:29 +0000 Subject: tidy up, make less awful --- arm.c | 263 +++++++++++++++++++++++++----------------------------------------- 1 file changed, 101 insertions(+), 162 deletions(-) (limited to 'arm.c') diff --git a/arm.c b/arm.c index 3e6510b..9f994cc 100644 --- a/arm.c +++ b/arm.c @@ -2,233 +2,172 @@ #include #include #include -#include - +#include #include #include -#include -#include -#include - +#include "util.h" +#include "sia.h" +#include "arm.h" -int happy = 0; - -int valid_fn (unsigned char fn) +int auth_cmd_with_ack (int fd, unsigned char fn, const void *data, size_t len, SIA_Block *b) { - switch (fn) { - case 0x30: - case 0x31: - case 0x32: - case 0x33: - case 0x34: - case 0x35: - case 0x36: - case 0x37: - case 0x38: - case 0x08: - case 0x39: - case 0x09: - case 0x43: - case 0x45: - case 0x4E: - case 0x4F: - case 0x50: - case 0x40: - case 0x3F: - case 0x23: - case 0x26: - case 0x41: - case 0x58: - case 0x4C: - case 0x56: - case 0x76: - case 0x49: - return 1; - } - return 0; -} - -int ackable (unsigned char fn) -{ - switch (fn) { - - - case 0x09: - case 0x39: - case 0x08: - case 0x38: - case 0x40: - case 0x43: - case 0x58: - return 0; + if (sia_login (fd, b)) { + printf ("Login failed\n"); + return -1; } - return 1; -} + b->reverse_channel = 0; + b->request_ack = 1; + b->function = fn; + memcpy (b->data, data, len); + b->length = len; -void ack (int fd) -{ - char buf[3]; + if (fd_drain (fd)) + return -1; - buf[0] = 0; - buf[1] = 0x38; - buf[2] = 0xff ^ buf[0] ^ buf[1]; + if (sia_write (fd, b) != 1) + return -1; - write (fd, buf, sizeof (buf)); + if (sia_rx_with_timeout_ignore_log (fd, b, SIA_TIMEOUT)) + return -1; + return 0; } -int rx_pkt (int fd, unsigned char *pkt, unsigned len) +static int get_thing (int fd, const char *thing, unsigned char *buf) { - unsigned char parity = 0xff; - unsigned i; + SIA_Block b = {0}; + unsigned len = strlen (thing); - for (i = 0; i < len; ++i) - parity ^= pkt[i]; + memset (buf, 0, SIA_MAX_DATA_LENGTH); - if (parity) return -1; + if (auth_cmd_with_ack (fd, SIA_FN_CONTROL, thing, len, &b)) + return -1; - happy++; + if (b.length < len + 2) + return -1; - for (i = 2; i < len - 1; ++i) { - if ((pkt[i] > 31) && (pkt[i] < 127)) - putchar (pkt[i]); - else - printf ("\\x%02x", pkt[i]); - } + if (memcmp (b.data, thing, len)) + return -1; - printf ("\n"); + if (b.data[len] != '*') + return -1; - if ((pkt[0] & 0x40) && ackable (pkt[1])) ack (fd); + memcpy (buf, &b.data[len + 1], b.length - 3); - return 0; -} + return b.length - 3; +} -int rx (int fd) +static const char *armed_to_str (int i) { - static unsigned char pkt[67]; - static unsigned ptr; - unsigned char byte; - unsigned len; - - if (read (fd, &byte, 1) != 1) { - ptr = 0; - return -1; - } + switch (i) { + case '0': + return "unset"; - pkt[ptr++] = byte; + case '1': + return "SET"; - if (ptr > 2 && !valid_fn (pkt[1])) { - ptr = 0; - return 0; + case '2': + return "Part SET"; } - len = pkt[0] & 0x3f; - - if (ptr == (len + 3)) { - ptr = 0; - return rx_pkt (fd, pkt, len + 3); - } - - return 0; + return "?"; } -int transact (int fd, int ack, unsigned char fn, void *data, unsigned len, int timeout) +static const char *state_to_str (int i) { - struct timeval tv = {0}; - int rc; - fd_set rfds; - unsigned char buf[67], parity; - unsigned i; - - happy = 0; - - tv.tv_sec = timeout; - - buf[0] = len; - - if (ack) buf[0] |= 0x40; + switch (i) { + case '0': + return "normal"; - buf[1] = fn; + case '1': + return "ALARM"; - memcpy (&buf[2], data, len); - - parity = 0xff; - - for (i = 0; i < (len + 2); ++i) - parity ^= buf[i]; + case '2': + return "reset req."; + } - buf[i] = parity; - i++; + return "?"; +} - write (fd, buf, i); +static const char *ready_to_str (int i) +{ + switch (i) { + case '0': + return "unset"; - fwrite (data, 1, len, stdout); + case '1': + return "set"; - printf (" => "); - fflush (stdout); + case '2': + return "part set"; + case '3': + return "ready to set"; + case '4': + return "time locked"; + } - while (!happy) { + return "?"; +} - FD_ZERO (&rfds); - FD_SET (fd, &rfds); - rc = select (fd + 1, &rfds, NULL, NULL, &tv); +int show_state (int fd) +{ + int i, l = SIA_MAX_DATA_LENGTH; + unsigned char armed[SIA_MAX_DATA_LENGTH]; + unsigned char state[SIA_MAX_DATA_LENGTH]; + unsigned char ready[SIA_MAX_DATA_LENGTH]; - if (!rc) break; - rx (fd); + i = get_thing (fd, "SA", armed); + if (i < 0) { + printf ("Get armed failed\n"); + return -1; } - return happy; -} + if (i < l) l = i; + i = get_thing (fd, "SA91", state); -int main (int argc, char *argv[]) -{ - struct sockaddr_in sin; - int fd; - - unsigned char buf[128]; + if (i < 0) { + printf ("Get state failed\n"); + return -1; + } - fd = socket (PF_INET, SOCK_STREAM, 0); + if (i < l) l = i; - sin.sin_family = AF_INET; - sin.sin_port = htons (10005); - inet_aton ("10.32.52.66", &sin.sin_addr); + i = get_thing (fd, "SA92", ready); - connect (fd, (struct sockaddr *)&sin, sizeof (sin)); + if (i < 0) { + printf ("Get ready failed\n"); + return -1; + } + if (i < l) l = i; + printf ("%-6s | %-15s | %-15s | %-15s\n", "Zone", "Armed", "State", "Ready"); + printf ("-------+-----------------+-----------------+----------------\n"); - transact (fd, 1, 0x3f, "543210", 6, 4); - transact (fd, 1, 0x43, "SA", 2, 4); + for (i = 0; i < l; ++i) + printf ("%6d | %-15s | %-15s | %-15s\n", i + 1, armed_to_str (armed[i]), state_to_str (state[i]), ready_to_str (ready[i])); - transact (fd, 1, 0x3f, "543210", 6, 4); - transact (fd, 1, 0x43, "SA91", 4, 4); + printf ("\n"); + return 0; +} - transact (fd, 1, 0x3f, "543210", 6, 4); - transact (fd, 1, 0x43, "SA92", 4, 4); - if (argc == 2) { - sprintf (buf, "SA1*%s", argv[1]); - transact (fd, 1, 0x3f, "543210", 6, 4); - transact (fd, 1, 0x43, buf, strlen (buf), 4); - } - close (fd); -} -- cgit v1.2.3