source: roaraudio/libroar/error.c @ 4869:431d1b8794f0

Last change on this file since 4869:431d1b8794f0 was 4869:431d1b8794f0, checked in by phi, 13 years ago

implemented roar_errno2()

File size: 6.4 KB
Line 
1//error.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
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
36#include "libroar.h"
37
38int roar_errno = ROAR_ERROR_NONE;
39
40int    roar_err_int(struct roar_error_frame * frame) {
41 if ( frame == NULL )
42  return -1;
43
44 memset(frame, 0, sizeof(struct roar_error_frame));
45
46 frame->cmd         = -1;
47 frame->ra_errno    = -1;
48 frame->ra_suberrno = -1;
49 frame->p_errno     = -1;
50
51 return 0;
52}
53
54void * roar_err_buildmsg(struct roar_message * mes, struct roar_error_frame * frame) {
55 int16_t * d;
56
57 if ( mes == NULL || frame == NULL )
58  return NULL;
59
60 memset(mes,  0, sizeof(struct roar_message));
61
62 d = (int16_t*)mes->data;
63
64 mes->datalen = 8 + frame->datalen;
65 frame->data  = &(mes->data[8]);
66
67 mes->data[0]    = 0; // version.
68 mes->data[1]    = frame->cmd;
69 mes->data[2]    = frame->ra_errno;
70 mes->data[3]    = frame->ra_suberrno;
71 d[2]            = ROAR_HOST2NET16(frame->p_errno);
72 d[3]            = ROAR_HOST2NET16(frame->flags);
73
74 return frame->data;
75}
76
77int    roar_err_parsemsg(struct roar_message * mes, struct roar_error_frame * frame) {
78 int16_t * d;
79
80 if ( mes == NULL || frame == NULL )
81  return -1;
82
83 d = (int16_t*)mes->data;
84
85 if ( mes->datalen < 8 )
86  return -1;
87
88 if ( mes->data[0] != 0 )
89  return -1;
90
91 frame->cmd         = mes->data[1];
92 frame->ra_errno    = mes->data[2];
93 frame->ra_suberrno = mes->data[3];
94 frame->p_errno     = ROAR_NET2HOST16(d[2]);
95 frame->flags       = ROAR_NET2HOST16(d[3]);
96
97 frame->datalen     = mes->datalen - 8;
98 frame->data        = &(mes->data[8]);
99
100 return 0;
101}
102
103int *  roar_errno2(void) {
104 return &roar_errno;
105}
106
107void   roar_err_clear(void) {
108 *roar_errno2() = ROAR_ERROR_NONE;
109}
110
111void   roar_err_set(const int error) {
112 *roar_errno2() = error;
113}
114
115void   roar_err_from_errno(void) {
116 int _roar_errno = ROAR_ERROR_NONE;
117
118 switch (errno) {
119#ifdef EPERM
120  case EPERM:        _roar_errno = ROAR_ERROR_PERM; break;
121#endif
122#ifdef ENOENT
123  case ENOENT:       _roar_errno = ROAR_ERROR_NOENT; break;
124#endif
125#ifdef EBADMSG
126  case EBADMSG:      _roar_errno = ROAR_ERROR_BADMSG; break;
127#endif
128#ifdef EBUSY
129  case EBUSY:        _roar_errno = ROAR_ERROR_BUSY; break;
130#endif
131#ifdef ECONNREFUSED
132  case ECONNREFUSED: _roar_errno = ROAR_ERROR_CONNREFUSED; break;
133#endif
134#ifdef ENOSYS
135  case ENOSYS:       _roar_errno = ROAR_ERROR_NOSYS; break;
136#endif
137#ifdef ENOTSUP
138  case ENOTSUP:      _roar_errno = ROAR_ERROR_NOTSUP; break;
139#endif
140#ifdef EPIPE
141  case EPIPE:        _roar_errno = ROAR_ERROR_PIPE; break;
142#endif
143#ifdef EPROTO
144  case EPROTO:       _roar_errno = ROAR_ERROR_PROTO; break;
145#endif
146#ifdef ERANGE
147  case ERANGE:       _roar_errno = ROAR_ERROR_RANGE; break;
148#endif
149#ifdef EMSGSIZE
150  case EMSGSIZE:     _roar_errno = ROAR_ERROR_MSGSIZE; break;
151#endif
152#ifdef ENOMEM
153  case ENOMEM:       _roar_errno = ROAR_ERROR_NOMEM; break;
154#endif
155#ifdef EINVAL
156  case EINVAL:       _roar_errno = ROAR_ERROR_INVAL; break;
157#endif
158  default:
159    _roar_errno = ROAR_ERROR_UNKNOWN;
160   break;
161 }
162
163 roar_err_set(_roar_errno);
164}
165
166void   roar_err_to_errno(void) {
167 int * err = roar_errno2();
168 switch (*err) {
169  case ROAR_ERROR_NONE:
170    errno = 0; // just gussing
171   break;
172#ifdef EPERM
173  case ROAR_ERROR_PERM:
174    errno = EPERM;
175   break;
176#endif
177#ifdef ENOENT
178  case ROAR_ERROR_NOENT:
179    errno = ENOENT;
180   break;
181#endif
182#ifdef EBADMSG
183  case ROAR_ERROR_BADMSG:
184    errno = EBADMSG;
185   break;
186#endif
187#ifdef EBUSY
188  case ROAR_ERROR_BUSY:
189    errno = EBUSY;
190   break;
191#endif
192#ifdef ECONNREFUSED
193  case ROAR_ERROR_CONNREFUSED:
194    errno = ECONNREFUSED;
195   break;
196#endif
197#ifdef ENOSYS
198  case ROAR_ERROR_NOSYS:
199    errno = ENOSYS;
200   break;
201#endif
202#ifdef ENOTSUP
203  case ROAR_ERROR_NOTSUP:
204    errno = ENOTSUP;
205   break;
206#endif
207#ifdef EPIPE
208  case ROAR_ERROR_PIPE:
209    errno = EPIPE;
210   break;
211#endif
212#ifdef EPROTO
213  case ROAR_ERROR_PROTO:
214    errno = EPROTO;
215   break;
216#endif
217#ifdef ERANGE
218  case ROAR_ERROR_RANGE:
219    errno = ERANGE;
220   break;
221#endif
222#ifdef EMSGSIZE
223  case ROAR_ERROR_MSGSIZE:
224    errno = EMSGSIZE;
225   break;
226#endif
227#ifdef ENOMEM
228  case ROAR_ERROR_NOMEM:
229    errno = ENOMEM;
230   break;
231#endif
232#ifdef EINVAL
233  case ROAR_ERROR_INVAL:
234    errno = EINVAL;
235   break;
236#endif
237  default:
238#ifdef EINVAL
239    errno = EINVAL;
240#else
241    errno = -1; // just guess
242#endif
243   break;
244 }
245}
246
247
248const char * roar_error2str(const int error) {
249 const struct {
250  const int    err;
251  const char * msg;
252 } msgs[] = {
253  {ROAR_ERROR_NONE,        "No error"},
254  {ROAR_ERROR_PERM,        "Operation not permitted"},
255  {ROAR_ERROR_NOENT,       "No such object, file or directory"},
256  {ROAR_ERROR_BADMSG,      "Bad message"},
257  {ROAR_ERROR_BUSY,        "Device or resource busy"},
258  {ROAR_ERROR_CONNREFUSED, "Connection refused"},
259  {ROAR_ERROR_NOSYS,       "Function not implemented"},
260  {ROAR_ERROR_NOTSUP,      "Operation not supported"},
261  {ROAR_ERROR_PIPE,        "Broken pipe"},
262  {ROAR_ERROR_PROTO,       "Protocol error"},
263  {ROAR_ERROR_RANGE,       "Result too large or parameter out of range"},
264  {ROAR_ERROR_MSGSIZE,     "Message too long"},
265  {ROAR_ERROR_NOMEM,       "Not enough space"},
266  {ROAR_ERROR_INVAL,       "Invalid argument"},
267  {-1, NULL}
268 };
269 int i;
270
271 for (i = 0; msgs[i].msg != NULL; i++)
272  if ( msgs[i].err == error )
273   return msgs[i].msg;
274
275 return NULL;
276}
277
278//ll
Note: See TracBrowser for help on using the repository browser.