source: roaraudio/libroar/basic.c @ 690:cee9bf5fa456

Last change on this file since 690:cee9bf5fa456 was 690:cee9bf5fa456, checked in by phi, 16 years ago

added copyright statements

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