]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/commitdiff
header file, open arguments from self, getopts beep
authorJiří Matěják <jiri.matejak@fel.cvut.cz>
Mon, 23 Apr 2018 14:50:14 +0000 (16:50 +0200)
committerJiří Matěják <jiri.matejak@fel.cvut.cz>
Mon, 23 Apr 2018 14:50:14 +0000 (16:50 +0200)
mt_rfid.c
mt_rfid.h [new file with mode: 0644]

index 6b2b42540b397d155d363d7124f64cbf340c5736..328350c74dc644a5d3ef00a4676b2582c7bbd109 100644 (file)
--- a/mt_rfid.c
+++ b/mt_rfid.c
@@ -14,6 +14,7 @@
 #include <pthread.h>
 #include <signal.h>
 
+#include "mt_rfid.h"
 #include "uFCoder.h"
 
 #define READER_TYPE    1              //uFR type (1Mbps)
 #endif
 #define PORT_INTERFACE 1              //Serial
 
+// really simple JSON helpers
 #define JSON_NUM(NAME) printf("\"" #NAME "\": %d", NAME)
 #define JSON_STR(NAME) printf("\"" #NAME "\": \"%s\"", NAME)
 #define JSON_START() printf("{\n")
 #define JSON_NEXT() printf(",\n")
 #define JSON_END() printf("\n}\n")
 
-static int run = 1;
+static mt_rfid_t ufr = {READER_TYPE, PORT_NAME, PORT_INTERFACE, 0, 1};
 
 static void signal_exit(int sig)
 {
-    run = 0;
+    ufr.run = 0;
     fprintf(stderr, "signal %d, terminating\n", sig);
 }
 
-static void *read_cards(void *ptr)
+void *mt_rfid_run(void *ptr)
 {
+    mt_rfid_t *self = (mt_rfid_t *)ptr;
     UFR_STATUS status;
 
     uint8_t card_type;
-    uint8_t sak;
-    uint8_t uid_num[10];
+    uint8_t sak;         //select acknowledge
+    uint8_t uid_num[10]; //uid as bytes
     uint8_t uid_size;
-    char uid[24];
+    char uid[24];        //uid as a string
 
     fprintf(stderr, "uFCoder library version: %s\n", GetDllVersionStr());
 
-    status = ReaderOpenEx(READER_TYPE, PORT_NAME, PORT_INTERFACE, 0);
+    status = ReaderOpenEx(self->reader_type, self->port_name, self->port_interface, 0);
     if (status != UFR_OK) {
         fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
         return NULL;
     }
 
-    //status = ReaderUISignal(0, 0);
-    //printf("ReaderUISignal: %s\n", UFR_Status2String(status));
-
     fputs(GetReaderDescription(), stderr);
 
-    while (run) {
+    while (self->run) {
         status = GetDlogicCardType(&card_type);
         if (status != UFR_OK) {
             if (status != UFR_NO_CARD) {
@@ -88,6 +88,10 @@ static void *read_cards(void *ptr)
         JSON_NUM(uid_size);  JSON_NEXT();
         JSON_STR(uid);       JSON_END();
 
+        if (self->beep) {
+            ReaderUISignal(0, 1);
+        }
+
         usleep(500000);
     }
 
@@ -97,14 +101,27 @@ static void *read_cards(void *ptr)
     return NULL;
 }
 
-int main(void)
+int main(int argc, char **argv)
 {
     pthread_t t;
+    char c;
+
+    while ((c = getopt(argc, argv, "p:bh")) != -1) {
+        switch (c) {
+            case 'p':
+                ufr.port_name = optarg;
+                break;
+            case 'b':
+                ufr.beep = 1;
+                break;
+        }
+    }
 
+    // avoid sigaction for windows compatibility
     signal(SIGINT, signal_exit);
     signal(SIGTERM, signal_exit);
 
-    if (pthread_create(&t, NULL, read_cards, NULL)) {
+    if (pthread_create(&t, NULL, mt_rfid_run, (void *)&ufr)) {
         perror("pthread_create");
         return -1;
     }
diff --git a/mt_rfid.h b/mt_rfid.h
new file mode 100644 (file)
index 0000000..c325a07
--- /dev/null
+++ b/mt_rfid.h
@@ -0,0 +1,14 @@
+#ifndef MT_RFID_H
+#define MT_RFID_H
+
+typedef struct {
+    unsigned reader_type;
+    char *port_name;
+    unsigned port_interface;
+    unsigned beep;
+    unsigned run;
+} mt_rfid_t;
+
+void *mt_rfid_run(void *ptr);
+
+#endif