| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libhound
|
|---|
| 30 | * @addtogroup audio
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Common USB functions.
|
|---|
| 35 | */
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <loc.h>
|
|---|
| 39 | #include <macros.h>
|
|---|
| 40 | #include <str.h>
|
|---|
| 41 | #include <stdbool.h>
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <types/common.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "protocol.h"
|
|---|
| 47 | #include "client.h"
|
|---|
| 48 | #include "server.h"
|
|---|
| 49 |
|
|---|
| 50 | enum ipc_methods {
|
|---|
| 51 | /** Create new context representation on the server side */
|
|---|
| 52 | IPC_M_HOUND_CONTEXT_REGISTER = IPC_FIRST_USER_METHOD,
|
|---|
| 53 | /** Release existing context representation on the server side */
|
|---|
| 54 | IPC_M_HOUND_CONTEXT_UNREGISTER,
|
|---|
| 55 | /** Request list of objects */
|
|---|
| 56 | IPC_M_HOUND_GET_LIST,
|
|---|
| 57 | /** Create new connection */
|
|---|
| 58 | IPC_M_HOUND_CONNECT,
|
|---|
| 59 | /** Destroy connection */
|
|---|
| 60 | IPC_M_HOUND_DISCONNECT,
|
|---|
| 61 | /** Switch IPC pipe to stream mode */
|
|---|
| 62 | IPC_M_HOUND_STREAM_ENTER,
|
|---|
| 63 | /** Switch IPC pipe back to general mode */
|
|---|
| 64 | IPC_M_HOUND_STREAM_EXIT,
|
|---|
| 65 | /** Wait until there is no data in the stream */
|
|---|
| 66 | IPC_M_HOUND_STREAM_DRAIN,
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | /** PCM format conversion helper structure */
|
|---|
| 71 | typedef union {
|
|---|
| 72 | struct {
|
|---|
| 73 | uint16_t rate;
|
|---|
| 74 | uint8_t channels;
|
|---|
| 75 | uint8_t format;
|
|---|
| 76 | } __attribute__((packed)) f;
|
|---|
| 77 | sysarg_t arg;
|
|---|
| 78 | } format_convert_t;
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | /****
|
|---|
| 82 | * CLIENT
|
|---|
| 83 | ****/
|
|---|
| 84 |
|
|---|
| 85 | /** Well defined service name */
|
|---|
| 86 | const char *HOUND_SERVICE = "audio/hound";
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Start a new audio session.
|
|---|
| 90 | * @param service Named service typically 'HOUND_SERVICE' constant.
|
|---|
| 91 | * @return Valid session on success, NULL on failure.
|
|---|
| 92 | */
|
|---|
| 93 | hound_sess_t *hound_service_connect(const char *service)
|
|---|
| 94 | {
|
|---|
| 95 | service_id_t id = 0;
|
|---|
| 96 | const errno_t ret =
|
|---|
| 97 | loc_service_get_id(service, &id, IPC_FLAG_BLOCKING);
|
|---|
| 98 | if (ret != EOK)
|
|---|
| 99 | return NULL;
|
|---|
| 100 | return loc_service_connect(id, INTERFACE_HOUND, IPC_FLAG_BLOCKING);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * End an existing audio session.
|
|---|
| 105 | * @param sess The session.
|
|---|
| 106 | */
|
|---|
| 107 | void hound_service_disconnect(hound_sess_t *sess)
|
|---|
| 108 | {
|
|---|
| 109 | if (sess)
|
|---|
| 110 | async_hangup(sess);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Register a named application context to the audio server.
|
|---|
| 115 | * @param sess Valid audio session.
|
|---|
| 116 | * @param name Valid string identifier
|
|---|
| 117 | * @param record True if the application context wishes to receive data.
|
|---|
| 118 | *
|
|---|
| 119 | * @param[out] id Return context ID.
|
|---|
| 120 | *
|
|---|
| 121 | * @return EOK on success, Error code on failure.
|
|---|
| 122 | */
|
|---|
| 123 | errno_t hound_service_register_context(hound_sess_t *sess,
|
|---|
| 124 | const char *name, bool record, hound_context_id_t *id)
|
|---|
| 125 | {
|
|---|
| 126 | assert(sess);
|
|---|
| 127 | assert(name);
|
|---|
| 128 | ipc_call_t call;
|
|---|
| 129 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 130 | aid_t mid =
|
|---|
| 131 | async_send_1(exch, IPC_M_HOUND_CONTEXT_REGISTER, record, &call);
|
|---|
| 132 | errno_t ret = mid ? EOK : EPARTY;
|
|---|
| 133 |
|
|---|
| 134 | if (ret == EOK)
|
|---|
| 135 | ret = async_data_write_start(exch, name, str_size(name));
|
|---|
| 136 | else
|
|---|
| 137 | async_forget(mid);
|
|---|
| 138 |
|
|---|
| 139 | if (ret == EOK)
|
|---|
| 140 | async_wait_for(mid, &ret);
|
|---|
| 141 |
|
|---|
| 142 | async_exchange_end(exch);
|
|---|
| 143 | if (ret == EOK) {
|
|---|
| 144 | *id = (hound_context_id_t)IPC_GET_ARG1(call);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | return ret;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Remove application context from the server's list.
|
|---|
| 152 | * @param sess Valid audio session.
|
|---|
| 153 | * @param id Valid context id.
|
|---|
| 154 | * @return Error code.
|
|---|
| 155 | */
|
|---|
| 156 | errno_t hound_service_unregister_context(hound_sess_t *sess,
|
|---|
| 157 | hound_context_id_t id)
|
|---|
| 158 | {
|
|---|
| 159 | assert(sess);
|
|---|
| 160 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 161 | const errno_t ret = async_req_1_0(exch, IPC_M_HOUND_CONTEXT_UNREGISTER,
|
|---|
| 162 | CAP_HANDLE_RAW(id));
|
|---|
| 163 | async_exchange_end(exch);
|
|---|
| 164 | return ret;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 168 | * Retrieve a list of server side actors.
|
|---|
| 169 | * @param[in] sess Valid audio session.
|
|---|
| 170 | * @param[out] ids list of string identifiers.
|
|---|
| 171 | * @param[out] count Number of elements int the @p ids list.
|
|---|
| 172 | * @param[in] flags list requirements.
|
|---|
| 173 | * @param[in] connection name of target actor. Used only if the list should
|
|---|
| 174 | * contain connected actors.
|
|---|
| 175 | * @retval Error code.
|
|---|
| 176 | */
|
|---|
| 177 | errno_t hound_service_get_list(hound_sess_t *sess, char ***ids, size_t *count,
|
|---|
| 178 | int flags, const char *connection)
|
|---|
| 179 | {
|
|---|
| 180 | assert(sess);
|
|---|
| 181 | assert(ids);
|
|---|
| 182 | assert(count);
|
|---|
| 183 |
|
|---|
| 184 | if (connection && !(flags & HOUND_CONNECTED))
|
|---|
| 185 | return EINVAL;
|
|---|
| 186 |
|
|---|
| 187 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 188 | if (!exch)
|
|---|
| 189 | return ENOMEM;
|
|---|
| 190 |
|
|---|
| 191 | ipc_call_t res_call;
|
|---|
| 192 | aid_t mid = async_send_3(exch, IPC_M_HOUND_GET_LIST, flags, *count,
|
|---|
| 193 | connection != NULL, &res_call);
|
|---|
| 194 |
|
|---|
| 195 | errno_t ret = EOK;
|
|---|
| 196 | if (mid && connection)
|
|---|
| 197 | ret = async_data_write_start(exch, connection,
|
|---|
| 198 | str_size(connection));
|
|---|
| 199 |
|
|---|
| 200 | if (ret == EOK)
|
|---|
| 201 | async_wait_for(mid, &ret);
|
|---|
| 202 |
|
|---|
| 203 | if (ret != EOK) {
|
|---|
| 204 | async_exchange_end(exch);
|
|---|
| 205 | return ret;
|
|---|
| 206 | }
|
|---|
| 207 | unsigned name_count = IPC_GET_ARG1(res_call);
|
|---|
| 208 |
|
|---|
| 209 | /* Start receiving names */
|
|---|
| 210 | char **names = NULL;
|
|---|
| 211 | if (name_count) {
|
|---|
| 212 | size_t *sizes = calloc(name_count, sizeof(size_t));
|
|---|
| 213 | names = calloc(name_count, sizeof(char *));
|
|---|
| 214 | if (!names || !sizes)
|
|---|
| 215 | ret = ENOMEM;
|
|---|
| 216 |
|
|---|
| 217 | if (ret == EOK)
|
|---|
| 218 | ret = async_data_read_start(exch, sizes,
|
|---|
| 219 | name_count * sizeof(size_t));
|
|---|
| 220 | for (unsigned i = 0; i < name_count && ret == EOK; ++i) {
|
|---|
| 221 | char *name = malloc(sizes[i] + 1);
|
|---|
| 222 | if (name) {
|
|---|
| 223 | memset(name, 0, sizes[i] + 1);
|
|---|
| 224 | ret = async_data_read_start(exch, name, sizes[i]);
|
|---|
| 225 | names[i] = name;
|
|---|
| 226 | } else {
|
|---|
| 227 | ret = ENOMEM;
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 | free(sizes);
|
|---|
| 231 | }
|
|---|
| 232 | async_exchange_end(exch);
|
|---|
| 233 | if (ret != EOK) {
|
|---|
| 234 | for (unsigned i = 0; i < name_count; ++i)
|
|---|
| 235 | free(names[i]);
|
|---|
| 236 | free(names);
|
|---|
| 237 | } else {
|
|---|
| 238 | *ids = names;
|
|---|
| 239 | *count = name_count;
|
|---|
| 240 | }
|
|---|
| 241 | return ret;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /**
|
|---|
| 245 | * Create a new connection between a source and a sink.
|
|---|
| 246 | * @param sess Valid audio session.
|
|---|
| 247 | * @param source Source name, valid string.
|
|---|
| 248 | * @param sink Sink name, valid string.
|
|---|
| 249 | * @return Error code.
|
|---|
| 250 | */
|
|---|
| 251 | errno_t hound_service_connect_source_sink(hound_sess_t *sess, const char *source,
|
|---|
| 252 | const char *sink)
|
|---|
| 253 | {
|
|---|
| 254 | assert(sess);
|
|---|
| 255 | assert(source);
|
|---|
| 256 | assert(sink);
|
|---|
| 257 |
|
|---|
| 258 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 259 | if (!exch)
|
|---|
| 260 | return ENOMEM;
|
|---|
| 261 | ipc_call_t call;
|
|---|
| 262 | aid_t id = async_send_0(exch, IPC_M_HOUND_CONNECT, &call);
|
|---|
| 263 | errno_t ret = id ? EOK : EPARTY;
|
|---|
| 264 | if (ret == EOK)
|
|---|
| 265 | ret = async_data_write_start(exch, source, str_size(source));
|
|---|
| 266 | if (ret == EOK)
|
|---|
| 267 | ret = async_data_write_start(exch, sink, str_size(sink));
|
|---|
| 268 | async_wait_for(id, &ret);
|
|---|
| 269 | async_exchange_end(exch);
|
|---|
| 270 | return ret;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /**
|
|---|
| 274 | * Destroy an existing connection between a source and a sink.
|
|---|
| 275 | * @param sess Valid audio session.
|
|---|
| 276 | * @param source Source name, valid string.
|
|---|
| 277 | * @param sink Sink name, valid string.
|
|---|
| 278 | * @return Error code.
|
|---|
| 279 | */
|
|---|
| 280 | errno_t hound_service_disconnect_source_sink(hound_sess_t *sess, const char *source,
|
|---|
| 281 | const char *sink)
|
|---|
| 282 | {
|
|---|
| 283 | assert(sess);
|
|---|
| 284 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 285 | if (!exch)
|
|---|
| 286 | return ENOMEM;
|
|---|
| 287 | ipc_call_t call;
|
|---|
| 288 | aid_t id = async_send_0(exch, IPC_M_HOUND_DISCONNECT, &call);
|
|---|
| 289 | errno_t ret = id ? EOK : EPARTY;
|
|---|
| 290 | if (ret == EOK)
|
|---|
| 291 | ret = async_data_write_start(exch, source, str_size(source));
|
|---|
| 292 | if (ret == EOK)
|
|---|
| 293 | ret = async_data_write_start(exch, sink, str_size(sink));
|
|---|
| 294 | async_wait_for(id, &ret);
|
|---|
| 295 | async_exchange_end(exch);
|
|---|
| 296 | return ENOTSUP;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | /**
|
|---|
| 300 | * Switch IPC exchange to a STREAM mode.
|
|---|
| 301 | * @param exch IPC exchange.
|
|---|
| 302 | * @param id context id this stream should be associated with
|
|---|
| 303 | * @param flags set stream properties
|
|---|
| 304 | * @param format format of the new stream.
|
|---|
| 305 | * @param bsize size of the server side buffer.
|
|---|
| 306 | * @return Error code.
|
|---|
| 307 | */
|
|---|
| 308 | errno_t hound_service_stream_enter(async_exch_t *exch, hound_context_id_t id,
|
|---|
| 309 | int flags, pcm_format_t format, size_t bsize)
|
|---|
| 310 | {
|
|---|
| 311 | const format_convert_t c = { .f = {
|
|---|
| 312 | .channels = format.channels,
|
|---|
| 313 | .rate = format.sampling_rate / 100,
|
|---|
| 314 | .format = format.sample_format,
|
|---|
| 315 | }};
|
|---|
| 316 | return async_req_4_0(exch, IPC_M_HOUND_STREAM_ENTER, CAP_HANDLE_RAW(id),
|
|---|
| 317 | flags, c.arg, bsize);
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | /**
|
|---|
| 321 | * Destroy existing stream and return IPC exchange to general mode.
|
|---|
| 322 | * @param exch IPC exchange.
|
|---|
| 323 | * @return Error code.
|
|---|
| 324 | */
|
|---|
| 325 | errno_t hound_service_stream_exit(async_exch_t *exch)
|
|---|
| 326 | {
|
|---|
| 327 | return async_req_0_0(exch, IPC_M_HOUND_STREAM_EXIT);
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | /**
|
|---|
| 331 | * Wait until the server side buffer is empty.
|
|---|
| 332 | * @param exch IPC exchange.
|
|---|
| 333 | * @return Error code.
|
|---|
| 334 | */
|
|---|
| 335 | errno_t hound_service_stream_drain(async_exch_t *exch)
|
|---|
| 336 | {
|
|---|
| 337 | return async_req_0_0(exch, IPC_M_HOUND_STREAM_DRAIN);
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /**
|
|---|
| 341 | * Write audio data to a stream.
|
|---|
| 342 | * @param exch IPC exchange in STREAM MODE.
|
|---|
| 343 | * @param data Audio data buffer.
|
|---|
| 344 | * @size size of the buffer
|
|---|
| 345 | * @return Error code.
|
|---|
| 346 | */
|
|---|
| 347 | errno_t hound_service_stream_write(async_exch_t *exch, const void *data, size_t size)
|
|---|
| 348 | {
|
|---|
| 349 | return async_data_write_start(exch, data, size);
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | /**
|
|---|
| 353 | * Read data from a stream.
|
|---|
| 354 | * @param exch IPC exchange in STREAM MODE.
|
|---|
| 355 | * @param data Audio data buffer.
|
|---|
| 356 | * @size size of the buffer
|
|---|
| 357 | * @return Error code.
|
|---|
| 358 | */
|
|---|
| 359 | errno_t hound_service_stream_read(async_exch_t *exch, void *data, size_t size)
|
|---|
| 360 | {
|
|---|
| 361 | return async_data_read_start(exch, data, size);
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | /****
|
|---|
| 365 | * SERVER
|
|---|
| 366 | ****/
|
|---|
| 367 |
|
|---|
| 368 | static void hound_server_read_data(void *stream);
|
|---|
| 369 | static void hound_server_write_data(void *stream);
|
|---|
| 370 | static const hound_server_iface_t *server_iface;
|
|---|
| 371 |
|
|---|
| 372 | /**
|
|---|
| 373 | * Set hound server interface implementation.
|
|---|
| 374 | * @param iface Initialized Hound server interface.
|
|---|
| 375 | */
|
|---|
| 376 | void hound_service_set_server_iface(const hound_server_iface_t *iface)
|
|---|
| 377 | {
|
|---|
| 378 | server_iface = iface;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | /** Server side implementation of the hound protocol. IPC connection handler.
|
|---|
| 382 | *
|
|---|
| 383 | * @param icall_handle Initial call handle
|
|---|
| 384 | * @param icall Pointer to initial call structure.
|
|---|
| 385 | * @param arg (unused)
|
|---|
| 386 | */
|
|---|
| 387 | void hound_connection_handler(cap_call_handle_t icall_handle, ipc_call_t *icall,
|
|---|
| 388 | void *arg)
|
|---|
| 389 | {
|
|---|
| 390 | hound_context_id_t id;
|
|---|
| 391 | errno_t ret;
|
|---|
| 392 | int flags;
|
|---|
| 393 | void *source;
|
|---|
| 394 | void *sink;
|
|---|
| 395 |
|
|---|
| 396 | /* Accept connection if there is a valid iface*/
|
|---|
| 397 | if (server_iface) {
|
|---|
| 398 | async_answer_0(icall_handle, EOK);
|
|---|
| 399 | } else {
|
|---|
| 400 | async_answer_0(icall_handle, ENOTSUP);
|
|---|
| 401 | return;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | while (true) {
|
|---|
| 405 | ipc_call_t call;
|
|---|
| 406 | cap_call_handle_t chandle = async_get_call(&call);
|
|---|
| 407 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 408 | case IPC_M_HOUND_CONTEXT_REGISTER:
|
|---|
| 409 | /* check interface functions */
|
|---|
| 410 | if (!server_iface || !server_iface->add_context) {
|
|---|
| 411 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 412 | break;
|
|---|
| 413 | }
|
|---|
| 414 | bool record = IPC_GET_ARG1(call);
|
|---|
| 415 | void *name;
|
|---|
| 416 |
|
|---|
| 417 | /* Get context name */
|
|---|
| 418 | ret = async_data_write_accept(&name, true, 0, 0, 0, 0);
|
|---|
| 419 | if (ret != EOK) {
|
|---|
| 420 | async_answer_0(chandle, ret);
|
|---|
| 421 | break;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | id = 0;
|
|---|
| 425 | ret = server_iface->add_context(server_iface->server,
|
|---|
| 426 | &id, name, record);
|
|---|
| 427 | /** new context should create a copy */
|
|---|
| 428 | free(name);
|
|---|
| 429 | if (ret != EOK) {
|
|---|
| 430 | async_answer_0(chandle, ret);
|
|---|
| 431 | } else {
|
|---|
| 432 | async_answer_1(chandle, EOK, CAP_HANDLE_RAW(id));
|
|---|
| 433 | }
|
|---|
| 434 | break;
|
|---|
| 435 | case IPC_M_HOUND_CONTEXT_UNREGISTER:
|
|---|
| 436 | /* check interface functions */
|
|---|
| 437 | if (!server_iface || !server_iface->rem_context) {
|
|---|
| 438 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 439 | break;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | /* get id, 1st param */
|
|---|
| 443 | id = (cap_handle_t) IPC_GET_ARG1(call);
|
|---|
| 444 | ret = server_iface->rem_context(server_iface->server,
|
|---|
| 445 | id);
|
|---|
| 446 | async_answer_0(chandle, ret);
|
|---|
| 447 | break;
|
|---|
| 448 | case IPC_M_HOUND_GET_LIST:
|
|---|
| 449 | /* check interface functions */
|
|---|
| 450 | if (!server_iface || !server_iface->get_list) {
|
|---|
| 451 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 452 | break;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | char **list = NULL;
|
|---|
| 456 | flags = IPC_GET_ARG1(call);
|
|---|
| 457 | size_t count = IPC_GET_ARG2(call);
|
|---|
| 458 | const bool conn = IPC_GET_ARG3(call);
|
|---|
| 459 | char *conn_name = NULL;
|
|---|
| 460 | ret = EOK;
|
|---|
| 461 |
|
|---|
| 462 | /* get connected actor name if provided */
|
|---|
| 463 | if (conn)
|
|---|
| 464 | ret = async_data_write_accept(
|
|---|
| 465 | (void**)&conn_name, true, 0, 0, 0, 0);
|
|---|
| 466 |
|
|---|
| 467 | if (ret == EOK)
|
|---|
| 468 | ret = server_iface->get_list(
|
|---|
| 469 | server_iface->server, &list, &count,
|
|---|
| 470 | conn_name, flags);
|
|---|
| 471 | free(conn_name);
|
|---|
| 472 |
|
|---|
| 473 | /* Alloc string sizes array */
|
|---|
| 474 | size_t *sizes = NULL;
|
|---|
| 475 | if (count)
|
|---|
| 476 | sizes = calloc(count, sizeof(size_t));
|
|---|
| 477 | if (count && !sizes)
|
|---|
| 478 | ret = ENOMEM;
|
|---|
| 479 | async_answer_1(chandle, ret, count);
|
|---|
| 480 |
|
|---|
| 481 | /* We are done */
|
|---|
| 482 | if (count == 0 || ret != EOK)
|
|---|
| 483 | break;
|
|---|
| 484 |
|
|---|
| 485 | /* Prepare sizes table */
|
|---|
| 486 | for (unsigned i = 0; i < count; ++i)
|
|---|
| 487 | sizes[i] = str_size(list[i]);
|
|---|
| 488 |
|
|---|
| 489 | /* Send sizes table */
|
|---|
| 490 | cap_call_handle_t id;
|
|---|
| 491 | if (async_data_read_receive(&id, NULL)) {
|
|---|
| 492 | ret = async_data_read_finalize(id, sizes,
|
|---|
| 493 | count * sizeof(size_t));
|
|---|
| 494 | }
|
|---|
| 495 | free(sizes);
|
|---|
| 496 |
|
|---|
| 497 | /* Proceed to send names */
|
|---|
| 498 | for (unsigned i = 0; i < count; ++i) {
|
|---|
| 499 | size_t size = str_size(list[i]);
|
|---|
| 500 | cap_call_handle_t id;
|
|---|
| 501 | if (ret == EOK &&
|
|---|
| 502 | async_data_read_receive(&id, NULL)) {
|
|---|
| 503 | ret = async_data_read_finalize(id,
|
|---|
| 504 | list[i], size);
|
|---|
| 505 | }
|
|---|
| 506 | free(list[i]);
|
|---|
| 507 | }
|
|---|
| 508 | free(list);
|
|---|
| 509 | break;
|
|---|
| 510 | case IPC_M_HOUND_CONNECT:
|
|---|
| 511 | /* check interface functions */
|
|---|
| 512 | if (!server_iface || !server_iface->connect) {
|
|---|
| 513 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 514 | break;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | source = NULL;
|
|---|
| 518 | sink = NULL;
|
|---|
| 519 |
|
|---|
| 520 | /* read source name */
|
|---|
| 521 | ret = async_data_write_accept(&source, true, 0, 0, 0,
|
|---|
| 522 | 0);
|
|---|
| 523 | /* read sink name */
|
|---|
| 524 | if (ret == EOK)
|
|---|
| 525 | ret = async_data_write_accept(&sink,
|
|---|
| 526 | true, 0, 0, 0, 0);
|
|---|
| 527 |
|
|---|
| 528 | if (ret == EOK)
|
|---|
| 529 | ret = server_iface->connect(
|
|---|
| 530 | server_iface->server, source, sink);
|
|---|
| 531 | free(source);
|
|---|
| 532 | free(sink);
|
|---|
| 533 | async_answer_0(chandle, ret);
|
|---|
| 534 | break;
|
|---|
| 535 | case IPC_M_HOUND_DISCONNECT:
|
|---|
| 536 | /* check interface functions */
|
|---|
| 537 | if (!server_iface || !server_iface->disconnect) {
|
|---|
| 538 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 539 | break;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | source = NULL;
|
|---|
| 543 | sink = NULL;
|
|---|
| 544 |
|
|---|
| 545 | /* read source name */
|
|---|
| 546 | ret = async_data_write_accept(&source, true, 0, 0, 0,
|
|---|
| 547 | 0);
|
|---|
| 548 | /*read sink name */
|
|---|
| 549 | if (ret == EOK)
|
|---|
| 550 | ret = async_data_write_accept(&sink,
|
|---|
| 551 | true, 0, 0, 0, 0);
|
|---|
| 552 | if (ret == EOK)
|
|---|
| 553 | ret = server_iface->connect(
|
|---|
| 554 | server_iface->server, source, sink);
|
|---|
| 555 | free(source);
|
|---|
| 556 | free(sink);
|
|---|
| 557 | async_answer_0(chandle, ret);
|
|---|
| 558 | break;
|
|---|
| 559 | case IPC_M_HOUND_STREAM_ENTER:
|
|---|
| 560 | /* check interface functions */
|
|---|
| 561 | if (!server_iface || !server_iface->is_record_context
|
|---|
| 562 | || !server_iface->add_stream
|
|---|
| 563 | || !server_iface->rem_stream) {
|
|---|
| 564 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 565 | break;
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | /* get parameters */
|
|---|
| 569 | id = (cap_handle_t) IPC_GET_ARG1(call);
|
|---|
| 570 | flags = IPC_GET_ARG2(call);
|
|---|
| 571 | const format_convert_t c = {.arg = IPC_GET_ARG3(call)};
|
|---|
| 572 | const pcm_format_t f = {
|
|---|
| 573 | .sampling_rate = c.f.rate * 100,
|
|---|
| 574 | .channels = c.f.channels,
|
|---|
| 575 | .sample_format = c.f.format,
|
|---|
| 576 | };
|
|---|
| 577 | size_t bsize = IPC_GET_ARG4(call);
|
|---|
| 578 |
|
|---|
| 579 | void *stream;
|
|---|
| 580 | ret = server_iface->add_stream(server_iface->server,
|
|---|
| 581 | id, flags, f, bsize, &stream);
|
|---|
| 582 | if (ret != EOK) {
|
|---|
| 583 | async_answer_0(chandle, ret);
|
|---|
| 584 | break;
|
|---|
| 585 | }
|
|---|
| 586 | const bool rec = server_iface->is_record_context(
|
|---|
| 587 | server_iface->server, id);
|
|---|
| 588 | if (rec) {
|
|---|
| 589 | if(server_iface->stream_data_read) {
|
|---|
| 590 | async_answer_0(chandle, EOK);
|
|---|
| 591 | /* start answering read calls */
|
|---|
| 592 | hound_server_write_data(stream);
|
|---|
| 593 | server_iface->rem_stream(
|
|---|
| 594 | server_iface->server, stream);
|
|---|
| 595 | } else {
|
|---|
| 596 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 597 | }
|
|---|
| 598 | } else {
|
|---|
| 599 | if (server_iface->stream_data_write) {
|
|---|
| 600 | async_answer_0(chandle, EOK);
|
|---|
| 601 | /* accept write calls */
|
|---|
| 602 | hound_server_read_data(stream);
|
|---|
| 603 | server_iface->rem_stream(
|
|---|
| 604 | server_iface->server, stream);
|
|---|
| 605 | } else {
|
|---|
| 606 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 607 | }
|
|---|
| 608 | }
|
|---|
| 609 | break;
|
|---|
| 610 | case IPC_M_HOUND_STREAM_EXIT:
|
|---|
| 611 | case IPC_M_HOUND_STREAM_DRAIN:
|
|---|
| 612 | /* Stream exit/drain is only allowed in stream context*/
|
|---|
| 613 | async_answer_0(chandle, EINVAL);
|
|---|
| 614 | break;
|
|---|
| 615 | default:
|
|---|
| 616 | async_answer_0(chandle, ENOTSUP);
|
|---|
| 617 | return;
|
|---|
| 618 | }
|
|---|
| 619 | }
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | /**
|
|---|
| 623 | * Read data and push it to the stream.
|
|---|
| 624 | * @param stream target stream, will push data there.
|
|---|
| 625 | */
|
|---|
| 626 | static void hound_server_read_data(void *stream)
|
|---|
| 627 | {
|
|---|
| 628 | cap_call_handle_t chandle;
|
|---|
| 629 | ipc_call_t call;
|
|---|
| 630 | size_t size = 0;
|
|---|
| 631 | errno_t ret_answer = EOK;
|
|---|
| 632 | /* accept data write or drain */
|
|---|
| 633 | while (async_data_write_receive_call(&chandle, &call, &size)
|
|---|
| 634 | || (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN)) {
|
|---|
| 635 | /* check drain first */
|
|---|
| 636 | if (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN) {
|
|---|
| 637 | errno_t ret = ENOTSUP;
|
|---|
| 638 | if (server_iface->drain_stream)
|
|---|
| 639 | ret = server_iface->drain_stream(stream);
|
|---|
| 640 | async_answer_0(chandle, ret);
|
|---|
| 641 | continue;
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | /* there was an error last time */
|
|---|
| 645 | if (ret_answer != EOK) {
|
|---|
| 646 | async_answer_0(chandle, ret_answer);
|
|---|
| 647 | continue;
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | char *buffer = malloc(size);
|
|---|
| 651 | if (!buffer) {
|
|---|
| 652 | async_answer_0(chandle, ENOMEM);
|
|---|
| 653 | continue;
|
|---|
| 654 | }
|
|---|
| 655 | const errno_t ret = async_data_write_finalize(chandle, buffer, size);
|
|---|
| 656 | if (ret == EOK) {
|
|---|
| 657 | /* push data to stream */
|
|---|
| 658 | ret_answer = server_iface->stream_data_write(
|
|---|
| 659 | stream, buffer, size);
|
|---|
| 660 | }
|
|---|
| 661 | }
|
|---|
| 662 | const errno_t ret = IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_EXIT
|
|---|
| 663 | ? EOK : EINVAL;
|
|---|
| 664 |
|
|---|
| 665 | async_answer_0(chandle, ret);
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | /**
|
|---|
| 669 | * Accept reads and pull data from the stream.
|
|---|
| 670 | * @param stream target stream, will pull data from there.
|
|---|
| 671 | */
|
|---|
| 672 | static void hound_server_write_data(void *stream)
|
|---|
| 673 | {
|
|---|
| 674 |
|
|---|
| 675 | cap_call_handle_t chandle;
|
|---|
| 676 | ipc_call_t call;
|
|---|
| 677 | size_t size = 0;
|
|---|
| 678 | errno_t ret_answer = EOK;
|
|---|
| 679 | /* accept data read and drain */
|
|---|
| 680 | while (async_data_read_receive_call(&chandle, &call, &size)
|
|---|
| 681 | || (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN)) {
|
|---|
| 682 | /* drain does not make much sense but it is allowed */
|
|---|
| 683 | if (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN) {
|
|---|
| 684 | errno_t ret = ENOTSUP;
|
|---|
| 685 | if (server_iface->drain_stream)
|
|---|
| 686 | ret = server_iface->drain_stream(stream);
|
|---|
| 687 | async_answer_0(chandle, ret);
|
|---|
| 688 | continue;
|
|---|
| 689 | }
|
|---|
| 690 | /* there was an error last time */
|
|---|
| 691 | if (ret_answer != EOK) {
|
|---|
| 692 | async_answer_0(chandle, ret_answer);
|
|---|
| 693 | continue;
|
|---|
| 694 | }
|
|---|
| 695 | char *buffer = malloc(size);
|
|---|
| 696 | if (!buffer) {
|
|---|
| 697 | async_answer_0(chandle, ENOMEM);
|
|---|
| 698 | continue;
|
|---|
| 699 | }
|
|---|
| 700 | errno_t ret = server_iface->stream_data_read(stream, buffer, size);
|
|---|
| 701 | if (ret == EOK) {
|
|---|
| 702 | ret_answer =
|
|---|
| 703 | async_data_read_finalize(chandle, buffer, size);
|
|---|
| 704 | }
|
|---|
| 705 | }
|
|---|
| 706 | const errno_t ret = IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_EXIT
|
|---|
| 707 | ? EOK : EINVAL;
|
|---|
| 708 |
|
|---|
| 709 | async_answer_0(chandle, ret);
|
|---|
| 710 | }
|
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 | /***
|
|---|
| 714 | * SERVER SIDE
|
|---|
| 715 | ***/
|
|---|
| 716 |
|
|---|
| 717 | /**
|
|---|
| 718 | * Register new hound service to the location service.
|
|---|
| 719 | * @param[in] name server name
|
|---|
| 720 | * @param[out] id assigned service id.
|
|---|
| 721 | * @return Error code.
|
|---|
| 722 | */
|
|---|
| 723 | errno_t hound_server_register(const char *name, service_id_t *id)
|
|---|
| 724 | {
|
|---|
| 725 | if (!name || !id)
|
|---|
| 726 | return EINVAL;
|
|---|
| 727 |
|
|---|
| 728 | errno_t ret = loc_server_register(name);
|
|---|
| 729 | if (ret != EOK)
|
|---|
| 730 | return ret;
|
|---|
| 731 |
|
|---|
| 732 | return loc_service_register(HOUND_SERVICE, id);
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | /**
|
|---|
| 736 | * Unregister server from the location service.
|
|---|
| 737 | * @param id previously assigned service id.
|
|---|
| 738 | */
|
|---|
| 739 | void hound_server_unregister(service_id_t id)
|
|---|
| 740 | {
|
|---|
| 741 | loc_service_unregister(id);
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | /**
|
|---|
| 745 | * Set callback on device category change event.
|
|---|
| 746 | * @param cb Callback function.
|
|---|
| 747 | * @return Error code.
|
|---|
| 748 | */
|
|---|
| 749 | errno_t hound_server_set_device_change_callback(dev_change_callback_t cb)
|
|---|
| 750 | {
|
|---|
| 751 | return loc_register_cat_change_cb(cb);
|
|---|
| 752 | }
|
|---|
| 753 |
|
|---|
| 754 | /**
|
|---|
| 755 | * Walk through all device in the audio-pcm category.
|
|---|
| 756 | * @param callback Function to call on every device.
|
|---|
| 757 | * @return Error code.
|
|---|
| 758 | */
|
|---|
| 759 | errno_t hound_server_devices_iterate(device_callback_t callback)
|
|---|
| 760 | {
|
|---|
| 761 | if (!callback)
|
|---|
| 762 | return EINVAL;
|
|---|
| 763 | static bool resolved = false;
|
|---|
| 764 | static category_id_t cat_id = 0;
|
|---|
| 765 |
|
|---|
| 766 | if (!resolved) {
|
|---|
| 767 | const errno_t ret = loc_category_get_id("audio-pcm", &cat_id,
|
|---|
| 768 | IPC_FLAG_BLOCKING);
|
|---|
| 769 | if (ret != EOK)
|
|---|
| 770 | return ret;
|
|---|
| 771 | resolved = true;
|
|---|
| 772 | }
|
|---|
| 773 |
|
|---|
| 774 | service_id_t *svcs = NULL;
|
|---|
| 775 | size_t count = 0;
|
|---|
| 776 | const errno_t ret = loc_category_get_svcs(cat_id, &svcs, &count);
|
|---|
| 777 | if (ret != EOK)
|
|---|
| 778 | return ret;
|
|---|
| 779 |
|
|---|
| 780 | for (unsigned i = 0; i < count; ++i) {
|
|---|
| 781 | char *name = NULL;
|
|---|
| 782 | loc_service_get_name(svcs[i], &name);
|
|---|
| 783 | callback(svcs[i], name);
|
|---|
| 784 | free(name);
|
|---|
| 785 | }
|
|---|
| 786 | free(svcs);
|
|---|
| 787 | return EOK;
|
|---|
| 788 | }
|
|---|
| 789 | /**
|
|---|
| 790 | * @}
|
|---|
| 791 | */
|
|---|