Minor code cleanups
[libjaylink.git] / libjaylink / libjaylink-internal.h
blob47a24a150610f8df754ec30b60c4d9c0a1e659d6
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2016 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef LIBJAYLINK_LIBJAYLINK_INTERNAL_H
21 #define LIBJAYLINK_LIBJAYLINK_INTERNAL_H
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <sys/types.h>
27 #include <libusb.h>
29 #include "libjaylink.h"
31 /**
32 * @file
34 * Internal libjaylink header file.
37 /** Macro to mark private libjaylink symbol. */
38 #if defined(_WIN32) || defined(__MSYS__) || defined(__CYGWIN__)
39 #define JAYLINK_PRIV
40 #else
41 #define JAYLINK_PRIV __attribute__ ((visibility ("hidden")))
42 #endif
44 /** Calculate the minimum of two numeric values. */
45 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
47 struct jaylink_context {
48 /** libusb context. */
49 struct libusb_context *usb_ctx;
50 /**
51 * List of allocated device instances.
53 * Used to prevent multiple device instances for the same device.
55 struct list *devs;
56 /** List of recently discovered devices. */
57 struct list *discovered_devs;
58 /** Current log level. */
59 enum jaylink_log_level log_level;
60 /** Log callback function. */
61 jaylink_log_callback log_callback;
62 /** User data to be passed to the log callback function. */
63 void *log_callback_data;
64 /** Log domain. */
65 char log_domain[JAYLINK_LOG_DOMAIN_MAX_LENGTH + 1];
68 struct jaylink_device {
69 /** libjaylink context. */
70 struct jaylink_context *ctx;
71 /** Number of references held on this device instance. */
72 size_t ref_count;
73 /** Host interface. */
74 enum jaylink_host_interface interface;
75 /** libusb device instance. */
76 struct libusb_device *usb_dev;
77 /** USB address of the device. */
78 uint8_t usb_address;
79 /**
80 * Serial number of the device.
82 * This number is for enumeration purpose only and can differ from the
83 * real serial number of the device.
85 uint32_t serial_number;
86 /** Indicates whether the serial number is valid. */
87 bool valid_serial_number;
90 struct jaylink_device_handle {
91 /** Device instance. */
92 struct jaylink_device *dev;
93 /** libusb device handle. */
94 struct libusb_device_handle *usb_devh;
95 /** USB interface number of the device. */
96 uint8_t interface_number;
97 /** USB interface IN endpoint of the device. */
98 uint8_t endpoint_in;
99 /** USB interface OUT endpoint of the device. */
100 uint8_t endpoint_out;
102 * Buffer for write and read operations.
104 * Note that write and read operations are always processed
105 * consecutively and therefore the same buffer can be used for both.
107 uint8_t *buffer;
108 /** Buffer size. */
109 size_t buffer_size;
110 /** Number of bytes left for the read operation. */
111 size_t read_length;
112 /** Number of bytes available in the buffer to be read. */
113 size_t bytes_available;
114 /** Current read position in the buffer. */
115 size_t read_pos;
117 * Number of bytes left to be written before the write operation will
118 * be performed.
120 size_t write_length;
122 * Current write position in the buffer.
124 * This is equivalent to the number of bytes in the buffer and used for
125 * write operations only.
127 size_t write_pos;
130 struct list {
131 void *data;
132 struct list *next;
135 typedef bool (*list_compare_callback)(const void *a, const void *b);
137 /*--- buffer.c --------------------------------------------------------------*/
139 JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value,
140 size_t offset);
141 JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset);
142 JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value,
143 size_t offset);
144 JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset);
146 /*--- device.c --------------------------------------------------------------*/
148 JAYLINK_PRIV struct jaylink_device *device_allocate(
149 struct jaylink_context *ctx);
151 /*--- discovery.c -----------------------------------------------------------*/
153 JAYLINK_PRIV ssize_t discovery_get_device_list(struct jaylink_context *ctx,
154 struct jaylink_device ***list);
156 /*--- list.c ----------------------------------------------------------------*/
158 JAYLINK_PRIV struct list *list_prepend(struct list *list, void *data);
159 JAYLINK_PRIV struct list *list_remove(struct list *list, const void *data);
160 JAYLINK_PRIV struct list *list_find_custom(struct list *list,
161 list_compare_callback cb, const void *cb_data);
162 JAYLINK_PRIV size_t list_length(struct list *list);
163 JAYLINK_PRIV void list_free(struct list *list);
165 /*--- log.c -----------------------------------------------------------------*/
167 JAYLINK_PRIV int log_vprintf(const struct jaylink_context *ctx,
168 enum jaylink_log_level level, const char *format, va_list args,
169 void *user_data);
170 JAYLINK_PRIV void log_err(const struct jaylink_context *ctx,
171 const char *format, ...);
172 JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx,
173 const char *format, ...);
174 JAYLINK_PRIV void log_info(const struct jaylink_context *ctx,
175 const char *format, ...);
176 JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx,
177 const char *format, ...);
179 /*--- transport.c -----------------------------------------------------------*/
181 JAYLINK_PRIV int transport_open(struct jaylink_device_handle *devh);
182 JAYLINK_PRIV int transport_close(struct jaylink_device_handle *devh);
183 JAYLINK_PRIV int transport_start_write_read(struct jaylink_device_handle *devh,
184 size_t write_length, size_t read_length, bool has_command);
185 JAYLINK_PRIV int transport_start_write(struct jaylink_device_handle *devh,
186 size_t length, bool has_command);
187 JAYLINK_PRIV int transport_start_read(struct jaylink_device_handle *devh,
188 size_t length);
189 JAYLINK_PRIV int transport_write(struct jaylink_device_handle *devh,
190 const uint8_t *buffer, size_t length);
191 JAYLINK_PRIV int transport_read(struct jaylink_device_handle *devh,
192 uint8_t *buffer, size_t length);
194 #endif /* LIBJAYLINK_LIBJAYLINK_INTERNAL_H */