blob: 2b7c66a4f354de60d91e9a1e35261d0440765405 [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
43 /* TODO: backends should call iio_scan_result_add() here */
44
45 *info = scan_result.info;
46
47 return (ssize_t) scan_result.size;
48}
49
50void iio_context_info_list_free(struct iio_context_info **list)
51{
52 struct iio_context_info **it;
53
54 for (it = list; *it; it++) {
55 struct iio_context_info *info = *it;
56
57 if (info->description)
58 free(info->description);
59 if (info->uri)
60 free(info->uri);
61 free(info);
62 }
63
64 free(list);
65}
66
67struct iio_context_info ** iio_scan_result_add(
68 struct iio_scan_result *scan_result, size_t num)
69{
70 struct iio_context_info **info;
71 size_t old_size, new_size;
72 size_t i;
73
74 old_size = scan_result->size;
75 new_size = old_size + num;
76
77 info = realloc(scan_result->info, (new_size + 1) * sizeof(*info));
78 if (!info)
79 goto err_free_info_list;
80
81 for (i = old_size; i < new_size; i++) {
82 /* Make sure iio_context_info_list_free won't overflow */
83 info[i + 1] = NULL;
84
85 info[i] = zalloc(sizeof(**info));
86 if (!info[i])
87 goto err_free_info_list;
88 }
89
90 scan_result->info = info;
91 scan_result->size = new_size;
92
93 return &info[old_size];
94
95err_free_info_list:
96 scan_result->size = 0;
97 iio_context_info_list_free(scan_result->info);
98 return NULL;
99}
100
101struct iio_scan_context * iio_create_scan_context(
102 const char *backend, unsigned int flags)
103{
104 struct iio_scan_context *ctx;
105
106 /* "flags" must be zero for now */
107 if (flags != 0) {
108 errno = EINVAL;
109 return NULL;
110 }
111
112 ctx = malloc(sizeof(*ctx));
113 if (!ctx) {
114 errno = ENOMEM;
115 return NULL;
116 }
117
118 return ctx;
119}
120
121void iio_scan_context_destroy(struct iio_scan_context *ctx)
122{
123 free(ctx);
124}