1 | //error.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
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_err_int(struct roar_error_frame * frame) { |
---|
39 | if ( frame == NULL ) |
---|
40 | return -1; |
---|
41 | |
---|
42 | memset(frame, 0, sizeof(struct roar_error_frame)); |
---|
43 | |
---|
44 | frame->cmd = -1; |
---|
45 | frame->ra_errno = -1; |
---|
46 | frame->ra_suberrno = -1; |
---|
47 | frame->p_errno = -1; |
---|
48 | |
---|
49 | return 0; |
---|
50 | } |
---|
51 | |
---|
52 | void * roar_err_buildmsg(struct roar_message * mes, struct roar_error_frame * frame) { |
---|
53 | int16_t * d; |
---|
54 | |
---|
55 | if ( mes == NULL || frame == NULL ) |
---|
56 | return NULL; |
---|
57 | |
---|
58 | memset(mes, 0, sizeof(struct roar_message)); |
---|
59 | |
---|
60 | d = (int16_t*)mes->data; |
---|
61 | |
---|
62 | mes->datalen = 8 + frame->datalen; |
---|
63 | frame->data = &(mes->data[8]); |
---|
64 | |
---|
65 | mes->data[0] = 0; // version. |
---|
66 | mes->data[1] = frame->cmd; |
---|
67 | mes->data[2] = frame->ra_errno; |
---|
68 | mes->data[3] = frame->ra_suberrno; |
---|
69 | d[2] = ROAR_HOST2NET16(frame->p_errno); |
---|
70 | d[3] = ROAR_HOST2NET16(frame->flags); |
---|
71 | |
---|
72 | return frame->data; |
---|
73 | } |
---|
74 | |
---|
75 | int roar_err_parsemsg(struct roar_message * mes, struct roar_error_frame * frame) { |
---|
76 | int16_t * d; |
---|
77 | |
---|
78 | if ( mes == NULL || frame == NULL ) |
---|
79 | return -1; |
---|
80 | |
---|
81 | d = (int16_t*)mes->data; |
---|
82 | |
---|
83 | if ( mes->datalen < 8 ) |
---|
84 | return -1; |
---|
85 | |
---|
86 | if ( mes->data[0] != 0 ) |
---|
87 | return -1; |
---|
88 | |
---|
89 | frame->cmd = mes->data[1]; |
---|
90 | frame->ra_errno = mes->data[2]; |
---|
91 | frame->ra_suberrno = mes->data[3]; |
---|
92 | frame->p_errno = ROAR_NET2HOST16(d[2]); |
---|
93 | frame->flags = ROAR_NET2HOST16(d[3]); |
---|
94 | |
---|
95 | frame->datalen = mes->datalen - 8; |
---|
96 | frame->data = &(mes->data[8]); |
---|
97 | |
---|
98 | return 0; |
---|
99 | } |
---|
100 | |
---|
101 | void roar_err_clear(void) { |
---|
102 | roar_errno = ROAR_ERROR_NONE; |
---|
103 | } |
---|
104 | |
---|
105 | void roar_err_from_errno(void) { |
---|
106 | switch (errno) { |
---|
107 | #ifdef EPERM |
---|
108 | case EPERM: roar_errno = ROAR_ERROR_PERM; break; |
---|
109 | #endif |
---|
110 | #ifdef ENOENT |
---|
111 | case ENOENT: roar_errno = ROAR_ERROR_NOENT; break; |
---|
112 | #endif |
---|
113 | #ifdef EBADMSG |
---|
114 | case EBADMSG: roar_errno = ROAR_ERROR_BADMSG; break; |
---|
115 | #endif |
---|
116 | #ifdef EBUSY |
---|
117 | case EBUSY: roar_errno = ROAR_ERROR_BUSY; break; |
---|
118 | #endif |
---|
119 | #ifdef ECONNREFUSED |
---|
120 | case ECONNREFUSED: roar_errno = ROAR_ERROR_CONNREFUSED; break; |
---|
121 | #endif |
---|
122 | #ifdef ENOSYS |
---|
123 | case ENOSYS: roar_errno = ROAR_ERROR_NOSYS; break; |
---|
124 | #endif |
---|
125 | #ifdef ENOTSUP |
---|
126 | case ENOTSUP: roar_errno = ROAR_ERROR_NOTSUP; break; |
---|
127 | #endif |
---|
128 | #ifdef EPIPE |
---|
129 | case EPIPE: roar_errno = ROAR_ERROR_PIPE; break; |
---|
130 | #endif |
---|
131 | #ifdef EPROTO |
---|
132 | case EPROTO: roar_errno = ROAR_ERROR_PROTO; break; |
---|
133 | #endif |
---|
134 | #ifdef ERANGE |
---|
135 | case ERANGE: roar_errno = ROAR_ERROR_RANGE; break; |
---|
136 | #endif |
---|
137 | #ifdef EMSGSIZE |
---|
138 | case EMSGSIZE: roar_errno = ROAR_ERROR_MSGSIZE; break; |
---|
139 | #endif |
---|
140 | #ifdef ENOMEM |
---|
141 | case ENOMEM: roar_errno = ROAR_ERROR_NOMEM; break; |
---|
142 | #endif |
---|
143 | #ifdef EINVAL |
---|
144 | case EINVAL: roar_errno = ROAR_ERROR_INVAL; break; |
---|
145 | #endif |
---|
146 | default: |
---|
147 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
148 | break; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | void roar_err_to_errno(void) { |
---|
153 | switch (roar_errno) { |
---|
154 | case ROAR_ERROR_NONE: |
---|
155 | errno = 0; // just gussing |
---|
156 | break; |
---|
157 | #ifdef EPERM |
---|
158 | case ROAR_ERROR_PERM: |
---|
159 | errno = EPERM; |
---|
160 | break; |
---|
161 | #endif |
---|
162 | #ifdef ENOENT |
---|
163 | case ROAR_ERROR_NOENT: |
---|
164 | errno = ENOENT; |
---|
165 | break; |
---|
166 | #endif |
---|
167 | #ifdef EBADMSG |
---|
168 | case ROAR_ERROR_BADMSG: |
---|
169 | errno = EBADMSG; |
---|
170 | break; |
---|
171 | #endif |
---|
172 | #ifdef EBUSY |
---|
173 | case ROAR_ERROR_BUSY: |
---|
174 | errno = EBUSY; |
---|
175 | break; |
---|
176 | #endif |
---|
177 | #ifdef ECONNREFUSED |
---|
178 | case ROAR_ERROR_CONNREFUSED: |
---|
179 | errno = ECONNREFUSED; |
---|
180 | break; |
---|
181 | #endif |
---|
182 | #ifdef ENOSYS |
---|
183 | case ROAR_ERROR_NOSYS: |
---|
184 | errno = ENOSYS; |
---|
185 | break; |
---|
186 | #endif |
---|
187 | #ifdef ENOTSUP |
---|
188 | case ROAR_ERROR_NOTSUP: |
---|
189 | errno = ENOTSUP; |
---|
190 | break; |
---|
191 | #endif |
---|
192 | #ifdef EPIPE |
---|
193 | case ROAR_ERROR_PIPE: |
---|
194 | errno = EPIPE; |
---|
195 | break; |
---|
196 | #endif |
---|
197 | #ifdef EPROTO |
---|
198 | case ROAR_ERROR_PROTO: |
---|
199 | errno = EPROTO; |
---|
200 | break; |
---|
201 | #endif |
---|
202 | #ifdef ERANGE |
---|
203 | case ROAR_ERROR_RANGE: |
---|
204 | errno = ERANGE; |
---|
205 | break; |
---|
206 | #endif |
---|
207 | #ifdef EMSGSIZE |
---|
208 | case ROAR_ERROR_MSGSIZE: |
---|
209 | errno = EMSGSIZE; |
---|
210 | break; |
---|
211 | #endif |
---|
212 | #ifdef ENOMEM |
---|
213 | case ROAR_ERROR_NOMEM: |
---|
214 | errno = ENOMEM; |
---|
215 | break; |
---|
216 | #endif |
---|
217 | #ifdef EINVAL |
---|
218 | case ROAR_ERROR_INVAL: |
---|
219 | errno = EINVAL; |
---|
220 | break; |
---|
221 | #endif |
---|
222 | default: |
---|
223 | #ifdef EINVAL |
---|
224 | errno = EINVAL; |
---|
225 | #else |
---|
226 | errno = -1; // just guess |
---|
227 | #endif |
---|
228 | break; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | //ll |
---|