Debian Codegraph @

src/link_xxx.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 #include <stdlib.h>
26
27 #include "ticables.h"
28 #include "internal.h"
29 #include "error.h"
30 #include "logging.h"
31 #include "data_log.h"
32
33 /**
34 * ticables_cable_open:
35 * @handle: a previously allocated handle
36 *
37 * Attempt to open a connection on the cable with the parameters
38 * given with #ticables_handle_new().
39 *
40 * Return value: 0 if successful, an error code otherwise.
41 **/
42 int TICALL ticables_cable_open(CableHandle* handle)
43 {
44 int ret = 0;
45
46 VALIDATE_HANDLE(handle);
47
48 const CableFncts * cable = handle->cable;
49 VALIDATE_CABLEFNCTS(cable);
50
51 ret = ticables_event_send_simple(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_OPEN, /* retval */ 0);
52
53 if (!ret)
54 {
55 if (cable->prepare)
56 {
57 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_PREPARE);
58 if (!ret)
59 {
60 ret = cable->prepare(handle);
61 }
62 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_PREPARE);
63 }
64
65 if (!ret)
66 {
67 if (cable->open)
68 {
69 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_OPEN);
70 if (!ret)
71 {
72 ret = cable->open(handle);
73 }
74 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_OPEN);
75 }
76 if (!ret)
77 {
78 handle->open = 1;
79 START_LOGGING(handle);
80 }
81 }
82 }
83
84 ret = ticables_event_send_simple(handle, /* type */ CABLE_EVENT_TYPE_AFTER_OPEN, /* retval */ ret);
85
86 return ret;
87 }
88
89 /**
90 * ticables_cable_close:
91 * @handle: a previously allocated handle
92 *
93 * Attempt to close a connection on the cable with the parameters
94 * given with #ticables_handle_new().
95 *
96 * Return value: 0 if successful, an error code otherwise.
97 **/
98 int TICALL ticables_cable_close(CableHandle* handle)
99 {
100 int ret = 0;
101
102 VALIDATE_HANDLE(handle);
103
104 const CableFncts * cable = handle->cable;
105 VALIDATE_CABLEFNCTS(cable);
106
107 ret = ticables_event_send_simple(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_CLOSE, /* retval */ 0);
108
109 STOP_LOGGING(handle);
110 if (!ret)
111 {
112 if (handle->open)
113 {
114 if (cable->close)
115 {
116 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_CLOSE);
117 if (!ret)
118 {
119 ret = cable->close(handle);
120 }
121 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_CLOSE);
122 }
123 handle->open = 0;
124 free(handle->device);
125 handle->device = NULL;
126 }
127 }
128
129 ret = ticables_event_send_simple(handle, /* type */ CABLE_EVENT_TYPE_AFTER_CLOSE, /* retval */ ret);
130
131 return ret;
132 }
133
134 /**
135 * ticables_cable_reset:
136 * @handle: a previously allocated handle
137 *
138 * Reset link cable status (flush buffers, set ready).
139 *
140 * Return value: 0 if successful, an error code otherwise.
141 **/
142 int TICALL ticables_cable_reset(CableHandle* handle)
143 {
144 int ret = 0;
145
146 VALIDATE_HANDLE(handle);
147
148 const CableFncts * cable = handle->cable;
149 VALIDATE_CABLEFNCTS(cable);
150
151 RETURN_IF_HANDLE_NOT_OPEN(handle);
152 RETURN_IF_HANDLE_BUSY(handle);
153
154 ret = ticables_event_send_simple(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_RESET, /* retval */ 0);
155
156 if (!ret)
157 {
158 handle->busy = 1;
159 if (cable->reset)
160 {
161 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_RESET);
162 if (!ret)
163 {
164 ret = cable->reset(handle);
165 }
166 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_RESET);
167 }
168 handle->busy = 0;
169 }
170
171 ret = ticables_event_send_simple(handle, /* type */ CABLE_EVENT_TYPE_AFTER_RESET, /* retval */ ret);
172
173 return ret;
174 }
175
176 /**
177 * ticables_cable_probe:
178 * @handle: a previously allocated handle
179 * @result: cable found (!0) or not (0)
180 *
181 * Attempt to probe if a cable is present. Open device if not opened.
182 *
183 * Return value: 0 if successful, an error code otherwise.
184 **/
185 int TICALL ticables_cable_probe(CableHandle* handle, int* result)
186 {
187 int opened;
188 int ret = 0;
189
190 VALIDATE_HANDLE(handle);
191
192 const CableFncts * cable = handle->cable;
193 VALIDATE_CABLEFNCTS(cable);
194
195 opened = handle->open;
196
197 // Check if device is already opened
198 if (!opened && cable->need_open)
199 {
200 ret = ticables_cable_open(handle);
201 }
202 else if (!opened && !cable->need_open)
203 {
204 if (cable->prepare)
205 {
206 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_PREPARE);
207 if (!ret)
208 {
209 ret = cable->prepare(handle);
210 }
211 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_PREPARE);
212 }
213 }
214
215 if (!ret)
216 {
217 // Do the check itself
218 if (cable->probe)
219 {
220 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_PROBE);
221 if (!ret)
222 {
223 ret = cable->probe(handle);
224 }
225 ret = ticables_event_send_simple_generic(handle, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_PROBE);
226 if (result != NULL)
227 {
228 *result = !ret;
229 }
230 else
231 {
232 ticables_critical("%s: result is NULL", __FUNCTION__);
233 }
234
235 // If it was opened for this, close it
236 if (!opened && cable->need_open)
237 {
238 ret = ticables_cable_close(handle);
239 }
240 else if (!opened && !cable->need_open)
241 {
242 free(handle->device); handle->device = NULL;
243 free(handle->priv2); handle->priv2 = NULL;
244 }
245 }
246 }
247
248 return ret;
249 }
250
251 /**
252 * ticables_cable_send:
253 * @handle: a previously allocated handle
254 * @data: buffer with data to send
255 * @len: length of buffer
256 *
257 * Send %len bytes of the %data buffer from PC to hand-held.
258 *
259 * Return value: 0 if successful, an error code otherwise.
260 **/
261 int TICALL ticables_cable_send(CableHandle* handle, uint8_t *data, uint32_t len)
262 {
263 int ret = 0;
264 CableEventData event;
265
266 VALIDATE_HANDLE(handle);
267
268 const CableFncts * cable = handle->cable;
269 VALIDATE_CABLEFNCTS(cable);
270
271 RETURN_IF_HANDLE_NOT_OPEN(handle);
272 RETURN_IF_HANDLE_BUSY(handle);
273
274 if (!len)
275 {
276 // Complain loudly, but don't return 0 immediately in such a case.
277 // Indeed, the DUSB file transfer code of the 84+(SE) & 89T wants writes
278 // of length 0 after writes of length 64, otherwise transfer hangs...
279 //
280 // The workaround for these models used to be done purely in libticables,
281 // but doing so broke ROM dumping... so the problem is probably not
282 // intrinsic to the USB controller and low-level USB stack.
283 ticables_critical("ticables_cable_send: len = 0\n");
284 }
285
286 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_SEND, /* retval */ 0, /* operation */ CABLE_FNCT_LAST);
287 ticables_event_fill_data(&event, /* data */ data, /* len */ len);
288 ret = ticables_event_send(handle, &event);
289
290 if (!ret)
291 {
292 handle->busy = 1;
293 if (data != NULL)
294 {
295 handle->rate.count += len;
296 if (cable->send)
297 {
298 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_SEND);
299 ticables_event_fill_data(&event, /* data */ data, /* len */ len);
300 ret = ticables_event_send(handle, &event);
301 if (!ret)
302 {
303 ret = cable->send(handle, data, len);
304 }
305 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_SEND);
306 ticables_event_fill_data(&event, /* data */ data, /* len */ len);
307 ret = ticables_event_send(handle, &event);
308 }
309 }
310 else
311 {
312 ticables_critical("%s: data is NULL", __FUNCTION__);
313 }
314 handle->busy = 0;
315 }
316
317 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_SEND, /* retval */ ret, /* operation */ CABLE_FNCT_LAST);
318 ticables_event_fill_data(&event, /* data */ data, /* len */ len);
319 ret = ticables_event_send(handle, &event);
320
321 return ret;
322 }
323
324 /**
325 * ticables_cable_recv:
326 * @handle: a previously allocated handle
327 * @data: buffer where data can be placed
328 * @len: number of bytes requested
329 *
330 * Attempt to receive %len bytes from hand-held to PC.
331 *
332 * Return value: 0 if successful, an error code otherwise.
333 **/
334 int TICALL ticables_cable_recv(CableHandle* handle, uint8_t *data, uint32_t len)
335 {
336 int ret = 0;
337 CableEventData event;
338
339 VALIDATE_HANDLE(handle);
340
341 const CableFncts * cable = handle->cable;
342 VALIDATE_CABLEFNCTS(cable);
343
344 RETURN_IF_HANDLE_NOT_OPEN(handle);
345 RETURN_IF_HANDLE_BUSY(handle);
346
347 if (!len)
348 {
349 // See ticables_cable_send above.
350 ticables_critical("ticables_cable_recv: len = 0\n");
351 }
352
353 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_RECV, /* retval */ 0, /* operation */ CABLE_FNCT_LAST);
354 ticables_event_fill_data(&event, /* data */ data, /* len */ len);
355 ret = ticables_event_send(handle, &event);
356 if (!ret)
357 {
358 handle->busy = 1;
359 if (data != NULL)
360 {
361 handle->rate.count += len;
362 if (cable->recv)
363 {
364 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_RECV);
365 ticables_event_fill_data(&event, /* data */ data, /* len */ len);
366 ret = ticables_event_send(handle, &event);
367 if (!ret)
368 {
369 ret = cable->recv(handle, data, len);
370 }
371 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_RECV);
372 ticables_event_fill_data(&event, /* data */ data, /* len */ len);
373 ret = ticables_event_send(handle, &event);
374 }
375 }
376 else
377 {
378 ticables_critical("%s: data is NULL", __FUNCTION__);
379 }
380 handle->busy = 0;
381 }
382
383 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_RECV, /* retval */ ret, /* operation */ CABLE_FNCT_LAST);
384 ticables_event_fill_data(&event, /* data */ data, /* len */ len);
385 ret = ticables_event_send(handle, &event);
386
387 return ret;
388 }
389
390 /**
391 * ticables_cable_check:
392 * @handle: a previously allocated handle
393 * @status: status is placed here
394 *
395 * Check for link cable status
396 *
397 * Return value: 0 if successful, an error code otherwise.
398 **/
399 int TICALL ticables_cable_check(CableHandle* handle, CableStatus *status)
400 {
401 int ret = 0;
402
403 VALIDATE_HANDLE(handle);
404
405 const CableFncts * cable = handle->cable;
406 VALIDATE_CABLEFNCTS(cable);
407
408 RETURN_IF_HANDLE_NOT_OPEN(handle);
409 RETURN_IF_HANDLE_BUSY(handle);
410
411 handle->busy = 1;
412 if (status != NULL)
413 {
414 if (cable->check)
415 {
416 CableEventData event;
417 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_CHECK);
418 event.data.intval = 0;
419 ret = ticables_event_send(handle, &event);
420 if (!ret)
421 {
422 ret = cable->check(handle, (int *)status);
423 }
424 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_CHECK);
425 event.data.intval = *status;
426 ret = ticables_event_send(handle, &event);
427 }
428 }
429 else
430 {
431 ticables_critical("%s: status is NULL", __FUNCTION__);
432 }
433 handle->busy = 0;
434
435 return ret;
436 }
437
438 /**
439 * ticables_cable_set_d0:
440 * @handle: a previously allocated handle
441 * @state: logical state (0 or 1) of D0 wire.
442 *
443 * Set the electrical state of the D0 wire (if possible).
444 *
445 * Return value: 0 if successful, an error code otherwise.
446 **/
447 int TICALL ticables_cable_set_d0(CableHandle* handle, int state)
448 {
449 int ret = 0;
450
451 VALIDATE_HANDLE(handle);
452
453 const CableFncts * cable = handle->cable;
454 VALIDATE_CABLEFNCTS(cable);
455
456 RETURN_IF_HANDLE_NOT_OPEN(handle);
457 RETURN_IF_HANDLE_BUSY(handle);
458
459 handle->busy = 1;
460 if (cable->set_d0)
461 {
462 CableEventData event;
463 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_SET_D0);
464 event.data.intval = state;
465 ret = ticables_event_send(handle, &event);
466 if (!ret)
467 {
468 ret = cable->set_d0(handle, state);
469 }
470 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_SET_D0);
471 event.data.intval = state;
472 ret = ticables_event_send(handle, &event);
473 }
474 handle->busy = 0;
475
476 return ret;
477 }
478
479 /**
480 * ticables_cable_set_d1:
481 * @handle: a previously allocated handle
482 * @state: logical state (0 or 1) of D1 wire.
483 *
484 * Set the electrical state of the D1 wire (if possible).
485 *
486 * Return value: 0 if successful, an error code otherwise.
487 **/
488 int TICALL ticables_cable_set_d1(CableHandle* handle, int state)
489 {
490 int ret = 0;
491
492 VALIDATE_HANDLE(handle);
493
494 const CableFncts * cable = handle->cable;
495 VALIDATE_CABLEFNCTS(cable);
496
497 RETURN_IF_HANDLE_NOT_OPEN(handle);
498 RETURN_IF_HANDLE_BUSY(handle);
499
500 handle->busy = 1;
501 if (cable->set_d1)
502 {
503 CableEventData event;
504 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_SET_D1);
505 event.data.intval = state;
506 ret = ticables_event_send(handle, &event);
507 if (!ret)
508 {
509 ret = cable->set_d1(handle, state);
510 }
511 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_SET_D1);
512 event.data.intval = state;
513 ret = ticables_event_send(handle, &event);
514 }
515 handle->busy = 0;
516
517 return ret;
518 }
519
520 /**
521 * ticables_cable_get_d0:
522 * @handle: a previously allocated handle
523 *
524 * Get the electrical state of the D0 wire (if possible).
525 *
526 * Return value: 0 or 1.
527 **/
528 int TICALL ticables_cable_get_d0(CableHandle* handle)
529 {
530 int ret = 0;
531
532 VALIDATE_HANDLE(handle);
533
534 const CableFncts * cable = handle->cable;
535 VALIDATE_CABLEFNCTS(cable);
536
537 RETURN_IF_HANDLE_NOT_OPEN(handle);
538 RETURN_IF_HANDLE_BUSY(handle);
539
540 handle->busy = 1;
541 if (cable->get_d0)
542 {
543 CableEventData event;
544 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_GET_D0);
545 ret = ticables_event_send(handle, &event);
546 if (!ret)
547 {
548 ret = cable->get_d0(handle);
549 }
550 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_GET_D0);
551 ret = ticables_event_send(handle, &event);
552 }
553 handle->busy = 0;
554
555 return ret;
556 }
557
558 /**
559 * ticables_cable_get_d1:
560 * @handle: a previously allocated handle
561 *
562 * Get the electrical state of the D1 wire (if possible).
563 *
564 * Return value: 0 or 1.
565 **/
566 int TICALL ticables_cable_get_d1(CableHandle* handle)
567 {
568 int ret = 0;
569
570 VALIDATE_HANDLE(handle);
571
572 const CableFncts * cable = handle->cable;
573 VALIDATE_CABLEFNCTS(cable);
574
575 RETURN_IF_HANDLE_NOT_OPEN(handle);
576 RETURN_IF_HANDLE_BUSY(handle);
577
578 handle->busy = 1;
579 if (cable->get_d1)
580 {
581 CableEventData event;
582 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_GET_D1);
583 ret = ticables_event_send(handle, &event);
584 if (!ret)
585 {
586 ret = cable->get_d1(handle);
587 }
588 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_GET_D1);
589 ret = ticables_event_send(handle, &event);
590 }
591 handle->busy = 0;
592
593 return ret;
594 }
595
596 /**
597 * ticables_cable_set_raw:
598 * @handle: a previously allocated handle
599 * @state: bit mask of lines to pull low
600 *
601 * Set the raw cable state (if the hardware supports raw access.) An
602 * input of 3 is the default state; 2 means to pull the D0 ("red")
603 * line low; 1 means to pull the D1 ("white") line low; 0 means both
604 * (never used in the TI link protocol.)
605 *
606 * Return value: 0 if successful, an error code otherwise.
607 **/
608 int TICALL ticables_cable_set_raw(CableHandle* handle, int state)
609 {
610 int ret = 0;
611
612 VALIDATE_HANDLE(handle);
613
614 const CableFncts * cable = handle->cable;
615 VALIDATE_CABLEFNCTS(cable);
616
617 RETURN_IF_HANDLE_NOT_OPEN(handle);
618 RETURN_IF_HANDLE_BUSY(handle);
619 if (!cable->set_raw)
620 {
621 return ERR_RAW_IO_UNSUPPORTED;
622 }
623
624 handle->busy = 1;
625 if (cable->set_raw)
626 {
627 CableEventData event;
628 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_SET_RAW);
629 event.data.intval = state;
630 ret = ticables_event_send(handle, &event);
631 if (!ret)
632 {
633 ret = cable->set_raw(handle, state);
634 }
635 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_SET_RAW);
636 event.data.intval = state;
637 ret = ticables_event_send(handle, &event);
638 }
639 handle->busy = 0;
640
641 return ret;
642 }
643
644 /**
645 * ticables_cable_get_raw:
646 * @handle: a previously allocated handle
647 * @state: pointer to variable to store current state
648 *
649 * Get the raw cable state (if the hardware supports raw access.) A
650 * bit set in the output means the line is high (1 for the D0 or "red"
651 * line, 2 for the D1 or "white" line.)
652 *
653 * Return value: 0 if successful, an error code otherwise.
654 **/
655 int TICALL ticables_cable_get_raw(CableHandle* handle, int *state)
656 {
657 int ret = 0;
658
659 VALIDATE_HANDLE(handle);
660 VALIDATE_NONNULL(state);
661
662 const CableFncts * cable = handle->cable;
663 VALIDATE_CABLEFNCTS(cable);
664
665 RETURN_IF_HANDLE_NOT_OPEN(handle);
666 RETURN_IF_HANDLE_BUSY(handle);
667 if (!cable->get_raw)
668 {
669 return ERR_RAW_IO_UNSUPPORTED;
670 }
671
672 handle->busy = 1;
673 if (cable->get_raw)
674 {
675 CableEventData event;
676 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_GET_RAW);
677 event.data.intval = 0;
678 ret = ticables_event_send(handle, &event);
679 if (!ret)
680 {
681 ret = cable->get_raw(handle, state);
682 }
683 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_GET_RAW);
684 event.data.intval = *state;
685 ret = ticables_event_send(handle, &event);
686 }
687 handle->busy = 0;
688
689 return ret;
690 }
691
692 /**
693 * ticables_cable_set_device:
694 * @handle: a previously allocated handle
695 * @device: new device information
696 *
697 * Sets cable device if that makes sense.
698 *
699 * Return value: 0 if successful, an error code otherwise.
700 **/
701 int TICALL ticables_cable_set_device(CableHandle* handle, const char * device)
702 {
703 int ret = 0;
704
705 VALIDATE_HANDLE(handle);
706 VALIDATE_NONNULL(device);
707
708 const CableFncts * cable = handle->cable;
709 VALIDATE_CABLEFNCTS(cable);
710
711 RETURN_IF_HANDLE_OPEN(handle);
712 RETURN_IF_HANDLE_BUSY(handle);
713
714 handle->busy = 1;
715 if (cable->set_device)
716 {
717 CableEventData event;
718 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_SET_DEVICE);
719 event.data.cptrval = (const void *)device;
720 ret = ticables_event_send(handle, &event);
721 if (!ret)
722 {
723 ret = cable->set_device(handle, device);
724 }
725 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_SET_DEVICE);
726 event.data.cptrval = (const void *)device;
727 ret = ticables_event_send(handle, &event);
728 }
729 handle->busy = 0;
730
731 return ret;
732 }
733
734 /**
735 * ticables_cable_get_device_info:
736 * @handle: a previously allocated handle
737 * @info: pointer to structure to store device info
738 *
739 * Get the type of device on the other end of the cable, if this can
740 * be determined.
741 */
742 int TICALL ticables_cable_get_device_info(CableHandle *handle, CableDeviceInfo *info)
743 {
744 int ret = 0;
745
746 VALIDATE_HANDLE(handle);
747 VALIDATE_NONNULL(info);
748
749 const CableFncts * cable = handle->cable;
750 VALIDATE_CABLEFNCTS(cable);
751
752 RETURN_IF_HANDLE_NOT_OPEN(handle);
753 RETURN_IF_HANDLE_BUSY(handle);
754
755 handle->busy = 1;
756 if (cable->get_device_info)
757 {
758 CableEventData event;
759 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_BEFORE_GENERIC_OPERATION, /* retval */ 0, /* operation */ CABLE_FNCT_GET_DEVICE_INFO);
760 event.data.ptrval = (void *)info;
761 ret = ticables_event_send(handle, &event);
762 if (!ret)
763 {
764 ret = (cable->get_device_info)(handle, info);
765 }
766 ticables_event_fill_header(handle, &event, /* type */ CABLE_EVENT_TYPE_AFTER_GENERIC_OPERATION, /* retval */ ret, /* operation */ CABLE_FNCT_GET_DEVICE_INFO);
767 event.data.ptrval = (void *)info;
768 ret = ticables_event_send(handle, &event);
769 }
770 else
771 {
772 info->family = CABLE_FAMILY_DBUS;
773 info->variant = CABLE_VARIANT_UNKNOWN;
774 }
775 handle->busy = 0;
776
777 return ret;
778 }
779
780 /**
781 * ticables_cable_progress_reset:
782 * @handle: a previously allocated handle
783 *
784 * Reset byte counter and timer used for computing data rate.
785 *
786 * Return value: always 0.
787 **/
788 int TICALL ticables_progress_reset(CableHandle* handle)
789 {
790 VALIDATE_HANDLE(handle);
791
792 handle->rate.count = 0;
793 TO_START(handle->rate.start);
794
795 return 0;
796 }
797
798 /**
799 * ticables_cable_progress_get:
800 * @handle: a previously allocated handle
801 * @count: number of bytes transfered
802 * @msec: time needed for the operation
803 * @rate: data rate
804 *
805 * Returns information needed to compute the transfer rate of the link cable.
806 *
807 * Return value: always 0.
808 **/
809 int TICALL ticables_progress_get(CableHandle* handle, int* count, int* msec, float* rate)
810 {
811 VALIDATE_HANDLE(handle);
812
813 TO_START(handle->rate.current);
814
815 if (count)
816 {
817 *count = handle->rate.count;
818 }
819
820 if (msec)
821 {
822 *msec = handle->rate.current - handle->rate.start;
823 }
824
825 if (rate)
826 {
827 if (handle->rate.current > handle->rate.start)
828 *rate = (float)handle->rate.count / ((float)(handle->rate.current - handle->rate.start));
829 }
830
831 #if 0
832 if (handle->rate.current > handle->rate.start)
833 printf("<%u %u %u %u %f\n",
834 handle->rate.count, handle->rate.start, handle->rate.current,
835 handle->rate.current - handle->rate.start, (float)handle->rate.count / ((float)(handle->rate.current - handle->rate.start)));
836 #endif
837
838 return 0;
839 }
840
841 /**
842 * ticables_cable_put:
843 * @handle: a previously allocated handle
844 * @data: data to send
845 *
846 * Send one byte from PC to hand-held.
847 * Convenient function implemented for compatibility.
848 *
849 * Return value: 0 if successful, an error code otherwise.
850 **/
851 int TICALL ticables_cable_put(CableHandle* handle, uint8_t data)
852 {
853 return ticables_cable_send(handle, &data, 1);
854 }
855
856 /**
857 * ticables_cable_get:
858 * @handle: a previously allocated handle
859 * @data: data to receive
860 *
861 * Receive one byte from hand-held to PC.
862 * Convenient function implemented for compatibility.
863 *
864 * Return value: 0 if successful, an error code otherwise.
865 **/
866 int TICALL ticables_cable_get(CableHandle* handle, uint8_t *data)
867 {
868 return ticables_cable_recv(handle, data, 1);
869 }
870
871 /**
872 * ticables_cable_get_pre_send_hook:
873 *
874 * Get the current pre send hook function pointer.
875 *
876 * Return value: a function pointer.
877 */
878 ticables_pre_send_hook_type TICALL ticables_cable_get_pre_send_hook(CableHandle *handle)
879 {
880 ticables_critical("%s: deprecated function, does nothing anymore", __FUNCTION__);
881
882 return NULL;
883 }
884
885 /**
886 * ticables_cable_set_pre_send_hook:
887 * @hook: new pre send hook
888 *
889 * Set the current pre send hook function pointer.
890 *
891 * Return value: the previous pre send hook, so that the caller can use it to chain hooks.
892 */
893 ticables_pre_send_hook_type TICALL ticables_cable_set_pre_send_hook(CableHandle *handle, ticables_pre_send_hook_type hook)
894 {
895 ticables_critical("%s: deprecated function, does nothing anymore", __FUNCTION__);
896
897 return NULL;
898 }
899
900 /**
901 * ticables_cable_get_post_send_hook:
902 *
903 * Get the current post send hook function pointer.
904 *
905 * Return value: a function pointer.
906 */
907 ticables_post_send_hook_type TICALL ticables_cable_get_post_send_hook(CableHandle *handle)
908 {
909 ticables_critical("%s: deprecated function, does nothing anymore", __FUNCTION__);
910
911 return NULL;
912 }
913
914 /**
915 * ticables_cable_set_post_send_hook:
916 * @hook: new post send hook
917 *
918 * Set the current post send hook function pointer.
919 *
920 * Return value: the previous post send hook, so that the caller can use it to chain hooks.
921 */
922 ticables_post_send_hook_type TICALL ticables_cable_set_post_send_hook(CableHandle *handle, ticables_post_send_hook_type hook)
923 {
924 ticables_critical("%s: deprecated function, does nothing anymore", __FUNCTION__);
925
926 return NULL;
927 }
928
929 /**
930 * ticables_cable_get_pre_recv_hook:
931 *
932 * Get the current pre recv hook function pointer.
933 *
934 * Return value: a function pointer.
935 */
936 ticables_pre_recv_hook_type TICALL ticables_cable_get_pre_recv_hook(CableHandle *handle)
937 {
938 ticables_critical("%s: deprecated function, does nothing anymore", __FUNCTION__);
939
940 return NULL;
941 }
942
943 /**
944 * ticables_cable_set_pre_recv_hook:
945 * @hook: new pre recv hook
946 *
947 * Set the current pre recv hook function pointer.
948 *
949 * Return value: the previous pre recv hook, so that the caller can use it to chain hooks.
950 */
951 ticables_pre_recv_hook_type TICALL ticables_cable_set_pre_recv_hook(CableHandle *handle, ticables_pre_recv_hook_type hook)
952 {
953 ticables_critical("%s: deprecated function, does nothing anymore", __FUNCTION__);
954
955 return NULL;
956 }
957
958 /**
959 * ticables_cable_get_post_recv_hook:
960 *
961 * Get the current post recv hook function pointer.
962 *
963 * Return value: a function pointer.
964 */
965 ticables_post_recv_hook_type TICALL ticables_cable_get_post_recv_hook(CableHandle *handle)
966 {
967 ticables_critical("%s: deprecated function, does nothing anymore", __FUNCTION__);
968
969 return NULL;
970 }
971
972 /**
973 * ticables_cable_set_post_recv_hook:
974 * @hook: new post recv hook
975 *
976 * Set the current post recv hook function pointer.
977 *
978 * Return value: the previous post recv hook, so that the caller can use it to chain hooks.
979 */
980 ticables_post_recv_hook_type TICALL ticables_cable_set_post_recv_hook(CableHandle *handle, ticables_post_recv_hook_type hook)
981 {
982 ticables_critical("%s: deprecated function, does nothing anymore", __FUNCTION__);
983
984 return NULL;
985 }
986
987 /**
988 * ticables_cable_get_event_hook:
989 *
990 * Get the current event hook function pointer.
991 *
992 * Return value: a function pointer.
993 */
994 ticables_event_hook_type TICALL ticables_cable_get_event_hook(CableHandle *handle)
995 {
996 if (!ticables_validate_handle(handle))
997 {
998 ticables_critical("%s: handle is NULL", __FUNCTION__);
999 return NULL;
1000 }
1001
1002 return handle->event_hook;
1003 }
1004
1005 /**
1006 * ticables_cable_set_post_recv_hook:
1007 * @hook: new post recv hook
1008 *
1009 * Set the current post recv hook function pointer.
1010 *
1011 * Return value: the previous post recv hook, so that the caller can use it to chain hooks.
1012 */
1013 ticables_event_hook_type TICALL ticables_cable_set_event_hook(CableHandle *handle, ticables_event_hook_type hook)
1014 {
1015 ticables_event_hook_type old_hook;
1016
1017 if (!ticables_validate_handle(handle))
1018 {
1019 ticables_critical("%s: handle is NULL", __FUNCTION__);
1020 return NULL;
1021 }
1022
1023 old_hook = handle->event_hook;
1024 handle->event_hook = hook;
1025
1026 return old_hook;
1027 }
1028
1029 /**
1030 * ticables_cable_get_event_user_pointer:
1031 *
1032 * Set the current post recv hook function pointer.
1033 *
1034 * Return value: the previous post recv hook, so that the caller can use it to chain hooks.
1035 */
1036 void * ticables_cable_get_event_user_pointer(CableHandle *handle)
1037 {
1038 if (!ticables_validate_handle(handle))
1039 {
1040 ticables_critical("%s: handle is NULL", __FUNCTION__);
1041 return NULL;
1042 }
1043
1044 return handle->user_pointer;
1045 }
1046
1047 /**
1048 * ticables_cable_set_event_user_pointer:
1049 * @user_pointer: new user pointer
1050 *
1051 * Set the current post recv hook function pointer.
1052 *
1053 * Return value: the previous post recv hook, so that the caller can use it to chain hooks.
1054 */
1055 void * ticables_cable_set_event_user_pointer(CableHandle *handle, void * user_pointer)
1056 {
1057 void * old_pointer;
1058
1059 if (!ticables_validate_handle(handle))
1060 {
1061 ticables_critical("%s: handle is NULL", __FUNCTION__);
1062 return NULL;
1063 }
1064
1065 old_pointer = handle->user_pointer;
1066 handle->user_pointer = user_pointer;
1067
1068 return old_pointer;
1069 }
1070
1071 /**
1072 * ticables_cable_get_event_count:
1073 *
1074 * Get the current event count since the handle was initialized.
1075 *
1076 * Return value: an unsigned integer.
1077 */
1078 uint32_t TICALL ticables_cable_get_event_count(CableHandle *handle)
1079 {
1080 if (!ticables_validate_handle(handle))
1081 {
1082 ticables_critical("%s: handle is NULL", __FUNCTION__);
1083 return 0;
1084 }
1085
1086 return handle->event_count;
1087 }
1088
1089 /**
1090 * ticables_cable_fire_user_event:
1091 * @handle: a previously allocated handle.
1092 * @type: event type.
1093 * @user_data: user-specified data.
1094 * @user_len: user-specified length.
1095 *
1096 * Fire a user-specified event to the registered event hook function, if any.
1097 *
1098 * Return value: 0 if successful, an error code otherwise.
1099 */
1100 int TICALL ticables_cable_fire_user_event(CableHandle *handle, CableEventType type, int retval, void * user_data, uint32_t user_len)
1101 {
1102 int ret = 0;
1103
1104 VALIDATE_HANDLE(handle);
1105
1106 if (handle->event_hook && type >= CABLE_EVENT_TYPE_USER)
1107 {
1108 CableEventData event;
1109 ticables_event_fill_header(handle, &event, /* type */ type, /* retval */ retval, /* operation */ CABLE_FNCT_LAST);
1110 event.data.user_data.data = (uint8_t *)user_data;
1111 event.data.user_data.len = user_len;
1112 handle->event_count++;
1113 ret = handle->event_hook(handle, handle->event_count, &event, handle->user_pointer);
1114 }
1115
1116 return ret;
1117 }