3d52c336087f7c3eae92b7f6c56e04b16883cd82
[comedilib.git] / lib / error.c
1 /*
2     lib/error.c
3     error handling routines
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1997-8 David A. Schleef <ds@stm.lbl.gov>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23
24 #include <libinternal.h>
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <comedi_errno.h>
29
30 char *__comedilib_error_strings[]={
31         "No error",
32         "Unknown error",
33         "Bad comedi_t structure",
34         "Invalid subdevice",
35         "Invalid channel",
36         "Buffer overflow",
37         "Buffer underflow",
38         "Command not supported",
39         "Not supported",
40 };
41 #define n_errors (sizeof(__comedilib_error_strings)/sizeof(void *))
42
43 int __comedi_loglevel=1;
44 int __comedi_errno=0;
45
46 int comedi_loglevel(int loglevel)
47 {
48         int old_loglevel=__comedi_loglevel;
49         
50         __comedi_loglevel=loglevel;
51         
52         return old_loglevel;
53 }
54
55 int comedi_errno(void)
56 {
57         return __comedi_errno;
58 }
59
60 char *comedi_strerror(int errnum)
61 {
62         if(errnum<COMEDI_NOERROR || errnum>=COMEDI_NOERROR+n_errors)
63                 return strerror(errnum);
64
65         return __comedilib_error_strings[errnum-COMEDI_NOERROR];
66 }
67
68 void comedi_perror(const char *s)
69 {
70         if(__comedi_loglevel>=3){
71                 fprintf(stderr,"comedi_perror(): __comedi_errno=%d\n",__comedi_errno);
72         }
73         if(!s)s="comedilib";
74         fprintf(stderr,"%s: %s\n",s,comedi_strerror(__comedi_errno));
75 }
76
77 void libc_error(void)
78 {
79         __comedi_errno=errno;
80         if(__comedi_loglevel>=2){
81                 comedi_perror("libc error");
82         }
83 }
84
85 void internal_error(int err)
86 {
87         __comedi_errno=err;
88         if(__comedi_loglevel>=2){
89                 comedi_perror("internal error");
90         }
91 }
92
93
94
95 int valid_dev(comedi_t *it)
96 {
97         if(!it || it->magic!=COMEDILIB_MAGIC){
98                 internal_error(COMEDILIB_BADDEV);
99                 return 0;
100         }
101         
102         return 1;
103 }
104
105 int valid_subd(comedi_t *it,unsigned int subd)
106 {
107         if(!valid_dev(it))return 0;
108         if(subd>=it->n_subdevices){
109                 internal_error(COMEDILIB_BADSUBD);
110                 return 0;
111         }
112         
113         return 1;
114 }
115
116 int valid_chan(comedi_t *it,unsigned int subd,unsigned int chan)
117 {
118         if(!valid_subd(it,subd))return 0;
119         if(chan>=it->subdevices[subd].n_chan){
120                 internal_error(COMEDILIB_BADCHAN);
121                 return 0;
122         }
123         
124         return 1;
125 }
126
127