1 /* Python bindings for librfid
2 * (C) 2007 by Kushal Das <kushal@openpcd.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <librfid/rfid.h>
26 #include <librfid/rfid_scan.h>
27 #include <librfid/rfid_reader.h>
28 #include <librfid/rfid_layer2.h>
29 #include <librfid/rfid_protocol.h>
31 #include <librfid/rfid_protocol_mifare_classic.h>
32 #include <librfid/rfid_protocol_mifare_ul.h>
36 static PyObject *pyi_open(PyObject *self, PyObject *args);
37 static PyObject *pyi_close(PyObject *self, PyObject *args);
38 static PyObject *pyi_rfidscan(PyObject *self, PyObject *args);
39 static PyObject *pyi_rfidlayeropt(PyObject *self, PyObject *args);
41 static PyObject *pyi_Error;
42 struct rfid_reader_handle *rh;
43 struct rfid_layer2_handle *l2h;
44 struct rfid_protocol_handle *ph;
46 static PyMethodDef pyi_Methods[] = {
47 {"open", pyi_open, METH_VARARGS,
48 "This will initialise the RFID reader"},
49 {"close", pyi_close, METH_VARARGS,
50 "This will close the RFID reader"},
51 {"scan", pyi_rfidscan, METH_VARARGS,
52 "This will scan for any card"},
53 {"get_id", pyi_rfidlayeropt, METH_VARARGS,
54 "This will read the id of the card"},
58 PyMODINIT_FUNC initpyrfid() {
61 m = Py_InitModule("pyrfid", pyi_Methods);
62 pyi_Error = PyErr_NewException("pyrfid.error", NULL, NULL);
64 PyModule_AddObject(m, "error", pyi_Error);
68 static PyObject *pyi_open(PyObject *self, PyObject *args) {
70 rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
72 return Py_BuildValue("i", 1);
74 return Py_BuildValue("i", 0);
77 static PyObject *pyi_close(PyObject *self, PyObject *args) {
78 rfid_reader_close(rh);
79 // Py_INCREF(Py_None);
81 return Py_BuildValue("i", 0);
84 static PyObject *pyi_rfidscan(PyObject *self, PyObject *args) {
86 rc = rfid_scan(rh, &l2h, &ph);
87 return Py_BuildValue("i", rc);
90 static PyObject *pyi_rfidlayeropt(PyObject *self, PyObject *args) {
91 unsigned char uid_buf[16];
93 unsigned int uid_len = sizeof(uid_buf);
94 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
96 strcpy(card_id,hexdump(uid_buf, uid_len));
97 return Py_BuildValue("s", card_id);