source: roaraudio/roard/req.c @ 4539:62d85b4e007e

Last change on this file since 4539:62d85b4e007e was 4539:62d85b4e007e, checked in by phi, 13 years ago

wrong var name

File size: 36.9 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 uint16_t * d = (uint16_t *) mes->data;
1076 uint32_t tmp;
1077 int i;
1078
1079 if ( mes->datalen < 2*2 )
1080  return -1;
1081
1082 for (i = 0; i < 2; i++) {
1083  d[i] = ROAR_NET2HOST16(d[i]);
1084 }
1085
1086 if ( d[0] != 0 )
1087  return -1;
1088
1089 switch (d[1]) {
1090  case ROAR_STREAM_PARA_FLAGS:
1091    if ( mes->datalen != 2*4 && mes->datalen != 2*5 )
1092     return -1;
1093
1094    d[2] = ROAR_NET2HOST16(d[2]);
1095    d[3] = ROAR_NET2HOST16(d[3]);
1096
1097    ROAR_DBG("req_on_set_stream_para(*): request seems to be valid");
1098
1099    tmp = 0;
1100
1101    if ( mes->datalen == 2*5 ) {
1102     d[4] = ROAR_NET2HOST16(d[4]);
1103     tmp   = d[4];
1104     tmp <<= 16;
1105    }
1106
1107    tmp |= d[3];
1108
1109    if ( d[2] == ROAR_RESET_FLAG ) {
1110     if ( streams_reset_flag(mes->stream, tmp) == -1 )
1111      return -1;
1112    } else {
1113     if ( streams_set_flag(mes->stream, tmp) == -1 )
1114      return -1;
1115    }
1116   break;
1117  case ROAR_STREAM_PARA_CHANMAP:
1118    if ( streams_set_map(mes->stream, &(mes->data[4]), mes->datalen - 4) == -1 )
1119     return -1;
1120   break;
1121  case ROAR_STREAM_PARA_ROLE:
1122    if ( mes->datalen != 2*3 )
1123     return -1;
1124
1125    d[2] = ROAR_NET2HOST16(d[2]);
1126
1127    if ( streams_set_role(mes->stream, d[2]) == -1 )
1128     return -1;
1129   break;
1130  case ROAR_STREAM_PARA_LTM:
1131    if ( mes->datalen < (6 * 2) )
1132     return -1;
1133
1134    for (i = 2; i < mes->datalen/2; i++) {
1135     d[i] = ROAR_NET2HOST16(d[i]);
1136    }
1137
1138    if ( mes->stream == -1 ) {
1139     for (i = 6; i < mes->datalen/2; i++)
1140      if ( streams_ltm_ctl(d[i], d[5], d[3], d[2]) == -1 )
1141       return -1;
1142    } else {
1143     if ( streams_ltm_ctl(mes->stream, d[5], d[3], d[2]) == -1 )
1144      return -1;
1145    }
1146   break;
1147  default:
1148    ROAR_WARN("req_on_set_stream_para(*): unsupported command version: %i, %i", d[0], d[1]);
1149    return -1;
1150   break;
1151 }
1152
1153 mes->cmd     = ROAR_CMD_OK;
1154 mes->datalen = 0;
1155
1156 return 0;
1157}
1158
1159int req_on_kick (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1160 struct roar_stream_server * ss;
1161 uint16_t * info = (uint16_t *) mes->data;
1162 int is_stream = 0;
1163
1164 if ( mes->datalen != 4 )
1165  return -1;
1166
1167 info[0] = ROAR_NET2HOST16(info[0]);
1168 info[1] = ROAR_NET2HOST16(info[1]);
1169
1170 switch (info[0]) {
1171  case ROAR_OT_CLIENT:
1172    clients_delete(info[1]);
1173   break;
1174  case ROAR_OT_STREAM:
1175    is_stream = 1;
1176   break;
1177  case ROAR_OT_SOURCE:
1178    if ( streams_get_flag(info[1], ROAR_FLAG_SOURCE) != 1 )
1179     return -1;
1180    is_stream = 1;
1181   break;
1182  case ROAR_OT_OUTPUT:
1183    if ( streams_get(info[1], &ss) == -1 )
1184     return -1;
1185
1186    if ( ss->driver_id == -1 )
1187     return -1;
1188
1189    is_stream = 1;
1190   break;
1191  case ROAR_OT_MIXER:
1192    if ( streams_get(info[1], &ss) == -1 )
1193     return -1;
1194
1195    if ( ROAR_STREAM(ss)->dir != ROAR_DIR_MIXING )
1196     return -1;
1197
1198    is_stream = 1;
1199   break;
1200  case ROAR_OT_BRIDGE:
1201    if ( streams_get(info[1], &ss) == -1 )
1202     return -1;
1203
1204    if ( ROAR_STREAM(ss)->dir != ROAR_DIR_BRIDGE )
1205     return -1;
1206
1207    is_stream = 1;
1208   break;
1209  default:
1210/* TODO: those types should be handled, too:
1211#define ROAR_OT_SAMPLE    4
1212#define ROAR_OT_LISTEN    8
1213#define ROAR_OT_ACTION    9
1214#define ROAR_OT_MSGQUEUE 10
1215#define ROAR_OT_MSGBUS   11
1216*/
1217    return -1;
1218   break;
1219 }
1220
1221 if ( is_stream ) {
1222  if ( streams_get_flag(info[1], ROAR_FLAG_IMMUTABLE) == 1 )
1223   return -1;
1224
1225  streams_delete(info[1]);
1226 }
1227
1228 mes->cmd     = ROAR_CMD_OK;
1229 mes->datalen = 0;
1230
1231 return 0;
1232}
1233
1234int req_on_attach      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1235 uint16_t * info = (uint16_t *) mes->data;
1236
1237 if ( mes->datalen < 6 )
1238  return -1;
1239
1240 info[0] = ROAR_NET2HOST16(info[0]);
1241 info[1] = ROAR_NET2HOST16(info[1]);
1242 info[2] = ROAR_NET2HOST16(info[2]);
1243
1244 if ( info[0] != 0 )
1245  return -1;
1246
1247 if ( info[1] == ROAR_ATTACH_SIMPLE ) {
1248  if ( client_stream_move(info[2], mes->stream) == -1 )
1249   return -1;
1250 } else {
1251  return -1;
1252 }
1253
1254 mes->cmd     = ROAR_CMD_OK;
1255 mes->datalen = 0;
1256
1257 return 0;
1258}
1259
1260int req_on_set_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1261 struct roar_stream_server * s;
1262 uint16_t * info = (uint16_t *) mes->data;
1263 uint16_t   version;
1264 uint16_t   scale = 65535;
1265 int stream;
1266 int i;
1267 int chans;
1268
1269 ROAR_DBG("req_on_set_vol(*) = ?");
1270 ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen);
1271
1272 if ( mes->datalen < (4*2) )
1273  return -1;
1274
1275 version = ROAR_NET2HOST16(info[0]);
1276 ROAR_DBG("req_on_set_vol(*): version=%i", (int)version);
1277
1278 switch (version) {
1279  case 0:
1280    stream = ROAR_NET2HOST16(info[1]);
1281   break;
1282  case 1:
1283    stream = mes->stream;
1284    scale  = ROAR_NET2HOST16(info[1]);
1285   break;
1286  default:
1287    return -1;
1288   break;
1289 }
1290 ROAR_DBG("req_on_set_vol(*): stream=%i", stream);
1291
1292 if ( scale == 0 )
1293  return -1;
1294
1295 // TODO: change this code.
1296 //       we should not directly change the stream object but use some stream_*()-func
1297 //       for that job.
1298
1299 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
1300  return -1;
1301
1302 s = g_streams[stream];
1303
1304 if ( s == NULL )
1305  return -1;
1306
1307 ROAR_DBG("req_on_set_vol(*): s=%p", s);
1308
1309 info[2] = ROAR_NET2HOST16(info[2]);
1310
1311 if ( info[2] == ROAR_SET_VOL_ALL ) {
1312  chans = (mes->datalen/2) - 3;
1313  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans);
1314
1315  if ( chans >= ROAR_MAX_CHANNELS )
1316   return -1;
1317
1318  ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer);
1319
1320  for (i = 0; i < chans; i++) {
1321   s->mixer.mixer[i] = ROAR_NET2HOST16(info[i+3]);
1322   ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, ROAR_NET2HOST16(info[i+3]));
1323  }
1324
1325  s->mixer.scale = scale;
1326
1327  ROAR_DBG("req_on_set_vol(*): mixer changed!");
1328
1329 } else if ( info[2] == ROAR_SET_VOL_ONE ) {
1330  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE");
1331  if ( ROAR_NET2HOST16(info[3]) >= ROAR_MAX_CHANNELS )
1332   return -1;
1333
1334  s->mixer.mixer[ROAR_NET2HOST16(info[3])] = ROAR_NET2HOST16(info[4]);
1335
1336  s->mixer.scale = scale;
1337 } else {
1338  return -1;
1339 }
1340
1341 if ( streams_set_mixer(stream) == -1 )
1342  return -1;
1343
1344 mes->cmd     = ROAR_CMD_OK;
1345 mes->datalen = 0;
1346
1347 return 0;
1348}
1349
1350int req_on_get_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1351 uint16_t * info = (uint16_t *) mes->data;
1352 uint16_t   version = -1;
1353 int stream;
1354 struct roar_stream_server * s;
1355 int i;
1356 int chans;
1357
1358 ROAR_DBG("req_on_get_vol(*) = ?");
1359 ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen);
1360
1361 if ( mes->datalen < 2 ) {
1362  return -1;
1363 }
1364
1365 version = ROAR_NET2HOST16(info[0]);
1366
1367 switch (version) {
1368  case 0:
1369    if ( mes->datalen < (2*2) )
1370     return -1;
1371
1372    stream = ROAR_NET2HOST16(info[1]);
1373   break;
1374  case 1:
1375    stream = mes->stream;
1376   break;
1377  default:
1378    return -1;
1379   break;
1380 }
1381
1382 ROAR_DBG("req_on_get_vol(*): stream=%i", stream);
1383
1384 // TODO: change this code.
1385 //       we should not directly change the stream object but use some stream_*()-func
1386 //       for that job.
1387
1388 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
1389  return -1;
1390
1391 s = g_streams[stream];
1392
1393 if ( s == NULL )
1394  return -1;
1395
1396 ROAR_DBG("req_on_get_vol(*): s=%p", s);
1397
1398 // ok, we have everything
1399
1400 info[0] = ROAR_HOST2NET16(version);
1401
1402 switch (version) {
1403  case 0:
1404    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1405
1406    for (i = 0; i < chans; i++)
1407     info[2+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1408
1409     mes->datalen = (2 + chans)*2;
1410   break;
1411  case 1:
1412    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1413    info[2] = ROAR_HOST2NET16(s->mixer.scale);
1414    info[3] = ROAR_HOST2NET16(s->mixer.rpg_mul);
1415    info[4] = ROAR_HOST2NET16(s->mixer.rpg_div);
1416
1417    for (i = 0; i < chans; i++)
1418     info[5+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1419
1420     mes->datalen = (5 + chans)*2;
1421   break;
1422  default:
1423    return -1;
1424   break;
1425 }
1426
1427 mes->cmd = ROAR_CMD_OK;
1428
1429 return 0;
1430}
1431
1432int req_on_add_data (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1433 struct roar_buffer * b;
1434 void               * buf;
1435
1436 if ( roar_buffer_new_data(&b, mes->datalen, &buf) == -1 ) {
1437  ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!");
1438  ROAR_DBG("req_on_add_data(*) = -1");
1439  return -1;
1440 }
1441
1442 if ( data == NULL ) {
1443  memcpy(buf, mes->data, mes->datalen);
1444 } else {
1445  memcpy(buf, *data, mes->datalen);
1446 }
1447
1448 if ( stream_add_buffer(mes->stream, b) == -1 ) {
1449  roar_buffer_free(b);
1450  return -1;
1451 }
1452
1453 mes->cmd     = ROAR_CMD_OK_STOP;
1454 mes->datalen = 0;
1455
1456 return 0;
1457}
1458
1459int req_on_beep        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1460 struct roar_beep bs;
1461 int16_t * info = (int16_t*)mes->data;
1462 int stream;
1463
1464 memset(&bs, 0, sizeof(bs));
1465
1466 if ( mes->datalen > 0 ) {
1467  if ( mes->datalen < 2 )
1468   return -1;
1469
1470  if ( ROAR_NET2HOST16(info[0]) != 0 ) /* version */
1471   return -1;
1472
1473  if ( mes->datalen != 8*2 )
1474   return -1;
1475
1476  bs.vol  = ROAR_NET2HOST16(info[1]);
1477  bs.time = ROAR_NET2HOST16(info[2]);
1478  bs.freq = ROAR_NET2HOST16(info[3]);
1479  bs.type = ROAR_NET2HOST16(info[4]);
1480  bs.x    = ROAR_NET2HOST16(info[5]);
1481  bs.y    = ROAR_NET2HOST16(info[6]);
1482  bs.z    = ROAR_NET2HOST16(info[7]);
1483 }
1484
1485 if ( (stream = beep_start(client, &bs)) == -1 )
1486  return -1;
1487
1488 mes->stream  = stream;
1489 mes->cmd     = ROAR_CMD_OK_STOP;
1490 mes->datalen = 0;
1491
1492 return 0;
1493}
1494
1495int req_on_wait        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1496 uint16_t * u16 = (uint16_t*)mes->data;
1497 struct roar_event events[4];
1498 size_t left, tmp;
1499 size_t num = 0;
1500 void * vp = mes->data;
1501
1502 vp += 4;
1503
1504 // check for complet header...
1505 if ( mes->datalen < 4 )
1506  return -1;
1507
1508 u16[0] = ROAR_NET2HOST16(u16[0]);
1509 u16[1] = ROAR_NET2HOST16(u16[1]);
1510
1511 // do we support version and flags?
1512 if ( u16[0] != 0 || u16[1] != 0 )
1513  return -1;
1514
1515 memset(events, 0, sizeof(events));
1516
1517 left = mes->datalen - 4;
1518
1519 while (left) {
1520  tmp = left;
1521  if ( roar_event_from_blob(&(events[num]), vp, &tmp) == -1 )
1522   return -1;
1523
1524  vp   += tmp;
1525  left -= tmp;
1526  num++;
1527 }
1528
1529 if ( clients_wait(client, events, num) == -1 )
1530  return -1;
1531
1532 flags[1] |= COMMAND_FLAG_OUT_NOSEND;
1533
1534 return 0;
1535}
1536
1537//ll
Note: See TracBrowser for help on using the repository browser.