diff options
author | Thomas Touhey <thomas@touhey.fr> | 2020-01-05 14:09:01 +0100 |
---|---|---|
committer | Thomas Touhey <thomas@touhey.fr> | 2020-01-05 14:09:01 +0100 |
commit | b58a5dac1ff27797d6f74010127e0e202084fa66 (patch) | |
tree | d241bfe17b6bbe8747387e1c81139701fd895356 /include/libtio | |
parent | 5e51624e299f6bd910e19c284bd9e2c6651114da (diff) |
Some improvements, i guess? not compiling yet though
Diffstat (limited to 'include/libtio')
-rw-r--r-- | include/libtio/chrono.h | 33 | ||||
-rw-r--r-- | include/libtio/endian.h | 108 | ||||
-rw-r--r-- | include/libtio/error.h | 7 | ||||
-rw-r--r-- | include/libtio/fs.h | 67 | ||||
-rw-r--r-- | include/libtio/integers.h | 96 | ||||
-rw-r--r-- | include/libtio/io/scsi.h | 3 | ||||
-rw-r--r-- | include/libtio/io/serial.h | 28 | ||||
-rw-r--r-- | include/libtio/io/usb.h | 7 | ||||
-rw-r--r-- | include/libtio/iter.h | 6 | ||||
-rw-r--r-- | include/libtio/native.h | 80 | ||||
-rw-r--r-- | include/libtio/stream.h | 12 | ||||
-rw-r--r-- | include/libtio/system.h | 101 |
12 files changed, 409 insertions, 139 deletions
diff --git a/include/libtio/chrono.h b/include/libtio/chrono.h index 71dd96a..19c51ba 100644 --- a/include/libtio/chrono.h +++ b/include/libtio/chrono.h @@ -6,20 +6,20 @@ TIO_BEGIN_NAMESPACE TIO_STRUCT(tio_timer, tio_timer_t) +TIO_STRUCT(tio_timer_functions, tio_timer_functions_t) /* Callbacks for timers. */ -typedef TIO_HOOK_TYPE(int) tio_get_timer_t - OF((tio_timer_t **tio__timer)); typedef TIO_HOOK_TYPE(void) tio_free_timer_t - OF((tio_timer_t *tio__timer)); + OF((void *tio__cookie)); typedef TIO_HOOK_TYPE(int) tio_get_spent_time_t - OF((tio_timer_t *tio__timer, unsigned long *tio__spent)); + OF((void *tio__cookie, unsigned long *tio__spent)); -/* Callback for sleep. */ +struct tio_timer_functions { + tio_free_timer_t *tio_timer_functions_free; -typedef TIO_HOOK_TYPE(int) tio_sleep_t - OF((unsigned long tio__ms)); + tio_get_spent_time_t *tio_timer_functions_get_spent_time; +}; /* --- * Public functions. @@ -27,31 +27,20 @@ typedef TIO_HOOK_TYPE(int) tio_sleep_t TIO_BEGIN_DECLS -/* Sleep. */ +/* Create a timer. */ -TIO_EXTERN(int) tio_sleep - OF((unsigned long tio__ms)); -TIO_EXTERN(void) tio_set_sleep_func - OF((tio_sleep_t *tio__func)); +TIO_EXTERN(int) tio_open_timer + OF((tio_timer_t **tio__timerp, void *tio__cookie, + tio_timer_functions_t const *tio__functions)); /* Timers are there to manage spent time on an operation to measure * the time operations can take and identify bottlenecks. */ -TIO_EXTERN(int) tio_get_timer - OF((tio_timer_t **tio__timer)); - #define tio_free_timer(TIMER) tio_free(TIMER) TIO_EXTERN(int) tio_get_spent_time OF((tio_timer_t *tio__timer, unsigned long *tio__spent)); -/* You can change these functions too! */ - -TIO_EXTERN(int) tio_set_timer_funcs - OF((tio_get_timer_t *tio__get_timer, - tio_free_timer_t *tio__free_timer, - tio_get_spent_time_t *tio__get_spent_time)); - TIO_END_DECLS TIO_END_NAMESPACE diff --git a/include/libtio/endian.h b/include/libtio/endian.h new file mode 100644 index 0000000..a168185 --- /dev/null +++ b/include/libtio/endian.h @@ -0,0 +1,108 @@ +#ifndef LIBTIO_ENDIAN_H +# define LIBTIO_ENDIAN_H 20190606 +# include "cdefs.h" +# include "integers.h" + +/* These function always exist. They are the default ones, if the platform + * does not have any equivalent function (or has one but indicates that + * fact badly). */ + +TIO_EXTERN(tio_uint16_t) tio_be16toh + OF((tio_uint16_t tio__x)); +TIO_EXTERN(tio_uint16_t) tio_le16toh + OF((tio_uint16_t tio__x)); +TIO_EXTERN(tio_uint32_t) tio_be32toh + OF((tio_uint32_t tio__x)); +TIO_EXTERN(tio_uint32_t) tio_le32toh + OF((tio_uint32_t tio__x)); + +TIO_EXTERN(tio_uint16_t) tio_htobe16 + OF((tio_uint16_t tio__x)); +TIO_EXTERN(tio_uint16_t) tio_htole16 + OF((tio_uint16_t tio__x)); +TIO_EXTERN(tio_uint32_t) tio_htobe32 + OF((tio_uint32_t tio__x)); +TIO_EXTERN(tio_uint32_t) tio_htole32 + OF((tio_uint32_t tio__x)); + +# if defined(__APPLE__) +# include <libkern/OSByteOrder.h> + +# define tio_int_be16toh(TIO__X) OSSwapBigToHostInt16(TIO__X) +# define tio_int_le16toh(TIO__X) OSSwapLittleToHostInt16(TIO__X) +# define tio_int_be32toh(TIO__X) OSSwapBigToHostInt32(TIO__X) +# define tio_int_le32toh(TIO__X) OSSwapLittleToHostInt32(TIO__X) + +# define tio_int_htobe16(TIO__X) OSSwapHostToBigInt16(TIO__X) +# define tio_int_htole16(TIO__X) OSSwapHostToLittleInt16(TIO__X) +# define tio_int_htobe32(TIO__X) OSSwapHostToBigInt32(TIO__X) +# define tio_int_htole32(TIO__X) OSSwapHostToLittleInt32(TIO__X) + +# elif defined(__OpenBSD__) +# include <sys/endian.h> +# define LIBTIO_ENDIAN_DEFINED + +# elif defined(_WIN16) || defined(_WIN32) || defined(_WIN64) \ + || defined(__WINDOWS__) +# include <winsock2.h> +# include <sys/param.h> + +# if BYTE_ORDER == LITTLE_ENDIAN +# define tio_int_be16toh(TIO__X) ntohs(TIO__X) +# define tio_int_le16toh(TIO__X) (TIO__X) +# define tio_int_be32toh(TIO__X) ntohl(TIO__X) +# define tio_int_le32toh(TIO__X) (TIO__X) + +# define tio_int_htobe16(TIO__X) htons(TIO__X) +# define tio_int_htole16(TIO__X) (TIO__X) +# define tio_int_htobe32(TIO__X) htonl(TIO__X) +# define tio_int_htole32(TIO__X) (TIO__X) +# else +# define tio_int_be16toh(TIO__X) (TIO__X) +# define tio_int_le16toh(TIO__X) ntohs(TIO__X) +# define tio_int_be32toh(TIO__X) (TIO__X) +# define tio_int_le32toh(TIO__X) ntohl(TIO__X) + +# define tio_int_htobe16(TIO__X) (TIO__X) +# define tio_int_htole16(TIO__X) htons(TIO__X) +# define tio_int_htobe32(TIO__X) (TIO__X) +# define tio_int_htole32(TIO__X) htonl(TIO__X) +# endif + +# elif defined(__GLIBC__) && defined(__USE_MISC) +# include <endian.h> +# define LIBTIO_ENDIAN_DEFINED + +# endif + +/* *e*toh, hto*e* macros are already defined, just make `tio_` aliases. */ + +# ifdef LIBTIO_ENDIAN_DEFINED +# define tio_int_be16toh(TIO__X) be16toh(TIO__X) +# define tio_int_le16toh(TIO__X) le16toh(TIO__X) +# define tio_int_be32toh(TIO__X) be32toh(TIO__X) +# define tio_int_le32toh(TIO__X) le32toh(TIO__X) + +# define tio_int_htobe16(TIO__X) htobe16(TIO__X) +# define tio_int_htole16(TIO__X) htole16(TIO__X) +# define tio_int_htobe32(TIO__X) htobe32(TIO__X) +# define tio_int_htole32(TIO__X) htole32(TIO__X) +# endif + +/* If we aren't including this from libcasio's endian source functions, + * `LIBTIO_NO_ENDIAN` should not be defined. Otherwise, as we will be + * defining the "dynamic" symbols, do not define the functions. */ + +# if !defined(LIBTIO_NO_ENDIAN) && defined(tio_int_be16toh) +# define tio_be16toh(TIO__X) tio_int_be16toh(TIO__X) +# define tio_le16toh(TIO__X) tio_int_le16toh(TIO__X) +# define tio_be32toh(TIO__X) tio_int_be32toh(TIO__X) +# define tio_le32toh(TIO__X) tio_int_le32toh(TIO__X) + +# define tio_htobe16(TIO__X) tio_int_htobe16(TIO__X) +# define tio_htole16(TIO__X) tio_int_htole16(TIO__X) +# define tio_htobe32(TIO__X) tio_int_htobe32(TIO__X) +# define tio_htole32(TIO__X) tio_int_htole32(TIO__X) +# endif + +#endif /* LIBTIO_ENDIAN_H */ diff --git a/include/libtio/error.h b/include/libtio/error.h index c81f300..67856d6 100644 --- a/include/libtio/error.h +++ b/include/libtio/error.h @@ -1,6 +1,7 @@ #ifndef LIBTIO_ERROR_H # define LIBTIO_ERROR_H 20180710 # include "cdefs.h" +# include "iter.h" TIO_BEGIN_NAMESPACE @@ -66,6 +67,12 @@ TIO_EXTERN(char const *) tio_error_name TIO_EXTERN(char const *) tio_error_desc OF((int tio__code)); +TIO_EXTERN(int) tio_list_errors + OF((tio_iter_t **tio__iterp)); + +# define tio_next_error(ITER, ERRNUMP) \ + (tio_next((ITER), (void **)(int **)(ERRNUMP))) + TIO_END_DECLS TIO_END_NAMESPACE diff --git a/include/libtio/fs.h b/include/libtio/fs.h index 2b4281e..e3c3c32 100644 --- a/include/libtio/fs.h +++ b/include/libtio/fs.h @@ -4,6 +4,7 @@ # include "stream.h" TIO_BEGIN_NAMESPACE +TIO_STRUCT(tio_fs, tio_fs_t) TIO_STRUCT(tio_fs_functions, tio_fs_functions_t) TIO_STRUCT(tio_fs_create_info, tio_fs_create_info_t) TIO_STRUCT(tio_path, tio_path_t) @@ -56,15 +57,37 @@ typedef TIO_HOOK_TYPE(int) tio_fs_move_t tio_path_t const *tio__destpath)); typedef TIO_HOOK_TYPE(int) tio_fs_stat_t - OF((void *tio__cookie, tio_stat_t *tio__stat, unsigned long tio__flags)); + OF((void *tio__cookie, tio_stat_t *tio__stat, + unsigned long tio__flags)); typedef TIO_HOOK_TYPE(int) tio_fs_open_t - OF((void *tio__cookie, tio_path_t *tio__path, unsigned long tio__flags)); + OF((void *tio__cookie, tio_path_t *tio__path, + unsigned long tio__flags)); typedef TIO_HOOK_TYPE(int) tio_fs_list_t - OF((void *tio__cookie, tio_iter_t **tio__iterp, tio_path_t *tio__path)); + OF((void *tio__cookie, tio_iter_t **tio__iterp, + tio_path_t *tio__path)); + +# define TIO_FSFLAG_CREATE 1 +# define TIO_FSFLAG_REMOVE 2 +# define TIO_FSFLAG_MOVE 4 +# define TIO_FSFLAG_LIST 8 +# define TIO_FSFLAG_STAT 16 +# define TIO_FSFLAG_OPEN 32 struct tio_fs_functions { + tio_fs_close_t *tio_fs_functions_close; + + /* Directory interactions. */ + + tio_fs_create_t *tio_fs_functions_create; + tio_fs_remove_t *tio_fs_functions_remove; + tio_fs_move_t *tio_fs_functions_move; + tio_fs_list_t *tio_fs_functions_list; + + /* File interactions. */ + tio_fs_stat_t *tio_fs_functions_stat; + tio_fs_open_t *tio_fs_functions_open; }; /* --- @@ -86,13 +109,45 @@ TIO_EXTERN(int) tio_set_path_child TIO_EXTERN(int) tio_set_path_to_parent OF((tio_path_t **tio__pathp)); -TIO_EXTERN(int) tio_free_path - OF((tio_path_t *tio__path)); +# define tio_free_path(PATH) tio_free(PATH) TIO_EXTERN(int) tio_get_path_name - OF((tio_path_t *tio__path, void **tio__namep, size_t *tio__name_sizep)); + OF((tio_path_t *tio__path, void **tio__namep, + size_t *tio__name_sizep)); + +/* Filesystem management. */ + +TIO_EXTERN(int) tio_create + OF((tio_fs_t *tio__fs, tio_path_t *tio__path, + tio_filetype_t tio__ftyp, ...)); +TIO_EXTERN(int) tio_remove + OF((tio_fs_t *tio__fs, tio_path_t *tio__path)); +TIO_EXTERN(int) tio_move + OF((tio_fs_t *tio__fs, tio_path_t const *tio__path, + tio_path_t const *tio__destpath)); + +TIO_EXTERN(int) tio_stat + OF((tio_fs_t *tio__fs, tio_stat_t *tio__stat, + unsigned long tio__flags)); +TIO_EXTERN(int) tio_open + OF((tio_fs_t *tio__fs, tio_stream_t **tio__streamp, + tio_path_t const *tio__path, unsigned long tio__flags)); + +TIO_EXTERN(int) tio_list + OF((tio_fs_t *tio__fs, tio_iter_t **tio__ + +/* Get a native filesystem. */ +# define tio_next_fs(ITER, FSNAMEP) \ + tio_next((ITER), (void **)(char const **)(FSNAMEP)) +TIO_EXTERN(int) tio_list_fs + OF((tio_iter_t **tio__iterp)); +TIO_EXTERN(int) tio_get_fs + OF((tio_fs_t **tio__fsp, char const *tio__name)); +TIO_EXTERN(int) tio_get_fs_folder + OF((tio_fs_t **tio__fsp, tio_fs_t *tio__fs, + tio_path_t *tio__foldpath)); TIO_END_DECLS TIO_END_NAMESPACE diff --git a/include/libtio/integers.h b/include/libtio/integers.h new file mode 100644 index 0000000..87bcd33 --- /dev/null +++ b/include/libtio/integers.h @@ -0,0 +1,96 @@ +#ifndef LIBTIO_INTEGERS_H +# define LIBTIO_INTEGERS_H 20190606 +# include "cdefs.h" + +TIO_BEGIN_NAMESPACE + +# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +# include <stdint.h> +# include <inttypes.h> + +/* `stdint.h` and `inttypes.h` are standard C99 headers. + * `stdint.h` provides the `uintN_t` types, and + * `inttypes.h` provides the `PRI[uxX]N` macros. */ + +typedef uint8_t tio_uint8_t; +typedef uint16_t tio_uint16_t; +typedef uint32_t tio_uint32_t; + +# define TIO_PRIu8 PRIu8 +# define TIO_PRIx8 PRIx8 +# define TIO_PRIX8 PRIX8 +# define TIO_PRIu16 PRIu16 +# define TIO_PRIx16 PRIx16 +# define TIO_PRIX16 PRIX16 +# define TIO_PRIu32 PRIu32 +# define TIO_PRIx32 PRIx32 +# define TIO_PRIX32 PRIX32 + +# else /* C89 */ +# include <limits.h> + +/* Here, we ought to do some C89 hacking. + * We'll use the `limits.h` definitions to try and guess which one of the + * default types are the 8-bit, 16-bit and 32-bit integer. */ + +# define TIO_P8 "hh" +typedef unsigned char tio_uint8_t; + +/* 16-bit integer. */ + +# if (USHRT_MAX > 0xffffUL) +# error "No 16-bit type, exiting!" +# endif +# define TIO_P16 "h" +typedef unsigned short tio_uint16_t; + +/* 32-bit integer. */ + +# if (UINT_MAX == 0xffffffffUL) +# define TIO_P32 "" +typedef unsigned int tio_uint32_t; +# elif (ULONG_MAX == 0xffffffffUL) +# define TIO_P32 "l" +typedef unsigned long tio_uint32_t; +# else + +/* There is nothing between `char` and `short`, and `char` is always + * byte-wide; + * + * `long long` is not defined in C89, and even if it can be used as a + * compiler extension for C89, it is supposed to be 64-bit or more. + * So basically we're running out of options here. */ + +# error "No 32-bit type, exiting!" +# endif + +# define TIO_PRIu8 TIO_P8 "u" +# define TIO_PRIx8 TIO_P8 "x" +# define TIO_PRIX8 TIO_P8 "X" +# define TIO_PRIu16 TIO_P16 "u" +# define TIO_PRIx16 TIO_P16 "x" +# define TIO_PRIX16 TIO_P16 "X" +# define TIO_PRIu32 TIO_P32 "u" +# define TIO_PRIx32 TIO_P32 "x" +# define TIO_PRIX32 TIO_P32 "X" +# endif + +/* printf thing for `size_t` */ + +# if defined(_WIN64) +# define TIO_PRIuSIZE "l64u" +# define TIO_PRIxSIZE "l64x" +# define TIO_PRIXSIZE "l64X" +# elif defined(_WIN32) +# define TIO_PRIuSIZE "u" +# define TIO_PRIxSIZE "x" +# define TIO_PRIXSIZE "X" +# else +# define TIO_PRIuSIZE "zu" +# define TIO_PRIxSIZE "zx" +# define TIO_PRIXSIZE "zX" +# endif + +TIO_END_NAMESPACE + +#endif /* LIBTIO_INTEGERS_H */ diff --git a/include/libtio/io/scsi.h b/include/libtio/io/scsi.h index fd9cd7e..a00f109 100644 --- a/include/libtio/io/scsi.h +++ b/include/libtio/io/scsi.h @@ -5,7 +5,8 @@ TIO_BEGIN_NAMESPACE /* An SCSI stream serves for interacting with an SCSI-enabled device. - * Using such a stream, you can send an SCSI request and receive the answer. + * Using such a stream, you can send an SCSI request and receive the + * answer. * * An SCSI request can be followed with data in two directions: to the * device and from the device: diff --git a/include/libtio/io/serial.h b/include/libtio/io/serial.h index b7d4aa1..e4cf055 100644 --- a/include/libtio/io/serial.h +++ b/include/libtio/io/serial.h @@ -27,14 +27,18 @@ struct tio_serial_attrs { }; /* Here are the control characters you can define: - * `TIO_XON`: the XON character to re-enable transmission (software control); - * `TIO_XOFF`: the XOFF charater to disable transmission (software control). */ + * + * `TIO_XON`: the XON character to re-enable transmission + * (software control). + * `TIO_XOFF`: the XOFF charater to disable transmission + * (software control). */ # define TIO_XON 0 # define TIO_XOFF 1 -/* Among the flags, you can find various things. First of all, the stop bits - * settings (1 or 2): +/* Among the flags, you can find various things. First of all, the stop + * bits settings (1 or 2): + * * `TIO_STOONE`: 1 stop bit; * `TIO_STOTWO`: 2 stop bits. * @@ -91,10 +95,11 @@ struct tio_serial_attrs { /* The XON/XOFF software control settings. * - * XOFF disables the transmission temporarily, usually because the device at - * the other end can't manage so much data at once, whereas XON re-enables the - * transmission, probably because the device at the other end has finished - * processing the data you sent it and is ready to process some more. + * XOFF disables the transmission temporarily, usually because the device + * at the other end can't manage so much data at once, whereas XON + * re-enables the transmission, probably because the device at the other + * end has finished processing the data you sent it and is ready to process + * some more. * * XON/XOFF software flow control can be enabled on input (XIN) and * output (XOFF). The XON and XOFF characters are common to both @@ -159,10 +164,9 @@ TIO_EXTERN(int) tio_make_serial_attrs unsigned long tio__flags)); /* List the available serial ports. - * The iterator yields strings (`char const *`) representing the path. */ - -TIO_EXTERN(int) tio_list_serial_ports - OF((tio_iter_t **tio__iterp)); + * The iterator yields strings (`char const *`) representing the path. + * + * Use `tio_list_serial_ports(tio_iter_t **, tio_system_t *)`. */ # define tio_next_serial_port(ITER, PTRP) \ (tio_next((ITER), (void **)(char const **)(PTRP))) diff --git a/include/libtio/io/usb.h b/include/libtio/io/usb.h index aed26a9..c18e612 100644 --- a/include/libtio/io/usb.h +++ b/include/libtio/io/usb.h @@ -38,10 +38,9 @@ struct tio_usb_device { TIO_BEGIN_DECLS /* List the available USB devices. - * The iterator yields strings (`char const *`) representing the path. */ - -TIO_EXTERN(int) tio_list_usb_devices - OF((tio_iter_t **tio__iterp)); + * The iterator yields strings (`char const *`) representing the path. + * + * Use `tio_list_usb_devices(tio_iter_t **iterp, tio_system_t *system)` */ # define tio_next_usb_device(ITER, PTRP) \ (tio_next((ITER), (void **)(tio_usb_device_t **)(PTRP))) diff --git a/include/libtio/iter.h b/include/libtio/iter.h index 31d1133..6a212ff 100644 --- a/include/libtio/iter.h +++ b/include/libtio/iter.h @@ -7,10 +7,8 @@ TIO_BEGIN_NAMESPACE /* A few things in libtio work using iterators. This is the centralized * iterator interface. */ -struct tio_iter; -typedef struct tio_iter tio_iter_t; -struct tio_iter_functions; -typedef struct tio_iter_functions tio_iter_functions_t; +TIO_STRUCT(tio_iter, tio_iter_t) +TIO_STRUCT(tio_iter_functions, tio_iter_functions_t) /* --- * Define something. diff --git a/include/libtio/native.h b/include/libtio/native.h index 4a005ad..2faba26 100644 --- a/include/libtio/native.h +++ b/include/libtio/native.h @@ -39,86 +39,6 @@ TIO_BEGIN_DECLS # define LIBTIO_DISABLED_MACOS 1 # endif -/* --- - * Native stream opening functions. - * --- */ - -/* FILE streams. */ - -# ifndef LIBTIO_DISABLED_FILE -# include <stdio.h> - -TIO_EXTERN(int) tio_open_std - OF((tio_stream_t **tio__stream, - FILE *tio__fl, int tio__close)); - -TIO_EXTERN(int) tio_open_stdout - OF((tio_stream_t **tio__stream)); -TIO_EXTERN(int) tio_open_stderr - OF((tio_stream_t **tio__stream)); -TIO_EXTERN(int) tio_open_stdin - OF((tio_stream_t **tio__stream)); - -# endif - -/* UNIX-specific streams and listings. */ - -# ifndef LIBTIO_DISABLED_UNIX - -TIO_EXTERN(int) tio_open_unix_file - OF((tio_stream_t **tio__streamp, int tio__fd, int tio__close)); -TIO_EXTERN(int) tio_open_unix_serial - OF((tio_stream_t **tio__streamp, int tio__fd, int tio__close)); - -TIO_EXTERN(int) tio_open_unix_serial_port - OF((tio_stream_t **tio__streamp, char const *tio__path)); - -/* These might not work, but we can try them on UNIX variants as they - * require the unistd.h functions at compile time. */ - -TIO_EXTERN(int) tio_list_unix_serial_ports_using_dev_serial - OF((tio_iter_t **tio__iterp)); -TIO_EXTERN(int) tio_list_unix_serial_ports_using_cu_devices - OF((tio_iter_t **tio__iterp)); - -# endif - -/* MS-Windows specific streams and port/device listing. */ - -# ifndef LIBTIO_DISABLED_WINDOWS -# include <windows.h> - -TIO_EXTERN(int) tio_open_windows_file_handle - OF((tio_stream_t **tio__streamp, HANDLE tio__handle, - int tio__cl)); -TIO_EXTERN(int) tio_open_windows_serial_handle - OF((tio_stream_t **tio__streamp, HANDLE tio__handle, - int tio__cl)); - -TIO_EXTERN(int) tio_open_windows_serial_port - OF((tio_stream_t **tio__streamp, char const *tio__path)); - -TIO_EXTERN(int) tio_list_windows_serial_ports - OF((tio_iter_t **tio__iterp)); - -# endif - -/* libusb stream. */ - -# ifndef LIBTIO_DISABLED_LIBUSB -# include <libusb.h> - -TIO_EXTERN(int) tio_open_libusb - OF((tio_stream_t **tio__streamp, int tio__bus, int tio__addr)); -TIO_EXTERN(int) tio_open_libusb_device - OF((tio_stream_t **tio__streamp, libusb_device *tio__device, - int tio__deref)); - -TIO_EXTERN(int) tio_list_libusb_devices - OF((tio_iter_t **tio__iterp)); - -# endif - TIO_END_DECLS TIO_END_NAMESPACE diff --git a/include/libtio/stream.h b/include/libtio/stream.h index ffa3333..8b3357e 100644 --- a/include/libtio/stream.h +++ b/include/libtio/stream.h @@ -36,7 +36,8 @@ struct tio_timeouts { * - before the first block. * - between blocks. * - * All timeouts are in microseconds, and considered only if non zero. */ + * All timeouts are in microseconds, and considered only if non + * zero. */ unsigned long tio_timeouts_total; unsigned long tio_timeouts_blocks; @@ -301,15 +302,6 @@ TIO_EXTERN(int) tio_open_limited TIO_EXTERN(int) tio_empty_limited OF((tio_stream_t *tio__stream)); -/* Open a native serial or USB stream. */ - -TIO_EXTERN(int) tio_open_native_serial - OF((tio_stream_t **tio__streamp, - char const *tio__path)); -TIO_EXTERN(int) tio_open_native_usb - OF((tio_stream_t **tio__streamp, - int tio__bus, int tio__addr)); - /* Make a generic stream over USB using bulk-only transport. */ TIO_EXTERN(int) tio_open_bulk_usb_over_stream diff --git a/include/libtio/system.h b/include/libtio/system.h new file mode 100644 index 0000000..ecc5a5b --- /dev/null +++ b/include/libtio/system.h @@ -0,0 +1,101 @@ +#ifndef TIO_SYSTEM_H +# define TIO_SYSTEM_H 20190907 +# include "cdefs.h" +# include "stream.h" + +TIO_BEGIN_NAMESPACE + +/* libtio uses a system as an abstraction. */ + +TIO_STRUCT(tio_system, tio_system_t) +TIO_STRUCT(tio_system_functions, tio_system_functions_t) + +# define TIO_STDSTREAM_IN 0 +# define TIO_STDSTREAM_OUT 1 +# define TIO_STDSTREAM_ERR 2 + +/* Callbacks. + * Uses `tio_close_t` from stream callbacks. */ + +typedef TIO_HOOK_TYPE(int) tio_open_stdstream_t + OF((void *tio__cookie, tio_stream_t **tio__stream, int tio__type)); + +typedef TIO_HOOK_TYPE(int) tio_open_usb_stream_t + OF((void *tio__cookie, tio_stream_t **tio__stream, int tio__bus, + int tio__addr)); +typedef TIO_HOOK_TYPE(int) tio_list_usb_devices_t + OF((void *tio__cookie, tio_iter_t **tio__iter)); + +typedef TIO_HOOK_TYPE(int) tio_open_serial_stream_t + OF((void *tio__cookie, tio_stream_t **tio__stream, + char const *tio__path, tio_serial_attrs_t const *tio__attrs)); +typedef TIO_HOOK_TYPE(int) tio_list_serial_ports_t + OF((void *tio__cookie, tio_iter_t **tio__iterp)); + +typedef TIO_HOOK_TYPE(int) tio_sleep_t + OF((void *tio__cookie, unsigned long tio__ms)); +typedef TIO_HOOK_TYPE(int) tio_get_timer_t + OF((void *tio__cookie, tio_timer_t **tio__timer)); + +/* Callbacks definition. */ + +# define TIO_OPENFLAG_SYSTEM_USB 1 +# define TIO_OPENFLAG_SYSTEM_SERIAL 2 +# define TIO_OPENFLAG_SYSTEM_CHRONO 4 + +# define TIO_OPENFLAG_SYSTEM_ALL 7 + +struct tio_system_functions { + tio_close_t *tio_system_functions_close; + + tio_open_usb_stream_t *tio_system_functions_open_usb_stream; + tio_list_usb_devices_t *tio_system_functions_list_usb_devices; + + tio_open_serial_stream_t *tio_system_functions_open_serial_stream; + tio_list_serial_ports_t *tio_system_functions_list_serial_ports; + + tio_sleep_t *tio_system_functions_sleep; + tio_get_timer_t *tio_system_functions_get_timer; +}; + +/* --- + * Management functions. + * --- */ + +TIO_BEGIN_DECLS + +/* Open a system. */ + +TIO_EXTERN(int) tio_open_system + OF((tio_system_t **tio__systemp, void *tio__cookie, + unsigned int tio__flags, + tio_system_functions_t const *tio__functions)); +TIO_EXTERN(int) tio_open_local_system + OF((tio_system_t **tio__systemp)); + +/* Stream interactions (USB and serial). */ + +TIO_EXTERN(int) tio_open_usb_stream + OF((tio_system_t *tio__system, tio_stream_t **tio__streamp, + int tio__bus, int tio__addr)); +TIO_EXTERN(int) tio_list_usb_devices + OF((tio_system_t *tio__system, tio_iter_t **tio__iterp)); + +TIO_EXTERN(int) tio_open_serial_stream + OF((tio_system_t *tio__system, tio_stream_t **tio__streamp, + char const *tio__path, tio_serial_attrs_t const *tio__attrs)); +TIO_EXTERN(int) tio_list_serial_ports + OF((tio_system_t *tio__system, tio_iter_t **tio__iterp)); + +TIO_EXTERN(int) tio_sleep_on_system + OF((tio_system_t *tio__system, unsigned long tio__ms)); +TIO_EXTERN(int) tio_get_system_timer + OF((tio_system_t *tio__system, tio_timer_t **tio__timerp)); + +# define tio_sleep(MS) tio_sleep_on_system(NULL, (MS)) +# define tio_get_timer(TP) tio_get_system_timer(NULL, (TP)) + +TIO_END_DECLS +TIO_END_NAMESPACE + +#endif /* TIO_NATIVE_H */ |