this is testing

Committer:
pmallick
Date:
Thu Jan 14 19:12:57 2021 +0530
Revision:
0:e8a1ba50c46b
this is testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmallick 0:e8a1ba50c46b 1 /*
pmallick 0:e8a1ba50c46b 2 * libtinyiiod - Tiny IIO Daemon Library
pmallick 0:e8a1ba50c46b 3 *
pmallick 0:e8a1ba50c46b 4 * Copyright (C) 2016 Analog Devices, Inc.
pmallick 0:e8a1ba50c46b 5 * Author: Paul Cercueil <paul.cercueil@analog.com>
pmallick 0:e8a1ba50c46b 6 *
pmallick 0:e8a1ba50c46b 7 * This library is free software; you can redistribute it and/or
pmallick 0:e8a1ba50c46b 8 * modify it under the terms of the GNU Lesser General Public
pmallick 0:e8a1ba50c46b 9 * License as published by the Free Software Foundation; either
pmallick 0:e8a1ba50c46b 10 * version 2.1 of the License, or (at your option) any later version.
pmallick 0:e8a1ba50c46b 11 *
pmallick 0:e8a1ba50c46b 12 * This library is distributed in the hope that it will be useful,
pmallick 0:e8a1ba50c46b 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
pmallick 0:e8a1ba50c46b 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
pmallick 0:e8a1ba50c46b 15 * Lesser General Public License for more details.
pmallick 0:e8a1ba50c46b 16 */
pmallick 0:e8a1ba50c46b 17
pmallick 0:e8a1ba50c46b 18 #include "tinyiiod-private.h"
pmallick 0:e8a1ba50c46b 19
pmallick 0:e8a1ba50c46b 20 #include "compat.h"
pmallick 0:e8a1ba50c46b 21
pmallick 0:e8a1ba50c46b 22 struct tinyiiod {
pmallick 0:e8a1ba50c46b 23 struct tinyiiod_ops *ops;
pmallick 0:e8a1ba50c46b 24 char *buf;
pmallick 0:e8a1ba50c46b 25 };
pmallick 0:e8a1ba50c46b 26
pmallick 0:e8a1ba50c46b 27 struct tinyiiod * tinyiiod_create(struct tinyiiod_ops *ops)
pmallick 0:e8a1ba50c46b 28 {
pmallick 0:e8a1ba50c46b 29 struct tinyiiod *iiod = malloc(sizeof(*iiod));
pmallick 0:e8a1ba50c46b 30
pmallick 0:e8a1ba50c46b 31 if (!iiod)
pmallick 0:e8a1ba50c46b 32 return NULL;
pmallick 0:e8a1ba50c46b 33
pmallick 0:e8a1ba50c46b 34 iiod->buf = malloc(IIOD_BUFFER_SIZE);
pmallick 0:e8a1ba50c46b 35 if (!iiod->buf) {
pmallick 0:e8a1ba50c46b 36 free(iiod);
pmallick 0:e8a1ba50c46b 37 return NULL;
pmallick 0:e8a1ba50c46b 38 }
pmallick 0:e8a1ba50c46b 39 iiod->ops = ops;
pmallick 0:e8a1ba50c46b 40
pmallick 0:e8a1ba50c46b 41 return iiod;
pmallick 0:e8a1ba50c46b 42 }
pmallick 0:e8a1ba50c46b 43
pmallick 0:e8a1ba50c46b 44 void tinyiiod_destroy(struct tinyiiod *iiod)
pmallick 0:e8a1ba50c46b 45 {
pmallick 0:e8a1ba50c46b 46 free(iiod->buf);
pmallick 0:e8a1ba50c46b 47 free(iiod);
pmallick 0:e8a1ba50c46b 48 }
pmallick 0:e8a1ba50c46b 49
pmallick 0:e8a1ba50c46b 50 int32_t tinyiiod_read_command(struct tinyiiod *iiod)
pmallick 0:e8a1ba50c46b 51 {
pmallick 0:e8a1ba50c46b 52 char buf[128];
pmallick 0:e8a1ba50c46b 53 int32_t ret;
pmallick 0:e8a1ba50c46b 54
pmallick 0:e8a1ba50c46b 55 ret = tinyiiod_read_line(iiod, buf, sizeof(buf));
pmallick 0:e8a1ba50c46b 56 if (ret < 0)
pmallick 0:e8a1ba50c46b 57 return ret;
pmallick 0:e8a1ba50c46b 58
pmallick 0:e8a1ba50c46b 59 ret = tinyiiod_parse_string(iiod, buf);
pmallick 0:e8a1ba50c46b 60 if (ret < 0)
pmallick 0:e8a1ba50c46b 61 tinyiiod_write_value(iiod, ret);
pmallick 0:e8a1ba50c46b 62
pmallick 0:e8a1ba50c46b 63 return ret;
pmallick 0:e8a1ba50c46b 64 }
pmallick 0:e8a1ba50c46b 65
pmallick 0:e8a1ba50c46b 66 ssize_t tinyiiod_read(struct tinyiiod *iiod, char *buf, size_t len)
pmallick 0:e8a1ba50c46b 67 {
pmallick 0:e8a1ba50c46b 68 return iiod->ops->read(buf, len);
pmallick 0:e8a1ba50c46b 69 }
pmallick 0:e8a1ba50c46b 70
pmallick 0:e8a1ba50c46b 71 ssize_t tinyiiod_read_line(struct tinyiiod *iiod, char *buf, size_t len)
pmallick 0:e8a1ba50c46b 72 {
pmallick 0:e8a1ba50c46b 73 char ch;
pmallick 0:e8a1ba50c46b 74 uint32_t i;
pmallick 0:e8a1ba50c46b 75 bool found = false;
pmallick 0:e8a1ba50c46b 76 int32_t ret;
pmallick 0:e8a1ba50c46b 77
pmallick 0:e8a1ba50c46b 78 if (iiod->ops->read_line)
pmallick 0:e8a1ba50c46b 79 return iiod->ops->read_line(buf, len);
pmallick 0:e8a1ba50c46b 80
pmallick 0:e8a1ba50c46b 81 for (i = 0; i < len - 1; i++) {
pmallick 0:e8a1ba50c46b 82 ret = iiod->ops->read(&ch, 1);
pmallick 0:e8a1ba50c46b 83 if (ret <= 0)
pmallick 0:e8a1ba50c46b 84 return -EIO;
pmallick 0:e8a1ba50c46b 85 buf[i] = ch;
pmallick 0:e8a1ba50c46b 86 if (buf[i] != '\n')
pmallick 0:e8a1ba50c46b 87 found = true;
pmallick 0:e8a1ba50c46b 88 else if (found)
pmallick 0:e8a1ba50c46b 89 break;
pmallick 0:e8a1ba50c46b 90 }
pmallick 0:e8a1ba50c46b 91
pmallick 0:e8a1ba50c46b 92 if (!found || i == len - 1) {
pmallick 0:e8a1ba50c46b 93 /* No \n found -> garbage data */
pmallick 0:e8a1ba50c46b 94 return -EIO;
pmallick 0:e8a1ba50c46b 95 }
pmallick 0:e8a1ba50c46b 96
pmallick 0:e8a1ba50c46b 97 buf[i - 1] = '\0';
pmallick 0:e8a1ba50c46b 98
pmallick 0:e8a1ba50c46b 99 return i;
pmallick 0:e8a1ba50c46b 100 }
pmallick 0:e8a1ba50c46b 101
pmallick 0:e8a1ba50c46b 102 ssize_t tinyiiod_write_char(struct tinyiiod *iiod, char c)
pmallick 0:e8a1ba50c46b 103 {
pmallick 0:e8a1ba50c46b 104 return iiod->ops->write(&c, 1);
pmallick 0:e8a1ba50c46b 105 }
pmallick 0:e8a1ba50c46b 106
pmallick 0:e8a1ba50c46b 107 ssize_t tinyiiod_write(struct tinyiiod *iiod, const char *data, size_t len)
pmallick 0:e8a1ba50c46b 108 {
pmallick 0:e8a1ba50c46b 109 return iiod->ops->write(data, len);
pmallick 0:e8a1ba50c46b 110 }
pmallick 0:e8a1ba50c46b 111
pmallick 0:e8a1ba50c46b 112 ssize_t tinyiiod_write_string(struct tinyiiod *iiod, const char *str)
pmallick 0:e8a1ba50c46b 113 {
pmallick 0:e8a1ba50c46b 114 return tinyiiod_write(iiod, str, strlen(str));
pmallick 0:e8a1ba50c46b 115 }
pmallick 0:e8a1ba50c46b 116
pmallick 0:e8a1ba50c46b 117 ssize_t tinyiiod_write_value(struct tinyiiod *iiod, int32_t value)
pmallick 0:e8a1ba50c46b 118 {
pmallick 0:e8a1ba50c46b 119 char buf[16];
pmallick 0:e8a1ba50c46b 120
pmallick 0:e8a1ba50c46b 121 snprintf(buf, sizeof(buf), "%"PRIi32"\n", value);
pmallick 0:e8a1ba50c46b 122 return tinyiiod_write_string(iiod, buf);
pmallick 0:e8a1ba50c46b 123 }
pmallick 0:e8a1ba50c46b 124
pmallick 0:e8a1ba50c46b 125 void tinyiiod_write_xml(struct tinyiiod *iiod)
pmallick 0:e8a1ba50c46b 126 {
pmallick 0:e8a1ba50c46b 127 char *xml;
pmallick 0:e8a1ba50c46b 128 iiod->ops->get_xml(&xml);
pmallick 0:e8a1ba50c46b 129 size_t len = strlen(xml);
pmallick 0:e8a1ba50c46b 130
pmallick 0:e8a1ba50c46b 131 tinyiiod_write_value(iiod, len);
pmallick 0:e8a1ba50c46b 132 tinyiiod_write(iiod, xml, len);
pmallick 0:e8a1ba50c46b 133 tinyiiod_write_char(iiod, '\n');
pmallick 0:e8a1ba50c46b 134 }
pmallick 0:e8a1ba50c46b 135
pmallick 0:e8a1ba50c46b 136 void tinyiiod_do_read_attr(struct tinyiiod *iiod, const char *device,
pmallick 0:e8a1ba50c46b 137 const char *channel, bool ch_out, const char *attr, enum iio_attr_type type)
pmallick 0:e8a1ba50c46b 138 {
pmallick 0:e8a1ba50c46b 139 ssize_t ret;
pmallick 0:e8a1ba50c46b 140
pmallick 0:e8a1ba50c46b 141 if (channel)
pmallick 0:e8a1ba50c46b 142 ret = iiod->ops->ch_read_attr(device, channel,
pmallick 0:e8a1ba50c46b 143 ch_out, attr, iiod->buf, IIOD_BUFFER_SIZE);
pmallick 0:e8a1ba50c46b 144 else
pmallick 0:e8a1ba50c46b 145 ret = iiod->ops->read_attr(device, attr,
pmallick 0:e8a1ba50c46b 146 iiod->buf, IIOD_BUFFER_SIZE, type);
pmallick 0:e8a1ba50c46b 147
pmallick 0:e8a1ba50c46b 148 tinyiiod_write_value(iiod, (int32_t) ret);
pmallick 0:e8a1ba50c46b 149 if (ret > 0) {
pmallick 0:e8a1ba50c46b 150 iiod->buf[ret] = '\n';
pmallick 0:e8a1ba50c46b 151 tinyiiod_write(iiod, iiod->buf, (size_t) ret + 1);
pmallick 0:e8a1ba50c46b 152 }
pmallick 0:e8a1ba50c46b 153 }
pmallick 0:e8a1ba50c46b 154
pmallick 0:e8a1ba50c46b 155 void tinyiiod_do_write_attr(struct tinyiiod *iiod, const char *device,
pmallick 0:e8a1ba50c46b 156 const char *channel, bool ch_out, const char *attr,
pmallick 0:e8a1ba50c46b 157 size_t bytes, enum iio_attr_type type)
pmallick 0:e8a1ba50c46b 158 {
pmallick 0:e8a1ba50c46b 159 ssize_t ret;
pmallick 0:e8a1ba50c46b 160
pmallick 0:e8a1ba50c46b 161 if (bytes > IIOD_BUFFER_SIZE - 1)
pmallick 0:e8a1ba50c46b 162 bytes = IIOD_BUFFER_SIZE - 1;
pmallick 0:e8a1ba50c46b 163
pmallick 0:e8a1ba50c46b 164 tinyiiod_read(iiod, iiod->buf, bytes);
pmallick 0:e8a1ba50c46b 165 iiod->buf[bytes] = '\0';
pmallick 0:e8a1ba50c46b 166
pmallick 0:e8a1ba50c46b 167 if (channel)
pmallick 0:e8a1ba50c46b 168 ret = iiod->ops->ch_write_attr(device, channel, ch_out,
pmallick 0:e8a1ba50c46b 169 attr, iiod->buf, bytes);
pmallick 0:e8a1ba50c46b 170 else
pmallick 0:e8a1ba50c46b 171 ret = iiod->ops->write_attr(device, attr, iiod->buf, bytes, type);
pmallick 0:e8a1ba50c46b 172
pmallick 0:e8a1ba50c46b 173 tinyiiod_write_value(iiod, (int32_t) ret);
pmallick 0:e8a1ba50c46b 174 }
pmallick 0:e8a1ba50c46b 175
pmallick 0:e8a1ba50c46b 176 void tinyiiod_do_open(struct tinyiiod *iiod, const char *device,
pmallick 0:e8a1ba50c46b 177 size_t sample_size, uint32_t mask)
pmallick 0:e8a1ba50c46b 178 {
pmallick 0:e8a1ba50c46b 179 int32_t ret = iiod->ops->open(device, sample_size, mask);
pmallick 0:e8a1ba50c46b 180 tinyiiod_write_value(iiod, ret);
pmallick 0:e8a1ba50c46b 181 }
pmallick 0:e8a1ba50c46b 182
pmallick 0:e8a1ba50c46b 183 void tinyiiod_do_close(struct tinyiiod *iiod, const char *device)
pmallick 0:e8a1ba50c46b 184 {
pmallick 0:e8a1ba50c46b 185 int32_t ret = iiod->ops->close(device);
pmallick 0:e8a1ba50c46b 186 tinyiiod_write_value(iiod, ret);
pmallick 0:e8a1ba50c46b 187 }
pmallick 0:e8a1ba50c46b 188
pmallick 0:e8a1ba50c46b 189 int32_t tinyiiod_do_open_instance(struct tinyiiod *iiod)
pmallick 0:e8a1ba50c46b 190 {
pmallick 0:e8a1ba50c46b 191 if (iiod->ops->open_instance)
pmallick 0:e8a1ba50c46b 192 return iiod->ops->open_instance();
pmallick 0:e8a1ba50c46b 193
pmallick 0:e8a1ba50c46b 194 return 0;
pmallick 0:e8a1ba50c46b 195 }
pmallick 0:e8a1ba50c46b 196
pmallick 0:e8a1ba50c46b 197 int32_t tinyiiod_do_close_instance(struct tinyiiod *iiod)
pmallick 0:e8a1ba50c46b 198 {
pmallick 0:e8a1ba50c46b 199 if (iiod->ops->close_instance)
pmallick 0:e8a1ba50c46b 200 return iiod->ops->close_instance();
pmallick 0:e8a1ba50c46b 201
pmallick 0:e8a1ba50c46b 202 return 0;
pmallick 0:e8a1ba50c46b 203 }
pmallick 0:e8a1ba50c46b 204
pmallick 0:e8a1ba50c46b 205 int32_t tinyiiod_do_writebuf(struct tinyiiod *iiod,
pmallick 0:e8a1ba50c46b 206 const char *device, size_t bytes_count)
pmallick 0:e8a1ba50c46b 207 {
pmallick 0:e8a1ba50c46b 208 size_t bytes, offset = 0, total_bytes = bytes_count;
pmallick 0:e8a1ba50c46b 209 char buf[256];
pmallick 0:e8a1ba50c46b 210 int32_t ret = 0;
pmallick 0:e8a1ba50c46b 211
pmallick 0:e8a1ba50c46b 212 tinyiiod_write_value(iiod, bytes_count);
pmallick 0:e8a1ba50c46b 213 while (bytes_count) {
pmallick 0:e8a1ba50c46b 214 bytes = bytes_count > sizeof(buf) ? sizeof(buf) : bytes_count;
pmallick 0:e8a1ba50c46b 215 ret = tinyiiod_read(iiod, buf, bytes);
pmallick 0:e8a1ba50c46b 216 if (ret > 0) {
pmallick 0:e8a1ba50c46b 217 ret = iiod->ops->write_data(device, buf, offset, ret);
pmallick 0:e8a1ba50c46b 218 offset += ret;
pmallick 0:e8a1ba50c46b 219 if (ret < 0)
pmallick 0:e8a1ba50c46b 220 return ret;
pmallick 0:e8a1ba50c46b 221 bytes_count -= ret;
pmallick 0:e8a1ba50c46b 222 } else
pmallick 0:e8a1ba50c46b 223 return ret;
pmallick 0:e8a1ba50c46b 224 }
pmallick 0:e8a1ba50c46b 225 if (iiod->ops->transfer_mem_to_dev) {
pmallick 0:e8a1ba50c46b 226 ret = iiod->ops->transfer_mem_to_dev(device, total_bytes);
pmallick 0:e8a1ba50c46b 227 if (ret < 0)
pmallick 0:e8a1ba50c46b 228 return ret;
pmallick 0:e8a1ba50c46b 229 }
pmallick 0:e8a1ba50c46b 230 tinyiiod_write_value(iiod, (int) total_bytes);
pmallick 0:e8a1ba50c46b 231
pmallick 0:e8a1ba50c46b 232 return ret;
pmallick 0:e8a1ba50c46b 233 }
pmallick 0:e8a1ba50c46b 234
pmallick 0:e8a1ba50c46b 235 int32_t tinyiiod_do_readbuf(struct tinyiiod *iiod,
pmallick 0:e8a1ba50c46b 236 const char *device, size_t bytes_count)
pmallick 0:e8a1ba50c46b 237 {
pmallick 0:e8a1ba50c46b 238 int32_t ret;
pmallick 0:e8a1ba50c46b 239 char buf[256];
pmallick 0:e8a1ba50c46b 240 uint32_t mask;
pmallick 0:e8a1ba50c46b 241 bool print_mask = true;
pmallick 0:e8a1ba50c46b 242 size_t offset = 0;
pmallick 0:e8a1ba50c46b 243
pmallick 0:e8a1ba50c46b 244 ret = iiod->ops->get_mask(device, &mask);
pmallick 0:e8a1ba50c46b 245 if (ret < 0) {
pmallick 0:e8a1ba50c46b 246 return ret;
pmallick 0:e8a1ba50c46b 247 }
pmallick 0:e8a1ba50c46b 248 if (iiod->ops->transfer_dev_to_mem) {
pmallick 0:e8a1ba50c46b 249 ret = iiod->ops->transfer_dev_to_mem(device, bytes_count);
pmallick 0:e8a1ba50c46b 250 if (ret < 0)
pmallick 0:e8a1ba50c46b 251 return ret;
pmallick 0:e8a1ba50c46b 252 }
pmallick 0:e8a1ba50c46b 253 while (bytes_count) {
pmallick 0:e8a1ba50c46b 254 size_t bytes = bytes_count > sizeof(buf) ? sizeof(buf) : bytes_count;
pmallick 0:e8a1ba50c46b 255
pmallick 0:e8a1ba50c46b 256 ret = (int) iiod->ops->read_data(device, buf, offset, bytes);
pmallick 0:e8a1ba50c46b 257 offset += bytes;
pmallick 0:e8a1ba50c46b 258 tinyiiod_write_value(iiod, ret);
pmallick 0:e8a1ba50c46b 259 if (ret < 0)
pmallick 0:e8a1ba50c46b 260 return ret;
pmallick 0:e8a1ba50c46b 261
pmallick 0:e8a1ba50c46b 262 if (print_mask) {
pmallick 0:e8a1ba50c46b 263 char buf_mask[10];
pmallick 0:e8a1ba50c46b 264
pmallick 0:e8a1ba50c46b 265 snprintf(buf_mask, sizeof(buf_mask), "%08"PRIx32"\n", mask);
pmallick 0:e8a1ba50c46b 266 tinyiiod_write_string(iiod, buf_mask);
pmallick 0:e8a1ba50c46b 267 print_mask = false;
pmallick 0:e8a1ba50c46b 268 }
pmallick 0:e8a1ba50c46b 269
pmallick 0:e8a1ba50c46b 270 tinyiiod_write(iiod, buf, (size_t) ret);
pmallick 0:e8a1ba50c46b 271 bytes_count -= (size_t) ret;
pmallick 0:e8a1ba50c46b 272 }
pmallick 0:e8a1ba50c46b 273
pmallick 0:e8a1ba50c46b 274 return ret;
pmallick 0:e8a1ba50c46b 275 }
pmallick 0:e8a1ba50c46b 276
pmallick 0:e8a1ba50c46b 277 int32_t tinyiiod_set_timeout(struct tinyiiod *iiod, uint32_t timeout)
pmallick 0:e8a1ba50c46b 278 {
pmallick 0:e8a1ba50c46b 279 int32_t ret = 0;
pmallick 0:e8a1ba50c46b 280
pmallick 0:e8a1ba50c46b 281 if (iiod->ops->set_timeout)
pmallick 0:e8a1ba50c46b 282 ret = iiod->ops->set_timeout(timeout);
pmallick 0:e8a1ba50c46b 283 tinyiiod_write_value(iiod, ret);
pmallick 0:e8a1ba50c46b 284
pmallick 0:e8a1ba50c46b 285 return ret;
pmallick 0:e8a1ba50c46b 286 }