blob: 55f5638a9be7b5c4efe7d08b7292e574ae6734c8 [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>
21
22struct iio_scan_context {
23 int foo; /* avoid complaints about empty structure */
24};
25
26const char * iio_context_info_get_description(
27 const struct iio_context_info *info)
28{
29 return info->description;
30}
31
32const char * iio_context_info_get_uri(
33 const struct iio_context_info *info)
34{
35 return info->uri;
36}
37
38ssize_t iio_scan_context_get_info_list(struct iio_scan_context *ctx,
39 struct iio_context_info ***info)
40{
41 struct iio_scan_result scan_result = { 0, NULL };
42
Lars-Peter Clausen3db58d62016-04-25 15:00:54 +020043#if LOCAL_BACKEND
44 {
45 int ret = local_context_scan(&scan_result);
46 if (ret < 0) {
47 if (scan_result.info)
48 iio_context_info_list_free(scan_result.info);
49 return ret;
50 }
51 }
52#endif
Lars-Peter Clausen08633732016-02-22 14:35:17 +010053
54 *info = scan_result.info;
55
56 return (ssize_t) scan_result.size;
57}
58
59void iio_context_info_list_free(struct iio_context_info **list)
60{
61 struct iio_context_info **it;
62
63 for (it = list; *it; it++) {
64 struct iio_context_info *info = *it;
65
66 if (info->description)
67 free(info->description);
68 if (info->uri)
69 free(info->uri);
70 free(info);
71 }
72
73 free(list);
74}
75
76struct iio_context_info ** iio_scan_result_add(
77 struct iio_scan_result *scan_result, size_t num)
78{
79 struct iio_context_info **info;
80 size_t old_size, new_size;
81 size_t i;
82
83 old_size = scan_result->size;
84 new_size = old_size + num;
85
86 info = realloc(scan_result->info, (new_size + 1) * sizeof(*info));
87 if (!info)
88 goto err_free_info_list;
89
90 for (i = old_size; i < new_size; i++) {
91 /* Make sure iio_context_info_list_free won't overflow */
92 info[i + 1] = NULL;
93
94 info[i] = zalloc(sizeof(**info));
95 if (!info[i])
96 goto err_free_info_list;
97 }
98
99 scan_result->info = info;
100 scan_result->size = new_size;
101
102 return &info[old_size];
103
104err_free_info_list:
105 scan_result->size = 0;
106 iio_context_info_list_free(scan_result->info);
107 return NULL;
108}
109
110struct iio_scan_context * iio_create_scan_context(
111 const char *backend, unsigned int flags)
112{
113 struct iio_scan_context *ctx;
114
115 /* "flags" must be zero for now */
116 if (flags != 0) {
117 errno = EINVAL;
118 return NULL;
119 }
120
121 ctx = malloc(sizeof(*ctx));
122 if (!ctx) {
123 errno = ENOMEM;
124 return NULL;
125 }
126
127 return ctx;
128}
129
130void iio_scan_context_destroy(struct iio_scan_context *ctx)
131{
132 free(ctx);
133}