configure.ac: Add 'check-news' Automake option
[libjaylink.git] / libjaylink / core.c
blob6bf4cc4a01316df3b1324a6be4a49bd3c86987bb
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 #include <stdlib.h>
21 #ifdef _WIN32
22 #include <winsock2.h>
23 #endif
24 #include <libusb.h>
26 #include "libjaylink.h"
27 #include "libjaylink-internal.h"
29 /**
30 * @mainpage
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
37 * compatible devices.
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.
59 /**
60 * @file
62 * Core library functions.
65 /**
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
71 * on failure.
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.
78 * @since 0.1.0
80 JAYLINK_API int jaylink_init(struct jaylink_context **ctx)
82 int ret;
83 struct jaylink_context *context;
84 #ifdef _WIN32
85 WSADATA wsa_data;
86 #endif
88 if (!ctx)
89 return JAYLINK_ERR_ARG;
91 context = malloc(sizeof(struct jaylink_context));
93 if (!context)
94 return JAYLINK_ERR_MALLOC;
96 if (libusb_init(&context->usb_ctx) != LIBUSB_SUCCESS) {
97 free(context);
98 return JAYLINK_ERR;
101 #ifdef _WIN32
102 ret = WSAStartup(MAKEWORD(2, 2), &wsa_data);
104 if (ret != 0) {
105 libusb_exit(context->usb_ctx);
106 free(context);
107 return JAYLINK_ERR;
110 if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) {
111 libusb_exit(context->usb_ctx);
112 free(context);
113 return JAYLINK_ERR;
115 #endif
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) {
129 #ifdef _WIN32
130 WSACleanup();
131 #endif
132 free(context);
133 return ret;
136 *ctx = context;
138 return JAYLINK_OK;
142 * Shutdown libjaylink.
144 * @param[in,out] ctx libjaylink context.
146 * @retval JAYLINK_OK Success.
147 * @retval JAYLINK_ERR_ARG Invalid arguments.
149 * @since 0.1.0
151 JAYLINK_API int jaylink_exit(struct jaylink_context *ctx)
153 struct list *item;
155 if (!ctx)
156 return JAYLINK_ERR_ARG;
158 item = ctx->discovered_devs;
160 while (item) {
161 jaylink_unref_device((struct jaylink_device *)item->data);
162 item = item->next;
165 list_free(ctx->discovered_devs);
166 list_free(ctx->devs);
168 libusb_exit(ctx->usb_ctx);
169 #ifdef _WIN32
170 WSACleanup();
171 #endif
173 free(ctx);
175 return JAYLINK_OK;
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.
186 * @since 0.1.0
188 JAYLINK_API bool jaylink_library_has_cap(enum jaylink_capability cap)
190 switch (cap) {
191 case JAYLINK_CAP_HIF_USB:
192 return true;
193 default:
194 return false;