summaryrefslogtreecommitdiff
path: root/python/pyrfid.c
blob: f1feacdc16b548421c8886326139432378ac0921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Python bindings for librfid 
 *  (C) 2007-2008 by Kushal Das <kushal@openpcd.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#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 "openpcd.h"
#include <Python.h>
static PyObject *pyi_open(PyObject *self, PyObject *args);
static PyObject *pyi_close(PyObject *self, PyObject *args);
static PyObject *pyi_select_card(PyObject *self, PyObject *args);
static PyObject *pyi_deselect_card(PyObject *self, PyObject *args);
static PyObject *pyi_get_card_id(PyObject *self, PyObject *args);
static PyObject *pyi_openpcd_read(PyObject *self, PyObject *args);
static PyObject *pyi_openpcd_write(PyObject *self, PyObject *args);
static PyObject *pyi_set_key(PyObject *self, PyObject *args);

static PyObject *pyi_Error;
MIFARE_HANDLE handle;

static PyMethodDef pyi_Methods[] = {
    {"open_reader", pyi_open, METH_VARARGS,
        "This will initialise the RFID reader"},
    {"close_reader", pyi_close, METH_VARARGS,
        "This will close the RFID reader"},
    {"select_card", pyi_select_card, METH_VARARGS,
        "This will select any card"},
    {"read",pyi_openpcd_read, METH_VARARGS,
        "This will read the card with given page number"},
    {"write",pyi_openpcd_write, METH_VARARGS,
        "This will write the card with given page number"},
    {"set_key",pyi_set_key, METH_VARARGS,
        "This will set the key with given key"},
    {"deselect_card", pyi_deselect_card, METH_VARARGS,
        "This will deselect any card"},
    {"get_card_id", pyi_get_card_id, METH_VARARGS,
        "This will get the card id"},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initpyrfid() {
    PyObject *m;

    m = Py_InitModule("pyrfid", pyi_Methods);
    pyi_Error = PyErr_NewException("pyrfid.error", NULL, NULL);
    Py_INCREF(pyi_Error);
    PyModule_AddObject(m, "error", pyi_Error);
    return;
}

static PyObject *pyi_open(PyObject *self, PyObject *args) {
        return Py_BuildValue("i",openpcd_open_reader(&handle));
}

static PyObject *pyi_close(PyObject *self, PyObject *args) {
        return Py_BuildValue("i", openpcd_close_reader(handle));
}

static PyObject *pyi_select_card(PyObject *self, PyObject *args) {
        return Py_BuildValue("i", openpcd_select_card(handle));
}

static PyObject *pyi_deselect_card(PyObject *self, PyObject *args) {
        return Py_BuildValue("i", openpcd_deselect_card(handle));
}

static PyObject *pyi_openpcd_read(PyObject *self, PyObject *args) {
        int ok, page;
        char s[16];
        ok = PyArg_ParseTuple(args, "i", &page);
        openpcd_read(handle, page, s, 16);
        return Py_BuildValue("s", s);
}


static PyObject *pyi_openpcd_write(PyObject *self, PyObject *args) {
        int ok, page, result;
        char *s = "indianindianindi";
        ok = PyArg_ParseTuple(args, "is", &page, &s);
        result = openpcd_write(handle, page, s, 16);
        return Py_BuildValue("i", result);
}

static PyObject *pyi_set_key(PyObject *self, PyObject *args) {
        int ok, key, result;
        char *s = "keykeykey";
        ok = PyArg_ParseTuple(args, "is", &key, &s);
        result = openpcd_set_key(handle, key, s);
        return Py_BuildValue("i", result);
}

static PyObject *pyi_get_card_id(PyObject *self, PyObject *args) {
	unsigned int uid;
	int result;
	result = openpcd_get_card_id(handle, &uid);
        if (result == 0)
		return Py_BuildValue("I", uid);
	else
		return Py_BuildValue("i", result);
}

personal git repositories of Harald Welte. Your mileage may vary