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
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
46 roar_errno = ROAR_ERROR_UNKNOWN;
47
48 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
49  server = roar_server;
50
51 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
52   user_sock[i] = 0;
53   server = user_sock;
54 }
55
56 if ( server == NULL || *server == 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
65  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
66   return fh;
67
68  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
69   return fh;
70
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
76 } else {
77  /* connect via (char*)server */
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;
84      obj = &server[i+2];
85      break;
86     }
87
88     port = atoi(server+i+1);
89     server[i] = 0;
90     break;
91    }
92   }
93  }
94
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);
103   server = user_sock;
104   if ( *obj == 0 ) {
105    strcat(user_sock, ROAR_DEFAULT_OBJECT);
106   }
107  }
108
109  if ( port || is_decnet ) {
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
118 if ( fh == -1 )
119  roar_errno = ROAR_ERROR_CONNREFUSED;
120
121 ROAR_DBG("roar_connect_raw(*) = %i", fh);
122
123 return fh;
124}
125
126int roar_connect    (struct roar_connection * con, char * server) {
127 roar_errno = ROAR_ERROR_UNKNOWN;
128 con->fh = roar_connect_raw(server);
129
130 if ( con->fh == -1 )
131  return -1;
132
133 roar_errno = ROAR_ERROR_NONE;
134
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;
143 m.pos     = 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
152 roar_errno = ROAR_ERROR_NONE;
153
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
162 roar_errno = ROAR_ERROR_UNKNOWN;
163
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
195#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
196int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
197 char buf[_ROAR_MESS_BUF_LEN];
198
199 roar_errno = ROAR_ERROR_UNKNOWN;
200
201 ROAR_DBG("roar_send_message(*): try to send an request...");
202
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);
208
209 if ( write(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
210  roar_errno = ROAR_ERROR_PIPE;
211  return -1;
212 }
213
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;
217   return -1;
218  }
219 }
220
221 roar_errno = ROAR_ERROR_NONE;
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
230 roar_errno = ROAR_ERROR_UNKNOWN;
231
232 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
233
234 if ( data )
235  *data = NULL;
236
237 if ( read(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
238  roar_errno = ROAR_ERROR_PROTO;
239  return -1;
240 }
241
242 ROAR_DBG("roar_recv_message(*): Got a header");
243
244 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
245  roar_errno = ROAR_ERROR_PROTO;
246  return -1;
247 }
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));
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
257 if ( mes->datalen == 0 ) {
258  ROAR_DBG("roar_recv_message(*): no data in this pkg");
259  ROAR_DBG("roar_recv_message(*) = 0");
260  roar_errno = ROAR_ERROR_NONE;
261  return 0;
262 }
263
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");
268   roar_errno = ROAR_ERROR_NONE;
269   return 0;
270  }
271
272  roar_errno = ROAR_ERROR_PIPE;
273  return -1;
274 } else {
275  if ( data == NULL ) {
276   roar_errno = ROAR_ERROR_MSGSIZE;
277   return -1;
278  }
279
280  if ( (*data = malloc(mes->datalen)) == NULL ) {
281   roar_errno = ROAR_ERROR_NOMEM;
282   return -1;
283  }
284
285  if ( mes->datalen == 0 ) {
286   roar_errno = ROAR_ERROR_NONE;
287   return 0;
288  }
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");
293   roar_errno = ROAR_ERROR_NONE;
294   return 0;
295  }
296
297  roar_errno = ROAR_ERROR_PIPE;
298  return -1;
299 }
300
301 // what happened here?
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.