source: roaraudio/libroar/basic.c @ 1435:73f97b3d345e

Last change on this file since 1435:73f97b3d345e was 1435:73f97b3d345e, checked in by phi, 15 years ago

make the file more microcontroller friendly

File size: 10.5 KB
Line 
1//basic.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_connect_raw (char * server) {
38 char user_sock[80];
39 char * roar_server;
40 int i = 0;
41 int port = 0;
42 int fh = -1;
43 int is_decnet = 0;
44 char * obj = NULL;
45#ifndef ROAR_TARGET_WIN32
46 struct passwd * pwd;
47#endif
48#ifdef ROAR_HAVE_LIBDNET
49 struct stat decnet_stat;
50#endif
51
52 roar_errno = ROAR_ERROR_UNKNOWN;
53
54 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
55  server = roar_server;
56
57#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICRONTROLLER)
58 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
59   user_sock[i] = 0;
60   server = user_sock;
61 }
62#endif
63
64 if ( server == NULL || *server == 0 ) {
65  /* connect via defaults */
66
67#ifndef ROAR_TARGET_MICRONTROLLER
68  roar_server = getenv("HOME");
69#else
70  roar_server = NULL;
71#endif
72
73  if ( roar_server == NULL ) {
74#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICRONTROLLER)
75   if ( (pwd = getpwuid(getuid())) == NULL ) {
76    roar_server = "/NX-HOME-DIR";
77   } else {
78    roar_server = pwd->pw_dir;
79   }
80#else
81   roar_server = "/WIN32-SUCKS";
82#endif
83  }
84
85  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
86
87  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
88   return fh;
89
90  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
91   return fh;
92
93  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
94   return fh;
95
96#ifdef ROAR_HAVE_LIBDNET
97  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
98   if ( roar_socket_get_local_nodename() ) {
99    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
100    return roar_socket_connect(user_sock, ROAR_DEFAULT_NUM);
101   }
102  }
103#endif
104
105 } else {
106  /* connect via (char*)server */
107  // find a port:
108  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
109   for (i = 0; server[i] != 0; i++) {
110    if ( server[i] == ':' ) {
111     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
112      is_decnet = 1;
113      obj = &server[i+2];
114      break;
115     }
116
117     port = atoi(server+i+1);
118     server[i] = 0;
119     break;
120    }
121   }
122  }
123
124  if ( is_decnet ) {
125    *user_sock = 0;
126   if ( *server == ':' ) {
127    if ( roar_socket_get_local_nodename() )
128     strncat(user_sock, roar_socket_get_local_nodename(), 6);
129   }
130
131   strncat(user_sock, server, 79);
132   server = user_sock;
133   if ( *obj == 0 ) {
134#ifdef DN_MAXOBJL
135    strncat(user_sock, ROAR_DEFAULT_OBJECT, DN_MAXOBJL+2);
136#else
137    ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown.");
138#endif
139   }
140  }
141
142  if ( port || is_decnet ) {
143   fh = roar_socket_connect(server, port);
144   // restore the original string
145   server[i] = ':';
146  } else {
147   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
148  }
149 }
150
151 if ( fh == -1 )
152  roar_errno = ROAR_ERROR_CONNREFUSED;
153
154 ROAR_DBG("roar_connect_raw(*) = %i", fh);
155
156 return fh;
157}
158
159int roar_connect    (struct roar_connection * con, char * server) {
160 int fh;
161
162 if ( con == NULL ) {
163  roar_errno = ROAR_ERROR_INVAL;
164  return -1;
165 }
166
167 roar_errno = ROAR_ERROR_UNKNOWN;
168 fh = roar_connect_raw(server);
169
170 if ( fh == -1 )
171  return -1;
172
173 return roar_connect_fh(con, fh);
174}
175
176int roar_connect_fh (struct roar_connection * con, int fh) {
177
178 if ( con == NULL || fh == -1 ) {
179  roar_errno = ROAR_ERROR_INVAL;
180  return -1;
181 }
182
183 // specal hack to set an ilegal value used internaly in libroar:
184 if ( fh == -2 )
185  fh = -1;
186
187 memset(con, 0, sizeof(struct roar_connection));
188
189 con->fh = fh;
190
191 roar_errno = ROAR_ERROR_NONE;
192 return 0;
193}
194
195int roar_disconnect (struct roar_connection * con) {
196 struct roar_message m;
197
198 m.datalen = 0;
199 m.stream  = 0;
200 m.pos     = 0;
201 m.cmd     = ROAR_CMD_QUIT;
202
203 roar_req(con, &m, NULL);
204
205 close(con->fh);
206
207 con->fh = -1;
208
209 roar_errno = ROAR_ERROR_NONE;
210
211 return 0;
212}
213
214int roar_identify   (struct roar_connection * con, char * name) {
215 struct roar_message mes;
216 pid_t pid;
217 int max_len;
218
219 roar_errno = ROAR_ERROR_UNKNOWN;
220
221 ROAR_DBG("roar_identify(*): try to identify myself...");
222
223 mes.cmd    = ROAR_CMD_IDENTIFY;
224 mes.stream = 0;
225 mes.pos    = 0;
226
227 ROAR_DBG("roar_identify(*): name=%p", name);
228
229 if ( name == NULL )
230  name = "libroar client";
231
232 ROAR_DBG("roar_identify(*): name=%p", name);
233
234 max_len = strlen(name);
235 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
236
237 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
238  max_len = LIBROAR_BUFFER_MSGDATA - 5;
239
240 mes.datalen = 5 + max_len;
241 mes.data[0] = 1;
242
243 pid = getpid();
244 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
245 ROAR_DBG("roar_identify(*): pid = %i", pid);
246
247 strncpy(mes.data+5, name, max_len);
248
249 return roar_req(con, &mes, NULL);
250}
251
252#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
253int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
254 struct roar_vio_calls vio;
255
256 if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
257  return -1;
258
259 return roar_vsend_message(&vio, mes, data);
260}
261
262int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char *  data) {
263 char buf[_ROAR_MESS_BUF_LEN];
264
265 roar_errno = ROAR_ERROR_UNKNOWN;
266
267 ROAR_DBG("roar_send_message(*): try to send an request...");
268
269 buf[0] = _ROAR_MESSAGE_VERSION;
270 buf[1] = (unsigned char) mes->cmd;
271 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
272 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
273 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
274
275 if ( roar_vio_write(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
276  roar_errno = ROAR_ERROR_PIPE;
277  return -1;
278 }
279
280 if ( mes->datalen != 0 ) {
281  if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) {
282   roar_errno = ROAR_ERROR_PIPE;
283   return -1;
284  }
285 }
286
287 roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links
288
289 roar_errno = ROAR_ERROR_NONE;
290
291 ROAR_DBG("roar_send_message(*) = 0");
292 return 0;
293}
294
295int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
296 struct roar_vio_calls vio;
297
298 if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
299  return -1;
300
301 return roar_vrecv_message(&vio, mes, data);
302}
303
304int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
305 char buf[_ROAR_MESS_BUF_LEN];
306
307 roar_errno = ROAR_ERROR_UNKNOWN;
308
309 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
310
311 if ( data )
312  *data = NULL;
313
314 if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
315  roar_errno = ROAR_ERROR_PROTO;
316  return -1;
317 }
318
319 ROAR_DBG("roar_recv_message(*): Got a header");
320
321 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
322  roar_errno = ROAR_ERROR_PROTO;
323  return -1;
324 }
325
326 mes->cmd     = (unsigned char)buf[1];
327 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
328 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
329 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
330
331 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
332           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
333
334 if ( mes->datalen == 0 ) {
335  ROAR_DBG("roar_recv_message(*): no data in this pkg");
336  ROAR_DBG("roar_recv_message(*) = 0");
337  roar_errno = ROAR_ERROR_NONE;
338  return 0;
339 }
340
341 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
342  if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) {
343   ROAR_DBG("roar_recv_message(*): Got data!");
344   ROAR_DBG("roar_recv_message(*) = 0");
345   roar_errno = ROAR_ERROR_NONE;
346   return 0;
347  }
348
349  roar_errno = ROAR_ERROR_PIPE;
350  return -1;
351 } else {
352  if ( data == NULL ) {
353   roar_errno = ROAR_ERROR_MSGSIZE;
354   return -1;
355  }
356
357  if ( (*data = malloc(mes->datalen)) == NULL ) {
358   roar_errno = ROAR_ERROR_NOMEM;
359   return -1;
360  }
361
362  if ( mes->datalen == 0 ) {
363   roar_errno = ROAR_ERROR_NONE;
364   return 0;
365  }
366
367  if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) {
368   ROAR_DBG("roar_recv_message(*): Got data!");
369   ROAR_DBG("roar_recv_message(*) = 0");
370   roar_errno = ROAR_ERROR_NONE;
371   return 0;
372  }
373
374  roar_errno = ROAR_ERROR_PIPE;
375  return -1;
376 }
377
378 // what happened here?
379 return -1;
380}
381
382int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
383 struct roar_vio_calls vio;
384
385 if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 )
386  return -1;
387
388 return roar_vreq(&vio, mes, data);
389}
390
391int roar_vreq         (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
392 if ( roar_vsend_message(vio, mes, data ? *data : NULL) != 0 )
393  return -1;
394
395 if ( data )
396  free(*data);
397
398 return roar_vrecv_message(vio, mes, data);
399}
400
401int roar_debug_message_print (struct roar_message * mes) {
402 if ( mes == NULL )
403  return -1;
404
405 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
406 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
407 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
408 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
409
410 ROAR_DBG("roar_debug_message_print(*) = 0");
411 return 0;
412}
413
414int roar_debug_audio_info_print (struct roar_audio_info * info) {
415 if ( info == NULL )
416  return -1;
417
418 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
419 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
420 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
421 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
422
423 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
424 return 0;
425}
426
427//ll
Note: See TracBrowser for help on using the repository browser.