libsigrok/error.c

134 lines
3.4 KiB
C
Raw Normal View History

/*
2013-04-23 20:24:30 +00:00
* This file is part of the libsigrok project.
*
* 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"
/**
* @file
*
* Error handling in libsigrok.
*/
/**
* @defgroup grp_error Error handling
*
* Error handling in libsigrok.
*
* libsigrok functions usually return @ref SR_OK upon success, or a negative
* error code on failure.
*
* @{
*/
/**
* 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.
*
* @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!
*
* @see sr_strerror_name
*
* @since 0.2.0
*/
SR_API const char *sr_strerror(int error_code)
{
/*
2013-04-27 08:49:52 +00:00
* Note: All defined SR_* error macros from libsigrok.h must have
* an entry in this function, as well as in sr_strerror_name().
*/
switch (error_code) {
case SR_OK:
2013-06-22 15:12:38 +00:00
return "no error";
case SR_ERR:
2013-06-22 15:12:38 +00:00
return "generic/unspecified error";
case SR_ERR_MALLOC:
2013-06-22 15:12:38 +00:00
return "memory allocation error";
case SR_ERR_ARG:
2013-06-22 15:12:38 +00:00
return "invalid argument";
case SR_ERR_BUG:
2013-06-22 15:12:38 +00:00
return "internal error";
case SR_ERR_SAMPLERATE:
2013-06-22 15:12:38 +00:00
return "invalid samplerate";
case SR_ERR_NA:
2013-06-22 15:12:38 +00:00
return "not applicable";
2013-04-27 08:49:52 +00:00
case SR_ERR_DEV_CLOSED:
2013-06-22 15:12:38 +00:00
return "device closed but should be open";
2013-06-13 12:23:06 +00:00
case SR_ERR_TIMEOUT:
2013-06-22 15:12:38 +00:00
return "timeout occurred";
default:
2013-06-22 15:12:38 +00:00
return "unknown error";
}
}
/**
* 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.
*
* @return A const string containing the "name" of the error code as string.
* The string must NOT be free'd by the caller!
*
* @see sr_strerror
*
* @since 0.2.0
*/
SR_API const char *sr_strerror_name(int error_code)
{
/*
2013-04-27 08:49:52 +00:00
* Note: All defined SR_* error macros from libsigrok.h must have
* an entry in this function, as well as in sr_strerror().
*/
switch (error_code) {
case SR_OK:
2013-06-22 15:12:38 +00:00
return "SR_OK";
case SR_ERR:
2013-06-22 15:12:38 +00:00
return "SR_ERR";
case SR_ERR_MALLOC:
2013-06-22 15:12:38 +00:00
return "SR_ERR_MALLOC";
case SR_ERR_ARG:
2013-06-22 15:12:38 +00:00
return "SR_ERR_ARG";
case SR_ERR_BUG:
2013-06-22 15:12:38 +00:00
return "SR_ERR_BUG";
case SR_ERR_SAMPLERATE:
2013-06-22 15:12:38 +00:00
return "SR_ERR_SAMPLERATE";
case SR_ERR_NA:
2013-06-22 15:12:38 +00:00
return "SR_ERR_NA";
2013-04-27 08:49:52 +00:00
case SR_ERR_DEV_CLOSED:
2013-06-22 15:12:38 +00:00
return "SR_ERR_DEV_CLOSED";
2013-06-13 12:23:06 +00:00
case SR_ERR_TIMEOUT:
2013-06-22 15:12:38 +00:00
return "SR_ERR_TIMEOUT";
default:
2013-06-22 15:12:38 +00:00
return "unknown error code";
}
}
/** @} */