53
61
#undef QUIET_MEMCHECKERS /* #define to memset coms read buffers before reading */
55
/* Provide some backwards compatibility to libusb0 */
57
# define USB_ENDPOINT_IN LIBUSB_ENDPOINT_IN
58
# define USB_ENDPOINT_OUT LIBUSB_ENDPOINT_OUT
59
# define USB_RECIP_DEVICE LIBUSB_RECIPIENT_DEVICE
60
# define USB_RECIP_INTERFACE LIBUSB_RECIPIENT_INTERFACE
61
# define USB_RECIP_ENDPOINT LIBUSB_RECIPIENT_ENDPOINT
62
# define USB_TYPE_STANDARD LIBUSB_REQUEST_TYPE_STANDARD
63
# define USB_TYPE_CLASS LIBUSB_REQUEST_TYPE_CLASS
64
# define USB_TYPE_VENDOR LIBUSB_REQUEST_TYPE_VENDOR
65
# define USB_ENDPOINT_TYPE_MASK LIBUSB_TRANSFER_TYPE_MASK
67
#define usb_device libusb_device
68
#define usb_device_descriptor libusb_device_descriptor
69
#define usb_dev_handle libusb_device_handle
70
#define usb_config_descriptor libusb_config_descriptor
71
#define usb_strerror libusb_strerror
63
typedef struct _icompath icompath;
64
typedef struct _icompaths icompaths;
65
typedef struct _icoms icoms;
69
struct usb_idevice; /* Forward declarations */
72
/* Information about each end point */
74
int valid; /* Flag, nz if this endpoint is valid */
75
int addr; /* Address of end point */
76
int packetsize; /* The max packet size */
77
int type; /* 2 = bulk, 3 = interrupt */
78
int interface; /* interface number */
79
int pipe; /* pipe number (1..N, OS X only) */
82
#define ICOM_EP_TYPE_BULK 2
83
#define ICOM_EP_TYPE_INTERRUPT 3
88
#endif /* ENABLE_USB */
90
/* - - - - - - - - - - - - - - - - - - - - */
92
/* Store information about a possible instrument communication path */
93
/* (Note a path doesn't have a reference to icompaths or its' log) */
95
char *name; /* instance description */
97
char *spath; /* Serial device path */
100
int nep; /* Number of end points */
101
unsigned int vid, pid; /* USB vendor and product id's */
102
struct usb_idevice *usbd; /* USB port, NULL if not USB */
103
struct hid_idevice *hidd; /* HID port, NULL if not HID */
104
#endif /* ENABLE_USB */
105
instType itype; /* Type of instrument if known */
108
extern icompath icomFakeDevice; /* Declare fake device */
110
/* The available instrument communication paths */
112
int npaths; /* Number of paths */
113
icompath **paths; /* Paths if any */
114
a1log *log; /* Verbose, debuge & error logging */
116
/* Re-populate the available instrument list */
117
/* return icom error */
118
int (*refresh)(struct _icompaths *p);
121
/* Add a serial path. path is copied. Return icom error */
122
int (*add_serial)(struct _icompaths *p, char *name, char *spath);
123
#endif /* ENABLE_SERIAL */
126
/* Add a usb path. usbd is taken, others are copied. Return icom error */
127
int (*add_usb)(struct _icompaths *p, char *name, unsigned int vid, unsigned int pid,
128
int nep, struct usb_idevice *usbd, instType itype);
130
/* Add an hid path. hidd is taken, others are copied. Return icom error */
131
int (*add_hid)(struct _icompaths *p, char *name, unsigned int vid, unsigned int pid,
132
int nep, struct hid_idevice *hidd, instType itype);
133
#endif /* ENABLE_USB */
135
/* Return the path corresponding to the port number, or NULL if out of range */
136
icompath *(*get_path)(
137
struct _icompaths *p,
138
int port); /* Enumerated port number, 1..n */
140
/* Clear all the paths */
141
void (*clear)(struct _icompaths *p);
144
void (*del)(struct _icompaths *p);
148
/* Allocate an icom paths and set it to the list of available devices */
149
/* Return NULL on error */
150
icompaths *new_icompaths(a1log *log);
74
153
/* - - - - - - - - - - - */
75
154
/* Serial related stuff */
142
221
/* Status bits/return values */
143
#define ICOM_OK 0x00000 /* No error */
145
#define ICOM_USER 0x10000 /* User abort operation */
146
#define ICOM_TERM 0x20000 /* User terminated operation */
147
#define ICOM_TRIG 0x30000 /* User trigger */
148
#define ICOM_CMND 0x40000 /* User command */
149
#define ICOM_USERM 0xF0000 /* Mask of user interrupts */
151
#define ICOM_NOTS 0x01000 /* Not supported */
152
#define ICOM_TO 0x02000 /* Timed out */
153
#define ICOM_SHORT 0x04000 /* Number of bytes wasn't read/written */
155
#define ICOM_USBR 0x00100 /* Unspecified USB read error */
156
#define ICOM_USBW 0x00200 /* Unspecified USB write error */
157
#define ICOM_SERR 0x00400 /* Unspecified Serial read error */
158
#define ICOM_SERW 0x00800 /* Unspecified Serial write error */
160
#define ICOM_XRE 0x00040 /* Xmit shift reg empty */
161
#define ICOM_XHE 0x00020 /* Xmit hold reg empty */
162
#define ICOM_BRK 0x00010 /* Break detected */
163
#define ICOM_FER 0x00008 /* Framing error */
164
#define ICOM_PER 0x00004 /* Parity error */
165
#define ICOM_OER 0x00002 /* Overun error */
166
#define ICOM_DRY 0x00001 /* Recv data ready */
168
/* Store information about a possible instrument communication path */
170
char *path; /* Serial port path, or USB/HID device description */
172
unsigned int vid, pid; /* USB vendor and product id's */
173
struct usb_device *dev; /* USB port, NULL if not USB */
174
struct _hid_device *hev; /* HID port, NULL if not HID */
175
instType itype; /* Type of instrument on the USB or HID port */
176
#endif /* ENABLE_USB */
181
/* Information about each end point */
183
int valid; /* Flag, nz if this endpoint is valid */
184
int addr; /* Address of end point */
185
int packetsize; /* The max packet size */
186
int type; /* 2 = bulk, 3 = interrupt */
189
#define ICOM_EP_TYPE_BULK 2
190
#define ICOM_EP_TYPE_INTERRUPT 3
199
#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0501
200
# if defined(_WIN32_WINNT)
203
# define _WIN32_WINNT 0x0501
205
#define WIN32_LEAN_AND_MEAN
222
#define ICOM_OK 0x000000 /* No error */
224
#define ICOM_NOTS 0x001000 /* Not supported */
225
#define ICOM_SIZE 0x002000 /* Request/response size exceeded limits */
226
#define ICOM_TO 0x004000 /* Timed out */
227
#define ICOM_SHORT 0x008000 /* Number of bytes wasn't read/written */
228
#define ICOM_CANC 0x010000 /* Was cancelled */
229
#define ICOM_SYS 0x020000 /* System error (ie. malloc, system call fail) */
230
#define ICOM_VER 0x040000 /* Version error - need up to date kernel driver */
232
#define ICOM_USBR 0x000100 /* Unspecified USB read error */
233
#define ICOM_USBW 0x000200 /* Unspecified USB write error */
234
#define ICOM_SERR 0x000400 /* Unspecified Serial read error */
235
#define ICOM_SERW 0x000800 /* Unspecified Serial write error */
237
#define ICOM_XRE 0x000040 /* Xmit shift reg empty */
238
#define ICOM_XHE 0x000020 /* Xmit hold reg empty */
239
#define ICOM_BRK 0x000010 /* Break detected */
240
#define ICOM_FER 0x000008 /* Framing error */
241
#define ICOM_PER 0x000004 /* Parity error */
242
#define ICOM_OER 0x000002 /* Overun error */
243
#define ICOM_DRY 0x000001 /* Recv data ready */
247
/* Cancelation token. */
248
typedef struct _usb_cancelt usb_cancelt;
251
void usb_init_cancel(usb_cancelt *p);
252
void usb_uninit_cancel(usb_cancelt *p);
215
icompath *ppath; /* path to selected port */
259
/* Copy of some of icompath contents: */
260
char *name; /* Device description */
261
instType itype; /* Type of instrument if known */
217
int port; /* io port number: 1, 2, 3, 4 .. n */
218
263
int is_open; /* Flag, NZ if this port is open */
219
int is_usb; /* Flag, NZ if this is a USB port */
220
int is_hid; /* Flag, NZ if this is an HID port */
222
267
/* Serial port parameters */
268
char *spath; /* Serial port path */
223
269
#if defined (MSDOS)
224
270
unsigned short porta; /* Hardware port address */
284
#endif /* ENABLE_SERIAL */
238
286
#ifdef ENABLE_USB
239
288
/* USB port parameters */
243
unsigned int vid, pid; /* USB vendor and product id's */
244
struct usb_device *usbd;
289
struct usb_idevice *usbd; /* USB port - copy of ppath->usbd */
290
#ifndef NATIVE_USB /* Hmm. could put all this within usb_idevice if not #defined ? */
292
libusb_device_handle *usbh;
245
294
struct usb_dev_handle *usbh;
246
int cnfg; /* Configuration */
297
icomuflags uflags; /* Bug workaround flags */
299
unsigned int vid, pid; /* USB vendor and product id's, used to distiguish instruments */
300
int nconfig; /* Number of configurations */
301
int config; /* Config this info applies to (always 1 ?) */
302
int cconfig; /* Current configuration (0 or 1 ?) */
247
303
int nifce; /* Number of interfaces */
304
int nep; /* Number of end points */
248
305
int wr_ep, rd_ep; /* Default end points to use for "serial" read/write */
249
306
int rd_qa; /* Read quanta size */
251
308
usb_ep ep[32]; /* Information about each end point for general usb i/o */
252
icomuflags uflags; /* Bug workaround flags */
254
310
/* Macro to access end point information */
255
311
#define EPINFO(addr) ep[((addr >> 3) & 0x10) + (addr & 0x0f)]
257
313
/* HID port parameters */
258
struct _hid_device *hidd; /* HID port, NULL if not HID */
314
struct hid_idevice *hidd; /* HID port - copy of ppath->hidd */
260
316
#endif /* ENABLE_USB */
262
318
/* General parameters */
263
int lerr; /* Last error code */
319
int lserr; /* Last serial communication error code */
264
320
int tc; /* Current serial parser termination character (-1 if not set) */
265
int npaths; /* Number of paths */
266
icompath **paths; /* Paths if any */
267
int debug; /* Flag, nz to print instrument io info to stderr */
269
int uih[256]; /* User interrupt handling key table. Value can be: */
270
/* ICOM_OK, ICOM_USER, ICOM_TERM, ICOM_TRIG, ICOM_CMND */
271
int cut; /* The character that caused the termination */
321
a1log *log; /* Debug & Error log */
322
int debug; /* legacy - Flag, nz to print instrument io info to stderr */
273
324
/* Linked list to automate SIGKILL cleanup */
274
325
struct _icoms *next;
278
/* Return a list of instrument io communication paths */
279
/* Return NULL on failure. */
280
icompath ** (*get_paths)( /* Return pointer to list of icompath */
281
struct _icoms *p /* End is marked with NULL pointer, */
282
); /* Storage will be freed with icoms object */
284
329
/* Return the port type */
285
330
icom_type (*port_type)(struct _icoms *p);
287
/* Return the instrument type if the port number is USB, instUnknown if not */
288
instType (*is_usb_portno)(
290
int port); /* Enumerated port number, 1..n */
292
/* Return the instrument type if the port number is HID, */
293
/* and instUnknown if it is not. */
294
instType (*is_hid_portno)(
296
int port); /* Enumerated port number, 1..n */
298
333
/* Select the serial communications port and characteristics */
299
void (*set_ser_port)(
334
/* return icom error */
300
336
struct _icoms *p,
301
int port, /* Enumerated port number, 1..n */
302
337
flow_control fc, /* Flow control */
305
340
stop_bits stop_bits,
306
341
word_length word_length);
342
#endif /* ENABLE_SERIAL */
308
345
/* Select the USB communications port and characteristics */
309
void (*set_usb_port)(
346
/* return icom error */
310
348
struct _icoms *p,
311
int port, /* Enumerated port number, 1..n */
312
349
int config, /* Configuration */
313
350
int wr_ep, /* "serial" write end point */
314
351
int rd_ep, /* "serial" read end point */
320
357
/* Select the HID communications port and characteristics */
321
void (*set_hid_port)(
358
/* return icom error */
322
360
struct _icoms *p,
323
int port, /* Enumerated port number, 1..n */
324
361
icomuflags usbflags, /* Any special handling flags */
325
362
int retries, /* > 0 if we should retry set_configuration (100msec) */
326
363
char **pnames /* List of process names to try and kill before opening */
365
#endif /* ENABLE_USB */
329
367
/* Close the port */
330
368
void (*close_port)(struct _icoms *p);
332
/* Reset user interrupt handling to default (Esc, ^C, q or 'Q' = Abort) */
333
void (*reset_uih)(struct _icoms *p);
335
/* Set a key range to the given handling type */
336
/* min & max are between 0 and 255, status is one of */
337
/* ICOM_OK, ICOM_USER, ICOM_TERM, ICOM_TRIG, ICOM_CMND */
338
void (*set_uih)(struct _icoms *p, int min, int max, int status);
340
/* Get the character that caused the user interrupt */
341
/* Clear it to 0x00 after reading it. */
342
int (*get_uih_char)(struct _icoms *p);
344
370
/* "Serial" write the characters in the buffer out */
345
371
/* Data will be written up to the terminating nul. */
346
/* Return error code & set error state. */
372
/* return icom error */
348
374
struct _icoms *p,
350
376
double tout); /* Timeout in seconds */
352
378
/* "Serial" read characters into the buffer */
353
/* Return error code & set error state. */
354
379
/* The returned data will be terminated by a nul. */
380
/* return icom error */
356
382
struct _icoms *p,
357
383
char *buf, /* Buffer to store characters read */
372
398
double tout); /* Timeout in seconds */
374
400
/* For a USB device, do a control message */
375
/* Return error code (don't set error state). */
376
int (*usb_control_th)(struct _icoms *p,
377
int requesttype, /* 8 bit request type (USB bmRequestType) */
378
int request, /* 8 bit request code (USB bRequest) */
379
int value, /* 16 bit value (USB wValue, sent little endian) */
380
int index, /* 16 bit index (USB wIndex, sent little endian) */
381
unsigned char *rwbuf, /* Read or write buffer */
382
int rwsize, /* Bytes to read or write */
383
double tout, /* Timeout in seconds */
384
int debug, /* debug flag value */
385
int *cut, /* Character that caused termination */
386
int checkabort); /* check for user abort */
388
/* Same as above, but set error state */
401
/* return icom error */
389
402
int (*usb_control)(struct _icoms *p,
390
403
int requesttype, /* 8 bit request type (USB bmRequestType) */
391
404
int request, /* 8 bit request code (USB bRequest) */
396
409
double tout); /* Timeout in seconds */
398
411
/* For a USB device, do a bulk or interrupt read from an end point */
399
/* Return error code (don't set error state). */
400
int (*usb_read_th)(struct _icoms *p,
401
void **hcancel, /* Optionaly return handle to allow cancel */
402
int ep, /* End point address */
403
unsigned char *buf, /* Read buffer */
404
int bsize, /* Bytes to read or write */
405
int *bread, /* Bytes read */
406
double tout, /* Timeout in seconds */
407
int debug, /* debug flag value */
408
int *cut, /* Character that caused termination */
409
int checkabort); /* check for user abort */
411
/* Same as above, but set error state */
412
/* return icom error */
412
413
int (*usb_read)(struct _icoms *p,
414
usb_cancelt *cancelt, /* Optionaly tokem that can be used to cancel */
413
415
int ep, /* End point address */
414
416
unsigned char *buf, /* Read buffer */
415
417
int bsize, /* Bytes to read or write */
417
419
double tout); /* Timeout in seconds */
419
421
/* For a USB device, do a bulk or interrupt write to an end point */
420
/* Return error code (don't set error state). */
421
int (*usb_write_th)(struct _icoms *p,
422
void **hcancel, /* Optionaly return handle to allow cancel */
423
int ep, /* End point address */
424
unsigned char *wbuf, /* Write buffer */
425
int wsize, /* Bytes to or write */
426
int *bwritten, /* Bytes written */
427
double tout, /* Timeout in seconds */
428
int debug, /* debug flag value */
429
int *cut, /* Character that caused termination */
430
int checkabort); /* check for user abort */
432
/* Same as above, but set error state */
422
/* return icom error */
433
423
int (*usb_write)(struct _icoms *p,
424
usb_cancelt *cancelt, /* Optionaly tokem that can be used to cancel */
434
425
int ep, /* End point address */
435
426
unsigned char *wbuf, /* Write buffer */
436
427
int wsize, /* Bytes to or write */
438
429
double tout); /* Timeout in seconds */
440
431
/* Cancel a read/write in another thread */
432
/* return icom error */
441
433
int (*usb_cancel_io)(struct _icoms *p,
442
void *hcancel); /* Cancel handle */
434
usb_cancelt *cantelt); /* Cancel handle */
444
436
/* Reset and end point toggle state to 0 */
437
/* return icom error */
445
438
int (*usb_resetep)(struct _icoms *p,
446
439
int ep); /* End point address */
448
441
/* Clear an end point halt */
442
/* return icom error */
449
443
int (*usb_clearhalt)(struct _icoms *p,
450
444
int ep); /* End point address */
452
/* For an HID device, read a message from the device - thread friendly. */
453
/* Set error state on error */
454
int (*hid_read_th)(struct _icoms *p,
455
unsigned char *buf, /* Read buffer */
456
int bsize, /* Bytes to read or write */
457
int *bread, /* Bytes read */
458
double tout, /* Timeout in seconds */
459
int debug, /* debug flag value */
460
int *cut, /* Character that caused termination */
461
int checkabort); /* Check for abort from keyboard */
463
446
/* For an HID device, read a message from the device. */
464
/* Set error state on error */
447
/* return icom error */
465
448
int (*hid_read)(struct _icoms *p,
466
449
unsigned char *buf, /* Read buffer */
467
450
int bsize, /* Bytes to read or write */
468
451
int *bread, /* Bytes read */
469
452
double tout); /* Timeout in seconds */
471
/* For an HID device, write a message to the device - thread friendly. */
472
/* Set error state on error */
473
int (*hid_write_th)(struct _icoms *p,
474
unsigned char *wbuf, /* Write buffer */
475
int wsize, /* Bytes to or write */
476
int *bwritten, /* Bytes written */
477
double tout, /* Timeout in seconds */
478
int debug, /* debug flag value */
479
int *cut, /* Character that caused termination */
480
int checkabort); /* Check for abort from keyboard */
482
454
/* For an HID device, write a message to the device */
483
/* Set error state on error */
455
/* return icom error */
484
456
int (*hid_write)(struct _icoms *p,
485
457
unsigned char *wbuf, /* Write buffer */
486
458
int wsize, /* Bytes to or write */
490
462
/* Destroy ourselves */
491
463
void (*del)(struct _icoms *p);
493
}; typedef struct _icoms icoms;
495
467
/* Constructor */
496
extern icoms *new_icoms(void);
498
/* - - - - - - - - */
499
/* Other functions */
501
/* Poll for a user abort, terminate, trigger or command. */
502
/* Wait for a key rather than polling, if wait != 0 */
504
/* ICOM_OK if no key has been hit, */
505
/* ICOM_USER if User abort has been hit, */
506
/* ICOM_TERM if User terminate has been hit. */
507
/* ICOM_TRIG if User trigger has been hit */
508
/* ICOM_CMND if User command has been hit */
509
int icoms_poll_user(icoms *p, int wait);
468
extern icoms *new_icoms(icompath *ipath, a1log *log);
511
470
/* - - - - - - - - - - - - - - - - - - -- */