2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
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 3 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/>.
24 #include <glib/gstdio.h>
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "resource"
35 * Access to resource files.
38 static FILE *try_open_file(const char *datadir, const char *subdir,
44 filename = g_build_filename(datadir, subdir, name, NULL);
45 file = g_fopen(filename, "rb");
48 sr_info("Opened '%s'.", filename);
50 sr_spew("Attempt to open '%s' failed: %s",
51 filename, g_strerror(errno));
57 static int resource_open_default(struct sr_resource *res,
58 const char *name, void *cb_data)
61 const char *builtindir, *subdir;
62 const char *const *datadirs;
68 case SR_RESOURCE_FIRMWARE:
69 builtindir = FIRMWARE_DIR;
70 subdir = "sigrok-firmware";
73 sr_err("%s: unknown type %d.", __func__, res->type);
77 file = try_open_file(g_get_user_data_dir(), subdir, name);
79 * Scan the hard-coded directory before the system directories to
80 * avoid picking up possibly outdated files from a system install.
83 file = try_open_file(builtindir, "", name);
86 datadirs = g_get_system_data_dirs();
87 while (*datadirs && !file)
88 file = try_open_file(*datadirs++, subdir, name);
91 sr_err("Failed to locate '%s'.", name);
95 filesize = sr_file_get_size(file);
97 sr_err("Failed to obtain size of '%s': %s",
98 name, g_strerror(errno));
102 res->size = filesize;
108 static int resource_close_default(struct sr_resource *res, void *cb_data)
116 sr_err("%s: invalid handle.", __func__);
120 if (fclose(file) < 0) {
121 sr_err("Failed to close file: %s", g_strerror(errno));
129 static ssize_t resource_read_default(const struct sr_resource *res,
130 void *buf, size_t count, void *cb_data)
139 sr_err("%s: invalid handle.", __func__);
142 if (count > SSIZE_MAX) {
143 sr_err("%s: count %zu too large.", __func__, count);
147 n_read = fread(buf, 1, count, file);
149 if (n_read != count && ferror(file)) {
150 sr_err("Failed to read resource file: %s", g_strerror(errno));
157 * Install resource access hooks.
159 * @param ctx libsigrok context. Must not be NULL.
160 * @param open_cb Resource open callback, or NULL to unset.
161 * @param close_cb Resource close callback, or NULL to unset.
162 * @param read_cb Resource read callback, or NULL to unset.
163 * @param cb_data User data pointer passed to callbacks.
165 * @retval SR_OK Success.
166 * @retval SR_ERR_ARG Invalid argument.
170 SR_API int sr_resource_set_hooks(struct sr_context *ctx,
171 sr_resource_open_callback open_cb,
172 sr_resource_close_callback close_cb,
173 sr_resource_read_callback read_cb, void *cb_data)
176 sr_err("%s: ctx was NULL.", __func__);
179 if (open_cb && close_cb && read_cb) {
180 ctx->resource_open_cb = open_cb;
181 ctx->resource_close_cb = close_cb;
182 ctx->resource_read_cb = read_cb;
183 ctx->resource_cb_data = cb_data;
184 } else if (!open_cb && !close_cb && !read_cb) {
185 ctx->resource_open_cb = &resource_open_default;
186 ctx->resource_close_cb = &resource_close_default;
187 ctx->resource_read_cb = &resource_read_default;
188 ctx->resource_cb_data = ctx;
190 sr_err("%s: inconsistent callback pointers.", __func__);
199 * @param ctx libsigrok context. Must not be NULL.
200 * @param[out] res Resource descriptor to fill in. Must not be NULL.
201 * @param type Resource type ID.
202 * @param name Name of the resource. Must not be NULL.
204 * @retval SR_OK Success.
205 * @retval SR_ERR_ARG Invalid argument.
206 * @retval SR_ERR Other error.
210 SR_PRIV int sr_resource_open(struct sr_context *ctx,
211 struct sr_resource *res, int type, const char *name)
219 ret = (*ctx->resource_open_cb)(res, name, ctx->resource_cb_data);
222 sr_err("Failed to open resource '%s'.", name);
230 * @param ctx libsigrok context. Must not be NULL.
231 * @param[inout] res Resource descriptor. Must not be NULL.
233 * @retval SR_OK Success.
234 * @retval SR_ERR_ARG Invalid argument.
235 * @retval SR_ERR Other error.
239 SR_PRIV int sr_resource_close(struct sr_context *ctx, struct sr_resource *res)
243 ret = (*ctx->resource_close_cb)(res, ctx->resource_cb_data);
246 sr_err("Failed to close resource.");
252 * Read resource data.
254 * @param ctx libsigrok context. Must not be NULL.
255 * @param[in] res Resource descriptor. Must not be NULL.
256 * @param[out] buf Buffer to store @a count bytes into. Must not be NULL.
257 * @param count Number of bytes to read.
259 * @return The number of bytes actually read, or a negative value on error.
260 * @retval SR_ERR_ARG Invalid argument.
261 * @retval SR_ERR Other error.
265 SR_PRIV ssize_t sr_resource_read(struct sr_context *ctx,
266 const struct sr_resource *res, void *buf, size_t count)
270 n_read = (*ctx->resource_read_cb)(res, buf, count,
271 ctx->resource_cb_data);
273 sr_err("Failed to read resource.");
279 * Load a resource into memory.
281 * @param ctx libsigrok context. Must not be NULL.
282 * @param type Resource type ID.
283 * @param name Name of the resource. Must not be NULL.
284 * @param[out] size Size in bytes of the returned buffer. Must not be NULL.
285 * @param max_size Size limit. Error out if the resource is larger than this.
287 * @return A buffer containing the resource data, or NULL on failure. Must
288 * be freed by the caller using g_free().
292 SR_PRIV void *sr_resource_load(struct sr_context *ctx,
293 int type, const char *name, size_t *size, size_t max_size)
295 struct sr_resource res;
299 if (sr_resource_open(ctx, &res, type, name) != SR_OK)
302 if (res.size > max_size) {
303 sr_err("Size %" PRIu64 " of '%s' exceeds limit %zu.",
304 res.size, name, max_size);
305 sr_resource_close(ctx, &res);
308 buf = g_try_malloc(res.size);
310 sr_err("Failed to allocate buffer for '%s'.", name);
311 sr_resource_close(ctx, &res);
315 n_read = sr_resource_read(ctx, &res, buf, res.size);
316 sr_resource_close(ctx, &res);
318 if (n_read < 0 || (size_t)n_read != res.size) {
320 sr_err("Failed to read '%s': premature end of file.",