Debian Codegraph @

src/ticables.cc

raw
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 <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #if defined(__WIN32__)
31 #include <windows.h>
32 #endif
33 #include <locale.h>
34 #ifdef __LINUX__
35 #include <sys/utsname.h>
36 #endif
37
38 #ifdef _MSC_VER
39 # include "./win32/usb.h"
40 #elif defined(HAVE_LIBUSB)
41 # include <usb.h>
42 #elif defined(HAVE_LIBUSB_1_0)
43 # include <libusb.h>
44 #endif
45
46 #include "gettext.h"
47 #include "ticables.h"
48 #include "internal.h"
49 #include "logging.h"
50 #include "error.h"
51 #include "link_xxx.h"
52 #include "data_log.h"
53
54 /*****************/
55 /* Internal data */
56 /*****************/
57
58 static CableFncts const *const cables[] =
59 {
60 &cable_nul,
61 #ifndef NO_CABLE_GRY
62 &cable_gry,
63 #endif
64 #if !defined(NO_CABLE_BLK) && !defined(__MACOSX__)
65 &cable_ser,
66 #endif
67 #if !defined(NO_CABLE_PAR) && (defined(HAVE_LINUX_PARPORT_H) || defined(__WIN32__))
68 &cable_par,
69 #endif
70 #if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) || defined(__WIN32__))
71 &cable_slv,
72 #endif
73 #if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) || defined(__WIN32__))
74 &cable_raw,
75 #endif
76 #ifndef NO_CABLE_VTI
77 &cable_vti,
78 #endif
79 #ifndef NO_CABLE_TIE
80 &cable_tie,
81 #endif
82 &cable_ilp,
83 &cable_nul, // Dead cable
84 #if !defined(NO_CABLE_TCPC) && !defined(__WIN32__)
85 &cable_tcpc,
86 #endif
87 #if !defined(NO_CABLE_TCPS) && !defined(__WIN32__)
88 &cable_tcps,
89 #endif
90 NULL
91 };
92
93 static const uint32_t supported_cables =
94 (1U << CABLE_NUL)
95 #ifndef NO_CABLE_GRY
96 | (1U << CABLE_GRY)
97 #endif
98 #if !defined(NO_CABLE_BLK) && !defined(__MACOSX__)
99 | (1U << CABLE_BLK)
100 #endif
101 #if !defined(NO_CABLE_PAR) && (defined(HAVE_LINUX_PARPORT_H) || defined(__WIN32__))
102 | (1U << CABLE_PAR)
103 #endif
104 #if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) || defined(__WIN32__))
105 | (1U << CABLE_SLV)
106 #endif
107 #if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) || defined(__WIN32__))
108 | (1U << CABLE_USB)
109 #endif
110 #ifndef NO_CABLE_VTI
111 | (1U << CABLE_VTI)
112 #endif
113 #ifndef NO_CABLE_TIE
114 | (1U << CABLE_TIE)
115 #endif
116 | (1U << CABLE_ILP)
117 // | (1U << CABLE_DEV) Dead cable
118 #if !defined(NO_CABLE_TCPC) && !defined(__WIN32__)
119 | (1U << CABLE_TCPC)
120 #endif
121 #if !defined(NO_CABLE_TCPS) && !defined(__WIN32__)
122 | (1U << CABLE_TCPS)
123 #endif
124 ;
125
126 /****************/
127 /* Entry points */
128 /****************/
129
130 // not static, must be shared between instances
131 int ticables_instance = 0; // counts # of instances
132 #ifdef HAVE_LIBUSB_1_0
133 int libusb_working = -1;
134 #endif
135
136 /**
137 * ticables_library_init:
138 *
139 * This function must be the first one to call. It inits library internals.
140 *
141 * Return value: the instance count.
142 **/
143 int TICALL ticables_library_init(void)
144 {
145 char locale_dir[65536];
146
147 #ifdef __WIN32__
148 HMODULE hDll;
149 int i;
150
151 hDll = GetModuleHandle("libticables2-8.dll");
152 GetModuleFileName(hDll, locale_dir, 65515);
153
154 for (i = strlen(locale_dir); i >= 0; i--)
155 {
156 if (locale_dir[i] == '\\')
157 {
158 break;
159 }
160 }
161 locale_dir[i] = '\0';
162
163 #ifdef __MINGW32__
164 strcat(locale_dir, "\\..\\share\\locale");
165 #else
166 strcat(locale_dir, "\\locale");
167 #endif
168 #else
169 strncpy(locale_dir, LOCALEDIR, sizeof(locale_dir) - 21);
170 #endif
171
172 if (ticables_instance)
173 {
174 return (++ticables_instance);
175 }
176 ticables_info(_("ticables library version %s"), libticables2_VERSION);
177 errno = 0;
178
179 #if defined(ENABLE_NLS)
180 ticables_info("setlocale: %s", setlocale(LC_ALL, ""));
181 ticables_info("bindtextdomain: %s", bindtextdomain(PACKAGE, locale_dir));
182 bind_textdomain_codeset(PACKAGE, "UTF-8"/*"ISO-8859-15"*/);
183 ticables_info("textdomain: %s", textdomain(NULL));
184 #endif
185 #ifdef __LINUX__
186 {
187 struct utsname buf;
188
189 uname(&buf);
190 ticables_info("kernel: %s", buf.release);
191 }
192 #endif
193 #if defined(HAVE_LIBUSB)
194 // init libusb
195 usb_init();
196 #elif defined(HAVE_LIBUSB_1_0)
197 // init libusb, keeping track of success / failure status
198 libusb_working = libusb_init(NULL);
199 if (libusb_working == LIBUSB_SUCCESS) {
200 libusb_set_debug(NULL, 3);
201 }
202 #endif
203
204 return (++ticables_instance);
205 }
206
207
208 /**
209 * ticables_library_exit:
210 *
211 * This function must be the last one to call. Used to release internal resources.
212 *
213 * Return value: the instance count.
214 **/
215 int TICALL ticables_library_exit(void)
216 {
217 #if defined(HAVE_LIBUSB)
218 // No exit function for libusb 0.1.x.
219 #elif defined(HAVE_LIBUSB_1_0)
220 // Must not call libusb_exit() if libusb_init() failed, or call it multiple times.
221 if (ticables_instance == 1 && libusb_working == LIBUSB_SUCCESS) {
222 libusb_exit(NULL);
223 }
224 #endif
225 return (--ticables_instance);
226 }
227
228 /***********/
229 /* Methods */
230 /***********/
231
232 /**
233 * ticables_version_get:
234 *
235 * This function returns the library version like "X.Y.Z".
236 *
237 * Return value: a string.
238 **/
239 const char *TICALL ticables_version_get(void)
240 {
241 return libticables2_VERSION;
242 }
243
244 /**
245 * ticables_supported_cables:
246 *
247 * This function returns the cables built into the current binary.
248 *
249 * Return value: an integer containing a binary OR of (1 << CABLE_*) values,
250 * where CABLE_* values are defined in enum CableModel.
251 **/
252 uint32_t TICALL ticables_supported_cables(void)
253 {
254 return supported_cables;
255 }
256
257 /**
258 * ticables_max_ports:
259 *
260 * This function returns the maximum number of ports supported for any given cable.
261 *
262 * Return value: the PORT_MAX value against which the library was built.
263 **/
264 uint32_t TICALL ticables_max_ports(void)
265 {
266 return PORT_MAX;
267 }
268
269 static int default_event_hook(CableHandle * handle, uint32_t event_count, const CableEventData * event, void * user_pointer)
270 {
271 (void)user_pointer;
272 const char * cablestr = ticables_model_to_string(ticables_get_model(handle));
273 const char * portstr = ticables_port_to_string(ticables_get_port(handle));
274 if (getenv("TICABLES_EVENT_DEBUG") != NULL)
275 {
276 ticables_info("Event #%u type %d for cable %s port %s", event_count, event->type, cablestr, portstr);
277 }
278 switch (event->type)
279 {
280 case CABLE_EVENT_TYPE_BEFORE_SEND: break;
281 case CABLE_EVENT_TYPE_AFTER_SEND:
282 {
283 LOG_N_DATA(handle, LOG_OUT, event->data.data.data, event->data.data.len);
284 break;
285 }
286 case CABLE_EVENT_TYPE_BEFORE_RECV: break;
287 case CABLE_EVENT_TYPE_AFTER_RECV:
288 {
289 LOG_N_DATA(handle, LOG_IN, event->data.data.data, event->data.data.len);
290 break;
291 }
292 default: break;
293 }
294 return event->retval;
295 }
296
297 /**
298 * ticables_handle_new:
299 * @model: a cable model
300 * @port: the generic port on which cable is attached.
301 *
302 * Create a new handle associated with the given cable on the given port.
303 * Must be freed with ticables_handle_del when no longer needed.
304 * Note: the handle is a pointer on an opaque structure and should not be modified.
305 *
306 * Return value: NULL if error, an handle otherwise.
307 **/
308 CableHandle* TICALL ticables_handle_new(CableModel model, CablePort port)
309 {
310 CableHandle *handle = (CableHandle *)calloc(1, sizeof(CableHandle));
311 int i;
312
313 handle->model = model;
314 handle->port = port;
315
316 handle->delay = DFLT_DELAY;
317 handle->timeout = DFLT_TIMEOUT;
318
319 for (i = 0; cables[i] != NULL; i++)
320 {
321 if (cables[i]->model == model)
322 {
323 handle->cable = (CableFncts *)cables[i];
324 break;
325 }
326 }
327
328 if (handle->cable == NULL)
329 {
330 free(handle);
331 handle = NULL;
332 }
333
334 if (handle != NULL)
335 {
336 handle->event_hook = default_event_hook;
337 //handle->event_count = 0;
338 }
339
340 return handle;
341 }
342
343 /**
344 * ticables_handle_del:
345 * @handle: the handle
346 *
347 * Release the cable and free the associated resources.
348 *
349 * Return value: always 0.
350 **/
351 int TICALL ticables_handle_del(CableHandle* handle)
352 {
353 VALIDATE_HANDLE(handle);
354
355 free(handle->priv2);
356 handle->priv2 = NULL;
357
358 free(handle->device);
359 handle->device = NULL;
360
361 memset((void *)handle, 0, sizeof(*handle));
362 free(handle);
363
364 return 0;
365 }
366
367 /**
368 * ticables_options_set_timeout:
369 * @handle: the handle
370 * @timeout: timeout value in tenth of seconds
371 *
372 * Set timeout for any cable.
373 *
374 * Return value: the previous timeout.
375 **/
376 unsigned int TICALL ticables_options_set_timeout(CableHandle* handle, unsigned int timeout)
377 {
378 if (handle != NULL)
379 {
380 const CableFncts *cable;
381 unsigned int old_timeout = handle->timeout;
382
383 handle->timeout = timeout;
384 cable = handle->cable;
385
386 if(!handle->open)
387 {
388 return -1;
389 }
390 if(handle->busy)
391 {
392 return ERR_BUSY;
393 }
394
395 handle->busy = 1;
396 if(cable->timeout)
397 {
398 cable->timeout(handle);
399 }
400 handle->busy = 0;
401
402 return old_timeout;
403 }
404 else
405 {
406 ticables_critical("%s: handle is NULL", __FUNCTION__);
407 return 0;
408 }
409 }
410
411 /**
412 * ticables_options_set_delay:
413 * @handle: the handle
414 * @delay: delay in micro-seconds
415 *
416 * Set inter-bit delay for parallel or BlackLink cable.
417 *
418 * Return value: the previous delay.
419 **/
420 unsigned int TICALL ticables_options_set_delay(CableHandle* handle, unsigned int delay)
421 {
422 if (handle != NULL)
423 {
424 unsigned int old_delay = handle->delay;
425 handle->delay = delay;
426 return old_delay;
427 }
428 else
429 {
430 ticables_critical("%s: handle is NULL", __FUNCTION__);
431 return 0;
432 }
433 }
434
435 /**
436 * ticables_get_model:
437 * @handle: the handle
438 *
439 * Retrieve link cable model.
440 *
441 * Return value: the previous #CableModel value.
442 **/
443 CableModel TICALL ticables_get_model(CableHandle* handle)
444 {
445 if (handle != NULL)
446 {
447 return handle->model;
448 }
449 else
450 {
451 ticables_critical("%s(NULL)", __FUNCTION__);
452 return CABLE_NUL;
453 }
454 }
455
456 /**
457 * ticables_get_port:
458 * @handle: the handle
459 *
460 * Retrieve link port.
461 *
462 * Return value: a #CablePort value.
463 **/
464 CablePort TICALL ticables_get_port(CableHandle* handle)
465 {
466 if (handle != NULL)
467 {
468 return handle->port;
469 }
470 else
471 {
472 ticables_critical("%s(NULL)", __FUNCTION__);
473 return PORT_0;
474 }
475 }
476
477 /**
478 * ticables_get_device:
479 * @handle: the handle
480 *
481 * Retrieve device port.
482 *
483 * Return value: a char pointer.
484 **/
485 const char * TICALL ticables_get_device(CableHandle* handle)
486 {
487 if (handle != NULL)
488 {
489 return handle->device;
490 }
491 else
492 {
493 ticables_critical("%s(NULL)", __FUNCTION__);
494 return NULL;
495 }
496 }
497
498 /**
499 * ticables_get_timeout:
500 * @handle: the handle
501 *
502 * Retrieve timeout.
503 *
504 * Return value: an unsigned integer;
505 **/
506 unsigned int TICALL ticables_get_timeout(CableHandle *handle)
507 {
508 if (handle != NULL)
509 {
510 return handle->timeout;
511 }
512 else
513 {
514 ticables_critical("%s(NULL)", __FUNCTION__);
515 return 0;
516 }
517 }
518
519 /**
520 * ticables_get_delay:
521 * @handle: the handle
522 *
523 * Retrieve timeout.
524 *
525 * Return value: an unsigned integer;
526 **/
527 unsigned int TICALL ticables_get_delay(CableHandle *handle)
528 {
529 if (handle != NULL)
530 {
531 return handle->delay;
532 }
533 else
534 {
535 ticables_critical("%s(NULL)", __FUNCTION__);
536 return 0;
537 }
538 }
539
540 /**
541 * ticables_handle_show:
542 * @handle: the handle
543 *
544 * Show information stored in the handle.
545 *
546 * Return value: always 0.
547 **/
548 int TICALL ticables_handle_show(CableHandle* handle)
549 {
550 if (handle != NULL)
551 {
552 ticables_info("%s", _("Link cable handle details:"));
553 ticables_info(_(" model : %s"), ticables_model_to_string(handle->model));
554 ticables_info(_(" port : %s"), ticables_port_to_string(handle->port));
555 ticables_info(_(" timeout : %2.1fs"), (float)handle->timeout / 10);
556 ticables_info(_(" delay : %i us"), handle->delay);
557 if(handle->device)
558 {
559 ticables_info(_(" device : %s"), handle->device);
560 ticables_info(_(" address : 0x%03x"), handle->address);
561 }
562 }
563 else
564 {
565 ticables_critical("%s(NULL)", __FUNCTION__);
566 }
567
568 return 0;
569 }