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
27 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
43 #include "libjaylink.h"
48 * Internal libjaylink header file.
51 /** Macro to mark private libjaylink symbol. */
52 #if defined(_WIN32) || defined(__MSYS__) || defined(__CYGWIN__)
55 #define JAYLINK_PRIV __attribute__ ((visibility ("hidden")))
58 /** Calculate the minimum of two numeric values. */
59 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
61 struct jaylink_context
{
63 /** libusb context. */
64 struct libusb_context
*usb_ctx
;
67 * List of allocated device instances.
69 * Used to prevent multiple device instances for the same device.
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
;
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. */
89 /** Host interface. */
90 enum jaylink_host_interface iface
;
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 available. */
99 bool has_serial_number
;
101 /** libusb device instance. */
102 struct libusb_device
*usb_dev
;
103 /** USB address of the device. */
104 enum jaylink_usb_address usb_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
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
121 uint8_t mac_address
[JAYLINK_MAC_ADDRESS_LENGTH
];
122 /** Indicates whether the MAC address is available. */
123 bool has_mac_address
;
127 * This field is used for devices with host interface #JAYLINK_HIF_TCP
130 char product_name
[JAYLINK_PRODUCT_NAME_MAX_LENGTH
];
131 /** Indicates whether the product name is available. */
132 bool has_product_name
;
136 * This field is used for devices with host interface #JAYLINK_HIF_TCP
139 char nickname
[JAYLINK_NICKNAME_MAX_LENGTH
];
140 /** Indicates whether the nickname is available. */
145 * This field is used for devices with host interface #JAYLINK_HIF_TCP
148 struct jaylink_hardware_version hw_version
;
149 /** Indicates whether the hardware version is available. */
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.
165 /** Number of bytes left for the read operation. */
167 /** Number of bytes available in the buffer to be read. */
168 size_t bytes_available
;
169 /** Current read position in the buffer. */
172 * Number of bytes left to be written before the write operation will
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.
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. */
190 /** USB interface OUT endpoint of the device. */
191 uint8_t endpoint_out
;
196 * This field is used for devices with host interface #JAYLINK_HIF_TCP
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
,
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
,
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
,
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
int socket_connect(int sock
, const struct sockaddr
*address
,
259 size_t address_length
, size_t timeout
);
260 JAYLINK_PRIV
bool socket_close(int sock
);
261 JAYLINK_PRIV
bool socket_bind(int sock
, const struct sockaddr
*address
,
263 JAYLINK_PRIV
bool socket_send(int sock
, const void *buffer
, size_t *length
,
265 JAYLINK_PRIV
bool socket_recv(int sock
, void *buffer
, size_t *length
,
267 JAYLINK_PRIV
bool socket_sendto(int sock
, const void *buffer
, size_t *length
,
268 int flags
, const struct sockaddr
*address
,
269 size_t address_length
);
270 JAYLINK_PRIV
bool socket_recvfrom(int sock
, void *buffer
, size_t *length
,
271 int flags
, struct sockaddr
*address
, size_t *address_length
);
272 JAYLINK_PRIV
bool socket_get_option(int sock
, int level
, int option
,
273 void *value
, size_t *length
);
274 JAYLINK_PRIV
bool socket_set_option(int sock
, int level
, int option
,
275 const void *value
, size_t length
);
276 JAYLINK_PRIV
bool socket_set_blocking(int sock
, bool blocking
);
278 /*--- transport.c -----------------------------------------------------------*/
280 JAYLINK_PRIV
int transport_open(struct jaylink_device_handle
*devh
);
281 JAYLINK_PRIV
int transport_close(struct jaylink_device_handle
*devh
);
282 JAYLINK_PRIV
int transport_start_write_read(struct jaylink_device_handle
*devh
,
283 size_t write_length
, size_t read_length
, bool has_command
);
284 JAYLINK_PRIV
int transport_start_write(struct jaylink_device_handle
*devh
,
285 size_t length
, bool has_command
);
286 JAYLINK_PRIV
int transport_start_read(struct jaylink_device_handle
*devh
,
288 JAYLINK_PRIV
int transport_write(struct jaylink_device_handle
*devh
,
289 const uint8_t *buffer
, size_t length
);
290 JAYLINK_PRIV
int transport_read(struct jaylink_device_handle
*devh
,
291 uint8_t *buffer
, size_t length
);
293 /*--- transport_usb.c -------------------------------------------------------*/
295 JAYLINK_PRIV
int transport_usb_open(struct jaylink_device_handle
*devh
);
296 JAYLINK_PRIV
int transport_usb_close(struct jaylink_device_handle
*devh
);
297 JAYLINK_PRIV
int transport_usb_start_write_read(
298 struct jaylink_device_handle
*devh
, size_t write_length
,
299 size_t read_length
, bool has_command
);
300 JAYLINK_PRIV
int transport_usb_start_write(struct jaylink_device_handle
*devh
,
301 size_t length
, bool has_command
);
302 JAYLINK_PRIV
int transport_usb_start_read(struct jaylink_device_handle
*devh
,
304 JAYLINK_PRIV
int transport_usb_write(struct jaylink_device_handle
*devh
,
305 const uint8_t *buffer
, size_t length
);
306 JAYLINK_PRIV
int transport_usb_read(struct jaylink_device_handle
*devh
,
307 uint8_t *buffer
, size_t length
);
309 /*--- transport_tcp.c -------------------------------------------------------*/
311 JAYLINK_PRIV
int transport_tcp_open(struct jaylink_device_handle
*devh
);
312 JAYLINK_PRIV
int transport_tcp_close(struct jaylink_device_handle
*devh
);
313 JAYLINK_PRIV
int transport_tcp_start_write_read(
314 struct jaylink_device_handle
*devh
, size_t write_length
,
315 size_t read_length
, bool has_command
);
316 JAYLINK_PRIV
int transport_tcp_start_write(struct jaylink_device_handle
*devh
,
317 size_t length
, bool has_command
);
318 JAYLINK_PRIV
int transport_tcp_start_read(struct jaylink_device_handle
*devh
,
320 JAYLINK_PRIV
int transport_tcp_write(struct jaylink_device_handle
*devh
,
321 const uint8_t *buffer
, size_t length
);
322 JAYLINK_PRIV
int transport_tcp_read(struct jaylink_device_handle
*devh
,
323 uint8_t *buffer
, size_t length
);
325 #endif /* LIBJAYLINK_LIBJAYLINK_INTERNAL_H */