forked from andig/canprogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelster_py.cpp
executable file
·293 lines (227 loc) · 6.93 KB
/
elster_py.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
*
* Copyright (C) 2014 Jürg Müller, CH-5524
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*/
#include <Python.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "NTypes.h"
#include "NUtils.h"
#include "KElsterTable.h"
#include "elster_instance.h"
static PyObject * elsterError;
static bool get_params(PyObject *args, const char * & params)
{
if (!PyArg_ParseTuple(args, "s", ¶ms))
return false;
if (!params)
return false;
while (*params == ' ')
params++;
return true;
}
// siehe: https://docs.python.org/3/extending/extending.html
#ifdef __cplusplus
extern "C" {
#endif
#define PY_RETURN_NONE Py_INCREF(Py_None), Py_None
static PyObject * elster_getname(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
return PyBytes_FromString(elster_instance::GetName(params));
}
static PyObject * elster_gettype(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
return PyBytes_FromString(elster_instance::GetType(params));
}
static PyObject * elster_getvalue(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
return PyBytes_FromString(elster_instance::GetValue(params));
}
static PyObject * elster_setvalue(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
if (elster_instance::SetValue(params))
return PyBytes_FromString("ok");
else
return PY_RETURN_NONE;
}
static PyObject * elster_setbits(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
if (elster_instance::SetBits(params))
return PyBytes_FromString("ok");
else
return PY_RETURN_NONE;
}
static PyObject * elster_clrbits(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
if (elster_instance::ClrBits(params))
return PyBytes_FromString("ok");
else
return PY_RETURN_NONE;
}
static PyObject * elster_getstring(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
return PyBytes_FromString(elster_instance::GetString(params));
}
static PyObject * elster_setstring(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
if (elster_instance::SetString(params))
return PyBytes_FromString("ok");
else
return PY_RETURN_NONE;
}
static PyObject * elster_trace(PyObject *self, PyObject *args)
{
return PyBytes_FromString(elster_instance::ToggleTrace());
}
static PyObject * elster_setdev(PyObject *self, PyObject *args)
{
const char * params;
if (!get_params(args, params))
return PY_RETURN_NONE;
if (elster_instance::SetDev(params))
return PyBytes_FromString("ok");
else
return PY_RETURN_NONE;
}
static PyObject * elster_setcs(PyObject *self, PyObject *args)
{
if (elster_instance::SetCS())
return PyBytes_FromString("ok");
else
return PY_RETURN_NONE;
}
static PyObject * elster_set_can232(PyObject *self, PyObject *args)
{
if (elster_instance::SetCAN232())
return PyBytes_FromString("ok");
else
return PY_RETURN_NONE;
}
static PyObject * elster_system(PyObject *self, PyObject *args)
{
const char * command;
int sts;
if (!PyArg_ParseTuple(args, "s", &command))
return PY_RETURN_NONE;
sts = system(command);
if (sts < 0)
{
PyErr_SetString(elsterError, "System command failed");
return PY_RETURN_NONE;
}
return PyLong_FromLong(sts);
}
static PyObject * elster_geterrmsg(PyObject *self, PyObject *args)
{
return PyBytes_FromString(elster_instance::GetErrMsg());
}
static PyMethodDef elsterMethods[] =
{
{"system", elster_system, METH_VARARGS, "Execute a shell command."},
{"getvalue", elster_getvalue, METH_VARARGS, "Get Elster value"},
{"setvalue", elster_setvalue, METH_VARARGS, "Set Elster value"}, // setvalue, setbits und clrbits sind noch nicht getestet
{"setbits", elster_setbits, METH_VARARGS, "Set bits"},
{"clrbits", elster_clrbits, METH_VARARGS, "Clear bits"},
{"getstring", elster_getstring, METH_VARARGS, "Get Elster string"},
{"setstring", elster_setstring, METH_VARARGS, "Set Elster string"},
{"getname", elster_getname, METH_VARARGS, "Get Elster name"},
{"gettype", elster_gettype, METH_VARARGS, "Get Elster type"},
{"trace", elster_trace, METH_VARARGS, "Set tracing"},
{"setdev", elster_setdev, METH_VARARGS, "Set can device"},
{"setcs", elster_setcs, METH_VARARGS, "Set optical inteface (ComfortSoft)"},
{"set_can232",elster_set_can232,METH_VARARGS, "Set CAN232 inteface (SUBtin)"},
{"geterrmsg", elster_geterrmsg, METH_VARARGS, "Get last error message"},
{NULL, NULL, 0, NULL}
};
////////////////////////////////////////////////////////////////////////////////
struct module_state
{
PyObject * error;
};
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
static int elster_traverse(PyObject *m, visitproc visit, void *arg)
{
Py_VISIT(GETSTATE(m)->error);
return 0;
}
static int elster_clear(PyObject *m)
{
Py_CLEAR(GETSTATE(m)->error);
return 0;
}
static void elster_free(void *)
{
elster_instance::Undef();
}
static struct PyModuleDef moduledef =
{
PyModuleDef_HEAD_INIT,
"elster",
NULL,
sizeof(struct module_state), // Wenn hier -1 steht, dann wird elster_free nicht ausgefuehrt?
elsterMethods,
NULL,
elster_traverse,
elster_clear,
elster_free
};
PyObject * PyInit_elster(void)
{
PyObject * m;
m = PyModule_Create(&moduledef);
if (!m)
return NULL;
elsterError = PyErr_NewException("elster.error", NULL, NULL);
Py_INCREF(elsterError);
PyModule_AddObject(m, "error", elsterError);
return m;
}
#ifdef __cplusplus
}
#endif
int main(int argc, char *argv[])
{
wchar_t name[1024];
for (unsigned i = 0; i <= strlen(argv[0]); i++)
name[i] = argv[0][i];
Py_SetProgramName(name);
Py_Initialize();
return 0;
}