2012-07-12 19:30:49 +00:00
|
|
|
/*
|
2013-04-23 20:24:30 +00:00
|
|
|
* This file is part of the libsigrok project.
|
2012-07-12 19:30:49 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libsigrok.h"
|
|
|
|
|
2012-10-21 22:30:12 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* Error handling in libsigrok.
|
|
|
|
*/
|
|
|
|
|
2012-10-21 14:13:36 +00:00
|
|
|
/**
|
|
|
|
* @defgroup grp_error Error handling
|
|
|
|
*
|
|
|
|
* Error handling in libsigrok.
|
|
|
|
*
|
2012-10-21 14:36:23 +00:00
|
|
|
* libsigrok functions usually return @ref SR_OK upon success, or a negative
|
|
|
|
* error code on failure.
|
|
|
|
*
|
2012-10-21 14:13:36 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-07-12 19:30:49 +00:00
|
|
|
/**
|
|
|
|
* Return a human-readable error string for the given libsigrok error code.
|
|
|
|
*
|
|
|
|
* @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
2012-07-12 19:30:49 +00:00
|
|
|
* @return A const string containing a short, human-readable (English)
|
|
|
|
* description of the error, such as "memory allocation error".
|
|
|
|
* The string must NOT be free'd by the caller!
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
2012-07-12 19:30:49 +00:00
|
|
|
* @see sr_strerror_name
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
|
|
|
* @since 0.2.0
|
2012-07-12 19:30:49 +00:00
|
|
|
*/
|
|
|
|
SR_API const char *sr_strerror(int error_code)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
/*
|
2013-04-27 08:49:52 +00:00
|
|
|
* Note: All defined SR_* error macros from libsigrok.h must have
|
2012-07-12 19:30:49 +00:00
|
|
|
* an entry in this function, as well as in sr_strerror_name().
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (error_code) {
|
|
|
|
case SR_OK:
|
|
|
|
str = "no error";
|
|
|
|
break;
|
|
|
|
case SR_ERR:
|
|
|
|
str = "generic/unspecified error";
|
|
|
|
break;
|
|
|
|
case SR_ERR_MALLOC:
|
|
|
|
str = "memory allocation error";
|
|
|
|
break;
|
2013-04-16 10:54:11 +00:00
|
|
|
case SR_ERR_ARG:
|
|
|
|
str = "invalid argument";
|
|
|
|
break;
|
2012-07-12 19:30:49 +00:00
|
|
|
case SR_ERR_BUG:
|
|
|
|
str = "internal error";
|
|
|
|
break;
|
|
|
|
case SR_ERR_SAMPLERATE:
|
|
|
|
str = "invalid samplerate";
|
|
|
|
break;
|
2013-04-16 10:54:11 +00:00
|
|
|
case SR_ERR_NA:
|
|
|
|
str = "not applicable";
|
|
|
|
break;
|
2013-04-27 08:49:52 +00:00
|
|
|
case SR_ERR_DEV_CLOSED:
|
|
|
|
str = "device closed but should be open";
|
|
|
|
break;
|
2012-07-12 19:30:49 +00:00
|
|
|
default:
|
|
|
|
str = "unknown error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the "name" string of the given libsigrok error code.
|
|
|
|
*
|
|
|
|
* For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
|
|
|
|
* the name of the SR_OK code is "SR_OK", and so on.
|
|
|
|
*
|
|
|
|
* This function can be used for various purposes where the "name" string of
|
|
|
|
* a libsigrok error code is useful.
|
|
|
|
*
|
|
|
|
* @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
2012-07-12 19:30:49 +00:00
|
|
|
* @return A const string containing the "name" of the error code as string.
|
|
|
|
* The string must NOT be free'd by the caller!
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
2012-07-12 19:30:49 +00:00
|
|
|
* @see sr_strerror
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
|
|
|
* @since 0.2.0
|
2012-07-12 19:30:49 +00:00
|
|
|
*/
|
|
|
|
SR_API const char *sr_strerror_name(int error_code)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
/*
|
2013-04-27 08:49:52 +00:00
|
|
|
* Note: All defined SR_* error macros from libsigrok.h must have
|
2012-07-12 19:30:49 +00:00
|
|
|
* an entry in this function, as well as in sr_strerror().
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (error_code) {
|
|
|
|
case SR_OK:
|
|
|
|
str = "SR_OK";
|
|
|
|
break;
|
|
|
|
case SR_ERR:
|
|
|
|
str = "SR_ERR";
|
|
|
|
break;
|
|
|
|
case SR_ERR_MALLOC:
|
|
|
|
str = "SR_ERR_MALLOC";
|
|
|
|
break;
|
2013-04-16 10:54:11 +00:00
|
|
|
case SR_ERR_ARG:
|
|
|
|
str = "SR_ERR_ARG";
|
|
|
|
break;
|
2012-07-12 19:30:49 +00:00
|
|
|
case SR_ERR_BUG:
|
|
|
|
str = "SR_ERR_BUG";
|
|
|
|
break;
|
|
|
|
case SR_ERR_SAMPLERATE:
|
|
|
|
str = "SR_ERR_SAMPLERATE";
|
|
|
|
break;
|
2013-04-16 10:54:11 +00:00
|
|
|
case SR_ERR_NA:
|
|
|
|
str = "SR_ERR_NA";
|
|
|
|
break;
|
2013-04-27 08:49:52 +00:00
|
|
|
case SR_ERR_DEV_CLOSED:
|
|
|
|
str = "SR_ERR_DEV_CLOSED";
|
|
|
|
break;
|
2012-07-12 19:30:49 +00:00
|
|
|
default:
|
|
|
|
str = "unknown error code";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2012-10-21 14:13:36 +00:00
|
|
|
|
|
|
|
/** @} */
|