source: roaraudio/libroar/ctl.c @ 3529:fba86b5b08a7

Last change on this file since 3529:fba86b5b08a7 was 3529:fba86b5b08a7, checked in by phi, 14 years ago

support to read complet mixer from roard, including scale and rpg

File size: 12.8 KB
Line 
1//ctl.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of libroar 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 *  libroar 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 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38int roar_get_clientid  (struct roar_connection * con) {
39 struct roar_message mes;
40
41 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
42
43 mes.cmd     = ROAR_CMD_WHOAMI;
44 mes.datalen = 0;
45
46 if ( roar_req(con, &mes, NULL) == -1 )
47  return -1;
48
49 if ( mes.cmd != ROAR_CMD_OK )
50  return -1;
51
52 if ( mes.datalen != 1 )
53  return -1;
54
55 return mes.data[0];
56}
57
58int roar_server_oinfo   (struct roar_connection * con, struct roar_stream * sa) {
59 struct roar_message mes;
60
61 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
62
63 mes.cmd     = ROAR_CMD_SERVER_OINFO;
64 mes.datalen = 0;
65
66 if ( roar_req(con, &mes, NULL) == -1 )
67  return -1;
68
69 if ( mes.cmd != ROAR_CMD_OK )
70  return -1;
71
72 if ( roar_stream_m2s(sa, &mes) == -1 )
73  return -1;
74
75 return 0;
76}
77
78int roar_get_standby   (struct roar_connection * con) {
79 struct roar_message mes;
80
81 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
82
83 mes.cmd = ROAR_CMD_GET_STANDBY;
84 mes.datalen = 0;
85
86 if ( roar_req(con, &mes, NULL) == -1 )
87  return -1;
88
89 if ( mes.cmd != ROAR_CMD_OK )
90  return -1;
91
92 return ROAR_NET2HOST16(*((uint16_t*)mes.data));
93}
94
95int roar_set_standby   (struct roar_connection * con, int state) {
96 struct roar_message mes;
97
98 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
99
100 mes.cmd = ROAR_CMD_SET_STANDBY;
101 mes.datalen = 2;
102
103 *((uint16_t*)mes.data) = ROAR_HOST2NET16((unsigned) state);
104
105 if ( roar_req(con, &mes, NULL) == -1 )
106  return -1;
107
108 if ( mes.cmd != ROAR_CMD_OK )
109  return -1;
110
111 return 0;
112}
113
114int roar_exit   (struct roar_connection * con) {
115 return roar_terminate(con, 0);
116}
117
118int roar_terminate (struct roar_connection * con, int terminate) {
119 struct roar_message mes;
120
121 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
122
123 mes.cmd     = ROAR_CMD_EXIT;
124 mes.datalen = 1;
125 mes.data[0] = terminate;
126
127 if ( roar_req(con, &mes, NULL) == -1 )
128  return -1;
129
130 if ( mes.cmd != ROAR_CMD_OK )
131  return -1;
132
133 return 0;
134}
135
136int roar_list         (struct roar_connection * con, int * items,   int max, int cmd) {
137 struct roar_message m;
138
139 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
140
141 roar_ctl_f2m_any(&m);
142 m.cmd = cmd;
143
144 if ( roar_req(con, &m, NULL) == -1 )
145  return -1;
146
147 return roar_ctl_m2ia(&m, items, max);
148}
149
150int roar_get_client   (struct roar_connection * con, struct roar_client * client, int id) {
151 struct roar_message m;
152
153 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
154
155 m.cmd     = ROAR_CMD_GET_CLIENT;
156 m.datalen = 1;
157 m.data[0] = id;
158
159 ROAR_DBG("roar_get_client(*): id = %i", id);
160
161 if ( roar_req(con, &m, NULL) == -1 )
162  return -1;
163
164 if ( m.cmd != ROAR_CMD_OK )
165  return -1;
166
167 ROAR_DBG("roar_get_client(*): got ok");
168
169 return roar_ctl_m2c(&m, client);
170}
171
172int roar_get_stream   (struct roar_connection * con, struct roar_stream * stream, int id) {
173 struct roar_message m;
174
175 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
176
177 m.cmd     = ROAR_CMD_GET_STREAM;
178 m.datalen = 1;
179 m.data[0] = id;
180
181 roar_errno = ROAR_ERROR_UNKNOWN;
182
183 if ( roar_req(con, &m, NULL) == -1 ) {
184  roar_errno = ROAR_ERROR_PROTO;
185  return -1;
186 }
187
188 if ( m.cmd != ROAR_CMD_OK )
189  return -1;
190
191 return roar_stream_m2s(stream, &m);
192}
193
194int roar_kick         (struct roar_connection * con, int type, int id) {
195 struct roar_message m;
196 uint16_t * info = (uint16_t *) m.data;
197
198 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
199
200 m.cmd     = ROAR_CMD_KICK;
201 m.datalen = 4;
202 info[0] = ROAR_HOST2NET16(type);
203 info[1] = ROAR_HOST2NET16(id);
204
205 if ( roar_req(con, &m, NULL) == -1 )
206  return -1;
207
208 if ( m.cmd != ROAR_CMD_OK )
209  return -1;
210
211 return 0;
212}
213
214int roar_set_vol      (struct roar_connection * con, int id, struct roar_mixer_settings * mixer, int channels) {
215 struct roar_message m;
216 uint16_t * info = (uint16_t *) m.data;
217 int i;
218
219 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
220
221 m.cmd     = ROAR_CMD_SET_VOL;
222 m.datalen = (3 + channels) * 2;
223 info[0] = 0;
224 info[1] = ROAR_HOST2NET16(id);
225 info[2] = ROAR_HOST2NET16(ROAR_SET_VOL_ALL);
226
227 for (i = 0; i < channels; i++)
228  info[i+3] = ROAR_HOST2NET16(mixer->mixer[i]);
229
230 if ( roar_req(con, &m, NULL) == -1 )
231  return -1;
232
233 if ( m.cmd != ROAR_CMD_OK )
234  return -1;
235
236 return 0;
237}
238
239int roar_get_vol      (struct roar_connection * con, int id, struct roar_mixer_settings * mixer, int * channels) {
240 struct roar_message m;
241 uint16_t * info = (uint16_t *) m.data;
242 int i;
243
244 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
245
246 m.cmd     = ROAR_CMD_GET_VOL;
247 m.datalen = 2*2;
248 m.stream  = id;
249
250 info[0]   = ROAR_HOST2NET16(1);
251
252 if ( roar_req(con, &m, NULL) == -1 )
253  return -1;
254
255 if ( m.cmd != ROAR_CMD_OK )
256  return -1;
257
258 if ( ROAR_NET2HOST16(info[0]) != 1 )
259  return -1;
260
261 info[1] = ROAR_NET2HOST16(info[1]);
262
263 if ( channels != NULL )
264  *channels = info[1];
265
266 if ( info[1] > ROAR_MAX_CHANNELS )
267  return -1;
268
269 mixer->scale   = ROAR_NET2HOST16(info[2]);
270 mixer->rpg_mul = ROAR_NET2HOST16(info[3]);
271 mixer->rpg_div = ROAR_NET2HOST16(info[4]);
272
273 for (i = 0; i < info[1]; i++)
274  mixer->mixer[i] = ROAR_NET2HOST16(info[i+5]);
275
276 return 0;
277}
278
279// converts: *_m2*, *_*2m
280
281int roar_ctl_f2m      (struct roar_message * m, unsigned char   filter, unsigned char   cmp, uint32_t   id) {
282
283 m->datalen = 7;
284
285 m->data[0] = 0;
286 m->data[1] = filter;
287 m->data[2] = cmp;
288 *((uint32_t*)&(m->data[3])) = ROAR_HOST2NET32(id);
289
290 return 0;
291}
292int roar_ctl_m2f      (struct roar_message * m, unsigned char * filter, unsigned char * cmp, uint32_t * id) {
293
294 if ( m->datalen != 7 )
295  return -1;
296
297 if ( m->data[0] != 0 ) {
298  ROAR_ERR("roar_ctl_m2f(*): version %i not supported!", m->data[0]);
299  return -1;
300 }
301
302 *filter = m->data[1];
303 *cmp    = m->data[2];
304
305 *id = ROAR_NET2HOST32(*((uint32_t*)&(m->data[3])));
306
307 return 0;
308}
309
310int roar_ctl_ia2m     (struct roar_message * m, int * data, int len) {
311 int i;
312
313 if ( len > LIBROAR_BUFFER_MSGDATA )
314  return -1;
315
316 m->datalen = len;
317
318 for (i = 0; i < len; i++)
319  m->data[i] = data[i];
320
321 return 0;
322}
323int roar_ctl_m2ia     (struct roar_message * m, int * data, int len) {
324 int i;
325
326 if ( m->datalen > len )
327  return -1;
328
329 for (i = 0; i < m->datalen; i++)
330  data[i] = m->data[i];
331
332 return m->datalen;
333}
334
335int roar_ctl_c2m      (struct roar_message * m, struct roar_client * c) {
336 int cur = 0;
337 int h;
338 int i;
339 int max_len;
340 uint32_t pid;
341 size_t len_rest;
342
343 if ( c == NULL )
344  return -1;
345
346 m->data[cur++] = 0;                       // 0: Version
347 m->data[cur++] = c->execed;               // 1: execed
348
349 h = 0;
350 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
351  if ( c->streams[i] != -1 )
352   m->data[cur+1+h++] = c->streams[i];
353 }
354
355 m->data[cur++] = h;                       // 2: num of streams
356 cur += h;
357
358 max_len = strlen(c->name);
359
360 // TODO: add some code to check if this fits in the pkg
361 // NOTE: add this code after we are sure how long this pkg will be
362 //       and fully decieded about this function.
363
364 m->data[cur++] = max_len;
365
366 strncpy((m->data)+cur, c->name, max_len);
367
368 cur += max_len;
369
370 pid = ROAR_HOST2NET32(c->pid);
371 memcpy(&(m->data[cur]), &pid, 4);
372 cur += 4;
373
374 pid = ROAR_HOST2NET32(c->uid);
375 memcpy(&(m->data[cur]), &pid, 4);
376 cur += 4;
377
378 pid = ROAR_HOST2NET32(c->gid);
379 memcpy(&(m->data[cur]), &pid, 4);
380 cur += 4;
381
382 pid = ROAR_HOST2NET32(c->proto);
383 memcpy(&(m->data[cur]), &pid, 4);
384 cur += 4;
385
386 pid = ROAR_HOST2NET32(c->byteorder);
387 memcpy(&(m->data[cur]), &pid, 4);
388 cur += 4;
389
390 len_rest = sizeof(m->data) - cur;
391 if ( roar_nnode_to_blob(&(c->nnode), &(m->data[cur]), &len_rest) == 0 ) {
392  cur += len_rest;
393 }
394
395 m->datalen = cur;
396
397 return 0;
398}
399
400int roar_ctl_m2c      (struct roar_message * m, struct roar_client * c) {
401 int i;
402 int cur;
403 uint32_t pid;
404 size_t len;
405
406 if ( m == NULL || c == NULL )
407  return -1;
408
409 if ( m->datalen == 0 )
410  return -1;
411
412 ROAR_DBG("roar_ctl_m2c(*): got data!, len = %i", m->datalen);
413
414 if ( m->data[0] != 0 ) {
415  ROAR_DBG("roar_ctl_m2c(*): wrong version!");
416  return -1;
417 }
418
419 if ( m->datalen < 3 )
420  return -1;
421
422 ROAR_DBG("roar_ctl_m2c(*): have usable data!");
423
424 c->execed = m->data[1];
425
426 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++)
427  c->streams[i] = -1;
428
429 for (i = 0; i < m->data[2]; i++)
430  c->streams[i] = m->data[3+i];
431
432 cur = 3 + m->data[2];
433
434 strncpy(c->name, (m->data)+cur+1, m->data[cur]);
435 c->name[(int)m->data[cur]] = 0;
436
437 cur += m->data[cur] + 1;
438
439 memcpy(&pid, &(m->data[cur]), 4);
440 c->pid = ROAR_NET2HOST32(pid);
441 cur += 4;
442
443 memcpy(&pid, &(m->data[cur]), 4);
444 c->uid = ROAR_NET2HOST32(pid);
445 cur += 4;
446
447 memcpy(&pid, &(m->data[cur]), 4);
448 c->gid = ROAR_NET2HOST32(pid);
449 cur += 4;
450
451 if ( m->datalen >= cur+4 ) {
452  memcpy(&pid, &(m->data[cur]), 4);
453  c->proto = ROAR_NET2HOST32(pid);
454  cur += 4;
455 } else {
456  c->proto = ROAR_PROTO_NONE;
457 }
458
459 if ( m->datalen >= cur+4 ) {
460  memcpy(&pid, &(m->data[cur]), 4);
461  c->byteorder = ROAR_NET2HOST32(pid);
462  cur += 4;
463 } else {
464  c->byteorder = ROAR_BYTEORDER_UNKNOWN;
465 }
466
467 if ( m->datalen > cur ) {
468  len = m->datalen - cur;
469  if ( roar_nnode_from_blob(&(c->nnode), &(m->data[cur]), &len) == 0 ) {
470   cur += len;
471  } else {
472   if ( roar_nnode_new(&(c->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 )
473    return -1;
474  }
475 } else {
476  if ( roar_nnode_new(&(c->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 )
477   return -1;
478 }
479
480 return 0;
481}
482
483int    roar_str2proto (char * proto) {
484 if ( !strcasecmp(proto, "roar") ) {
485  return ROAR_PROTO_ROARAUDIO;
486 } else if ( !strcasecmp(proto, "roaraudio") ) {
487  return ROAR_PROTO_ROARAUDIO;
488 } else if ( !strcasecmp(proto, "esd") ) {
489  return ROAR_PROTO_ESOUND;
490 } else if ( !strcasecmp(proto, "esound") ) {
491  return ROAR_PROTO_ESOUND;
492 } else if ( !strcasecmp(proto, "auto") ) {
493  return ROAR_PROTO_AUTO;
494 } else if ( !strcasecmp(proto, "(auto)") ) {
495  return ROAR_PROTO_AUTO;
496 } else if ( !strcasecmp(proto, "http") ) {
497  return ROAR_PROTO_HTTP;
498 } else if ( !strcasecmp(proto, "gopher") ) {
499  return ROAR_PROTO_GOPHER;
500 } else if ( !strcasecmp(proto, "icy") ) {
501  return ROAR_PROTO_ICY;
502 } else if ( !strcasecmp(proto, "simple") ) {
503  return ROAR_PROTO_SIMPLE;
504 }
505
506 return -1;
507}
508
509char * roar_proto2str (int    proto) {
510 switch (proto) {
511  case ROAR_PROTO_ROARAUDIO: return "RoarAudio"; break;
512  case ROAR_PROTO_ESOUND:    return "EsounD";    break;
513  case ROAR_PROTO_AUTO:      return "(auto)";    break;
514  case ROAR_PROTO_HTTP:      return "http";      break;
515  case ROAR_PROTO_GOPHER:    return "gopher";    break;
516  case ROAR_PROTO_ICY:       return "ICY";       break;
517  case ROAR_PROTO_SIMPLE:    return "Simple";    break;
518  default:
519    return "(unknown)";
520 }
521}
522
523int    roar_str2byteorder (char * byteorder) {
524 if (        !strcasecmp(byteorder, "le")            || !strcasecmp(byteorder, "little") ||
525             !strcasecmp(byteorder, "little endian") || !strcasecmp(byteorder, "1234")   ) {
526  return ROAR_BYTEORDER_LE;
527 } else if ( !strcasecmp(byteorder, "be")            || !strcasecmp(byteorder, "big")    ||
528             !strcasecmp(byteorder, "big endian")    || !strcasecmp(byteorder, "4321")   ) {
529  return ROAR_BYTEORDER_BE;
530 } else if ( !strcasecmp(byteorder, "pdp")           ||
531             !strcasecmp(byteorder, "pdp endian") ) {
532  return ROAR_BYTEORDER_PDP;
533 } else if ( !strcasecmp(byteorder, "network")       ||
534             !strcasecmp(byteorder, "network byteorder") ) {
535  return ROAR_BYTEORDER_NETWORK;
536 }
537
538 return -1;
539}
540
541char * roar_byteorder2str (int    byteorder) {
542 switch (byteorder) {
543  case ROAR_BYTEORDER_LE:      return "little endian"; break;
544  case ROAR_BYTEORDER_BE:      return "big endian";    break;
545  case ROAR_BYTEORDER_PDP:     return "pdp endian";    break;
546//  case ROAR_BYTEORDER_NETWORK: return "network";       break;
547  default:
548    return "(unknown)";
549 }
550}
551
552//ll
Note: See TracBrowser for help on using the repository browser.