source: roaraudio/libroar/ctl.c @ 3517:1a3218a3fc5b

Last change on this file since 3517:1a3218a3fc5b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

File size: 12.7 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 info[0] = 0;
249 info[1] = ROAR_HOST2NET16(id);
250
251 if ( roar_req(con, &m, NULL) == -1 )
252  return -1;
253
254 if ( m.cmd != ROAR_CMD_OK )
255  return -1;
256
257 if ( info[0] != 0 )
258  return -1;
259
260 info[1] = ROAR_NET2HOST16(info[1]);
261
262 if ( channels != NULL )
263  *channels = info[1];
264
265 if ( info[1] > ROAR_MAX_CHANNELS )
266  return -1;
267
268 mixer->scale   = 65535;
269 mixer->rpg_mul = 1;
270 mixer->rpg_div = 1;
271
272 for (i = 0; i < info[1]; i++)
273  mixer->mixer[i] = ROAR_NET2HOST16(info[i+2]);
274
275 return 0;
276}
277
278// converts: *_m2*, *_*2m
279
280int roar_ctl_f2m      (struct roar_message * m, unsigned char   filter, unsigned char   cmp, uint32_t   id) {
281
282 m->datalen = 7;
283
284 m->data[0] = 0;
285 m->data[1] = filter;
286 m->data[2] = cmp;
287 *((uint32_t*)&(m->data[3])) = ROAR_HOST2NET32(id);
288
289 return 0;
290}
291int roar_ctl_m2f      (struct roar_message * m, unsigned char * filter, unsigned char * cmp, uint32_t * id) {
292
293 if ( m->datalen != 7 )
294  return -1;
295
296 if ( m->data[0] != 0 ) {
297  ROAR_ERR("roar_ctl_m2f(*): version %i not supported!", m->data[0]);
298  return -1;
299 }
300
301 *filter = m->data[1];
302 *cmp    = m->data[2];
303
304 *id = ROAR_NET2HOST32(*((uint32_t*)&(m->data[3])));
305
306 return 0;
307}
308
309int roar_ctl_ia2m     (struct roar_message * m, int * data, int len) {
310 int i;
311
312 if ( len > LIBROAR_BUFFER_MSGDATA )
313  return -1;
314
315 m->datalen = len;
316
317 for (i = 0; i < len; i++)
318  m->data[i] = data[i];
319
320 return 0;
321}
322int roar_ctl_m2ia     (struct roar_message * m, int * data, int len) {
323 int i;
324
325 if ( m->datalen > len )
326  return -1;
327
328 for (i = 0; i < m->datalen; i++)
329  data[i] = m->data[i];
330
331 return m->datalen;
332}
333
334int roar_ctl_c2m      (struct roar_message * m, struct roar_client * c) {
335 int cur = 0;
336 int h;
337 int i;
338 int max_len;
339 uint32_t pid;
340 size_t len_rest;
341
342 if ( c == NULL )
343  return -1;
344
345 m->data[cur++] = 0;                       // 0: Version
346 m->data[cur++] = c->execed;               // 1: execed
347
348 h = 0;
349 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
350  if ( c->streams[i] != -1 )
351   m->data[cur+1+h++] = c->streams[i];
352 }
353
354 m->data[cur++] = h;                       // 2: num of streams
355 cur += h;
356
357 max_len = strlen(c->name);
358
359 // TODO: add some code to check if this fits in the pkg
360 // NOTE: add this code after we are sure how long this pkg will be
361 //       and fully decieded about this function.
362
363 m->data[cur++] = max_len;
364
365 strncpy((m->data)+cur, c->name, max_len);
366
367 cur += max_len;
368
369 pid = ROAR_HOST2NET32(c->pid);
370 memcpy(&(m->data[cur]), &pid, 4);
371 cur += 4;
372
373 pid = ROAR_HOST2NET32(c->uid);
374 memcpy(&(m->data[cur]), &pid, 4);
375 cur += 4;
376
377 pid = ROAR_HOST2NET32(c->gid);
378 memcpy(&(m->data[cur]), &pid, 4);
379 cur += 4;
380
381 pid = ROAR_HOST2NET32(c->proto);
382 memcpy(&(m->data[cur]), &pid, 4);
383 cur += 4;
384
385 pid = ROAR_HOST2NET32(c->byteorder);
386 memcpy(&(m->data[cur]), &pid, 4);
387 cur += 4;
388
389 len_rest = sizeof(m->data) - cur;
390 if ( roar_nnode_to_blob(&(c->nnode), &(m->data[cur]), &len_rest) == 0 ) {
391  cur += len_rest;
392 }
393
394 m->datalen = cur;
395
396 return 0;
397}
398
399int roar_ctl_m2c      (struct roar_message * m, struct roar_client * c) {
400 int i;
401 int cur;
402 uint32_t pid;
403 size_t len;
404
405 if ( m == NULL || c == NULL )
406  return -1;
407
408 if ( m->datalen == 0 )
409  return -1;
410
411 ROAR_DBG("roar_ctl_m2c(*): got data!, len = %i", m->datalen);
412
413 if ( m->data[0] != 0 ) {
414  ROAR_DBG("roar_ctl_m2c(*): wrong version!");
415  return -1;
416 }
417
418 if ( m->datalen < 3 )
419  return -1;
420
421 ROAR_DBG("roar_ctl_m2c(*): have usable data!");
422
423 c->execed = m->data[1];
424
425 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++)
426  c->streams[i] = -1;
427
428 for (i = 0; i < m->data[2]; i++)
429  c->streams[i] = m->data[3+i];
430
431 cur = 3 + m->data[2];
432
433 strncpy(c->name, (m->data)+cur+1, m->data[cur]);
434 c->name[(int)m->data[cur]] = 0;
435
436 cur += m->data[cur] + 1;
437
438 memcpy(&pid, &(m->data[cur]), 4);
439 c->pid = ROAR_NET2HOST32(pid);
440 cur += 4;
441
442 memcpy(&pid, &(m->data[cur]), 4);
443 c->uid = ROAR_NET2HOST32(pid);
444 cur += 4;
445
446 memcpy(&pid, &(m->data[cur]), 4);
447 c->gid = ROAR_NET2HOST32(pid);
448 cur += 4;
449
450 if ( m->datalen >= cur+4 ) {
451  memcpy(&pid, &(m->data[cur]), 4);
452  c->proto = ROAR_NET2HOST32(pid);
453  cur += 4;
454 } else {
455  c->proto = ROAR_PROTO_NONE;
456 }
457
458 if ( m->datalen >= cur+4 ) {
459  memcpy(&pid, &(m->data[cur]), 4);
460  c->byteorder = ROAR_NET2HOST32(pid);
461  cur += 4;
462 } else {
463  c->byteorder = ROAR_BYTEORDER_UNKNOWN;
464 }
465
466 if ( m->datalen > cur ) {
467  len = m->datalen - cur;
468  if ( roar_nnode_from_blob(&(c->nnode), &(m->data[cur]), &len) == 0 ) {
469   cur += len;
470  } else {
471   if ( roar_nnode_new(&(c->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 )
472    return -1;
473  }
474 } else {
475  if ( roar_nnode_new(&(c->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 )
476   return -1;
477 }
478
479 return 0;
480}
481
482int    roar_str2proto (char * proto) {
483 if ( !strcasecmp(proto, "roar") ) {
484  return ROAR_PROTO_ROARAUDIO;
485 } else if ( !strcasecmp(proto, "roaraudio") ) {
486  return ROAR_PROTO_ROARAUDIO;
487 } else if ( !strcasecmp(proto, "esd") ) {
488  return ROAR_PROTO_ESOUND;
489 } else if ( !strcasecmp(proto, "esound") ) {
490  return ROAR_PROTO_ESOUND;
491 } else if ( !strcasecmp(proto, "auto") ) {
492  return ROAR_PROTO_AUTO;
493 } else if ( !strcasecmp(proto, "(auto)") ) {
494  return ROAR_PROTO_AUTO;
495 } else if ( !strcasecmp(proto, "http") ) {
496  return ROAR_PROTO_HTTP;
497 } else if ( !strcasecmp(proto, "gopher") ) {
498  return ROAR_PROTO_GOPHER;
499 } else if ( !strcasecmp(proto, "icy") ) {
500  return ROAR_PROTO_ICY;
501 } else if ( !strcasecmp(proto, "simple") ) {
502  return ROAR_PROTO_SIMPLE;
503 }
504
505 return -1;
506}
507
508char * roar_proto2str (int    proto) {
509 switch (proto) {
510  case ROAR_PROTO_ROARAUDIO: return "RoarAudio"; break;
511  case ROAR_PROTO_ESOUND:    return "EsounD";    break;
512  case ROAR_PROTO_AUTO:      return "(auto)";    break;
513  case ROAR_PROTO_HTTP:      return "http";      break;
514  case ROAR_PROTO_GOPHER:    return "gopher";    break;
515  case ROAR_PROTO_ICY:       return "ICY";       break;
516  case ROAR_PROTO_SIMPLE:    return "Simple";    break;
517  default:
518    return "(unknown)";
519 }
520}
521
522int    roar_str2byteorder (char * byteorder) {
523 if (        !strcasecmp(byteorder, "le")            || !strcasecmp(byteorder, "little") ||
524             !strcasecmp(byteorder, "little endian") || !strcasecmp(byteorder, "1234")   ) {
525  return ROAR_BYTEORDER_LE;
526 } else if ( !strcasecmp(byteorder, "be")            || !strcasecmp(byteorder, "big")    ||
527             !strcasecmp(byteorder, "big endian")    || !strcasecmp(byteorder, "4321")   ) {
528  return ROAR_BYTEORDER_BE;
529 } else if ( !strcasecmp(byteorder, "pdp")           ||
530             !strcasecmp(byteorder, "pdp endian") ) {
531  return ROAR_BYTEORDER_PDP;
532 } else if ( !strcasecmp(byteorder, "network")       ||
533             !strcasecmp(byteorder, "network byteorder") ) {
534  return ROAR_BYTEORDER_NETWORK;
535 }
536
537 return -1;
538}
539
540char * roar_byteorder2str (int    byteorder) {
541 switch (byteorder) {
542  case ROAR_BYTEORDER_LE:      return "little endian"; break;
543  case ROAR_BYTEORDER_BE:      return "big endian";    break;
544  case ROAR_BYTEORDER_PDP:     return "pdp endian";    break;
545//  case ROAR_BYTEORDER_NETWORK: return "network";       break;
546  default:
547    return "(unknown)";
548 }
549}
550
551//ll
Note: See TracBrowser for help on using the repository browser.