forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.c
More file actions
466 lines (383 loc) · 10.6 KB
/
Copy pathcommon.c
File metadata and controls
466 lines (383 loc) · 10.6 KB
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
/*-*- linux-c -*-*/
/*
* ALSA <-> SOF PCM I/O plugin
*
* Copyright (c) 2022 by Liam Girdwood <liam.r.girdwood@intel.com>
*
* 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 <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "common.h"
/*
* Timing
*/
void plug_timespec_add_ms(struct timespec *ts, unsigned long ms)
{
long ns;
long secs = ms / 1000;
/* get ms remainder */
ms = ms - (secs * 1000);
ns = ms * 1000000;
ts->tv_nsec += ns;
if (ts->tv_nsec > 1000000000) {
secs++;
ts->tv_nsec -= 1000000000;
}
ts->tv_sec += (secs + DEBUG_TV_SECS);
}
long plug_timespec_delta_ns(struct timespec *before, struct timespec *after)
{
long ns;
ns = (after->tv_sec - before->tv_sec) * 1000000000;
ns += after->tv_nsec - before->tv_nsec;
return ns;
}
static const char *suffix_name(const char *longname)
{
size_t len = strlen(longname);
int i = len;
/* longname name invalid */
if (len < 1) {
SNDERR("invalid topology long name\n");
return NULL;
}
/* find the last '/' in the longname topology path */
while (--i >= 0) {
if (longname[i] == '/') {
i += 1; /* skip / */
return &longname[i];
}
}
/* no / in topology path, so use full path */
return longname;
}
/*
* IPC
*
* POSIX message queues are used for interprocess IPC messaging.
*/
/*
* Initialise the IPC object.
*/
int plug_socket_path_init(struct plug_socket_desc *ipc, const char *tplg, const char *type,
int index)
{
snprintf(ipc->path, NAME_SIZE, "/tmp/%s-%s", tplg, type);
return 0;
}
/*
* Locking
*
* POSIX semaphores are used to block and synchronise audio between
* different threads and processes.
*/
/*
* Initialise the lock object.
*/
int plug_lock_init(struct plug_sem_desc *lock, const char *tplg, const char *type, int index)
{
const char *name = suffix_name(tplg);
if (!name)
return -EINVAL;
/* semaphores need the leading / */
snprintf(lock->name, NAME_SIZE, "/lock-%s-%s-%d", name, type, index);
return 0;
}
/*
* SHM
*
* Shared memory is used for audio data and audio context sharing between
* threads and processes.
*/
/*
* Initialise the SHM object.
*/
int plug_shm_init(struct plug_shm_desc *shm, const char *tplg, const char *type, int index)
{
const char *name = suffix_name(tplg);
if (!name)
return -EINVAL;
snprintf(shm->name, NAME_SIZE, "/shm-%s-%s-%d", name, type, index);
shm->size = SHM_SIZE;
return 0;
}
/*
* Open an existing shared memory region using the SHM object.
*/
int plug_shm_open(struct plug_shm_desc *shm)
{
struct stat status;
/* open SHM to be used for low latency position */
shm->fd = shm_open(shm->name, O_RDWR,
S_IRWXU | S_IRWXG);
if (shm->fd < 0) {
//SNDERR("failed to open SHM position %s: %s\n",
// shm->name, strerror(errno));
return -errno;
}
fstat(shm->fd, &status);
/* map it locally for context readback */
shm->addr = mmap(NULL, status.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm->fd, 0);
if (!shm->addr) {
SNDERR("failed to mmap SHM position%s: %s\n", shm->name, strerror(errno));
return -errno;
}
return 0;
}
static int plug_socket_timed_wait(struct plug_socket_desc *ipc, fd_set *fds, int timeout_ms,
bool write)
{
struct timeval timeout;
int result;
/* Set the timeout for select */
timeout.tv_sec = 0;
timeout.tv_usec = timeout_ms * 1000;
/* now wait for socket to be readable/writable */
if (write)
result = select(ipc->socket_fd + 1, NULL, fds, NULL, &timeout);
else
result = select(ipc->socket_fd + 1, fds, NULL, NULL, &timeout);
if (result == -1) {
SNDERR("error waiting for socket to be %s\n", write ? "writable" : "readable");
return result;
}
if (result == 0) {
SNDERR("IPC Socket %s timeout\n", write ? "write" : "read");
return -ETIMEDOUT;
}
/* socket ready for read/write */
if (FD_ISSET(ipc->socket_fd, fds))
return 0;
/* socket not ready */
return -EINVAL;
}
static int plug_ipc_cmd_tx(struct plug_socket_desc *ipc, void *msg, size_t len)
{
fd_set write_fds;
char mailbox[IPC3_MAX_MSG_SIZE];
ssize_t bytes;
int err;
if (len > IPC3_MAX_MSG_SIZE) {
SNDERR("ipc: message too big %d\n", len);
return -EINVAL;
}
memset(mailbox, 0, IPC3_MAX_MSG_SIZE);
memcpy(mailbox, msg, len);
/* Wait for the socket to be writable */
FD_ZERO(&write_fds);
FD_SET(ipc->socket_fd, &write_fds);
err = plug_socket_timed_wait(ipc, &write_fds, 20, true);
if (err < 0)
return err;
bytes = send(ipc->socket_fd, mailbox, IPC3_MAX_MSG_SIZE, 0);
if (bytes == -1) {
SNDERR("failed to send IPC message : %s\n", strerror(errno));
return -errno;
}
return bytes;
}
static int plug_ipc_cmd_rx(struct plug_socket_desc *ipc, char mailbox[IPC3_MAX_MSG_SIZE])
{
fd_set read_fds;
int err;
/* Wait for the socket to be readable */
FD_ZERO(&read_fds);
FD_SET(ipc->socket_fd, &read_fds);
err = plug_socket_timed_wait(ipc, &read_fds, 200, false);
if (err < 0)
return err;
memset(mailbox, 0, IPC3_MAX_MSG_SIZE);
return recv(ipc->socket_fd, mailbox, IPC3_MAX_MSG_SIZE, 0);
}
int plug_ipc_cmd_tx_rx(struct plug_socket_desc *ipc, void *msg, size_t len, void *reply,
size_t rlen)
{
char mailbox[IPC3_MAX_MSG_SIZE];
ssize_t bytes;
int err;
/* send IPC message */
bytes = plug_ipc_cmd_tx(ipc, msg, len);
if (bytes == -1) {
SNDERR("failed to send IPC message : %s\n", strerror(errno));
return -errno;
}
/* wait for response */
memset(mailbox, 0, IPC3_MAX_MSG_SIZE);
bytes = plug_ipc_cmd_rx(ipc, mailbox);
if (bytes == -1) {
SNDERR("failed to read IPC message reply %s\n", strerror(errno));
return -errno;
}
/* no response or connection lost, try to restablish connection */
if (bytes == 0) {
close(ipc->socket_fd);
err = plug_create_client_socket(ipc);
if (err < 0) {
SNDERR("failed to reestablish connection to SOF pipe IPC socket : %s",
strerror(err));
return -errno;
}
/* send IPC message again */
bytes = plug_ipc_cmd_tx(ipc, msg, len);
if (bytes == -1) {
SNDERR("failed to send IPC message : %s\n", strerror(errno));
return -errno;
}
/* wait for response */
memset(mailbox, 0, IPC3_MAX_MSG_SIZE);
bytes = plug_ipc_cmd_rx(ipc, mailbox);
if (bytes == -1) {
SNDERR("failed to read IPC message reply %s\n", strerror(errno));
return -errno;
}
/* connection lost again, quit now */
if (bytes == 0)
return -errno;
}
/* do the message work */
if (rlen && reply)
memcpy(reply, mailbox, rlen);
return 0;
}
void plug_ctl_ipc_message(struct ipc4_module_large_config *config, int param_id,
size_t size, uint32_t module_id, uint32_t instance_id,
uint32_t type)
{
config->primary.r.type = type;
config->primary.r.msg_tgt = SOF_IPC4_MESSAGE_TARGET_MODULE_MSG;
config->primary.r.rsp = SOF_IPC4_MESSAGE_DIR_MSG_REQUEST;
config->primary.r.module_id = module_id;
config->primary.r.instance_id = instance_id;
config->extension.r.data_off_size = size;
config->extension.r.large_param_id = param_id;
}
int plug_send_bytes_data(struct plug_socket_desc *ipc, uint32_t module_id, uint32_t instance_id,
struct sof_abi_hdr *abi)
{
struct ipc4_module_large_config config = {{ 0 }};
struct ipc4_message_reply reply;
void *msg;
int msg_size;
int err;
/* configure the IPC message */
plug_ctl_ipc_message(&config, abi->type, abi->size, module_id, instance_id,
SOF_IPC4_MOD_LARGE_CONFIG_SET);
config.extension.r.final_block = 1;
config.extension.r.init_block = 1;
/* allocate memory for IPC message */
msg_size = sizeof(config) + abi->size;
msg = calloc(msg_size, 1);
if (!msg)
return -ENOMEM;
/* set the IPC message data */
memcpy(msg, &config, sizeof(config));
memcpy(msg + sizeof(config), abi->data, abi->size);
/* send the message and check status */
err = plug_ipc_cmd_tx_rx(ipc, msg, msg_size, &reply, sizeof(reply));
free(msg);
if (err < 0) {
SNDERR("failed to send IPC to set bytes data\n");
return err;
}
if (reply.primary.r.status != IPC4_SUCCESS) {
SNDERR("IPC failed with status %d\n", reply.primary.r.status);
return -EINVAL;
}
return 0;
}
int plug_socket_create(struct plug_socket_desc *ipc)
{
struct sockaddr_un addr;
int sockfd;
/* Check if the socket path already exists */
if (access(ipc->path, F_OK) != -1) {
/* If it exists, remove it */
if (unlink(ipc->path) == -1) {
SNDERR("unlink previous socket file");
return -EINVAL;
}
}
/* Create the socket */
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
SNDERR("failed to create new socket");
return sockfd;
}
ipc->socket_fd = sockfd;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, ipc->path, sizeof(addr.sun_path) - 1);
/* Bind the socket to the address */
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
SNDERR("failed to bind new socket for IPC path\n");
close(sockfd);
return -EINVAL;
}
if (listen(sockfd, MAX_IPC_CLIENTS) == -1) {
SNDERR("failed to listen on socket for IPC\n");
return -EINVAL;
}
return 0;
}
static int set_socket_nonblocking(int sockfd)
{
int flags = fcntl(sockfd, F_GETFL, 0);
if (flags == -1) {
SNDERR("fcntl(F_GETFL) failed");
return -EINVAL;
}
flags |= O_NONBLOCK;
if (fcntl(sockfd, F_SETFL, flags) == -1) {
SNDERR("fcntl(F_SETFL) failed");
return -EINVAL;
}
return 0;
}
int plug_create_client_socket(struct plug_socket_desc *ipc)
{
struct sockaddr_un addr;
int sockfd;
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
SNDERR("error: failed to create sof-pipe IPC socket\n");
return sockfd;
}
if (set_socket_nonblocking(sockfd) < 0) {
close(sockfd);
return -EINVAL;
}
ipc->socket_fd = sockfd;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, ipc->path, sizeof(addr.sun_path) - 1);
/* Connect to the server (non-blocking) */
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
if (errno != EINPROGRESS) {
SNDERR("failed to connect to ipc socket");
return -errno;
}
}
return sockfd;
}