-/**
- * mt_rfid.c
- */
-
-// shit to avoid constant repetition
-#define CONCAT_AGAIN(A,B) A ## B
-#define CONCAT(A,B) CONCAT_AGAIN(A,B)
-
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <uFCoder.h>
#include "mt_rfid.h"
-#include "ev_signal_exit.h"
+#include "signal_exit.h"
+#include "json_helpers.h"
+
+// shit to avoid constant repetition
+#define CONCAT_AGAIN(A,B) A ## B
+#define CONCAT(A,B) CONCAT_AGAIN(A,B)
static int set_nonblock(int fd)
{
perror("fcntl (set)");
return -1;
}
- fprintf(stderr, "set non-blocking\n");
+ fprintf(stderr, "set %d non-blocking\n", fd);
return 0;
}
return -1;
}
- fprintf(stderr, "set rts %d\n", level);
+ fprintf(stderr, "set %d rts %d\n", fd, level);
return 0;
}
tcsetattr(fd, TCSANOW, &options);
}
-static int tty_open(const char *port, int baud_rate)
+static int tty_open(const char *port, int br)
{
int fd = open(port, O_RDONLY | O_NOCTTY);
}
set_nonblock(fd);
- set_rts(fd, 0);
- set_baud_rate(fd, baud_rate);
- usleep(1200000); //value by d-logic
+ set_rts(fd, 0); //disable
+ set_baud_rate(fd, br);
+ usleep(1200000); //value by d-logic
tcflush(fd, TCIFLUSH);
return fd;
}
-// 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
return;
}
- JSON_PRINT();
+ rfid_json_print(fd, card_type, sak, uid, size);
#ifdef UFR_BEEP
ReaderUISignal(0, 1); // no light, one beep
#endif
}
+#define UFR_ASYNC_SUFFIX 0 // keep it zero: separates uids, terminates strings
+
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;
- if (uid == ASYNC_SUFFIX) {
- //*(w->uid - 1) = 0; // no need if ASYNC_SUFFIX is 0
+ if (uid == UFR_ASYNC_SUFFIX) {
+ //*(w->uid - 1) = 0; // no need if UFR_ASYNC_SUFFIX is 0
w->uid = w->uid_data;
ufr_read(w->uid, w->fd);
}
fprintf(stderr, "%s\n", GetReaderDescription());
status = SetAsyncCardIdSendConfig(
- 1, //enable send
- 0, //disable prefix
- 0, //prefix
- ASYNC_SUFFIX, //suffix
- 0, //disable send removed
- ASYNC_BAUD_RATE
+ 1, //enable send
+ 0, //disable prefix
+ 0, //prefix
+ UFR_ASYNC_SUFFIX, //suffix
+ 0, //disable send removed
+ UFR_ASYNC_BAUD_RATE
);
fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
if (status != UFR_OK) {
int mt_rfid_init(mt_rfid_t *self, struct ev_loop *loop, int fd)
{
- if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
+ if (ufr_open(UFR_READER_TYPE, UFR_PORT_NAME, UFR_PORT_INTERFACE) == -1) {
return -1;
}
- int tty = tty_open(PORT_NAME, CONCAT(B, ASYNC_BAUD_RATE));
+ int tty = tty_open(UFR_PORT_NAME, CONCAT(B, UFR_ASYNC_BAUD_RATE));
if (tty < 0) {
return -2;
}
fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
}
-#ifdef IS_MAIN
+#ifndef NO_MAIN
int main(int argc, char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
ev_run(loop, 0);
mt_rfid_deinit(&rfid);
+ ev_loop_destroy(loop);
return 0;
}
#endif
-
-/* other tty options
- //Enable the receiver and set local mode...
- options.c_cflag |= (CLOCAL | CREAD);
-
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
- options.c_cflag &= ~CRTSCTS;
- options.c_lflag |= (ICANON | ECHO | ECHOE);
- //Disable XON/XOFF both i/p and o/p
- options.c_iflag &= ~(IXON | IXOFF | IXANY);
- //Non Cannon mode
- options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
-
- options.c_oflag |= OPOST;
-*/
-