#include <Python.h>
#include <tx80211.h>

static PyObject *
lorcon_tx80211_getversion(PyObject *self, PyObject *args)
{
	return Py_BuildValue("i", tx80211_getversion());
}

static PyObject *
lorcon_tx80211_getcardlist(PyObject *self, PyObject *args)
{
   struct tx80211_cardlist *cl;
   int c;
   PyObject *pylist;
   
   if (!(cl = tx80211_getcardlist())) {
      Py_INCREF(Py_None);
      return Py_None;
   }
   
   if (!(pylist = PyList_New(cl->num_cards))) {
      printf("Bad Stuff!!! Can't allocate a new list!\n");
      exit(1);
   }
   
   for (c = 0; c < cl->num_cards; c++) {
      PyObject *pydict = PyDict_New();
      if (!pydict) {
         printf("Bad Stuff!!! Can't allocate a new dict.\n");
         exit(1);
      }
      PyDict_SetItemString(pydict, "name", Py_BuildValue("s", cl->cardnames[c]));
      PyDict_SetItemString(pydict, "description", Py_BuildValue("s", cl->descriptions[c]));
      PyDict_SetItemString(pydict, "capabilities", Py_BuildValue("i", cl->capabilities[c]));
      PyList_SetItem(pylist, c, pydict);
   }
   
   tx80211_freecardlist(cl);
   
   return pylist;
}   

static PyMethodDef LorconMethods[] = {
	{"version", lorcon_tx80211_getversion, METH_VARARGS,
	 "Returns the LORCON internal version in the format YYYYMMRR (year-month-release#)."},
	{"cardlist", lorcon_tx80211_getcardlist, METH_VARARGS,
	 "Returns the string representing the list of supported wireless cards or None, if there's an error."},
	{NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initlorcon(void)
{
	(void) Py_InitModule("lorcon", LorconMethods);
}

