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/>.
26 #include "libjaylink.h"
27 #include "libjaylink-internal.h"
32 * @section sec_intro Introduction
34 * This document describes the API of libjaylink.
36 * libjaylink is a shared library written in C to access SEGGER J-Link and
39 * @section sec_error Error handling
41 * The libjaylink functions which can fail use the return value to indicate an
42 * error. The functions typically return an error code of #jaylink_error.
43 * For each function, all possible error codes and their detailed descriptions
44 * are documented. As the possible error codes returned by a function may
45 * change it is recommended to also always cover unexpected values when
46 * checking for error codes to be compatible with later versions of libjaylink.
48 * There are a few exceptions where a function directly returns the result
49 * instead of an error code because it is more convenient from an API
50 * perspective and because there is only a single reason for failure which is
51 * clearly distinguishable from the result.
53 * @section sec_license License
55 * libjaylink is licensed under the terms of the GNU General Public
56 * License (GPL), version 2 or later.
62 * Core library functions.
66 * Initialize libjaylink.
68 * This function must be called before any other libjaylink function is called.
70 * @param[out] ctx Newly allocated libjaylink context on success, and undefined
73 * @retval JAYLINK_OK Success.
74 * @retval JAYLINK_ERR_ARG Invalid arguments.
75 * @retval JAYLINK_ERR_MALLOC Memory allocation error.
76 * @retval JAYLINK_ERR Other error conditions.
80 JAYLINK_API
int jaylink_init(struct jaylink_context
**ctx
)
83 struct jaylink_context
*context
;
89 return JAYLINK_ERR_ARG
;
91 context
= malloc(sizeof(struct jaylink_context
));
94 return JAYLINK_ERR_MALLOC
;
96 if (libusb_init(&context
->usb_ctx
) != LIBUSB_SUCCESS
) {
102 ret
= WSAStartup(MAKEWORD(2, 2), &wsa_data
);
105 libusb_exit(context
->usb_ctx
);
110 if (LOBYTE(wsa_data
.wVersion
) != 2 || HIBYTE(wsa_data
.wVersion
) != 2) {
111 libusb_exit(context
->usb_ctx
);
117 context
->devs
= NULL
;
118 context
->discovered_devs
= NULL
;
120 /* Show error and warning messages by default. */
121 context
->log_level
= JAYLINK_LOG_LEVEL_WARNING
;
123 context
->log_callback
= &log_vprintf
;
124 context
->log_callback_data
= NULL
;
126 ret
= jaylink_log_set_domain(context
, JAYLINK_LOG_DOMAIN_DEFAULT
);
128 if (ret
!= JAYLINK_OK
) {
142 * Shutdown libjaylink.
144 * @param[in,out] ctx libjaylink context.
146 * @retval JAYLINK_OK Success.
147 * @retval JAYLINK_ERR_ARG Invalid arguments.
151 JAYLINK_API
int jaylink_exit(struct jaylink_context
*ctx
)
156 return JAYLINK_ERR_ARG
;
158 item
= ctx
->discovered_devs
;
161 jaylink_unref_device((struct jaylink_device
*)item
->data
);
165 list_free(ctx
->discovered_devs
);
166 list_free(ctx
->devs
);
168 libusb_exit(ctx
->usb_ctx
);
179 * Check for a capability of libjaylink.
181 * @param[in] cap Capability to check for.
183 * @retval true Capability is supported.
184 * @retval false Capability is not supported or invalid argument.
188 JAYLINK_API
bool jaylink_library_has_cap(enum jaylink_capability cap
)
191 case JAYLINK_CAP_HIF_USB
: