Add jaylink_device_get_usb_bus_ports()
[libjaylink.git] / libjaylink / libjaylink-internal.h
blobf97ec14696e96f53a7d56e5432de172b37c4c87c
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 <stdarg.h>
27 #include <sys/types.h>
28 #ifdef _WIN32
29 #include <ws2tcpip.h>
30 #else
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #endif
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
39 #ifdef HAVE_LIBUSB
40 #include <libusb.h>
41 #endif
43 #include "libjaylink.h"
45 /**
46 * @file
48 * Internal libjaylink header file.
51 /** Macro to mark private libjaylink symbol. */
52 #if defined(_WIN32) || defined(__MSYS__) || defined(__CYGWIN__)
53 #define JAYLINK_PRIV
54 #else
55 #define JAYLINK_PRIV __attribute__ ((visibility ("hidden")))
56 #endif
58 /** Calculate the minimum of two numeric values. */
59 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
61 struct jaylink_context {
62 #ifdef HAVE_LIBUSB
63 /** libusb context. */
64 struct libusb_context *usb_ctx;
65 #endif
66 /**
67 * List of allocated device instances.
69 * Used to prevent multiple device instances for the same device.
71 struct list *devs;
72 /** List of recently discovered devices. */
73 struct list *discovered_devs;
74 /** Current log level. */
75 enum jaylink_log_level log_level;
76 /** Log callback function. */
77 jaylink_log_callback log_callback;
78 /** User data to be passed to the log callback function. */
79 void *log_callback_data;
80 /** Log domain. */
81 char log_domain[JAYLINK_LOG_DOMAIN_MAX_LENGTH + 1];
84 struct jaylink_device {
85 /** libjaylink context. */
86 struct jaylink_context *ctx;
87 /** Number of references held on this device instance. */
88 size_t ref_count;
89 /** Host interface. */
90 enum jaylink_host_interface iface;
91 /**
92 * Serial number of the device.
94 * This number is for enumeration purpose only and can differ from the
95 * real serial number of the device.
97 uint32_t serial_number;
98 /** Indicates whether the serial number is valid. */
99 bool valid_serial_number;
100 #ifdef HAVE_LIBUSB
101 /** libusb device instance. */
102 struct libusb_device *usb_dev;
103 /** USB address of the device. */
104 uint8_t usb_address;
105 #endif
107 * IPv4 address.
109 * The address is encoded as string in quad-dotted decimal format.
111 * This field is used for devices with host interface #JAYLINK_HIF_TCP
112 * only.
114 char ipv4_address[INET_ADDRSTRLEN];
116 * Media Access Control (MAC) address.
118 * This field is used for devices with host interface #JAYLINK_HIF_TCP
119 * only.
121 uint8_t mac_address[JAYLINK_MAC_ADDRESS_LENGTH];
122 /** Indicates whether the MAC address is available. */
123 bool has_mac_address;
125 * Product name.
127 * This field is used for devices with host interface #JAYLINK_HIF_TCP
128 * only.
130 char product_name[JAYLINK_PRODUCT_NAME_MAX_LENGTH];
131 /** Indicates whether the product name is available. */
132 bool has_product_name;
134 * Nickname.
136 * This field is used for devices with host interface #JAYLINK_HIF_TCP
137 * only.
139 char nickname[JAYLINK_NICKNAME_MAX_LENGTH];
140 /** Indicates whether the nickname is available. */
141 bool has_nickname;
143 * Hardware version.
145 * This field is used for devices with host interface #JAYLINK_HIF_TCP
146 * only.
148 struct jaylink_hardware_version hw_version;
149 /** Indicates whether the hardware version is available. */
150 bool has_hw_version;
153 struct jaylink_device_handle {
154 /** Device instance. */
155 struct jaylink_device *dev;
157 * Buffer for write and read operations.
159 * Note that write and read operations are always processed
160 * consecutively and therefore the same buffer can be used for both.
162 uint8_t *buffer;
163 /** Buffer size. */
164 size_t buffer_size;
165 /** Number of bytes left for the read operation. */
166 size_t read_length;
167 /** Number of bytes available in the buffer to be read. */
168 size_t bytes_available;
169 /** Current read position in the buffer. */
170 size_t read_pos;
172 * Number of bytes left to be written before the write operation will
173 * be performed.
175 size_t write_length;
177 * Current write position in the buffer.
179 * This is equivalent to the number of bytes in the buffer and used for
180 * write operations only.
182 size_t write_pos;
183 #ifdef HAVE_LIBUSB
184 /** libusb device handle. */
185 struct libusb_device_handle *usb_devh;
186 /** USB interface number of the device. */
187 uint8_t interface_number;
188 /** USB interface IN endpoint of the device. */
189 uint8_t endpoint_in;
190 /** USB interface OUT endpoint of the device. */
191 uint8_t endpoint_out;
192 #endif
194 * Socket descriptor.
196 * This field is used for devices with host interface #JAYLINK_HIF_TCP
197 * only.
199 int sock;
202 struct list {
203 void *data;
204 struct list *next;
207 typedef bool (*list_compare_callback)(const void *data, const void *user_data);
209 /*--- buffer.c --------------------------------------------------------------*/
211 JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value,
212 size_t offset);
213 JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset);
214 JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value,
215 size_t offset);
216 JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset);
218 /*--- device.c --------------------------------------------------------------*/
220 JAYLINK_PRIV struct jaylink_device *device_allocate(
221 struct jaylink_context *ctx);
223 /*--- discovery_tcp.c -------------------------------------------------------*/
225 JAYLINK_PRIV int discovery_tcp_scan(struct jaylink_context *ctx);
227 /*--- discovery_usb.c -------------------------------------------------------*/
229 JAYLINK_PRIV int discovery_usb_scan(struct jaylink_context *ctx);
231 /*--- list.c ----------------------------------------------------------------*/
233 JAYLINK_PRIV struct list *list_prepend(struct list *list, void *data);
234 JAYLINK_PRIV struct list *list_remove(struct list *list, const void *data);
235 JAYLINK_PRIV struct list *list_find_custom(struct list *list,
236 list_compare_callback callback, const void *user_data);
237 JAYLINK_PRIV size_t list_length(struct list *list);
238 JAYLINK_PRIV void list_free(struct list *list);
240 /*--- log.c -----------------------------------------------------------------*/
242 JAYLINK_PRIV int log_vprintf(const struct jaylink_context *ctx,
243 enum jaylink_log_level level, const char *format, va_list args,
244 void *user_data);
245 JAYLINK_PRIV void log_err(const struct jaylink_context *ctx,
246 const char *format, ...);
247 JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx,
248 const char *format, ...);
249 JAYLINK_PRIV void log_info(const struct jaylink_context *ctx,
250 const char *format, ...);
251 JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx,
252 const char *format, ...);
253 JAYLINK_PRIV void log_dbgio(const struct jaylink_context *ctx,
254 const char *format, ...);
256 /*--- socket.c --------------------------------------------------------------*/
258 JAYLINK_PRIV bool socket_close(int sock);
259 JAYLINK_PRIV bool socket_bind(int sock, const struct sockaddr *address,
260 size_t length);
261 JAYLINK_PRIV bool socket_send(int sock, const void *buffer, size_t *length,
262 int flags);
263 JAYLINK_PRIV bool socket_recv(int sock, void *buffer, size_t *length,
264 int flags);
265 JAYLINK_PRIV bool socket_sendto(int sock, const void *buffer, size_t *length,
266 int flags, const struct sockaddr *address,
267 size_t address_length);
268 JAYLINK_PRIV bool socket_recvfrom(int sock, void *buffer, size_t *length,
269 int flags, struct sockaddr *address, size_t *address_length);
270 JAYLINK_PRIV bool socket_set_option(int sock, int level, int option,
271 const void *value, size_t length);
273 /*--- transport.c -----------------------------------------------------------*/
275 JAYLINK_PRIV int transport_open(struct jaylink_device_handle *devh);
276 JAYLINK_PRIV int transport_close(struct jaylink_device_handle *devh);
277 JAYLINK_PRIV int transport_start_write_read(struct jaylink_device_handle *devh,
278 size_t write_length, size_t read_length, bool has_command);
279 JAYLINK_PRIV int transport_start_write(struct jaylink_device_handle *devh,
280 size_t length, bool has_command);
281 JAYLINK_PRIV int transport_start_read(struct jaylink_device_handle *devh,
282 size_t length);
283 JAYLINK_PRIV int transport_write(struct jaylink_device_handle *devh,
284 const uint8_t *buffer, size_t length);
285 JAYLINK_PRIV int transport_read(struct jaylink_device_handle *devh,
286 uint8_t *buffer, size_t length);
288 /*--- transport_usb.c -------------------------------------------------------*/
290 JAYLINK_PRIV int transport_usb_open(struct jaylink_device_handle *devh);
291 JAYLINK_PRIV int transport_usb_close(struct jaylink_device_handle *devh);
292 JAYLINK_PRIV int transport_usb_start_write_read(
293 struct jaylink_device_handle *devh, size_t write_length,
294 size_t read_length, bool has_command);
295 JAYLINK_PRIV int transport_usb_start_write(struct jaylink_device_handle *devh,
296 size_t length, bool has_command);
297 JAYLINK_PRIV int transport_usb_start_read(struct jaylink_device_handle *devh,
298 size_t length);
299 JAYLINK_PRIV int transport_usb_write(struct jaylink_device_handle *devh,
300 const uint8_t *buffer, size_t length);
301 JAYLINK_PRIV int transport_usb_read(struct jaylink_device_handle *devh,
302 uint8_t *buffer, size_t length);
304 /*--- transport_tcp.c -------------------------------------------------------*/
306 JAYLINK_PRIV int transport_tcp_open(struct jaylink_device_handle *devh);
307 JAYLINK_PRIV int transport_tcp_close(struct jaylink_device_handle *devh);
308 JAYLINK_PRIV int transport_tcp_start_write_read(
309 struct jaylink_device_handle *devh, size_t write_length,
310 size_t read_length, bool has_command);
311 JAYLINK_PRIV int transport_tcp_start_write(struct jaylink_device_handle *devh,
312 size_t length, bool has_command);
313 JAYLINK_PRIV int transport_tcp_start_read(struct jaylink_device_handle *devh,
314 size_t length);
315 JAYLINK_PRIV int transport_tcp_write(struct jaylink_device_handle *devh,
316 const uint8_t *buffer, size_t length);
317 JAYLINK_PRIV int transport_tcp_read(struct jaylink_device_handle *devh,
318 uint8_t *buffer, size_t length);
320 #endif /* LIBJAYLINK_LIBJAYLINK_INTERNAL_H */