libosdp-sys 3.2.1

Sys crate for https://github.com/goToMain/libosdp
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * Copyright (c) 2020-2026 Siddharth Chandrasekaran <sidcha.dev@gmail.com>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdarg.h>
#include "module.h"

int pyosdp_dict_add_bool(PyObject *dict, const char *key, bool val)
{
	int ret;
	PyObject *val_obj;

	val_obj = val ? Py_True : Py_False;

	Py_INCREF(val_obj);
	ret = PyDict_SetItemString(dict, key, val_obj);
	Py_DECREF(val_obj);
	return ret;
}

int pyosdp_dict_add_int(PyObject *dict, const char *key, int val)
{
	int ret;
	PyObject *val_obj;

	val_obj = PyLong_FromLong((long)val);
	if (val_obj == NULL)
		return -1;
	ret = PyDict_SetItemString(dict, key, val_obj);
	Py_DECREF(val_obj);
	return ret;
}

int pyosdp_dict_add_str(PyObject *dict, const char *key, const char *val)
{
	int ret;
	PyObject *val_obj;

	val_obj = PyUnicode_FromString(val);
	if (val_obj == NULL)
		return -1;
	ret = PyDict_SetItemString(dict, key, val_obj);
	Py_DECREF(val_obj);
	return ret;
}

int pyosdp_dict_add_bytes(PyObject *dict, const char *key, const uint8_t *data,
			  int len)
{
	int ret;
	PyObject *obj;

	obj = Py_BuildValue("y#", data, len);
	if (obj == NULL)
		return -1;
	ret = PyDict_SetItemString(dict, key, obj);
	Py_DECREF(obj);
	return ret;
}

int pyosdp_module_add_type(PyObject *module, const char *name,
			   PyTypeObject *type)
{
	if (PyType_Ready(type)) {
		return -1;
	}
	Py_INCREF(type);
	if (PyModule_AddObject(module, name, (PyObject *)type)) {
		Py_DECREF(type);
		return -1;
	}
	return 0;
}

int pyosdp_parse_int(PyObject *obj, int *res)
{
	PyObject *tmp;

	/* Check if obj is numeric */
	if (PyNumber_Check(obj) != 1) {
		PyErr_SetString(PyExc_TypeError, "Expected number");
		return -1;
	}

	tmp = PyNumber_Long(obj);
	*res = (int)PyLong_AsUnsignedLong(tmp);
	Py_DECREF(tmp);
	return 0;
}

int pyosdp_parse_bool(PyObject *obj, bool *res)
{
	/* Check if obj is boolean */
	if (PyBool_Check(obj) != 1) {
		PyErr_SetString(PyExc_TypeError, "Expected boolean");
		return -1;
	}

	*res = PyObject_IsTrue(obj);
	return 0;
}

/* NOTE: Caller must free the returned string with free() */
int pyosdp_parse_str(PyObject *obj, char **str)
{
	char *s;
	PyObject *str_ref;

	str_ref = PyUnicode_AsEncodedString(obj, "UTF-8", "strict");
	if (str_ref == NULL) {
		PyErr_SetString(PyExc_TypeError, "Expected string");
		return -1;
	}

	s = PyBytes_AsString(str_ref);
	if (s == NULL) {
		PyErr_SetString(PyExc_TypeError, "Expected string");
		Py_DECREF(str_ref);
		return -1;
	}
	*str = strdup(s);
	if (*str == NULL) {
		PyErr_SetString(PyExc_MemoryError, "String allocation failed");
		Py_DECREF(str_ref);
		return -1;
	}
	Py_DECREF(str_ref);
	return 0;
}

int pyosdp_parse_bytes(PyObject *obj, uint8_t **data, int *length, bool allow_empty)
{
	Py_ssize_t len;
	uint8_t *buf;

	if (!obj || !PyArg_Parse(obj, "y#", &buf, &len))
		return -1;

	if (buf == NULL || (!allow_empty && len == 0)) {
		PyErr_Format(PyExc_ValueError, "Unable to extact data bytes");
		return -1;
	}
	*data = buf;
	*length = len;
	return 0;
}

