update FSF postal address (Jeremy Laine)
[librfid] / python / pyrfid.c
1 /* Python bindings for librfid 
2  *  (C) 2007 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
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_rfidscan(PyObject *self, PyObject *args);
39 static PyObject *pyi_rfidlayeropt(PyObject *self, PyObject *args);
40
41 static PyObject *pyi_Error;
42 struct rfid_reader_handle *rh;
43 struct rfid_layer2_handle *l2h;
44 struct rfid_protocol_handle *ph;
45
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"},
55     {NULL, NULL, 0, NULL}
56 };
57
58 PyMODINIT_FUNC initpyrfid() {
59     PyObject *m;
60
61     m = Py_InitModule("pyrfid", pyi_Methods);
62     pyi_Error = PyErr_NewException("pyrfid.error", NULL, NULL);
63     Py_INCREF(pyi_Error);
64     PyModule_AddObject(m, "error", pyi_Error);
65     return;
66 }
67
68 static PyObject *pyi_open(PyObject *self, PyObject *args) {
69     rfid_init();
70     rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
71     if (!rh)
72         return Py_BuildValue("i", 1);
73     else
74         return Py_BuildValue("i", 0);
75 }
76
77 static PyObject *pyi_close(PyObject *self, PyObject *args) {
78     rfid_reader_close(rh);
79 //     Py_INCREF(Py_None);
80 //     return Py_None;
81      return Py_BuildValue("i", 0);
82 }
83
84 static PyObject *pyi_rfidscan(PyObject *self, PyObject *args) {
85     int rc;
86         rc = rfid_scan(rh, &l2h, &ph);
87         return Py_BuildValue("i", rc);
88 }
89
90 static PyObject *pyi_rfidlayeropt(PyObject *self, PyObject *args) {
91                 unsigned char uid_buf[16];
92                 char card_id[16];
93                 unsigned int uid_len = sizeof(uid_buf);
94                 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
95                                    &uid_len);
96                 strcpy(card_id,hexdump(uid_buf, uid_len));
97                 return Py_BuildValue("s", card_id);
98 }