source: roaraudio/libroar/basic.c @ 3836:83dbb40a308c

Last change on this file since 3836:83dbb40a308c was 3836:83dbb40a308c, checked in by phi, 14 years ago

spacing, added roar_noop()

File size: 12.5 KB
RevLine 
[0]1//basic.c:
2
[690]3/*
[3372]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
[690]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]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
[0]36#include "libroar.h"
37
38int roar_connect_raw (char * server) {
39 char user_sock[80];
40 char * roar_server;
[701]41 int i = 0;
[0]42 int port = 0;
43 int fh = -1;
[501]44 int is_decnet = 0;
[516]45 char * obj = NULL;
[1475]46#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]47 struct passwd * pwd;
[1393]48#endif
[828]49#ifdef ROAR_HAVE_LIBDNET
50 struct stat decnet_stat;
51#endif
[3372]52#ifdef ROAR_HAVE_LIBX11
53 struct roar_x11_connection * x11con;
54#endif
[0]55
[807]56 roar_errno = ROAR_ERROR_UNKNOWN;
57
[2897]58 // load config
59 roar_libroar_get_config();
60
[2567]61 if ( server == NULL )
62  server = roar_libroar_get_server();
63
[61]64 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
[0]65  server = roar_server;
66
[3372]67#ifdef ROAR_HAVE_LIBX11
68 if ( server == NULL ) {
69  if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
70   server = roar_x11_get_prop(x11con, "ROAR_SERVER");
71   roar_x11_disconnect(x11con);
72  }
73 }
74#endif
75
[1436]76#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[448]77 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
78   user_sock[i] = 0;
79   server = user_sock;
80 }
[1093]81#endif
[448]82
[3077]83 if ( server != NULL && !strcasecmp(server, "+slp") ) {
[3076]84  server = roar_slp_find_roard(0);
85  if ( server == NULL ) {
86   return -1;
87  }
88 }
89
[61]90 if ( server == NULL || *server == 0 ) {
[0]91  /* connect via defaults */
92
[1764]93#ifdef ROAR_HAVE_UNIX
[1436]94#ifndef ROAR_TARGET_MICROCONTROLLER
[1026]95  roar_server = getenv("HOME");
[1435]96#else
97  roar_server = NULL;
98#endif
[1026]99
100  if ( roar_server == NULL ) {
[1436]101#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]102   if ( (pwd = getpwuid(getuid())) == NULL ) {
103    roar_server = "/NX-HOME-DIR";
104   } else {
105    roar_server = pwd->pw_dir;
106   }
[1078]107#else
108   roar_server = "/WIN32-SUCKS";
109#endif
[1026]110  }
111
112  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
[0]113
114  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
115   return fh;
116
[237]117  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
118   return fh;
[1764]119#endif
[237]120
[0]121  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
122   return fh;
123
[828]124#ifdef ROAR_HAVE_LIBDNET
125  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
126   if ( roar_socket_get_local_nodename() ) {
127    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
[2010]128    if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 )
129     return fh;
[828]130   }
[522]131  }
[828]132#endif
[2007]133
134#ifdef ROAR_HAVE_LIBSLP
[2014]135 if ( (server = roar_slp_find_roard(0)) != NULL )
136  if ( (fh = roar_connect_raw(server)) != -1 )
137   return fh;
138
139 /* in case we can not connect to the server given this may be a cache problem,
140    we do a new lookup with the cache disabled in this case                     */
141 ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
142 if ( (server = roar_slp_find_roard(1)) != NULL )
143  if ( (fh = roar_connect_raw(server)) != -1 )
144   return fh;
[2007]145#endif
[522]146
[2014]147 return -1;
148
[0]149 } else {
150  /* connect via (char*)server */
[501]151  // find a port:
152  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
153   for (i = 0; server[i] != 0; i++) {
154    if ( server[i] == ':' ) {
155     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
156      is_decnet = 1;
[516]157      obj = &server[i+2];
[501]158      break;
159     }
160
161     port = atoi(server+i+1);
162     server[i] = 0;
163     break;
164    }
[0]165   }
166  }
167
[521]168  if ( is_decnet ) {
169    *user_sock = 0;
170   if ( *server == ':' ) {
171    if ( roar_socket_get_local_nodename() )
[1066]172     strncat(user_sock, roar_socket_get_local_nodename(), 6);
[521]173   }
174
[1066]175   strncat(user_sock, server, 79);
[516]176   server = user_sock;
[521]177   if ( *obj == 0 ) {
[1066]178#ifdef DN_MAXOBJL
179    strncat(user_sock, ROAR_DEFAULT_OBJECT, DN_MAXOBJL+2);
180#else
181    ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown.");
182#endif
[521]183   }
[516]184  }
185
[501]186  if ( port || is_decnet ) {
[0]187   fh = roar_socket_connect(server, port);
188   // restore the original string
189   server[i] = ':';
190  } else {
191   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
192  }
193 }
194
[807]195 if ( fh == -1 )
196  roar_errno = ROAR_ERROR_CONNREFUSED;
197
[0]198 ROAR_DBG("roar_connect_raw(*) = %i", fh);
199
200 return fh;
201}
202
203int roar_connect    (struct roar_connection * con, char * server) {
[1315]204 int fh;
205
206 if ( con == NULL ) {
207  roar_errno = ROAR_ERROR_INVAL;
208  return -1;
209 }
210
[807]211 roar_errno = ROAR_ERROR_UNKNOWN;
[1315]212 fh = roar_connect_raw(server);
[0]213
[1315]214 if ( fh == -1 )
[0]215  return -1;
216
[1315]217 return roar_connect_fh(con, fh);
218}
219
220int roar_connect_fh (struct roar_connection * con, int fh) {
221
222 if ( con == NULL || fh == -1 ) {
223  roar_errno = ROAR_ERROR_INVAL;
224  return -1;
225 }
226
227 // specal hack to set an ilegal value used internaly in libroar:
228 if ( fh == -2 )
229  fh = -1;
230
231 memset(con, 0, sizeof(struct roar_connection));
232
[1660]233 con->__fh = fh;
[1315]234
[807]235 roar_errno = ROAR_ERROR_NONE;
[0]236 return 0;
237}
238
[1660]239int roar_get_connection_fh (struct roar_connection * con) {
[2809]240 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio", NULL);
241
[2043]242 if ( con == NULL )
243  return -1;
244
[1660]245 return con->__fh;
246}
247
[2043]248int roar_get_connection_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
249 if ( con == NULL || vio == NULL )
250  return -1;
251
[2809]252 return roar_vio_open_fh_socket(vio, con->__fh);
[2043]253}
254
[0]255int roar_disconnect (struct roar_connection * con) {
[2809]256 struct roar_vio_calls vio;
[0]257 struct roar_message m;
258
259 m.datalen = 0;
260 m.stream  = 0;
[133]261 m.pos     = 0;
[0]262 m.cmd     = ROAR_CMD_QUIT;
263
264 roar_req(con, &m, NULL);
265
[2809]266 if ( roar_get_connection_vio(con, &vio) != -1 ) {
267  roar_vio_close(&vio);
268 }
[0]269
[1660]270 roar_connect_fh(con, -2);
[0]271
[807]272 roar_errno = ROAR_ERROR_NONE;
273
[0]274 return 0;
275}
276
[3836]277int roar_noop         (struct roar_connection * con) {
278 struct roar_message mes;
279
280 if ( con == NULL ) {
281  return -1;
282 }
283
284 memset(&mes, 0, sizeof(mes));
285
286 mes.cmd = ROAR_CMD_NOOP;
287
288 return roar_req(con, &mes, NULL);
289}
290
[0]291int roar_identify   (struct roar_connection * con, char * name) {
292 struct roar_message mes;
293 pid_t pid;
294 int max_len;
295
[807]296 roar_errno = ROAR_ERROR_UNKNOWN;
297
[0]298 ROAR_DBG("roar_identify(*): try to identify myself...");
299
300 mes.cmd    = ROAR_CMD_IDENTIFY;
301 mes.stream = 0;
302 mes.pos    = 0;
303
304 ROAR_DBG("roar_identify(*): name=%p", name);
305
306 if ( name == NULL )
307  name = "libroar client";
308
309 ROAR_DBG("roar_identify(*): name=%p", name);
310
311 max_len = strlen(name);
312 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
313
314 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
315  max_len = LIBROAR_BUFFER_MSGDATA - 5;
316
317 mes.datalen = 5 + max_len;
318 mes.data[0] = 1;
319
320 pid = getpid();
321 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
322 ROAR_DBG("roar_identify(*): pid = %i", pid);
323
324 strncpy(mes.data+5, name, max_len);
325
326 return roar_req(con, &mes, NULL);
327}
328
[8]329#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
[0]330int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
[1319]331 struct roar_vio_calls vio;
332
[2809]333 if ( roar_get_connection_vio(con, &vio) == -1 )
[1319]334  return -1;
335
336 return roar_vsend_message(&vio, mes, data);
337}
338
339int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char *  data) {
[0]340 char buf[_ROAR_MESS_BUF_LEN];
341
[807]342 roar_errno = ROAR_ERROR_UNKNOWN;
343
[0]344 ROAR_DBG("roar_send_message(*): try to send an request...");
345
[8]346 buf[0] = _ROAR_MESSAGE_VERSION;
347 buf[1] = (unsigned char) mes->cmd;
348 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
349 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
350 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
[0]351
[1319]352 if ( roar_vio_write(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
[807]353  roar_errno = ROAR_ERROR_PIPE;
[0]354  return -1;
[807]355 }
[0]356
[807]357 if ( mes->datalen != 0 ) {
[1319]358  if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) {
[807]359   roar_errno = ROAR_ERROR_PIPE;
[0]360   return -1;
[807]361  }
362 }
363
364 roar_errno = ROAR_ERROR_NONE;
[0]365
366 ROAR_DBG("roar_send_message(*) = 0");
367 return 0;
368}
369
370int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
[1319]371 struct roar_vio_calls vio;
372
[2809]373 if ( roar_get_connection_vio(con, &vio) == -1 )
[1319]374  return -1;
375
376 return roar_vrecv_message(&vio, mes, data);
377}
378
379int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
[0]380 char buf[_ROAR_MESS_BUF_LEN];
381
[807]382 roar_errno = ROAR_ERROR_UNKNOWN;
383
[0]384 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
385
[3758]386 if ( mes == NULL )
387  return -1;
388
389 if ( data != NULL )
[0]390  *data = NULL;
391
[3758]392 memset(mes, 0, sizeof(struct roar_message));
393
[1319]394 if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
[807]395  roar_errno = ROAR_ERROR_PROTO;
[0]396  return -1;
[807]397 }
[0]398
399 ROAR_DBG("roar_recv_message(*): Got a header");
400
[807]401 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
402  roar_errno = ROAR_ERROR_PROTO;
[8]403  return -1;
[807]404 }
[8]405
406 mes->cmd     = (unsigned char)buf[1];
407 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
408 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
409 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
[0]410
[3722]411 if ( (int16_t)mes->stream == -1 )
412  mes->stream = -1;
413
[0]414 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
415           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
416
[104]417 if ( mes->datalen == 0 ) {
418  ROAR_DBG("roar_recv_message(*): no data in this pkg");
419  ROAR_DBG("roar_recv_message(*) = 0");
[807]420  roar_errno = ROAR_ERROR_NONE;
[104]421  return 0;
422 }
423
[0]424 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
[1319]425  if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) {
[0]426   ROAR_DBG("roar_recv_message(*): Got data!");
427   ROAR_DBG("roar_recv_message(*) = 0");
[807]428   roar_errno = ROAR_ERROR_NONE;
[0]429   return 0;
430  }
[807]431
432  roar_errno = ROAR_ERROR_PIPE;
[0]433  return -1;
434 } else {
[807]435  if ( data == NULL ) {
436   roar_errno = ROAR_ERROR_MSGSIZE;
[0]437   return -1;
[807]438  }
[0]439
[807]440  if ( (*data = malloc(mes->datalen)) == NULL ) {
441   roar_errno = ROAR_ERROR_NOMEM;
[0]442   return -1;
[807]443  }
[0]444
[807]445  if ( mes->datalen == 0 ) {
446   roar_errno = ROAR_ERROR_NONE;
[0]447   return 0;
[807]448  }
[0]449
[1319]450  if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) {
[0]451   ROAR_DBG("roar_recv_message(*): Got data!");
452   ROAR_DBG("roar_recv_message(*) = 0");
[807]453   roar_errno = ROAR_ERROR_NONE;
[0]454   return 0;
455  }
[807]456
457  roar_errno = ROAR_ERROR_PIPE;
[0]458  return -1;
459 }
460
[807]461 // what happened here?
[0]462 return -1;
463}
464
465int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
[1319]466 struct roar_vio_calls vio;
467
[2809]468 if ( roar_get_connection_vio(con, &vio) == -1 )
[1319]469  return -1;
470
471 return roar_vreq(&vio, mes, data);
472}
473
474int roar_vreq         (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
475 if ( roar_vsend_message(vio, mes, data ? *data : NULL) != 0 )
[0]476  return -1;
477
478 if ( data )
479  free(*data);
480
[2053]481 roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links
482
[1319]483 return roar_vrecv_message(vio, mes, data);
[0]484}
485
486int roar_debug_message_print (struct roar_message * mes) {
487 if ( mes == NULL )
488  return -1;
489
490 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
491 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
492 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
493 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
494
495 ROAR_DBG("roar_debug_message_print(*) = 0");
496 return 0;
497}
498
499int roar_debug_audio_info_print (struct roar_audio_info * info) {
500 if ( info == NULL )
501  return -1;
502
503 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
504 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
505 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
506 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
507
508 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
509 return 0;
510}
511
512//ll
Note: See TracBrowser for help on using the repository browser.