/* ss-core/sshw.h * * Interface to ss-hw. * * Copyright (C) 2003, 2004 * Andy Goth * * This code is available under the GNU General Public License; see COPYING. */ #include #include #include #include #include "evlist/evlist.h" #include "prjlibs/vec.h" #include "ss-core/core.h" #include "ss-core/sshw.h" typedef struct sshw { core_t* core; } sshw_t; static int sshw_cleanup (device_t* dev); static int sshw_incoming(device_t* dev); int sshw_init(device_t* dev, core_t* core) { sshw_t* sshw = malloc(sizeof(sshw_t)); if (sshw == NULL) return -1; sshw->core = core; dev->sw_state = sshw ; dev->sw_cleanup = sshw_cleanup ; dev->sw_incoming = sshw_incoming; return 0; } int sshw_send_inv_delay(device_t* dev, int delay) { return device_sendf(dev, "INV-DELAY %d\n", delay); } int sshw_send_rf_field (device_t* dev, int enable) { return device_sendf(dev, "RF-FIELD %s\n", enable ? "ON" : "OFF"); } static int sshw_cleanup(device_t* dev) { free(dev->sw_state); dev->sw_state = NULL; dev->sw_cleanup = NULL; dev->sw_incoming = NULL; return 0; } static int sshw_incoming(device_t* dev) { sshw_t* sshw = dev->sw_state; char* buf = (char*)VEC_ELTS(byte_t, &dev->buf_in); char* data, *next; again: /* See if we have a complete packet. */ while (dev->buf_cursor < VEC_LEN(byte_t, &dev->buf_in)) { if (buf[dev->buf_cursor] == '\n') goto found; ++dev->buf_cursor; } /* Nope, no more data. */ return 0; found: /* Yes, complete packet. */ data = buf; while (*data != ' ' && *data != '\n') ++data; /* Find space. */ while (*data == ' ' ) ++data; /* Skip spaces. */ /* What kind? */ if (strncasecmp(buf, "DOORS", 5) == 0) { int a = -1, b = -1; sscanf(data, "%d%d", &a, &b); if ((a != 0 && a != 1) || (b != 0 && b != 1)) goto bad_packet; core_doors(sshw->core, a, b); } else if (strncasecmp(buf, "PRESSURE", 8) == 0) { int a = -1, b = -1, c = -1, d = -1; if (sscanf(data, "%d%d%d%d", &a, &b, &c, &d) != 4) goto bad_packet; if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255) goto bad_packet; core_pressure(sshw->core, a, b, c, d); } else if (strncasecmp(buf, "TAG-ADD", 7) == 0) { uint64_t uid; uint32_t type; if (sscanf(data, "%llx%x", &uid, &type) != 2) goto bad_packet; core_tag_add(sshw->core, uid, type); } else if (strncasecmp(buf, "TAG-DEL", 7) == 0) { uint64_t uid; if (sscanf(data, "%llx", &uid) != 1) goto bad_packet; core_tag_del(sshw->core, uid); } else { bad_packet: fprintf(stderr, "Invalid packet received from %s (%s): `%.*s'\n", dev->name, dev->path, dev->buf_cursor, buf); /* Fall through. */ } /* Discard consumed data. */ VEC_SHIFTN(byte_t, &dev->buf_in, dev->buf_cursor + 1); dev->buf_cursor = 0; /* Maybe we have another packet...? */ goto again; } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */