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 #ifndef __TICABLES__
23 #define __TICABLES__
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include "stdints1.h"
30 #include "export1.h"
31 #include "timeout.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 	/***********************/
38 	/* Types & Definitions */
39 	/***********************/
40 
41 /* Versioning */
42 
43 #ifdef __WIN32__
44 # define libticables2_VERSION "1.3.5"
45 #else
46 # define libticables2_VERSION VERSION
47 #endif
48 
49 /* Types */
50 
51 #define DFLT_TIMEOUT  15	/* 1.5 second */
52 #define DFLT_DELAY    10	/* 10 micro-seconds */
53 
54 #define ERROR_READ_TIMEOUT	4	/* fixed in error.h */
55 #define ERROR_WRITE_TIMEOUT	6	/* fixed in error.h */
56 
57 /**
58  * CableModel:
59  *
60  * An enumeration which contains the following cable types:
61  **/
62 typedef enum
63 {
64 	CABLE_NUL = 0,
65 	CABLE_GRY, CABLE_BLK, CABLE_PAR, CABLE_SLV, CABLE_USB,
66 	CABLE_VTI, CABLE_TIE, CABLE_ILP, CABLE_DEV,
67 	CABLE_TCPC, CABLE_TCPS,
68 	CABLE_MAX
69 } CableModel;
70 
71 /**
72  * CablePort:
73  *
74  * An enumeration which contains the following ports:
75  **/
76 typedef enum
77 {
78 	PORT_0 = 0,
79 	PORT_1, PORT_2, PORT_3, PORT_4, PORT_MAX
80 } CablePort;
81 
82 /**
83  * UsbPid:
84  *
85  * An enumeration which contains the following devices:
86  **/
87 typedef enum
88 {
89 	PID_UNKNOWN		= 0,
90 	PID_TIGLUSB     = 0xE001,
91 	PID_TI89TM		= 0xE004,
92 	PID_TI84P		= 0xE003,
93 	PID_TI84P_SE    = 0xE008,
94 	PID_NSPIRE      = 0xE012,
95 } UsbPid;
96 
97 /**
98  * CableStatus:
99  *
100  * An enumeration which contains the following values:
101  **/
102 typedef enum
103 {
104 	STATUS_NONE = 0,
105 	STATUS_RX = 1,
106 	STATUS_TX = 2,
107 } CableStatus;
108 
109 /**
110  * ProbingMethod:
111  *
112  * Defines how to probe cables:
113  **/
114 typedef enum
115 {
116 	PROBE_NONE	= 0,
117 	PROBE_FIRST = 1,
118 	PROBE_USB	= 2,
119 	PROBE_DBUS	= 4,
120 	PROBE_ALL	= 6,
121 } ProbingMethod;
122 
123 /**
124  * CableFamily:
125  *
126  * Defines the various types of devices supported and which can be
127  * detected automatically; each family is generally incompatible with
128  * the others in terms of protocol, file formats, or both.
129  */
130 typedef enum
131 {
132 	CABLE_FAMILY_UNKNOWN = 0,
133 	CABLE_FAMILY_DBUS,          /* Traditional TI link protocol */
134 	CABLE_FAMILY_USB_TI8X,      /* Direct USB for TI-84 Plus, CSE, CE, etc. */
135 	CABLE_FAMILY_USB_TI9X,      /* Direct USB for TI-89 Titanium */
136 	CABLE_FAMILY_USB_NSPIRE     /* Direct USB for TI-Nspire series */
137 } CableFamily;
138 
139 /**
140  * CableVariant:
141  *
142  * Defines the various sub-types (for a given CableFamily) that are
143  * supported and can be detected automatically.
144  */
145 typedef enum
146 {
147 	CABLE_VARIANT_UNKNOWN = 0,
148 	CABLE_VARIANT_TIGLUSB,      /* CABLE_FAMILY_DBUS */
149 	CABLE_VARIANT_TI84P,        /* CABLE_FAMILY_USB_TI8X */
150 	CABLE_VARIANT_TI84PSE,      /* CABLE_FAMILY_USB_TI8X */
151 	CABLE_VARIANT_TI84PCSE,     /* CABLE_FAMILY_USB_TI8X */
152 	CABLE_VARIANT_TI84PCE,      /* CABLE_FAMILY_USB_TI8X */
153 	CABLE_VARIANT_TI83PCE,      /* CABLE_FAMILY_USB_TI8X */
154 	CABLE_VARIANT_TI82A,        /* CABLE_FAMILY_USB_TI8X */
155 	CABLE_VARIANT_TI89TM,       /* CABLE_FAMILY_USB_TI9X */
156 	CABLE_VARIANT_NSPIRE,       /* CABLE_FAMILY_USB_NSPIRE */
157 	CABLE_VARIANT_TI84PT        /* CABLE_FAMILY_USB_TI8X */
158 } CableVariant;
159 
160 /**
161  * CableDeviceInfo:
162  * @family: calculator family
163  * @variant: calculator variant
164  *
165  * Information returned for each cable by ticables_get_usb_device_info.
166  */
167 typedef struct
168 {
169 	CableFamily     family;
170 	CableVariant    variant;
171 } CableDeviceInfo;
172 
173 /**
174  * DataRate:
175  * @count: number of bytes transferred
176  * @start: the time when transfer started
177  * @current: free of use
178  * @stop: the time when transfer finished
179  *
180  * A structure used for benchmarks.
181  * !!! This structure is for private use !!!
182  **/
183 typedef struct
184 {
185 	int		count;
186 	tiTIME	start;
187 	tiTIME	current;
188 	tiTIME	stop;
189 } DataRate;
190 
191 typedef struct _CableFncts	 CableFncts;
192 typedef struct _CableHandle  CableHandle;
193 
194 /**
195  * Cable:
196  * @model: link cable model (CableModel).
197  * @name: name of cable like "SER"
198  * @fullname: complete name of cable like "BlackLink"
199  * @description: description of cable like "serial cable"
200  * @need_open: set if cable need to be 'open'/'close' before calling 'probe'
201  * @prepare: detect and map I/O
202  * @probe: check for cable presence
203  * @timeout: used to update timeout
204  * @open: open I/O port or device
205  * @close: close I/O port or device
206  * @reset: reset I/O port or device
207  * @send: send data onto the cable
208  * @recv: recv data from cable
209  * @check: check for data arrival
210  * @set_d0: set D0/red wire
211  * @set_d1: set D1/white wire
212  * @get_d0 get D0/red wire
213  * @get_d1 set D1/red wire
214  * @set_raw: set both wires
215  * @get_raw: read both wires
216  *
217  * A structure used for handling a link cable.
218  * !!! This structure is for private use !!!
219  **/
220 struct _CableFncts
221 {
222 	const int		model;
223 	const char*		name;
224 	const char*		fullname;
225 	const char*		description;
226 
227 	const int		need_open;
228 
229 	int (*prepare)	(CableHandle *);
230 
231 	int (*open)		(CableHandle *);
232 	int (*close)	(CableHandle *);
233 	int (*reset)	(CableHandle *);
234 	int (*probe)	(CableHandle *);
235 	int (*timeout)	(CableHandle *);
236 
237 	int (*send)		(CableHandle *, uint8_t *, uint32_t);
238 	int (*recv)		(CableHandle *, uint8_t *, uint32_t);
239 	int (*check)	(CableHandle *, int *);
240 
241 	int (*set_d0)	(CableHandle *, int);
242 	int (*set_d1)	(CableHandle *, int);
243 	int (*get_d0)	(CableHandle *);
244 	int (*get_d1)	(CableHandle *);
245 	int (*set_raw)  (CableHandle *, int);
246 	int (*get_raw)  (CableHandle *, int *);
247 	int (*set_device) (CableHandle*, const char*);
248 	int (*get_device_info) (CableHandle *, CableDeviceInfo *);
249 };
250 
251 typedef int (*ticables_pre_send_hook_type)(CableHandle * handle, uint8_t * data, uint32_t len);
252 typedef int (*ticables_post_send_hook_type)(CableHandle * handle, uint8_t * data, uint32_t len, int retval);
253 typedef int (*ticables_pre_recv_hook_type)(CableHandle * handle, uint8_t * data, uint32_t len);
254 typedef int (*ticables_post_recv_hook_type)(CableHandle * handle, uint8_t * data, uint32_t len, int retval);
255 
256 /**
257  * CableHandle:
258  * @model: cable model
259  * @port: generic port
260  * @timeout: timeout value in 0.1 seconds
261  * @delay: inter-bit delay for serial/parallel cable in us
262  * @device: device name like COMx or ttySx (if used)
263  * @address: I/O base address of device (if used)
264  * @cable: a Cable structure used by this handle
265  * @rate: data rate during transfers
266  * @priv: opaque data for internal/private use (static)
267  * @priv2: idem (allocated)
268  * @priv3: idem (static)
269  * @open: set if cable has been open
270  * @busy: set if cable is busy
271  * @pre_send_hook: callback fired before sending a block of data
272  * @post_send_hook: callback fired after sending a block of data.
273  * @pre_recv_hook: callback fired before receiving a block of data
274  * @post_recv_hook: callback fired after receiving a block of data.
275  *
276  * A structure used to store information as an handle.
277  * !!! This structure is for private use !!!
278  **/
279 struct _CableHandle
280 {
281 	CableModel		model;
282 	CablePort		port;
283 	unsigned int	timeout;
284 	unsigned int	delay;
285 
286 	char *			device;
287 	unsigned int	address;
288 
289 	CableFncts*		cable;
290 	DataRate		rate;
291 
292 	void*			priv;
293 	void*			priv2;
294 	void*			priv3;
295 
296 	int				open;
297 	int				busy;
298 
299 	ticables_pre_send_hook_type pre_send_hook;
300 	ticables_post_send_hook_type post_send_hook;
301 	ticables_pre_recv_hook_type pre_recv_hook;
302 	ticables_post_recv_hook_type post_recv_hook;
303 };
304 
305 /**
306  * CableOptions:
307  * @cable_model: model
308  * @cable_port: port
309  * @cable_timeout: timeout in tenth of seconds
310  * @cable_delay: inter-bit delay in us
311  * @calc_model: calculator model
312  *
313  * A convenient structure free of use by the user.
314  **/
315 typedef struct
316 {
317 	CableModel      model;
318 	CablePort       port;
319 	unsigned int    timeout;
320 	unsigned int    delay;
321 
322 	int             calc; // unused
323 } CableOptions;
324 
325 // namespace scheme: library_class_function like ticables_fext_get
326 
327 	/****************/
328 	/* Entry points */
329 	/****************/
330 
331 	TIEXPORT1 int TICALL ticables_library_init(void);
332 	TIEXPORT1 int TICALL ticables_library_exit(void);
333 
334 	/*********************/
335 	/* General functions */
336 	/*********************/
337 
338 	// ticables.c
339 	TIEXPORT1 const char * TICALL ticables_version_get(void);
340 	TIEXPORT1 uint32_t TICALL ticables_supported_cables(void);
341 
342 	TIEXPORT1 CableHandle * TICALL ticables_handle_new(CableModel model, CablePort port);
343 	TIEXPORT1 int        TICALL ticables_handle_del(CableHandle *handle);
344 
345 	TIEXPORT1 unsigned int TICALL ticables_options_set_timeout(CableHandle *handle, unsigned int timeout);
346 	TIEXPORT1 unsigned int TICALL ticables_options_set_delay(CableHandle *handle, unsigned int delay);
347 
348 	TIEXPORT1 CableModel TICALL ticables_get_model(CableHandle *handle);
349 	TIEXPORT1 CablePort TICALL ticables_get_port(CableHandle *handle);
350 	TIEXPORT1 const char * TICALL ticables_get_device(CableHandle *handle);
351 	TIEXPORT1 unsigned int TICALL ticables_get_timeout(CableHandle *handle);
352 	TIEXPORT1 unsigned int TICALL ticables_get_delay(CableHandle *handle);
353 
354 	TIEXPORT1 int TICALL ticables_handle_show(CableHandle *handle);
355 
356 	// error.c
357 	TIEXPORT1 int         TICALL ticables_error_get(int number, char **message);
358 	TIEXPORT1 int         TICALL ticables_error_free(char *message);
359 
360 	// link_xxx.c
361 	TIEXPORT1 int TICALL ticables_cable_open(CableHandle *handle);
362 	TIEXPORT1 int TICALL ticables_cable_close(CableHandle *handle);
363 
364 	TIEXPORT1 int TICALL ticables_cable_reset(CableHandle *handle);
365 	TIEXPORT1 int TICALL ticables_cable_probe(CableHandle *handle, int *result);
366 
367 	TIEXPORT1 int TICALL ticables_cable_send(CableHandle *handle, uint8_t *data, uint32_t len);
368 	TIEXPORT1 int TICALL ticables_cable_recv(CableHandle *handle, uint8_t *data, uint32_t len);
369 
370 	TIEXPORT1 int TICALL ticables_cable_check(CableHandle *handle, CableStatus *status);
371 
372 	TIEXPORT1 int TICALL ticables_cable_set_d0(CableHandle *handle, int state);
373 	TIEXPORT1 int TICALL ticables_cable_set_d1(CableHandle *handle, int state);
374 
375 	TIEXPORT1 int TICALL ticables_cable_get_d0(CableHandle *handle);
376 	TIEXPORT1 int TICALL ticables_cable_get_d1(CableHandle *handle);
377 
378 	TIEXPORT1 int TICALL ticables_cable_set_raw(CableHandle *handle, int state);
379 	TIEXPORT1 int TICALL ticables_cable_get_raw(CableHandle *handle, int *state);
380 
381 	TIEXPORT1 int TICALL ticables_cable_set_device(CableHandle *handle, const char *device);
382 
383 	TIEXPORT1 int TICALL ticables_cable_get_device_info(CableHandle *handle, CableDeviceInfo *info);
384 
385 	TIEXPORT1 int TICALL ticables_progress_reset(CableHandle *handle);
386 	TIEXPORT1 int TICALL ticables_progress_get(CableHandle *handle, int *count, int *msec, float *rate);
387 
388 	TIEXPORT1 int TICALL ticables_cable_put(CableHandle *handle, uint8_t data);
389 	TIEXPORT1 int TICALL ticables_cable_get(CableHandle *handle, uint8_t *data);
390 
391 	TIEXPORT1 ticables_pre_send_hook_type TICALL ticables_cable_get_pre_send_hook(CableHandle *handle);
392 	TIEXPORT1 ticables_pre_send_hook_type TICALL ticables_cable_set_pre_send_hook(CableHandle *handle, ticables_pre_send_hook_type hook);
393 	TIEXPORT1 ticables_post_send_hook_type TICALL ticables_cable_get_post_send_hook(CableHandle *handle);
394 	TIEXPORT1 ticables_post_send_hook_type TICALL ticables_cable_set_post_send_hook(CableHandle *handle, ticables_post_send_hook_type hook);
395 
396 	TIEXPORT1 ticables_pre_recv_hook_type TICALL ticables_cable_get_pre_recv_hook(CableHandle *handle);
397 	TIEXPORT1 ticables_pre_recv_hook_type TICALL ticables_cable_set_pre_recv_hook(CableHandle *handle, ticables_pre_recv_hook_type hook);
398 	TIEXPORT1 ticables_post_recv_hook_type TICALL ticables_cable_get_post_recv_hook(CableHandle *handle);
399 	TIEXPORT1 ticables_post_recv_hook_type TICALL ticables_cable_set_post_recv_hook(CableHandle *handle, ticables_post_recv_hook_type hook);
400 
401 	// type2str.c
402 	TIEXPORT1 const char * TICALL ticables_model_to_string(CableModel model);
403 	TIEXPORT1 CableModel   TICALL ticables_string_to_model (const char *str);
404 
405 	TIEXPORT1 const char * TICALL ticables_port_to_string(CablePort port);
406 	TIEXPORT1 CablePort    TICALL ticables_string_to_port(const char *str);
407 
408 	TIEXPORT1 const char * TICALL ticables_usbpid_to_string(UsbPid pid);
409 	TIEXPORT1 UsbPid       TICALL ticables_string_to_usbpid(const char *str);
410 
411 	// probe.c
412 	TIEXPORT1 int TICALL ticables_probing_do(int ***result, unsigned int timeout, ProbingMethod method);
413 	TIEXPORT1 int TICALL ticables_probing_finish(int ***result);
414 	TIEXPORT1 void TICALL ticables_probing_show(int **array);
415 
416 	TIEXPORT1 int TICALL ticables_is_usb_enabled(void);
417 
418 	TIEXPORT1 int TICALL ticables_get_usb_device_info(CableDeviceInfo **array, int *len);
419 	TIEXPORT1 int TICALL ticables_free_usb_device_info(CableDeviceInfo *array);
420 
421 	TIEXPORT1 int TICALL ticables_get_usb_devices(int **array, int *len);
422 	TIEXPORT1 int TICALL ticables_free_usb_devices(int *array);
423 
424 	/************************/
425 	/* Deprecated functions */
426 	/************************/
427 
428 #ifdef __cplusplus
429 }
430 #endif
431 
432 #endif
433