diff options
Diffstat (limited to 'mbglib/common/mbgerror.c')
-rw-r--r-- | mbglib/common/mbgerror.c | 413 |
1 files changed, 413 insertions, 0 deletions
diff --git a/mbglib/common/mbgerror.c b/mbglib/common/mbgerror.c new file mode 100644 index 0000000..3e8b121 --- /dev/null +++ b/mbglib/common/mbgerror.c @@ -0,0 +1,413 @@ + +/************************************************************************** + * + * $Id: mbgerror.c 1.1.1.10 2015/05/20 12:34:03 martin TEST $ + * + * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany + * + * Description: + * Meinberg Library to communicate with USB devices from user space + * + * ----------------------------------------------------------------------- + * $Log: mbgerror.c $ + * Revision 1.1.1.10 2015/05/20 12:34:03 martin + * Revision 1.1.1.9 2014/10/28 15:19:17 martin + * Revision 1.1.1.8 2014/05/27 12:44:33Z martin + * Revision 1.1.1.7 2014/05/09 11:39:17 marvin + * Added access denied error to win32_to_mbg error handling. + * Revision 1.1.1.6 2014/03/18 11:25:54Z gregoire + * Fixed build errors fopr CVI, but this needs + * to be tested / improved. + * Revision 1.1.1.5 2014/03/12 09:01:56Z martin + * Revision 1.1.1.4 2014/03/11 14:12:29 martin + * Revision 1.1.1.3 2014/03/11 14:08:55Z martin + * Revision 1.1.1.2 2014/03/10 16:57:15Z martin + * Revision 1.1.1.1 2014/03/07 13:20:26 martin + * Tmp. saved changes. + * Revision 1.1 2014/03/07 12:08:14 martin + * Initial revision. + * +**************************************************************************/ + +#define _MBGERROR + #include <mbgerror.h> +#undef _MBGERROR + +#include <mbg_tgt.h> + +#include <stdio.h> +#include <string.h> + +#if defined( MBG_TGT_POSIX ) + #include <errno.h> + #include <netdb.h> +#endif + + + +#if defined( MBG_TGT_WIN32 ) + +/*HDR*/ +/** + * @brief Translate a Windows non-socket API error code to one of the @ref MBG_ERROR_CODES + * + * @param last_err A Windows non-socket API error code as returned by GetLastError() + * @param info An optional informational text string, or NULL + * + * @return One of the @ref MBG_ERROR_CODES + */ +int mbg_win32_last_err_to_mbg( DWORD last_err, const char *info ) +{ + #if DEBUG + if ( info ) + fprintf( stderr, "%s, wsa_err: 0x%08lX\n", info, (long) last_err ); + #endif + + //##++++++++++++++++ TODO check codes + switch ( last_err ) // codes usually defined in winerror.h + { + case ERROR_SUCCESS: return MBG_SUCCESS; + case ERROR_ACCESS_DENIED: return MBG_ERR_ACCESS; + + case ERROR_PRIVILEGE_NOT_HELD: return MBG_ERR_PERM; + + #if 0 + case ERROR_INVALID_HANDLE: + + case ERROR_NOT_ENOUGH_MEMORY: + case ERROR_OUTOFMEMORY: + + case ERROR_WRITE_PROTECT: + case ERROR_BAD_UNIT: + case ERROR_NOT_READY: + case ERROR_WRITE_FAULT: + case ERROR_READ_FAULT: + case ERROR_GEN_FAILURE: + case ERROR_SHARING_VIOLATION: + case ERROR_LOCK_VIOLATION: + case ERROR_NOT_SUPPORTED: + + case ERROR_DUP_NAME: + case ERROR_BAD_DEV_TYPE: + case ERROR_BUFFER_OVERFLOW: + + case ERROR_BUSY: + case ERROR_NOACCESS: + #endif + } + + return MBG_ERR_UNSPEC; + +} // mbg_win32_last_err_to_mbg + + + +/*HDR*/ +/** + * @brief Translate a Windows socket API error code to one of the @ref MBG_ERROR_CODES + * + * @param wsa_err A Windows socket API error code as returned by WSAGetLastError() + * @param info An optional informational text string, or NULL + * + * @return One of the @ref MBG_ERROR_CODES + */ +int mbg_win32_wsa_err_to_mbg( DWORD wsa_err, const char *info ) +{ + #if DEBUG + if ( info ) + fprintf( stderr, "%s, wsa_err: 0x%08lX\n", info, (long) wsa_err ); + #endif + + //##++++++++++++++++ TODO check codes + switch ( wsa_err ) + { + case WSAENOTSOCK: + return MBG_ERR_NOT_A_SOCKET; + + case WSANOTINITIALISED: + return MBG_ERR_SOCK_INIT; + + case WSAEFAULT: + case WSAENETDOWN: + case WSAEINVAL: + case WSAEINTR: + case WSAEINPROGRESS: + return MBG_ERR_SOCK_INIT; + + case WSAEAFNOSUPPORT: + case WSAEMFILE: + case WSAEINVALIDPROVIDER: + case WSAEINVALIDPROCTABLE: + case WSAENOBUFS: + case WSAEPROTONOSUPPORT: + case WSAEPROTOTYPE: + case WSAEPROVIDERFAILEDINIT: + case WSAESOCKTNOSUPPORT: + return MBG_ERR_INV_SOCK_FD; //##++++++++++++++++ + +#if 0 + case WSAEALREADY: + case WSAEISCONN: + case WSAENETUNREACH: + return MBG_ERR_SOCKET_CFG; //##+++++ + break; +#endif + } + + return MBG_ERR_UNSPEC; + +} // mbg_win32_wsa_err_to_mbg + +#endif // defined( MBG_TGT_WIN32 ) + + + +#if defined( MBG_TGT_POSIX ) + +/*HDR*/ +/** + * @brief Translate a POSIX errno error code to one of the @ref MBG_ERROR_CODES + * + * @param posix_errno A POSIX error code as usually defined in errno.h + * @param info An optional informational text string, or NULL + * + * @return One of the @ref MBG_ERROR_CODES + */ +int mbg_posix_errno_to_mbg( int posix_errno, const char *info ) +{ + #if DEBUG + if ( info ) + fprintf( stderr, "%s: %s (errno: %i)\n", info, + strerror( posix_errno ), posix_errno ); + #endif + + switch ( posix_errno ) + { + case EPERM: return MBG_ERR_PERM; + case EINTR: return MBG_ERR_INTR; + case EIO: return MBG_ERR_IO; + case ENXIO: return MBG_ERR_NOT_FOUND; + case ENOMEM: return MBG_ERR_NO_MEM; + case EACCES: return MBG_ERR_ACCESS; + case EBUSY: return MBG_ERR_BUSY; + case ENODEV: return MBG_ERR_NO_DEV; + case EINVAL: return MBG_ERR_INV_PARM; + case EPIPE: return MBG_ERR_PIPE; + case EOVERFLOW: return MBG_ERR_OVERFLOW; + case ENOTSOCK: return MBG_ERR_NOT_A_SOCKET; + default: break; + } + + return MBG_ERR_UNSPEC; + +} // mbg_posix_errno_to_mbg + + +/*HDR*/ +/** + * @brief Translate a POSIX h_errno error code to one of the @ref MBG_ERROR_CODES + * + * This function is specific to translate error codes returned by + * gethostbyname() and gethostbyaddr(). In case of error these functions + * don't set errno but h_errno to a specific value. + * + * The functions gethostbyname() and gethostbyaddr() are obsolete, + * and getaddressinfo() should be used preferably. + * + * @param posix_h_errno An error code as usually defined in netdb.h + * @param info An optional informational text string, or NULL + * + * @return One of the @ref MBG_ERROR_CODES + */ +int mbg_posix_h_errno_to_mbg( int posix_h_errno, const char *info ) +{ + #if DEBUG + if ( info ) + fprintf( stderr, "%s: %s (h_errno: %i)\n", info, + hstrerror( posix_h_errno ), posix_h_errno ); + #endif + + switch ( posix_h_errno ) + { + case HOST_NOT_FOUND: // The specified host is unknown + + // case NO_ADDRESS: // Usually same numeric value as NO_DATA + case NO_DATA: // The requested name is valid but does not have an IP address + + case NO_RECOVERY: // A nonrecoverable name server error occurred + + case TRY_AGAIN: // A temporary error occurred on an authoritative name server. Try again later. + return MBG_ERR_UNSPEC; //##++++++++++++++++++++++++ + } + + return MBG_ERR_UNSPEC; + +} // mbg_posix_h_errno_to_mbg + +#endif // defined( MBG_TGT_POSIX ) + + + +/*HDR*/ +/** + * @brief Get and translate last error after non-socket function call + * + * Retrieve the "last error" code after a non-socket function has been called + * and translate to one of the @ref MBG_ERROR_CODES. + * + * On POSIX systems the "last error" code is always stored in errno, but + * e.g. under Windows the "last error" code after a socket function + * has to be retrieved by calling WSAGetLastError(), whereas the "last error" + * code from non-socket POSIX-like functions has to be retrieved + * by calling GetLastError(). + * + * @param info An optional informational text string, or NULL + * + * @return One of the @ref MBG_ERROR_CODES + */ +int mbg_get_last_error( const char *info ) +{ + #if defined( MBG_TGT_WIN32 ) + + // Under Windows the "last error" code after a non-socket function + // has to be retrieved by calling GetLastError(), whereas + // the "last error" code from POSIX-like socket functions + // is retrieved by calling WSAGetLastError(). + return mbg_win32_wsa_err_to_mbg( GetLastError(), info ); + + #elif defined( MBG_TGT_POSIX ) + + // On POSIX systems the "last error" code is always stored in errno. + return mbg_posix_errno_to_mbg( errno, info ); + + #else + + #error This function is not supported for this target. + + #endif + +} // mbg_get_last_error + + + +/*HDR*/ +/** + * @brief Get and translate last error after socket function call + * + * Retrieve the "last error" code after a socket function has been called + * and translate to one of the @ref MBG_ERROR_CODES. + * + * On POSIX systems the "last error" code is always stored in errno, but + * e.g. under Windows the "last error" code after a socket function + * has to be retrieved by calling WSAGetLastError, whereas the "last error" + * code from non-socket POSIX-like functions is stored in errno as usual. + * + * @param info An optional informational text string, or NULL + * + * @return One of the @ref MBG_ERROR_CODES + */ +int mbg_get_last_socket_error( const char *info ) +{ + #if defined( MBG_TGT_CVI ) + + #warning This needs to be implemented for CVI + return MBG_ERR_UNSPEC; + + #elif defined( MBG_TGT_WIN32 ) + + // Under Windows the "last error" code after a socket function + // has to be retrieved by calling WSAGetLastError, whereas + // the "last error" code from non-socket POSIX-like functions + // is stored in errno as usual. + return mbg_win32_wsa_err_to_mbg( WSAGetLastError(), info ); + + #elif defined( MBG_TGT_POSIX ) + + // On POSIX systems the "last error" code is always stored in errno. + return mbg_posix_errno_to_mbg( errno, info ); + + #else + + #error This function is not supported for this target. + + #endif + +} // mbg_get_last_socket_error + + + +/*HDR*/ +/** + * @brief Retrieve and convert last error after gethostbyname() + * + * This function is specific to retrieve and translate error codes + * returned by gethostbyname() and gethostbyaddr(). In case of error + * these functions don't set errno but h_errno on POSIX systems, but + * under Windows the error code can be retrieved by WSAGetLastError() + * as usual. + * + * The functions gethostbyname() and gethostbyaddr() are obsolete, + * and getaddressinfo() should be used preferably. + * + * @param info An optional informational text string, or NULL + * + * @return One of the @ref MBG_ERROR_CODES + */ +int mbg_get_gethostbyname_error( const char *info ) +{ + #if defined( MBG_TGT_CVI ) + + #warning This needs to be implemented for CVI + return MBG_ERR_UNSPEC; + + #elif defined( MBG_TGT_WIN32 ) + + return mbg_win32_wsa_err_to_mbg( WSAGetLastError(), info ); + + #elif defined( MBG_TGT_POSIX ) + + return mbg_posix_h_errno_to_mbg( h_errno, info ); + + #else + + #error This function is not supported for this target. + + #endif + +} // mbg_get_gethostbyname_error + + + +#if 0 // not yet finished +// error handler for getaddressinfo() +/* + * Handle specific error returned by getaddressinfo() + */ + /*HDR*/ +int mbg_gai_error( int rc, const char *info ) +{ + #if defined( MBG_TGT_CVI ) + + #warning This needs to be implemented for CVI + return MBG_ERR_UNSPEC; + + #elif defined( MBG_TGT_WIN32 ) + + return mbg_win32_wsa_err_to_mbg( WSAGetLastError(), info ); + + #elif defined( MBG_TGT_POSIX ) + + return mbg_posix_h_errno_to_mbg( h_errno, info ); + + #else + + return MBG_ERR_UNSPEC; + + #endif + +} // mbg_get_gai_error + +#endif + + + |