#include #include #include #include #include #include #include #include "util.h" #include "sia.h" #include "arm.h" int auth_cmd_with_ack (int fd, unsigned char fn, const void *data, size_t len, SIA_Block *b) { if (sia_login (fd, b)) { printf ("Login failed\n"); return -1; } b->reverse_channel = 0; b->request_ack = 1; b->function = fn; memcpy (b->data, data, len); b->length = len; if (fd_drain (fd)) return -1; if (sia_write (fd, b) != 1) return -1; if (sia_rx_with_timeout_ignore_log (fd, b, SIA_TIMEOUT)) return -1; return 0; } static int get_thing (int fd, const char *thing, unsigned char *buf) { SIA_Block b = {0}; unsigned len = strlen (thing); memset (buf, 0, SIA_MAX_DATA_LENGTH); if (auth_cmd_with_ack (fd, SIA_FN_CONTROL, thing, len, &b)) return -1; if (b.length < len + 2) return -1; if (memcmp (b.data, thing, len)) return -1; if (b.data[len] != '*') return -1; memcpy (buf, &b.data[len + 1], b.length - 3); return b.length - 3; } static const char *armed_to_str (int i) { switch (i) { case '0': return "unset"; case '1': return "SET"; case '2': return "Part SET"; } return "?"; } static const char *state_to_str (int i) { switch (i) { case '0': return "normal"; case '1': return "ALARM"; case '2': return "reset req."; } return "?"; } static const char *ready_to_str (int i) { switch (i) { case '0': return "unset"; case '1': return "set"; case '2': return "part set"; case '3': return "ready to set"; case '4': return "time locked"; } return "?"; } int show_state (int fd, int zone) { 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]; int s = 0; i = get_thing (fd, "SA", armed); if (i < 0) { printf ("Get armed failed\n"); return -1; } if (i < l) l = i; i = get_thing (fd, "SA91", state); if (i < 0) { printf ("Get state failed\n"); return -1; } if (i < l) l = i; i = get_thing (fd, "SA92", ready); 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"); if (zone > 0) { s = zone; l = zone + 1; } for (i = s; 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])); printf ("\n"); if (zone < 0) zone = 0; return armed[zone] - '0'; }