Tiny IIO Daemon Library

Dependents:  

Committer:
mahphalke
Date:
Wed Oct 07 15:36:37 2020 +0530
Revision:
2:2119a523fbbc
Parent:
1:6cb62c8c58d6
Added dual license support (LGPL and ADI-BSD)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahphalke 1:6cb62c8c58d6 1 /***************************************************************************//**
mahphalke 1:6cb62c8c58d6 2 * @file parser.c
mahphalke 1:6cb62c8c58d6 3 * @brief Tiny IIO Daemon Library string parser module
mahphalke 1:6cb62c8c58d6 4 * @author Paul Cercueil <paul.cercueil@analog.com>
mahphalke 1:6cb62c8c58d6 5 ********************************************************************************
mahphalke 1:6cb62c8c58d6 6 * Copyright (c) 2016, 2020 Analog Devices, Inc.
mahphalke 1:6cb62c8c58d6 7 *
mahphalke 1:6cb62c8c58d6 8 * All rights reserved.
mahphalke 1:6cb62c8c58d6 9 *
mahphalke 1:6cb62c8c58d6 10 * This software is proprietary to Analog Devices, Inc. and its licensors.
mahphalke 1:6cb62c8c58d6 11 * By using this software you agree to the terms of the associated
mahphalke 1:6cb62c8c58d6 12 * Analog Devices Software License Agreement.
mahphalke 1:6cb62c8c58d6 13 *******************************************************************************/
mahphalke 1:6cb62c8c58d6 14
mahphalke 1:6cb62c8c58d6 15 #include "tinyiiod-private.h"
mahphalke 1:6cb62c8c58d6 16
mahphalke 1:6cb62c8c58d6 17 #include "compat.h"
mahphalke 1:6cb62c8c58d6 18
mahphalke 1:6cb62c8c58d6 19 static int32_t parse_rw_string(struct tinyiiod *iiod, char *str, bool write)
mahphalke 1:6cb62c8c58d6 20 {
mahphalke 1:6cb62c8c58d6 21 char *device, *channel, *attr, *ptr;
mahphalke 1:6cb62c8c58d6 22 bool is_channel = false, output = false, debug = false;
mahphalke 1:6cb62c8c58d6 23 long bytes;
mahphalke 1:6cb62c8c58d6 24
mahphalke 1:6cb62c8c58d6 25 ptr = strchr(str, ' ');
mahphalke 1:6cb62c8c58d6 26 if (!ptr)
mahphalke 1:6cb62c8c58d6 27 return -EINVAL;
mahphalke 1:6cb62c8c58d6 28
mahphalke 1:6cb62c8c58d6 29 *ptr = '\0';
mahphalke 1:6cb62c8c58d6 30 device = str;
mahphalke 1:6cb62c8c58d6 31 str = ptr + 1;
mahphalke 1:6cb62c8c58d6 32
mahphalke 1:6cb62c8c58d6 33 if (!strncmp(str, "INPUT ", sizeof("INPUT ") - 1)) {
mahphalke 1:6cb62c8c58d6 34 is_channel = true;
mahphalke 1:6cb62c8c58d6 35 str += sizeof("INPUT ") - 1;
mahphalke 1:6cb62c8c58d6 36 } else if (!strncmp(str, "OUTPUT ", sizeof("OUTPUT ") - 1)) {
mahphalke 1:6cb62c8c58d6 37 is_channel = true;
mahphalke 1:6cb62c8c58d6 38 output = true;
mahphalke 1:6cb62c8c58d6 39 str += sizeof("OUTPUT ") - 1;
mahphalke 1:6cb62c8c58d6 40 } else if (!strncmp(str, "DEBUG ", sizeof("DEBUG ") - 1)) {
mahphalke 1:6cb62c8c58d6 41 debug = true;
mahphalke 1:6cb62c8c58d6 42 str += sizeof("DEBUG ") - 1;
mahphalke 1:6cb62c8c58d6 43 }
mahphalke 1:6cb62c8c58d6 44
mahphalke 1:6cb62c8c58d6 45 if (is_channel) {
mahphalke 1:6cb62c8c58d6 46 ptr = strchr(str, ' ');
mahphalke 1:6cb62c8c58d6 47 if (!ptr)
mahphalke 1:6cb62c8c58d6 48 return -EINVAL;
mahphalke 1:6cb62c8c58d6 49
mahphalke 1:6cb62c8c58d6 50 *ptr = '\0';
mahphalke 1:6cb62c8c58d6 51 channel = str;
mahphalke 1:6cb62c8c58d6 52 str = ptr + 1;
mahphalke 1:6cb62c8c58d6 53 } else {
mahphalke 1:6cb62c8c58d6 54 channel = NULL;
mahphalke 1:6cb62c8c58d6 55 }
mahphalke 1:6cb62c8c58d6 56
mahphalke 1:6cb62c8c58d6 57 ptr = strchr(str, ' ');
mahphalke 1:6cb62c8c58d6 58 if ((!!ptr) ^ write)
mahphalke 1:6cb62c8c58d6 59 return -EINVAL;
mahphalke 1:6cb62c8c58d6 60
mahphalke 1:6cb62c8c58d6 61 attr = str;
mahphalke 1:6cb62c8c58d6 62
mahphalke 1:6cb62c8c58d6 63 if (write) {
mahphalke 1:6cb62c8c58d6 64 *ptr = '\0';
mahphalke 1:6cb62c8c58d6 65 str = ptr + 1;
mahphalke 1:6cb62c8c58d6 66 } else {
mahphalke 1:6cb62c8c58d6 67 tinyiiod_do_read_attr(iiod, device, channel,
mahphalke 1:6cb62c8c58d6 68 output, attr, debug);
mahphalke 1:6cb62c8c58d6 69 return 0;
mahphalke 1:6cb62c8c58d6 70 }
mahphalke 1:6cb62c8c58d6 71
mahphalke 1:6cb62c8c58d6 72 bytes = strtol(str, &ptr, 10);
mahphalke 1:6cb62c8c58d6 73 if (str == ptr || bytes < 0)
mahphalke 1:6cb62c8c58d6 74 return -EINVAL;
mahphalke 1:6cb62c8c58d6 75
mahphalke 1:6cb62c8c58d6 76 tinyiiod_do_write_attr(iiod, device, channel,
mahphalke 1:6cb62c8c58d6 77 output, attr, (size_t) bytes, debug);
mahphalke 1:6cb62c8c58d6 78
mahphalke 1:6cb62c8c58d6 79 return 0;
mahphalke 1:6cb62c8c58d6 80 }
mahphalke 1:6cb62c8c58d6 81
mahphalke 1:6cb62c8c58d6 82 static int32_t parse_open_string(struct tinyiiod *iiod, char *str)
mahphalke 1:6cb62c8c58d6 83 {
mahphalke 1:6cb62c8c58d6 84 char *device, *ptr;
mahphalke 1:6cb62c8c58d6 85 long samples_count;
mahphalke 1:6cb62c8c58d6 86 uint32_t mask = 0;
mahphalke 1:6cb62c8c58d6 87
mahphalke 1:6cb62c8c58d6 88 ptr = strchr(str, ' ');
mahphalke 1:6cb62c8c58d6 89 if (!ptr)
mahphalke 1:6cb62c8c58d6 90 return -EINVAL;
mahphalke 1:6cb62c8c58d6 91
mahphalke 1:6cb62c8c58d6 92 *ptr = '\0';
mahphalke 1:6cb62c8c58d6 93 device = str;
mahphalke 1:6cb62c8c58d6 94 str = ptr + 1;
mahphalke 1:6cb62c8c58d6 95
mahphalke 1:6cb62c8c58d6 96 samples_count = strtol(str, &ptr, 10);
mahphalke 1:6cb62c8c58d6 97 if (str == ptr || *ptr != ' ' || samples_count < 0)
mahphalke 1:6cb62c8c58d6 98 return -EINVAL;
mahphalke 1:6cb62c8c58d6 99
mahphalke 1:6cb62c8c58d6 100 str = ptr + 1;
mahphalke 1:6cb62c8c58d6 101
mahphalke 1:6cb62c8c58d6 102 mask = strtoul(str, NULL, 16);
mahphalke 1:6cb62c8c58d6 103
mahphalke 1:6cb62c8c58d6 104 tinyiiod_do_open(iiod, device, (size_t) samples_count, mask);
mahphalke 1:6cb62c8c58d6 105
mahphalke 1:6cb62c8c58d6 106 return 0;
mahphalke 1:6cb62c8c58d6 107 }
mahphalke 1:6cb62c8c58d6 108
mahphalke 1:6cb62c8c58d6 109 static int32_t parse_timeout_string(struct tinyiiod *iiod, char *str)
mahphalke 1:6cb62c8c58d6 110 {
mahphalke 1:6cb62c8c58d6 111 uint32_t timeout = strtoul(str, NULL, 10);
mahphalke 1:6cb62c8c58d6 112
mahphalke 1:6cb62c8c58d6 113 return tinyiiod_set_timeout(iiod, timeout);
mahphalke 1:6cb62c8c58d6 114 }
mahphalke 1:6cb62c8c58d6 115
mahphalke 1:6cb62c8c58d6 116 static int32_t parse_writebuf_string(struct tinyiiod *iiod, char *str)
mahphalke 1:6cb62c8c58d6 117 {
mahphalke 1:6cb62c8c58d6 118 char *device, *ptr;
mahphalke 1:6cb62c8c58d6 119 long bytes_count;
mahphalke 1:6cb62c8c58d6 120
mahphalke 1:6cb62c8c58d6 121 ptr = strchr(str, ' ');
mahphalke 1:6cb62c8c58d6 122 if (!ptr)
mahphalke 1:6cb62c8c58d6 123 return -EINVAL;
mahphalke 1:6cb62c8c58d6 124
mahphalke 1:6cb62c8c58d6 125 *ptr = '\0';
mahphalke 1:6cb62c8c58d6 126 device = str;
mahphalke 1:6cb62c8c58d6 127 str = ptr + 1;
mahphalke 1:6cb62c8c58d6 128
mahphalke 1:6cb62c8c58d6 129 bytes_count = strtol(str, &ptr, 10);
mahphalke 1:6cb62c8c58d6 130 if (str == ptr || *ptr != '\0' || bytes_count < 0)
mahphalke 1:6cb62c8c58d6 131 return -EINVAL;
mahphalke 1:6cb62c8c58d6 132
mahphalke 1:6cb62c8c58d6 133 return tinyiiod_do_writebuf(iiod, device, (size_t) bytes_count);
mahphalke 1:6cb62c8c58d6 134 }
mahphalke 1:6cb62c8c58d6 135
mahphalke 1:6cb62c8c58d6 136 static int32_t parse_readbuf_string(struct tinyiiod *iiod, char *str)
mahphalke 1:6cb62c8c58d6 137 {
mahphalke 1:6cb62c8c58d6 138 char *device, *ptr;
mahphalke 1:6cb62c8c58d6 139 long bytes_count;
mahphalke 1:6cb62c8c58d6 140
mahphalke 1:6cb62c8c58d6 141 ptr = strchr(str, ' ');
mahphalke 1:6cb62c8c58d6 142 if (!ptr)
mahphalke 1:6cb62c8c58d6 143 return -EINVAL;
mahphalke 1:6cb62c8c58d6 144
mahphalke 1:6cb62c8c58d6 145 *ptr = '\0';
mahphalke 1:6cb62c8c58d6 146 device = str;
mahphalke 1:6cb62c8c58d6 147 str = ptr + 1;
mahphalke 1:6cb62c8c58d6 148
mahphalke 1:6cb62c8c58d6 149 bytes_count = strtol(str, &ptr, 10);
mahphalke 1:6cb62c8c58d6 150 if (str == ptr || *ptr != '\0' || bytes_count < 0)
mahphalke 1:6cb62c8c58d6 151 return -EINVAL;
mahphalke 1:6cb62c8c58d6 152
mahphalke 1:6cb62c8c58d6 153 return tinyiiod_do_readbuf(iiod, device, (size_t) bytes_count);
mahphalke 1:6cb62c8c58d6 154 }
mahphalke 1:6cb62c8c58d6 155
mahphalke 1:6cb62c8c58d6 156 int32_t tinyiiod_parse_string(struct tinyiiod *iiod, char *str)
mahphalke 1:6cb62c8c58d6 157 {
mahphalke 1:6cb62c8c58d6 158 while (*str == '\n' || *str == '\r')
mahphalke 1:6cb62c8c58d6 159 str++;
mahphalke 1:6cb62c8c58d6 160
mahphalke 1:6cb62c8c58d6 161 if (str[0] == '\0')
mahphalke 1:6cb62c8c58d6 162 return 0;
mahphalke 1:6cb62c8c58d6 163
mahphalke 1:6cb62c8c58d6 164 if (!strncmp(str, "VERSION", sizeof("VERSION"))) {
mahphalke 1:6cb62c8c58d6 165 char buf[32];
mahphalke 1:6cb62c8c58d6 166
mahphalke 1:6cb62c8c58d6 167 snprintf(buf, sizeof(buf), "%"PRIu16".%"PRIu16".%-7.7s\n",
mahphalke 1:6cb62c8c58d6 168 TINYIIOD_VERSION_MAJOR,
mahphalke 1:6cb62c8c58d6 169 TINYIIOD_VERSION_MINOR,
mahphalke 1:6cb62c8c58d6 170 TINYIIOD_VERSION_GIT);
mahphalke 1:6cb62c8c58d6 171 tinyiiod_write_string(iiod, buf);
mahphalke 1:6cb62c8c58d6 172 return 0;
mahphalke 1:6cb62c8c58d6 173 }
mahphalke 1:6cb62c8c58d6 174
mahphalke 1:6cb62c8c58d6 175 if (!strncmp(str, "PRINT", sizeof("PRINT"))) {
mahphalke 1:6cb62c8c58d6 176 tinyiiod_write_xml(iiod);
mahphalke 1:6cb62c8c58d6 177 return 0;
mahphalke 1:6cb62c8c58d6 178 }
mahphalke 1:6cb62c8c58d6 179
mahphalke 1:6cb62c8c58d6 180 if (!strncmp(str, "READ ", sizeof("READ ") - 1))
mahphalke 1:6cb62c8c58d6 181 return parse_rw_string(iiod, str + sizeof("READ ") - 1, false);
mahphalke 1:6cb62c8c58d6 182
mahphalke 1:6cb62c8c58d6 183 if (!strncmp(str, "WRITE ", sizeof("WRITE ") -1))
mahphalke 1:6cb62c8c58d6 184 return parse_rw_string(iiod, str + sizeof("WRITE ") - 1, true);
mahphalke 1:6cb62c8c58d6 185
mahphalke 1:6cb62c8c58d6 186 if (!strncmp(str, "OPEN ", sizeof("OPEN ") -1))
mahphalke 1:6cb62c8c58d6 187 return parse_open_string(iiod, str + sizeof("OPEN ") - 1);
mahphalke 1:6cb62c8c58d6 188
mahphalke 1:6cb62c8c58d6 189 if (!strncmp(str, "CLOSE ", sizeof("CLOSE ") -1)) {
mahphalke 1:6cb62c8c58d6 190 tinyiiod_do_close(iiod, str + sizeof("CLOSE ") - 1);
mahphalke 1:6cb62c8c58d6 191 return 0;
mahphalke 1:6cb62c8c58d6 192 }
mahphalke 1:6cb62c8c58d6 193
mahphalke 1:6cb62c8c58d6 194 if (!strncmp(str, "READBUF ", sizeof("READBUF ") -1))
mahphalke 1:6cb62c8c58d6 195 return parse_readbuf_string(iiod, str + sizeof("READBUF ") - 1);
mahphalke 1:6cb62c8c58d6 196
mahphalke 1:6cb62c8c58d6 197 if (!strncmp(str, "TIMEOUT ", sizeof("TIMEOUT ") - 1))
mahphalke 1:6cb62c8c58d6 198 return parse_timeout_string(iiod, str + sizeof("TIMEOUT ") - 1);
mahphalke 1:6cb62c8c58d6 199
mahphalke 1:6cb62c8c58d6 200 if (!strncmp(str, "WRITEBUF ", sizeof("WRITEBUF ") -1))
mahphalke 1:6cb62c8c58d6 201 return parse_writebuf_string(iiod, str + sizeof("WRITEBUF ") - 1);
mahphalke 1:6cb62c8c58d6 202
mahphalke 1:6cb62c8c58d6 203 if (!strncmp(str, "EXIT", sizeof("EXIT") - 1))
mahphalke 1:6cb62c8c58d6 204 return tinyiiod_do_close_instance(iiod);
mahphalke 1:6cb62c8c58d6 205
mahphalke 1:6cb62c8c58d6 206 if (!strncmp(str, "GETTRIG", sizeof("GETTRIG") - 1))
mahphalke 1:6cb62c8c58d6 207 return tinyiiod_write_value(iiod, -ENODEV);
mahphalke 1:6cb62c8c58d6 208
mahphalke 1:6cb62c8c58d6 209 return -EINVAL;
mahphalke 1:6cb62c8c58d6 210 }