#include <termios.h>
#include <sys/ioctl.h>
-#include <ev.h>
#include <uFCoder.h>
#include "mt_rfid.h"
+#include "ev_signal_exit.h"
-int set_nonblock(int fd)
+static int set_nonblock(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1) {
return 0;
}
-int set_rts(int fd, int level)
+static int set_rts(int fd, int level)
{
int uart_status;
return 0;
}
-void set_baud_rate(int fd, int br)
+static void set_baud_rate(int fd, int br) //TODO add some checking
{
struct termios options;
tcsetattr(fd, TCSANOW, &options);
}
-int tty_open(const char *port, int baud_rate)
+static int tty_open(const char *port, int baud_rate)
{
int fd = open(port, O_RDONLY | O_NOCTTY);
return fd;
}
-typedef struct ev_io_ufr {
- ev_io w;
- //lws_context *context;
- //ufr_session_data *session_data;
- char uid_data[24];
- char *uid;
- int fd;
-} ev_io_ufr;
-
-static void sigint_cb(EV_P_ ev_signal *w, int revents)
-{
- ev_break(EV_A_ EVBREAK_ALL);
-}
+// really simple JSON helpers
+#define JSON_START() dprintf(fd,"{")
+#define JSON_NUM(NAME) dprintf(fd,"\"" #NAME "\": %d", NAME)
+#define JSON_NEXT() dprintf(fd,",")
+#define JSON_STR(NAME) dprintf(fd,"\"" #NAME "\": \"%s\"", NAME)
+#define JSON_END() dprintf(fd,"}\n")
+
+// print complete json
+#define JSON_PRINT() do { \
+ JSON_START(); \
+ JSON_STR(type); \
+ JSON_NEXT(); \
+ JSON_NUM(card_type); \
+ JSON_NEXT(); \
+ JSON_NUM(sak); \
+ JSON_NEXT(); \
+ JSON_NUM(size); \
+ JSON_NEXT(); \
+ JSON_STR(uid); \
+ JSON_END(); \
+} while (0)
static void ufr_read(char *uid, int fd)
{
+ static char *type = "rfid";
+
UFR_STATUS status;
uint8_t card_type;
uint8_t sak; //select acknowledge
uint8_t uid_bytes[10]; //uid as bytes
uint8_t size;
- char *type = "rfid";
status = GetDlogicCardType(&card_type);
if (status != UFR_OK) {
return;
}
- JSON_START();
- JSON_STR(type);
- JSON_NEXT();
- JSON_NUM(card_type);
- JSON_NEXT();
- JSON_NUM(sak);
- JSON_NEXT();
- JSON_NUM(size);
- JSON_NEXT();
- JSON_STR(uid);
- JSON_END();
+ JSON_PRINT();
#ifdef UFR_BEEP
ReaderUISignal(0, 1); // no light, one beep
#endif
-
- /*lws_callback_on_writable_all_protocol(self->context,
- &protocols[PROTOCOL_DUMB_INCREMENT]);*/
}
static void ufr_cb(EV_P_ ev_io *w_, int revents)
ev_io_ufr *w = (ev_io_ufr *)w_;
char uid;
read(w->w.fd, &uid, 1);
- *(w->uid)++ = uid;
+ *(w->uid++) = uid;
if (uid == ASYNC_SUFFIX) {
- //*(w->uid - 1) = 0;
+ //*(w->uid - 1) = 0; // no need if ASYNC_SUFFIX is 0
w->uid = w->uid_data;
ufr_read(w->uid, w->fd);
}
-
- //for one-shot events, one must manually stop the watcher with its corresponding stop function.
- //ev_io_stop (EV_A_ w);
-
- //this causes all nested ev_run's to stop iterating
- //ev_break (EV_A_ EVBREAK_ALL);
}
-ev_io_ufr ufr_watcher;
-ev_signal int_watcher, term_watcher;
-
-int libev_run(int fd)
-{
- struct ev_loop *loop = EV_DEFAULT;
-
- ufr_watcher.uid = ufr_watcher.uid_data;
- ufr_watcher.fd = 1;
- ev_io_init(&(ufr_watcher.w), ufr_cb, fd, EV_READ);
- ev_io_start(loop, (ev_io *)&ufr_watcher);
-
- ev_signal_init(&int_watcher, sigint_cb, SIGINT);
- ev_signal_start(loop, &int_watcher);
- ev_signal_init(&term_watcher, sigint_cb, SIGTERM);
- ev_signal_start(loop, &term_watcher);
-
- ev_run(loop, 0);
-
- return 0;
-}
-
-int ufr_open(unsigned reader_type, char *port_name, unsigned port_interface)
+static int ufr_open(unsigned reader_type, char *port_name,
+ unsigned port_interface)
{
UFR_STATUS status;
return 0;
}
-int main(int argc, char **argv)
+int mt_rfid_init(mt_rfid_t *self, struct ev_loop *loop, int fd)
{
if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
return -1;
}
- int fd = tty_open(PORT_NAME, CONCAT(B, ASYNC_BAUD_RATE));
- if (fd < 0) {
+ int tty = tty_open(PORT_NAME, CONCAT(B, ASYNC_BAUD_RATE));
+ if (tty < 0) {
return -2;
}
+ self->fd = tty;
- libev_run(fd);
+ ev_io_ufr *w = &(self->w);
+ w->uid = w->uid_data;
+ w->fd = fd;
+ ev_io_init(&(w->w), ufr_cb, tty, EV_READ);
+ ev_io_start(loop, (ev_io *)w);
- if (close(fd) == 0) {
- fprintf(stderr, "closed %d\n", fd);
+ return 0;
+}
+
+void mt_rfid_deinit(mt_rfid_t *self)
+{
+ if (close(self->fd) == 0) {
+ fprintf(stderr, "closed %d\n", self->fd);
} else {
perror("close");
}
- UFR_STATUS status;
- status = ReaderClose();
+ UFR_STATUS status = ReaderClose();
fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
+}
+
+#ifdef IS_MAIN
+int main(int argc, char **argv)
+{
+ struct ev_loop *loop = EV_DEFAULT;
+ mt_rfid_t rfid;
+
+ set_signal_exit(loop);
+
+ if (mt_rfid_init(&rfid, loop, STDOUT_FILENO) != 0) {
+ return -1;
+ }
+
+ ev_run(loop, 0);
+
+ mt_rfid_deinit(&rfid);
return 0;
}
+#endif
/* other tty options
//Enable the receiver and set local mode...