Ticket #1: lorconmodule.c

File lorconmodule.c, 1.5 kB (added by shubnub@…, 20 months ago)

Python Bindings for LORCON

Line 
1#include <Python.h>
2#include <tx80211.h>
3
4static PyObject *
5lorcon_tx80211_getversion(PyObject *self, PyObject *args)
6{
7        return Py_BuildValue("i", tx80211_getversion());
8}
9
10static PyObject *
11lorcon_tx80211_getcardlist(PyObject *self, PyObject *args)
12{
13   struct tx80211_cardlist *cl;
14   int c;
15   PyObject *pylist;
16   
17   if (!(cl = tx80211_getcardlist())) {
18      Py_INCREF(Py_None);
19      return Py_None;
20   }
21   
22   if (!(pylist = PyList_New(cl->num_cards))) {
23      printf("Bad Stuff!!! Can't allocate a new list!\n");
24      exit(1);
25   }
26   
27   for (c = 0; c < cl->num_cards; c++) {
28      PyObject *pydict = PyDict_New();
29      if (!pydict) {
30         printf("Bad Stuff!!! Can't allocate a new dict.\n");
31         exit(1);
32      }
33      PyDict_SetItemString(pydict, "name", Py_BuildValue("s", cl->cardnames[c]));
34      PyDict_SetItemString(pydict, "description", Py_BuildValue("s", cl->descriptions[c]));
35      PyDict_SetItemString(pydict, "capabilities", Py_BuildValue("i", cl->capabilities[c]));
36      PyList_SetItem(pylist, c, pydict);
37   }
38   
39   tx80211_freecardlist(cl);
40   
41   return pylist;
42}   
43
44static PyMethodDef LorconMethods[] = {
45        {"version", lorcon_tx80211_getversion, METH_VARARGS,
46         "Returns the LORCON internal version in the format YYYYMMRR (year-month-release#)."},
47        {"cardlist", lorcon_tx80211_getcardlist, METH_VARARGS,
48         "Returns the string representing the list of supported wireless cards or None, if there's an error."},
49        {NULL, NULL, 0, NULL}
50};
51
52PyMODINIT_FUNC
53initlorcon(void)
54{
55        (void) Py_InitModule("lorcon", LorconMethods);
56}