source: roaraudio/roard/req.c @ 4500:07a9fe743aa1

Last change on this file since 4500:07a9fe743aa1 was 4500:07a9fe743aa1, checked in by phi, 13 years ago

done some basic work for stds listing

File size: 36.8 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(stream, (struct roar_stream_server **)&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(ROAR_STREAM(s)->pos_rel_id, (struct roar_stream_server **)&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(ROAR_STREAM(s)->pos_rel_id, (struct roar_stream_server **)&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, 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(1)) == NULL )
667     return -1;
668    stds->stds[0] = 0x11223344;
669
670    for ( i = 0; i < stds->stds_len; i++) {
671     stds->stds[i] = ROAR_HOST2NET32(stds->stds[i]);
672    }
673
674    caps.data = stds->stds;
675    caps.len  = stds->stds_len * 4;
676    // TODO: add support for **data.
677    if ( roar_caps_to_msg(mes, &caps, NULL) == -1 ) {
678     roar_stds_free(stds);
679     return -1;
680    }
681    roar_stds_free(stds);
682   break;
683  default:
684    return -1;
685   break;
686 }
687
688 mes->cmd = ROAR_CMD_OK;
689
690 return 0;
691}
692
693int req_on_get_standby (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
694 mes->cmd     = ROAR_CMD_OK;
695 mes->pos     = g_pos;
696 mes->datalen = 2;
697
698 *((uint16_t*)mes->data) = ROAR_HOST2NET16((unsigned) g_standby);
699
700 return 0;
701}
702
703int req_on_set_standby (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
704 if ( mes->datalen != 2 )
705  return -1;
706
707 g_standby = ROAR_NET2HOST16(*((uint16_t*)mes->data));
708
709 mes->cmd     = ROAR_CMD_OK;
710 mes->pos     = g_pos;
711 mes->datalen = 0;
712
713 return 0;
714}
715
716int req_on_exit      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
717 int term = 0;
718
719 if ( mes->datalen == 1 )
720  term = mes->data[0];
721
722 mes->cmd     = ROAR_CMD_OK;
723 mes->pos     = g_pos;
724 mes->datalen = 0;
725
726 ROAR_DBG("req_on_exit(*): term=%i", term);
727
728 if ( term ) {
729  cleanup_listen_socket(1);
730 } else {
731  alive = 0;
732 }
733
734 return 0;
735}
736
737int req_on_list_clients(int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
738 unsigned char filter, cmp;
739 uint32_t id;
740 int clients[ROAR_CLIENTS_MAX];
741 int i, c = 0;
742
743 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
744  return -1;
745
746 // TODO: add code to support filter
747 if ( filter != ROAR_CTL_FILTER_ANY )
748  return -1;
749
750 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
751  if ( g_clients[i] != NULL ) {
752   clients[c++] = i;
753  }
754 }
755
756 roar_ctl_ia2m(mes, clients, c);
757
758 mes->cmd = ROAR_CMD_OK;
759
760 return 0;
761}
762int req_on_list_streams(int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
763 unsigned char filter, cmp;
764 uint32_t id;
765 int streams[ROAR_STREAMS_MAX];
766 int i, c = 0;
767 int match;
768
769 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
770  return -1;
771
772 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
773  if ( g_streams[i] == NULL )
774   continue;
775
776  match = 0;
777
778  switch (filter) {
779   case ROAR_CTL_FILTER_ANY:
780     match = 1;
781    break;
782   case ROAR_CTL_FILTER_DIR:
783     match = roar_filter_match(cmp, id, ROAR_STREAM(g_streams[i])->dir);
784    break;
785   default: // unsupported filter...
786     return -1;
787    break;
788  }
789
790  if ( match )
791   streams[c++] = i;
792 }
793
794 roar_ctl_ia2m(mes, streams, c);
795
796 mes->cmd = ROAR_CMD_OK;
797
798 return 0;
799}
800
801int req_on_get_client  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
802 struct roar_client * c;
803
804 if ( mes->datalen != 1 )
805  return -1;
806
807 if ( clients_get(mes->data[0], &c) == -1 )
808  return -1;
809
810 mes->cmd = ROAR_CMD_OK;
811
812 return roar_ctl_c2m(mes, c);
813}
814
815int req_on_get_stream  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
816 struct roar_stream_server * s;
817
818 if ( mes->datalen != 1 )
819  return -1;
820
821 if ( streams_get(mes->data[0], &s) == -1 )
822  return -1;
823
824 mes->cmd = ROAR_CMD_OK;
825 mes->stream = mes->data[0];
826
827 return roar_stream_s2m(ROAR_STREAM(s), mes);
828}
829
830int req_on_get_stream_para (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
831 struct roar_stream * s;
832 struct roar_stream_server * ss;
833 struct roar_audio_info * audio_info;
834 struct roar_stream_ltm * ltm;
835 uint16_t * d  = (uint16_t *) mes->data;
836 int64_t * d64 = ( int64_t *) mes->data;
837 int64_t * d64ptr;
838 int i, h, k;
839 char * str;
840 size_t needed;
841 int test, bits;
842
843 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);
844
845 if ( mes->datalen < 4 )
846  return -1;
847
848 for (i = 0; i < 2; i++) {
849  d[i] = ROAR_NET2HOST16(d[i]);
850 }
851
852 if ( d[0] != 0 ) {
853  ROAR_WARN("req_on_get_stream_para(*): unsupported command version: %i, %i", (int)d[0], (int)d[1]);
854  return -1;
855 }
856
857 switch (d[1]) {
858  case ROAR_STREAM_PARA_INFO:
859    if ( streams_get(mes->stream, &ss) == -1 ) {
860     ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream);
861     return -1;
862    }
863
864    if ( streams_calc_delay(mes->stream) == -1 ) {
865     ROAR_WARN("req_on_get_stream_para(*): can not calc delay for stream %i", mes->stream);
866    }
867
868    s = ROAR_STREAM(ss);
869
870    audio_info = &(s->info);
871
872    mes->datalen = 2*12;
873
874    d[ 2] = ROAR_OUTPUT_CALC_OUTBUFSIZE(audio_info);
875    d[ 3] = ss->pre_underruns;
876    d[ 4] = ss->post_underruns;
877    d[ 5] = ss->codec_orgi;
878    d[ 6] = (ss->flags & 0xFFFF) | (ss->primary ? ROAR_FLAG_PRIMARY : 0) | (ss->driver_id != -1 ? ROAR_FLAG_OUTPUT : 0);
879    d[ 7] = ss->delay/1000;
880    d[ 8] = ss->state;
881    d[ 9] = (ss->flags & 0xFFFF0000) >> 16;
882    d[10] = ss->mixer_stream;
883    d[11] = ss->role;
884
885    ROAR_DBG("req_on_get_stream_para(*): ss->driver_id=%i", ss->driver_id);
886
887    ROAR_DBG("req_on_get_stream_para(*): delay=%i, send delay=%i", ss->delay, d[7]);
888
889    for (i = 0; i < mes->datalen/2; i++) {
890     d[i] = ROAR_HOST2NET16(d[i]);
891    }
892
893    mes->pos = s->pos;
894   break;
895
896  case ROAR_STREAM_PARA_NAME:
897   str = streams_get_name(mes->stream);
898
899   if ( str == NULL )
900    return -1;
901
902    mes->datalen = 4 + strlen(str);
903
904    if ( mes->datalen > LIBROAR_BUFFER_MSGDATA )
905     return -1;
906
907    strncpy(((char*)&(mes->data))+4, str, mes->datalen);
908
909    d[0] = ROAR_HOST2NET16(d[0]);
910    d[1] = ROAR_HOST2NET16(d[1]);
911   break;
912
913  case ROAR_STREAM_PARA_CHANMAP:
914    if ( streams_get(mes->stream, &ss) == -1 ) {
915     ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream);
916     return -1;
917    }
918
919    s = ROAR_STREAM(ss);
920
921    memcpy(&(mes->data[4]), ss->chanmap.in, s->info.channels);
922    mes->datalen = 2*2 + s->info.channels;
923
924    d[0] = ROAR_HOST2NET16(d[0]);
925    d[1] = ROAR_HOST2NET16(d[1]);
926   break;
927
928  case ROAR_STREAM_PARA_LTM:
929    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM request...", client);
930
931    if ( mes->datalen < (6 * 2) ) {
932     ROAR_ERR("req_on_get_stream_para(client=%i, ...): LTM request of client is corruped: message too short", client);
933     return -1;
934    }
935
936    for (i = 2; i < mes->datalen/2; i++) {
937     d[i] = ROAR_NET2HOST16(d[i]);
938    }
939
940    if ( d[2] != ROAR_LTM_SST_GET_RAW ) {
941     ROAR_ERR("req_on_get_stream_para(client=%i, ...): LTM request of client is corruped: unknown LTM subtype: %i", client, (int)d[2]);
942     return -1;
943    }
944
945    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM request of type GET_RAW", client);
946
947    test = d[5];
948    bits = 0;
949    while (test) {
950     if ( test & 0x1 )
951      bits++;
952
953     test >>= 1;
954    }
955
956    needed = 0;
957
958    if ( mes->stream == -1 ) {
959     ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM multi-stream request...", client);
960
961     for (i = 6; i < mes->datalen/2; i++) {
962      if ( (ltm = streams_ltm_get(d[i], d[5], d[3])) == NULL )
963       return -1;
964
965      needed += ltm->channels;
966     }
967    } else {
968     ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM single-stream request for stream %i...", client, mes->stream);
969     if ( (ltm = streams_ltm_get(mes->stream, d[5], d[3])) == NULL )
970      return -1;
971
972     needed = ltm->channels;
973    }
974
975    needed *= bits;
976
977    needed += mes->stream == -1 ? (mes->datalen/2) - 6 : 1;
978
979    ROAR_DBG("req_on_get_stream_para(client=%i, ...): data size for answer is %i 64 bit sub-packets", client, (int)needed);
980
981    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
982    d64 = _dataspace(mes, data, flags, needed * 8);
983    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
984
985    if ( (d = roar_mm_malloc(mes->datalen)) == NULL )
986     return -1;
987
988    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d=%p, data=%p{%p}", client, d, data, *data);
989
990
991    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
992    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
993    memcpy(d, mes->data, mes->datalen);
994    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
995
996    d64ptr = d64;
997
998    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
999    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1000
1001    if ( mes->stream == -1 ) {
1002     for (i = 6; i < mes->datalen/2; i++) {
1003      ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1004
1005      if ( (ltm = streams_ltm_get(d[i], d[5], d[3])) == NULL )
1006       return -1;
1007
1008      *d64ptr = ltm->channels & 0xFFFF;
1009       d64ptr++;
1010
1011      for (h = 0; h < ltm->channels; h++) {
1012       for (k = 0; k < ROAR_LTM_MTBITS; k++) {
1013        if ( d[5] & (1<<k) ) {
1014         switch (1<<k) {
1015          case ROAR_LTM_MT_RMS:
1016            *d64ptr = ltm->cur[h].rms;
1017           break;
1018          default:
1019            ROAR_ERR("req_on_get_stream_para(client=%i, ...): client requets unknown MT for LTM: bit %i", client, k);
1020         }
1021         d64ptr++;
1022        }
1023       }
1024      }
1025     }
1026    } else {
1027     if ( (ltm = streams_ltm_get(mes->stream, d[5], d[3])) == NULL )
1028      return -1;
1029
1030     *d64ptr = ltm->channels & 0xFFFF;
1031      d64ptr++;
1032
1033     for (h = 0; h < ltm->channels; h++) {
1034      for (k = 0; k < ROAR_LTM_MTBITS; k++) {
1035       if ( d[5] & (1<<k) ) {
1036        switch (1<<k) {
1037         case ROAR_LTM_MT_RMS:
1038           *d64ptr = ltm->cur[h].rms;
1039           ROAR_DBG("req_on_get_stream_para(client=%i, ...): rms=%lli to %p", client, (long long int)*d64ptr, d64ptr);
1040          break;
1041         default:
1042           ROAR_ERR("req_on_get_stream_para(client=%i, ...): client requets unknown MT for LTM: bit %i", client, k);
1043        }
1044        d64ptr++;
1045       }
1046      }
1047     }
1048    }
1049
1050    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1051
1052    roar_mm_free(d);
1053
1054    for (i = 0; i < needed; i++) {
1055     d64[i] = ROAR_HOST2NET64(d64[i]);
1056    }
1057
1058    mes->datalen = needed * 8;
1059    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM d64=%p, d64ptr=%p", client, d64, d64ptr);
1060    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM final message has %i byte of data", client, (int)mes->datalen);
1061    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1062    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM GET_RAW request: OK. returning...", client);
1063   break;
1064
1065  default:
1066    ROAR_WARN("req_on_get_stream_para(*): unsupported command: %i", d[1]);
1067    return -1;
1068 }
1069
1070 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);
1071 mes->cmd = ROAR_CMD_OK;
1072 return 0;
1073}
1074
1075int req_on_set_stream_para (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1076 uint16_t * d = (uint16_t *) mes->data;
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 )
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    if ( d[2] == ROAR_RESET_FLAG ) {
1100     if ( streams_reset_flag(mes->stream, d[3]) == -1 )
1101      return -1;
1102    } else {
1103     if ( streams_set_flag(mes->stream, d[3]) == -1 )
1104      return -1;
1105    }
1106   break;
1107  case ROAR_STREAM_PARA_CHANMAP:
1108    if ( streams_set_map(mes->stream, &(mes->data[4]), mes->datalen - 4) == -1 )
1109     return -1;
1110   break;
1111  case ROAR_STREAM_PARA_ROLE:
1112    if ( mes->datalen != 2*3 )
1113     return -1;
1114
1115    d[2] = ROAR_NET2HOST16(d[2]);
1116
1117    if ( streams_set_role(mes->stream, d[2]) == -1 )
1118     return -1;
1119   break;
1120  case ROAR_STREAM_PARA_LTM:
1121    if ( mes->datalen < (6 * 2) )
1122     return -1;
1123
1124    for (i = 2; i < mes->datalen/2; i++) {
1125     d[i] = ROAR_NET2HOST16(d[i]);
1126    }
1127
1128    if ( mes->stream == -1 ) {
1129     for (i = 6; i < mes->datalen/2; i++)
1130      if ( streams_ltm_ctl(d[i], d[5], d[3], d[2]) == -1 )
1131       return -1;
1132    } else {
1133     if ( streams_ltm_ctl(mes->stream, d[5], d[3], d[2]) == -1 )
1134      return -1;
1135    }
1136   break;
1137  default:
1138    ROAR_WARN("req_on_set_stream_para(*): unsupported command version: %i, %i", d[0], d[1]);
1139    return -1;
1140   break;
1141 }
1142
1143 mes->cmd     = ROAR_CMD_OK;
1144 mes->datalen = 0;
1145
1146 return 0;
1147}
1148
1149int req_on_kick (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1150 struct roar_stream_server * ss;
1151 uint16_t * info = (uint16_t *) mes->data;
1152 int is_stream = 0;
1153
1154 if ( mes->datalen != 4 )
1155  return -1;
1156
1157 info[0] = ROAR_NET2HOST16(info[0]);
1158 info[1] = ROAR_NET2HOST16(info[1]);
1159
1160 switch (info[0]) {
1161  case ROAR_OT_CLIENT:
1162    clients_delete(info[1]);
1163   break;
1164  case ROAR_OT_STREAM:
1165    is_stream = 1;
1166   break;
1167  case ROAR_OT_SOURCE:
1168    if ( streams_get_flag(info[1], ROAR_FLAG_SOURCE) != 1 )
1169     return -1;
1170    is_stream = 1;
1171   break;
1172  case ROAR_OT_OUTPUT:
1173    if ( streams_get(info[1], &ss) == -1 )
1174     return -1;
1175
1176    if ( ss->driver_id == -1 )
1177     return -1;
1178
1179    is_stream = 1;
1180   break;
1181  case ROAR_OT_MIXER:
1182    if ( streams_get(info[1], &ss) == -1 )
1183     return -1;
1184
1185    if ( ROAR_STREAM(ss)->dir != ROAR_DIR_MIXING )
1186     return -1;
1187
1188    is_stream = 1;
1189   break;
1190  case ROAR_OT_BRIDGE:
1191    if ( streams_get(info[1], &ss) == -1 )
1192     return -1;
1193
1194    if ( ROAR_STREAM(ss)->dir != ROAR_DIR_BRIDGE )
1195     return -1;
1196
1197    is_stream = 1;
1198   break;
1199  default:
1200/* TODO: those types should be handled, too:
1201#define ROAR_OT_SAMPLE    4
1202#define ROAR_OT_LISTEN    8
1203#define ROAR_OT_ACTION    9
1204#define ROAR_OT_MSGQUEUE 10
1205#define ROAR_OT_MSGBUS   11
1206*/
1207    return -1;
1208   break;
1209 }
1210
1211 if ( is_stream ) {
1212  if ( streams_get_flag(info[1], ROAR_FLAG_IMMUTABLE) == 1 )
1213   return -1;
1214
1215  streams_delete(info[1]);
1216 }
1217
1218 mes->cmd     = ROAR_CMD_OK;
1219 mes->datalen = 0;
1220
1221 return 0;
1222}
1223
1224int req_on_attach      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1225 uint16_t * info = (uint16_t *) mes->data;
1226
1227 if ( mes->datalen < 6 )
1228  return -1;
1229
1230 info[0] = ROAR_NET2HOST16(info[0]);
1231 info[1] = ROAR_NET2HOST16(info[1]);
1232 info[2] = ROAR_NET2HOST16(info[2]);
1233
1234 if ( info[0] != 0 )
1235  return -1;
1236
1237 if ( info[1] == ROAR_ATTACH_SIMPLE ) {
1238  if ( client_stream_move(info[2], mes->stream) == -1 )
1239   return -1;
1240 } else {
1241  return -1;
1242 }
1243
1244 mes->cmd     = ROAR_CMD_OK;
1245 mes->datalen = 0;
1246
1247 return 0;
1248}
1249
1250int req_on_set_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1251 struct roar_stream_server * s;
1252 uint16_t * info = (uint16_t *) mes->data;
1253 uint16_t   version;
1254 uint16_t   scale = 65535;
1255 int stream;
1256 int i;
1257 int chans;
1258
1259 ROAR_DBG("req_on_set_vol(*) = ?");
1260 ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen);
1261
1262 if ( mes->datalen < (4*2) )
1263  return -1;
1264
1265 version = ROAR_NET2HOST16(info[0]);
1266 ROAR_DBG("req_on_set_vol(*): version=%i", (int)version);
1267
1268 switch (version) {
1269  case 0:
1270    stream = ROAR_NET2HOST16(info[1]);
1271   break;
1272  case 1:
1273    stream = mes->stream;
1274    scale  = ROAR_NET2HOST16(info[1]);
1275   break;
1276  default:
1277    return -1;
1278   break;
1279 }
1280 ROAR_DBG("req_on_set_vol(*): stream=%i", stream);
1281
1282 if ( scale == 0 )
1283  return -1;
1284
1285 // TODO: change this code.
1286 //       we should not directly change the stream object but use some stream_*()-func
1287 //       for that job.
1288
1289 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
1290  return -1;
1291
1292 s = g_streams[stream];
1293
1294 if ( s == NULL )
1295  return -1;
1296
1297 ROAR_DBG("req_on_set_vol(*): s=%p", s);
1298
1299 info[2] = ROAR_NET2HOST16(info[2]);
1300
1301 if ( info[2] == ROAR_SET_VOL_ALL ) {
1302  chans = (mes->datalen/2) - 3;
1303  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans);
1304
1305  if ( chans >= ROAR_MAX_CHANNELS )
1306   return -1;
1307
1308  ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer);
1309
1310  for (i = 0; i < chans; i++) {
1311   s->mixer.mixer[i] = ROAR_NET2HOST16(info[i+3]);
1312   ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, ROAR_NET2HOST16(info[i+3]));
1313  }
1314
1315  s->mixer.scale = scale;
1316
1317  ROAR_DBG("req_on_set_vol(*): mixer changed!");
1318
1319 } else if ( info[2] == ROAR_SET_VOL_ONE ) {
1320  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE");
1321  if ( ROAR_NET2HOST16(info[3]) >= ROAR_MAX_CHANNELS )
1322   return -1;
1323
1324  s->mixer.mixer[ROAR_NET2HOST16(info[3])] = ROAR_NET2HOST16(info[4]);
1325
1326  s->mixer.scale = scale;
1327 } else {
1328  return -1;
1329 }
1330
1331 if ( streams_set_mixer(stream) == -1 )
1332  return -1;
1333
1334 mes->cmd     = ROAR_CMD_OK;
1335 mes->datalen = 0;
1336
1337 return 0;
1338}
1339
1340int req_on_get_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1341 uint16_t * info = (uint16_t *) mes->data;
1342 uint16_t   version = -1;
1343 int stream;
1344 struct roar_stream_server * s;
1345 int i;
1346 int chans;
1347
1348 ROAR_DBG("req_on_get_vol(*) = ?");
1349 ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen);
1350
1351 if ( mes->datalen < 2 ) {
1352  return -1;
1353 }
1354
1355 version = ROAR_NET2HOST16(info[0]);
1356
1357 switch (version) {
1358  case 0:
1359    if ( mes->datalen < (2*2) )
1360     return -1;
1361
1362    stream = ROAR_NET2HOST16(info[1]);
1363   break;
1364  case 1:
1365    stream = mes->stream;
1366   break;
1367  default:
1368    return -1;
1369   break;
1370 }
1371
1372 ROAR_DBG("req_on_get_vol(*): stream=%i", stream);
1373
1374 // TODO: change this code.
1375 //       we should not directly change the stream object but use some stream_*()-func
1376 //       for that job.
1377
1378 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
1379  return -1;
1380
1381 s = g_streams[stream];
1382
1383 if ( s == NULL )
1384  return -1;
1385
1386 ROAR_DBG("req_on_get_vol(*): s=%p", s);
1387
1388 // ok, we have everything
1389
1390 info[0] = ROAR_HOST2NET16(version);
1391
1392 switch (version) {
1393  case 0:
1394    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1395
1396    for (i = 0; i < chans; i++)
1397     info[2+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1398
1399     mes->datalen = (2 + chans)*2;
1400   break;
1401  case 1:
1402    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1403    info[2] = ROAR_HOST2NET16(s->mixer.scale);
1404    info[3] = ROAR_HOST2NET16(s->mixer.rpg_mul);
1405    info[4] = ROAR_HOST2NET16(s->mixer.rpg_div);
1406
1407    for (i = 0; i < chans; i++)
1408     info[5+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1409
1410     mes->datalen = (5 + chans)*2;
1411   break;
1412  default:
1413    return -1;
1414   break;
1415 }
1416
1417 mes->cmd = ROAR_CMD_OK;
1418
1419 return 0;
1420}
1421
1422int req_on_add_data (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1423 struct roar_buffer * b;
1424 char               * buf;
1425
1426 if ( roar_buffer_new_data(&b, mes->datalen, (void **)&buf) == -1 ) {
1427  ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!");
1428  ROAR_DBG("req_on_add_data(*) = -1");
1429  return -1;
1430 }
1431
1432 if ( data == NULL ) {
1433  memcpy(buf, mes->data, mes->datalen);
1434 } else {
1435  memcpy(buf, *data, mes->datalen);
1436 }
1437
1438 if ( stream_add_buffer(mes->stream, b) == -1 ) {
1439  roar_buffer_free(b);
1440  return -1;
1441 }
1442
1443 mes->cmd     = ROAR_CMD_OK_STOP;
1444 mes->datalen = 0;
1445
1446 return 0;
1447}
1448
1449int req_on_beep        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1450 struct roar_beep bs;
1451 int16_t * info = (int16_t*)mes->data;
1452 int stream;
1453
1454 memset(&bs, 0, sizeof(bs));
1455
1456 if ( mes->datalen > 0 ) {
1457  if ( mes->datalen < 2 )
1458   return -1;
1459
1460  if ( ROAR_NET2HOST16(info[0]) != 0 ) /* version */
1461   return -1;
1462
1463  if ( mes->datalen != 8*2 )
1464   return -1;
1465
1466  bs.vol  = ROAR_NET2HOST16(info[1]);
1467  bs.time = ROAR_NET2HOST16(info[2]);
1468  bs.freq = ROAR_NET2HOST16(info[3]);
1469  bs.type = ROAR_NET2HOST16(info[4]);
1470  bs.x    = ROAR_NET2HOST16(info[5]);
1471  bs.y    = ROAR_NET2HOST16(info[6]);
1472  bs.z    = ROAR_NET2HOST16(info[7]);
1473 }
1474
1475 if ( (stream = beep_start(client, &bs)) == -1 )
1476  return -1;
1477
1478 mes->stream  = stream;
1479 mes->cmd     = ROAR_CMD_OK_STOP;
1480 mes->datalen = 0;
1481
1482 return 0;
1483}
1484
1485int req_on_wait        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1486 uint16_t * u16 = (uint16_t*)mes->data;
1487 struct roar_event events[4];
1488 size_t left, tmp;
1489 size_t num = 0;
1490 void * vp = mes->data;
1491
1492 vp += 4;
1493
1494 // check for complet header...
1495 if ( mes->datalen < 4 )
1496  return -1;
1497
1498 u16[0] = ROAR_NET2HOST16(u16[0]);
1499 u16[1] = ROAR_NET2HOST16(u16[1]);
1500
1501 // do we support version and flags?
1502 if ( u16[0] != 0 || u16[1] != 0 )
1503  return -1;
1504
1505 memset(events, 0, sizeof(events));
1506
1507 left = mes->datalen - 4;
1508
1509 while (left) {
1510  tmp = left;
1511  if ( roar_event_from_blob(&(events[num]), vp, &tmp) == -1 )
1512   return -1;
1513
1514  vp   += tmp;
1515  left -= tmp;
1516  num++;
1517 }
1518
1519 if ( clients_wait(client, events, num) == -1 )
1520  return -1;
1521
1522 flags[1] |= COMMAND_FLAG_OUT_NOSEND;
1523
1524 return 0;
1525}
1526
1527//ll
Note: See TracBrowser for help on using the repository browser.