Add JAYLINK_LOG_LEVEL_DEBUG_IO
[libjaylink.git] / libjaylink / libjaylink.h
blob223aa849dea7bae41c2b679a923f387f2054284b
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_H
21 #define LIBJAYLINK_LIBJAYLINK_H
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <stdarg.h>
27 #ifdef _WIN32
28 #include <ws2tcpip.h>
29 #else
30 #include <arpa/inet.h>
31 #endif
33 /**
34 * @file
36 * Public libjaylink header file to be used by applications.
39 /** Error codes returned by libjaylink functions. */
40 enum jaylink_error {
41 /** No error. */
42 JAYLINK_OK = 0,
43 /** Unspecified error. */
44 JAYLINK_ERR = -1,
45 /** Invalid argument. */
46 JAYLINK_ERR_ARG = -2,
47 /** Memory allocation error. */
48 JAYLINK_ERR_MALLOC = -3,
49 /** Timeout occurred. */
50 JAYLINK_ERR_TIMEOUT = -4,
51 /** Protocol violation. */
52 JAYLINK_ERR_PROTO = -5,
53 /** Entity not available. */
54 JAYLINK_ERR_NOT_AVAILABLE = -6,
55 /** Operation not supported. */
56 JAYLINK_ERR_NOT_SUPPORTED = -7,
57 /** Input/output error. */
58 JAYLINK_ERR_IO = -8,
59 /** Device: unspecified error. */
60 JAYLINK_ERR_DEV = -1000,
61 /** Device: operation not supported. */
62 JAYLINK_ERR_DEV_NOT_SUPPORTED = -1001,
63 /** Device: entity not available. */
64 JAYLINK_ERR_DEV_NOT_AVAILABLE = -1002,
65 /** Device: not enough memory to perform operation. */
66 JAYLINK_ERR_DEV_NO_MEMORY = -1003
69 /** libjaylink log levels. */
70 enum jaylink_log_level {
71 /** Output no messages. */
72 JAYLINK_LOG_LEVEL_NONE = 0,
73 /** Output error messages. */
74 JAYLINK_LOG_LEVEL_ERROR = 1,
75 /** Output warnings. */
76 JAYLINK_LOG_LEVEL_WARNING = 2,
77 /** Output informational messages. */
78 JAYLINK_LOG_LEVEL_INFO = 3,
79 /** Output debug messages. */
80 JAYLINK_LOG_LEVEL_DEBUG = 4,
81 /** Output I/O debug messages. */
82 JAYLINK_LOG_LEVEL_DEBUG_IO = 5
85 /** Default libjaylink log domain. */
86 #define JAYLINK_LOG_DOMAIN_DEFAULT "jaylink: "
88 /** Maximum length of a libjaylink log domain in bytes. */
89 #define JAYLINK_LOG_DOMAIN_MAX_LENGTH 32
91 /** libjaylink capabilities. */
92 enum jaylink_capability {
93 /** Library supports USB as host interface. */
94 JAYLINK_CAP_HIF_USB = 0
97 /** Host interfaces. */
98 enum jaylink_host_interface {
99 /** Universal Serial Bus (USB). */
100 JAYLINK_HIF_USB = (1 << 0),
101 /** Transmission Control Protocol (TCP). */
102 JAYLINK_HIF_TCP = (1 << 1)
106 * USB addresses.
108 * The USB address is a way to identify USB devices and is related to the USB
109 * Product ID (PID) of a device.
111 enum jaylink_usb_address {
112 /** USB address 0 (Product ID 0x0101). */
113 JAYLINK_USB_ADDRESS_0 = 0,
114 /** USB address 1 (Product ID 0x0102). */
115 JAYLINK_USB_ADDRESS_1 = 1,
116 /** USB address 2 (Product ID 0x0103). */
117 JAYLINK_USB_ADDRESS_2 = 2,
118 /** USB address 3 (Product ID 0x0104). */
119 JAYLINK_USB_ADDRESS_3 = 3
122 /** Device capabilities. */
123 enum jaylink_device_capability {
124 /** Device supports retrieval of the hardware version. */
125 JAYLINK_DEV_CAP_GET_HW_VERSION = 1,
126 /** Device supports adaptive clocking. */
127 JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING = 3,
128 /** Device supports reading configuration data. */
129 JAYLINK_DEV_CAP_READ_CONFIG = 4,
130 /** Device supports writing configuration data. */
131 JAYLINK_DEV_CAP_WRITE_CONFIG = 5,
132 /** Device supports retrieval of target interface speeds. */
133 JAYLINK_DEV_CAP_GET_SPEEDS = 9,
134 /** Device supports retrieval of free memory size. */
135 JAYLINK_DEV_CAP_GET_FREE_MEMORY = 11,
136 /** Device supports retrieval of hardware information. */
137 JAYLINK_DEV_CAP_GET_HW_INFO = 12,
138 /** Device supports the setting of the target power supply. */
139 JAYLINK_DEV_CAP_SET_TARGET_POWER = 13,
140 /** Device supports target interface selection. */
141 JAYLINK_DEV_CAP_SELECT_TIF = 17,
142 /** Device supports retrieval of counter values. */
143 JAYLINK_DEV_CAP_GET_COUNTERS = 19,
144 /** Device supports capturing of SWO trace data. */
145 JAYLINK_DEV_CAP_SWO = 23,
146 /** Device supports file I/O operations. */
147 JAYLINK_DEV_CAP_FILE_IO = 26,
148 /** Device supports registration of connections. */
149 JAYLINK_DEV_CAP_REGISTER = 27,
150 /** Device supports retrieval of extended capabilities. */
151 JAYLINK_DEV_CAP_GET_EXT_CAPS = 31,
152 /** Device supports EMUCOM. */
153 JAYLINK_DEV_CAP_EMUCOM = 33,
154 /** Device supports ethernet connectivity. */
155 JAYLINK_DEV_CAP_ETHERNET = 38
158 /** Hardware information. */
159 enum jaylink_hardware_info {
161 * Status of the target power supply.
163 * This indicates whether the target power supply on pin 19 of the
164 * 20-pin JTAG / SWD connector is enabled or disabled.
166 * @see jaylink_set_target_power()
168 JAYLINK_HW_INFO_TARGET_POWER = (1 << 0),
169 /** Current consumption of the target in mA. */
170 JAYLINK_HW_INFO_ITARGET = (1 << 2),
171 /** Peak current consumption of the target in mA. */
172 JAYLINK_HW_INFO_ITARGET_PEAK = (1 << 3)
175 /** Device counters. */
176 enum jaylink_counter {
177 /** Time the device is connected to a target in milliseconds. */
178 JAYLINK_COUNTER_TARGET_TIME = (1 << 0),
180 * Number of times the device was connected or disconnected from a
181 * target.
183 JAYLINK_COUNTER_TARGET_CONNECTIONS = (1 << 1)
186 /** Device hardware types. */
187 enum jaylink_hardware_type {
188 /** J-Link. */
189 JAYLINK_HW_TYPE_JLINK = 0,
190 /** Flasher. */
191 JAYLINK_HW_TYPE_FLASHER = 2,
192 /** J-Link Pro. */
193 JAYLINK_HW_TYPE_JLINK_PRO = 3
196 /** Target interfaces. */
197 enum jaylink_target_interface {
198 /** Joint Test Action Group, IEEE 1149.1 (JTAG). */
199 JAYLINK_TIF_JTAG = 0,
200 /** Serial Wire Debug (SWD). */
201 JAYLINK_TIF_SWD = 1,
202 /** Background Debug Mode 3 (BDM3). */
203 JAYLINK_TIF_BDM3 = 2,
204 /** Renesas’ single-wire debug interface (FINE). */
205 JAYLINK_TIF_FINE = 3,
206 /** 2-wire JTAG for PIC32 compliant devices. */
207 JAYLINK_TIF_2W_JTAG_PIC32 = 4,
211 * JTAG command versions.
213 * The JTAG command version only affects the device and the communication
214 * protocol. The behaviour of a JTAG operation is not affected at all.
216 enum jaylink_jtag_version {
218 * JTAG command version 2.
220 * This version is obsolete for major hardware version 5 and above. Use
221 * #JAYLINK_JTAG_VERSION_3 for these versions instead.
223 JAYLINK_JTAG_VERSION_2 = 1,
224 /** JTAG command version 3. */
225 JAYLINK_JTAG_VERSION_3 = 2
228 /** Serial Wire Output (SWO) capture modes. */
229 enum jaylink_swo_mode {
230 /** Universal Asynchronous Receiver Transmitter (UART). */
231 JAYLINK_SWO_MODE_UART = 0
234 /** Target interface speed information. */
235 struct jaylink_speed {
236 /** Base frequency in Hz. */
237 uint32_t freq;
238 /** Minimum frequency divider. */
239 uint16_t div;
242 /** Serial Wire Output (SWO) speed information. */
243 struct jaylink_swo_speed {
244 /** Base frequency in Hz. */
245 uint32_t freq;
246 /** Minimum frequency divider. */
247 uint32_t min_div;
248 /** Maximum frequency divider. */
249 uint32_t max_div;
250 /** Minimum prescaler. */
251 uint32_t min_prescaler;
252 /** Maximum prescaler. */
253 uint32_t max_prescaler;
256 /** Device hardware version. */
257 struct jaylink_hardware_version {
258 /** Hardware type. */
259 enum jaylink_hardware_type type;
260 /** Major version. */
261 uint8_t major;
262 /** Minor version. */
263 uint8_t minor;
264 /** Revision number. */
265 uint8_t revision;
268 /** Device hardware status. */
269 struct jaylink_hardware_status {
270 /** Target reference voltage in mV. */
271 uint16_t target_voltage;
272 /** TCK pin state. */
273 bool tck;
274 /** TDI pin state. */
275 bool tdi;
276 /** TDO pin state. */
277 bool tdo;
278 /** TMS pin state. */
279 bool tms;
280 /** TRES pin state. */
281 bool tres;
282 /** TRST pin state. */
283 bool trst;
286 /** Device connection. */
287 struct jaylink_connection {
288 /** Handle. */
289 uint16_t handle;
291 * Process ID (PID).
293 * Identification of the client process. Usually this is the
294 * Process ID (PID) of the client process in an arbitrary format.
296 uint32_t pid;
298 * Host ID (HID).
300 * IPv4 address string of the client in quad-dotted decimal format
301 * (e.g. 192.0.2.235). The address 0.0.0.0 should be used for the
302 * registration of an USB connection.
304 char hid[INET_ADDRSTRLEN];
305 /** IID. */
306 uint8_t iid;
307 /** CID. */
308 uint8_t cid;
310 * Timestamp of the last registration in milliseconds.
312 * The timestamp is relative to the time the device was powered up.
314 uint32_t timestamp;
317 /** Target interface speed value for adaptive clocking. */
318 #define JAYLINK_SPEED_ADAPTIVE_CLOCKING 0xffff
320 /** Size of the device configuration data in bytes. */
321 #define JAYLINK_DEV_CONFIG_SIZE 256
323 /** Number of bytes required to store device capabilities. */
324 #define JAYLINK_DEV_CAPS_SIZE 4
326 /** Number of bytes required to store extended device capabilities. */
327 #define JAYLINK_DEV_EXT_CAPS_SIZE 32
329 /** Maximum number of connections that can be registered on a device. */
330 #define JAYLINK_MAX_CONNECTIONS 16
332 /** Media Access Control (MAC) address length in bytes. */
333 #define JAYLINK_MAC_ADDRESS_LENGTH 6
336 * Maximum length of a device's nickname including trailing null-terminator in
337 * bytes.
339 #define JAYLINK_NICKNAME_MAX_LENGTH 32
342 * Maximum length of a device's product name including trailing null-terminator
343 * in bytes.
345 #define JAYLINK_PRODUCT_NAME_MAX_LENGTH 32
347 /** Maximum length of a filename in bytes. */
348 #define JAYLINK_FILE_NAME_MAX_LENGTH 255
350 /** Maximum transfer size for a file in bytes. */
351 #define JAYLINK_FILE_MAX_TRANSFER_SIZE 0x100000
354 * EMUCOM channel with the system time of the device in milliseconds.
356 * The channel is read-only and the time is encoded in 4 bytes. The byte order
357 * is little-endian.
359 #define JAYLINK_EMUCOM_CHANNEL_TIME 0x0
362 * Offset of EMUCOM user channels.
364 * User channels are available to implement vendor and/or device specific
365 * functionalities. All channels below are reserved.
367 #define JAYLINK_EMUCOM_CHANNEL_USER 0x10000
370 * @struct jaylink_context
372 * Opaque structure representing a libjaylink context.
374 struct jaylink_context;
377 * @struct jaylink_device
379 * Opaque structure representing a device.
381 struct jaylink_device;
384 * @struct jaylink_device_handle
386 * Opaque structure representing a handle of a device.
388 struct jaylink_device_handle;
390 /** Macro to mark public libjaylink API symbol. */
391 #ifdef _WIN32
392 #define JAYLINK_API
393 #else
394 #define JAYLINK_API __attribute__ ((visibility ("default")))
395 #endif
398 * Log callback function type.
400 * @param[in] ctx libjaylink context.
401 * @param[in] level Log level.
402 * @param[in] format Message format in printf()-style.
403 * @param[in] args Message arguments.
404 * @param[in,out] user_data User data passed to the callback function.
406 * @return Number of characters printed on success, or a negative error code on
407 * failure.
409 typedef int (*jaylink_log_callback)(const struct jaylink_context *ctx,
410 enum jaylink_log_level level, const char *format, va_list args,
411 void *user_data);
413 /*--- core.c ----------------------------------------------------------------*/
415 JAYLINK_API int jaylink_init(struct jaylink_context **ctx);
416 JAYLINK_API int jaylink_exit(struct jaylink_context *ctx);
417 JAYLINK_API bool jaylink_library_has_cap(enum jaylink_capability cap);
419 /*--- device.c --------------------------------------------------------------*/
421 JAYLINK_API int jaylink_get_devices(struct jaylink_context *ctx,
422 struct jaylink_device ***devs, size_t *count);
423 JAYLINK_API void jaylink_free_devices(struct jaylink_device **devs,
424 bool unref);
425 JAYLINK_API int jaylink_device_get_host_interface(
426 const struct jaylink_device *dev,
427 enum jaylink_host_interface *iface);
428 JAYLINK_API int jaylink_device_get_serial_number(
429 const struct jaylink_device *dev, uint32_t *serial_number);
430 JAYLINK_API int jaylink_device_get_usb_address(
431 const struct jaylink_device *dev,
432 enum jaylink_usb_address *address);
433 JAYLINK_API int jaylink_device_get_ipv4_address(
434 const struct jaylink_device *dev, char *address);
435 JAYLINK_API int jaylink_device_get_mac_address(
436 const struct jaylink_device *dev, uint8_t *address);
437 JAYLINK_API int jaylink_device_get_hardware_version(
438 const struct jaylink_device *dev,
439 struct jaylink_hardware_version *version);
440 JAYLINK_API int jaylink_device_get_product_name(
441 const struct jaylink_device *dev, char *name);
442 JAYLINK_API int jaylink_device_get_nickname(const struct jaylink_device *dev,
443 char *nickname);
444 JAYLINK_API struct jaylink_device *jaylink_ref_device(
445 struct jaylink_device *dev);
446 JAYLINK_API void jaylink_unref_device(struct jaylink_device *dev);
447 JAYLINK_API int jaylink_open(struct jaylink_device *dev,
448 struct jaylink_device_handle **devh);
449 JAYLINK_API int jaylink_close(struct jaylink_device_handle *devh);
450 JAYLINK_API struct jaylink_device *jaylink_get_device(
451 struct jaylink_device_handle *devh);
452 JAYLINK_API int jaylink_get_firmware_version(
453 struct jaylink_device_handle *devh, char **version,
454 size_t *length);
455 JAYLINK_API int jaylink_get_hardware_info(struct jaylink_device_handle *devh,
456 uint32_t mask, uint32_t *info);
457 JAYLINK_API int jaylink_get_counters(struct jaylink_device_handle *devh,
458 uint32_t mask, uint32_t *values);
459 JAYLINK_API int jaylink_get_hardware_version(
460 struct jaylink_device_handle *devh,
461 struct jaylink_hardware_version *version);
462 JAYLINK_API int jaylink_get_hardware_status(struct jaylink_device_handle *devh,
463 struct jaylink_hardware_status *status);
464 JAYLINK_API int jaylink_get_caps(struct jaylink_device_handle *devh,
465 uint8_t *caps);
466 JAYLINK_API int jaylink_get_extended_caps(struct jaylink_device_handle *devh,
467 uint8_t *caps);
468 JAYLINK_API int jaylink_get_free_memory(struct jaylink_device_handle *devh,
469 uint32_t *size);
470 JAYLINK_API int jaylink_read_raw_config(struct jaylink_device_handle *devh,
471 uint8_t *config);
472 JAYLINK_API int jaylink_write_raw_config(struct jaylink_device_handle *devh,
473 const uint8_t *config);
474 JAYLINK_API int jaylink_register(struct jaylink_device_handle *devh,
475 struct jaylink_connection *connection,
476 struct jaylink_connection *connections, size_t *count);
477 JAYLINK_API int jaylink_unregister(struct jaylink_device_handle *devh,
478 const struct jaylink_connection *connection,
479 struct jaylink_connection *connections, size_t *count);
481 /*--- discovery.c -----------------------------------------------------------*/
483 JAYLINK_API int jaylink_discovery_scan(struct jaylink_context *ctx,
484 uint32_t ifaces);
486 /*--- emucom.c --------------------------------------------------------------*/
488 JAYLINK_API int jaylink_emucom_read(struct jaylink_device_handle *devh,
489 uint32_t channel, uint8_t *buffer, uint32_t *length);
490 JAYLINK_API int jaylink_emucom_write(struct jaylink_device_handle *devh,
491 uint32_t channel, const uint8_t *buffer, uint32_t *length);
493 /*--- error.c ---------------------------------------------------------------*/
495 JAYLINK_API const char *jaylink_strerror(int error_code);
496 JAYLINK_API const char *jaylink_strerror_name(int error_code);
498 /*--- fileio.c --------------------------------------------------------------*/
500 JAYLINK_API int jaylink_file_read(struct jaylink_device_handle *devh,
501 const char *filename, uint8_t *buffer, uint32_t offset,
502 uint32_t *length);
503 JAYLINK_API int jaylink_file_write(struct jaylink_device_handle *devh,
504 const char *filename, const uint8_t *buffer, uint32_t offset,
505 uint32_t *length);
506 JAYLINK_API int jaylink_file_get_size(struct jaylink_device_handle *devh,
507 const char *filename, uint32_t *size);
508 JAYLINK_API int jaylink_file_delete(struct jaylink_device_handle *devh,
509 const char *filename);
511 /*--- jtag.c ----------------------------------------------------------------*/
513 JAYLINK_API int jaylink_jtag_io(struct jaylink_device_handle *devh,
514 const uint8_t *tms, const uint8_t *tdi, uint8_t *tdo,
515 uint16_t length, enum jaylink_jtag_version version);
516 JAYLINK_API int jaylink_jtag_clear_trst(struct jaylink_device_handle *devh);
517 JAYLINK_API int jaylink_jtag_set_trst(struct jaylink_device_handle *devh);
519 /*--- log.c -----------------------------------------------------------------*/
521 JAYLINK_API int jaylink_log_set_level(struct jaylink_context *ctx,
522 enum jaylink_log_level level);
523 JAYLINK_API int jaylink_log_get_level(const struct jaylink_context *ctx,
524 enum jaylink_log_level *level);
525 JAYLINK_API int jaylink_log_set_callback(struct jaylink_context *ctx,
526 jaylink_log_callback callback, void *user_data);
527 JAYLINK_API int jaylink_log_set_domain(struct jaylink_context *ctx,
528 const char *domain);
529 JAYLINK_API const char *jaylink_log_get_domain(
530 const struct jaylink_context *ctx);
532 /*--- strutil.c -------------------------------------------------------------*/
534 JAYLINK_API int jaylink_parse_serial_number(const char *str,
535 uint32_t *serial_number);
537 /*--- swd.c -----------------------------------------------------------------*/
539 JAYLINK_API int jaylink_swd_io(struct jaylink_device_handle *devh,
540 const uint8_t *direction, const uint8_t *out, uint8_t *in,
541 uint16_t length);
543 /*--- swo.c -----------------------------------------------------------------*/
545 JAYLINK_API int jaylink_swo_start(struct jaylink_device_handle *devh,
546 enum jaylink_swo_mode mode, uint32_t baudrate, uint32_t size);
547 JAYLINK_API int jaylink_swo_stop(struct jaylink_device_handle *devh);
548 JAYLINK_API int jaylink_swo_read(struct jaylink_device_handle *devh,
549 uint8_t *buffer, uint32_t *length);
550 JAYLINK_API int jaylink_swo_get_speeds(struct jaylink_device_handle *devh,
551 enum jaylink_swo_mode mode, struct jaylink_swo_speed *speed);
553 /*--- target.c --------------------------------------------------------------*/
555 JAYLINK_API int jaylink_set_speed(struct jaylink_device_handle *devh,
556 uint16_t speed);
557 JAYLINK_API int jaylink_get_speeds(struct jaylink_device_handle *devh,
558 struct jaylink_speed *speed);
559 JAYLINK_API int jaylink_select_interface(struct jaylink_device_handle *devh,
560 enum jaylink_target_interface iface,
561 enum jaylink_target_interface *prev_iface);
562 JAYLINK_API int jaylink_get_available_interfaces(
563 struct jaylink_device_handle *devh, uint32_t *ifaces);
564 JAYLINK_API int jaylink_get_selected_interface(
565 struct jaylink_device_handle *devh,
566 enum jaylink_target_interface *iface);
567 JAYLINK_API int jaylink_clear_reset(struct jaylink_device_handle *devh);
568 JAYLINK_API int jaylink_set_reset(struct jaylink_device_handle *devh);
569 JAYLINK_API int jaylink_set_target_power(struct jaylink_device_handle *devh,
570 bool enable);
572 /*--- util.c ----------------------------------------------------------------*/
574 JAYLINK_API bool jaylink_has_cap(const uint8_t *caps, uint32_t cap);
576 /*--- version.c -------------------------------------------------------------*/
578 JAYLINK_API int jaylink_version_package_get_major(void);
579 JAYLINK_API int jaylink_version_package_get_minor(void);
580 JAYLINK_API int jaylink_version_package_get_micro(void);
581 JAYLINK_API const char *jaylink_version_package_get_string(void);
582 JAYLINK_API int jaylink_version_library_get_current(void);
583 JAYLINK_API int jaylink_version_library_get_revision(void);
584 JAYLINK_API int jaylink_version_library_get_age(void);
585 JAYLINK_API const char *jaylink_version_library_get_string(void);
587 #include "version.h"
589 #endif /* LIBJAYLINK_LIBJAYLINK_H */