summaryrefslogtreecommitdiff
path: root/src/control/eld.c
blob: 1e161eb1d271fae7f4cd3822aa7e90fc56d1e8d7 (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
/**
 * \file control/eld.c
 * \brief ELD decoder
 * \author Jaroslav Kysela <perex@perex>
 * \date 2022
 */
/*
 *  Control Interface - Decode ELD
 *
 *  Copyright (c) 2022 Jaroslav Kysela <perex@perex.cz>
 *
 *
 *   This library 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; either version 2.1 of
 *   the License, or (at your option) any later version.
 *
 *   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 library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "control_local.h"

static void __fill_eld_ctl_id(snd_ctl_elem_id_t *id, int dev, int subdev)
{
	snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_PCM);
	snd_ctl_elem_id_set_name(id, "ELD");
	snd_ctl_elem_id_set_device(id, dev);
	snd_ctl_elem_id_set_index(id, subdev);
}

int __snd_pcm_info_eld_fixup(snd_pcm_info_t * info)
{
	snd_ctl_t *ctl;
	snd_ctl_elem_info_t cinfo = {0};
	snd_ctl_elem_value_t value = {0};
	unsigned char *eld;
	unsigned int l;
	char *s, c;
	int ret, valid;

	ret = snd_ctl_hw_open(&ctl, NULL, info->card, 0);
	if (ret < 0) {
		SYSMSG("Cannot open the associated CTL\n");
		return ret;
	}

	__fill_eld_ctl_id(&cinfo.id, info->device, info->subdevice);
	value.id = cinfo.id;
	ret = snd_ctl_elem_info(ctl, &cinfo);
	if (ret >= 0 && cinfo.type == SND_CTL_ELEM_TYPE_BYTES)
		ret = snd_ctl_elem_read(ctl, &value);
	snd_ctl_close(ctl);
	if (ret == -ENOENT || cinfo.type != SND_CTL_ELEM_TYPE_BYTES || cinfo.count == 0)
		return 0;
	if (ret < 0) {
		SYSMSG("Cannot read ELD\n");
		return ret;
	}
	/* decode connected HDMI device name */
	eld = value.value.bytes.data;
	if (cinfo.count < 20 || cinfo.count > 256)
		return -EIO;
	l = eld[4] & 0x1f;
	if (l == 0 || l > 16 || 20 + l > cinfo.count)
		return -EIO;
	s = alloca(l + 1);
	s[l] = '\0';
	/* sanitize */
	valid = 0;
	while (l > 0) {
		l--;
		c = eld[20 + l];
		if (c < ' ' || c >= 0x7f) {
			s[l] = ' ';
		} else {
			valid += !!isalnum(c);
			s[l] = c;
		}
	}
	if (valid > 3)
		snd_strlcpy((char *)info->name, s, sizeof(info->name));
	return 0;
}