Lars-Peter Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 1 | /* |
| 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 Cercueil | 668fd03 | 2016-05-20 16:03:50 +0200 | [diff] [blame] | 21 | #include <stdbool.h> |
| 22 | #include <string.h> |
Lars-Peter Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 23 | |
| 24 | struct iio_scan_context { |
Lars-Peter Clausen | 794ba03 | 2016-04-25 15:07:00 +0200 | [diff] [blame] | 25 | #if USB_BACKEND |
| 26 | struct iio_scan_backend_context *usb_ctx; |
Lars-Peter Clausen | 794ba03 | 2016-04-25 15:07:00 +0200 | [diff] [blame] | 27 | #endif |
Paul Cercueil | 668fd03 | 2016-05-20 16:03:50 +0200 | [diff] [blame] | 28 | bool scan_local; |
Lars-Peter Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | const char * iio_context_info_get_description( |
| 32 | const struct iio_context_info *info) |
| 33 | { |
| 34 | return info->description; |
| 35 | } |
| 36 | |
| 37 | const char * iio_context_info_get_uri( |
| 38 | const struct iio_context_info *info) |
| 39 | { |
| 40 | return info->uri; |
| 41 | } |
| 42 | |
| 43 | ssize_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 Clausen | 3db58d6 | 2016-04-25 15:00:54 +0200 | [diff] [blame] | 48 | #if LOCAL_BACKEND |
Paul Cercueil | 668fd03 | 2016-05-20 16:03:50 +0200 | [diff] [blame] | 49 | if (ctx->scan_local) { |
Lars-Peter Clausen | 3db58d6 | 2016-04-25 15:00:54 +0200 | [diff] [blame] | 50 | 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 Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 58 | |
Lars-Peter Clausen | 794ba03 | 2016-04-25 15:07:00 +0200 | [diff] [blame] | 59 | #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 Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 70 | *info = scan_result.info; |
| 71 | |
| 72 | return (ssize_t) scan_result.size; |
| 73 | } |
| 74 | |
| 75 | void 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 Cercueil | 7d9c5e0 | 2016-05-20 16:08:46 +0200 | [diff] [blame] | 89 | if (list) |
| 90 | free(list); |
Lars-Peter Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | struct 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 | |
| 121 | err_free_info_list: |
| 122 | scan_result->size = 0; |
| 123 | iio_context_info_list_free(scan_result->info); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | struct 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 Cercueil | 668fd03 | 2016-05-20 16:03:50 +0200 | [diff] [blame] | 138 | ctx = calloc(1, sizeof(*ctx)); |
Lars-Peter Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 139 | if (!ctx) { |
| 140 | errno = ENOMEM; |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Paul Cercueil | 668fd03 | 2016-05-20 16:03:50 +0200 | [diff] [blame] | 144 | if (!backend || !strcmp(backend, "local")) |
| 145 | ctx->scan_local = true; |
| 146 | |
Lars-Peter Clausen | 794ba03 | 2016-04-25 15:07:00 +0200 | [diff] [blame] | 147 | #if USB_BACKEND |
Paul Cercueil | 668fd03 | 2016-05-20 16:03:50 +0200 | [diff] [blame] | 148 | if (!backend || !strcmp(backend, "usb")) |
| 149 | ctx->usb_ctx = usb_context_scan_init(); |
Lars-Peter Clausen | 794ba03 | 2016-04-25 15:07:00 +0200 | [diff] [blame] | 150 | #endif |
| 151 | |
Lars-Peter Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 152 | return ctx; |
| 153 | } |
| 154 | |
| 155 | void iio_scan_context_destroy(struct iio_scan_context *ctx) |
| 156 | { |
Lars-Peter Clausen | 794ba03 | 2016-04-25 15:07:00 +0200 | [diff] [blame] | 157 | #if USB_BACKEND |
| 158 | if (ctx->usb_ctx) |
| 159 | usb_context_scan_free(ctx->usb_ctx); |
| 160 | #endif |
Lars-Peter Clausen | 0863373 | 2016-02-22 14:35:17 +0100 | [diff] [blame] | 161 | free(ctx); |
| 162 | } |