source: roaraudio/libroar/basic.c @ 807:9289636092cd

Last change on this file since 807:9289636092cd was 807:9289636092cd, checked in by phi, 16 years ago

set roar_errno

File size: 8.6 KB
RevLine 
[0]1//basic.c:
2
[690]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
[0]35#include "libroar.h"
36
37int roar_connect_raw (char * server) {
38 char user_sock[80];
39 char * roar_server;
[701]40 int i = 0;
[0]41 int port = 0;
42 int fh = -1;
[501]43 int is_decnet = 0;
[516]44 char * obj = NULL;
[0]45
[807]46 roar_errno = ROAR_ERROR_UNKNOWN;
47
[61]48 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
[0]49  server = roar_server;
50
[448]51 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
52   user_sock[i] = 0;
53   server = user_sock;
54 }
55
[61]56 if ( server == NULL || *server == 0 ) {
[0]57  /* connect via defaults */
58
59
60  snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
61
62  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
63   return fh;
64
[237]65  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
66   return fh;
67
[0]68  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
69   return fh;
70
[522]71  if ( roar_socket_get_local_nodename() ) {
72   snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
73   return roar_socket_connect(user_sock, ROAR_DEFAULT_NUM);
74  }
75
[0]76 } else {
77  /* connect via (char*)server */
[501]78  // find a port:
79  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
80   for (i = 0; server[i] != 0; i++) {
81    if ( server[i] == ':' ) {
82     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
83      is_decnet = 1;
[516]84      obj = &server[i+2];
[501]85      break;
86     }
87
88     port = atoi(server+i+1);
89     server[i] = 0;
90     break;
91    }
[0]92   }
93  }
94
[521]95  if ( is_decnet ) {
96    *user_sock = 0;
97   if ( *server == ':' ) {
98    if ( roar_socket_get_local_nodename() )
99     strcat(user_sock, roar_socket_get_local_nodename());
100   }
101
102   strcat(user_sock, server);
[516]103   server = user_sock;
[521]104   if ( *obj == 0 ) {
105    strcat(user_sock, ROAR_DEFAULT_OBJECT);
106   }
[516]107  }
108
[501]109  if ( port || is_decnet ) {
[0]110   fh = roar_socket_connect(server, port);
111   // restore the original string
112   server[i] = ':';
113  } else {
114   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
115  }
116 }
117
[807]118 if ( fh == -1 )
119  roar_errno = ROAR_ERROR_CONNREFUSED;
120
[0]121 ROAR_DBG("roar_connect_raw(*) = %i", fh);
122
123 return fh;
124}
125
126int roar_connect    (struct roar_connection * con, char * server) {
[807]127 roar_errno = ROAR_ERROR_UNKNOWN;
[0]128 con->fh = roar_connect_raw(server);
129
130 if ( con->fh == -1 )
131  return -1;
132
[807]133 roar_errno = ROAR_ERROR_NONE;
134
[0]135 return 0;
136}
137
138int roar_disconnect (struct roar_connection * con) {
139 struct roar_message m;
140
141 m.datalen = 0;
142 m.stream  = 0;
[133]143 m.pos     = 0;
[0]144 m.cmd     = ROAR_CMD_QUIT;
145
146 roar_req(con, &m, NULL);
147
148 close(con->fh);
149
150 con->fh = -1;
151
[807]152 roar_errno = ROAR_ERROR_NONE;
153
[0]154 return 0;
155}
156
157int roar_identify   (struct roar_connection * con, char * name) {
158 struct roar_message mes;
159 pid_t pid;
160 int max_len;
161
[807]162 roar_errno = ROAR_ERROR_UNKNOWN;
163
[0]164 ROAR_DBG("roar_identify(*): try to identify myself...");
165
166 mes.cmd    = ROAR_CMD_IDENTIFY;
167 mes.stream = 0;
168 mes.pos    = 0;
169
170 ROAR_DBG("roar_identify(*): name=%p", name);
171
172 if ( name == NULL )
173  name = "libroar client";
174
175 ROAR_DBG("roar_identify(*): name=%p", name);
176
177 max_len = strlen(name);
178 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
179
180 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
181  max_len = LIBROAR_BUFFER_MSGDATA - 5;
182
183 mes.datalen = 5 + max_len;
184 mes.data[0] = 1;
185
186 pid = getpid();
187 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
188 ROAR_DBG("roar_identify(*): pid = %i", pid);
189
190 strncpy(mes.data+5, name, max_len);
191
192 return roar_req(con, &mes, NULL);
193}
194
[8]195#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
[0]196int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
197 char buf[_ROAR_MESS_BUF_LEN];
198
[807]199 roar_errno = ROAR_ERROR_UNKNOWN;
200
[0]201 ROAR_DBG("roar_send_message(*): try to send an request...");
202
[8]203 buf[0] = _ROAR_MESSAGE_VERSION;
204 buf[1] = (unsigned char) mes->cmd;
205 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
206 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
207 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
[0]208
[807]209 if ( write(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
210  roar_errno = ROAR_ERROR_PIPE;
[0]211  return -1;
[807]212 }
[0]213
[807]214 if ( mes->datalen != 0 ) {
215  if ( write(con->fh, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) {
216   roar_errno = ROAR_ERROR_PIPE;
[0]217   return -1;
[807]218  }
219 }
220
221 roar_errno = ROAR_ERROR_NONE;
[0]222
223 ROAR_DBG("roar_send_message(*) = 0");
224 return 0;
225}
226
227int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
228 char buf[_ROAR_MESS_BUF_LEN];
229
[807]230 roar_errno = ROAR_ERROR_UNKNOWN;
231
[0]232 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
233
234 if ( data )
235  *data = NULL;
236
[807]237 if ( read(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
238  roar_errno = ROAR_ERROR_PROTO;
[0]239  return -1;
[807]240 }
[0]241
242 ROAR_DBG("roar_recv_message(*): Got a header");
243
[807]244 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
245  roar_errno = ROAR_ERROR_PROTO;
[8]246  return -1;
[807]247 }
[8]248
249 mes->cmd     = (unsigned char)buf[1];
250 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
251 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
252 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
[0]253
254 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
255           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
256
[104]257 if ( mes->datalen == 0 ) {
258  ROAR_DBG("roar_recv_message(*): no data in this pkg");
259  ROAR_DBG("roar_recv_message(*) = 0");
[807]260  roar_errno = ROAR_ERROR_NONE;
[104]261  return 0;
262 }
263
[0]264 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
265  if ( read(con->fh, mes->data, mes->datalen) == mes->datalen ) {
266   ROAR_DBG("roar_recv_message(*): Got data!");
267   ROAR_DBG("roar_recv_message(*) = 0");
[807]268   roar_errno = ROAR_ERROR_NONE;
[0]269   return 0;
270  }
[807]271
272  roar_errno = ROAR_ERROR_PIPE;
[0]273  return -1;
274 } else {
[807]275  if ( data == NULL ) {
276   roar_errno = ROAR_ERROR_MSGSIZE;
[0]277   return -1;
[807]278  }
[0]279
[807]280  if ( (*data = malloc(mes->datalen)) == NULL ) {
281   roar_errno = ROAR_ERROR_NOMEM;
[0]282   return -1;
[807]283  }
[0]284
[807]285  if ( mes->datalen == 0 ) {
286   roar_errno = ROAR_ERROR_NONE;
[0]287   return 0;
[807]288  }
[0]289
290  if ( read(con->fh, *data, mes->datalen) == mes->datalen ) {
291   ROAR_DBG("roar_recv_message(*): Got data!");
292   ROAR_DBG("roar_recv_message(*) = 0");
[807]293   roar_errno = ROAR_ERROR_NONE;
[0]294   return 0;
295  }
[807]296
297  roar_errno = ROAR_ERROR_PIPE;
[0]298  return -1;
299 }
300
[807]301 // what happened here?
[0]302 return -1;
303}
304
305int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
306 if ( roar_send_message(con, mes, data ? *data : NULL) != 0 )
307  return -1;
308
309 if ( data )
310  free(*data);
311
312 return roar_recv_message(con, mes, data);
313}
314
315int roar_debug_message_print (struct roar_message * mes) {
316 if ( mes == NULL )
317  return -1;
318
319 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
320 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
321 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
322 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
323
324 ROAR_DBG("roar_debug_message_print(*) = 0");
325 return 0;
326}
327
328int roar_debug_audio_info_print (struct roar_audio_info * info) {
329 if ( info == NULL )
330  return -1;
331
332 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
333 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
334 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
335 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
336
337 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
338 return 0;
339}
340
341//ll
Note: See TracBrowser for help on using the repository browser.