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 static int32_t parse_rw_string(struct tinyiiod *iiod, char *str, bool write)
pmallick 0:3afcd581558d 23 {
pmallick 0:3afcd581558d 24 char *device, *channel, *attr, *ptr;
pmallick 0:3afcd581558d 25 bool is_channel = false, output = false;
pmallick 0:3afcd581558d 26 enum iio_attr_type type = IIO_ATTR_TYPE_DEVICE;
pmallick 0:3afcd581558d 27 long bytes;
pmallick 0:3afcd581558d 28
pmallick 0:3afcd581558d 29 ptr = strchr(str, ' ');
pmallick 0:3afcd581558d 30 if (!ptr)
pmallick 0:3afcd581558d 31 return -EINVAL;
pmallick 0:3afcd581558d 32
pmallick 0:3afcd581558d 33 *ptr = '\0';
pmallick 0:3afcd581558d 34 device = str;
pmallick 0:3afcd581558d 35 str = ptr + 1;
pmallick 0:3afcd581558d 36
pmallick 0:3afcd581558d 37 if (!strncmp(str, "INPUT ", sizeof("INPUT ") - 1)) {
pmallick 0:3afcd581558d 38 is_channel = true;
pmallick 0:3afcd581558d 39 str += sizeof("INPUT ") - 1;
pmallick 0:3afcd581558d 40 } else if (!strncmp(str, "OUTPUT ", sizeof("OUTPUT ") - 1)) {
pmallick 0:3afcd581558d 41 is_channel = true;
pmallick 0:3afcd581558d 42 output = true;
pmallick 0:3afcd581558d 43 str += sizeof("OUTPUT ") - 1;
pmallick 0:3afcd581558d 44 } else if (!strncmp(str, "DEBUG ", sizeof("DEBUG ") - 1)) {
pmallick 0:3afcd581558d 45 type = IIO_ATTR_TYPE_DEBUG;
pmallick 0:3afcd581558d 46 str += sizeof("DEBUG ") - 1;
pmallick 0:3afcd581558d 47 } else if (!strncmp(str, "BUFFER ", sizeof("BUFFER ") - 1)) {
pmallick 0:3afcd581558d 48 type = IIO_ATTR_TYPE_BUFFER;
pmallick 0:3afcd581558d 49 str += sizeof("BUFFER ") - 1;
pmallick 0:3afcd581558d 50 }
pmallick 0:3afcd581558d 51
pmallick 0:3afcd581558d 52 if (is_channel) {
pmallick 0:3afcd581558d 53 ptr = strchr(str, ' ');
pmallick 0:3afcd581558d 54 if (!ptr)
pmallick 0:3afcd581558d 55 return -EINVAL;
pmallick 0:3afcd581558d 56
pmallick 0:3afcd581558d 57 *ptr = '\0';
pmallick 0:3afcd581558d 58 channel = str;
pmallick 0:3afcd581558d 59 str = ptr + 1;
pmallick 0:3afcd581558d 60 } else {
pmallick 0:3afcd581558d 61 channel = NULL;
pmallick 0:3afcd581558d 62 }
pmallick 0:3afcd581558d 63
pmallick 0:3afcd581558d 64 ptr = strchr(str, ' ');
pmallick 0:3afcd581558d 65 if ((!!ptr) ^ write)
pmallick 0:3afcd581558d 66 return -EINVAL;
pmallick 0:3afcd581558d 67
pmallick 0:3afcd581558d 68 attr = str;
pmallick 0:3afcd581558d 69
pmallick 0:3afcd581558d 70 if (write) {
pmallick 0:3afcd581558d 71 *ptr = '\0';
pmallick 0:3afcd581558d 72 str = ptr + 1;
pmallick 0:3afcd581558d 73 } else {
pmallick 0:3afcd581558d 74 tinyiiod_do_read_attr(iiod, device, channel,
pmallick 0:3afcd581558d 75 output, attr, type);
pmallick 0:3afcd581558d 76 return 0;
pmallick 0:3afcd581558d 77 }
pmallick 0:3afcd581558d 78
pmallick 0:3afcd581558d 79 bytes = strtol(str, &ptr, 10);
pmallick 0:3afcd581558d 80 if (str == ptr || bytes < 0)
pmallick 0:3afcd581558d 81 return -EINVAL;
pmallick 0:3afcd581558d 82
pmallick 0:3afcd581558d 83 tinyiiod_do_write_attr(iiod, device, channel,
pmallick 0:3afcd581558d 84 output, attr, (size_t) bytes, type);
pmallick 0:3afcd581558d 85
pmallick 0:3afcd581558d 86 return 0;
pmallick 0:3afcd581558d 87 }
pmallick 0:3afcd581558d 88
pmallick 0:3afcd581558d 89 static int32_t parse_open_string(struct tinyiiod *iiod, char *str)
pmallick 0:3afcd581558d 90 {
pmallick 0:3afcd581558d 91 char *device, *ptr;
pmallick 0:3afcd581558d 92 long samples_count;
pmallick 0:3afcd581558d 93 uint32_t mask = 0;
pmallick 0:3afcd581558d 94
pmallick 0:3afcd581558d 95 ptr = strchr(str, ' ');
pmallick 0:3afcd581558d 96 if (!ptr)
pmallick 0:3afcd581558d 97 return -EINVAL;
pmallick 0:3afcd581558d 98
pmallick 0:3afcd581558d 99 *ptr = '\0';
pmallick 0:3afcd581558d 100 device = str;
pmallick 0:3afcd581558d 101 str = ptr + 1;
pmallick 0:3afcd581558d 102
pmallick 0:3afcd581558d 103 samples_count = strtol(str, &ptr, 10);
pmallick 0:3afcd581558d 104 if (str == ptr || *ptr != ' ' || samples_count < 0)
pmallick 0:3afcd581558d 105 return -EINVAL;
pmallick 0:3afcd581558d 106
pmallick 0:3afcd581558d 107 str = ptr + 1;
pmallick 0:3afcd581558d 108
pmallick 0:3afcd581558d 109 mask = strtoul(str, NULL, 16);
pmallick 0:3afcd581558d 110
pmallick 0:3afcd581558d 111 tinyiiod_do_open(iiod, device, (size_t) samples_count, mask);
pmallick 0:3afcd581558d 112
pmallick 0:3afcd581558d 113 return 0;
pmallick 0:3afcd581558d 114 }
pmallick 0:3afcd581558d 115
pmallick 0:3afcd581558d 116 static int32_t parse_timeout_string(struct tinyiiod *iiod, char *str)
pmallick 0:3afcd581558d 117 {
pmallick 0:3afcd581558d 118 uint32_t timeout = strtoul(str, NULL, 10);
pmallick 0:3afcd581558d 119
pmallick 0:3afcd581558d 120 return tinyiiod_set_timeout(iiod, timeout);
pmallick 0:3afcd581558d 121 }
pmallick 0:3afcd581558d 122
pmallick 0:3afcd581558d 123 static int32_t parse_writebuf_string(struct tinyiiod *iiod, char *str)
pmallick 0:3afcd581558d 124 {
pmallick 0:3afcd581558d 125 char *device, *ptr;
pmallick 0:3afcd581558d 126 long bytes_count;
pmallick 0:3afcd581558d 127
pmallick 0:3afcd581558d 128 ptr = strchr(str, ' ');
pmallick 0:3afcd581558d 129 if (!ptr)
pmallick 0:3afcd581558d 130 return -EINVAL;
pmallick 0:3afcd581558d 131
pmallick 0:3afcd581558d 132 *ptr = '\0';
pmallick 0:3afcd581558d 133 device = str;
pmallick 0:3afcd581558d 134 str = ptr + 1;
pmallick 0:3afcd581558d 135
pmallick 0:3afcd581558d 136 bytes_count = strtol(str, &ptr, 10);
pmallick 0:3afcd581558d 137 if (str == ptr || *ptr != '\0' || bytes_count < 0)
pmallick 0:3afcd581558d 138 return -EINVAL;
pmallick 0:3afcd581558d 139
pmallick 0:3afcd581558d 140 return tinyiiod_do_writebuf(iiod, device, (size_t) bytes_count);
pmallick 0:3afcd581558d 141 }
pmallick 0:3afcd581558d 142
pmallick 0:3afcd581558d 143 static int32_t parse_readbuf_string(struct tinyiiod *iiod, char *str)
pmallick 0:3afcd581558d 144 {
pmallick 0:3afcd581558d 145 char *device, *ptr;
pmallick 0:3afcd581558d 146 long bytes_count;
pmallick 0:3afcd581558d 147
pmallick 0:3afcd581558d 148 ptr = strchr(str, ' ');
pmallick 0:3afcd581558d 149 if (!ptr)
pmallick 0:3afcd581558d 150 return -EINVAL;
pmallick 0:3afcd581558d 151
pmallick 0:3afcd581558d 152 *ptr = '\0';
pmallick 0:3afcd581558d 153 device = str;
pmallick 0:3afcd581558d 154 str = ptr + 1;
pmallick 0:3afcd581558d 155
pmallick 0:3afcd581558d 156 bytes_count = strtol(str, &ptr, 10);
pmallick 0:3afcd581558d 157 if (str == ptr || *ptr != '\0' || bytes_count < 0)
pmallick 0:3afcd581558d 158 return -EINVAL;
pmallick 0:3afcd581558d 159
pmallick 0:3afcd581558d 160 return tinyiiod_do_readbuf(iiod, device, (size_t) bytes_count);
pmallick 0:3afcd581558d 161 }
pmallick 0:3afcd581558d 162
pmallick 0:3afcd581558d 163 int32_t tinyiiod_parse_string(struct tinyiiod *iiod, char *str)
pmallick 0:3afcd581558d 164 {
pmallick 0:3afcd581558d 165 while (*str == '\n' || *str == '\r')
pmallick 0:3afcd581558d 166 str++;
pmallick 0:3afcd581558d 167
pmallick 0:3afcd581558d 168 if (str[0] == '\0')
pmallick 0:3afcd581558d 169 return 0;
pmallick 0:3afcd581558d 170
pmallick 0:3afcd581558d 171 if (!strncmp(str, "VERSION", sizeof("VERSION"))) {
pmallick 0:3afcd581558d 172 char buf[32];
pmallick 0:3afcd581558d 173
pmallick 0:3afcd581558d 174 snprintf(buf, sizeof(buf), "%"PRIu16".%"PRIu16".%07x\n",
pmallick 0:3afcd581558d 175 TINYIIOD_VERSION_MAJOR,
pmallick 0:3afcd581558d 176 TINYIIOD_VERSION_MINOR,
pmallick 0:3afcd581558d 177 TINYIIOD_VERSION_GIT);
pmallick 0:3afcd581558d 178 tinyiiod_write_string(iiod, buf);
pmallick 0:3afcd581558d 179 return 0;
pmallick 0:3afcd581558d 180 }
pmallick 0:3afcd581558d 181
pmallick 0:3afcd581558d 182 if (!strncmp(str, "PRINT", sizeof("PRINT"))) {
pmallick 0:3afcd581558d 183 tinyiiod_write_xml(iiod);
pmallick 0:3afcd581558d 184 return 0;
pmallick 0:3afcd581558d 185 }
pmallick 0:3afcd581558d 186
pmallick 0:3afcd581558d 187 if (!strncmp(str, "READ ", sizeof("READ ") - 1))
pmallick 0:3afcd581558d 188 return parse_rw_string(iiod, str + sizeof("READ ") - 1, false);
pmallick 0:3afcd581558d 189
pmallick 0:3afcd581558d 190 if (!strncmp(str, "WRITE ", sizeof("WRITE ") -1))
pmallick 0:3afcd581558d 191 return parse_rw_string(iiod, str + sizeof("WRITE ") - 1, true);
pmallick 0:3afcd581558d 192
pmallick 0:3afcd581558d 193 if (!strncmp(str, "OPEN ", sizeof("OPEN ") -1))
pmallick 0:3afcd581558d 194 return parse_open_string(iiod, str + sizeof("OPEN ") - 1);
pmallick 0:3afcd581558d 195
pmallick 0:3afcd581558d 196 if (!strncmp(str, "CLOSE ", sizeof("CLOSE ") -1)) {
pmallick 0:3afcd581558d 197 tinyiiod_do_close(iiod, str + sizeof("CLOSE ") - 1);
pmallick 0:3afcd581558d 198 return 0;
pmallick 0:3afcd581558d 199 }
pmallick 0:3afcd581558d 200
pmallick 0:3afcd581558d 201 if (!strncmp(str, "READBUF ", sizeof("READBUF ") -1))
pmallick 0:3afcd581558d 202 return parse_readbuf_string(iiod, str + sizeof("READBUF ") - 1);
pmallick 0:3afcd581558d 203
pmallick 0:3afcd581558d 204 if (!strncmp(str, "TIMEOUT ", sizeof("TIMEOUT ") - 1))
pmallick 0:3afcd581558d 205 return parse_timeout_string(iiod, str + sizeof("TIMEOUT ") - 1);
pmallick 0:3afcd581558d 206
pmallick 0:3afcd581558d 207 if (!strncmp(str, "WRITEBUF ", sizeof("WRITEBUF ") -1))
pmallick 0:3afcd581558d 208 return parse_writebuf_string(iiod, str + sizeof("WRITEBUF ") - 1);
pmallick 0:3afcd581558d 209
pmallick 0:3afcd581558d 210 if (!strncmp(str, "EXIT", sizeof("EXIT") - 1))
pmallick 0:3afcd581558d 211 return tinyiiod_do_close_instance(iiod);
pmallick 0:3afcd581558d 212
pmallick 0:3afcd581558d 213 if (!strncmp(str, "GETTRIG", sizeof("GETTRIG") - 1))
pmallick 0:3afcd581558d 214 return tinyiiod_write_value(iiod, -ENODEV);
pmallick 0:3afcd581558d 215
pmallick 0:3afcd581558d 216 return -EINVAL;
pmallick 0:3afcd581558d 217 }