Rename JTAG command version enums
[libjaylink.git] / libjaylink / libjaylink-internal.h
blobf22cb2acd81310e22eba1ca5116f2590089bff2b
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 <sys/types.h>
28 #include "libjaylink.h"
31 * libusb.h includes windows.h and therefore must be included after anything
32 * that includes winsock2.h.
34 #include <libusb.h>
36 /**
37 * @file
39 * Internal libjaylink header file.
42 /** Macro to mark private libjaylink symbol. */
43 #if defined(_WIN32) || defined(__MSYS__) || defined(__CYGWIN__)
44 #define JAYLINK_PRIV
45 #else
46 #define JAYLINK_PRIV __attribute__ ((visibility ("hidden")))
47 #endif
49 /** Calculate the minimum of two numeric values. */
50 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
52 struct jaylink_context {
53 /** libusb context. */
54 struct libusb_context *usb_ctx;
55 /**
56 * List of allocated device instances.
58 * Used to prevent multiple device instances for the same device.
60 struct list *devs;
61 /** List of recently discovered devices. */
62 struct list *discovered_devs;
63 /** Current log level. */
64 enum jaylink_log_level log_level;
65 /** Log callback function. */
66 jaylink_log_callback log_callback;
67 /** User data to be passed to the log callback function. */
68 void *log_callback_data;
69 /** Log domain. */
70 char log_domain[JAYLINK_LOG_DOMAIN_MAX_LENGTH + 1];
73 struct jaylink_device {
74 /** libjaylink context. */
75 struct jaylink_context *ctx;
76 /** Number of references held on this device instance. */
77 size_t ref_count;
78 /** Host interface. */
79 enum jaylink_host_interface interface;
80 /** libusb device instance. */
81 struct libusb_device *usb_dev;
82 /** USB address of the device. */
83 uint8_t usb_address;
84 /**
85 * Serial number of the device.
87 * This number is for enumeration purpose only and can differ from the
88 * real serial number of the device.
90 uint32_t serial_number;
91 /** Indicates whether the serial number is valid. */
92 bool valid_serial_number;
95 struct jaylink_device_handle {
96 /** Device instance. */
97 struct jaylink_device *dev;
98 /** libusb device handle. */
99 struct libusb_device_handle *usb_devh;
100 /** USB interface number of the device. */
101 uint8_t interface_number;
102 /** USB interface IN endpoint of the device. */
103 uint8_t endpoint_in;
104 /** USB interface OUT endpoint of the device. */
105 uint8_t endpoint_out;
107 * Buffer for write and read operations.
109 * Note that write and read operations are always processed
110 * consecutively and therefore the same buffer can be used for both.
112 uint8_t *buffer;
113 /** Buffer size. */
114 size_t buffer_size;
115 /** Number of bytes left for the read operation. */
116 size_t read_length;
117 /** Number of bytes available in the buffer to be read. */
118 size_t bytes_available;
119 /** Current read position in the buffer. */
120 size_t read_pos;
122 * Number of bytes left to be written before the write operation will
123 * be performed.
125 size_t write_length;
127 * Current write position in the buffer.
129 * This is equivalent to the number of bytes in the buffer and used for
130 * write operations only.
132 size_t write_pos;
135 struct list {
136 void *data;
137 struct list *next;
140 typedef bool (*list_compare_callback)(const void *a, const void *b);
142 /*--- buffer.c --------------------------------------------------------------*/
144 JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value,
145 size_t offset);
146 JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset);
147 JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value,
148 size_t offset);
149 JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset);
151 /*--- device.c --------------------------------------------------------------*/
153 JAYLINK_PRIV struct jaylink_device *device_allocate(
154 struct jaylink_context *ctx);
156 /*--- discovery.c -----------------------------------------------------------*/
158 JAYLINK_PRIV ssize_t discovery_get_device_list(struct jaylink_context *ctx,
159 struct jaylink_device ***list);
161 /*--- list.c ----------------------------------------------------------------*/
163 JAYLINK_PRIV struct list *list_prepend(struct list *list, void *data);
164 JAYLINK_PRIV struct list *list_remove(struct list *list, const void *data);
165 JAYLINK_PRIV struct list *list_find_custom(struct list *list,
166 list_compare_callback cb, const void *cb_data);
167 JAYLINK_PRIV size_t list_length(struct list *list);
168 JAYLINK_PRIV void list_free(struct list *list);
170 /*--- log.c -----------------------------------------------------------------*/
172 JAYLINK_PRIV int log_vprintf(const struct jaylink_context *ctx,
173 enum jaylink_log_level level, const char *format, va_list args,
174 void *user_data);
175 JAYLINK_PRIV void log_err(const struct jaylink_context *ctx,
176 const char *format, ...);
177 JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx,
178 const char *format, ...);
179 JAYLINK_PRIV void log_info(const struct jaylink_context *ctx,
180 const char *format, ...);
181 JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx,
182 const char *format, ...);
184 /*--- transport.c -----------------------------------------------------------*/
186 JAYLINK_PRIV int transport_open(struct jaylink_device_handle *devh);
187 JAYLINK_PRIV int transport_close(struct jaylink_device_handle *devh);
188 JAYLINK_PRIV int transport_start_write_read(struct jaylink_device_handle *devh,
189 size_t write_length, size_t read_length, bool has_command);
190 JAYLINK_PRIV int transport_start_write(struct jaylink_device_handle *devh,
191 size_t length, bool has_command);
192 JAYLINK_PRIV int transport_start_read(struct jaylink_device_handle *devh,
193 size_t length);
194 JAYLINK_PRIV int transport_write(struct jaylink_device_handle *devh,
195 const uint8_t *buffer, size_t length);
196 JAYLINK_PRIV int transport_read(struct jaylink_device_handle *devh,
197 uint8_t *buffer, size_t length);
199 #endif /* LIBJAYLINK_LIBJAYLINK_INTERNAL_H */