libsigrok  unreleased development snapshot
sigrok hardware access and backend library
error.c
Go to the documentation of this file.
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 #include <libsigrok/libsigrok.h>
22 
23 /**
24  * @file
25  *
26  * Error handling in libsigrok.
27  */
28 
29 /**
30  * @defgroup grp_error Error handling
31  *
32  * Error handling in libsigrok.
33  *
34  * libsigrok functions usually return @ref SR_OK upon success, or a negative
35  * error code on failure.
36  *
37  * @{
38  */
39 
40 /**
41  * Return a human-readable error string for the given libsigrok error code.
42  *
43  * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
44  *
45  * @return A const string containing a short, human-readable (English)
46  * description of the error, such as "memory allocation error".
47  * The string must NOT be free'd by the caller!
48  *
49  * @see sr_strerror_name
50  *
51  * @since 0.2.0
52  */
53 SR_API const char *sr_strerror(int error_code)
54 {
55  /*
56  * Note: All defined SR_* error macros from libsigrok.h must have
57  * an entry in this function, as well as in sr_strerror_name().
58  */
59 
60  switch (error_code) {
61  case SR_OK:
62  return "no error";
63  case SR_ERR:
64  return "generic/unspecified error";
65  case SR_ERR_MALLOC:
66  return "memory allocation error";
67  case SR_ERR_ARG:
68  return "invalid argument";
69  case SR_ERR_BUG:
70  return "internal error";
71  case SR_ERR_SAMPLERATE:
72  return "invalid samplerate";
73  case SR_ERR_NA:
74  return "not applicable";
75  case SR_ERR_DEV_CLOSED:
76  return "device closed but should be open";
77  case SR_ERR_TIMEOUT:
78  return "timeout occurred";
80  return "no channel group specified";
81  case SR_ERR_DATA:
82  return "data is invalid";
83  case SR_ERR_IO:
84  return "input/output error";
85  default:
86  return "unknown error";
87  }
88 }
89 
90 /**
91  * Return the "name" string of the given libsigrok error code.
92  *
93  * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
94  * the name of the SR_OK code is "SR_OK", and so on.
95  *
96  * This function can be used for various purposes where the "name" string of
97  * a libsigrok error code is useful.
98  *
99  * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
100  *
101  * @return A const string containing the "name" of the error code as string.
102  * The string must NOT be free'd by the caller!
103  *
104  * @see sr_strerror
105  *
106  * @since 0.2.0
107  */
108 SR_API const char *sr_strerror_name(int error_code)
109 {
110  /*
111  * Note: All defined SR_* error macros from libsigrok.h must have
112  * an entry in this function, as well as in sr_strerror().
113  */
114 
115  switch (error_code) {
116  case SR_OK:
117  return "SR_OK";
118  case SR_ERR:
119  return "SR_ERR";
120  case SR_ERR_MALLOC:
121  return "SR_ERR_MALLOC";
122  case SR_ERR_ARG:
123  return "SR_ERR_ARG";
124  case SR_ERR_BUG:
125  return "SR_ERR_BUG";
126  case SR_ERR_SAMPLERATE:
127  return "SR_ERR_SAMPLERATE";
128  case SR_ERR_NA:
129  return "SR_ERR_NA";
130  case SR_ERR_DEV_CLOSED:
131  return "SR_ERR_DEV_CLOSED";
132  case SR_ERR_TIMEOUT:
133  return "SR_ERR_TIMEOUT";
135  return "SR_ERR_CHANNEL_GROUP";
136  case SR_ERR_DATA:
137  return "SR_ERR_DATA";
138  case SR_ERR_IO:
139  return "SR_ERR_IO";
140  default:
141  return "unknown error code";
142  }
143 }
144 
145 /** @} */
Data is invalid.
Definition: libsigrok.h:77
No error.
Definition: libsigrok.h:67
#define SR_API
Definition: libsigrok.h:128
A timeout occurred.
Definition: libsigrok.h:75
Input/output error.
Definition: libsigrok.h:78
Incorrect samplerate.
Definition: libsigrok.h:72
Generic/unspecified error.
Definition: libsigrok.h:68
Errors hinting at internal bugs.
Definition: libsigrok.h:71
const char * sr_strerror_name(int error_code)
Return the "name" string of the given libsigrok error code.
Definition: error.c:108
Malloc/calloc/realloc error.
Definition: libsigrok.h:69
Function argument error.
Definition: libsigrok.h:70
A channel group must be specified.
Definition: libsigrok.h:76
The public libsigrok header file to be used by frontends.
Not applicable.
Definition: libsigrok.h:73
Device is closed, but must be open.
Definition: libsigrok.h:74
const char * sr_strerror(int error_code)
Return a human-readable error string for the given libsigrok error code.
Definition: error.c:53