summaryrefslogtreecommitdiffstats
path: root/src/modules/module-avb/mmrp.c
blob: 022aea8b3c25334fb55e03de848d7a1995cd624f (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
/* AVB support
 *
 * Copyright © 2022 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <unistd.h>

#include <pipewire/pipewire.h>

#include "utils.h"
#include "mmrp.h"

static const uint8_t mmrp_mac[6] = AVB_MMRP_MAC;

struct attr {
	struct avb_mmrp_attribute attr;
	struct spa_list link;
};

struct mmrp {
	struct server *server;
	struct spa_hook server_listener;

	struct spa_source *source;

	struct spa_list attributes;
};

static bool mmrp_check_header(void *data, const void *hdr, size_t *hdr_size, bool *has_params)
{
	const struct avb_packet_mmrp_msg *msg = hdr;
	uint8_t attr_type = msg->attribute_type;

	if (!AVB_MMRP_ATTRIBUTE_TYPE_VALID(attr_type))
		return false;

	*hdr_size = sizeof(*msg);
	*has_params = false;
	return true;
}

static int mmrp_attr_event(void *data, uint64_t now, uint8_t attribute_type, uint8_t event)
{
	struct mmrp *mmrp = data;
	struct attr *a;
	spa_list_for_each(a, &mmrp->attributes, link)
		if (a->attr.type == attribute_type)
			avb_mrp_attribute_update_state(a->attr.mrp, now, event);
	return 0;
}

static void debug_service_requirement(const struct avb_packet_mmrp_service_requirement *t)
{
	char buf[128];
	pw_log_info("service requirement");
	pw_log_info(" %s", avb_utils_format_addr(buf, sizeof(buf), t->addr));
}

static int process_service_requirement(struct mmrp *mmrp, uint64_t now, uint8_t attr_type,
		const void *m, uint8_t event, uint8_t param, int num)
{
	const struct avb_packet_mmrp_service_requirement *t = m;
	struct attr *a;

	debug_service_requirement(t);

	spa_list_for_each(a, &mmrp->attributes, link)
		if (a->attr.type == attr_type &&
		    memcmp(a->attr.attr.service_requirement.addr, t->addr, 6) == 0)
			avb_mrp_attribute_rx_event(a->attr.mrp, now, event);
	return 0;
}

static void debug_process_mac(const struct avb_packet_mmrp_mac *t)
{
	char buf[128];
	pw_log_info("mac");
	pw_log_info(" %s", avb_utils_format_addr(buf, sizeof(buf), t->addr));
}

static int process_mac(struct mmrp *mmrp, uint64_t now, uint8_t attr_type,
		const void *m, uint8_t event, uint8_t param, int num)
{
	const struct avb_packet_mmrp_mac *t = m;
	struct attr *a;

	debug_process_mac(t);

	spa_list_for_each(a, &mmrp->attributes, link)
		if (a->attr.type == attr_type &&
		    memcmp(a->attr.attr.mac.addr, t->addr, 6) == 0)
			avb_mrp_attribute_rx_event(a->attr.mrp, now, event);
	return 0;
}

static const struct {
	int (*dispatch) (struct mmrp *mmrp, uint64_t now, uint8_t attr_type,
			const void *m, uint8_t event, uint8_t param, int num);
} dispatch[] = {
	[AVB_MMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT] = { process_service_requirement, },
	[AVB_MMRP_ATTRIBUTE_TYPE_MAC] = { process_mac, },
};

static int mmrp_process(void *data, uint64_t now, uint8_t attribute_type, const void *value,
			uint8_t event, uint8_t param, int index)
{
	struct mmrp *mmrp = data;
	return dispatch[attribute_type].dispatch(mmrp, now,
				attribute_type, value, event, param, index);
}

static const struct avb_mrp_parse_info info = {
	AVB_VERSION_MRP_PARSE_INFO,
	.check_header = mmrp_check_header,
	.attr_event = mmrp_attr_event,
	.process = mmrp_process,
};

static int mmrp_message(struct mmrp *mmrp, uint64_t now, const void *message, int len)
{
	pw_log_debug("MMRP");
	return avb_mrp_parse_packet(mmrp->server->mrp,
			now, message, len, &info, mmrp);
}

static void on_socket_data(void *data, int fd, uint32_t mask)
{
	struct mmrp *mmrp = data;
	struct timespec now;

	if (mask & SPA_IO_IN) {
		int len;
		uint8_t buffer[2048];

		len = recv(fd, buffer, sizeof(buffer), 0);

		if (len < 0) {
			pw_log_warn("got recv error: %m");
		}
		else if (len < (int)sizeof(struct avb_packet_header)) {
			pw_log_warn("short packet received (%d < %d)", len,
					(int)sizeof(struct avb_packet_header));
		} else {
			clock_gettime(CLOCK_REALTIME, &now);
			mmrp_message(mmrp, SPA_TIMESPEC_TO_NSEC(&now), buffer, len);
		}
	}
}
static void mmrp_destroy(void *data)
{
	struct mmrp *mmrp = data;
	spa_hook_remove(&mmrp->server_listener);
	pw_loop_destroy_source(mmrp->server->impl->loop, mmrp->source);
	free(mmrp);
}

static const struct server_events server_events = {
	AVB_VERSION_SERVER_EVENTS,
	.destroy = mmrp_destroy,
};

struct avb_mmrp_attribute *avb_mmrp_attribute_new(struct avb_mmrp *m,
		uint8_t type)
{
	struct mmrp *mmrp = (struct mmrp*)m;
	struct avb_mrp_attribute *attr;
	struct attr *a;

	attr = avb_mrp_attribute_new(mmrp->server->mrp, sizeof(struct attr));

	a = attr->user_data;
	a->attr.mrp = attr;
	a->attr.type = type;
	spa_list_append(&mmrp->attributes, &a->link);

	return &a->attr;
}

struct avb_mmrp *avb_mmrp_register(struct server *server)
{
	struct mmrp *mmrp;
	int fd, res;

	fd = avb_server_make_socket(server, AVB_MMRP_ETH, mmrp_mac);
	if (fd < 0) {
		errno = -fd;
		return NULL;
	}
	mmrp = calloc(1, sizeof(*mmrp));
	if (mmrp == NULL) {
		res = -errno;
		goto error_close;
	}

	mmrp->server = server;
	spa_list_init(&mmrp->attributes);

	mmrp->source = pw_loop_add_io(server->impl->loop, fd, SPA_IO_IN, true, on_socket_data, mmrp);
	if (mmrp->source == NULL) {
		res = -errno;
		pw_log_error("mmrp %p: can't create mmrp source: %m", mmrp);
		goto error_no_source;
	}
	avdecc_server_add_listener(server, &mmrp->server_listener, &server_events, mmrp);

	return (struct avb_mmrp*)mmrp;

error_no_source:
	free(mmrp);
error_close:
	close(fd);
	errno = -res;
	return NULL;
}