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 | |
---|
38 | int roar_errno = ROAR_ERROR_NONE; |
---|
39 | |
---|
40 | int 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 | |
---|
54 | void * 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 | |
---|
77 | int 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 | |
---|
103 | int * roar_errno2(void) { |
---|
104 | return &roar_errno; |
---|
105 | } |
---|
106 | |
---|
107 | void roar_err_clear(void) { |
---|
108 | *roar_errno2() = ROAR_ERROR_NONE; |
---|
109 | } |
---|
110 | |
---|
111 | void roar_err_set(const int error) { |
---|
112 | *roar_errno2() = error; |
---|
113 | } |
---|
114 | |
---|
115 | void 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 | #ifdef EALREADY |
---|
159 | case EALREADY: _roar_errno = ROAR_ERROR_ALREADY; break; |
---|
160 | #endif |
---|
161 | #ifdef EBADRQC |
---|
162 | case EBADRQC: _roar_errno = ROAR_ERROR_BADRQC; break; |
---|
163 | #endif |
---|
164 | #ifdef EDOM |
---|
165 | case EDOM: _roar_errno = ROAR_ERROR_DOM; break; |
---|
166 | #endif |
---|
167 | #ifdef EEXIST |
---|
168 | case EEXIST: _roar_errno = ROAR_ERROR_EXIST; break; |
---|
169 | #endif |
---|
170 | #ifdef EFAULT |
---|
171 | case EFAULT: _roar_errno = ROAR_ERROR_FAULT; break; |
---|
172 | #endif |
---|
173 | #ifdef EIO |
---|
174 | case EIO: _roar_errno = ROAR_ERROR_IO; break; |
---|
175 | #endif |
---|
176 | #ifdef EKEYEXPIRED |
---|
177 | case EKEYEXPIRED: _roar_errno = ROAR_ERROR_KEYEXPIRED; break; |
---|
178 | #endif |
---|
179 | #ifdef EKEYREJECTED |
---|
180 | case EKEYREJECTED: _roar_errno = ROAR_ERROR_KEYREJECTED; break; |
---|
181 | #endif |
---|
182 | #ifdef ELOOP |
---|
183 | case ELOOP: _roar_errno = ROAR_ERROR_LOOP; break; |
---|
184 | #endif |
---|
185 | #ifdef EMFILE |
---|
186 | case EMFILE: _roar_errno = ROAR_ERROR_MFILE; break; |
---|
187 | #endif |
---|
188 | #ifdef ENAMETOOLONG |
---|
189 | case ENAMETOOLONG: _roar_errno = ROAR_ERROR_NAMETOOLONG; break; |
---|
190 | #endif |
---|
191 | #ifdef ENODATA |
---|
192 | case ENODATA: _roar_errno = ROAR_ERROR_NODATA; break; |
---|
193 | #endif |
---|
194 | #ifdef ENODEV |
---|
195 | case ENODEV: _roar_errno = ROAR_ERROR_NODEV; break; |
---|
196 | #endif |
---|
197 | #ifdef ENOSPC |
---|
198 | case ENOSPC: _roar_errno = ROAR_ERROR_NOSPC; break; |
---|
199 | #endif |
---|
200 | #ifdef ENOTCONN |
---|
201 | case ENOTCONN: _roar_errno = ROAR_ERROR_NOTCONN; break; |
---|
202 | #endif |
---|
203 | #ifdef EPROTONOSUPPORT |
---|
204 | case EPROTONOSUPPORT: _roar_errno = ROAR_ERROR_PROTONOSUP; break; |
---|
205 | #endif |
---|
206 | #ifdef EROFS |
---|
207 | case EROFS: _roar_errno = ROAR_ERROR_RO; break; |
---|
208 | #endif |
---|
209 | #ifdef ETIMEDOUT |
---|
210 | case ETIMEDOUT: _roar_errno = ROAR_ERROR_TIMEDOUT; break; |
---|
211 | #endif |
---|
212 | #ifdef EAGAIN |
---|
213 | case EAGAIN: _roar_errno = ROAR_ERROR_AGAIN; break; |
---|
214 | #endif |
---|
215 | #ifdef ENETDOWN |
---|
216 | case ENETDOWN: _roar_errno = ROAR_ERROR_LINKDOWN; break; |
---|
217 | #endif |
---|
218 | default: |
---|
219 | _roar_errno = ROAR_ERROR_UNKNOWN; |
---|
220 | break; |
---|
221 | } |
---|
222 | |
---|
223 | roar_err_set(_roar_errno); |
---|
224 | } |
---|
225 | |
---|
226 | void roar_err_to_errno(void) { |
---|
227 | int * err = roar_errno2(); |
---|
228 | switch (*err) { |
---|
229 | case ROAR_ERROR_NONE: |
---|
230 | errno = 0; // just gussing |
---|
231 | break; |
---|
232 | #ifdef EPERM |
---|
233 | case ROAR_ERROR_PERM: |
---|
234 | errno = EPERM; |
---|
235 | break; |
---|
236 | #endif |
---|
237 | #ifdef ENOENT |
---|
238 | case ROAR_ERROR_NOENT: |
---|
239 | errno = ENOENT; |
---|
240 | break; |
---|
241 | #endif |
---|
242 | #ifdef EBADMSG |
---|
243 | case ROAR_ERROR_BADMSG: |
---|
244 | errno = EBADMSG; |
---|
245 | break; |
---|
246 | #endif |
---|
247 | #ifdef EBUSY |
---|
248 | case ROAR_ERROR_BUSY: |
---|
249 | errno = EBUSY; |
---|
250 | break; |
---|
251 | #endif |
---|
252 | #ifdef ECONNREFUSED |
---|
253 | case ROAR_ERROR_CONNREFUSED: |
---|
254 | errno = ECONNREFUSED; |
---|
255 | break; |
---|
256 | #endif |
---|
257 | #ifdef ENOSYS |
---|
258 | case ROAR_ERROR_NOSYS: |
---|
259 | errno = ENOSYS; |
---|
260 | break; |
---|
261 | #endif |
---|
262 | #ifdef ENOTSUP |
---|
263 | case ROAR_ERROR_NOTSUP: |
---|
264 | errno = ENOTSUP; |
---|
265 | break; |
---|
266 | #endif |
---|
267 | #ifdef EPIPE |
---|
268 | case ROAR_ERROR_PIPE: |
---|
269 | errno = EPIPE; |
---|
270 | break; |
---|
271 | #endif |
---|
272 | #ifdef EPROTO |
---|
273 | case ROAR_ERROR_PROTO: |
---|
274 | errno = EPROTO; |
---|
275 | break; |
---|
276 | #endif |
---|
277 | #ifdef ERANGE |
---|
278 | case ROAR_ERROR_RANGE: |
---|
279 | errno = ERANGE; |
---|
280 | break; |
---|
281 | #endif |
---|
282 | #ifdef EMSGSIZE |
---|
283 | case ROAR_ERROR_MSGSIZE: |
---|
284 | errno = EMSGSIZE; |
---|
285 | break; |
---|
286 | #endif |
---|
287 | #ifdef ENOMEM |
---|
288 | case ROAR_ERROR_NOMEM: |
---|
289 | errno = ENOMEM; |
---|
290 | break; |
---|
291 | #endif |
---|
292 | #ifdef EINVAL |
---|
293 | case ROAR_ERROR_INVAL: |
---|
294 | errno = EINVAL; |
---|
295 | break; |
---|
296 | #endif |
---|
297 | #ifdef EALREADY |
---|
298 | case ROAR_ERROR_ALREADY: |
---|
299 | errno = EALREADY; |
---|
300 | break; |
---|
301 | #endif |
---|
302 | #ifdef EBADRQC |
---|
303 | case ROAR_ERROR_BADRQC: |
---|
304 | errno = EBADRQC; |
---|
305 | break; |
---|
306 | #endif |
---|
307 | #ifdef EDOM |
---|
308 | case ROAR_ERROR_DOM: |
---|
309 | errno = EDOM; |
---|
310 | break; |
---|
311 | #endif |
---|
312 | #ifdef EEXIST |
---|
313 | case ROAR_ERROR_EXIST: |
---|
314 | errno = EEXIST; |
---|
315 | break; |
---|
316 | #endif |
---|
317 | #ifdef EFAULT |
---|
318 | case ROAR_ERROR_FAULT: |
---|
319 | errno = EFAULT; |
---|
320 | break; |
---|
321 | #endif |
---|
322 | #ifdef EIO |
---|
323 | case ROAR_ERROR_IO: |
---|
324 | case ROAR_ERROR_RIO: |
---|
325 | errno = EIO; |
---|
326 | break; |
---|
327 | #endif |
---|
328 | #ifdef EKEYEXPIRED |
---|
329 | case ROAR_ERROR_KEYEXPIRED: |
---|
330 | errno = EKEYEXPIRED; |
---|
331 | break; |
---|
332 | #endif |
---|
333 | #ifdef EKEYREJECTED |
---|
334 | case ROAR_ERROR_KEYREJECTED: |
---|
335 | errno = EKEYREJECTED; |
---|
336 | break; |
---|
337 | #endif |
---|
338 | #ifdef ELOOP |
---|
339 | case ROAR_ERROR_LOOP: |
---|
340 | errno = ELOOP; |
---|
341 | break; |
---|
342 | #endif |
---|
343 | #ifdef EMFILE |
---|
344 | case ROAR_ERROR_MFILE: |
---|
345 | errno = EMFILE; |
---|
346 | break; |
---|
347 | #endif |
---|
348 | #ifdef ENAMETOOLONG |
---|
349 | case ROAR_ERROR_NAMETOOLONG: |
---|
350 | errno = ENAMETOOLONG; |
---|
351 | break; |
---|
352 | #endif |
---|
353 | #ifdef ENODATA |
---|
354 | case ROAR_ERROR_NODATA: |
---|
355 | errno = ENODATA; |
---|
356 | break; |
---|
357 | #endif |
---|
358 | #ifdef ENODEV |
---|
359 | case ROAR_ERROR_NODEV: |
---|
360 | case ROAR_ERROR_NODRV: |
---|
361 | errno = ENODEV; |
---|
362 | break; |
---|
363 | #endif |
---|
364 | #ifdef ENOSPC |
---|
365 | case ROAR_ERROR_NOSPC: |
---|
366 | errno = ENOSPC; |
---|
367 | break; |
---|
368 | #endif |
---|
369 | #ifdef EINVAL |
---|
370 | case ROAR_ERROR_TYPEMM: |
---|
371 | errno = EINVAL; |
---|
372 | break; |
---|
373 | #endif |
---|
374 | #ifdef ENOSYS |
---|
375 | case ROAR_ERROR_NORSYS: |
---|
376 | errno = ENOSYS; |
---|
377 | break; |
---|
378 | #endif |
---|
379 | #ifdef ENOTCONN |
---|
380 | case ROAR_ERROR_NOTCONN: |
---|
381 | errno = ENOTCONN; |
---|
382 | break; |
---|
383 | #endif |
---|
384 | #ifdef EPROTONOSUPPORT |
---|
385 | case ROAR_ERROR_PROTONOSUP: |
---|
386 | errno = EPROTONOSUPPORT; |
---|
387 | break; |
---|
388 | #endif |
---|
389 | #ifdef EROFS |
---|
390 | case ROAR_ERROR_RO: |
---|
391 | errno = EROFS; |
---|
392 | break; |
---|
393 | #endif |
---|
394 | #ifdef ETIMEDOUT |
---|
395 | case ROAR_ERROR_TIMEDOUT: |
---|
396 | errno = ETIMEDOUT; |
---|
397 | break; |
---|
398 | #endif |
---|
399 | #ifdef EAGAIN |
---|
400 | case ROAR_ERROR_AGAIN: |
---|
401 | errno = EAGAIN; |
---|
402 | break; |
---|
403 | #endif |
---|
404 | #ifdef ENETDOWN |
---|
405 | case ROAR_ERROR_LINKDOWN: |
---|
406 | errno = ENETDOWN; |
---|
407 | break; |
---|
408 | #endif |
---|
409 | default: |
---|
410 | #ifdef EINVAL |
---|
411 | errno = EINVAL; |
---|
412 | #else |
---|
413 | errno = -1; // just guess |
---|
414 | #endif |
---|
415 | break; |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | const char * roar_error2str(const int error) { |
---|
421 | const struct { |
---|
422 | const int err; |
---|
423 | const char * msg; |
---|
424 | } msgs[] = { |
---|
425 | {ROAR_ERROR_NONE, "No error"}, |
---|
426 | {ROAR_ERROR_PERM, "Operation not permitted"}, |
---|
427 | {ROAR_ERROR_NOENT, "No such object, file or directory"}, |
---|
428 | {ROAR_ERROR_BADMSG, "Bad message"}, |
---|
429 | {ROAR_ERROR_BUSY, "Device or resource busy"}, |
---|
430 | {ROAR_ERROR_CONNREFUSED, "Connection refused"}, |
---|
431 | {ROAR_ERROR_NOSYS, "Function not implemented"}, |
---|
432 | {ROAR_ERROR_NOTSUP, "Operation not supported"}, |
---|
433 | {ROAR_ERROR_PIPE, "Broken pipe"}, |
---|
434 | {ROAR_ERROR_PROTO, "Protocol error"}, |
---|
435 | {ROAR_ERROR_RANGE, "Result too large or parameter out of range"}, |
---|
436 | {ROAR_ERROR_MSGSIZE, "Message too long"}, |
---|
437 | {ROAR_ERROR_NOMEM, "Not enough space"}, |
---|
438 | {ROAR_ERROR_INVAL, "Invalid argument"}, |
---|
439 | {ROAR_ERROR_ALREADY, "Connection already in progress"}, |
---|
440 | {ROAR_ERROR_BADRQC, "Invalid request code"}, |
---|
441 | {ROAR_ERROR_DOM, "Mathematics argument out of domain of function"}, |
---|
442 | {ROAR_ERROR_EXIST, "File or object exists"}, |
---|
443 | {ROAR_ERROR_FAULT, "Bad address"}, |
---|
444 | {ROAR_ERROR_IO, "I/O-Error"}, |
---|
445 | {ROAR_ERROR_KEYEXPIRED, "Key has expired"}, |
---|
446 | {ROAR_ERROR_KEYREJECTED, "Key was rejected by service"}, |
---|
447 | {ROAR_ERROR_LOOP, "Too many recursions"}, |
---|
448 | {ROAR_ERROR_MFILE, "Too many open files or objects"}, |
---|
449 | {ROAR_ERROR_NAMETOOLONG, "File or object name too long"}, |
---|
450 | {ROAR_ERROR_NODATA, "No message is available on the read queue"}, |
---|
451 | {ROAR_ERROR_NODEV, "No such device"}, |
---|
452 | {ROAR_ERROR_NODRV, "No such driver"}, |
---|
453 | {ROAR_ERROR_NOSPC, "No space left on device"}, |
---|
454 | {ROAR_ERROR_TYPEMM, "Type missmatch. Object of diffrent type required"}, |
---|
455 | {ROAR_ERROR_NORSYS, "Feature not implemented by remote end"}, |
---|
456 | {ROAR_ERROR_NOTCONN, "Socket or object not connected"}, |
---|
457 | {ROAR_ERROR_PROTONOSUP, "Protocol not supported"}, |
---|
458 | {ROAR_ERROR_RIO, "Remote I/O Error"}, |
---|
459 | {ROAR_ERROR_RO, "File or object is read only"}, |
---|
460 | {ROAR_ERROR_TIMEDOUT, "Connection timed out"}, |
---|
461 | {ROAR_ERROR_AGAIN, "Resource temporarily unavailable"}, |
---|
462 | {ROAR_ERROR_NOISE, "Line too noisy"}, |
---|
463 | {ROAR_ERROR_LINKDOWN, "Physical or logical link down"}, |
---|
464 | {-1, NULL} |
---|
465 | }; |
---|
466 | int i; |
---|
467 | |
---|
468 | for (i = 0; msgs[i].msg != NULL; i++) |
---|
469 | if ( msgs[i].err == error ) |
---|
470 | return msgs[i].msg; |
---|
471 | |
---|
472 | return NULL; |
---|
473 | } |
---|
474 | |
---|
475 | //ll |
---|