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