libsigrok  0.3.0
sigrok hardware access and backend library
error.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the libsigrok project.
00003  *
00004  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #include "libsigrok.h"
00022 
00023 /**
00024  * @file
00025  *
00026  * Error handling in libsigrok.
00027  */
00028 
00029 /**
00030  * @defgroup grp_error Error handling
00031  *
00032  * Error handling in libsigrok.
00033  *
00034  * libsigrok functions usually return @ref SR_OK upon success, or a negative
00035  * error code on failure.
00036  *
00037  * @{
00038  */
00039 
00040 /**
00041  * Return a human-readable error string for the given libsigrok error code.
00042  *
00043  * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
00044  *
00045  * @return A const string containing a short, human-readable (English)
00046  *         description of the error, such as "memory allocation error".
00047  *         The string must NOT be free'd by the caller!
00048  *
00049  * @see sr_strerror_name
00050  *
00051  * @since 0.2.0
00052  */
00053 SR_API const char *sr_strerror(int error_code)
00054 {
00055         /*
00056          * Note: All defined SR_* error macros from libsigrok.h must have
00057          * an entry in this function, as well as in sr_strerror_name().
00058          */
00059 
00060         switch (error_code) {
00061         case SR_OK:
00062                 return "no error";
00063         case SR_ERR:
00064                 return "generic/unspecified error";
00065         case SR_ERR_MALLOC:
00066                 return "memory allocation error";
00067         case SR_ERR_ARG:
00068                 return "invalid argument";
00069         case SR_ERR_BUG:
00070                 return "internal error";
00071         case SR_ERR_SAMPLERATE:
00072                 return "invalid samplerate";
00073         case SR_ERR_NA:
00074                 return "not applicable";
00075         case SR_ERR_DEV_CLOSED:
00076                 return "device closed but should be open";
00077         case SR_ERR_TIMEOUT:
00078                 return "timeout occurred";
00079         case SR_ERR_CHANNEL_GROUP:
00080                 return "no channel group specified";
00081         default:
00082                 return "unknown error";
00083         }
00084 }
00085 
00086 /**
00087  * Return the "name" string of the given libsigrok error code.
00088  *
00089  * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
00090  * the name of the SR_OK code is "SR_OK", and so on.
00091  *
00092  * This function can be used for various purposes where the "name" string of
00093  * a libsigrok error code is useful.
00094  *
00095  * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
00096  *
00097  * @return A const string containing the "name" of the error code as string.
00098  *         The string must NOT be free'd by the caller!
00099  *
00100  * @see sr_strerror
00101  *
00102  * @since 0.2.0
00103  */
00104 SR_API const char *sr_strerror_name(int error_code)
00105 {
00106         /*
00107          * Note: All defined SR_* error macros from libsigrok.h must have
00108          * an entry in this function, as well as in sr_strerror().
00109          */
00110 
00111         switch (error_code) {
00112         case SR_OK:
00113                 return "SR_OK";
00114         case SR_ERR:
00115                 return "SR_ERR";
00116         case SR_ERR_MALLOC:
00117                 return "SR_ERR_MALLOC";
00118         case SR_ERR_ARG:
00119                 return "SR_ERR_ARG";
00120         case SR_ERR_BUG:
00121                 return "SR_ERR_BUG";
00122         case SR_ERR_SAMPLERATE:
00123                 return "SR_ERR_SAMPLERATE";
00124         case SR_ERR_NA:
00125                 return "SR_ERR_NA";
00126         case SR_ERR_DEV_CLOSED:
00127                 return "SR_ERR_DEV_CLOSED";
00128         case SR_ERR_TIMEOUT:
00129                 return "SR_ERR_TIMEOUT";
00130         case SR_ERR_CHANNEL_GROUP:
00131                 return "SR_ERR_CHANNEL_GROUP";
00132         default:
00133                 return "unknown error code";
00134         }
00135 }
00136 
00137 /** @} */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines