|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <ctype.h> |
| 8 | +#include <string.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <stdbool.h> |
| 11 | +#include <errno.h> |
| 12 | +#include "shell_util.h" |
| 13 | + |
| 14 | +static size_t whitespace_trim(char *out, size_t len, const char *str) |
| 15 | +{ |
| 16 | + if (len == 0) { |
| 17 | + return 0; |
| 18 | + } |
| 19 | + |
| 20 | + const char *end; |
| 21 | + size_t out_size; |
| 22 | + |
| 23 | + while (str[0] == ' ') { |
| 24 | + str++; |
| 25 | + } |
| 26 | + |
| 27 | + if (*str == 0) { |
| 28 | + *out = 0; |
| 29 | + return 1; |
| 30 | + } |
| 31 | + |
| 32 | + end = str + strlen(str) - 1; |
| 33 | + while (end > str && (end[0] == ' ')) { |
| 34 | + end--; |
| 35 | + } |
| 36 | + end++; |
| 37 | + |
| 38 | + out_size = (end - str) + 1; |
| 39 | + |
| 40 | + if (out_size > len) { |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + memcpy(out, str, out_size - 1); |
| 45 | + out[out_size - 1] = 0; |
| 46 | + |
| 47 | + return out_size; |
| 48 | +} |
| 49 | + |
| 50 | +int bt_mesh_str2l(const char *str, long *val) |
| 51 | +{ |
| 52 | + char *endptr = NULL; |
| 53 | + char c; |
| 54 | + |
| 55 | + errno = 0; |
| 56 | + *val = strtol(str, &endptr, 10); |
| 57 | + if (errno == ERANGE) { |
| 58 | + return -ERANGE; |
| 59 | + } else if (errno || endptr == str) { |
| 60 | + return -EINVAL; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + while ((c = *endptr++)) { |
| 65 | + if (!isspace(c)) { |
| 66 | + return -EINVAL; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +int bt_mesh_str2ul(const char *str, unsigned long *val) |
| 74 | +{ |
| 75 | + char *endptr = NULL; |
| 76 | + char c; |
| 77 | + |
| 78 | + while (isspace(*str)) { |
| 79 | + str++; |
| 80 | + } |
| 81 | + if (*str == '-') { |
| 82 | + return -EINVAL; |
| 83 | + } |
| 84 | + |
| 85 | + errno = 0; |
| 86 | + *val = strtoul(str, &endptr, 10); |
| 87 | + if (errno == ERANGE) { |
| 88 | + return -ERANGE; |
| 89 | + } else if (errno || endptr == str) { |
| 90 | + return -EINVAL; |
| 91 | + } |
| 92 | + |
| 93 | + while ((c = *endptr++)) { |
| 94 | + if (!isspace(c)) { |
| 95 | + return -EINVAL; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +int bt_mesh_str2dbl(const char *str, double *val) |
| 103 | +{ |
| 104 | + char trimmed_buf[22] = { 0 }; |
| 105 | + long intgr; |
| 106 | + unsigned long frac; |
| 107 | + double frac_dbl; |
| 108 | + int err = 0; |
| 109 | + |
| 110 | + size_t len = whitespace_trim(trimmed_buf, sizeof(trimmed_buf), str); |
| 111 | + |
| 112 | + if (len < 2) { |
| 113 | + return -EINVAL; |
| 114 | + } |
| 115 | + |
| 116 | + int comma_idx = strcspn(trimmed_buf, "."); |
| 117 | + int frac_len = strlen(trimmed_buf + comma_idx + 1); |
| 118 | + |
| 119 | + /* Covers corner case "." input */ |
| 120 | + if (strlen(trimmed_buf) < 2 && trimmed_buf[comma_idx] != 0) { |
| 121 | + return -EINVAL; |
| 122 | + } |
| 123 | + |
| 124 | + trimmed_buf[comma_idx] = 0; |
| 125 | + |
| 126 | + /* Avoid fractional overflow by losing one precision point */ |
| 127 | + if (frac_len > 9) { |
| 128 | + trimmed_buf[comma_idx + 10] = 0; |
| 129 | + frac_len = 9; |
| 130 | + } |
| 131 | + |
| 132 | + /* Avoid doing str2long if intgr part is empty or only single sign char*/ |
| 133 | + if (trimmed_buf[0] == '\0' || |
| 134 | + (trimmed_buf[1] == '\0' && (trimmed_buf[0] == '+' || trimmed_buf[0] == '-'))) { |
| 135 | + intgr = 0; |
| 136 | + } else { |
| 137 | + err = bt_mesh_str2l(trimmed_buf, &intgr); |
| 138 | + |
| 139 | + if (err) { |
| 140 | + return err; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /* Avoid doing str2ulong if fractional part is empty */ |
| 145 | + if ((trimmed_buf + comma_idx + 1)[0] == '\0') { |
| 146 | + frac = 0; |
| 147 | + } else { |
| 148 | + err = bt_mesh_str2ul(trimmed_buf + comma_idx + 1, &frac); |
| 149 | + |
| 150 | + if (err) { |
| 151 | + return err; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + frac_dbl = (double)frac; |
| 156 | + |
| 157 | + for (int i = 0; i < frac_len; i++) { |
| 158 | + frac_dbl /= 10; |
| 159 | + } |
| 160 | + |
| 161 | + *val = (trimmed_buf[0] == '-') ? ((double)intgr - frac_dbl) : |
| 162 | + ((double)intgr + frac_dbl); |
| 163 | + |
| 164 | + return err; |
| 165 | +} |
| 166 | + |
| 167 | +int bt_mesh_str2bool(const char *str, bool *val) |
| 168 | +{ |
| 169 | + long temp; |
| 170 | + |
| 171 | + if (!strcmp(str, "on") || !strcmp(str, "enable") || !strcmp(str, "true")) { |
| 172 | + *val = true; |
| 173 | + return 0; |
| 174 | + } |
| 175 | + |
| 176 | + if (!strcmp(str, "off") || !strcmp(str, "disable") || !strcmp(str, "false")) { |
| 177 | + *val = false; |
| 178 | + return 0; |
| 179 | + } |
| 180 | + |
| 181 | + int err = bt_mesh_str2l(str, &temp); |
| 182 | + |
| 183 | + if (err) { |
| 184 | + return err; |
| 185 | + } |
| 186 | + |
| 187 | + *val = (bool)temp; |
| 188 | + return 0; |
| 189 | +} |
0 commit comments