Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame] | 1 | /* $Id$ |
| 2 | * |
| 3 | */ |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 4 | #ifndef __PJ_ERRNO_H__ |
| 5 | #define __PJ_ERRNO_H__ |
| 6 | |
| 7 | /** |
| 8 | * @file errno.h |
| 9 | * @brief PJLIB Error Codes |
| 10 | */ |
| 11 | #include <pj/types.h> |
| 12 | #include <pj/compat/errno.h> |
| 13 | |
| 14 | PJ_BEGIN_DECL |
| 15 | |
| 16 | /** |
| 17 | * @defgroup pj_errno Error Codes |
| 18 | * @ingroup PJ |
| 19 | * @{ |
| 20 | * |
| 21 | * In PJLIB, error/status codes from operating system are translated |
| 22 | * into PJLIB error namespace, and stored in @a pj_status_t. All functions |
| 23 | * that work with @a pj_status_t expect to get PJLIB error code instead |
| 24 | * of native codes. |
| 25 | * |
| 26 | * @section pj_errno_retval Return Values |
| 27 | * |
| 28 | * All functions that returns @a pj_status_t returns @a PJ_SUCCESS if the |
| 29 | * operation was completed successfully, or non-zero value to indicate |
| 30 | * error. If the error came from operating system, then the native error |
| 31 | * code is translated/folded into PJLIB's error namespace by using |
| 32 | * #PJ_STATUS_FROM_OS() macro. The function will do this automatically |
| 33 | * before returning the error to caller. |
| 34 | * |
| 35 | * @section pj_errno_errmsg Error Message |
| 36 | * |
| 37 | * To get the error message corresponding to a particular code, use function |
| 38 | * #pj_strerror(). This function expects error code in PJLIB error namespace, |
| 39 | * not the native error code. Application can pass the value from the |
| 40 | * following sources to this function: |
| 41 | * - #pj_get_os_error() |
| 42 | * - #pj_get_netos_error() |
| 43 | * - any return value from function returning @a pj_status_t. |
| 44 | * |
| 45 | * Application MUST NOT pass native error code (such as error code from |
| 46 | * functions like GetLastError() or errno) to PJLIB functions expecting |
| 47 | * @a pj_status_t. |
| 48 | * |
| 49 | */ |
| 50 | |
| 51 | /** |
| 52 | * Get the last platform error/status, folded into pj_status_t. |
| 53 | * @return OS dependent error code, folded into pj_status_t. |
| 54 | * @remark This function gets errno, or calls GetLastError() function and |
| 55 | * convert the code into pj_status_t with PJ_STATUS_FROM_OS. Do |
| 56 | * not call this for socket functions! |
| 57 | * @see pj_get_netos_error() |
| 58 | */ |
| 59 | PJ_DECL(pj_status_t) pj_get_os_error(void); |
| 60 | |
| 61 | /** |
| 62 | * Set last error. |
| 63 | * @param code pj_status_t |
| 64 | */ |
| 65 | PJ_DECL(void) pj_set_os_error(pj_status_t code); |
| 66 | |
| 67 | /** |
| 68 | * Get the last error from socket operations. |
| 69 | * @return Last socket error, folded into pj_status_t. |
| 70 | */ |
| 71 | PJ_DECL(pj_status_t) pj_get_netos_error(void); |
| 72 | |
| 73 | /** |
| 74 | * Set error code. |
| 75 | * @param code pj_status_t. |
| 76 | */ |
| 77 | PJ_DECL(void) pj_set_netos_error(pj_status_t code); |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Get the error message for the specified error code. The message |
| 82 | * string will be NULL terminated. |
| 83 | * |
| 84 | * @param statcode The error code. |
| 85 | * @param buf Buffer to hold the error message string. |
| 86 | * @param bufsize Size of the buffer. |
| 87 | * |
| 88 | * @return The error message as NULL terminated string, |
| 89 | * wrapped with pj_str_t. |
| 90 | */ |
| 91 | PJ_DECL(pj_str_t) pj_strerror( pj_status_t statcode, |
| 92 | char *buf, pj_size_t bufsize); |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * @hideinitializer |
| 97 | * Return platform os error code folded into pj_status_t code. This is |
| 98 | * the macro that is used throughout the library for all PJLIB's functions |
| 99 | * that returns error from operating system. Application may override |
| 100 | * this macro to reduce size (e.g. by defining it to always return |
| 101 | * #PJ_EUNKNOWN). |
| 102 | * |
| 103 | * Note: |
| 104 | * This macro MUST return non-zero value regardless whether zero is |
| 105 | * passed as the argument. The reason is to protect logic error when |
| 106 | * the operating system doesn't report error codes properly. |
| 107 | * |
| 108 | * @param os_code Platform OS error code. This value may be evaluated |
| 109 | * more than once. |
| 110 | * @return The platform os error code folded into pj_status_t. |
| 111 | */ |
| 112 | #ifndef PJ_RETURN_OS_ERROR |
| 113 | # define PJ_RETURN_OS_ERROR(os_code) (os_code ? \ |
| 114 | PJ_STATUS_FROM_OS(os_code) : -1) |
| 115 | #endif |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * @hideinitializer |
| 120 | * Fold a platform specific error into an pj_status_t code. |
| 121 | * |
| 122 | * @param e The platform os error code. |
| 123 | * @return pj_status_t |
| 124 | * @warning Macro implementation; the syserr argument may be evaluated |
| 125 | * multiple times. |
| 126 | */ |
| 127 | #define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : e + PJ_ERRNO_START_SYS) |
| 128 | |
| 129 | /** |
| 130 | * @hideinitializer |
| 131 | * Fold an pj_status_t code back to the native platform defined error. |
| 132 | * |
| 133 | * @param e The pj_status_t folded platform os error code. |
| 134 | * @return pj_os_err_type |
| 135 | * @warning macro implementation; the statcode argument may be evaluated |
| 136 | * multiple times. If the statcode was not created by |
| 137 | * pj_get_os_error or PJ_STATUS_FROM_OS, the results are undefined. |
| 138 | */ |
| 139 | #define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : e - PJ_ERRNO_START_SYS) |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * @defgroup pj_errnum PJLIB's Own Error Codes |
| 144 | * @ingroup pj_errno |
| 145 | * @{ |
| 146 | */ |
| 147 | |
| 148 | /** |
| 149 | * @hideinitializer |
| 150 | * Unknown error has been reported. |
| 151 | */ |
| 152 | #define PJ_EUNKNOWN (PJ_ERRNO_START_STATUS + 1) |
| 153 | /** |
| 154 | * @hideinitializer |
| 155 | * The operation is pending and will be completed later. |
| 156 | */ |
| 157 | #define PJ_EPENDING (PJ_ERRNO_START_STATUS + 2) |
| 158 | /** |
| 159 | * @hideinitializer |
| 160 | * Too many connecting sockets. |
| 161 | */ |
| 162 | #define PJ_ETOOMANYCONN (PJ_ERRNO_START_STATUS + 3) |
| 163 | /** |
| 164 | * @hideinitializer |
| 165 | * Invalid argument. |
| 166 | */ |
| 167 | #define PJ_EINVAL (PJ_ERRNO_START_STATUS + 4) |
| 168 | /** |
| 169 | * @hideinitializer |
| 170 | * Name too long (eg. hostname too long). |
| 171 | */ |
| 172 | #define PJ_ENAMETOOLONG (PJ_ERRNO_START_STATUS + 5) |
| 173 | /** |
| 174 | * @hideinitializer |
| 175 | * Not found. |
| 176 | */ |
| 177 | #define PJ_ENOTFOUND (PJ_ERRNO_START_STATUS + 6) |
| 178 | /** |
| 179 | * @hideinitializer |
| 180 | * Not enough memory. |
| 181 | */ |
| 182 | #define PJ_ENOMEM (PJ_ERRNO_START_STATUS + 7) |
| 183 | /** |
| 184 | * @hideinitializer |
| 185 | * Bug detected! |
| 186 | */ |
| 187 | #define PJ_EBUG (PJ_ERRNO_START_STATUS + 8) |
| 188 | /** |
| 189 | * @hideinitializer |
| 190 | * Operation timed out. |
| 191 | */ |
| 192 | #define PJ_ETIMEDOUT (PJ_ERRNO_START_STATUS + 9) |
| 193 | /** |
| 194 | * @hideinitializer |
| 195 | * Too many objects. |
| 196 | */ |
| 197 | #define PJ_ETOOMANY (PJ_ERRNO_START_STATUS + 10) |
| 198 | /** |
| 199 | * @hideinitializer |
| 200 | * Object is busy. |
| 201 | */ |
| 202 | #define PJ_EBUSY (PJ_ERRNO_START_STATUS + 11) |
| 203 | /** |
| 204 | * @hideinitializer |
| 205 | * The specified option is not supported. |
| 206 | */ |
| 207 | #define PJ_ENOTSUP (PJ_ERRNO_START_STATUS + 12) |
| 208 | /** |
| 209 | * @hideinitializer |
| 210 | * Invalid operation. |
| 211 | */ |
| 212 | #define PJ_EINVALIDOP (PJ_ERRNO_START_STATUS + 13) |
Benny Prijono | 85d3f45 | 2005-11-09 15:37:19 +0000 | [diff] [blame] | 213 | /**
|
| 214 | * @hideinitializer
|
| 215 | * Operation is cancelled.
|
| 216 | */
|
| 217 | #define PJ_ECANCELLED (PJ_ERRNO_START_STATUS + 14)
|
Benny Prijono | f010e69 | 2005-11-11 19:01:31 +0000 | [diff] [blame^] | 218 | /**
|
| 219 | * @hideinitializer
|
| 220 | * Object already exists.
|
| 221 | */
|
| 222 | #define PJ_EEXISTS (PJ_ERRNO_START_STATUS + 14)
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 223 | |
| 224 | /** @} */ /* pj_errnum */ |
| 225 | |
| 226 | /** @} */ /* pj_errno */ |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * PJ_ERRNO_START is where PJLIB specific error values start. |
| 231 | */ |
| 232 | #define PJ_ERRNO_START 20000 |
| 233 | |
| 234 | /** |
| 235 | * PJ_ERRNO_SPACE_SIZE is the maximum number of errors in one of |
| 236 | * the error/status range below. |
| 237 | */ |
| 238 | #define PJ_ERRNO_SPACE_SIZE 50000 |
| 239 | |
| 240 | /** |
| 241 | * PJ_ERRNO_START_STATUS is where PJLIB specific status codes start. |
| 242 | */ |
| 243 | #define PJ_ERRNO_START_STATUS (PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE) |
| 244 | |
| 245 | /** |
| 246 | * PJ_ERRNO_START_SYS converts platform specific error codes into |
| 247 | * pj_status_t values. |
| 248 | */ |
| 249 | #define PJ_ERRNO_START_SYS (PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE) |
| 250 | |
| 251 | /** |
| 252 | * PJ_ERRNO_START_USER are reserved for applications that use error |
| 253 | * codes along with PJLIB codes. |
| 254 | */ |
| 255 | #define PJ_ERRNO_START_USER (PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE) |
| 256 | |
| 257 | |
| 258 | PJ_END_DECL |
| 259 | |
| 260 | #endif /* __PJ_ERRNO_H__ */ |
| 261 | |