1 /* Python bindings for librfid
2 * (C) 2007-2008 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>
33 /*#include <common.h>*/
36 static PyObject *pyi_open(PyObject *self, PyObject *args);
37 static PyObject *pyi_close(PyObject *self, PyObject *args);
38 static PyObject *pyi_select_card(PyObject *self, PyObject *args);
39 static PyObject *pyi_deselect_card(PyObject *self, PyObject *args);
40 static PyObject *pyi_get_card_id(PyObject *self, PyObject *args);
41 static PyObject *pyi_openpcd_read(PyObject *self, PyObject *args);
42 static PyObject *pyi_openpcd_write(PyObject *self, PyObject *args);
43 static PyObject *pyi_set_key(PyObject *self, PyObject *args);
45 static PyObject *pyi_Error;
48 static PyMethodDef pyi_Methods[] = {
49 {"open_reader", pyi_open, METH_VARARGS,
50 "This will initialise the RFID reader"},
51 {"close_reader", pyi_close, METH_VARARGS,
52 "This will close the RFID reader"},
53 {"select_card", pyi_select_card, METH_VARARGS,
54 "This will select any card"},
55 {"read",pyi_openpcd_read, METH_VARARGS,
56 "This will read the card with given page number"},
57 {"write",pyi_openpcd_write, METH_VARARGS,
58 "This will write the card with given page number"},
59 {"set_key",pyi_set_key, METH_VARARGS,
60 "This will set the key with given key"},
61 {"deselect_card", pyi_deselect_card, METH_VARARGS,
62 "This will deselect any card"},
63 {"get_card_id", pyi_get_card_id, METH_VARARGS,
64 "This will get the card id"},
68 PyMODINIT_FUNC initpyrfid() {
71 m = Py_InitModule("pyrfid", pyi_Methods);
72 pyi_Error = PyErr_NewException("pyrfid.error", NULL, NULL);
74 PyModule_AddObject(m, "error", pyi_Error);
78 static PyObject *pyi_open(PyObject *self, PyObject *args) {
79 return Py_BuildValue("i",openpcd_open_reader(&handle));
82 static PyObject *pyi_close(PyObject *self, PyObject *args) {
83 return Py_BuildValue("i", openpcd_close_reader(handle));
86 static PyObject *pyi_select_card(PyObject *self, PyObject *args) {
87 return Py_BuildValue("i", openpcd_select_card(handle));
90 static PyObject *pyi_deselect_card(PyObject *self, PyObject *args) {
91 return Py_BuildValue("i", openpcd_deselect_card(handle));
94 static PyObject *pyi_openpcd_read(PyObject *self, PyObject *args) {
97 ok = PyArg_ParseTuple(args, "i", &page);
98 openpcd_read(handle, page, s, 16);
99 return Py_BuildValue("s", s);
103 static PyObject *pyi_openpcd_write(PyObject *self, PyObject *args) {
104 int ok, page, result;
105 char *s = "indianindianindi";
106 ok = PyArg_ParseTuple(args, "is", &page, &s);
107 result = openpcd_write(handle, page, s, 16);
108 return Py_BuildValue("i", result);
111 static PyObject *pyi_set_key(PyObject *self, PyObject *args) {
113 char *s = "keykeykey";
114 ok = PyArg_ParseTuple(args, "is", &key, &s);
115 result = openpcd_set_key(handle, key, s);
116 return Py_BuildValue("i", result);
119 static PyObject *pyi_get_card_id(PyObject *self, PyObject *args) {
122 result = openpcd_get_card_id(handle, &uid);
124 return Py_BuildValue("I", uid);
126 return Py_BuildValue("i", result);