source: roaraudio/roard/req.c @ 4552:47a0412f706d

Last change on this file since 4552:47a0412f706d was 4552:47a0412f706d, checked in by phi, 13 years ago

implemented flag toggling and flag protection

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