this is testing

Committer:
pmallick
Date:
Thu Jan 14 18:54:16 2021 +0530
Revision:
0:3afcd581558d
this is testing

Who changed what in which revision?

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