summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authormeri <meri@e0336214-984f-0b4b-a45f-81c69e1f0ede>2007-05-30 22:03:53 +0000
committermeri <meri@e0336214-984f-0b4b-a45f-81c69e1f0ede>2007-05-30 22:03:53 +0000
commitf323012d879181580bec0cff312819d678ed2e56 (patch)
tree819d0bf93be5ff83b50f9d63c2f730c0b88b21aa /python
parenta91cd3d9e2a4e985c63850679e8242c6d08fed08 (diff)
python support by Kushal Das
git-svn-id: https://svn.gnumonks.org/trunk/librfid@1997 e0336214-984f-0b4b-a45f-81c69e1f0ede
Diffstat (limited to 'python')
-rw-r--r--python/Makefile34
-rw-r--r--python/pyrfid.c80
-rw-r--r--python/test.py14
3 files changed, 128 insertions, 0 deletions
diff --git a/python/Makefile b/python/Makefile
new file mode 100644
index 0000000..28fd93c
--- /dev/null
+++ b/python/Makefile
@@ -0,0 +1,34 @@
+CC=gcc
+PYTHON=python
+PREFIX=/usr
+PYTHON_VER=2.5
+PYTHON_INC=$(PREFIX)/include/python$(PYTHON_VER)
+PYTHON_LIB=$(PREFIX)/lib/python$(PYTHON_VER)/site-packages/
+LIBRFID_DIR=../src/.libs/librfid.la
+LIBUSB_DIR=/usr/local/lib
+
+SOURCE_MAIN=pyrfid.c
+SOURCES=$(SOURCE_MAIN) ../utils/common.c
+INCLUDES=-I$(PYTHON_INC) -I../include/ -I../utils/
+CFLAGS=-O3 -Wall $(INCLUDES)
+LDFLAGS=-shared -L$(LIBRFID_DIR) -lrfid -L$(LIBUSB_DIR) -lusb -Wl,--rpath -Wl,/usr/local/lib $(LIBS)
+TARGET=$(SOURCE_MAIN:.c=.so)
+OBJECTS=$(SOURCES:.c=.o)
+
+all: $(SOURCE_MAIN) $(TARGET)
+
+test:
+ $(PYTHON) $@.py
+
+install: $(TARGET)
+ install $(TARGET) $(PYTHON_LIB)
+
+$(TARGET): $(OBJECTS)
+ $(CC) $(LDFLAGS) -o $@ $(OBJECTS)
+
+.c.o:
+ $(CC) $(CFLAGS) -c $< -o $@
+
+clean:
+ rm -f $(OBJECTS) $(TARGET)
+
diff --git a/python/pyrfid.c b/python/pyrfid.c
new file mode 100644
index 0000000..e626718
--- /dev/null
+++ b/python/pyrfid.c
@@ -0,0 +1,80 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <libgen.h>
+
+#include <librfid/rfid.h>
+#include <librfid/rfid_scan.h>
+#include <librfid/rfid_reader.h>
+#include <librfid/rfid_layer2.h>
+#include <librfid/rfid_protocol.h>
+
+#include <librfid/rfid_protocol_mifare_classic.h>
+#include <librfid/rfid_protocol_mifare_ul.h>
+#include <common.h>
+
+#include <Python.h>
+static PyObject *pyi_open(PyObject *self, PyObject *args);
+static PyObject *pyi_close(PyObject *self, PyObject *args);
+static PyObject *pyi_rfidscan(PyObject *self, PyObject *args);
+static PyObject *pyi_rfidlayeropt(PyObject *self, PyObject *args);
+
+static PyObject *pyi_Error;
+struct rfid_reader_handle *rh;
+struct rfid_layer2_handle *l2h;
+struct rfid_protocol_handle *ph;
+
+static PyMethodDef pyi_Methods[] = {
+ {"open", pyi_open, METH_VARARGS,
+ "This will initialise the RFID reader"},
+ {"close", pyi_close, METH_VARARGS,
+ "This will close the RFID reader"},
+ {"scan", pyi_rfidscan, METH_VARARGS,
+ "This will scan for any card"},
+ {"get_id", pyi_rfidlayeropt, METH_VARARGS,
+ "This will read the id of the card"},
+ {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC initpyrfid() {
+ PyObject *m;
+
+ m = Py_InitModule("openpcd", pyi_Methods);
+ pyi_Error = PyErr_NewException("openpcd.error", NULL, NULL);
+ Py_INCREF(pyi_Error);
+ PyModule_AddObject(m, "error", pyi_Error);
+ return;
+}
+
+static PyObject *pyi_open(PyObject *self, PyObject *args) {
+ rfid_init();
+ rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
+ if (!rh)
+ return Py_BuildValue("i", 1);
+ else
+ return Py_BuildValue("i", 0);
+}
+
+static PyObject *pyi_close(PyObject *self, PyObject *args) {
+ rfid_reader_close(rh);
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyObject *pyi_rfidscan(PyObject *self, PyObject *args) {
+ int rc;
+ rc = rfid_scan(rh, &l2h, &ph);
+ return Py_BuildValue("i", rc);
+}
+
+static PyObject *pyi_rfidlayeropt(PyObject *self, PyObject *args) {
+ unsigned char uid_buf[16];
+ char card_id[16];
+ unsigned int uid_len = sizeof(uid_buf);
+ rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
+ &uid_len);
+ strcpy(card_id,hexdump(uid_buf, uid_len));
+ return Py_BuildValue("s", card_id);
+}
diff --git a/python/test.py b/python/test.py
new file mode 100644
index 0000000..4180542
--- /dev/null
+++ b/python/test.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+import pyrfid
+
+res = openpcd.open()
+if res == 1:
+ print "No device found"
+else:
+ print "We found a device :)"
+ while 1:
+ res = openpcd.scan()
+ if res == 3:
+ print "The id of the card is %s" %(openpcd.get_id())
+openpcd.close()
personal git repositories of Harald Welte. Your mileage may vary