summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/alsa/conf.c
blob: 89e3656a042d7655bb68af4adae442c5d13c7ab3 (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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// SPDX-License-Identifier: GPL-2.0
//
// kselftest configuration helpers for the hw specific configuration
//
// Original author: Jaroslav Kysela <perex@perex.cz>
// Copyright (c) 2022 Red Hat Inc.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <assert.h>
#include <dirent.h>
#include <regex.h>
#include <sys/stat.h>

#include "../kselftest.h"
#include "alsa-local.h"

#define SYSFS_ROOT "/sys"

struct card_cfg_data *conf_cards;

static const char *alsa_config =
"ctl.hw {\n"
"	@args [ CARD ]\n"
"	@args.CARD.type string\n"
"	type hw\n"
"	card $CARD\n"
"}\n"
"pcm.hw {\n"
"	@args [ CARD DEV SUBDEV ]\n"
"	@args.CARD.type string\n"
"	@args.DEV.type integer\n"
"	@args.SUBDEV.type integer\n"
"	type hw\n"
"	card $CARD\n"
"	device $DEV\n"
"	subdevice $SUBDEV\n"
"}\n"
;

#ifdef SND_LIB_VER
#if SND_LIB_VERSION >= SND_LIB_VER(1, 2, 6)
#define LIB_HAS_LOAD_STRING
#endif
#endif

#ifndef LIB_HAS_LOAD_STRING
static int snd_config_load_string(snd_config_t **config, const char *s,
				  size_t size)
{
	snd_input_t *input;
	snd_config_t *dst;
	int err;

	assert(config && s);
	if (size == 0)
		size = strlen(s);
	err = snd_input_buffer_open(&input, s, size);
	if (err < 0)
		return err;
	err = snd_config_top(&dst);
	if (err < 0) {
		snd_input_close(input);
		return err;
	}
	err = snd_config_load(dst, input);
	snd_input_close(input);
	if (err < 0) {
		snd_config_delete(dst);
		return err;
	}
	*config = dst;
	return 0;
}
#endif

snd_config_t *get_alsalib_config(void)
{
	snd_config_t *config;
	int err;

	err = snd_config_load_string(&config, alsa_config, strlen(alsa_config));
	if (err < 0) {
		ksft_print_msg("Unable to parse custom alsa-lib configuration: %s\n",
			       snd_strerror(err));
		ksft_exit_fail();
	}
	return config;
}

static struct card_cfg_data *conf_data_by_card(int card, bool msg)
{
	struct card_cfg_data *conf;

	for (conf = conf_cards; conf; conf = conf->next) {
		if (conf->card == card) {
			if (msg)
				ksft_print_msg("using hw card config %s for card %d\n",
					       conf->filename, card);
			return conf;
		}
	}
	return NULL;
}

static int dump_config_tree(snd_config_t *top)
{
	snd_output_t *out;
	int err;

	err = snd_output_stdio_attach(&out, stdout, 0);
	if (err < 0)
		ksft_exit_fail_msg("stdout attach\n");
	if (snd_config_save(top, out))
		ksft_exit_fail_msg("config save\n");
	snd_output_close(out);
}

snd_config_t *conf_load_from_file(const char *filename)
{
	snd_config_t *dst;
	snd_input_t *input;
	int err;

	err = snd_input_stdio_open(&input, filename, "r");
	if (err < 0)
		ksft_exit_fail_msg("Unable to parse filename %s\n", filename);
	err = snd_config_top(&dst);
	if (err < 0)
		ksft_exit_fail_msg("Out of memory\n");
	err = snd_config_load(dst, input);
	snd_input_close(input);
	if (err < 0)
		ksft_exit_fail_msg("Unable to parse filename %s\n", filename);
	return dst;
}

static char *sysfs_get(const char *sysfs_root, const char *id)
{
	char path[PATH_MAX], link[PATH_MAX + 1];
	struct stat sb;
	ssize_t len;
	char *e;
	int fd;

	if (id[0] == '/')
		id++;
	snprintf(path, sizeof(path), "%s/%s", sysfs_root, id);
	if (lstat(path, &sb) != 0)
		return NULL;
	if (S_ISLNK(sb.st_mode)) {
		len = readlink(path, link, sizeof(link) - 1);
		if (len <= 0) {
			ksft_exit_fail_msg("sysfs: cannot read link '%s': %s\n",
					   path, strerror(errno));
			return NULL;
		}
		link[len] = '\0';
		e = strrchr(link, '/');
		if (e)
			return strdup(e + 1);
		return NULL;
	}
	if (S_ISDIR(sb.st_mode))
		return NULL;
	if ((sb.st_mode & S_IRUSR) == 0)
		return NULL;

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		if (errno == ENOENT)
			return NULL;
		ksft_exit_fail_msg("sysfs: open failed for '%s': %s\n",
				   path, strerror(errno));
	}
	len = read(fd, path, sizeof(path)-1);
	close(fd);
	if (len < 0)
		ksft_exit_fail_msg("sysfs: unable to read value '%s': %s\n",
				   path, strerror(errno));
	while (len > 0 && path[len-1] == '\n')
		len--;
	path[len] = '\0';
	e = strdup(path);
	if (e == NULL)
		ksft_exit_fail_msg("Out of memory\n");
	return e;
}

