source: roaraudio/roard/req.c @ 5216:e16c15116d46

Last change on this file since 5216:e16c15116d46 was 5216:e16c15116d46, checked in by phi, 12 years ago

moved roar_err_{build,parse}msg2() -> roar_err_{build,parse}msg(), see #184

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