error.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * This file is part of the libsigrok project.
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <libsigrok/libsigrok.h>
  21. /**
  22. * @file
  23. *
  24. * Error handling in libsigrok.
  25. */
  26. /**
  27. * @defgroup grp_error Error handling
  28. *
  29. * Error handling in libsigrok.
  30. *
  31. * libsigrok functions usually return @ref SR_OK upon success, or a negative
  32. * error code on failure.
  33. *
  34. * @{
  35. */
  36. /**
  37. * Return a human-readable error string for the given libsigrok error code.
  38. *
  39. * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
  40. *
  41. * @return A const string containing a short, human-readable (English)
  42. * description of the error, such as "memory allocation error".
  43. * The string must NOT be free'd by the caller!
  44. *
  45. * @see sr_strerror_name
  46. *
  47. * @since 0.2.0
  48. */
  49. SR_API const char *sr_strerror(int error_code)
  50. {
  51. /*
  52. * Note: All defined SR_* error macros from libsigrok.h must have
  53. * an entry in this function, as well as in sr_strerror_name().
  54. */
  55. switch (error_code) {
  56. case SR_OK:
  57. return "no error";
  58. case SR_ERR:
  59. return "generic/unspecified error";
  60. case SR_ERR_MALLOC:
  61. return "memory allocation error";
  62. case SR_ERR_ARG:
  63. return "invalid argument";
  64. case SR_ERR_BUG:
  65. return "internal error";
  66. case SR_ERR_SAMPLERATE:
  67. return "invalid samplerate";
  68. case SR_ERR_NA:
  69. return "not applicable";
  70. case SR_ERR_DEV_CLOSED:
  71. return "device closed but should be open";
  72. case SR_ERR_TIMEOUT:
  73. return "timeout occurred";
  74. case SR_ERR_CHANNEL_GROUP:
  75. return "no channel group specified";
  76. case SR_ERR_DATA:
  77. return "data is invalid";
  78. case SR_ERR_IO:
  79. return "input/output error";
  80. default:
  81. return "unknown error";
  82. }
  83. }
  84. /**
  85. * Return the "name" string of the given libsigrok error code.
  86. *
  87. * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
  88. * the name of the SR_OK code is "SR_OK", and so on.
  89. *
  90. * This function can be used for various purposes where the "name" string of
  91. * a libsigrok error code is useful.
  92. *
  93. * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
  94. *
  95. * @return A const string containing the "name" of the error code as string.
  96. * The string must NOT be free'd by the caller!
  97. *
  98. * @see sr_strerror
  99. *
  100. * @since 0.2.0
  101. */
  102. SR_API const char *sr_strerror_name(int error_code)
  103. {
  104. /*
  105. * Note: All defined SR_* error macros from libsigrok.h must have
  106. * an entry in this function, as well as in sr_strerror().
  107. */
  108. switch (error_code) {
  109. case SR_OK:
  110. return "SR_OK";
  111. case SR_ERR:
  112. return "SR_ERR";
  113. case SR_ERR_MALLOC:
  114. return "SR_ERR_MALLOC";
  115. case SR_ERR_ARG:
  116. return "SR_ERR_ARG";
  117. case SR_ERR_BUG:
  118. return "SR_ERR_BUG";
  119. case SR_ERR_SAMPLERATE:
  120. return "SR_ERR_SAMPLERATE";
  121. case SR_ERR_NA:
  122. return "SR_ERR_NA";
  123. case SR_ERR_DEV_CLOSED:
  124. return "SR_ERR_DEV_CLOSED";
  125. case SR_ERR_TIMEOUT:
  126. return "SR_ERR_TIMEOUT";
  127. case SR_ERR_CHANNEL_GROUP:
  128. return "SR_ERR_CHANNEL_GROUP";
  129. case SR_ERR_DATA:
  130. return "SR_ERR_DATA";
  131. case SR_ERR_IO:
  132. return "SR_ERR_IO";
  133. default:
  134. return "unknown error code";
  135. }
  136. }
  137. /** @} */