source: mainline/uspace/lib/hound/src/protocol.c@ 4f8772d4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4f8772d4 was 76d0981d, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Use proper boolean constant in while loops.

  • Property mode set to 100644
File size: 19.9 KB
RevLine 
[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]50enum 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]71typedef 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
[b66d43b]81/****
82 * CLIENT
83 ****/
84
[876f5561]85/** Well defined service name */
[059490c2]86const 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]93hound_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]107void 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]123errno_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) {
144 *id = (hound_context_id_t)IPC_GET_ARG1(call);
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]156errno_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]177errno_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]251errno_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]280errno_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]308errno_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{
[6133470]311 const format_convert_t c = { .f = {
312 .channels = format.channels,
313 .rate = format.sampling_rate / 100,
314 .format = format.sample_format,
315 }};
[eadaeae8]316 return async_req_4_0(exch, IPC_M_HOUND_STREAM_ENTER, CAP_HANDLE_RAW(id),
317 flags, c.arg, bsize);
[504f1ea3]318}
319
[876f5561]320/**
321 * Destroy existing stream and return IPC exchange to general mode.
322 * @param exch IPC exchange.
323 * @return Error code.
324 */
[b7fd2a0]325errno_t hound_service_stream_exit(async_exch_t *exch)
[504f1ea3]326{
[b66d43b]327 return async_req_0_0(exch, IPC_M_HOUND_STREAM_EXIT);
[504f1ea3]328}
329
[876f5561]330/**
331 * Wait until the server side buffer is empty.
332 * @param exch IPC exchange.
333 * @return Error code.
334 */
[b7fd2a0]335errno_t hound_service_stream_drain(async_exch_t *exch)
[504f1ea3]336{
[fd7c98b]337 return async_req_0_0(exch, IPC_M_HOUND_STREAM_DRAIN);
[504f1ea3]338}
339
[876f5561]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 */
[b7fd2a0]347errno_t hound_service_stream_write(async_exch_t *exch, const void *data, size_t size)
[504f1ea3]348{
[b66d43b]349 return async_data_write_start(exch, data, size);
[504f1ea3]350}
351
[876f5561]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 */
[b7fd2a0]359errno_t hound_service_stream_read(async_exch_t *exch, void *data, size_t size)
[504f1ea3]360{
[fd7c98b]361 return async_data_read_start(exch, data, size);
[504f1ea3]362}
363
[b66d43b]364/****
365 * SERVER
366 ****/
367
[5bf4310]368static void hound_server_read_data(void *stream);
369static void hound_server_write_data(void *stream);
[939871a]370static const hound_server_iface_t *server_iface;
[b66d43b]371
[876f5561]372/**
373 * Set hound server interface implementation.
374 * @param iface Initialized Hound server interface.
375 */
[e6e5f4e]376void hound_service_set_server_iface(const hound_server_iface_t *iface)
[b66d43b]377{
378 server_iface = iface;
379}
380
[eed4139]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)
[876f5561]386 */
[eed4139]387void hound_connection_handler(cap_call_handle_t icall_handle, ipc_call_t *icall,
388 void *arg)
[b66d43b]389{
[84239b1]390 hound_context_id_t id;
391 errno_t ret;
392 int flags;
393 void *source;
394 void *sink;
395
[b66d43b]396 /* Accept connection if there is a valid iface*/
397 if (server_iface) {
[a46e56b]398 async_answer_0(icall_handle, EOK);
[b66d43b]399 } else {
[a46e56b]400 async_answer_0(icall_handle, ENOTSUP);
[b66d43b]401 return;
402 }
403
[76d0981d]404 while (true) {
[b66d43b]405 ipc_call_t call;
[a46e56b]406 cap_call_handle_t chandle = async_get_call(&call);
[a4165561]407 switch (IPC_GET_IMETHOD(call)) {
[84239b1]408 case IPC_M_HOUND_CONTEXT_REGISTER:
[876f5561]409 /* check interface functions */
[b66d43b]410 if (!server_iface || !server_iface->add_context) {
[a46e56b]411 async_answer_0(chandle, ENOTSUP);
[b66d43b]412 break;
413 }
414 bool record = IPC_GET_ARG1(call);
415 void *name;
[876f5561]416
417 /* Get context name */
[84239b1]418 ret = async_data_write_accept(&name, true, 0, 0, 0, 0);
[b66d43b]419 if (ret != EOK) {
[a46e56b]420 async_answer_0(chandle, ret);
[b66d43b]421 break;
422 }
[84239b1]423
424 id = 0;
[b66d43b]425 ret = server_iface->add_context(server_iface->server,
426 &id, name, record);
[876f5561]427 /** new context should create a copy */
428 free(name);
[b66d43b]429 if (ret != EOK) {
[a46e56b]430 async_answer_0(chandle, ret);
[e6e5f4e]431 } else {
[a46e56b]432 async_answer_1(chandle, EOK, CAP_HANDLE_RAW(id));
[b66d43b]433 }
[e6e5f4e]434 break;
[84239b1]435 case IPC_M_HOUND_CONTEXT_UNREGISTER:
[876f5561]436 /* check interface functions */
[a4165561]437 if (!server_iface || !server_iface->rem_context) {
[a46e56b]438 async_answer_0(chandle, ENOTSUP);
[a4165561]439 break;
440 }
[876f5561]441
442 /* get id, 1st param */
[eadaeae8]443 id = (cap_handle_t) IPC_GET_ARG1(call);
[84239b1]444 ret = server_iface->rem_context(server_iface->server,
445 id);
[a46e56b]446 async_answer_0(chandle, ret);
[6ec1d48]447 break;
[84239b1]448 case IPC_M_HOUND_GET_LIST:
[876f5561]449 /* check interface functions */
[6ec1d48]450 if (!server_iface || !server_iface->get_list) {
[a46e56b]451 async_answer_0(chandle, ENOTSUP);
[6ec1d48]452 break;
453 }
[876f5561]454
[33b8d024]455 char **list = NULL;
[84239b1]456 flags = IPC_GET_ARG1(call);
[6ec1d48]457 size_t count = IPC_GET_ARG2(call);
458 const bool conn = IPC_GET_ARG3(call);
459 char *conn_name = NULL;
[84239b1]460 ret = EOK;
[876f5561]461
462 /* get connected actor name if provided */
[6ec1d48]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);
[876f5561]472
473 /* Alloc string sizes array */
[6ec1d48]474 size_t *sizes = NULL;
475 if (count)
476 sizes = calloc(count, sizeof(size_t));
477 if (count && !sizes)
478 ret = ENOMEM;
[a46e56b]479 async_answer_1(chandle, ret, count);
[6ec1d48]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 */
[3be9d10]490 cap_call_handle_t id;
[6ec1d48]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]);
[3be9d10]500 cap_call_handle_t id;
[6ec1d48]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;
[84239b1]510 case IPC_M_HOUND_CONNECT:
[876f5561]511 /* check interface functions */
[6ec1d48]512 if (!server_iface || !server_iface->connect) {
[a46e56b]513 async_answer_0(chandle, ENOTSUP);
[6ec1d48]514 break;
515 }
[876f5561]516
[84239b1]517 source = NULL;
518 sink = NULL;
[876f5561]519
520 /* read source name */
[84239b1]521 ret = async_data_write_accept(&source, true, 0, 0, 0,
522 0);
[876f5561]523 /* read sink name */
[6ec1d48]524 if (ret == EOK)
525 ret = async_data_write_accept(&sink,
526 true, 0, 0, 0, 0);
[876f5561]527
[6ec1d48]528 if (ret == EOK)
529 ret = server_iface->connect(
530 server_iface->server, source, sink);
531 free(source);
532 free(sink);
[a46e56b]533 async_answer_0(chandle, ret);
[6ec1d48]534 break;
[84239b1]535 case IPC_M_HOUND_DISCONNECT:
[876f5561]536 /* check interface functions */
[6ec1d48]537 if (!server_iface || !server_iface->disconnect) {
[a46e56b]538 async_answer_0(chandle, ENOTSUP);
[6ec1d48]539 break;
540 }
[876f5561]541
[84239b1]542 source = NULL;
543 sink = NULL;
[876f5561]544
545 /* read source name */
[84239b1]546 ret = async_data_write_accept(&source, true, 0, 0, 0,
547 0);
[876f5561]548 /*read sink name */
[6ec1d48]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);
[a46e56b]557 async_answer_0(chandle, ret);
[6ec1d48]558 break;
[84239b1]559 case IPC_M_HOUND_STREAM_ENTER:
[876f5561]560 /* check interface functions */
[cc3c27ad]561 if (!server_iface || !server_iface->is_record_context
562 || !server_iface->add_stream
563 || !server_iface->rem_stream) {
[a46e56b]564 async_answer_0(chandle, ENOTSUP);
[b66d43b]565 break;
566 }
567
[876f5561]568 /* get parameters */
[eadaeae8]569 id = (cap_handle_t) IPC_GET_ARG1(call);
[84239b1]570 flags = IPC_GET_ARG2(call);
[6133470]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 };
[b66d43b]577 size_t bsize = IPC_GET_ARG4(call);
[876f5561]578
[b66d43b]579 void *stream;
[84239b1]580 ret = server_iface->add_stream(server_iface->server,
[6133470]581 id, flags, f, bsize, &stream);
[b66d43b]582 if (ret != EOK) {
[a46e56b]583 async_answer_0(chandle, ret);
[b66d43b]584 break;
585 }
[37ea333]586 const bool rec = server_iface->is_record_context(
587 server_iface->server, id);
588 if (rec) {
[5bf4310]589 if(server_iface->stream_data_read) {
[a46e56b]590 async_answer_0(chandle, EOK);
[876f5561]591 /* start answering read calls */
[5bf4310]592 hound_server_write_data(stream);
[cc3c27ad]593 server_iface->rem_stream(
594 server_iface->server, stream);
[5bf4310]595 } else {
[a46e56b]596 async_answer_0(chandle, ENOTSUP);
[5bf4310]597 }
[37ea333]598 } else {
[5bf4310]599 if (server_iface->stream_data_write) {
[a46e56b]600 async_answer_0(chandle, EOK);
[876f5561]601 /* accept write calls */
[5bf4310]602 hound_server_read_data(stream);
[cc3c27ad]603 server_iface->rem_stream(
604 server_iface->server, stream);
[5bf4310]605 } else {
[a46e56b]606 async_answer_0(chandle, ENOTSUP);
[5bf4310]607 }
[37ea333]608 }
[b66d43b]609 break;
610 case IPC_M_HOUND_STREAM_EXIT:
[a4165561]611 case IPC_M_HOUND_STREAM_DRAIN:
612 /* Stream exit/drain is only allowed in stream context*/
[a46e56b]613 async_answer_0(chandle, EINVAL);
[939871a]614 break;
[b66d43b]615 default:
[a46e56b]616 async_answer_0(chandle, ENOTSUP);
[b66d43b]617 return;
618 }
619 }
620}
621
[876f5561]622/**
623 * Read data and push it to the stream.
624 * @param stream target stream, will push data there.
625 */
[5bf4310]626static void hound_server_read_data(void *stream)
[b66d43b]627{
[a46e56b]628 cap_call_handle_t chandle;
[71780e0]629 ipc_call_t call;
[b66d43b]630 size_t size = 0;
[b7fd2a0]631 errno_t ret_answer = EOK;
[876f5561]632 /* accept data write or drain */
[a46e56b]633 while (async_data_write_receive_call(&chandle, &call, &size)
[704baed]634 || (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN)) {
[876f5561]635 /* check drain first */
[704baed]636 if (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN) {
[b7fd2a0]637 errno_t ret = ENOTSUP;
[704baed]638 if (server_iface->drain_stream)
639 ret = server_iface->drain_stream(stream);
[a46e56b]640 async_answer_0(chandle, ret);
[704baed]641 continue;
642 }
[876f5561]643
644 /* there was an error last time */
[6d74977]645 if (ret_answer != EOK) {
[a46e56b]646 async_answer_0(chandle, ret_answer);
[6d74977]647 continue;
648 }
[876f5561]649
[b66d43b]650 char *buffer = malloc(size);
651 if (!buffer) {
[a46e56b]652 async_answer_0(chandle, ENOMEM);
[b66d43b]653 continue;
654 }
[a46e56b]655 const errno_t ret = async_data_write_finalize(chandle, buffer, size);
[b66d43b]656 if (ret == EOK) {
[876f5561]657 /* push data to stream */
[6d74977]658 ret_answer = server_iface->stream_data_write(
[704baed]659 stream, buffer, size);
[b66d43b]660 }
661 }
[b7fd2a0]662 const errno_t ret = IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_EXIT
[71780e0]663 ? EOK : EINVAL;
664
[a46e56b]665 async_answer_0(chandle, ret);
[b66d43b]666}
[504f1ea3]667
[876f5561]668/**
669 * Accept reads and pull data from the stream.
670 * @param stream target stream, will pull data from there.
671 */
[5bf4310]672static void hound_server_write_data(void *stream)
[37ea333]673{
674
[a46e56b]675 cap_call_handle_t chandle;
[37ea333]676 ipc_call_t call;
677 size_t size = 0;
[b7fd2a0]678 errno_t ret_answer = EOK;
[876f5561]679 /* accept data read and drain */
[a46e56b]680 while (async_data_read_receive_call(&chandle, &call, &size)
[9e40d443]681 || (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN)) {
[876f5561]682 /* drain does not make much sense but it is allowed */
[9e40d443]683 if (IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_DRAIN) {
[b7fd2a0]684 errno_t ret = ENOTSUP;
[9e40d443]685 if (server_iface->drain_stream)
686 ret = server_iface->drain_stream(stream);
[a46e56b]687 async_answer_0(chandle, ret);
[9e40d443]688 continue;
689 }
[876f5561]690 /* there was an error last time */
[6d74977]691 if (ret_answer != EOK) {
[a46e56b]692 async_answer_0(chandle, ret_answer);
[6d74977]693 continue;
694 }
[37ea333]695 char *buffer = malloc(size);
696 if (!buffer) {
[a46e56b]697 async_answer_0(chandle, ENOMEM);
[37ea333]698 continue;
699 }
[b7fd2a0]700 errno_t ret = server_iface->stream_data_read(stream, buffer, size);
[37ea333]701 if (ret == EOK) {
[6d74977]702 ret_answer =
[a46e56b]703 async_data_read_finalize(chandle, buffer, size);
[37ea333]704 }
705 }
[b7fd2a0]706 const errno_t ret = IPC_GET_IMETHOD(call) == IPC_M_HOUND_STREAM_EXIT
[37ea333]707 ? EOK : EINVAL;
708
[a46e56b]709 async_answer_0(chandle, ret);
[37ea333]710}
711
[7e7def5]712
[afa7c17]713/***
[7e7def5]714 * SERVER SIDE
[afa7c17]715 ***/
[b66d43b]716
[876f5561]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 */
[b7fd2a0]723errno_t hound_server_register(const char *name, service_id_t *id)
[afa7c17]724{
725 if (!name || !id)
726 return EINVAL;
727
[b7fd2a0]728 errno_t ret = loc_server_register(name);
[afa7c17]729 if (ret != EOK)
730 return ret;
731
732 return loc_service_register(HOUND_SERVICE, id);
733}
734
[876f5561]735/**
736 * Unregister server from the location service.
737 * @param id previously assigned service id.
738 */
[afa7c17]739void hound_server_unregister(service_id_t id)
740{
741 loc_service_unregister(id);
742}
743
[876f5561]744/**
745 * Set callback on device category change event.
746 * @param cb Callback function.
747 * @return Error code.
748 */
[b7fd2a0]749errno_t hound_server_set_device_change_callback(dev_change_callback_t cb)
[afa7c17]750{
751 return loc_register_cat_change_cb(cb);
752}
753
[876f5561]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 */
[b7fd2a0]759errno_t hound_server_devices_iterate(device_callback_t callback)
[afa7c17]760{
761 if (!callback)
762 return EINVAL;
763 static bool resolved = false;
764 static category_id_t cat_id = 0;
765
766 if (!resolved) {
[b7fd2a0]767 const errno_t ret = loc_category_get_id("audio-pcm", &cat_id,
[afa7c17]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;
[b7fd2a0]776 const errno_t ret = loc_category_get_svcs(cat_id, &svcs, &count);
[afa7c17]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 */
Note: See TracBrowser for help on using the repository browser.