]>
Commit | Line | Data |
---|---|---|
b5a8e848 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
b5a8e848 UH |
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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
c1aae900 | 22 | #include <libsigrok/libsigrok.h> |
b5a8e848 | 23 | |
393fb9cb UH |
24 | /** |
25 | * @file | |
26 | * | |
27 | * Error handling in libsigrok. | |
28 | */ | |
29 | ||
7b870c38 UH |
30 | /** |
31 | * @defgroup grp_error Error handling | |
32 | * | |
33 | * Error handling in libsigrok. | |
34 | * | |
f21193fa UH |
35 | * libsigrok functions usually return @ref SR_OK upon success, or a negative |
36 | * error code on failure. | |
37 | * | |
7b870c38 UH |
38 | * @{ |
39 | */ | |
40 | ||
b5a8e848 UH |
41 | /** |
42 | * Return a human-readable error string for the given libsigrok error code. | |
43 | * | |
44 | * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC. | |
9fb5f2df | 45 | * |
b5a8e848 UH |
46 | * @return A const string containing a short, human-readable (English) |
47 | * description of the error, such as "memory allocation error". | |
48 | * The string must NOT be free'd by the caller! | |
9fb5f2df | 49 | * |
b5a8e848 | 50 | * @see sr_strerror_name |
9fb5f2df UH |
51 | * |
52 | * @since 0.2.0 | |
b5a8e848 UH |
53 | */ |
54 | SR_API const char *sr_strerror(int error_code) | |
55 | { | |
b5a8e848 | 56 | /* |
409a811b | 57 | * Note: All defined SR_* error macros from libsigrok.h must have |
b5a8e848 UH |
58 | * an entry in this function, as well as in sr_strerror_name(). |
59 | */ | |
60 | ||
61 | switch (error_code) { | |
62 | case SR_OK: | |
f05e7b7a | 63 | return "no error"; |
b5a8e848 | 64 | case SR_ERR: |
f05e7b7a | 65 | return "generic/unspecified error"; |
b5a8e848 | 66 | case SR_ERR_MALLOC: |
f05e7b7a | 67 | return "memory allocation error"; |
0223135b | 68 | case SR_ERR_ARG: |
f05e7b7a | 69 | return "invalid argument"; |
b5a8e848 | 70 | case SR_ERR_BUG: |
f05e7b7a | 71 | return "internal error"; |
b5a8e848 | 72 | case SR_ERR_SAMPLERATE: |
f05e7b7a | 73 | return "invalid samplerate"; |
0223135b | 74 | case SR_ERR_NA: |
f05e7b7a | 75 | return "not applicable"; |
409a811b | 76 | case SR_ERR_DEV_CLOSED: |
f05e7b7a | 77 | return "device closed but should be open"; |
3e62166e | 78 | case SR_ERR_TIMEOUT: |
f05e7b7a | 79 | return "timeout occurred"; |
660e398f UH |
80 | case SR_ERR_CHANNEL_GROUP: |
81 | return "no channel group specified"; | |
b7f44605 BV |
82 | case SR_ERR_DATA: |
83 | return "data is invalid"; | |
3c558259 BG |
84 | case SR_ERR_IO: |
85 | return "input/output error"; | |
b5a8e848 | 86 | default: |
f05e7b7a | 87 | return "unknown error"; |
b5a8e848 | 88 | } |
b5a8e848 UH |
89 | } |
90 | ||
91 | /** | |
92 | * Return the "name" string of the given libsigrok error code. | |
93 | * | |
94 | * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC", | |
95 | * the name of the SR_OK code is "SR_OK", and so on. | |
96 | * | |
97 | * This function can be used for various purposes where the "name" string of | |
98 | * a libsigrok error code is useful. | |
99 | * | |
100 | * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC. | |
9fb5f2df | 101 | * |
b5a8e848 UH |
102 | * @return A const string containing the "name" of the error code as string. |
103 | * The string must NOT be free'd by the caller! | |
9fb5f2df | 104 | * |
b5a8e848 | 105 | * @see sr_strerror |
9fb5f2df UH |
106 | * |
107 | * @since 0.2.0 | |
b5a8e848 UH |
108 | */ |
109 | SR_API const char *sr_strerror_name(int error_code) | |
110 | { | |
b5a8e848 | 111 | /* |
409a811b | 112 | * Note: All defined SR_* error macros from libsigrok.h must have |
b5a8e848 UH |
113 | * an entry in this function, as well as in sr_strerror(). |
114 | */ | |
115 | ||
116 | switch (error_code) { | |
117 | case SR_OK: | |
f05e7b7a | 118 | return "SR_OK"; |
b5a8e848 | 119 | case SR_ERR: |
f05e7b7a | 120 | return "SR_ERR"; |
b5a8e848 | 121 | case SR_ERR_MALLOC: |
f05e7b7a | 122 | return "SR_ERR_MALLOC"; |
0223135b | 123 | case SR_ERR_ARG: |
f05e7b7a | 124 | return "SR_ERR_ARG"; |
b5a8e848 | 125 | case SR_ERR_BUG: |
f05e7b7a | 126 | return "SR_ERR_BUG"; |
b5a8e848 | 127 | case SR_ERR_SAMPLERATE: |
f05e7b7a | 128 | return "SR_ERR_SAMPLERATE"; |
0223135b | 129 | case SR_ERR_NA: |
f05e7b7a | 130 | return "SR_ERR_NA"; |
409a811b | 131 | case SR_ERR_DEV_CLOSED: |
f05e7b7a | 132 | return "SR_ERR_DEV_CLOSED"; |
3e62166e | 133 | case SR_ERR_TIMEOUT: |
f05e7b7a | 134 | return "SR_ERR_TIMEOUT"; |
660e398f UH |
135 | case SR_ERR_CHANNEL_GROUP: |
136 | return "SR_ERR_CHANNEL_GROUP"; | |
129d5bc9 UH |
137 | case SR_ERR_DATA: |
138 | return "SR_ERR_DATA"; | |
c4f95827 UH |
139 | case SR_ERR_IO: |
140 | return "SR_ERR_IO"; | |
b5a8e848 | 141 | default: |
f05e7b7a | 142 | return "unknown error code"; |
b5a8e848 | 143 | } |
b5a8e848 | 144 | } |
7b870c38 UH |
145 | |
146 | /** @} */ |