source: roaraudio/libroar/ctl.c @ 798:86e3c1bfb7cb

Last change on this file since 798:86e3c1bfb7cb was 798:86e3c1bfb7cb, checked in by phi, 16 years ago

set roar_errno in roar_get_stream()

File size: 8.9 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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_server_oinfo   (struct roar_connection * con, struct roar_stream * sa) {
38 struct roar_message mes;
39
40 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
41
42 mes.cmd     = ROAR_CMD_SERVER_OINFO;
43 mes.datalen = 0;
44
45 if ( roar_req(con, &mes, NULL) == -1 )
46  return -1;
47
48 if ( mes.cmd != ROAR_CMD_OK )
49  return -1;
50
51 if ( roar_stream_m2s(sa, &mes) == -1 )
52  return -1;
53
54 return 0;
55}
56
57int roar_get_standby   (struct roar_connection * con) {
58 struct roar_message mes;
59
60 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
61
62 mes.cmd = ROAR_CMD_GET_STANDBY;
63 mes.datalen = 0;
64
65 if ( roar_req(con, &mes, NULL) == -1 )
66  return -1;
67
68 if ( mes.cmd != ROAR_CMD_OK )
69  return -1;
70
71 return ROAR_NET2HOST16(*((uint16_t*)mes.data));
72}
73
74int roar_set_standby   (struct roar_connection * con, int state) {
75 struct roar_message mes;
76
77 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
78
79 mes.cmd = ROAR_CMD_SET_STANDBY;
80 mes.datalen = 2;
81
82 *((uint16_t*)mes.data) = ROAR_HOST2NET16((unsigned) state);
83
84 if ( roar_req(con, &mes, NULL) == -1 )
85  return -1;
86
87 if ( mes.cmd != ROAR_CMD_OK )
88  return -1;
89
90 return 0;
91}
92
93int roar_exit   (struct roar_connection * con) {
94 return roar_terminate(con, 0);
95}
96
97int roar_terminate (struct roar_connection * con, int terminate) {
98 struct roar_message mes;
99
100 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
101
102 mes.cmd     = ROAR_CMD_EXIT;
103 mes.datalen = 1;
104 mes.data[0] = terminate;
105
106 if ( roar_req(con, &mes, NULL) == -1 )
107  return -1;
108
109 if ( mes.cmd != ROAR_CMD_OK )
110  return -1;
111
112 return 0;
113}
114
115int roar_list         (struct roar_connection * con, int * items,   int max, int cmd) {
116 struct roar_message m;
117
118 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
119
120 roar_ctl_f2m_any(&m);
121 m.cmd = cmd;
122
123 if ( roar_req(con, &m, NULL) == -1 )
124  return -1;
125
126 return roar_ctl_m2ia(&m, items, max);
127}
128
129int roar_get_client   (struct roar_connection * con, struct roar_client * client, int id) {
130 struct roar_message m;
131
132 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
133
134 m.cmd     = ROAR_CMD_GET_CLIENT;
135 m.datalen = 1;
136 m.data[0] = id;
137
138 ROAR_DBG("roar_get_client(*): id = %i", id);
139
140 if ( roar_req(con, &m, NULL) == -1 )
141  return -1;
142
143 if ( m.cmd != ROAR_CMD_OK )
144  return -1;
145
146 ROAR_DBG("roar_get_client(*): got ok");
147
148 return roar_ctl_m2c(&m, client);
149}
150
151int roar_get_stream   (struct roar_connection * con, struct roar_stream * stream, int id) {
152 struct roar_message m;
153
154 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
155
156 m.cmd     = ROAR_CMD_GET_STREAM;
157 m.datalen = 1;
158 m.data[0] = id;
159
160 roar_errno = ROAR_ERROR_UNKNOWN;
161
162 if ( roar_req(con, &m, NULL) == -1 ) {
163  roar_errno = ROAR_ERROR_PROTO;
164  return -1;
165 }
166
167 if ( m.cmd != ROAR_CMD_OK )
168  return -1;
169
170 return roar_stream_m2s(stream, &m);
171}
172
173int roar_kick         (struct roar_connection * con, int type, int id) {
174 struct roar_message m;
175 uint16_t * info = (uint16_t *) m.data;
176
177 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
178
179 m.cmd     = ROAR_CMD_KICK;
180 m.datalen = 4;
181 info[0] = ROAR_HOST2NET16(type);
182 info[1] = ROAR_HOST2NET16(id);
183
184 if ( roar_req(con, &m, NULL) == -1 )
185  return -1;
186
187 if ( m.cmd != ROAR_CMD_OK )
188  return -1;
189
190 return 0;
191}
192
193int roar_set_vol      (struct roar_connection * con, int id, struct roar_mixer_settings * mixer, int channels) {
194 struct roar_message m;
195 uint16_t * info = (uint16_t *) m.data;
196 int i;
197
198 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
199
200 m.cmd     = ROAR_CMD_SET_VOL;
201 m.datalen = (3 + channels) * 2;
202 info[0] = 0;
203 info[1] = ROAR_HOST2NET16(id);
204 info[2] = ROAR_HOST2NET16(ROAR_SET_VOL_ALL);
205
206 for (i = 0; i < channels; i++)
207  info[i+3] = ROAR_HOST2NET16(mixer->mixer[i]);
208
209 if ( roar_req(con, &m, NULL) == -1 )
210  return -1;
211
212 if ( m.cmd != ROAR_CMD_OK )
213  return -1;
214
215 return 0;
216}
217
218int roar_get_vol      (struct roar_connection * con, int id, struct roar_mixer_settings * mixer, int * channels) {
219 struct roar_message m;
220 uint16_t * info = (uint16_t *) m.data;
221 int i;
222
223 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
224
225 m.cmd     = ROAR_CMD_GET_VOL;
226 m.datalen = 2*2;
227 info[0] = 0;
228 info[1] = ROAR_HOST2NET16(id);
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 if ( info[0] != 0 )
237  return -1;
238
239 info[1] = ROAR_NET2HOST16(info[1]);
240
241 if ( channels != NULL )
242  *channels = info[1];
243
244 if ( info[1] > ROAR_MAX_CHANNELS )
245  return -1;
246
247 for (i = 0; i < info[1]; i++)
248  mixer->mixer[i] = ROAR_NET2HOST16(info[i+2]);
249
250 return 0;
251}
252
253// converts: *_m2*, *_*2m
254
255int roar_ctl_f2m      (struct roar_message * m, unsigned char   filter, unsigned char   cmp, uint32_t   id) {
256
257 m->datalen = 7;
258
259 m->data[0] = 0;
260 m->data[1] = filter;
261 m->data[2] = cmp;
262 *((uint32_t*)&(m->data[3])) = ROAR_HOST2NET32(id);
263
264 return 0;
265}
266int roar_ctl_m2f      (struct roar_message * m, unsigned char * filter, unsigned char * cmp, uint32_t * id) {
267
268 if ( m->datalen != 7 )
269  return -1;
270
271 if ( m->data[0] != 0 ) {
272  ROAR_ERR("roar_ctl_m2f(*): version %i not supported!", m->data[0]);
273  return -1;
274 }
275
276 *filter = m->data[1];
277 *cmp    = m->data[2];
278
279 *id = ROAR_NET2HOST32(*((uint32_t*)&(m->data[3])));
280
281 return 0;
282}
283
284int roar_ctl_ia2m     (struct roar_message * m, int * data, int len) {
285 int i;
286
287 if ( len > LIBROAR_BUFFER_MSGDATA )
288  return -1;
289
290 m->datalen = len;
291
292 for (i = 0; i < len; i++)
293  m->data[i] = data[i];
294
295 return 0;
296}
297int roar_ctl_m2ia     (struct roar_message * m, int * data, int len) {
298 int i;
299
300 if ( m->datalen > len )
301  return -1;
302
303 for (i = 0; i < m->datalen; i++)
304  data[i] = m->data[i];
305
306 return m->datalen;
307}
308
309int roar_ctl_c2m      (struct roar_message * m, struct roar_client * c) {
310 int cur = 0;
311 int h;
312 int i;
313 int max_len;
314 uint32_t pid;
315
316 if ( c == NULL )
317  return -1;
318
319 m->data[cur++] = 0;                       // 0: Version
320 m->data[cur++] = c->execed;               // 1: execed
321
322 h = 0;
323 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
324  if ( c->streams[i] != -1 )
325   m->data[cur+1+h++] = c->streams[i];
326 }
327
328 m->data[cur++] = h;                       // 2: num of streams
329 cur += h;
330
331 max_len = strlen(c->name);
332
333 // TODO: add some code to check if this fits in the pkg
334 // NOTE: add this code after we are sure how long this pkg will be
335 //       and fully decieded about this function.
336
337 m->data[cur++] = max_len;
338
339 strncpy((m->data)+cur, c->name, max_len);
340
341 cur += max_len;
342
343 pid = ROAR_HOST2NET32(c->pid);
344 memcpy(&(m->data[cur]), &pid, 4);
345 cur += 4;
346
347 pid = ROAR_HOST2NET32(c->uid);
348 memcpy(&(m->data[cur]), &pid, 4);
349 cur += 4;
350
351 pid = ROAR_HOST2NET32(c->gid);
352 memcpy(&(m->data[cur]), &pid, 4);
353 cur += 4;
354
355 m->datalen = cur;
356
357 return 0;
358}
359
360int roar_ctl_m2c      (struct roar_message * m, struct roar_client * c) {
361 int i;
362 int cur;
363 uint32_t pid;
364
365 if ( m == NULL || c == NULL )
366  return -1;
367
368 if ( m->datalen == 0 )
369  return -1;
370
371 ROAR_DBG("roar_ctl_m2c(*): got data!, len = %i", m->datalen);
372
373 if ( m->data[0] != 0 ) {
374  ROAR_DBG("roar_ctl_m2c(*): wrong version!");
375  return -1;
376 }
377
378 if ( m->datalen < 3 )
379  return -1;
380
381 ROAR_DBG("roar_ctl_m2c(*): have usable data!");
382
383 c->execed = m->data[1];
384
385 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++)
386  c->streams[i] = -1;
387
388 for (i = 0; i < m->data[2]; i++)
389  c->streams[i] = m->data[3+i];
390
391 cur = 3 + m->data[2];
392
393 strncpy(c->name, (m->data)+cur+1, m->data[cur]);
394 c->name[(int)m->data[cur]] = 0;
395
396 cur += m->data[cur] + 1;
397
398 memcpy(&pid, &(m->data[cur]), 4);
399 c->pid = ROAR_NET2HOST32(pid);
400 cur += 4;
401
402 memcpy(&pid, &(m->data[cur]), 4);
403 c->uid = ROAR_NET2HOST32(pid);
404 cur += 4;
405
406 memcpy(&pid, &(m->data[cur]), 4);
407 c->gid = ROAR_NET2HOST32(pid);
408 cur += 4;
409
410 return 0;
411}
412
413//ll
Note: See TracBrowser for help on using the repository browser.