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>
36 #include "libjaylink.h"
41 * Internal libjaylink header file.
44 /** Macro to mark private libjaylink symbol. */
45 #if defined(_WIN32) || defined(__MSYS__) || defined(__CYGWIN__)
48 #define JAYLINK_PRIV __attribute__ ((visibility ("hidden")))
51 /** Calculate the minimum of two numeric values. */
52 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
54 struct jaylink_context
{
55 /** libusb context. */
56 struct libusb_context
*usb_ctx
;
58 * List of allocated device instances.
60 * Used to prevent multiple device instances for the same device.
63 /** List of recently discovered devices. */
64 struct list
*discovered_devs
;
65 /** Current log level. */
66 enum jaylink_log_level log_level
;
67 /** Log callback function. */
68 jaylink_log_callback log_callback
;
69 /** User data to be passed to the log callback function. */
70 void *log_callback_data
;
72 char log_domain
[JAYLINK_LOG_DOMAIN_MAX_LENGTH
+ 1];
75 struct jaylink_device
{
76 /** libjaylink context. */
77 struct jaylink_context
*ctx
;
78 /** Number of references held on this device instance. */
80 /** Host interface. */
81 enum jaylink_host_interface interface
;
83 * Serial number of the device.
85 * This number is for enumeration purpose only and can differ from the
86 * real serial number of the device.
88 uint32_t serial_number
;
89 /** Indicates whether the serial number is valid. */
90 bool valid_serial_number
;
91 /** libusb device instance. */
92 struct libusb_device
*usb_dev
;
93 /** USB address of the device. */
98 * The address is encoded as string in quad-dotted decimal format.
100 * This field is used for devices with host interface #JAYLINK_HIF_TCP
103 char ipv4_address
[INET_ADDRSTRLEN
];
105 * Media Access Control (MAC) address.
107 * This field is used for devices with host interface #JAYLINK_HIF_TCP
110 uint8_t mac_address
[JAYLINK_MAC_ADDRESS_LENGTH
];
111 /** Indicates whether the MAC address is available. */
112 bool has_mac_address
;
116 * This field is used for devices with host interface #JAYLINK_HIF_TCP
119 char product_name
[JAYLINK_PRODUCT_NAME_MAX_LENGTH
];
120 /** Indicates whether the product name is available. */
121 bool has_product_name
;
125 * This field is used for devices with host interface #JAYLINK_HIF_TCP
128 char nickname
[JAYLINK_NICKNAME_MAX_LENGTH
];
129 /** Indicates whether the nickname is available. */
134 * This field is used for devices with host interface #JAYLINK_HIF_TCP
137 struct jaylink_hardware_version hw_version
;
138 /** Indicates whether the hardware version is available. */
142 struct jaylink_device_handle
{
143 /** Device instance. */
144 struct jaylink_device
*dev
;
146 * Buffer for write and read operations.
148 * Note that write and read operations are always processed
149 * consecutively and therefore the same buffer can be used for both.
154 /** Number of bytes left for the read operation. */
156 /** Number of bytes available in the buffer to be read. */
157 size_t bytes_available
;
158 /** Current read position in the buffer. */
161 * Number of bytes left to be written before the write operation will
166 * Current write position in the buffer.
168 * This is equivalent to the number of bytes in the buffer and used for
169 * write operations only.
172 /** libusb device handle. */
173 struct libusb_device_handle
*usb_devh
;
174 /** USB interface number of the device. */
175 uint8_t interface_number
;
176 /** USB interface IN endpoint of the device. */
178 /** USB interface OUT endpoint of the device. */
179 uint8_t endpoint_out
;
183 * This field is used for devices with host interface #JAYLINK_HIF_TCP
194 typedef bool (*list_compare_callback
)(const void *data
, const void *user_data
);
196 /*--- buffer.c --------------------------------------------------------------*/
198 JAYLINK_PRIV
void buffer_set_u16(uint8_t *buffer
, uint16_t value
,
200 JAYLINK_PRIV
uint16_t buffer_get_u16(const uint8_t *buffer
, size_t offset
);
201 JAYLINK_PRIV
void buffer_set_u32(uint8_t *buffer
, uint32_t value
,
203 JAYLINK_PRIV
uint32_t buffer_get_u32(const uint8_t *buffer
, size_t offset
);
205 /*--- device.c --------------------------------------------------------------*/
207 JAYLINK_PRIV
struct jaylink_device
*device_allocate(
208 struct jaylink_context
*ctx
);
210 /*--- discovery_tcp.c -------------------------------------------------------*/
212 JAYLINK_PRIV
int discovery_tcp_scan(struct jaylink_context
*ctx
);
214 /*--- discovery_usb.c -------------------------------------------------------*/
216 JAYLINK_PRIV
int discovery_usb_scan(struct jaylink_context
*ctx
);
218 /*--- list.c ----------------------------------------------------------------*/
220 JAYLINK_PRIV
struct list
*list_prepend(struct list
*list
, void *data
);
221 JAYLINK_PRIV
struct list
*list_remove(struct list
*list
, const void *data
);
222 JAYLINK_PRIV
struct list
*list_find_custom(struct list
*list
,
223 list_compare_callback callback
, const void *user_data
);
224 JAYLINK_PRIV
size_t list_length(struct list
*list
);
225 JAYLINK_PRIV
void list_free(struct list
*list
);
227 /*--- log.c -----------------------------------------------------------------*/
229 JAYLINK_PRIV
int log_vprintf(const struct jaylink_context
*ctx
,
230 enum jaylink_log_level level
, const char *format
, va_list args
,
232 JAYLINK_PRIV
void log_err(const struct jaylink_context
*ctx
,
233 const char *format
, ...);
234 JAYLINK_PRIV
void log_warn(const struct jaylink_context
*ctx
,
235 const char *format
, ...);
236 JAYLINK_PRIV
void log_info(const struct jaylink_context
*ctx
,
237 const char *format
, ...);
238 JAYLINK_PRIV
void log_dbg(const struct jaylink_context
*ctx
,
239 const char *format
, ...);
241 /*--- socket.c --------------------------------------------------------------*/
243 JAYLINK_PRIV
bool socket_close(int sock
);
244 JAYLINK_PRIV
bool socket_bind(int sock
, const struct sockaddr
*address
,
246 JAYLINK_PRIV
bool socket_send(int sock
, const void *buffer
, size_t *length
,
248 JAYLINK_PRIV
bool socket_recv(int sock
, void *buffer
, size_t *length
,
250 JAYLINK_PRIV
bool socket_sendto(int sock
, const void *buffer
, size_t *length
,
251 int flags
, const struct sockaddr
*address
,
252 size_t address_length
);
253 JAYLINK_PRIV
bool socket_recvfrom(int sock
, void *buffer
, size_t *length
,
254 int flags
, struct sockaddr
*address
, size_t *address_length
);
255 JAYLINK_PRIV
bool socket_set_option(int sock
, int level
, int option
,
256 const void *value
, size_t length
);
258 /*--- transport.c -----------------------------------------------------------*/
260 JAYLINK_PRIV
int transport_open(struct jaylink_device_handle
*devh
);
261 JAYLINK_PRIV
int transport_close(struct jaylink_device_handle
*devh
);
262 JAYLINK_PRIV
int transport_start_write_read(struct jaylink_device_handle
*devh
,
263 size_t write_length
, size_t read_length
, bool has_command
);
264 JAYLINK_PRIV
int transport_start_write(struct jaylink_device_handle
*devh
,
265 size_t length
, bool has_command
);
266 JAYLINK_PRIV
int transport_start_read(struct jaylink_device_handle
*devh
,
268 JAYLINK_PRIV
int transport_write(struct jaylink_device_handle
*devh
,
269 const uint8_t *buffer
, size_t length
);
270 JAYLINK_PRIV
int transport_read(struct jaylink_device_handle
*devh
,
271 uint8_t *buffer
, size_t length
);
273 /*--- transport_usb.c -------------------------------------------------------*/
275 JAYLINK_PRIV
int transport_usb_open(struct jaylink_device_handle
*devh
);
276 JAYLINK_PRIV
int transport_usb_close(struct jaylink_device_handle
*devh
);
277 JAYLINK_PRIV
int transport_usb_start_write_read(
278 struct jaylink_device_handle
*devh
, size_t write_length
,
279 size_t read_length
, bool has_command
);
280 JAYLINK_PRIV
int transport_usb_start_write(struct jaylink_device_handle
*devh
,
281 size_t length
, bool has_command
);
282 JAYLINK_PRIV
int transport_usb_start_read(struct jaylink_device_handle
*devh
,
284 JAYLINK_PRIV
int transport_usb_write(struct jaylink_device_handle
*devh
,
285 const uint8_t *buffer
, size_t length
);
286 JAYLINK_PRIV
int transport_usb_read(struct jaylink_device_handle
*devh
,
287 uint8_t *buffer
, size_t length
);
289 /*--- transport_tcp.c -------------------------------------------------------*/
291 JAYLINK_PRIV
int transport_tcp_open(struct jaylink_device_handle
*devh
);
292 JAYLINK_PRIV
int transport_tcp_close(struct jaylink_device_handle
*devh
);
293 JAYLINK_PRIV
int transport_tcp_start_write_read(
294 struct jaylink_device_handle
*devh
, size_t write_length
,
295 size_t read_length
, bool has_command
);
296 JAYLINK_PRIV
int transport_tcp_start_write(struct jaylink_device_handle
*devh
,
297 size_t length
, bool has_command
);
298 JAYLINK_PRIV
int transport_tcp_start_read(struct jaylink_device_handle
*devh
,
300 JAYLINK_PRIV
int transport_tcp_write(struct jaylink_device_handle
*devh
,
301 const uint8_t *buffer
, size_t length
);
302 JAYLINK_PRIV
int transport_tcp_read(struct jaylink_device_handle
*devh
,
303 uint8_t *buffer
, size_t length
);
305 #endif /* LIBJAYLINK_LIBJAYLINK_INTERNAL_H */