use defined hexdump istead of rfid_hexdump
[librfid] / python / pyrfid.c
1 /* Python bindings for librfid 
2  *  (C) 2007-2008 by Kushal Das <kushal@openpcd.org>
3  *
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
7  *
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.
12  *
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
16  */
17
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <libgen.h>
24
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>
30
31 #include <librfid/rfid_protocol_mifare_classic.h>
32 #include <librfid/rfid_protocol_mifare_ul.h>
33 /*#include <common.h>*/
34 #include "openpcd.h"
35 #include <Python.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);
44
45 static PyObject *pyi_Error;
46 MIFARE_HANDLE handle;
47
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"},
65     {NULL, NULL, 0, NULL}
66 };
67
68 PyMODINIT_FUNC initpyrfid() {
69     PyObject *m;
70
71     m = Py_InitModule("pyrfid", pyi_Methods);
72     pyi_Error = PyErr_NewException("pyrfid.error", NULL, NULL);
73     Py_INCREF(pyi_Error);
74     PyModule_AddObject(m, "error", pyi_Error);
75     return;
76 }
77
78 static PyObject *pyi_open(PyObject *self, PyObject *args) {
79         return Py_BuildValue("i",openpcd_open_reader(&handle));
80 }
81
82 static PyObject *pyi_close(PyObject *self, PyObject *args) {
83         return Py_BuildValue("i", openpcd_close_reader(handle));
84 }
85
86 static PyObject *pyi_select_card(PyObject *self, PyObject *args) {
87         return Py_BuildValue("i", openpcd_select_card(handle));
88 }
89
90 static PyObject *pyi_deselect_card(PyObject *self, PyObject *args) {
91         return Py_BuildValue("i", openpcd_deselect_card(handle));
92 }
93
94 static PyObject *pyi_openpcd_read(PyObject *self, PyObject *args) {
95         int ok, page;
96         char s[16];
97         ok = PyArg_ParseTuple(args, "i", &page);
98         openpcd_read(handle, page, s, 16);
99         return Py_BuildValue("s", s);
100 }
101
102
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);
109 }
110
111 static PyObject *pyi_set_key(PyObject *self, PyObject *args) {
112         int ok, key, result;
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);
117 }
118
119 static PyObject *pyi_get_card_id(PyObject *self, PyObject *args) {
120         unsigned int uid;
121         int result;
122         result = openpcd_get_card_id(handle, &uid);
123         if (result == 0)
124                 return Py_BuildValue("I", uid);
125         else
126                 return Py_BuildValue("i", result);
127 }
128