blob: 1365d187498aad27bbba735e4db6d0dbb5d26b98 [file] [log] [blame]
Lars-Peter Clausen08633732016-02-22 14:35:17 +01001/*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
4 * Copyright (C) 2016 Analog Devices, Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 */
17
18#include "iio-private.h"
19
20#include <errno.h>
Paul Cercueil668fd032016-05-20 16:03:50 +020021#include <stdbool.h>
22#include <string.h>
Lars-Peter Clausen08633732016-02-22 14:35:17 +010023
24struct iio_scan_context {
Lars-Peter Clausen794ba032016-04-25 15:07:00 +020025#if USB_BACKEND
26 struct iio_scan_backend_context *usb_ctx;
Lars-Peter Clausen794ba032016-04-25 15:07:00 +020027#endif
Paul Cercueil668fd032016-05-20 16:03:50 +020028 bool scan_local;
Lars-Peter Clausen08633732016-02-22 14:35:17 +010029};
30
31const char * iio_context_info_get_description(
32 const struct iio_context_info *info)
33{
34 return info->description;
35}
36
37const char * iio_context_info_get_uri(
38 const struct iio_context_info *info)
39{
40 return info->uri;
41}
42
43ssize_t iio_scan_context_get_info_list(struct iio_scan_context *ctx,
44 struct iio_context_info ***info)
45{
46 struct iio_scan_result scan_result = { 0, NULL };
47
Lars-Peter Clausen3db58d62016-04-25 15:00:54 +020048#if LOCAL_BACKEND
Paul Cercueil668fd032016-05-20 16:03:50 +020049 if (ctx->scan_local) {
Lars-Peter Clausen3db58d62016-04-25 15:00:54 +020050 int ret = local_context_scan(&scan_result);
51 if (ret < 0) {
52 if (scan_result.info)
53 iio_context_info_list_free(scan_result.info);
54 return ret;
55 }
56 }
57#endif
Lars-Peter Clausen08633732016-02-22 14:35:17 +010058
Lars-Peter Clausen794ba032016-04-25 15:07:00 +020059#if USB_BACKEND
60 if (ctx->usb_ctx) {
61 int ret = usb_context_scan(ctx->usb_ctx, &scan_result);
62 if (ret < 0) {
63 if (scan_result.info)
64 iio_context_info_list_free(scan_result.info);
65 return ret;
66 }
67 }
68#endif
69
Lars-Peter Clausen08633732016-02-22 14:35:17 +010070 *info = scan_result.info;
71
72 return (ssize_t) scan_result.size;
73}
74
75void iio_context_info_list_free(struct iio_context_info **list)
76{
77 struct iio_context_info **it;
78
79 for (it = list; *it; it++) {
80 struct iio_context_info *info = *it;
81
82 if (info->description)
83 free(info->description);
84 if (info->uri)
85 free(info->uri);
86 free(info);
87 }
88
Paul Cercueil7d9c5e02016-05-20 16:08:46 +020089 if (list)
90 free(list);
Lars-Peter Clausen08633732016-02-22 14:35:17 +010091}
92
93struct iio_context_info ** iio_scan_result_add(
94 struct iio_scan_result *scan_result, size_t num)
95{
96 struct iio_context_info **info;
97 size_t old_size, new_size;
98 size_t i;
99
100 old_size = scan_result->size;
101 new_size = old_size + num;
102
103 info = realloc(scan_result->info, (new_size + 1) * sizeof(*info));
104 if (!info)
105 goto err_free_info_list;
106
107 for (i = old_size; i < new_size; i++) {
108 /* Make sure iio_context_info_list_free won't overflow */
109 info[i + 1] = NULL;
110
111 info[i] = zalloc(sizeof(**info));
112 if (!info[i])
113 goto err_free_info_list;
114 }
115
116 scan_result->info = info;
117 scan_result->size = new_size;
118
119 return &info[old_size];
120
121err_free_info_list:
122 scan_result->size = 0;
123 iio_context_info_list_free(scan_result->info);
124 return NULL;
125}
126
127struct iio_scan_context * iio_create_scan_context(
128 const char *backend, unsigned int flags)
129{
130 struct iio_scan_context *ctx;
131
132 /* "flags" must be zero for now */
133 if (flags != 0) {
134 errno = EINVAL;
135 return NULL;
136 }
137
Paul Cercueil668fd032016-05-20 16:03:50 +0200138 ctx = calloc(1, sizeof(*ctx));
Lars-Peter Clausen08633732016-02-22 14:35:17 +0100139 if (!ctx) {
140 errno = ENOMEM;
141 return NULL;
142 }
143
Paul Cercueil668fd032016-05-20 16:03:50 +0200144 if (!backend || !strcmp(backend, "local"))
145 ctx->scan_local = true;
146
Lars-Peter Clausen794ba032016-04-25 15:07:00 +0200147#if USB_BACKEND
Paul Cercueil668fd032016-05-20 16:03:50 +0200148 if (!backend || !strcmp(backend, "usb"))
149 ctx->usb_ctx = usb_context_scan_init();
Lars-Peter Clausen794ba032016-04-25 15:07:00 +0200150#endif
151
Lars-Peter Clausen08633732016-02-22 14:35:17 +0100152 return ctx;
153}
154
155void iio_scan_context_destroy(struct iio_scan_context *ctx)
156{
Lars-Peter Clausen794ba032016-04-25 15:07:00 +0200157#if USB_BACKEND
158 if (ctx->usb_ctx)
159 usb_context_scan_free(ctx->usb_ctx);
160#endif
Lars-Peter Clausen08633732016-02-22 14:35:17 +0100161 free(ctx);
162}