discovery: Add initial TCP/IP support
[libjaylink.git] / libjaylink / libjaylink-internal.h
blob5da37dc1621d1f6d29067c5b751ec7ffb15d6512
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
34 #include <libusb.h>
36 #include "libjaylink.h"
38 /**
39 * @file
41 * Internal libjaylink header file.
44 /** Macro to mark private libjaylink symbol. */
45 #if defined(_WIN32) || defined(__MSYS__) || defined(__CYGWIN__)
46 #define JAYLINK_PRIV
47 #else
48 #define JAYLINK_PRIV __attribute__ ((visibility ("hidden")))
49 #endif
51 /** Calculate the minimum of two numeric values. */
52 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
54 /** Media Access Control (MAC) address length in bytes. */
55 #define MAC_ADDRESS_LENGTH 6
57 struct jaylink_context {
58 /** libusb context. */
59 struct libusb_context *usb_ctx;
60 /**
61 * List of allocated device instances.
63 * Used to prevent multiple device instances for the same device.
65 struct list *devs;
66 /** List of recently discovered devices. */
67 struct list *discovered_devs;
68 /** Current log level. */
69 enum jaylink_log_level log_level;
70 /** Log callback function. */
71 jaylink_log_callback log_callback;
72 /** User data to be passed to the log callback function. */
73 void *log_callback_data;
74 /** Log domain. */
75 char log_domain[JAYLINK_LOG_DOMAIN_MAX_LENGTH + 1];
78 struct jaylink_device {
79 /** libjaylink context. */
80 struct jaylink_context *ctx;
81 /** Number of references held on this device instance. */
82 size_t ref_count;
83 /** Host interface. */
84 enum jaylink_host_interface interface;
85 /**
86 * Serial number of the device.
88 * This number is for enumeration purpose only and can differ from the
89 * real serial number of the device.
91 uint32_t serial_number;
92 /** Indicates whether the serial number is valid. */
93 bool valid_serial_number;
94 /** libusb device instance. */
95 struct libusb_device *usb_dev;
96 /** USB address of the device. */
97 uint8_t usb_address;
98 /**
99 * IPv4 address.
101 * The address is encoded as string in quad-dotted decimal format.
103 * This field is used for devices with host interface #JAYLINK_HIF_TCP
104 * only.
106 char ipv4_address[INET_ADDRSTRLEN];
108 * Media Access Control (MAC) address.
110 * This field is used for devices with host interface #JAYLINK_HIF_TCP
111 * only.
113 uint8_t mac_address[MAC_ADDRESS_LENGTH];
114 /** Indicates whether the MAC address is available. */
115 bool has_mac_address;
117 * Product name.
119 * This field is used for devices with host interface #JAYLINK_HIF_TCP
120 * only.
122 char product_name[JAYLINK_PRODUCT_NAME_MAX_LENGTH];
123 /** Indicates whether the product name is available. */
124 bool has_product_name;
126 * Nickname.
128 * This field is used for devices with host interface #JAYLINK_HIF_TCP
129 * only.
131 char nickname[JAYLINK_NICKNAME_MAX_LENGTH];
132 /** Indicates whether the nickname is available. */
133 bool has_nickname;
135 * Hardware version.
137 * This field is used for devices with host interface #JAYLINK_HIF_TCP
138 * only.
140 struct jaylink_hardware_version hw_version;
141 /** Indicates whether the hardware version is available. */
142 bool has_hw_version;
145 struct jaylink_device_handle {
146 /** Device instance. */
147 struct jaylink_device *dev;
148 /** libusb device handle. */
149 struct libusb_device_handle *usb_devh;
150 /** USB interface number of the device. */
151 uint8_t interface_number;
152 /** USB interface IN endpoint of the device. */
153 uint8_t endpoint_in;
154 /** USB interface OUT endpoint of the device. */
155 uint8_t endpoint_out;
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;
185 struct list {
186 void *data;
187 struct list *next;
190 typedef bool (*list_compare_callback)(const void *data, const void *user_data);
192 /*--- buffer.c --------------------------------------------------------------*/
194 JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value,
195 size_t offset);
196 JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset);
197 JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value,
198 size_t offset);
199 JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset);
201 /*--- device.c --------------------------------------------------------------*/
203 JAYLINK_PRIV struct jaylink_device *device_allocate(
204 struct jaylink_context *ctx);
206 /*--- discovery_tcp.c -------------------------------------------------------*/
208 JAYLINK_PRIV int discovery_tcp_scan(struct jaylink_context *ctx);
210 /*--- discovery_usb.c -------------------------------------------------------*/
212 JAYLINK_PRIV int discovery_usb_scan(struct jaylink_context *ctx);
214 /*--- list.c ----------------------------------------------------------------*/
216 JAYLINK_PRIV struct list *list_prepend(struct list *list, void *data);
217 JAYLINK_PRIV struct list *list_remove(struct list *list, const void *data);
218 JAYLINK_PRIV struct list *list_find_custom(struct list *list,
219 list_compare_callback callback, const void *user_data);
220 JAYLINK_PRIV size_t list_length(struct list *list);
221 JAYLINK_PRIV void list_free(struct list *list);
223 /*--- log.c -----------------------------------------------------------------*/
225 JAYLINK_PRIV int log_vprintf(const struct jaylink_context *ctx,
226 enum jaylink_log_level level, const char *format, va_list args,
227 void *user_data);
228 JAYLINK_PRIV void log_err(const struct jaylink_context *ctx,
229 const char *format, ...);
230 JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx,
231 const char *format, ...);
232 JAYLINK_PRIV void log_info(const struct jaylink_context *ctx,
233 const char *format, ...);
234 JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx,
235 const char *format, ...);
237 /*--- socket.c --------------------------------------------------------------*/
239 JAYLINK_PRIV bool socket_close(int sock);
240 JAYLINK_PRIV bool socket_bind(int sock, const struct sockaddr *address,
241 size_t length);
242 JAYLINK_PRIV bool socket_sendto(int sock, const void *buffer, size_t *length,
243 int flags, const struct sockaddr *address,
244 size_t address_length);
245 JAYLINK_PRIV bool socket_recvfrom(int sock, void *buffer, size_t *length,
246 int flags, struct sockaddr *address, size_t *address_length);
247 JAYLINK_PRIV bool socket_set_option(int sock, int level, int option,
248 const void *value, size_t length);
250 /*--- transport.c -----------------------------------------------------------*/
252 JAYLINK_PRIV int transport_open(struct jaylink_device_handle *devh);
253 JAYLINK_PRIV int transport_close(struct jaylink_device_handle *devh);
254 JAYLINK_PRIV int transport_start_write_read(struct jaylink_device_handle *devh,
255 size_t write_length, size_t read_length, bool has_command);
256 JAYLINK_PRIV int transport_start_write(struct jaylink_device_handle *devh,
257 size_t length, bool has_command);
258 JAYLINK_PRIV int transport_start_read(struct jaylink_device_handle *devh,
259 size_t length);
260 JAYLINK_PRIV int transport_write(struct jaylink_device_handle *devh,
261 const uint8_t *buffer, size_t length);
262 JAYLINK_PRIV int transport_read(struct jaylink_device_handle *devh,
263 uint8_t *buffer, size_t length);
265 #endif /* LIBJAYLINK_LIBJAYLINK_INTERNAL_H */