source: roaraudio/roard/req.c @ 4621:c20151c5eb6f

Last change on this file since 4621:c20151c5eb6f was 4621:c20151c5eb6f, checked in by phi, 13 years ago

test for correct set of mixer in case of forced mixer connect

File size: 39.1 KB
Line 
1//req.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28// include for uname() used by req_on_server_info()
29#ifdef ROAR_HAVE_UNAME
30#include <sys/utsname.h>
31#endif
32
33static void * _dataspace(struct roar_message * mes, char ** data, uint32_t flags[2], size_t len) {
34 if ( len <= LIBROAR_BUFFER_MSGDATA )
35  return mes->data;
36
37 if ( *data != NULL )
38  free(*data);
39
40 *data = malloc(len);
41
42 ROAR_DBG("_dataspace(mes=%p, data=%p, flags=%p, len=%llu): *data=%p", mes, data, flags, (long long unsigned int)len, *data);
43
44 if ( *data == NULL )
45  return NULL;
46
47 flags[1] |= COMMAND_FLAG_OUT_LONGDATA;
48
49 return *data;
50}
51
52int req_on_noop        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
53 mes->cmd     = ROAR_CMD_OK;
54 mes->pos     = g_pos;
55 mes->datalen = 0;
56 return 0;
57}
58
59int req_on_identify    (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
60 struct roar_client_server * cs;
61 struct roar_client        * c;
62 int max_len;
63
64 if ( mes->datalen < 1 )
65  return -1;
66
67 clients_get_server(client, &cs);
68
69 c = ROAR_CLIENT(cs);
70
71 if ( mes->data[0] == 1 ) {
72  if ( c->pid == -1 ) {
73   c->pid       = ROAR_NET2HOST32(*(uint32_t*)((mes->data)+1));
74   ROAR_DBG("req_on_identify(): new PID: c->pid = %i", c->pid);
75  }
76
77  ROAR_DBG("req_on_identify(): final PID: c->pid = %i", c->pid);
78
79  max_len = (mes->datalen - 5) < (ROAR_BUFFER_NAME-1) ? (mes->datalen - 5) : (ROAR_BUFFER_NAME-1);
80
81  strncpy(c->name, mes->data + 5, max_len);
82  c->name[max_len] = 0;
83
84  // we set the acclevel to IDENTED here.
85  // if it is alreay > IDENTED we reset it anyway
86  // as potential auth relevent data changed.
87  cs->acclev = ACCLEV_IDENTED;
88
89  mes->cmd     = ROAR_CMD_OK;
90  mes->pos     = g_pos;
91  mes->datalen = 0;
92
93  ROAR_DBG("req_on_identify(*): client=%i, pid=%i", client, c->pid);
94  ROAR_DBG("req_on_identify(*) = 0");
95  return 0;
96 }
97
98 return -1;
99}
100
101int req_on_auth        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
102 struct roar_client_server * cs;
103 struct roar_auth_message    authmes;
104 int ret;
105
106 clients_get_server(client, &cs);
107
108 // TODO: add code to support some auth.
109
110 if ( roar_auth_from_mes(&authmes, mes, *data) == -1 )
111  return -1;
112
113 ROAR_INFO("req_on_auth(client=%i,...): authtype=%s(%i)", ROAR_DBG_INFO_VERBOSE,
114                           client, roar_autht2str(authmes.type), authmes.type);
115
116 ret = auth_client_ckeck(cs, &authmes);
117
118 if ( ret != 1 )
119  return -1;
120
121 mes->cmd     = ROAR_CMD_OK;
122 mes->pos     = g_pos;
123 mes->datalen = 0;
124 return 0;
125}
126
127
128int req_on_whoami      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
129 mes->cmd     = ROAR_CMD_OK;
130 mes->pos     = g_pos;
131 mes->datalen = 1;
132 mes->data[0] = client;
133 return 0;
134}
135
136
137int req_on_new_stream  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
138 int stream;
139 struct roar_stream_server * ss;
140 struct roar_stream        * s;
141 struct roar_stream * source_stream;
142 struct roar_audio_info * info;
143 struct roar_audio_info * source_info;
144
145 ROAR_DBG("req_on_new_stream(client=%i, ...): creating stream...", client);
146 if ((stream = streams_new()) == -1 )
147  return -1;
148
149 ROAR_DBG("req_on_new_stream(client=%i, ...): stream=%i", client, stream);
150
151 ROAR_DBG("req_on_new_stream(client=%i, ...): getting stream...", client);
152 if ( streams_get(stream, &ss) == -1 ) {
153  streams_delete(stream);
154  return -1;
155 }
156
157 s = ROAR_STREAM(ss);
158
159 ROAR_DBG("req_on_new_stream(client=%i, ...): set client of stream...", client);
160 if ( client_stream_add(client, stream) == -1 ) {
161  streams_delete(stream);
162  return -1;
163 }
164
165 ROAR_DBG("req_on_new_stream(client=%i, ...): loading stream from message...", client);
166 if ( roar_stream_m2s(s, mes) == -1 ) {
167  streams_delete(stream);
168  return -1;
169 }
170
171 ROAR_DBG("req_on_new_stream(client=%i, ...): setting id and codec of stream...", client);
172 ROAR_STREAM(s)->id = stream; // roar_stream_m2s() resets this
173 ROAR_STREAM_SERVER(s)->codec_orgi = ROAR_STREAM(s)->info.codec;
174
175 ROAR_DBG("req_on_new_stream(client=%i, ...): setting direction stream...", client);
176 // int streams_set_dir    (int id, int dir, int defaults)
177 if ( streams_set_dir(stream, ROAR_STREAM(s)->dir, 1) == -1 ) {
178  streams_delete(stream);
179  return -1;
180 }
181
182 if ( mes->stream != -1 && mes->stream != ss->mixer_stream ) {
183  streams_delete(stream);
184  return -1;
185 }
186
187 ROAR_DBG("req_on_new_stream(client=%i, ...): setting up direction specific stream settings...", client);
188 switch (ROAR_STREAM(s)->dir) {
189  case ROAR_DIR_LIGHT_IN:
190  case ROAR_DIR_LIGHT_OUT:
191#ifndef ROAR_WITHOUT_DCOMP_LIGHT
192    info = &(ROAR_STREAM(s)->info);
193
194    info->channels = 0;
195    info->bits     = 0;
196    info->rate     = 0;
197#else
198    streams_delete(stream);
199    return -1;
200#endif
201
202   break;
203  case ROAR_DIR_MIDI_IN:
204  case ROAR_DIR_MIDI_OUT:
205#ifndef ROAR_WITHOUT_DCOMP_MIDI
206    info = &(ROAR_STREAM(s)->info);
207
208    info->channels = ROAR_MIDI_CHANNELS_DEFAULT;
209    info->bits     = ROAR_MIDI_BITS;
210    info->rate     = 0;
211#else
212    streams_delete(stream);
213    return -1;
214#endif
215
216   break;
217
218  case ROAR_DIR_RAW_IN:
219#ifndef ROAR_WITHOUT_DCOMP_RAW
220    if ( ROAR_STREAM(s)->pos_rel_id == -1     ||
221         ROAR_STREAM(s)->pos_rel_id == stream ||
222         streams_get_dir(ROAR_STREAM(s)->pos_rel_id) != ROAR_DIR_RAW_OUT
223       ) {
224     ROAR_STREAM(s)->pos_rel_id = -1; // force this here as it will try to delete itself while deleting
225                                      // in case rel_id == stream
226     streams_delete(stream);
227     return -1;
228    }
229#else
230  case ROAR_DIR_RAW_OUT:
231    streams_delete(stream);
232    return -1;
233#endif
234
235   break;
236  case ROAR_DIR_THRU:
237
238    if ( ROAR_STREAM(s)->pos_rel_id == -1 || ROAR_STREAM(s)->pos_rel_id == stream ) {
239     ROAR_STREAM(s)->pos_rel_id = -1; // force this here as it will try to delete itself while deleting
240                                      // in case rel_id == stream
241     streams_delete(stream);
242     return -1;
243    }
244
245    if ( streams_get_clientobj(ROAR_STREAM(s)->pos_rel_id, &source_stream) == -1 ) {
246     streams_delete(stream);
247     return -1;
248    }
249
250    info        = &(ROAR_STREAM(s)->info);
251    source_info = &(ROAR_STREAM(source_stream)->info);
252
253    info->channels = source_info->channels;
254    info->bits     = source_info->bits;
255    info->rate     = source_info->rate;
256    info->codec    = source_info->codec;
257    ROAR_STREAM_SERVER(s)->codec_orgi = ROAR_STREAM_SERVER(source_stream)->codec_orgi;
258
259   break;
260  case ROAR_DIR_FILTER:
261    info        = &(ROAR_STREAM(s)->info);
262
263    if ( ROAR_STREAM(s)->pos_rel_id == -1 ) {
264     source_info = g_sa;
265    } else {
266     if ( streams_get_clientobj(ROAR_STREAM(s)->pos_rel_id, &source_stream) == -1 ) {
267      streams_delete(stream);
268      return -1;
269     }
270     source_info = &(ROAR_STREAM(source_stream)->info);
271    }
272
273    if ( info->channels != source_info->channels || info->bits != source_info->bits ||
274         info->codec    != source_info->codec    || info->rate != source_info->rate ) {
275     // the stream parameters don't match the one of the stream being filtered.
276     // -> delete and reject the stream.
277     streams_delete(stream);
278     return -1;
279    }
280   break;
281 }
282
283 ROAR_DBG("req_on_new_stream(client=%i, ...): returning (OK, stream=%i)...", client, stream);
284
285 mes->cmd     = ROAR_CMD_OK;
286 mes->stream  = stream;
287 mes->datalen = 0;
288
289 return 0;
290}
291
292int req_on_exec_stream (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
293 int r;
294
295 ROAR_DBG("req_on_exec_stream(client=%i, mes={stream=%i,...},...): execing stream", client, mes->stream);
296
297
298 if ( streams_is_ready(mes->stream) ) {
299  flags[1] |= COMMAND_FLAG_OUT_CLOSECON;
300 } else {
301  if ( (r = client_stream_exec(client, mes->stream)) == -1 )
302   return -1;
303 }
304
305 ROAR_DBG("req_on_exec_stream(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream);
306 mes->cmd     = ROAR_CMD_OK;
307 mes->datalen = 0;
308
309 return 0;
310}
311
312int req_on_con_stream  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
313 char   host[80] = {0};
314 int    port = 0;
315 int    type;
316 int    fh;
317 int    len;
318
319 if ( mes->datalen < 4 )
320  return -1;
321
322 if ( *(mes->data) != 0 )
323  return -1;
324
325 if ( mes->datalen > 80 ) // we do not support long messages here
326  return -1;
327
328 type = (unsigned)mes->data[1];
329 port = ROAR_NET2HOST16(((uint16_t*)mes->data)[1]);
330
331 len = mes->datalen - 4;
332
333 strncpy(host, &(mes->data[4]), len);
334 host[len] = 0;
335
336 if ( type > ROAR_SOCKET_TYPE_MAX )
337  return -1;
338
339 if ( type == ROAR_SOCKET_TYPE_FILE ) // disabled because of security resons
340  return -1;
341
342 if ( type == ROAR_SOCKET_TYPE_FORK ) // why should we connect to ourself?
343  return -1;
344
345 ROAR_DBG("req_on_con_stream(*): CONNECT(type=%i, host='%s', port=%i)", type, host, port);
346
347 if ( (fh = roar_socket_open(ROAR_SOCKET_MODE_CONNECT, type, host, port)) == -1 )
348  return -1;
349
350 if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) {
351  close(fh);
352  return 1;
353 }
354
355 mes->datalen = 0;
356 mes->cmd     = ROAR_CMD_OK;
357
358 return 0;
359}
360
361int req_on_passfh      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
362 int sock = clients_get_fh(client);
363 int16_t * d = (int16_t*)mes->data;
364 struct roard_listen * lsock;
365 int listening;
366 int fh;
367 int i;
368
369 ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...) = ?", client, mes->stream);
370
371 if ( (fh = roar_socket_recv_fh(sock, NULL, NULL)) == -1 ) {
372  ROAR_WARN("req_on_passfh(client=%i, mes={stream=%i,...},...): was unabled to get filehandle from remote end. bad.", client, mes->stream);
373  ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (ERROR)...", client, mes->stream);
374  return -1;
375 }
376
377 ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): fh=%i", client, mes->stream, fh);
378
379 if ( mes->stream != -1 ) { // stream pass:
380  ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): This is a stream passfh", client, mes->stream);
381
382  if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) {
383   close(fh);
384   ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (ERROR)...", client, mes->stream);
385   return -1;
386  }
387
388  ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream);
389
390  mes->datalen = 0;
391  mes->cmd     = ROAR_CMD_OK;
392
393  return 0;
394 }
395
396// non-stream pass:
397
398 ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): This is a client passfh", client, mes->stream);
399
400/*
401 0: Version,   16
402 1: Flags,     16
403 2: Protocol,  16
404 3: Byteorder, 16
405 Options...
406*/
407
408 if ( mes->datalen < 4*2 )
409  return -1;
410
411 for (i = 0; i < 4; i++) {
412  d[i] = ROAR_NET2HOST16(d[i]);
413 }
414
415 if ( d[0] != 0 ) // version
416  return -1;
417
418 listening = d[1] & ROAR_CLIENTPASS_FLAG_LISTEN;
419
420 if ( listening )
421  d[1] -= ROAR_CLIENTPASS_FLAG_LISTEN;
422
423 if ( d[1] != 0 ) // flags
424  return -1;
425
426 if ( listening ) {
427  if ( get_listen(&lsock, NULL) == -1 ) {
428   close(fh);
429   return -1;
430  }
431
432  roar_vio_open_fh_socket(&(lsock->sock), fh);
433  lsock->used   = 1;
434  lsock->proto  = d[2];
435 } else {
436  if ( clients_new_from_fh(fh, d[2], d[3], 1) == -1 )
437   return -1;
438 }
439
440 ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream);
441
442 mes->datalen = 0;
443 mes->cmd     = ROAR_CMD_OK;
444
445 return 0;
446}
447
448#ifdef ROAR_SUPPORT_META
449int req_on_set_meta    (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
450 int type;
451 int mode;
452 int namelen, vallen;
453 char   val[255+1];
454 char   name[ROAR_META_MAX_NAMELEN+1];
455
456 if ( mes->datalen < 3 )
457  return -1;
458
459 if ( mes->data[0] != 0 ) // version
460  return -1;
461
462 mode = (unsigned) mes->data[1];
463 type = (unsigned) mes->data[2];
464
465 ROAR_DBG("req_on_set_meta(*): mode=%i, type=%i", mode, type);
466
467 if ( mode == ROAR_META_MODE_CLEAR ) {
468  stream_meta_clear(mes->stream);
469  mes->datalen = 0;
470  mes->cmd     = ROAR_CMD_OK;
471  return 0;
472 } else if ( mode == ROAR_META_MODE_DELETE ) { // unsuppoerted at the moment
473  return -1;
474 } else if ( mode == ROAR_META_MODE_FINALIZE ) {
475  stream_meta_finalize(mes->stream);
476  mes->datalen = 0;
477  mes->cmd     = ROAR_CMD_OK;
478  return 0;
479 } else if ( mode == ROAR_META_MODE_SET || mode == ROAR_META_MODE_ADD ) {
480  if ( mes->datalen < 5 )
481   return -1;
482
483  namelen = (unsigned) mes->data[3];
484  vallen  = (unsigned) mes->data[4];
485
486  ROAR_DBG("req_on_set_meta(*): namelen=%i, vallen=%i", namelen, vallen);
487
488  if ( mes->datalen < (5 + namelen + vallen) )
489   return -1;
490
491  if ( namelen > ROAR_META_MAX_NAMELEN )
492   return -1;
493
494  strncpy(name, &(mes->data[5]), namelen);
495  name[namelen] = 0;
496
497  if ( vallen > 255 )
498   return -1;
499
500  strncpy(val, &(mes->data[5+namelen]), vallen);
501  val[vallen] = 0;
502
503  if ( mode == ROAR_META_MODE_SET ) {
504   if ( stream_meta_set(mes->stream, type, name, val) == -1 )
505    return -1;
506  } else {
507   if ( stream_meta_add(mes->stream, type, name, val) == -1 )
508    return -1;
509  }
510
511  mes->datalen = 0;
512  mes->cmd     = ROAR_CMD_OK;
513  return 0;
514 } else { // unknown mode!
515  return -1;
516 }
517
518 return -1;
519}
520
521int req_on_get_meta    (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
522 int vallen;
523 int type;
524 char val[LIBROAR_BUFFER_MSGDATA-1];
525
526 if ( mes->datalen != 2 )
527  return -1;
528
529 if ( mes->data[0] != 0 ) // version
530  return -1;
531
532 type = (unsigned) mes->data[1];
533
534 if ( stream_meta_get(mes->stream, type, NULL, val, LIBROAR_BUFFER_MSGDATA-2) == -1 )
535  return -1;
536
537 vallen = strlen(val);
538
539 mes->cmd     = ROAR_CMD_OK;
540 mes->datalen = 2 + vallen;
541
542 mes->data[0] = 0;
543 mes->data[1] = (unsigned char) vallen;
544
545 val[vallen] = 0;
546
547 strncpy(&(mes->data[2]), val, vallen+1);
548
549 return 0;
550}
551
552int req_on_list_meta   (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
553 int i;
554 int len = 0;
555 int types[ROAR_META_MAX_PER_STREAM];
556
557 if ( mes->datalen != 1 )
558  return -1;
559
560 if ( mes->data[0] != 0 ) // version
561  return -1;
562
563 if ( (len = stream_meta_list(mes->stream, types, ROAR_META_MAX_PER_STREAM)) == -1 )
564  return -1;
565
566 mes->cmd     = ROAR_CMD_OK;
567 mes->datalen = 1 + len;
568 mes->data[0] = 0;
569
570 for (i = 0; i < len; i++)
571  mes->data[i+1] = types[i];
572
573 return 0;
574}
575#endif
576
577int req_on_server_info (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
578#ifdef ROAR_HAVE_UNAME
579 struct utsname utsname;
580#endif
581 struct roar_server_info info;
582 uint16_t * d16;
583
584 if ( mes->datalen != 4 )
585  return -1;
586
587 d16 = (uint16_t*)mes->data;
588
589 // check version.
590 if ( ROAR_NET2HOST16(d16[0]) != 0 )
591  return -1;
592
593 switch (ROAR_NET2HOST16(d16[1])) {
594  case ROAR_IT_SERVER:
595   memset(&info, 0, sizeof(info));
596
597   info.version = "roard/" PACKAGE_VERSION " <" DEVICE_VENDOR_STRING ">";
598
599   if ( !!strcmp(g_config->location, CONF_DEF_STRING) )
600    info.location = g_config->location;
601
602   if ( !!strcmp(g_config->description, CONF_DEF_STRING) )
603   info.description = g_config->description;
604
605#ifdef ROAR_HAVE_UNAME
606   if ( uname(&utsname) == 0 ) {
607    info.un.sysname  = utsname.sysname;
608    info.un.release  = utsname.release;
609    info.un.nodename = utsname.nodename;
610    info.un.machine  = utsname.machine;
611   }
612#endif
613
614   *data = NULL;
615
616   if ( roar_server_info_to_mes(mes, &info, (void**)data) == -1 )
617    return -1;
618
619   if ( *data != NULL )
620    flags[1] |= COMMAND_FLAG_OUT_LONGDATA;
621  break;
622  default: /* unknown request */
623    return -1;
624   break;
625 }
626
627 mes->cmd = ROAR_CMD_OK;
628
629 return 0;
630}
631
632int req_on_server_oinfo    (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
633 struct roar_stream s;
634 int dir = ROAR_DIR_PLAY;
635 int subsys;
636//ROAR_DIR_OUTPUT
637
638 if ( mes->datalen != 0 ) {
639  if ( mes->datalen != 2 )
640   return -1;
641
642  if ( mes->data[0] != 0 )
643   return -1;
644
645  dir = mes->data[1];
646 }
647
648 subsys = streams_dir2subsys(dir);
649
650 memset(&s, 0, sizeof(struct roar_stream));
651
652 s.dir           = ROAR_DIR_MIXING;
653 s.pos_rel_id    = -1;
654
655 switch (subsys) {
656  case ROAR_SUBSYS_WAVEFORM:
657    s.info.rate     = g_sa->rate;
658    s.info.bits     = g_sa->bits;
659    s.info.channels = g_sa->channels;
660    s.info.codec    = g_sa->codec;
661    s.pos           = g_pos;
662   break;
663#ifndef ROAR_WITHOUT_DCOMP_MIDI
664  case ROAR_SUBSYS_MIDI:
665   break;
666#endif
667#ifndef ROAR_WITHOUT_DCOMP_LIGHT
668  case ROAR_SUBSYS_LIGHT:
669    s.info.rate     = ROAR_OUTPUT_CFREQ;
670    s.info.bits     = ROAR_LIGHT_BITS;
671    s.info.channels = g_light_state.channels;
672   break;
673#endif
674#ifndef ROAR_WITHOUT_DCOMP_RAW
675  case ROAR_SUBSYS_RAW:
676    // no need to set anything here.
677   break;
678#endif
679#ifndef ROAR_WITHOUT_DCOMP_RDTCS
680  case ROAR_SUBSYS_RDTCS:
681    s.info.rate     = ROAR_OUTPUT_CFREQ;
682    s.info.bits     = ROAR_RDTCS_BITS;
683    s.info.channels = ROAR_RDTCS_CHANNELS;
684    s.info.codec    = ROAR_RDTCS_CODEC;
685   break;
686#endif
687  default:
688    return -1;
689   break;
690 }
691
692 if ( roar_stream_s2m(&s, mes) == -1 )
693  return -1;
694
695 mes->cmd = ROAR_CMD_OK;
696
697 return 0;
698}
699
700int req_on_caps        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
701 struct roar_caps caps;
702 struct roar_stds * stds;
703 size_t i;
704
705 if ( roar_caps_from_msg(&caps, mes, *data) == -1 )
706  return -1;
707
708 // handle the data from the caps command here...
709
710 if ( !(caps.flags & ROAR_CF_REQUEST) ) {
711  mes->cmd = ROAR_CMD_OK;
712  mes->datalen = 0;
713  return 0;
714 }
715
716 mes->datalen = 0;
717
718 switch (caps.type) {
719  case ROAR_CT_STANDARDS:
720    if ( (stds = roar_stds_new(g_caps_stds.stds_len)) == NULL )
721     return -1;
722
723    for ( i = 0; i < stds->stds_len; i++) {
724     stds->stds[i] = ROAR_HOST2NET32(g_caps_stds.stds[i]);
725    }
726
727    caps.data = stds->stds;
728    caps.len  = stds->stds_len * 4;
729    // TODO: add support for **data.
730    if ( roar_caps_to_msg(mes, &caps, NULL) == -1 ) {
731     roar_stds_free(stds);
732     return -1;
733    }
734    roar_stds_free(stds);
735   break;
736  default:
737    return -1;
738   break;
739 }
740
741 mes->cmd = ROAR_CMD_OK;
742
743 return 0;
744}
745
746int req_on_get_standby (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
747 mes->cmd     = ROAR_CMD_OK;
748 mes->pos     = g_pos;
749 mes->datalen = 2;
750
751 *((uint16_t*)mes->data) = ROAR_HOST2NET16((unsigned) g_standby);
752
753 return 0;
754}
755
756int req_on_set_standby (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
757 if ( mes->datalen != 2 )
758  return -1;
759
760 g_standby = ROAR_NET2HOST16(*((uint16_t*)mes->data));
761
762 mes->cmd     = ROAR_CMD_OK;
763 mes->pos     = g_pos;
764 mes->datalen = 0;
765
766 return 0;
767}
768
769int req_on_exit      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
770 int term = 0;
771
772 if ( mes->datalen == 1 )
773  term = mes->data[0];
774
775 mes->cmd     = ROAR_CMD_OK;
776 mes->pos     = g_pos;
777 mes->datalen = 0;
778
779 ROAR_DBG("req_on_exit(*): term=%i", term);
780
781 if ( term ) {
782  cleanup_listen_socket(1);
783 } else {
784  alive = 0;
785 }
786
787 return 0;
788}
789
790int req_on_list_clients(int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
791 unsigned char filter, cmp;
792 uint32_t id;
793 int clients[ROAR_CLIENTS_MAX];
794 int i, c = 0;
795
796 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
797  return -1;
798
799 // TODO: add code to support filter
800 if ( filter != ROAR_CTL_FILTER_ANY )
801  return -1;
802
803 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
804  if ( g_clients[i] != NULL ) {
805   clients[c++] = i;
806  }
807 }
808
809 roar_ctl_ia2m(mes, clients, c);
810
811 mes->cmd = ROAR_CMD_OK;
812
813 return 0;
814}
815int req_on_list_streams(int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
816 unsigned char filter, cmp;
817 uint32_t id;
818 int streams[ROAR_STREAMS_MAX];
819 int i, c = 0;
820 int match;
821
822 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
823  return -1;
824
825 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
826  if ( g_streams[i] == NULL )
827   continue;
828
829  match = 0;
830
831  switch (filter) {
832   case ROAR_CTL_FILTER_ANY:
833     match = 1;
834    break;
835   case ROAR_CTL_FILTER_DIR:
836     match = roar_filter_match(cmp, id, ROAR_STREAM(g_streams[i])->dir);
837    break;
838   default: // unsupported filter...
839     return -1;
840    break;
841  }
842
843  if ( match )
844   streams[c++] = i;
845 }
846
847 roar_ctl_ia2m(mes, streams, c);
848
849 mes->cmd = ROAR_CMD_OK;
850
851 return 0;
852}
853
854int req_on_get_client  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
855 struct roar_client * c;
856
857 if ( mes->datalen != 1 )
858  return -1;
859
860 if ( clients_get(mes->data[0], &c) == -1 )
861  return -1;
862
863 mes->cmd = ROAR_CMD_OK;
864
865 return roar_ctl_c2m(mes, c);
866}
867
868int req_on_get_stream  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
869 struct roar_stream_server * s;
870
871 if ( mes->datalen != 1 )
872  return -1;
873
874 if ( streams_get(mes->data[0], &s) == -1 )
875  return -1;
876
877 mes->cmd = ROAR_CMD_OK;
878 mes->stream = mes->data[0];
879
880 return roar_stream_s2m(ROAR_STREAM(s), mes);
881}
882
883int req_on_get_stream_para (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
884 struct roar_stream * s;
885 struct roar_stream_server * ss;
886 struct roar_audio_info * audio_info;
887 struct roar_stream_ltm * ltm;
888 uint16_t * d  = (uint16_t *) mes->data;
889 int64_t * d64 = ( int64_t *) mes->data;
890 int64_t * d64ptr;
891 int i, h, k;
892 char * str;
893 size_t needed;
894 int test, bits;
895
896 ROAR_DBG("req_on_get_stream_para(client=%i, mes=%p{.stream=%i, .datalen=%i,...}, data=%p, flags=%p) = ?", client, mes, (int)mes->stream, (int)mes->datalen, data, flags);
897
898 if ( mes->datalen < 4 )
899  return -1;
900
901 for (i = 0; i < 2; i++) {
902  d[i] = ROAR_NET2HOST16(d[i]);
903 }
904
905 if ( d[0] != 0 ) {
906  ROAR_WARN("req_on_get_stream_para(*): unsupported command version: %i, %i", (int)d[0], (int)d[1]);
907  return -1;
908 }
909
910 switch (d[1]) {
911  case ROAR_STREAM_PARA_INFO:
912    if ( streams_get(mes->stream, &ss) == -1 ) {
913     ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream);
914     return -1;
915    }
916
917    if ( streams_calc_delay(mes->stream) == -1 ) {
918     ROAR_WARN("req_on_get_stream_para(*): can not calc delay for stream %i", mes->stream);
919    }
920
921    s = ROAR_STREAM(ss);
922
923    audio_info = &(s->info);
924
925    mes->datalen = 2*12;
926
927    d[ 2] = ROAR_OUTPUT_CALC_OUTBUFSIZE(audio_info);
928    d[ 3] = ss->pre_underruns;
929    d[ 4] = ss->post_underruns;
930    d[ 5] = ss->codec_orgi;
931    d[ 6] = (ss->flags & 0xFFFF) | (ss->primary ? ROAR_FLAG_PRIMARY : 0) | (ss->driver_id != -1 ? ROAR_FLAG_OUTPUT : 0);
932    d[ 7] = ss->delay/1000;
933    d[ 8] = ss->state;
934    d[ 9] = (ss->flags & 0xFFFF0000) >> 16;
935    d[10] = ss->mixer_stream;
936    d[11] = ss->role;
937
938    ROAR_DBG("req_on_get_stream_para(*): ss->driver_id=%i", ss->driver_id);
939
940    ROAR_DBG("req_on_get_stream_para(*): delay=%i, send delay=%i", ss->delay, d[7]);
941
942    for (i = 0; i < mes->datalen/2; i++) {
943     d[i] = ROAR_HOST2NET16(d[i]);
944    }
945
946    mes->pos = s->pos;
947   break;
948
949  case ROAR_STREAM_PARA_NAME:
950   str = streams_get_name(mes->stream);
951
952   if ( str == NULL )
953    return -1;
954
955    mes->datalen = 4 + strlen(str);
956
957    if ( mes->datalen > LIBROAR_BUFFER_MSGDATA )
958     return -1;
959
960    strncpy(((char*)&(mes->data))+4, str, mes->datalen);
961
962    d[0] = ROAR_HOST2NET16(d[0]);
963    d[1] = ROAR_HOST2NET16(d[1]);
964   break;
965
966  case ROAR_STREAM_PARA_CHANMAP:
967    if ( streams_get(mes->stream, &ss) == -1 ) {
968     ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream);
969     return -1;
970    }
971
972    s = ROAR_STREAM(ss);
973
974    memcpy(&(mes->data[4]), ss->chanmap.in, s->info.channels);
975    mes->datalen = 2*2 + s->info.channels;
976
977    d[0] = ROAR_HOST2NET16(d[0]);
978    d[1] = ROAR_HOST2NET16(d[1]);
979   break;
980
981  case ROAR_STREAM_PARA_LTM:
982    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM request...", client);
983
984    if ( mes->datalen < (6 * 2) ) {
985     ROAR_ERR("req_on_get_stream_para(client=%i, ...): LTM request of client is corruped: message too short", client);
986     return -1;
987    }
988
989    for (i = 2; i < mes->datalen/2; i++) {
990     d[i] = ROAR_NET2HOST16(d[i]);
991    }
992
993    if ( d[2] != ROAR_LTM_SST_GET_RAW ) {
994     ROAR_ERR("req_on_get_stream_para(client=%i, ...): LTM request of client is corruped: unknown LTM subtype: %i", client, (int)d[2]);
995     return -1;
996    }
997
998    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM request of type GET_RAW", client);
999
1000    test = d[5];
1001    bits = 0;
1002    while (test) {
1003     if ( test & 0x1 )
1004      bits++;
1005
1006     test >>= 1;
1007    }
1008
1009    needed = 0;
1010
1011    if ( mes->stream == -1 ) {
1012     ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM multi-stream request...", client);
1013
1014     for (i = 6; i < mes->datalen/2; i++) {
1015      if ( (ltm = streams_ltm_get(d[i], d[5], d[3])) == NULL )
1016       return -1;
1017
1018      needed += ltm->channels;
1019     }
1020    } else {
1021     ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM single-stream request for stream %i...", client, mes->stream);
1022     if ( (ltm = streams_ltm_get(mes->stream, d[5], d[3])) == NULL )
1023      return -1;
1024
1025     needed = ltm->channels;
1026    }
1027
1028    needed *= bits;
1029
1030    needed += mes->stream == -1 ? (mes->datalen/2) - 6 : 1;
1031
1032    ROAR_DBG("req_on_get_stream_para(client=%i, ...): data size for answer is %i 64 bit sub-packets", client, (int)needed);
1033
1034    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
1035    d64 = _dataspace(mes, data, flags, needed * 8);
1036    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1037
1038    if ( (d = roar_mm_malloc(mes->datalen)) == NULL )
1039     return -1;
1040
1041    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d=%p, data=%p{%p}", client, d, data, *data);
1042
1043
1044    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
1045    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1046    memcpy(d, mes->data, mes->datalen);
1047    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1048
1049    d64ptr = d64;
1050
1051    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
1052    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1053
1054    if ( mes->stream == -1 ) {
1055     for (i = 6; i < mes->datalen/2; i++) {
1056      ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1057
1058      if ( (ltm = streams_ltm_get(d[i], d[5], d[3])) == NULL )
1059       return -1;
1060
1061      *d64ptr = ltm->channels & 0xFFFF;
1062       d64ptr++;
1063
1064      for (h = 0; h < ltm->channels; h++) {
1065       for (k = 0; k < ROAR_LTM_MTBITS; k++) {
1066        if ( d[5] & (1<<k) ) {
1067         switch (1<<k) {
1068          case ROAR_LTM_MT_RMS:
1069            *d64ptr = ltm->cur[h].rms;
1070           break;
1071          default:
1072            ROAR_ERR("req_on_get_stream_para(client=%i, ...): client requets unknown MT for LTM: bit %i", client, k);
1073         }
1074         d64ptr++;
1075        }
1076       }
1077      }
1078     }
1079    } else {
1080     if ( (ltm = streams_ltm_get(mes->stream, d[5], d[3])) == NULL )
1081      return -1;
1082
1083     *d64ptr = ltm->channels & 0xFFFF;
1084      d64ptr++;
1085
1086     for (h = 0; h < ltm->channels; h++) {
1087      for (k = 0; k < ROAR_LTM_MTBITS; k++) {
1088       if ( d[5] & (1<<k) ) {
1089        switch (1<<k) {
1090         case ROAR_LTM_MT_RMS:
1091           *d64ptr = ltm->cur[h].rms;
1092           ROAR_DBG("req_on_get_stream_para(client=%i, ...): rms=%lli to %p", client, (long long int)*d64ptr, d64ptr);
1093          break;
1094         default:
1095           ROAR_ERR("req_on_get_stream_para(client=%i, ...): client requets unknown MT for LTM: bit %i", client, k);
1096        }
1097        d64ptr++;
1098       }
1099      }
1100     }
1101    }
1102
1103    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1104
1105    roar_mm_free(d);
1106
1107    for (i = 0; i < needed; i++) {
1108     d64[i] = ROAR_HOST2NET64(d64[i]);
1109    }
1110
1111    mes->datalen = needed * 8;
1112    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM d64=%p, d64ptr=%p", client, d64, d64ptr);
1113    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM final message has %i byte of data", client, (int)mes->datalen);
1114    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1115    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM GET_RAW request: OK. returning...", client);
1116   break;
1117
1118  default:
1119    ROAR_WARN("req_on_get_stream_para(*): unsupported command: %i", d[1]);
1120    return -1;
1121 }
1122
1123 ROAR_DBG("req_on_get_stream_para(client=%i, mes=%p{.stream=%i, .datalen=%i,...}, data=%p, flags=%p) = 0 // returning OK", client, mes, (int)mes->stream, (int)mes->datalen, data, flags);
1124 mes->cmd = ROAR_CMD_OK;
1125 return 0;
1126}
1127
1128int req_on_set_stream_para (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1129 struct roar_stream_server * ss;
1130 uint16_t * d = (uint16_t *) mes->data;
1131 uint32_t tmp, tmp2, flagstore;
1132 int protect = 0;
1133 int i;
1134
1135 if ( mes->datalen < 2*2 )
1136  return -1;
1137
1138 for (i = 0; i < 2; i++) {
1139  d[i] = ROAR_NET2HOST16(d[i]);
1140 }
1141
1142 if ( d[0] != 0 )
1143  return -1;
1144
1145 switch (d[1]) {
1146  case ROAR_STREAM_PARA_FLAGS:
1147    if ( mes->datalen != 2*4 && mes->datalen != 2*5 )
1148     return -1;
1149
1150    d[2] = ROAR_NET2HOST16(d[2]);
1151    d[3] = ROAR_NET2HOST16(d[3]);
1152
1153    ROAR_DBG("req_on_set_stream_para(*): request seems to be valid");
1154
1155    tmp = 0;
1156
1157    if ( mes->datalen == 2*5 ) {
1158     d[4] = ROAR_NET2HOST16(d[4]);
1159     tmp   = d[4];
1160     tmp <<= 16;
1161    }
1162
1163    tmp |= d[3];
1164
1165    if ( d[2] & ROAR_PROTECT_FLAG ) {
1166     protect = 1;
1167     d[2] -= ROAR_PROTECT_FLAG;
1168    }
1169
1170    switch (d[2]) {
1171     case ROAR_SET_FLAG:
1172       if ( streams_set_flag(mes->stream, tmp) == -1 )
1173        return -1;
1174      break;
1175     case ROAR_RESET_FLAG:
1176       if ( streams_reset_flag(mes->stream, tmp) == -1 )
1177        return -1;
1178      break;
1179     case ROAR_NOOP_FLAG:
1180      break;
1181     case ROAR_TOGGLE_FLAG:
1182       if ( streams_get(mes->stream, &ss) == -1 )
1183        return -1;
1184
1185       flagstore = ss->flags;
1186
1187       tmp2 = flagstore & tmp; // those are the flags we need to reset.
1188       ROAR_DBG("req_on_set_stream_para(*): tmp2=0x%.8lx", (long int)tmp2);
1189       if ( tmp2 )
1190        if ( streams_reset_flag(mes->stream, tmp2) == -1 )
1191         return -1;
1192
1193       tmp2 = (flagstore ^ tmp) & tmp; // those are the flags we need to set.
1194       ROAR_DBG("req_on_set_stream_para(*): tmp2=0x%.8lx", (long int)tmp2);
1195       if ( tmp2 )
1196        if ( streams_set_flag(mes->stream, tmp2) == -1 )
1197         return -1;
1198      break;
1199     default:
1200       return -1;
1201      break;
1202    }
1203
1204   ROAR_DBG("req_on_set_stream_para(*): protect=%i", protect);
1205   if ( protect )
1206    if ( streams_protect_flag(mes->stream, tmp) == -1 )
1207     return -1;
1208
1209   break;
1210  case ROAR_STREAM_PARA_CHANMAP:
1211    if ( streams_set_map(mes->stream, &(mes->data[4]), mes->datalen - 4) == -1 )
1212     return -1;
1213   break;
1214  case ROAR_STREAM_PARA_ROLE:
1215    if ( mes->datalen != 2*3 )
1216     return -1;
1217
1218    d[2] = ROAR_NET2HOST16(d[2]);
1219
1220    if ( streams_set_role(mes->stream, d[2]) == -1 )
1221     return -1;
1222   break;
1223  case ROAR_STREAM_PARA_LTM:
1224    if ( mes->datalen < (6 * 2) )
1225     return -1;
1226
1227    for (i = 2; i < mes->datalen/2; i++) {
1228     d[i] = ROAR_NET2HOST16(d[i]);
1229    }
1230
1231    if ( mes->stream == -1 ) {
1232     for (i = 6; i < mes->datalen/2; i++)
1233      if ( streams_ltm_ctl(d[i], d[5], d[3], d[2]) == -1 )
1234       return -1;
1235    } else {
1236     if ( streams_ltm_ctl(mes->stream, d[5], d[3], d[2]) == -1 )
1237      return -1;
1238    }
1239   break;
1240  default:
1241    ROAR_WARN("req_on_set_stream_para(*): unsupported command version: %i, %i", d[0], d[1]);
1242    return -1;
1243   break;
1244 }
1245
1246 mes->cmd     = ROAR_CMD_OK;
1247 mes->datalen = 0;
1248
1249 return 0;
1250}
1251
1252int req_on_kick (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1253 struct roar_stream_server * ss;
1254 uint16_t * info = (uint16_t *) mes->data;
1255 int is_stream = 0;
1256
1257 if ( mes->datalen != 4 )
1258  return -1;
1259
1260 info[0] = ROAR_NET2HOST16(info[0]);
1261 info[1] = ROAR_NET2HOST16(info[1]);
1262
1263 switch (info[0]) {
1264  case ROAR_OT_CLIENT:
1265    clients_delete(info[1]);
1266   break;
1267  case ROAR_OT_STREAM:
1268    is_stream = 1;
1269   break;
1270  case ROAR_OT_SOURCE:
1271    if ( streams_get_flag(info[1], ROAR_FLAG_SOURCE) != 1 )
1272     return -1;
1273    is_stream = 1;
1274   break;
1275  case ROAR_OT_OUTPUT:
1276    if ( streams_get(info[1], &ss) == -1 )
1277     return -1;
1278
1279    if ( ss->driver_id == -1 )
1280     return -1;
1281
1282    is_stream = 1;
1283   break;
1284  case ROAR_OT_MIXER:
1285    if ( streams_get(info[1], &ss) == -1 )
1286     return -1;
1287
1288    if ( ROAR_STREAM(ss)->dir != ROAR_DIR_MIXING )
1289     return -1;
1290
1291    is_stream = 1;
1292   break;
1293  case ROAR_OT_BRIDGE:
1294    if ( streams_get(info[1], &ss) == -1 )
1295     return -1;
1296
1297    if ( ROAR_STREAM(ss)->dir != ROAR_DIR_BRIDGE )
1298     return -1;
1299
1300    is_stream = 1;
1301   break;
1302  default:
1303/* TODO: those types should be handled, too:
1304#define ROAR_OT_SAMPLE    4
1305#define ROAR_OT_LISTEN    8
1306#define ROAR_OT_ACTION    9
1307#define ROAR_OT_MSGQUEUE 10
1308#define ROAR_OT_MSGBUS   11
1309*/
1310    return -1;
1311   break;
1312 }
1313
1314 if ( is_stream ) {
1315  if ( streams_get_flag(info[1], ROAR_FLAG_IMMUTABLE) == 1 )
1316   return -1;
1317
1318  streams_delete(info[1]);
1319 }
1320
1321 mes->cmd     = ROAR_CMD_OK;
1322 mes->datalen = 0;
1323
1324 return 0;
1325}
1326
1327int req_on_attach      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1328 uint16_t * info = (uint16_t *) mes->data;
1329
1330 if ( mes->datalen < 6 )
1331  return -1;
1332
1333 info[0] = ROAR_NET2HOST16(info[0]);
1334 info[1] = ROAR_NET2HOST16(info[1]);
1335 info[2] = ROAR_NET2HOST16(info[2]);
1336
1337 if ( info[0] != 0 )
1338  return -1;
1339
1340 if ( info[1] == ROAR_ATTACH_SIMPLE ) {
1341  if ( client_stream_move(info[2], mes->stream) == -1 )
1342   return -1;
1343 } else {
1344  return -1;
1345 }
1346
1347 mes->cmd     = ROAR_CMD_OK;
1348 mes->datalen = 0;
1349
1350 return 0;
1351}
1352
1353int req_on_set_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1354 struct roar_stream_server * s;
1355 uint16_t * info = (uint16_t *) mes->data;
1356 uint16_t   version;
1357 uint16_t   scale = 65535;
1358 int stream;
1359 int i;
1360 int chans;
1361
1362 ROAR_DBG("req_on_set_vol(*) = ?");
1363 ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen);
1364
1365 if ( mes->datalen < (4*2) )
1366  return -1;
1367
1368 version = ROAR_NET2HOST16(info[0]);
1369 ROAR_DBG("req_on_set_vol(*): version=%i", (int)version);
1370
1371 switch (version) {
1372  case 0:
1373    stream = ROAR_NET2HOST16(info[1]);
1374   break;
1375  case 1:
1376    stream = mes->stream;
1377    scale  = ROAR_NET2HOST16(info[1]);
1378   break;
1379  default:
1380    return -1;
1381   break;
1382 }
1383 ROAR_DBG("req_on_set_vol(*): stream=%i", stream);
1384
1385 if ( scale == 0 )
1386  return -1;
1387
1388 // TODO: change this code.
1389 //       we should not directly change the stream object but use some stream_*()-func
1390 //       for that job.
1391
1392 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
1393  return -1;
1394
1395 s = g_streams[stream];
1396
1397 if ( s == NULL )
1398  return -1;
1399
1400 ROAR_DBG("req_on_set_vol(*): s=%p", s);
1401
1402 info[2] = ROAR_NET2HOST16(info[2]);
1403
1404 if ( info[2] == ROAR_SET_VOL_ALL ) {
1405  chans = (mes->datalen/2) - 3;
1406  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans);
1407
1408  if ( chans >= ROAR_MAX_CHANNELS )
1409   return -1;
1410
1411  ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer);
1412
1413  for (i = 0; i < chans; i++) {
1414   s->mixer.mixer[i] = ROAR_NET2HOST16(info[i+3]);
1415   ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, ROAR_NET2HOST16(info[i+3]));
1416  }
1417
1418  s->mixer.scale = scale;
1419
1420  ROAR_DBG("req_on_set_vol(*): mixer changed!");
1421
1422 } else if ( info[2] == ROAR_SET_VOL_ONE ) {
1423  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE");
1424  if ( ROAR_NET2HOST16(info[3]) >= ROAR_MAX_CHANNELS )
1425   return -1;
1426
1427  s->mixer.mixer[ROAR_NET2HOST16(info[3])] = ROAR_NET2HOST16(info[4]);
1428
1429  s->mixer.scale = scale;
1430 } else {
1431  return -1;
1432 }
1433
1434 if ( streams_set_mixer(stream) == -1 )
1435  return -1;
1436
1437 mes->cmd     = ROAR_CMD_OK;
1438 mes->datalen = 0;
1439
1440 return 0;
1441}
1442
1443int req_on_get_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1444 uint16_t * info = (uint16_t *) mes->data;
1445 uint16_t   version = -1;
1446 int stream;
1447 struct roar_stream_server * s;
1448 int i;
1449 int chans;
1450
1451 ROAR_DBG("req_on_get_vol(*) = ?");
1452 ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen);
1453
1454 if ( mes->datalen < 2 ) {
1455  return -1;
1456 }
1457
1458 version = ROAR_NET2HOST16(info[0]);
1459
1460 switch (version) {
1461  case 0:
1462    if ( mes->datalen < (2*2) )
1463     return -1;
1464
1465    stream = ROAR_NET2HOST16(info[1]);
1466   break;
1467  case 1:
1468    stream = mes->stream;
1469   break;
1470  default:
1471    return -1;
1472   break;
1473 }
1474
1475 ROAR_DBG("req_on_get_vol(*): stream=%i", stream);
1476
1477 // TODO: change this code.
1478 //       we should not directly change the stream object but use some stream_*()-func
1479 //       for that job.
1480
1481 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
1482  return -1;
1483
1484 s = g_streams[stream];
1485
1486 if ( s == NULL )
1487  return -1;
1488
1489 ROAR_DBG("req_on_get_vol(*): s=%p", s);
1490
1491 // ok, we have everything
1492
1493 info[0] = ROAR_HOST2NET16(version);
1494
1495 switch (version) {
1496  case 0:
1497    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1498
1499    for (i = 0; i < chans; i++)
1500     info[2+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1501
1502     mes->datalen = (2 + chans)*2;
1503   break;
1504  case 1:
1505    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1506    info[2] = ROAR_HOST2NET16(s->mixer.scale);
1507    info[3] = ROAR_HOST2NET16(s->mixer.rpg_mul);
1508    info[4] = ROAR_HOST2NET16(s->mixer.rpg_div);
1509
1510    for (i = 0; i < chans; i++)
1511     info[5+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1512
1513     mes->datalen = (5 + chans)*2;
1514   break;
1515  default:
1516    return -1;
1517   break;
1518 }
1519
1520 mes->cmd = ROAR_CMD_OK;
1521
1522 return 0;
1523}
1524
1525int req_on_add_data (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1526 struct roar_buffer * b;
1527 void               * buf;
1528
1529 if ( roar_buffer_new_data(&b, mes->datalen, &buf) == -1 ) {
1530  ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!");
1531  ROAR_DBG("req_on_add_data(*) = -1");
1532  return -1;
1533 }
1534
1535 if ( data == NULL ) {
1536  memcpy(buf, mes->data, mes->datalen);
1537 } else {
1538  memcpy(buf, *data, mes->datalen);
1539 }
1540
1541 if ( stream_add_buffer(mes->stream, b) == -1 ) {
1542  roar_buffer_free(b);
1543  return -1;
1544 }
1545
1546 mes->cmd     = ROAR_CMD_OK_STOP;
1547 mes->datalen = 0;
1548
1549 return 0;
1550}
1551
1552int req_on_beep        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1553 struct roar_beep bs;
1554 int16_t * info = (int16_t*)mes->data;
1555 int stream;
1556
1557 memset(&bs, 0, sizeof(bs));
1558
1559 if ( mes->datalen > 0 ) {
1560  if ( mes->datalen < 2 )
1561   return -1;
1562
1563  if ( ROAR_NET2HOST16(info[0]) != 0 ) /* version */
1564   return -1;
1565
1566  if ( mes->datalen != 8*2 )
1567   return -1;
1568
1569  bs.vol  = ROAR_NET2HOST16(info[1]);
1570  bs.time = ROAR_NET2HOST16(info[2]);
1571  bs.freq = ROAR_NET2HOST16(info[3]);
1572  bs.type = ROAR_NET2HOST16(info[4]);
1573  bs.x    = ROAR_NET2HOST16(info[5]);
1574  bs.y    = ROAR_NET2HOST16(info[6]);
1575  bs.z    = ROAR_NET2HOST16(info[7]);
1576 }
1577
1578 if ( (stream = beep_start(client, &bs)) == -1 )
1579  return -1;
1580
1581 mes->stream  = stream;
1582 mes->cmd     = ROAR_CMD_OK_STOP;
1583 mes->datalen = 0;
1584
1585 return 0;
1586}
1587
1588int req_on_wait        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1589 uint16_t * u16 = (uint16_t*)mes->data;
1590 struct roar_event events[4];
1591 size_t left, tmp;
1592 size_t num = 0;
1593 void * vp = mes->data;
1594
1595 vp += 4;
1596
1597 // check for complet header...
1598 if ( mes->datalen < 4 )
1599  return -1;
1600
1601 u16[0] = ROAR_NET2HOST16(u16[0]);
1602 u16[1] = ROAR_NET2HOST16(u16[1]);
1603
1604 // do we support version and flags?
1605 if ( u16[0] != 0 || u16[1] != 0 )
1606  return -1;
1607
1608 memset(events, 0, sizeof(events));
1609
1610 left = mes->datalen - 4;
1611
1612 while (left) {
1613  tmp = left;
1614  if ( roar_event_from_blob(&(events[num]), vp, &tmp) == -1 )
1615   return -1;
1616
1617  vp   += tmp;
1618  left -= tmp;
1619  num++;
1620 }
1621
1622 if ( clients_wait(client, events, num) == -1 )
1623  return -1;
1624
1625 flags[1] |= COMMAND_FLAG_OUT_NOSEND;
1626
1627 return 0;
1628}
1629
1630//ll
Note: See TracBrowser for help on using the repository browser.