static bool sysfs_match(const char *sysfs_root, snd_config_t *config)
{
	snd_config_t *node, *path_config, *regex_config;
	snd_config_iterator_t i, next;
	const char *path_string, *regex_string, *v;
	regex_t re;
	regmatch_t match[1];
	int iter = 0, ret;

	snd_config_for_each(i, next, config) {
		node = snd_config_iterator_entry(i);
		if (snd_config_search(node, "path", &path_config))
			ksft_exit_fail_msg("Missing path field in the sysfs block\n");
		if (snd_config_search(node, "regex", &regex_config))
			ksft_exit_fail_msg("Missing regex field in the sysfs block\n");
		if (snd_config_get_string(path_config, &path_string))
			ksft_exit_fail_msg("Path field in the sysfs block is not a string\n");
		if (snd_config_get_string(regex_config, &regex_string))
			ksft_exit_fail_msg("Regex field in the sysfs block is not a string\n");
		iter++;
		v = sysfs_get(sysfs_root, path_string);
		if (!v)
			return false;
		if (regcomp(&re, regex_string, REG_EXTENDED))
			ksft_exit_fail_msg("Wrong regex '%s'\n", regex_string);
		ret = regexec(&re, v, 1, match, 0);
		regfree(&re);
		if (ret)
			return false;
	}
	return iter > 0;
}

static void assign_card_config(int card, const char *sysfs_card_root)
{
	struct card_cfg_data *data;
	snd_config_t *sysfs_card_config;

	for (data = conf_cards; data; data = data->next) {
		snd_config_search(data->config, "sysfs", &sysfs_card_config);
		if (!sysfs_match(sysfs_card_root, sysfs_card_config))
			continue;

		data->card = card;
		break;
	}
}

static void assign_card_configs(void)
{
	char fn[128];
	int card;

	for (card = 0; card < 32; card++) {
		snprintf(fn, sizeof(fn), "%s/class/sound/card%d", SYSFS_ROOT, card);
		if (access(fn, R_OK) == 0)
			assign_card_config(card, fn);
	}
}

static int filename_filter(const struct dirent *dirent)
{
	size_t flen;

	if (dirent == NULL)
		return 0;
	if (dirent->d_type == DT_DIR)
		return 0;
	flen = strlen(dirent->d_name);
	if (flen <= 5)
		return 0;
	if (strncmp(&dirent->d_name[flen-5], ".conf", 5) == 0)
		return 1;
	return 0;
}

static bool match_config(const char *filename)
{
	struct card_cfg_data *data;
	snd_config_t *config, *sysfs_config, *card_config, *sysfs_card_config, *node;
	snd_config_iterator_t i, next;

	config = conf_load_from_file(filename);
	if (snd_config_search(config, "sysfs", &sysfs_config) ||
	    snd_config_get_type(sysfs_config) != SND_CONFIG_TYPE_COMPOUND)
		ksft_exit_fail_msg("Missing global sysfs block in filename %s\n", filename);
	if (snd_config_search(config, "card", &card_config) ||
	    snd_config_get_type(card_config) != SND_CONFIG_TYPE_COMPOUND)
		ksft_exit_fail_msg("Missing global card block in filename %s\n", filename);
	if (!sysfs_match(SYSFS_ROOT, sysfs_config))
		return false;
	snd_config_for_each(i, next, card_config) {
		node = snd_config_iterator_entry(i);
		if (snd_config_search(node, "sysfs", &sysfs_card_config) ||
		    snd_config_get_type(sysfs_card_config) != SND_CONFIG_TYPE_COMPOUND)
			ksft_exit_fail_msg("Missing card sysfs block in filename %s\n", filename);

		data = malloc(sizeof(*data));
		if (!data)
			ksft_exit_fail_msg("Out of memory\n");
		data->filename = filename;
		data->config = node;
		data->card = -1;
		if (snd_config_get_id(node, &data->config_id))
			ksft_exit_fail_msg("snd_config_get_id failed for card\n");
		data->next = conf_cards;
		conf_cards = data;
	}
	return true;
}

void conf_load(void)
{
	const char *fn = "conf.d";
	struct dirent **namelist;
	int n, j;

	n = scandir(fn, &namelist, filename_filter, alphasort);
	if (n < 0)
		ksft_exit_fail_msg("scandir: %s\n", strerror(errno));
	for (j = 0; j < n; j++) {
		size_t sl = strlen(fn) + strlen(namelist[j]->d_name) + 2;
		char *filename = malloc(sl);
		if (filename == NULL)
			ksft_exit_fail_msg("Out of memory\n");
		sprintf(filename, "%s/%s", fn, namelist[j]->d_name);
		if (match_config(filename))
			filename = NULL;
		free(filename);
		free(namelist[j]);
	}
	free(namelist);

	assign_card_configs();
}

