2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 #include "libsigrok.h"
26 * Error handling in libsigrok.
30 * @defgroup grp_error Error handling
32 * Error handling in libsigrok.
34 * libsigrok functions usually return @ref SR_OK upon success, or a negative
35 * error code on failure.
41 * Return a human-readable error string for the given libsigrok error code.
43 * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
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!
49 * @see sr_strerror_name
53 SR_API const char *sr_strerror(int error_code)
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().
64 return "generic/unspecified error";
66 return "memory allocation error";
68 return "invalid argument";
70 return "internal error";
71 case SR_ERR_SAMPLERATE:
72 return "invalid samplerate";
74 return "not applicable";
75 case SR_ERR_DEV_CLOSED:
76 return "device closed but should be open";
78 return "timeout occurred";
79 case SR_ERR_PROBE_GROUP:
80 return "no probe group specified";
82 return "unknown error";
87 * Return the "name" string of the given libsigrok error code.
89 * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
90 * the name of the SR_OK code is "SR_OK", and so on.
92 * This function can be used for various purposes where the "name" string of
93 * a libsigrok error code is useful.
95 * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
97 * @return A const string containing the "name" of the error code as string.
98 * The string must NOT be free'd by the caller!
104 SR_API const char *sr_strerror_name(int error_code)
107 * Note: All defined SR_* error macros from libsigrok.h must have
108 * an entry in this function, as well as in sr_strerror().
111 switch (error_code) {
117 return "SR_ERR_MALLOC";
122 case SR_ERR_SAMPLERATE:
123 return "SR_ERR_SAMPLERATE";
126 case SR_ERR_DEV_CLOSED:
127 return "SR_ERR_DEV_CLOSED";
129 return "SR_ERR_TIMEOUT";
130 case SR_ERR_PROBE_GROUP:
131 return "SR_PROBE_GROUP";
133 return "unknown error code";