int pyosdp_dict_get_str(PyObject *dict, const char *key, char **str)
{
	PyObject *tmp;

	if (!PyDict_Check(dict)) {
		PyErr_SetString(PyExc_TypeError, "arg is not a dict");
		return -1;
	}

	tmp = PyDict_GetItemString(dict, key);
	if (tmp == NULL) {
		PyErr_Format(PyExc_KeyError,
			     "Key: '%s' of type: string expected", key);
		return -1;
	}

	return pyosdp_parse_str(tmp, str);
}

int pyosdp_dict_get_int(PyObject *dict, const char *key, int *res)
{
	PyObject *tmp;

	if (!PyDict_Check(dict)) {
		PyErr_SetString(PyExc_TypeError, "arg is not a dict");
		return -1;
	}

	tmp = PyDict_GetItemString(dict, key);
	if (tmp == NULL) {
		PyErr_Format(PyExc_KeyError, "Key: '%s' of type: int expected",
			     key);
		return -1;
	}

	return pyosdp_parse_int(tmp, res);
}

int pyosdp_dict_get_bool(PyObject *dict, const char *key, bool *res)
{
	PyObject *tmp;

	if (!PyDict_Check(dict)) {
		PyErr_SetString(PyExc_TypeError, "arg is not a dict");
		return -1;
	}

	tmp = PyDict_GetItemString(dict, key);
	if (tmp == NULL)
		return 1;

	return pyosdp_parse_bool(tmp, res);
}

int pyosdp_dict_get_bytes(PyObject *dict, const char *key, uint8_t **data,
			  int *length)
{
	PyObject *obj;

	if (!PyDict_Check(dict)) {
		PyErr_SetString(PyExc_TypeError, "arg is not a dict");
		return -1;
	}

	obj = PyDict_GetItemString(dict, key);
	if (obj == NULL) {
		PyErr_Format(PyExc_KeyError,
			     "Key: '%s' of type: bytes expected", key);
		return -1;
	}

	return pyosdp_parse_bytes(obj, data, length, false);
}

int pyosdp_dict_get_bytes_allow_empty(PyObject *dict, const char *key, uint8_t **data,
			  int *length)
{
	PyObject *obj;

	if (!PyDict_Check(dict)) {
		PyErr_SetString(PyExc_TypeError, "arg is not a dict");
		return -1;
	}

	obj = PyDict_GetItemString(dict, key);
	if (obj == NULL) {
		PyErr_Format(PyExc_KeyError,
			     "Key: '%s' of type: bytes expected", key);
		return -1;
	}

	return pyosdp_parse_bytes(obj, data, length, true);
}

int pyosdp_dict_get_object(PyObject *dict, const char *key, PyObject **obj)
{
	if (!PyDict_Check(dict)) {
		PyErr_SetString(PyExc_TypeError, "arg is not a dict");
		return -1;
	}

	*obj = PyDict_GetItemString(dict, key);
	if (*obj == NULL)
		return -1;

	return 0;
}

/* --- Channel --- */

static int channel_read_callback(void *data, uint8_t *buf, int maxlen)
{
	Py_ssize_t len;
	PyObject *channel = data;
	uint8_t *tmp;

	PyObject *result = PyObject_CallMethod(channel, "read", "I", maxlen);

	if (!result || !PyBytes_Check(result))
		return -1;

	PyArg_Parse(result, "y#", &tmp, &len);
	if (len <= maxlen) {
		memcpy(buf, tmp, len);
	} else {
		PyErr_SetString(PyExc_TypeError,
				"read callback maxlen not respected");
		len = -1;
	}
	Py_DECREF(result);
	return len;
}