void conf_free(void)
{
	struct card_cfg_data *conf;

	while (conf_cards) {
		conf = conf_cards;
		conf_cards = conf->next;
		snd_config_delete(conf->config);
	}
}

snd_config_t *conf_by_card(int card)
{
	struct card_cfg_data *conf;

	conf = conf_data_by_card(card, true);
	if (conf)
		return conf->config;
	return NULL;
}

static int conf_get_by_keys(snd_config_t *root, const char *key1,
			    const char *key2, snd_config_t **result)
{
	int ret;

	if (key1) {
		ret = snd_config_search(root, key1, &root);
		if (ret != -ENOENT && ret < 0)
			return ret;
	}
	if (key2)
		ret = snd_config_search(root, key2, &root);
	if (ret >= 0)
		*result = root;
	return ret;
}

snd_config_t *conf_get_subtree(snd_config_t *root, const char *key1, const char *key2)
{
	int ret;

	if (!root)
		return NULL;
	ret = conf_get_by_keys(root, key1, key2, &root);
	if (ret == -ENOENT)
		return NULL;
	if (ret < 0)
		ksft_exit_fail_msg("key '%s'.'%s' search error: %s\n", key1, key2, snd_strerror(ret));
	return root;
}

int conf_get_count(snd_config_t *root, const char *key1, const char *key2)
{
	snd_config_t *cfg;
	snd_config_iterator_t i, next;
	int count, ret;

	if (!root)
		return -1;
	ret = conf_get_by_keys(root, key1, key2, &cfg);
	if (ret == -ENOENT)
		return -1;
	if (ret < 0)
		ksft_exit_fail_msg("key '%s'.'%s' search error: %s\n", key1, key2, snd_strerror(ret));
	if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND)
		ksft_exit_fail_msg("key '%s'.'%s' is not a compound\n", key1, key2);
	count = 0;
	snd_config_for_each(i, next, cfg)
		count++;
	return count;
}

const char *conf_get_string(snd_config_t *root, const char *key1, const char *key2, const char *def)
{
	snd_config_t *cfg;
	const char *s;
	int ret;

	if (!root)
		return def;
	ret = conf_get_by_keys(root, key1, key2, &cfg);
	if (ret == -ENOENT)
		return def;
	if (ret < 0)
		ksft_exit_fail_msg("key '%s'.'%s' search error: %s\n", key1, key2, snd_strerror(ret));
	if (snd_config_get_string(cfg, &s))
		ksft_exit_fail_msg("key '%s'.'%s' is not a string\n", key1, key2);
	return s;
}

long conf_get_long(snd_config_t *root, const char *key1, const char *key2, long def)
{
	snd_config_t *cfg;
	long l;
	int ret;

	if (!root)
		return def;
	ret = conf_get_by_keys(root, key1, key2, &cfg);
	if (ret == -ENOENT)
		return def;
	if (ret < 0)
		ksft_exit_fail_msg("key '%s'.'%s' search error: %s\n", key1, key2, snd_strerror(ret));
	if (snd_config_get_integer(cfg, &l))
		ksft_exit_fail_msg("key '%s'.'%s' is not an integer\n", key1, key2);
	return l;
}

int conf_get_bool(snd_config_t *root, const char *key1, const char *key2, int def)
{
	snd_config_t *cfg;
	int ret;

	if (!root)
		return def;
	ret = conf_get_by_keys(root, key1, key2, &cfg);
	if (ret == -ENOENT)
		return def;
	if (ret < 0)
		ksft_exit_fail_msg("key '%s'.'%s' search error: %s\n", key1, key2, snd_strerror(ret));
	ret = snd_config_get_bool(cfg);
	if (ret < 0)
		ksft_exit_fail_msg("key '%s'.'%s' is not an bool\n", key1, key2);
	return !!ret;
}

void conf_get_string_array(snd_config_t *root, const char *key1, const char *key2,
			   const char **array, int array_size, const char *def)
{
	snd_config_t *cfg;
	char buf[16];
	int ret, index;

	ret = conf_get_by_keys(root, key1, key2, &cfg);
	if (ret == -ENOENT)
		cfg = NULL;
	else if (ret < 0)
		ksft_exit_fail_msg("key '%s'.'%s' search error: %s\n", key1, key2, snd_strerror(ret));
	for (index = 0; index < array_size; index++) {
		if (cfg == NULL) {
			array[index] = def;
		} else {
			sprintf(buf, "%i", index);
			array[index] = conf_get_string(cfg, buf, NULL, def);
		}
	}
}