interslipmodule.c 5.7 KB
Newer Older
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

#include "Python.h"
#include "InterslipLib.h"
#include "macglue.h"

static PyObject *ErrorObject;

/* ----------------------------------------------------- */

static char pyis_open__doc__[] =
"Load the interslip driver (optional)"
;

static PyObject *
pyis_open(self, args)
	PyObject *self;	/* Not used */
	PyObject *args;
{
	OSErr err;

	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	err = is_open();
	if ( err ) {
		PyErr_Mac(ErrorObject, err);
		return NULL;
	}
	Py_INCREF(Py_None);
	return Py_None;
}

static char pyis_connect__doc__[] =
"Tell the driver to start a connect"
;

static PyObject *
pyis_connect(self, args)
	PyObject *self;	/* Not used */
	PyObject *args;
{
	OSErr err;

	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	err = is_connect();
	if ( err ) {
		PyErr_Mac(ErrorObject, err);
		return NULL;
	}
	Py_INCREF(Py_None);
	return Py_None;
}

static char pyis_disconnect__doc__[] =
"Tell the interslip driver to start a disconnect"
;

static PyObject *
pyis_disconnect(self, args)
	PyObject *self;	/* Not used */
	PyObject *args;
{
	OSErr err;

	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	err = is_disconnect();
	if ( err ) {
		PyErr_Mac(ErrorObject, err);
		return NULL;
	}
	Py_INCREF(Py_None);
	return Py_None;
}

static char pyis_status__doc__[] =
"Return (numeric_status, message_seqnum, message_string) status tuple"
;

static PyObject *
pyis_status(self, args)
	PyObject *self;	/* Not used */
	PyObject *args;
{
	long status, seqnum;
	StringPtr message;
	OSErr err;

	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	err = is_status(&status, &seqnum, &message);
	if ( err ) {
		PyErr_Mac(ErrorObject, err);
		return NULL;
	}
	return Py_BuildValue("iiO&", (int)status, (int)seqnum, PyMac_BuildStr255, message);
}

static char pyis_getconfig__doc__[] =
"Return configuration data (ibaud, obaud, flags, idrvname, odrvname, cfgname)"
;

static PyObject *
pyis_getconfig(self, args)
	PyObject *self;	/* Not used */
	PyObject *args;
{
	long baudrate, flags;
	Str255 idrvname, odrvname, cfgname;
	OSErr err;
	int ibaud, obaud;

	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	err = is_getconfig(&baudrate, &flags, idrvname, odrvname, cfgname);
	if ( err ) {
		PyErr_Mac(ErrorObject, err);
		return NULL;
	}
	ibaud = (baudrate >> 16) & 0xffff;
	obaud = baudrate & 0xffff;
	return Py_BuildValue("iiiO&O&O&", ibaud, obaud, (int)flags, PyMac_BuildStr255, idrvname,
			PyMac_BuildStr255, odrvname, PyMac_BuildStr255, cfgname);
}

static char pyis_setconfig__doc__[] =
"Set configuration data (ibaud, obaud, flags, idrvname, odrvname, cfgname)"
;

static PyObject *
pyis_setconfig(self, args)
	PyObject *self;	/* Not used */
	PyObject *args;
{
	long baudrate;
	int flags;
	Str255 idrvname, odrvname, cfgname;
	OSErr err;
	int ibaud, obaud;

	if (!PyArg_ParseTuple(args, "iiiO&O&O&", &ibaud, &obaud, &flags, PyMac_GetStr255, idrvname,
			PyMac_GetStr255, odrvname, PyMac_GetStr255, cfgname))
		return NULL;
	baudrate = (ibaud << 16) | obaud;
	err = is_setconfig(baudrate, (long)flags, idrvname, odrvname, cfgname);
	if ( err ) {
		PyErr_Mac(ErrorObject, err);
		return NULL;
	}
	Py_INCREF(Py_None);
	return Py_None;
}

/* List of methods defined in the module */

static struct PyMethodDef pyis_methods[] = {
	{"open",	pyis_open,	1,	pyis_open__doc__},
 {"connect",	pyis_connect,	1,	pyis_connect__doc__},
 {"disconnect",	pyis_disconnect,	1,	pyis_disconnect__doc__},
 {"status",	pyis_status,	1,	pyis_status__doc__},
 {"getconfig",	pyis_getconfig,	1,	pyis_getconfig__doc__},
 {"setconfig",	pyis_setconfig,	1,	pyis_setconfig__doc__},
 
	{NULL,		NULL}		/* sentinel */
};


/* Initialization function for the module (*must* be called initinterslip) */

static char interslip_module_documentation[] = 
""
;

void
initinterslip()
{
	PyObject *m, *d;

	/* Create the module and add the functions */
	m = Py_InitModule4("interslip", pyis_methods,
		interslip_module_documentation,
		(PyObject*)NULL,PYTHON_API_VERSION);

	/* Add some symbolic constants to the module */
	d = PyModule_GetDict(m);
	ErrorObject = PyString_FromString("interslip.error");
	PyDict_SetItemString(d, "error", ErrorObject);

	/* XXXX Add constants here */

	PyDict_SetItemString(d, "IDLE", PyInt_FromLong(IS_IDLE));
	PyDict_SetItemString(d, "WMODEM", PyInt_FromLong(IS_WMODEM));
	PyDict_SetItemString(d, "DIAL", PyInt_FromLong(IS_DIAL));
	PyDict_SetItemString(d, "LOGIN", PyInt_FromLong(IS_LOGIN));
	PyDict_SetItemString(d, "RUN", PyInt_FromLong(IS_RUN));
	PyDict_SetItemString(d, "DISC", PyInt_FromLong(IS_DISC));
	
	/* Check for errors */
	if (PyErr_Occurred())
		Py_FatalError("can't initialize module interslip");
}