/* ss-hw/user.c * * User interface functions. * * 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-hw/user.h" typedef struct user { device_t* rfid; } user_t; static int user_cleanup (device_t* dev); static int user_incoming(device_t* dev); int user_init(device_t* dev, device_t* rfid) { user_t* user = malloc(sizeof(user_t)); if (user == NULL) return -1; user->rfid = rfid; dev->sw_state = user ; dev->sw_cleanup = user_cleanup ; dev->sw_incoming = user_incoming; return 0; } int user_send_pressure(device_t* dev, int a, int b, int c, int d) { return device_sendf(dev, "PRESSURE %d %d %d %d\n", a, b, c, d); } int user_send_doors(device_t* dev, int a, int b) { return device_sendf(dev, "DOORS %d %d\n", a, b); } int user_send_tag_add(device_t* dev, uint64_t uid, uint32_t type) { return device_sendf(dev, "TAG-ADD %016llx %08x\n", uid, type); } int user_send_tag_del(device_t* dev, uint64_t uid) { return device_sendf(dev, "TAG-DEL %016llx\n", uid); } static int user_cleanup(device_t* dev) { free(dev->sw_state); dev->sw_state = NULL; dev->sw_cleanup = NULL; dev->sw_incoming = NULL; return 0; } static int user_incoming(device_t* dev) { user_t* user = 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: /* We have a winnar. :^) */ data = buf; while (*data != ' ' && *data != '\n') ++data; while (*data == ' ' ) ++data; /* What kind of packet? */ if (strncasecmp(buf, "INV-DELAY", 9) == 0) { int delay = strtol(data, &next, 10); if (*data == '\n' || *next != '\n') goto bad_packet; rfid_inv_delay(user->rfid, atoi(data)); } else if (strncasecmp(buf, "RF-FIELD", 8) == 0) { int value; if (strncasecmp(data, "ON\n" , 3) == 0) value = 1; else if (strncasecmp(data, "OFF\n", 4) == 0) value = 0; else goto bad_packet; rfid_rf_field(user->rfid, 1); } 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: */