source: roaraudio/roard/req.c @ 4521:4277b6a0c8a1

Last change on this file since 4521:4277b6a0c8a1 was 4521:4277b6a0c8a1, checked in by phi, 14 years ago

fixed a lot compiler warnings

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