113 lines
4.1 KiB
C
113 lines
4.1 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* libp7/stream.h |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2017/01/04 15:01:48 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#ifndef LIBP7_STREAM_H
|
|
# define LIBP7_STREAM_H
|
|
# include <libp7/types.h>
|
|
# ifdef __cplusplus
|
|
extern "C" {
|
|
# endif
|
|
|
|
/* This file is there so you can implement your own custom protocol instead
|
|
* of CASIO's Protocol 7 to communicate with a CASIO calculator, while
|
|
* using libp7 utilities to look for the calculator. */
|
|
/* ************************************************************************** */
|
|
/* Stream utilities */
|
|
/* ************************************************************************** */
|
|
/* Structure of a stream - use this to define your custom streams!
|
|
* These functions take the uncasted cookie and the required arguments for
|
|
* the actions (`timeout` is in milliseconds). They should return the libp7
|
|
* error code, or 0 if no error. There is no partial success: you manage to
|
|
* receive/send the full buffer, or you do not.
|
|
*
|
|
* some of the streams managed by default already have buffering, so this
|
|
* implementation doesn't add another layer of it.
|
|
* if you need buffering, implement it! */
|
|
typedef struct p7_stream_s {
|
|
/* callbacks */
|
|
int (*read)(void *cookie, unsigned char *dest, size_t size,
|
|
unsigned int timeout);
|
|
int (*write)(void *cookie, const unsigned char *data, size_t size);
|
|
int (*close)(void *cookie);
|
|
int (*setcomm)(void *cookie, int speed, int parity, int stopbits);
|
|
|
|
/* cookie */
|
|
void *cookie;
|
|
} p7_stream_t;
|
|
|
|
/* baud speeds */
|
|
# define P7_B9600 9600
|
|
# define P7_B19200 19200
|
|
|
|
/* parities */
|
|
# define P7_PARITY_NONE 0
|
|
# define P7_PARITY_ODD 1
|
|
# define P7_PARITY_EVEN 2
|
|
|
|
/* stop bits */
|
|
# define P7_ONESTOPBIT 1
|
|
# define P7_TWOSTOPBITS 2
|
|
|
|
/* initialize libp7 with a custom stream */
|
|
int p7_sinit(p7_handle_t **h, unsigned int flags,
|
|
const char *name, p7_stream_t stream);
|
|
|
|
/* get the stream from the handle */
|
|
const p7_stream_t *p7_get_stream(p7_handle_t *handle);
|
|
|
|
/* read and write data from and to stream */
|
|
int p7_read(p7_stream_t *stream, void *dest, size_t size, unsigned int timeout);
|
|
int p7_write(p7_stream_t *stream, const void *data, size_t size);
|
|
int p7_setcomm(p7_stream_t *stream, int speed, int parity, int stopbits);
|
|
|
|
/* ************************************************************************** */
|
|
/* Built-in streams */
|
|
/* ************************************************************************** */
|
|
/* FILE stream */
|
|
# ifndef P7_DISABLED_FILE
|
|
int p7_finit(p7_handle_t **handle, unsigned int flags,
|
|
FILE *readstream, FILE *writestream);
|
|
# endif
|
|
|
|
/* STREAMS device */
|
|
# if defined(__linux__)
|
|
int p7_fdinit(p7_handle_t **handle, unsigned int flags,
|
|
const char *name, int readfd, int writefd);
|
|
int p7_fdcominit(p7_handle_t **handle, unsigned int flags, int com);
|
|
# else
|
|
# define P7_DISABLED_STREAMS
|
|
# endif
|
|
|
|
/* libusb initialization */
|
|
# ifndef P7_DISABLED_LIBUSB
|
|
int p7_libusbinit(p7_handle_t **handle, unsigned int flags);
|
|
# endif
|
|
|
|
/* Microsoft Windows initialization */
|
|
# ifdef __WINDOWS__
|
|
int p7_winit(p7_handle_t **handle, unsigned int flags, const char *path);
|
|
int p7_wcominit(p7_handle_t **handle, unsigned int flags, int com);
|
|
# else
|
|
# define P7_DISABLED_WINDOWS
|
|
# endif
|
|
|
|
/* Check if there is serial/usb on this platform */
|
|
# if defined(P7_DISABLED_STREAMS) && defined(P7_DISABLED_WINDOWS)
|
|
# define P7_NOSERIAL
|
|
# endif
|
|
# if defined(P7_DISABLED_LIBUSB) && defined(P7_DISABLED_STREAMS) \
|
|
&& defined(P7_DISABLED_WINDOWS)
|
|
# define P7_NOUSB
|
|
# endif
|
|
|
|
# ifdef __cplusplus
|
|
}
|
|
# endif
|
|
#endif /* LIBP7_STREAM_H */
|