[afa7c17] | 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 | */
|
---|
[89d3946f] | 36 | #include <adt/list.h>
|
---|
[afa7c17] | 37 | #include <errno.h>
|
---|
| 38 | #include <loc.h>
|
---|
[6ec1d48] | 39 | #include <macros.h>
|
---|
[afa7c17] | 40 | #include <str.h>
|
---|
[76d0981d] | 41 | #include <stdbool.h>
|
---|
[afa7c17] | 42 | #include <stdlib.h>
|
---|
[89d3946f] | 43 | #include <stdio.h>
|
---|
[cf13b17] | 44 | #include <types/common.h>
|
---|
[afa7c17] | 45 |
|
---|
[059490c2] | 46 | #include "protocol.h"
|
---|
[afa7c17] | 47 | #include "client.h"
|
---|
| 48 | #include "server.h"
|
---|
| 49 |
|
---|
[504f1ea3] | 50 | enum ipc_methods {
|
---|
[876f5561] | 51 | /** Create new context representation on the server side */
|
---|
[504f1ea3] | 52 | IPC_M_HOUND_CONTEXT_REGISTER = IPC_FIRST_USER_METHOD,
|
---|
[876f5561] | 53 | /** Release existing context representation on the server side */
|
---|
[504f1ea3] | 54 | IPC_M_HOUND_CONTEXT_UNREGISTER,
|
---|
[876f5561] | 55 | /** Request list of objects */
|
---|
[13318d1] | 56 | IPC_M_HOUND_GET_LIST,
|
---|
[876f5561] | 57 | /** Create new connection */
|
---|
[13318d1] | 58 | IPC_M_HOUND_CONNECT,
|
---|
[876f5561] | 59 | /** Destroy connection */
|
---|
[13318d1] | 60 | IPC_M_HOUND_DISCONNECT,
|
---|
[876f5561] | 61 | /** Switch IPC pipe to stream mode */
|
---|
[b66d43b] | 62 | IPC_M_HOUND_STREAM_ENTER,
|
---|
[876f5561] | 63 | /** Switch IPC pipe back to general mode */
|
---|
[b66d43b] | 64 | IPC_M_HOUND_STREAM_EXIT,
|
---|
[876f5561] | 65 | /** Wait until there is no data in the stream */
|
---|
[504f1ea3] | 66 | IPC_M_HOUND_STREAM_DRAIN,
|
---|
| 67 | };
|
---|
| 68 |
|
---|
[876f5561] | 69 |
|
---|
| 70 | /** PCM format conversion helper structure */
|
---|
[6133470] | 71 | typedef union {
|
---|
| 72 | struct {
|
---|
| 73 | uint16_t rate;
|
---|
| 74 | uint8_t channels;
|
---|
| 75 | uint8_t format;
|
---|
[84239b1] | 76 | } __attribute__((packed)) f;
|
---|
[6133470] | 77 | sysarg_t arg;
|
---|
| 78 | } format_convert_t;
|
---|
| 79 |
|
---|
| 80 |
|
---|
[904b1bc] | 81 | /*
|
---|
[b66d43b] | 82 | * CLIENT
|
---|
[904b1bc] | 83 | */
|
---|
[b66d43b] | 84 |
|
---|
[876f5561] | 85 | /** Well defined service name */
|
---|
[059490c2] | 86 | const char *HOUND_SERVICE = "audio/hound";
|
---|
| 87 |
|
---|
[876f5561] | 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 | */
|
---|
[059490c2] | 93 | hound_sess_t *hound_service_connect(const char *service)
|
---|
| 94 | {
|
---|
| 95 | service_id_t id = 0;
|
---|
[b7fd2a0] | 96 | const errno_t ret =
|
---|
[059490c2] | 97 | loc_service_get_id(service, &id, IPC_FLAG_BLOCKING);
|
---|
| 98 | if (ret != EOK)
|
---|
| 99 | return NULL;
|
---|
[f9b2cb4c] | 100 | return loc_service_connect(id, INTERFACE_HOUND, IPC_FLAG_BLOCKING);
|
---|
[059490c2] | 101 | }
|
---|
| 102 |
|
---|
[876f5561] | 103 | /**
|
---|
| 104 | * End an existing audio session.
|
---|
| 105 | * @param sess The session.
|
---|
| 106 | */
|
---|
[059490c2] | 107 | void hound_service_disconnect(hound_sess_t *sess)
|
---|
| 108 | {
|
---|
| 109 | if (sess)
|
---|
| 110 | async_hangup(sess);
|
---|
| 111 | }
|
---|
[afa7c17] | 112 |
|
---|
[876f5561] | 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.
|
---|
[fed5a9b] | 118 | *
|
---|
| 119 | * @param[out] id Return context ID.
|
---|
| 120 | *
|
---|
| 121 | * @return EOK on success, Error code on failure.
|
---|
[876f5561] | 122 | */
|
---|
[b7fd2a0] | 123 | errno_t hound_service_register_context(hound_sess_t *sess,
|
---|
[fed5a9b] | 124 | const char *name, bool record, hound_context_id_t *id)
|
---|
[504f1ea3] | 125 | {
|
---|
| 126 | assert(sess);
|
---|
| 127 | assert(name);
|
---|
[e6e5f4e] | 128 | ipc_call_t call;
|
---|
[504f1ea3] | 129 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[e6e5f4e] | 130 | aid_t mid =
|
---|
| 131 | async_send_1(exch, IPC_M_HOUND_CONTEXT_REGISTER, record, &call);
|
---|
[b7fd2a0] | 132 | errno_t ret = mid ? EOK : EPARTY;
|
---|
[e6e5f4e] | 133 |
|
---|
[b66d43b] | 134 | if (ret == EOK)
|
---|
| 135 | ret = async_data_write_start(exch, name, str_size(name));
|
---|
[e6e5f4e] | 136 | else
|
---|
| 137 | async_forget(mid);
|
---|
| 138 |
|
---|
| 139 | if (ret == EOK)
|
---|
[fed5a9b] | 140 | async_wait_for(mid, &ret);
|
---|
[e6e5f4e] | 141 |
|
---|
[504f1ea3] | 142 | async_exchange_end(exch);
|
---|
[fed5a9b] | 143 | if (ret == EOK) {
|
---|
[984a9ba] | 144 | *id = (hound_context_id_t) IPC_GET_ARG1(call);
|
---|
[fed5a9b] | 145 | }
|
---|
| 146 |
|
---|
| 147 | return ret;
|
---|
[504f1ea3] | 148 | }
|
---|
| 149 |
|
---|
[876f5561] | 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 | */
|
---|
[eadaeae8] | 156 | errno_t hound_service_unregister_context(hound_sess_t *sess,
|
---|
| 157 | hound_context_id_t id)
|
---|
[504f1ea3] | 158 | {
|
---|
| 159 | assert(sess);
|
---|
| 160 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[eadaeae8] | 161 | const errno_t ret = async_req_1_0(exch, IPC_M_HOUND_CONTEXT_UNREGISTER,
|
---|
| 162 | CAP_HANDLE_RAW(id));
|
---|
[504f1ea3] | 163 | async_exchange_end(exch);
|
---|
| 164 | return ret;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[876f5561] | 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 | */
|
---|
[33b8d024] | 177 | errno_t hound_service_get_list(hound_sess_t *sess, char ***ids, size_t *count,
|
---|
[13318d1] | 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,
|
---|
[c81132d] | 193 | connection != NULL, &res_call);
|
---|
[13318d1] | 194 |
|
---|
[b7fd2a0] | 195 | errno_t ret = EOK;
|
---|
[13318d1] | 196 | if (mid && connection)
|
---|
| 197 | ret = async_data_write_start(exch, connection,
|
---|
| 198 | str_size(connection));
|
---|
| 199 |
|
---|
| 200 | if (ret == EOK)
|
---|
[fed5a9b] | 201 | async_wait_for(mid, &ret);
|
---|
[13318d1] | 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 */
|
---|
[33b8d024] | 210 | char **names = NULL;
|
---|
[13318d1] | 211 | if (name_count) {
|
---|
[6ec1d48] | 212 | size_t *sizes = calloc(name_count, sizeof(size_t));
|
---|
[13318d1] | 213 | names = calloc(name_count, sizeof(char *));
|
---|
[6ec1d48] | 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) {
|
---|
[d120133] | 223 | memset(name, 0, sizes[i] + 1);
|
---|
[6ec1d48] | 224 | ret = async_data_read_start(exch, name, sizes[i]);
|
---|
| 225 | names[i] = name;
|
---|
| 226 | } else {
|
---|
| 227 | ret = ENOMEM;
|
---|
[13318d1] | 228 | }
|
---|
| 229 | }
|
---|
[6ec1d48] | 230 | free(sizes);
|
---|
[13318d1] | 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 |
|
---|
[876f5561] | 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 | */
|
---|
[b7fd2a0] | 251 | errno_t hound_service_connect_source_sink(hound_sess_t *sess, const char *source,
|
---|
[13318d1] | 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;
|
---|
[6ec1d48] | 261 | ipc_call_t call;
|
---|
| 262 | aid_t id = async_send_0(exch, IPC_M_HOUND_CONNECT, &call);
|
---|
[b7fd2a0] | 263 | errno_t ret = id ? EOK : EPARTY;
|
---|
[13318d1] | 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));
|
---|
[fed5a9b] | 268 | async_wait_for(id, &ret);
|
---|
[13318d1] | 269 | async_exchange_end(exch);
|
---|
| 270 | return ret;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[876f5561] | 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 | */
|
---|
[b7fd2a0] | 280 | errno_t hound_service_disconnect_source_sink(hound_sess_t *sess, const char *source,
|
---|
[13318d1] | 281 | const char *sink)
|
---|
| 282 | {
|
---|
| 283 | assert(sess);
|
---|
| 284 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 285 | if (!exch)
|
---|
| 286 | return ENOMEM;
|
---|
[6ec1d48] | 287 | ipc_call_t call;
|
---|
| 288 | aid_t id = async_send_0(exch, IPC_M_HOUND_DISCONNECT, &call);
|
---|
[b7fd2a0] | 289 | errno_t ret = id ? EOK : EPARTY;
|
---|
[13318d1] | 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));
|
---|
[fed5a9b] | 294 | async_wait_for(id, &ret);
|
---|
[13318d1] | 295 | async_exchange_end(exch);
|
---|
| 296 | return ENOTSUP;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[876f5561] | 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 | */
|
---|
[b7fd2a0] | 308 | errno_t hound_service_stream_enter(async_exch_t *exch, hound_context_id_t id,
|
---|
[504f1ea3] | 309 | int flags, pcm_format_t format, size_t bsize)
|
---|
| 310 | {
|
---|
[904b1bc] | 311 | const format_convert_t c = {
|
---|
| 312 | .f = {
|
---|
| 313 | .channels = format.channels,
|
---|
| 314 | .rate = format.sampling_rate / 100,
|
---|
| 315 | .format = format.sample_format,
|
---|
| 316 | }
|
---|
| 317 | };
|
---|
| 318 |
|
---|
[eadaeae8] | 319 | return async_req_4_0(exch, IPC_M_HOUND_STREAM_ENTER, CAP_HANDLE_RAW(id),
|
---|
| 320 | flags, c.arg, bsize);
|
---|
[504f1ea3] | 321 | }
|
---|
| 322 |
|
---|
[876f5561] | 323 | /**
|
---|
| 324 | * Destroy existing stream and return IPC exchange to general mode.
|
---|
| 325 | * @param exch IPC exchange.
|
---|
| 326 | * @return Error code.
|
---|
| 327 | */
|
---|
[b7fd2a0] | 328 | errno_t hound_service_stream_exit(async_exch_t *exch)
|
---|
[504f1ea3] | 329 | {
|
---|
[b66d43b] | 330 | return async_req_0_0(exch, IPC_M_HOUND_STREAM_EXIT);
|
---|
[504f1ea3] | 331 | }
|
---|
| 332 |
|
---|
[876f5561] | 333 | /**
|
---|
| 334 | * Wait until the server side buffer is empty.
|
---|
| 335 | * @param exch IPC exchange.
|
---|
| 336 | * @return Error code.
|
---|
| 337 | */
|
---|
[b7fd2a0] | 338 | errno_t hound_service_stream_drain(async_exch_t *exch)
|
---|
[504f1ea3] | 339 | {
|
---|
[fd7c98b] | 340 | return async_req_0_0(exch, IPC_M_HOUND_STREAM_DRAIN);
|
---|
[504f1ea3] | 341 | }
|
---|
| 342 |
|
---|
[876f5561] | 343 | /**
|
---|
| 344 | * Write audio data to a stream.
|
---|
| 345 | * @param exch IPC exchange in STREAM MODE.
|
---|
| 346 | * @param data Audio data buffer.
|
---|
| 347 | * @size size of the buffer
|
---|
| 348 | * @return Error code.
|
---|
| 349 | */
|
---|
[b7fd2a0] | 350 | errno_t hound_service_stream_write(async_exch_t *exch, const void *data, size_t size)
|
---|
[504f1ea3] | 351 | {
|
---|
[b66d43b] | 352 | return async_data_write_start(exch, data, size);
|
---|
[504f1ea3] | 353 | }
|
---|
| 354 |
|
---|
[876f5561] | 355 | /**
|
---|
| 356 | * Read data from a stream.
|
---|
| 357 | * @param exch IPC exchange in STREAM MODE.
|
---|
| 358 | * @param data Audio data buffer.
|
---|
| 359 | * @size size of the buffer
|
---|
| 360 | * @return Error code.
|
---|
| 361 | */
|
---|
[b7fd2a0] | 362 | errno_t hound_service_stream_read(async_exch_t *exch, void *data, size_t size)
|
---|
[504f1ea3] | 363 | {
|
---|
[fd7c98b] | 364 | return async_data_read_start(exch, data, size);
|
---|
[504f1ea3] | 365 | }
|
---|
| 366 |
|
---|
[904b1bc] | 367 | /*
|
---|
[b66d43b] | 368 | * SERVER
|
---|
[904b1bc] | 369 | */
|
---|
[b66d43b] | 370 |
|
---|
[5bf4310] | 371 | static void hound_server_read_data(void *stream);
|
---|
| 372 | static void hound_server_write_data(void *stream);
|
---|
[939871a] | 373 | static const hound_server_iface_t *server_iface;
|
---|
[b66d43b] | 374 |
|
---|
[876f5561] | 375 | /**
|
---|
| 376 | * Set hound server interface implementation.
|
---|
| 377 | * @param iface Initialized Hound server interface.
|
---|
| 378 | */
|
---|
[e6e5f4e] | 379 | void hound_service_set_server_iface(const hound_server_iface_t *iface)
|
---|
[b66d43b] | 380 | {
|
---|
| 381 | server_iface = iface;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
[eed4139] | 384 | /** Server side implementation of the hound protocol. IPC connection handler.
|
---|
| 385 | *
|
---|
[984a9ba] | 386 | * @param icall Pointer to initial call structure.
|
---|
| 387 | * @param arg (unused)
|
---|
| 388 | *
|
---|
[876f5561] | 389 | */
|
---|
[984a9ba] | 390 | void hound_connection_handler(ipc_call_t *icall, void *arg)
|
---|
[b66d43b] | 391 | {
|
---|
[984a9ba] | 392 | hound_context_id_t context;
|
---|
[84239b1] | 393 | errno_t ret;
|
---|
| 394 | int flags;
|
---|
| 395 | void *source;
|
---|
| 396 | void *sink;
|
---|
| 397 |
|
---|
[b66d43b] | 398 | /* Accept connection if there is a valid iface*/
|
---|
| 399 | if (server_iface) {
|
---|
[984a9ba] | 400 | async_answer_0(icall, EOK);
|
---|
[b66d43b] | 401 | } else {
|
---|
[984a9ba] | 402 | async_answer_0(icall, ENOTSUP);
|
---|
[b66d43b] | 403 | return;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
[76d0981d] | 406 | while (true) {
|
---|
[b66d43b] | 407 | ipc_call_t call;
|
---|
[984a9ba] | 408 | async_get_call(&call);
|
---|
| 409 |
|
---|
[a4165561] | 410 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[84239b1] | 411 | case IPC_M_HOUND_CONTEXT_REGISTER:
|
---|
[876f5561] | 412 | /* check interface functions */
|
---|
[b66d43b] | 413 | if (!server_iface || !server_iface->add_context) {
|
---|
[984a9ba] | 414 | async_answer_0(&call, ENOTSUP);
|
---|
[b66d43b] | 415 | break;
|
---|
| 416 | }
|
---|
| 417 | bool record = IPC_GET_ARG1(call);
|
---|
| 418 | void *name;
|
---|
[876f5561] | 419 |
|
---|
| 420 | /* Get context name */
|
---|
[84239b1] | 421 | ret = async_data_write_accept(&name, true, 0, 0, 0, 0);
|
---|
[b66d43b] | 422 | if (ret != EOK) {
|
---|
[984a9ba] | 423 | async_answer_0(&call, ret);
|
---|
[b66d43b] | 424 | break;
|
---|
| 425 | }
|
---|
[84239b1] | 426 |
|
---|
[984a9ba] | 427 | context = 0;
|
---|
[b66d43b] | 428 | ret = server_iface->add_context(server_iface->server,
|
---|
[984a9ba] | 429 | &context, name, record);
|
---|
[876f5561] | 430 | /** new context should create a copy */
|
---|
| 431 | free(name);
|
---|
[b66d43b] | 432 | if (ret != EOK) {
|
---|
[984a9ba] | 433 | async_answer_0(&call, ret);
|
---|
[e6e5f4e] | 434 | } else {
|
---|
[984a9ba] | 435 | async_answer_1(&call, EOK, CAP_HANDLE_RAW(context));
|
---|
[b66d43b] | 436 | }
|
---|
[e6e5f4e] | 437 | break;
|
---|
[84239b1] | 438 | case IPC_M_HOUND_CONTEXT_UNREGISTER:
|
---|
[876f5561] | 439 | /* check interface functions */
|
---|
[a4165561] | 440 | if (!server_iface || !server_iface->rem_context) {
|
---|
[984a9ba] | 441 | async_answer_0(&call, ENOTSUP);
|
---|
[a4165561] | 442 | break;
|
---|
| 443 | }
|
---|
[876f5561] | 444 |
|
---|
| 445 | /* get id, 1st param */
|
---|
[984a9ba] | 446 | context = (hound_context_id_t) IPC_GET_ARG1(call);
|
---|
[84239b1] | 447 | ret = server_iface->rem_context(server_iface->server,
|
---|
[984a9ba] | 448 | context);
|
---|
| 449 | async_answer_0(&call, ret);
|
---|
[6ec1d48] | 450 | break;
|
---|
[84239b1] | 451 | case IPC_M_HOUND_GET_LIST:
|
---|
[876f5561] | 452 | /* check interface functions */
|
---|
[6ec1d48] | 453 | if (!server_iface || !server_iface->get_list) {
|
---|
[984a9ba] | 454 | async_answer_0(&call, ENOTSUP);
|
---|
[6ec1d48] | 455 | break;
|
---|
| 456 | }
|
---|
[876f5561] | 457 |
|
---|
[33b8d024] | 458 | char **list = NULL;
|
---|
[84239b1] | 459 | flags = IPC_GET_ARG1(call);
|
---|
[6ec1d48] | 460 | size_t count = IPC_GET_ARG2(call);
|
---|
| 461 | const bool conn = IPC_GET_ARG3(call);
|
---|
| 462 | char *conn_name = NULL;
|
---|
[84239b1] | 463 | ret = EOK;
|
---|
[876f5561] | 464 |
|
---|
| 465 | /* get connected actor name if provided */
|
---|
[6ec1d48] | 466 | if (conn)
|
---|
| 467 | ret = async_data_write_accept(
|
---|
[904b1bc] | 468 | (void **)&conn_name, true, 0, 0, 0, 0);
|
---|
[6ec1d48] | 469 |
|
---|
| 470 | if (ret == EOK)
|
---|
| 471 | ret = server_iface->get_list(
|
---|
| 472 | server_iface->server, &list, &count,
|
---|
| 473 | conn_name, flags);
|
---|
| 474 | free(conn_name);
|
---|
[876f5561] | 475 |
|
---|
| 476 | /* Alloc string sizes array */
|
---|
[6ec1d48] | 477 | size_t *sizes = NULL;
|
---|
| 478 | if (count)
|
---|
| 479 | sizes = calloc(count, sizeof(size_t));
|
---|
| 480 | if (count && !sizes)
|
---|
| 481 | ret = ENOMEM;
|
---|
[984a9ba] | 482 | async_answer_1(&call, ret, count);
|
---|
[6ec1d48] | 483 |
|
---|
| 484 | /* We are done */
|
---|
| 485 | if (count == 0 || ret != EOK)
|
---|
| 486 | break;
|
---|
| 487 |
|
---|
| 488 | /* Prepare sizes table */
|
---|
| 489 | for (unsigned i = 0; i < count; ++i)
|
---|
| 490 | sizes[i] = str_size(list[i]);
|
---|
| 491 |
|
---|
| 492 | /* Send sizes table */
|
---|
[984a9ba] | 493 | ipc_call_t id;
|
---|
[6ec1d48] | 494 | if (async_data_read_receive(&id, NULL)) {
|
---|
[984a9ba] | 495 | ret = async_data_read_finalize(&id, sizes,
|
---|
[6ec1d48] | 496 | count * sizeof(size_t));
|
---|
| 497 | }
|
---|
| 498 | free(sizes);
|
---|
| 499 |
|
---|
| 500 | /* Proceed to send names */
|
---|
| 501 | for (unsigned i = 0; i < count; ++i) {
|
---|
| 502 | size_t size = str_size(list[i]);
|
---|
[984a9ba] | 503 | ipc_call_t id;
|
---|
[6ec1d48] | 504 | if (ret == EOK &&
|
---|
| 505 | async_data_read_receive(&id, NULL)) {
|
---|
[984a9ba] | 506 | ret = async_data_read_finalize(&id,
|
---|
[6ec1d48] | 507 | list[i], size);
|
---|
| 508 | }
|
---|
| 509 | free(list[i]);
|
---|
| 510 | }
|
---|
| 511 | free(list);
|
---|
| 512 | break;
|
---|
[84239b1] | 513 | case IPC_M_HOUND_CONNECT:
|
---|
[876f5561] | 514 | /* check interface functions */
|
---|
[6ec1d48] | 515 | if (!server_iface || !server_iface->connect) {
|
---|
[984a9ba] | 516 | async_answer_0(&call, ENOTSUP);
|
---|
[6ec1d48] | 517 | break;
|
---|
| 518 | }
|
---|
[876f5561] | 519 |
|
---|
[84239b1] | 520 | source = NULL;
|
---|
| 521 | sink = NULL;
|
---|
[876f5561] | 522 |
|
---|
| 523 | /* read source name */
|
---|
[84239b1] | 524 | ret = async_data_write_accept(&source, true, 0, 0, 0,
|
---|
| 525 | 0);
|
---|
[876f5561] | 526 | /* read sink name */
|
---|
[6ec1d48] | 527 | if (ret == EOK)
|
---|
| 528 | ret = async_data_write_accept(&sink,
|
---|
| 529 | true, 0, 0, 0, 0);
|
---|
[876f5561] | 530 |
|
---|
[6ec1d48] | 531 | if (ret == EOK)
|
---|
| 532 | ret = server_iface->connect(
|
---|
| 533 | server_iface->server, source, sink);
|
---|
| 534 | free(source);
|
---|
| 535 | free(sink);
|
---|
[984a9ba] | 536 | async_answer_0(&call, ret);
|
---|
[6ec1d48] | 537 | break;
|
---|
[84239b1] | 538 | case IPC_M_HOUND_DISCONNECT:
|
---|
[876f5561] | 539 | /* check interface functions */
|
---|
[6ec1d48] | 540 | if (!server_iface || !server_iface->disconnect) {
|
---|
[984a9ba] | 541 | async_answer_0(&call, ENOTSUP);
|
---|
[6ec1d48] | 542 | break;
|
---|
| 543 | }
|
---|
[876f5561] | 544 |
|
---|
[84239b1] | 545 | source = NULL;
|
---|
| 546 | sink = NULL;
|
---|
[876f5561] | 547 |
|
---|
| 548 | /* read source name */
|
---|
[84239b1] | 549 | ret = async_data_write_accept(&source, true, 0, 0, 0,
|
---|
| 550 | 0);
|
---|
[876f5561] | 551 | /*read sink name */
|
---|
[6ec1d48] | 552 | if (ret == EOK)
|
---|
| 553 | ret = async_data_write_accept(&sink,
|
---|
| 554 | true, 0, 0, 0, 0);
|
---|
| 555 | if (ret == EOK)
|
---|
| 556 | ret = server_iface->connect(
|
---|
| 557 | server_iface->server, source, sink);
|
---|
| 558 | free(source);
|
---|
| 559 | free(sink);
|
---|
[984a9ba] | 560 | async_answer_0(&call, ret);
|
---|
[6ec1d48] | 561 | break;
|
---|
[84239b1] | 562 | case IPC_M_HOUND_STREAM_ENTER:
|
---|
[876f5561] | 563 | /* check interface functions */
|
---|
[904b1bc] | 564 | if (!server_iface || !server_iface->is_record_context ||
|
---|
| 565 | !server_iface->add_stream ||
|
---|
| 566 | !server_iface->rem_stream) {
|
---|
[984a9ba] | 567 | async_answer_0(&call, ENOTSUP);
|
---|
[b66d43b] | 568 | break;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
[876f5561] | 571 | /* get parameters */
|
---|
[984a9ba] | 572 | context = (hound_context_id_t) IPC_GET_ARG1(call);
|
---|
[84239b1] | 573 | flags = IPC_GET_ARG2(call);
|
---|
[904b1bc] | 574 | const format_convert_t c = { .arg = IPC_GET_ARG3(call) };
|
---|
[6133470] | 575 | const pcm_format_t f = {
|
---|
[904b1bc] | 576 | .sampling_rate = c.f.rate * 100,
|
---|
| 577 | .channels = c.f.channels,
|
---|
| 578 | .sample_format = c.f.format,
|
---|
[6133470] | 579 | };
|
---|
[b66d43b] | 580 | size_t bsize = IPC_GET_ARG4(call);
|
---|
[876f5561] | 581 |
|
---|
[b66d43b] | 582 | void *stream;
|
---|
[84239b1] | 583 | ret = server_iface->add_stream(server_iface->server,
|
---|
[984a9ba] | 584 | context, flags, f, bsize, &stream);
|
---|
[b66d43b] | 585 | if (ret != EOK) {
|
---|
[984a9ba] | 586 | async_answer_0(&call, ret);
|
---|
[b66d43b] | 587 | break;
|
---|
| 588 | }
|
---|
[37ea333] | 589 | const bool rec = server_iface->is_record_context(
|
---|
[984a9ba] | 590 | server_iface->server, context);
|
---|
[37ea333] | 591 | if (rec) {
|
---|
[904b1bc] | 592 | if (server_iface->stream_data_read) {
|
---|
[984a9ba] | 593 | async_answer_0(&call, EOK);
|
---|
[876f5561] | 594 | /* start answering read calls */
|
---|
[5bf4310] | 595 | hound_server_write_data(stream);
|
---|
[cc3c27ad] | 596 | server_iface->rem_stream(
|
---|
| 597 | server_iface->server, stream);
|
---|
[5bf4310] | 598 | } else {
|
---|
[984a9ba] | 599 | async_answer_0(&call, ENOTSUP);
|
---|
[5bf4310] | 600 | }
|
---|
[37ea333] | 601 | } else {
|
---|
[5bf4310] | 602 | if (server_iface->stream_data_write) {
|
---|
[984a9ba] | 603 | async_answer_0(&call, EOK);
|
---|
[876f5561] | 604 | /* accept write calls */
|
---|
[5bf4310] | 605 | hound_server_read_data(stream);
|
---|
[cc3c27ad] | 606 | server_iface->rem_stream(
|
---|
| 607 | server_iface->server, stream);
|
---|
[5bf4310] | 608 | } else {
|
---|
[984a9ba] | 609 | async_answer_0(&call, ENOTSUP);
|
---|
[5bf4310] | 610 | }
|
---|
[37ea333] | 611 | }
|
---|
[b66d43b] | 612 | break;
|
---|
| 613 | case IPC_M_HOUND_STREAM_EXIT:
|
---|
[a4165561] | 614 | case IPC_M_HOUND_STREAM_DRAIN:
|
---|
| 615 | /* Stream exit/drain is only allowed in stream context*/
|
---|
[984a9ba] | 616 | async_answer_0(&call, EINVAL);
|
---|
[939871a] | 617 | break;
|
---|
[b66d43b] | 618 | default:
|
---|
[984a9ba] | 619 | async_answer_0(&call, ENOTSUP);
|
---|
[b66d43b] | 620 | return;
|
---|
| 621 | }
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 |
|
---|
[876f5561] | 625 | /**
|
---|
| 626 | * Read data and push it to the stream.
|
---|
| 627 | * @param stream target stream, will push data there.
|
---|
| 628 | */
|
---|
[5bf4310] | 629 | static void hound_server_read_data(void *stream)
|
---|
[b66d43b] | 630 | {
|
---|
[71780e0] | 631 | ipc_call_t call;
|
---|
[b66d43b] | 632 | size_t size = 0;
|
---|
[b7fd2a0] | 633 | errno_t ret_answer = EOK;
|
---|
[984a9ba] | 634 |
|
---|
[876f5561] | 635 | /* accept data write or drain */
|
---|
[984a9ba] | 636 | while (async_data_write_receive(&call, &size) ||
|
---|
[904b1bc] | 637 | (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN)) {
|
---|
[876f5561] | 638 | /* check drain first */
|
---|
[704baed] | 639 | if (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN) {
|
---|
[b7fd2a0] | 640 | errno_t ret = ENOTSUP;
|
---|
[704baed] | 641 | if (server_iface->drain_stream)
|
---|
| 642 | ret = server_iface->drain_stream(stream);
|
---|
[984a9ba] | 643 | async_answer_0(&call, ret);
|
---|
[704baed] | 644 | continue;
|
---|
| 645 | }
|
---|
[876f5561] | 646 |
|
---|
| 647 | /* there was an error last time */
|
---|
[6d74977] | 648 | if (ret_answer != EOK) {
|
---|
[984a9ba] | 649 | async_answer_0(&call, ret_answer);
|
---|
[6d74977] | 650 | continue;
|
---|
| 651 | }
|
---|
[876f5561] | 652 |
|
---|
[b66d43b] | 653 | char *buffer = malloc(size);
|
---|
| 654 | if (!buffer) {
|
---|
[984a9ba] | 655 | async_answer_0(&call, ENOMEM);
|
---|
[b66d43b] | 656 | continue;
|
---|
| 657 | }
|
---|
[984a9ba] | 658 | const errno_t ret = async_data_write_finalize(&call, buffer, size);
|
---|
[b66d43b] | 659 | if (ret == EOK) {
|
---|
[876f5561] | 660 | /* push data to stream */
|
---|
[6d74977] | 661 | ret_answer = server_iface->stream_data_write(
|
---|
[704baed] | 662 | stream, buffer, size);
|
---|
[b66d43b] | 663 | }
|
---|
| 664 | }
|
---|
[904b1bc] | 665 | const errno_t ret = IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_EXIT ?
|
---|
| 666 | EOK : EINVAL;
|
---|
[71780e0] | 667 |
|
---|
[984a9ba] | 668 | async_answer_0(&call, ret);
|
---|
[b66d43b] | 669 | }
|
---|
[504f1ea3] | 670 |
|
---|
[876f5561] | 671 | /**
|
---|
| 672 | * Accept reads and pull data from the stream.
|
---|
| 673 | * @param stream target stream, will pull data from there.
|
---|
| 674 | */
|
---|
[5bf4310] | 675 | static void hound_server_write_data(void *stream)
|
---|
[37ea333] | 676 | {
|
---|
| 677 |
|
---|
| 678 | ipc_call_t call;
|
---|
| 679 | size_t size = 0;
|
---|
[b7fd2a0] | 680 | errno_t ret_answer = EOK;
|
---|
[984a9ba] | 681 |
|
---|
[876f5561] | 682 | /* accept data read and drain */
|
---|
[984a9ba] | 683 | while (async_data_read_receive(&call, &size) ||
|
---|
[904b1bc] | 684 | (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN)) {
|
---|
[876f5561] | 685 | /* drain does not make much sense but it is allowed */
|
---|
[9e40d443] | 686 | if (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN) {
|
---|
[b7fd2a0] | 687 | errno_t ret = ENOTSUP;
|
---|
[9e40d443] | 688 | if (server_iface->drain_stream)
|
---|
| 689 | ret = server_iface->drain_stream(stream);
|
---|
[984a9ba] | 690 | async_answer_0(&call, ret);
|
---|
[9e40d443] | 691 | continue;
|
---|
| 692 | }
|
---|
[876f5561] | 693 | /* there was an error last time */
|
---|
[6d74977] | 694 | if (ret_answer != EOK) {
|
---|
[984a9ba] | 695 | async_answer_0(&call, ret_answer);
|
---|
[6d74977] | 696 | continue;
|
---|
| 697 | }
|
---|
[37ea333] | 698 | char *buffer = malloc(size);
|
---|
| 699 | if (!buffer) {
|
---|
[984a9ba] | 700 | async_answer_0(&call, ENOMEM);
|
---|
[37ea333] | 701 | continue;
|
---|
| 702 | }
|
---|
[b7fd2a0] | 703 | errno_t ret = server_iface->stream_data_read(stream, buffer, size);
|
---|
[37ea333] | 704 | if (ret == EOK) {
|
---|
[6d74977] | 705 | ret_answer =
|
---|
[984a9ba] | 706 | async_data_read_finalize(&call, buffer, size);
|
---|
[37ea333] | 707 | }
|
---|
| 708 | }
|
---|
[904b1bc] | 709 | const errno_t ret = IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_EXIT ?
|
---|
| 710 | EOK : EINVAL;
|
---|
[37ea333] | 711 |
|
---|
[984a9ba] | 712 | async_answer_0(&call, ret);
|
---|
[37ea333] | 713 | }
|
---|
| 714 |
|
---|
[7e7def5] | 715 |
|
---|
[904b1bc] | 716 | /*
|
---|
[7e7def5] | 717 | * SERVER SIDE
|
---|
[904b1bc] | 718 | */
|
---|
[b66d43b] | 719 |
|
---|
[876f5561] | 720 | /**
|
---|
| 721 | * Register new hound service to the location service.
|
---|
| 722 | * @param[in] name server name
|
---|
| 723 | * @param[out] id assigned service id.
|
---|
| 724 | * @return Error code.
|
---|
| 725 | */
|
---|
[b7fd2a0] | 726 | errno_t hound_server_register(const char *name, service_id_t *id)
|
---|
[afa7c17] | 727 | {
|
---|
| 728 | if (!name || !id)
|
---|
| 729 | return EINVAL;
|
---|
| 730 |
|
---|
[b7fd2a0] | 731 | errno_t ret = loc_server_register(name);
|
---|
[afa7c17] | 732 | if (ret != EOK)
|
---|
| 733 | return ret;
|
---|
| 734 |
|
---|
| 735 | return loc_service_register(HOUND_SERVICE, id);
|
---|
| 736 | }
|
---|
| 737 |
|
---|
[876f5561] | 738 | /**
|
---|
| 739 | * Unregister server from the location service.
|
---|
| 740 | * @param id previously assigned service id.
|
---|
| 741 | */
|
---|
[afa7c17] | 742 | void hound_server_unregister(service_id_t id)
|
---|
| 743 | {
|
---|
| 744 | loc_service_unregister(id);
|
---|
| 745 | }
|
---|
| 746 |
|
---|
[876f5561] | 747 | /**
|
---|
| 748 | * Set callback on device category change event.
|
---|
| 749 | * @param cb Callback function.
|
---|
| 750 | * @return Error code.
|
---|
| 751 | */
|
---|
[e89a06a] | 752 | errno_t hound_server_set_device_change_callback(dev_change_callback_t cb,
|
---|
| 753 | void *arg)
|
---|
[afa7c17] | 754 | {
|
---|
[e89a06a] | 755 | return loc_register_cat_change_cb(cb, arg);
|
---|
[afa7c17] | 756 | }
|
---|
| 757 |
|
---|
[876f5561] | 758 | /**
|
---|
| 759 | * Walk through all device in the audio-pcm category.
|
---|
| 760 | * @param callback Function to call on every device.
|
---|
| 761 | * @return Error code.
|
---|
| 762 | */
|
---|
[b7fd2a0] | 763 | errno_t hound_server_devices_iterate(device_callback_t callback)
|
---|
[afa7c17] | 764 | {
|
---|
| 765 | if (!callback)
|
---|
| 766 | return EINVAL;
|
---|
| 767 | static bool resolved = false;
|
---|
| 768 | static category_id_t cat_id = 0;
|
---|
| 769 |
|
---|
| 770 | if (!resolved) {
|
---|
[b7fd2a0] | 771 | const errno_t ret = loc_category_get_id("audio-pcm", &cat_id,
|
---|
[afa7c17] | 772 | IPC_FLAG_BLOCKING);
|
---|
| 773 | if (ret != EOK)
|
---|
| 774 | return ret;
|
---|
| 775 | resolved = true;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 | service_id_t *svcs = NULL;
|
---|
| 779 | size_t count = 0;
|
---|
[b7fd2a0] | 780 | const errno_t ret = loc_category_get_svcs(cat_id, &svcs, &count);
|
---|
[afa7c17] | 781 | if (ret != EOK)
|
---|
| 782 | return ret;
|
---|
| 783 |
|
---|
| 784 | for (unsigned i = 0; i < count; ++i) {
|
---|
| 785 | char *name = NULL;
|
---|
| 786 | loc_service_get_name(svcs[i], &name);
|
---|
| 787 | callback(svcs[i], name);
|
---|
| 788 | free(name);
|
---|
| 789 | }
|
---|
| 790 | free(svcs);
|
---|
| 791 | return EOK;
|
---|
| 792 | }
|
---|
| 793 | /**
|
---|
| 794 | * @}
|
---|
| 795 | */
|
---|