summaryrefslogtreecommitdiffstats
path: root/src/procqueue.c
blob: 78dee4d7ba752a3184c84529d0a8da6ba9962e7e (plain)
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
/* Processing Queue Management */

/*
 * This file is part of gapk (GSM Audio Pocket Knife).
 *
 * gapk is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * gapk 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gapk.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <talloc.h>

#include <osmocom/core/linuxlist.h>

#include <osmocom/gapk/procqueue.h>
#include <osmocom/gapk/logging.h>

/* Internal root talloc context */
extern TALLOC_CTX *gapk_root_ctx;

/* crate a new (empty) processing queue */
struct osmo_gapk_pq *
osmo_gapk_pq_create(const char *name)
{
	struct osmo_gapk_pq *pq;

	/* Allocate memory for a new processing queue */
	pq = talloc_zero(gapk_root_ctx, struct osmo_gapk_pq);
	if (!pq)
		return NULL;

	if (name != NULL) {
		/* Rename talloc context */
		talloc_set_name(pq, "struct osmo_gapk_pq '%s'", name);
		/* Set queue name */
		pq->name = name;
	}

	/* Init its list of items */
	INIT_LLIST_HEAD(&pq->items);

	return pq;
}

/*! destroy a processing queue, calls exit() callback of each item
 *  \param[in] pq Processing Queue to be destroyed */
void
osmo_gapk_pq_destroy(struct osmo_gapk_pq *pq)
{
	struct osmo_gapk_pq_item *item, *item_next;

	if (!pq)
		return;

	/* Iterate over all items in queue */
	llist_for_each_entry_safe(item, item_next, &pq->items, list) {
		/* Free output buffer memory */
		talloc_free(item->buf);

		/* Call exit handler if preset */
		if (item->exit)
			item->exit(item->state);

		/* Delete an item from list */
		llist_del(&item->list);
		talloc_free(item);
	}

	talloc_free(pq);
}

/*! allocate + add an item to a processing queue; return new item
 *  \param[in] pq Processing Queue to which item is added
 *  \returns new PQ item; NULL on error */
struct osmo_gapk_pq_item *
osmo_gapk_pq_add_item(struct osmo_gapk_pq *pq)
{
	struct osmo_gapk_pq_item *item;

	/* Allocate memory for a new item */
	item = talloc_zero(pq, struct osmo_gapk_pq_item);
	if (!item)
		return NULL;

	/* Add one to the end of a queue */
	llist_add_tail(&item->list, &pq->items);

	/* Increase the items count */
	pq->n_items++;

	return item;
}

/*! check a processing queue; make sure I/O data lengths are equal
 *  \param[in] pq     Make sure both source and sink are preset
 *  \param[in] strict Processing Queue to be checked
 *  \returns 0 on succcess; negative on error */
int
osmo_gapk_pq_check(struct osmo_gapk_pq *pq, int strict)
{
	struct osmo_gapk_pq_item *item_prev = NULL;
	struct osmo_gapk_pq_item *item;

	/* Make sure I/O data lengths are equal */
	llist_for_each_entry(item, &pq->items, list) {
		if (item_prev && item->len_in) {
			if (item->len_in != item_prev->len_out) {
				LOGPGAPK(LOGL_ERROR, "PQ '%s': item '%s/%s' requires "
					"input size %u, but previous '%s/%s' has %u\n",
					pq->name, item->cat_name, item->sub_name,
					item->len_in, item_prev->cat_name,
					item_prev->sub_name, item_prev->len_out);
				return -EINVAL;
			}
		}

		/* Save pointer to the previous item */
		item_prev = item;
	}

	if (strict) {
		/* Make sure the first item is a source */
		item = llist_first_entry(&pq->items,
			struct osmo_gapk_pq_item, list);
		if (item->type != OSMO_GAPK_ITEM_TYPE_SOURCE)
			goto src_sink_err;

		/* Make sure the last item is a sink */
		item = llist_last_entry(&pq->items,
			struct osmo_gapk_pq_item, list);
		if (item->type != OSMO_GAPK_ITEM_TYPE_SINK)
			goto src_sink_err;
	}

	return 0;

src_sink_err:
	LOGPGAPK(LOGL_ERROR, "PQ '%s': the first item should be a source, "
		"and the last one should be a sink\n", pq->name);
	return -EINVAL;
}

/*! prepare a processing queue; allocates buffers
 *  \param[in] pq Processing Queue to be prepared
 *  \returns 0 on succcess; negative on error */
int
osmo_gapk_pq_prepare(struct osmo_gapk_pq *pq)
{
	struct osmo_gapk_pq_item *item;

	/* Iterate over all items in queue */
	llist_for_each_entry(item, &pq->items, list) {
		/* The sink item doesn't require an output buffer */
		if (item->list.next != &pq->items) {
			unsigned int buf_size = item->len_out;

			/**
			 * Use maximum known buffer size
			 * for variable-length codec output
			 */
			if (!buf_size)
				buf_size = VAR_BUF_SIZE;

			/* Allocate memory for an output buffer */
			item->buf = talloc_named_const(item, buf_size, ".buffer");
			if (!item->buf)
				return -ENOMEM;
		}
	}

	return 0;
}

/*! execute a processing queue; iterate over processing elements
 *  \param[in] pq Processing Queue to be executed
 *  \returns 0 on success; negative on error (if any item returns negative) */
int
osmo_gapk_pq_execute(struct osmo_gapk_pq *pq)
{
	struct osmo_gapk_pq_item *item;
	unsigned int len_prev = 0;
	uint8_t *buf_prev = NULL;
	int rv;

	/* Iterate over all items in queue */
	llist_for_each_entry(item, &pq->items, list) {
		/* Call item's processing handler */
		rv = item->proc(item->state, item->buf, buf_prev, len_prev);
		if (rv < 0) {
			LOGPGAPK(LOGL_ERROR, "PQ '%s': execution aborted: "
				"item '%s/%s' returned %d\n", pq->name,
				item->cat_name, item->sub_name, rv);
			return rv;
		}

		buf_prev = item->buf;
		len_prev = rv;
	}

	return 0;
}

char *
osmo_gapk_pq_describe(struct osmo_gapk_pq *pq)
{
	struct osmo_gapk_pq_item *item;
	char *result = NULL;
	int i = 0;

	/* Nothing to describe */
	if (!pq->n_items)
		return NULL;

	/* Iterate over all items in queue */
	llist_for_each_entry(item, &pq->items, list) {
		result = talloc_asprintf_append(result, "%s/%s%s",
			item->cat_name, item->sub_name,
			++i < pq->n_items ? " -> " : "");
	}

	/* Change talloc context name */
	talloc_set_name_const(result, ".description");

	/* Change parent talloc context to pq */
	talloc_steal(pq, result);

	return result;
}