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
;
145 /** libusb device handle. */
146 struct libusb_device_handle
*usb_devh
;
147 /** USB interface number of the device. */
148 uint8_t interface_number
;
149 /** USB interface IN endpoint of the device. */
151 /** USB interface OUT endpoint of the device. */
152 uint8_t endpoint_out
;
154 * Buffer for write and read operations.
156 * Note that write and read operations are always processed
157 * consecutively and therefore the same buffer can be used for both.
162 /** Number of bytes left for the read operation. */
164 /** Number of bytes available in the buffer to be read. */
165 size_t bytes_available
;
166 /** Current read position in the buffer. */
169 * Number of bytes left to be written before the write operation will
174 * Current write position in the buffer.
176 * This is equivalent to the number of bytes in the buffer and used for
177 * write operations only.
187 typedef bool (*list_compare_callback
)(const void *data
, const void *user_data
);
189 /*--- buffer.c --------------------------------------------------------------*/
191 JAYLINK_PRIV
void buffer_set_u16(uint8_t *buffer
, uint16_t value
,
193 JAYLINK_PRIV
uint16_t buffer_get_u16(const uint8_t *buffer
, size_t offset
);
194 JAYLINK_PRIV
void buffer_set_u32(uint8_t *buffer
, uint32_t value
,
196 JAYLINK_PRIV
uint32_t buffer_get_u32(const uint8_t *buffer
, size_t offset
);
198 /*--- device.c --------------------------------------------------------------*/
200 JAYLINK_PRIV
struct jaylink_device
*device_allocate(
201 struct jaylink_context
*ctx
);
203 /*--- discovery_tcp.c -------------------------------------------------------*/
205 JAYLINK_PRIV
int discovery_tcp_scan(struct jaylink_context
*ctx
);
207 /*--- discovery_usb.c -------------------------------------------------------*/
209 JAYLINK_PRIV
int discovery_usb_scan(struct jaylink_context
*ctx
);
211 /*--- list.c ----------------------------------------------------------------*/
213 JAYLINK_PRIV
struct list
*list_prepend(struct list
*list
, void *data
);
214 JAYLINK_PRIV
struct list
*list_remove(struct list
*list
, const void *data
);
215 JAYLINK_PRIV
struct list
*list_find_custom(struct list
*list
,
216 list_compare_callback callback
, const void *user_data
);
217 JAYLINK_PRIV
size_t list_length(struct list
*list
);
218 JAYLINK_PRIV
void list_free(struct list
*list
);
220 /*--- log.c -----------------------------------------------------------------*/
222 JAYLINK_PRIV
int log_vprintf(const struct jaylink_context
*ctx
,
223 enum jaylink_log_level level
, const char *format
, va_list args
,
225 JAYLINK_PRIV
void log_err(const struct jaylink_context
*ctx
,
226 const char *format
, ...);
227 JAYLINK_PRIV
void log_warn(const struct jaylink_context
*ctx
,
228 const char *format
, ...);
229 JAYLINK_PRIV
void log_info(const struct jaylink_context
*ctx
,
230 const char *format
, ...);
231 JAYLINK_PRIV
void log_dbg(const struct jaylink_context
*ctx
,
232 const char *format
, ...);
234 /*--- socket.c --------------------------------------------------------------*/
236 JAYLINK_PRIV
bool socket_close(int sock
);
237 JAYLINK_PRIV
bool socket_bind(int sock
, const struct sockaddr
*address
,
239 JAYLINK_PRIV
bool socket_sendto(int sock
, const void *buffer
, size_t *length
,
240 int flags
, const struct sockaddr
*address
,
241 size_t address_length
);
242 JAYLINK_PRIV
bool socket_recvfrom(int sock
, void *buffer
, size_t *length
,
243 int flags
, struct sockaddr
*address
, size_t *address_length
);
244 JAYLINK_PRIV
bool socket_set_option(int sock
, int level
, int option
,
245 const void *value
, size_t length
);
247 /*--- transport.c -----------------------------------------------------------*/
249 JAYLINK_PRIV
int transport_open(struct jaylink_device_handle
*devh
);
250 JAYLINK_PRIV
int transport_close(struct jaylink_device_handle
*devh
);
251 JAYLINK_PRIV
int transport_start_write_read(struct jaylink_device_handle
*devh
,
252 size_t write_length
, size_t read_length
, bool has_command
);
253 JAYLINK_PRIV
int transport_start_write(struct jaylink_device_handle
*devh
,
254 size_t length
, bool has_command
);
255 JAYLINK_PRIV
int transport_start_read(struct jaylink_device_handle
*devh
,
257 JAYLINK_PRIV
int transport_write(struct jaylink_device_handle
*devh
,
258 const uint8_t *buffer
, size_t length
);
259 JAYLINK_PRIV
int transport_read(struct jaylink_device_handle
*devh
,
260 uint8_t *buffer
, size_t length
);
262 #endif /* LIBJAYLINK_LIBJAYLINK_INTERNAL_H */