/* Hey EMACS -*- linux-c -*- */ /* libcalccables - link cable library, a part of the CalcForge project * Copyright (C) 1999-2005 Romain Lievin * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #if defined(__WIN32__) #include #endif #ifdef __LINUX__ #include #endif #ifdef _MSC_VER # include "./win32/usb.h" #elif defined(HAVE_LIBUSB) # include #endif #include "gettext.h" #include "calccables.h" #include "logging.h" #include "error.h" #include "link_xxx.h" #include "data_log.h" /*****************/ /* Internal data */ /*****************/ static CableFncts const *const cables[] = { &cable_nul, #ifndef NO_CABLE_GRY &cable_gry, #endif #if !defined(NO_CABLE_BLK) && !defined(__MACOSX__) &cable_ser, #endif #if !defined(NO_CABLE_PAR) && (defined(HAVE_LINUX_PARPORT_H) || defined(__WIN32__)) &cable_par, #endif #if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(__WIN32__)) &cable_slv, #endif #if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(__WIN32__)) &cable_raw, #endif #ifndef NO_CABLE_VTI &cable_vti, #endif #ifndef NO_CABLE_TIE &cable_tie, #endif &cable_ilp, #if !defined(NO_CABLE_SLV) && defined(HAVE_LINUX_TICABLE_H) &cable_dev, #endif NULL }; /****************/ /* Entry points */ /****************/ // not static, must be shared between instances int calccables_instance = 0; // counts # of instances /** * calccables_library_init: * * This function must be the first one to call. It inits library internals. * * Return value: the instance count. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_library_init(void) { char locale_dir[65536]; #ifdef __WIN32__ HANDLE hDll; int i; hDll = GetModuleHandle("libcalccables-0.dll"); GetModuleFileName(hDll, locale_dir, 65535); for (i = strlen(locale_dir); i >= 0; i--) { if (locale_dir[i] == '\\') break; } locale_dir[i] = '\0'; #ifdef __MINGW32__ strcat(locale_dir, "\\..\\share\\locale"); #else strcat(locale_dir, "\\locale"); #endif #else strcpy(locale_dir, LOCALEDIR); #endif if (calccables_instance) return (++calccables_instance); calccables_info(_("libcalccables version %s"), VERSION); errno = 0; #if defined(ENABLE_NLS) calccables_info("setlocale: %s", setlocale(LC_ALL, "")); calccables_info("bindtextdomain: %s", bindtextdomain(PACKAGE, locale_dir)); //bind_textdomain_codeset(PACKAGE, "UTF-8"/*"ISO-8859-15"*/); calccables_info("textdomain: %s", textdomain(PACKAGE)); #endif #ifdef __LINUX__ { struct utsname buf; uname(&buf); calccables_info("kernel: %s", buf.release); } #endif #if defined(HAVE_LIBUSB) /* init the libusb */ usb_init(); #endif return (++calccables_instance); } /** * calccables_library_exit: * * This function must be the last one to call. Used to release internal resources. * * Return value: the instance count. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_library_exit(void) { return (--calccables_instance); } /***********/ /* Methods */ /***********/ /** * calccables_version_get: * * This function returns the library version like "X.Y.Z". * * Return value: a string. **/ CALCCABLES_EXPORT const char *CALCFORGE_CALL calccables_version_get(void) { return VERSION; } /** * calccables_handle_new: * @model: a cable model * @port: the generic port on which cable is attached. * * Create a new handle associated with the given cable on the given port. * Must be freed with calccables_handle_del when no longer needed. * Note: the handle is a pointer on an opaque structure and should not be modified. * * Return value: NULL if error, an handle otherwise. **/ CALCCABLES_EXPORT CableHandle* CALCFORGE_CALL calccables_handle_new(CableModel model, CablePort port) { CableHandle *handle = (CableHandle *)calloc(1, sizeof(CableHandle)); int i; handle->model = model; handle->port = port; handle->delay = DFLT_DELAY; handle->timeout = DFLT_TIMEOUT; for(i = 0; cables[i]; i++) if(cables[i]->model == model) { handle->cable = (CableFncts *)cables[i]; break; } if(handle->cable == NULL) return NULL; return handle; } /** * calccables_handle_del: * @handle: the handle * * Release the cable and free the associated resources. * * Return value: always 0. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_handle_del(CableHandle* handle) { if(handle) { if(handle->priv2) { free(handle->priv2); handle->priv2 = NULL; } if(handle->device) { free(handle->device); handle->device = NULL; } free(handle); handle = NULL; } return 0; } /** * calccables_options_set_timeout: * @handle: the handle * @timeout: timeout value in tenth of seconds * * Set timeout for any cable. * * Return value: the previous timeout. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_options_set_timeout(CableHandle* handle, int timeout) { int old_timeout = handle->timeout; handle->timeout = timeout; if(handle) { const CableFncts *cable = handle->cable; if(!handle->open) return -1; if(handle->busy) return ERR_BUSY; handle->busy = 1; if(cable->timeout) cable->timeout(handle); handle->busy = 0; } return old_timeout; } /** * calccables_options_set_delay: * @handle: the handle * @delay: delay in micro-seconds * * Set inter-bit delay for parallel or BlackLink cable. * * Return value: the previous delay. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_options_set_delay(CableHandle* handle, int delay) { int old_delay = handle->delay; handle->delay = delay; return old_delay; } /** * calccables_get_model: * @handle: the handle * * Retrieve link cable model. * * Return value: the previous #CableModel value. **/ CALCCABLES_EXPORT CableModel CALCFORGE_CALL calccables_get_model(CableHandle* handle) { return handle->model; } /** * calccables_get_port: * @handle: the handle * * Retrieve link port. * * Return value: a #CablePort value. **/ CALCCABLES_EXPORT CablePort CALCFORGE_CALL calccables_get_port(CableHandle* handle) { return handle->port; } /** * calccables_handle_show: * @handle: the handle * * Show informations stored in the handle. * * Return value: always 0. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_handle_show(CableHandle* handle) { calccables_info(_("Link cable handle details:")); calccables_info(_(" model : %s"), calccables_model_to_string(handle->model)); calccables_info(_(" port : %s"), calccables_port_to_string(handle->port)); calccables_info(_(" timeout : %2.1fs"), (float)handle->timeout / 10); calccables_info(_(" delay : %i us"), handle->delay); if(handle->device) { calccables_info(_(" device : %s"), handle->device); calccables_info(_(" address : 0x%03x"), handle->address); } return 0; }