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