| 1 |
/* Hey EMACS -*- linux-c -*- */ |
| 2 |
/* $Id$ */ |
| 3 |
|
| 4 |
/* libticables2 - link cable library, a part of the TiLP project |
| 5 |
* Copyright (C) 1999-2005 Romain Lievin |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software Foundation, |
| 19 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <stdlib.h> |
| 29 |
|
| 30 |
#include "gettext.h" |
| 31 |
#include "logging.h" |
| 32 |
#include "ticables.h" |
| 33 |
#include "error.h" |
| 34 |
#include "internal.h" |
| 35 |
|
| 36 |
#include "linux/detect.h" |
| 37 |
#include "win32/detect.h" |
| 38 |
#include "bsd/detect.h" |
| 39 |
#include "macos/detect.h" |
| 40 |
|
| 41 |
int TICALL ticables_probing_found(int *array) |
| 42 |
{ |
| 43 |
if (array != NULL) |
| 44 |
{ |
| 45 |
int i; |
| 46 |
for (i = PORT_FIRST; i < PORT_MAX; i++) |
| 47 |
{ |
| 48 |
if (array[i]) |
| 49 |
{ |
| 50 |
return i; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
ticables_critical("%s(NULL)", __FUNCTION__); |
| 57 |
} |
| 58 |
|
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
void TICALL ticables_probing_show(int **array) |
| 63 |
{ |
| 64 |
if (array != NULL) |
| 65 |
{ |
| 66 |
int model; |
| 67 |
|
| 68 |
for (model = CABLE_NUL; model < CABLE_MAX; model++) |
| 69 |
{ |
| 70 |
int * arraymodel = array[model]; |
| 71 |
if (arraymodel != NULL) |
| 72 |
{ |
| 73 |
ticables_info(" %i: %i %i %i %i", model, array[model][PORT_1], array[model][PORT_2], array[model][PORT_3], array[model][PORT_4]); |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
ticables_info("%i: null", model); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
ticables_critical("%s(NULL)", __FUNCTION__); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* ticables_probing_do: |
| 89 |
* @result: address of an array of integers to put the result. |
| 90 |
* @timeout: timeout to set during probing |
| 91 |
* @method: defines which link cables you want to search for. |
| 92 |
* |
| 93 |
* Returns cables which have been detected. All cables should be closed before calling this function ! |
| 94 |
* The array defines a matrix of PORT_MAX columns and CABLE_MAX + 1 rows; the rows corresponding to |
| 95 |
* cables which cannot be probed are allocated but empty. |
| 96 |
* The array must be freed by #ticables_probing_finish when no longer used. |
| 97 |
* |
| 98 |
* Return value: 0 if successful, ERR_NO_CABLE if no cables found. |
| 99 |
**/ |
| 100 |
int TICALL ticables_probing_do(int ***result, unsigned int timeout, ProbingMethod method) |
| 101 |
{ |
| 102 |
int port; |
| 103 |
int model; |
| 104 |
int **array; |
| 105 |
int found = 0; |
| 106 |
|
| 107 |
if (result == NULL) |
| 108 |
{ |
| 109 |
ticables_critical("%s: result is NULL", __FUNCTION__); |
| 110 |
return ERR_PROBE_FAILED; |
| 111 |
} |
| 112 |
*result = NULL; |
| 113 |
|
| 114 |
ticables_info("%s", _("Link cable probing:")); |
| 115 |
|
| 116 |
array = (int **)calloc(CABLE_MAX + 1, sizeof(int *)); |
| 117 |
if (array == NULL) |
| 118 |
{ |
| 119 |
return ERR_PROBE_FAILED; // THIS RETURNS ! |
| 120 |
} |
| 121 |
|
| 122 |
for (model = CABLE_NUL; model <= CABLE_MAX; model++) |
| 123 |
{ |
| 124 |
array[model] = (int *)calloc(PORT_MAX, sizeof(int)); |
| 125 |
if (array[model] == NULL) |
| 126 |
{ |
| 127 |
for (model = CABLE_NUL; model <= CABLE_MAX; model++) |
| 128 |
{ |
| 129 |
free(array[model]); |
| 130 |
} |
| 131 |
free(array); |
| 132 |
return ERR_PROBE_FAILED; // THIS RETURNS ! |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// look for USB devices (faster) |
| 137 |
if (method & PROBE_USB) |
| 138 |
{ |
| 139 |
int *list, n, i; |
| 140 |
|
| 141 |
ticables_get_usb_devices(&list, &n); |
| 142 |
|
| 143 |
for (i = 0; i < n; i++) |
| 144 |
{ |
| 145 |
port = i+1; |
| 146 |
|
| 147 |
if (list[i] == PID_TIGLUSB) |
| 148 |
{ |
| 149 |
array[CABLE_SLV][port] = !0; |
| 150 |
} |
| 151 |
|
| 152 |
if (list[i]) |
| 153 |
{ |
| 154 |
array[CABLE_USB][port] = !0; |
| 155 |
found = !0; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
ticables_free_usb_devices(list); |
| 160 |
} |
| 161 |
|
| 162 |
if ((method & PROBE_FIRST) && found) |
| 163 |
{ |
| 164 |
*result = array; |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
// look for DBUS devices (slower) |
| 169 |
if (method & PROBE_DBUS) |
| 170 |
{ |
| 171 |
for (model = CABLE_GRY; model <= CABLE_PAR; model++) |
| 172 |
{ |
| 173 |
for (port = PORT_1; port < PORT_MAX; port++) |
| 174 |
{ |
| 175 |
CableHandle* handle; |
| 176 |
int err, ret; |
| 177 |
|
| 178 |
handle = ticables_handle_new((CableModel)model, (CablePort)port); |
| 179 |
if (handle != NULL) |
| 180 |
{ |
| 181 |
ticables_options_set_timeout(handle, timeout); |
| 182 |
err = ticables_cable_probe(handle, &ret); |
| 183 |
array[model][port] = (ret && !err) ? 1: 0; |
| 184 |
if (array[model][port]) |
| 185 |
{ |
| 186 |
found = !0; |
| 187 |
} |
| 188 |
|
| 189 |
if (found && (method & PROBE_FIRST)) |
| 190 |
{ |
| 191 |
ticables_handle_del(handle); |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
ticables_handle_del(handle); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
*result = array; |
| 201 |
return found ? 0 : ERR_NO_CABLE; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* ticables_probing_finish: |
| 206 |
* @result: address of an array of integers. |
| 207 |
* |
| 208 |
* Free the array created by #ticables_probing_do. |
| 209 |
* |
| 210 |
* Return value: always 0. |
| 211 |
**/ |
| 212 |
int TICALL ticables_probing_finish(int ***result) |
| 213 |
{ |
| 214 |
int i; |
| 215 |
|
| 216 |
if (result != NULL && *result != NULL) |
| 217 |
{ |
| 218 |
for (i = CABLE_NUL; i <= CABLE_MAX; i++) |
| 219 |
{ |
| 220 |
free((*result)[i]); |
| 221 |
(*result)[i] = NULL; |
| 222 |
} |
| 223 |
|
| 224 |
free(*result); |
| 225 |
*result = NULL; |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
ticables_critical("%s(NULL)", __FUNCTION__); |
| 230 |
} |
| 231 |
|
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* ticables_is_usb_enabled: |
| 237 |
* |
| 238 |
* Checks whether USB support is available. Can be called at any time. |
| 239 |
* |
| 240 |
* Return value: !0 if available, 0 otherwise. |
| 241 |
**/ |
| 242 |
int TICALL ticables_is_usb_enabled(void) |
| 243 |
{ |
| 244 |
#if defined(__WIN32__) |
| 245 |
return !win32_check_libusb(); |
| 246 |
#elif defined(__MACOSX__) |
| 247 |
return !macosx_check_libusb(); |
| 248 |
#elif defined(__BSD__) |
| 249 |
return !bsd_check_libusb(); |
| 250 |
#elif defined(__LINUX__) |
| 251 |
return !linux_check_libusb(); |
| 252 |
#else |
| 253 |
return 0; |
| 254 |
#endif |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* ticables_get_usb_devices: |
| 259 |
* @list: out address of a NULL-terminated allocated array of integers (PIDs). |
| 260 |
* @len: out pointer to number of detected USB devices. |
| 261 |
* |
| 262 |
* Returns the list of detected USB PIDs. Note that list is in the |
| 263 |
* same order as PORT#x. |
| 264 |
* The array must be freed when no longer used. |
| 265 |
* |
| 266 |
* Return value: 0 if successful, an error code otherwise. |
| 267 |
**/ |
| 268 |
int TICALL ticables_get_usb_devices(int **list, int *len) |
| 269 |
{ |
| 270 |
if (list != NULL) |
| 271 |
{ |
| 272 |
int ret = 0; |
| 273 |
const USBCableInfo *info = NULL; |
| 274 |
int i, n = 0; |
| 275 |
|
| 276 |
#if defined(__WIN32__) || (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0)) |
| 277 |
ret = usb_probe_device_info(&info, &n); |
| 278 |
#endif |
| 279 |
*list = (int *)calloc(1 + n, sizeof(int)); |
| 280 |
for (i = 0; i < n; i++) |
| 281 |
{ |
| 282 |
(*list)[i] = info[i].pid; |
| 283 |
} |
| 284 |
if (len) |
| 285 |
{ |
| 286 |
*len = i; |
| 287 |
} |
| 288 |
|
| 289 |
return ret; |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
ticables_critical("%s: list is NULL", __FUNCTION__); |
| 294 |
return -1; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* \brief Frees an array of USB devices |
| 300 |
* \param array the array previously allocated by ticables_get_usb_devices() |
| 301 |
* \return Always 0 |
| 302 |
*/ |
| 303 |
int TICALL ticables_free_usb_devices(int *array) |
| 304 |
{ |
| 305 |
free(array); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
void translate_usb_device_info(CableDeviceInfo *info, const USBCableInfo *usbinfo) |
| 310 |
{ |
| 311 |
if (usbinfo->pid == PID_TIGLUSB) |
| 312 |
{ |
| 313 |
info->family = CABLE_FAMILY_DBUS; |
| 314 |
info->variant = CABLE_VARIANT_TIGLUSB; |
| 315 |
} |
| 316 |
else if (usbinfo->pid == PID_TI84P) |
| 317 |
{ |
| 318 |
info->family = CABLE_FAMILY_USB_TI8X; |
| 319 |
if (!strcmp(usbinfo->product_str, "TI-84 Plus")) |
| 320 |
{ |
| 321 |
info->variant = CABLE_VARIANT_TI84P; |
| 322 |
} |
| 323 |
else if (!strcmp(usbinfo->product_str, "TI-82 Advanced")) |
| 324 |
{ |
| 325 |
info->variant = CABLE_VARIANT_TI82A; |
| 326 |
} |
| 327 |
else if (usbinfo->version <= 0x0110) |
| 328 |
{ |
| 329 |
info->variant = CABLE_VARIANT_TI84P; |
| 330 |
} |
| 331 |
else if (usbinfo->version == 0x0190) |
| 332 |
{ |
| 333 |
info->variant = CABLE_VARIANT_TI82A; |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
info->variant = CABLE_VARIANT_UNKNOWN; |
| 338 |
} |
| 339 |
} |
| 340 |
else if (usbinfo->pid == PID_TI84P_SE) |
| 341 |
{ |
| 342 |
info->family = CABLE_FAMILY_USB_TI8X; |
| 343 |
if (!strcmp(usbinfo->product_str, "TI-84 Plus Silver Edition")) |
| 344 |
{ |
| 345 |
info->variant = CABLE_VARIANT_TI84PSE; |
| 346 |
} |
| 347 |
else if (!strcmp(usbinfo->product_str, "TI-84 Plus C Silver Edition")) |
| 348 |
{ |
| 349 |
info->variant = CABLE_VARIANT_TI84PCSE; |
| 350 |
} |
| 351 |
else if (!strcmp(usbinfo->product_str, "TI-84 Plus CE")) |
| 352 |
{ |
| 353 |
info->variant = CABLE_VARIANT_TI84PCE; |
| 354 |
} |
| 355 |
else if (!strcmp(usbinfo->product_str, "TI-83 Premium CE")) |
| 356 |
{ |
| 357 |
info->variant = CABLE_VARIANT_TI83PCE; |
| 358 |
} |
| 359 |
else if (!strcmp(usbinfo->product_str, "TI-84 Plus T")) |
| 360 |
{ |
| 361 |
info->variant = CABLE_VARIANT_TI84PT; |
| 362 |
} |
| 363 |
else if (usbinfo->version <= 0x0110) |
| 364 |
{ |
| 365 |
info->variant = CABLE_VARIANT_TI84PSE; |
| 366 |
} |
| 367 |
else if (usbinfo->version == 0x0120) |
| 368 |
{ |
| 369 |
info->variant = CABLE_VARIANT_TI84PCSE; |
| 370 |
} |
| 371 |
else if (usbinfo->version == 0x0220) |
| 372 |
{ |
| 373 |
info->variant = CABLE_VARIANT_TI84PCE; |
| 374 |
} |
| 375 |
else if (usbinfo->version == 0x0260) |
| 376 |
{ |
| 377 |
info->variant = CABLE_VARIANT_TI83PCE; |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
info->variant = CABLE_VARIANT_UNKNOWN; |
| 382 |
} |
| 383 |
} |
| 384 |
else if (usbinfo->pid == PID_TI89TM) |
| 385 |
{ |
| 386 |
info->family = CABLE_FAMILY_USB_TI9X; |
| 387 |
info->variant = CABLE_VARIANT_TI89TM; |
| 388 |
} |
| 389 |
else if (usbinfo->pid == PID_NSPIRE) |
| 390 |
{ |
| 391 |
info->family = CABLE_FAMILY_USB_NSPIRE; |
| 392 |
info->variant = CABLE_VARIANT_NSPIRE; |
| 393 |
} |
| 394 |
else |
| 395 |
{ |
| 396 |
ticables_critical("unknown PID %x", usbinfo->pid); |
| 397 |
info->family = CABLE_FAMILY_UNKNOWN; |
| 398 |
info->variant = CABLE_VARIANT_UNKNOWN; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* ticables_get_usb_device_info: |
| 404 |
* @array: address of a newly allocated array of CableDeviceInfo structures |
| 405 |
* @length: number of detected USB devices. |
| 406 |
* |
| 407 |
* Returns the list of detected USB devices. Note that list is in the |
| 408 |
* same order as PORT#x. |
| 409 |
* The array must be freed when no longer used. |
| 410 |
* |
| 411 |
* Return value: 0 if successful, an error code otherwise. |
| 412 |
**/ |
| 413 |
int TICALL ticables_get_usb_device_info(CableDeviceInfo **list, int *len) |
| 414 |
{ |
| 415 |
if (list != NULL) |
| 416 |
{ |
| 417 |
int ret = 0; |
| 418 |
const USBCableInfo *usbinfo = NULL; |
| 419 |
int i, n = 0; |
| 420 |
|
| 421 |
#if defined(__WIN32__) || (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0)) |
| 422 |
ret = usb_probe_device_info(&usbinfo, &n); |
| 423 |
#endif |
| 424 |
*list = (CableDeviceInfo *)calloc(1 + n, sizeof(CableDeviceInfo)); |
| 425 |
for (i = 0; i < n; i++) |
| 426 |
{ |
| 427 |
translate_usb_device_info(&(*list)[i], &usbinfo[i]); |
| 428 |
} |
| 429 |
if (len) |
| 430 |
{ |
| 431 |
*len = i; |
| 432 |
} |
| 433 |
|
| 434 |
return ret; |
| 435 |
} |
| 436 |
else |
| 437 |
{ |
| 438 |
ticables_critical("%s: list is NULL", __FUNCTION__); |
| 439 |
return -1; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* \brief Frees an array of USB device information |
| 445 |
* \param array the array previously allocated by ticables_get_usb_device_info() |
| 446 |
* \return Always 0 |
| 447 |
*/ |
| 448 |
int TICALL ticables_free_usb_device_info(CableDeviceInfo *list) |
| 449 |
{ |
| 450 |
free(list); |
| 451 |
return 0; |
| 452 |
} |