]> sigrok.org Git - libsigrok.git/blame - error.c
README.devices: Minor updates.
[libsigrok.git] / 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
21#include "libsigrok.h"
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{
55 const char *str;
56
57 /*
409a811b 58 * Note: All defined SR_* error macros from libsigrok.h must have
b5a8e848
UH
59 * an entry in this function, as well as in sr_strerror_name().
60 */
61
62 switch (error_code) {
63 case SR_OK:
64 str = "no error";
65 break;
66 case SR_ERR:
67 str = "generic/unspecified error";
68 break;
69 case SR_ERR_MALLOC:
70 str = "memory allocation error";
71 break;
0223135b
UH
72 case SR_ERR_ARG:
73 str = "invalid argument";
74 break;
b5a8e848
UH
75 case SR_ERR_BUG:
76 str = "internal error";
77 break;
78 case SR_ERR_SAMPLERATE:
79 str = "invalid samplerate";
80 break;
0223135b
UH
81 case SR_ERR_NA:
82 str = "not applicable";
83 break;
409a811b
UH
84 case SR_ERR_DEV_CLOSED:
85 str = "device closed but should be open";
86 break;
3e62166e
BV
87 case SR_ERR_TIMEOUT:
88 str = "timeout occurred";
89 break;
b5a8e848
UH
90 default:
91 str = "unknown error";
92 break;
93 }
94
95 return str;
96}
97
98/**
99 * Return the "name" string of the given libsigrok error code.
100 *
101 * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
102 * the name of the SR_OK code is "SR_OK", and so on.
103 *
104 * This function can be used for various purposes where the "name" string of
105 * a libsigrok error code is useful.
106 *
107 * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
9fb5f2df 108 *
b5a8e848
UH
109 * @return A const string containing the "name" of the error code as string.
110 * The string must NOT be free'd by the caller!
9fb5f2df 111 *
b5a8e848 112 * @see sr_strerror
9fb5f2df
UH
113 *
114 * @since 0.2.0
b5a8e848
UH
115 */
116SR_API const char *sr_strerror_name(int error_code)
117{
118 const char *str;
119
120 /*
409a811b 121 * Note: All defined SR_* error macros from libsigrok.h must have
b5a8e848
UH
122 * an entry in this function, as well as in sr_strerror().
123 */
124
125 switch (error_code) {
126 case SR_OK:
127 str = "SR_OK";
128 break;
129 case SR_ERR:
130 str = "SR_ERR";
131 break;
132 case SR_ERR_MALLOC:
133 str = "SR_ERR_MALLOC";
134 break;
0223135b
UH
135 case SR_ERR_ARG:
136 str = "SR_ERR_ARG";
137 break;
b5a8e848
UH
138 case SR_ERR_BUG:
139 str = "SR_ERR_BUG";
140 break;
141 case SR_ERR_SAMPLERATE:
142 str = "SR_ERR_SAMPLERATE";
143 break;
0223135b
UH
144 case SR_ERR_NA:
145 str = "SR_ERR_NA";
146 break;
409a811b
UH
147 case SR_ERR_DEV_CLOSED:
148 str = "SR_ERR_DEV_CLOSED";
149 break;
3e62166e
BV
150 case SR_ERR_TIMEOUT:
151 str = "SR_ERR_TIMEOUT";
152 break;
b5a8e848
UH
153 default:
154 str = "unknown error code";
155 break;
156 }
157
158 return str;
159}
7b870c38
UH
160
161/** @} */