static int channel_write_callback(void *data, uint8_t *buf, int len)
{
	PyObject *channel = data;
	PyObject *byte_array;

	byte_array = Py_BuildValue("y#", buf, len);
	if (byte_array == NULL)
		return -1;

	PyObject *result = PyObject_CallMethod(channel, "write", "O", byte_array);
	if (!result || !PyLong_Check(result)) {
		Py_DECREF(byte_array);
		Py_XDECREF(result);
		return -1;
	}

	len = (int)PyLong_AsLong(result);
	Py_DECREF(byte_array);
	Py_DECREF(result);
	return len;
}

static void channel_flush_callback(void *data)
{
	PyObject *channel = data;
	PyObject *result;

	result = PyObject_CallMethod(channel, "flush", NULL);
	Py_XDECREF(result);
}

#ifdef OPT_OSDP_RX_ZERO_COPY
static int channel_read_packet_callback(void *data, const uint8_t **buf, int *max_len)
{
	Py_ssize_t len;
	PyObject *channel = data;
	PyObject *result;
	static uint8_t *packet_buf = NULL;

	result = PyObject_CallMethod(channel, "read_packet", NULL);

	/* No packet ready */
	if (result == Py_None || result == NULL) {
		Py_XDECREF(result);
		return -1;
	}

	if (!PyBytes_Check(result)) {
		Py_DECREF(result);
		return -1;
	}

	/* Store the Python bytes object in channel data for later release */
	PyArg_Parse(result, "y#", &packet_buf, &len);

	/* Keep the result alive until release_packet is called */
	PyObject_SetAttrString(channel, "_current_packet", result);

	*buf = packet_buf;
	*max_len = (int)len;

	return 0;
}

static void channel_release_packet_callback(void *data, const uint8_t *buf)
{
	PyObject *channel = data;
	PyObject *result, *packet_obj;

	ARG_UNUSED(buf);

	/* Release the stored packet object */
	packet_obj = PyObject_GetAttrString(channel, "_current_packet");
	if (packet_obj && packet_obj != Py_None) {
		Py_DECREF(packet_obj);
		PyObject_SetAttrString(channel, "_current_packet", Py_None);
	}

	result = PyObject_CallMethod(channel, "release_packet", "O", Py_None);
	Py_XDECREF(result);
}
#endif

void pyosdp_get_channel(PyObject *channel, struct osdp_channel *ops)
{
	int id = 0;
	PyObject *id_obj;

	id_obj = PyObject_GetAttrString(channel, "id");
	if (id_obj && PyLong_Check(id_obj)) {
		id = (int)PyLong_AsLong(id_obj);
	}

	ops->id = id;
	ops->send = channel_write_callback;
	ops->flush = channel_flush_callback;
	ops->data = channel;

#ifdef OPT_OSDP_RX_ZERO_COPY
	/* Check if channel has read_packet method */
	if (PyObject_HasAttrString(channel, "read_packet")) {
		ops->recv = NULL;
		ops->recv_pkt = channel_read_packet_callback;
		ops->release_pkt = channel_release_packet_callback;
	} else {
		ops->recv = channel_read_callback;
		ops->recv_pkt = NULL;
		ops->release_pkt = NULL;
	}
#else
	ops->recv = channel_read_callback;
#endif

	Py_INCREF(channel);
}

void pyosdp_add_error_context(PyObject *exc_type, const char *format, ...)
{
	PyObject *cause_exc, *new_exc;
	va_list args;
	char message[512];

	va_start(args, format);
	vsnprintf(message, sizeof(message), format, args);
	va_end(args);

	cause_exc = PyErr_GetRaisedException();
	if (cause_exc != NULL) {
		PyErr_SetString(exc_type, message);
		new_exc = PyErr_GetRaisedException();
		if (new_exc != NULL) {
			PyException_SetCause(new_exc, cause_exc);
			PyErr_SetRaisedException(new_exc);
		}
	} else {
		PyErr_SetString(exc_type, message);
	}
}