/* 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 "gettext.h" #include "logging.h" #include "calccables.h" #include "error.h" #include "linux/detect.h" #include "win32/detect.h" #include "bsd/detect.h" /** * calccables_probing_do: * @result: address of an array of integers to put the result. * @timeout: timeout to set during probing * @method: defines which link cables you want to search for. * * Returns cables which have been detected. All cables should be closed before ! * The array is like a matrix which contains 5 columns (PORT_0 to PORT_4) and * 7 lines (CABLE_GRY to CABLE_USB). * The array must be freed by #calccables_probing_finish when no longer used. * * Return value: 0 if successful, ERR_NO_CABLE if no cables found. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_probing_do(int ***result, int timeout, ProbingMethod method) { CablePort port; CableModel model; int **array; int found = 0; calccables_info(_("Link cable probing:")); array = (int **)calloc(CABLE_MAX + 1, sizeof(int *)); for(model = CABLE_NUL; model <= CABLE_MAX; model++) array[model] = (int *)calloc(5, sizeof(int)); // look for USB devices (faster) if(method & PROBE_USB) { int *list, n, i; calccables_get_usb_devices(&list, &n); for(i = 0; i < n; i++) { port = i+1; if(list[i] == PID_TIGLUSB) array[CABLE_SLV][port] = !0; if(list[i]) array[CABLE_USB][port] = !0; if(list[i]) found = !0; } } if((method & PROBE_FIRST) && found) { *result = array; return found ? 0 : ERR_NO_CABLE; } // look for DBUS devices (slower) if(method & PROBE_DBUS) { for(model = CABLE_GRY; model <= CABLE_PAR; model++) { for(port = PORT_1; port <= PORT_4; port++) { CableHandle* handle; int err, ret; handle = calccables_handle_new(model, port); if(handle) { calccables_options_set_timeout(handle, timeout); err = calccables_cable_probe(handle, &ret); array[model][port] = (ret && !err) ? 1: 0; if(array[model][port]) found = !0; if(found && (method & PROBE_FIRST)) { calccables_handle_del(handle); break; } } calccables_handle_del(handle); } } } *result = array; return found ? 0 : ERR_NO_CABLE; } /** * calccables_probing_finish: * @result: address of an array of integers. * * Free the array created by #calccables_probing_do. * * Return value: always 0. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_probing_finish(int ***result) { int i; for(i = CABLE_GRY; i <= CABLE_TIE; i++) { free((*result)[i]); (*result)[i] = NULL; } free(*result); *result = NULL; return 0; } /** * calccables_is_usb_enabled: * * Checks whether USB support is available. Can be called at any time. * * Return value: !0 if available, 0 otherwise. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_is_usb_enabled(void) { #if defined(__WIN32__) return !win32_check_libusb(); #elif defined(__LINUX__) && defined(HAVE_LIBUSB) return !linux_check_libusb(); #elif defined(HAVE_LIBUSB) return 1; #else return 0; #endif return 0; } extern int usb_probe_devices(int **list); /** * calccables_get_usb_devices: * @array: address of a NULL-terminated allocated array of integers (PIDs). * @length: number of detected USB devices. * * Returns the list of detected USB PIDs. Note that list is in the * same order as PORT#x. * The array must be freed when no longer used. * * Return value: 0 if successful, an error code otherwise. **/ CALCCABLES_EXPORT int CALCFORGE_CALL calccables_get_usb_devices(int **list, int *len) { #if defined(__WIN32__) || defined(HAVE_LIBUSB) int i, *p; int ret = 0; ret = usb_probe_devices(list); if(ret) { *list = calloc(1, sizeof(int)); if(len) *len = 0; return ret; } for(p = *list, i = 0; *p; p++, i++); //printf("%i: %04x\n", i, (*list)[i]); if(len) *len = i; #else *list = calloc(1, sizeof(int)); if(len) *len = 0; #endif return 0; }