b.liu | d440f9f | 2025-04-18 10:44:31 +0800 | [diff] [blame] | 1 | /*-----------------------------------------------------------------------------------------------*/ |
| 2 | /** |
| 3 | @file test_utils.c |
| 4 | @brief Test related interface definition |
| 5 | */ |
| 6 | /*-----------------------------------------------------------------------------------------------*/ |
| 7 | |
| 8 | /*------------------------------------------------------------------------------------------------- |
| 9 | Copyright (c) 2018 Quectel Wireless Solution, Co., Ltd. All Rights Reserved. |
| 10 | Quectel Wireless Solution Proprietary and Confidential. |
| 11 | -------------------------------------------------------------------------------------------------*/ |
| 12 | |
| 13 | /*------------------------------------------------------------------------------------------------- |
| 14 | EDIT HISTORY |
| 15 | This section contains comments describing changes made to the file. |
| 16 | Notice that changes are listed in reverse chronological order. |
| 17 | $Header: $ |
| 18 | when who what, where, why |
| 19 | -------- --- ---------------------------------------------------------- |
| 20 | 20190508 tyler.kuang Created . |
| 21 | -------------------------------------------------------------------------------------------------*/ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdint.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include "ql_test_utils.h" |
| 28 | |
| 29 | /*-----------------------------------------------------------------------------------------------*/ |
| 30 | /** |
| 31 | @brief Read a int value from stdin |
| 32 | @param[out] val, Return read data |
| 33 | @return |
| 34 | 0 - successful |
| 35 | 1 - read an enter |
| 36 | -1 - invalid input |
| 37 | */ |
| 38 | /*-----------------------------------------------------------------------------------------------*/ |
| 39 | int t_get_int(int *val) |
| 40 | { |
| 41 | int dat; |
| 42 | char *ptr_end = NULL; |
| 43 | char buf[256] = {0}; |
| 44 | |
| 45 | if(NULL == fgets(buf, sizeof(buf)-1, stdin)) |
| 46 | { |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | if(0 == buf[0]) |
| 51 | { |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | if(buf[0] == '\n') |
| 56 | { |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | dat = strtol(buf, &ptr_end, 10); |
| 61 | if(ptr_end!=NULL && ptr_end[0]!='\n') |
| 62 | { |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | if(val) |
| 67 | { |
| 68 | val[0] = dat; |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | /*-----------------------------------------------------------------------------------------------*/ |
| 75 | /** |
| 76 | @brief Read a uint32 value from stdin |
| 77 | @param[out] val, Return read data |
| 78 | @return |
| 79 | 0 - successful |
| 80 | 1 - read an enter |
| 81 | -1 - invalid input |
| 82 | */ |
| 83 | /*-----------------------------------------------------------------------------------------------*/ |
| 84 | int t_get_hex(uint32_t *val) |
| 85 | { |
| 86 | int dat; |
| 87 | char *ptr_end = NULL; |
| 88 | char buf[256] = {0}; |
| 89 | |
| 90 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 91 | { |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if(0 == buf[0]) |
| 96 | { |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | if(buf[0] == '\n') |
| 101 | { |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | dat = strtol(buf, &ptr_end, 16); |
| 106 | if(ptr_end!=NULL && ptr_end[0]!='\n') |
| 107 | { |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | if(val) |
| 112 | { |
| 113 | val[0] = dat; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /*-----------------------------------------------------------------------------------------------*/ |
| 120 | /** |
| 121 | @brief Read a char value from stdin |
| 122 | @param[out] val, Return read data |
| 123 | @return |
| 124 | 0 - successful |
| 125 | 1 - read an enter |
| 126 | -1 - invalid input |
| 127 | */ |
| 128 | /*-----------------------------------------------------------------------------------------------*/ |
| 129 | int t_get_char(int *val) |
| 130 | { |
| 131 | char buf[256] = {0}; |
| 132 | |
| 133 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 134 | { |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | if(0 == buf[0]) |
| 139 | { |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | if(buf[0] == '\n') |
| 144 | { |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | if(buf[1]!='\n') |
| 149 | { |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | if(val) |
| 154 | { |
| 155 | val[0] = buf[0]; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /*-----------------------------------------------------------------------------------------------*/ |
| 162 | /** |
| 163 | @brief Read a string value from stdin |
| 164 | @param[out] val, Return read data |
| 165 | @return |
| 166 | 0 - successful |
| 167 | 1 - read an enter |
| 168 | -1 - invalid input |
| 169 | */ |
| 170 | /*-----------------------------------------------------------------------------------------------*/ |
| 171 | int t_get_string(char *str_buf, int str_len) |
| 172 | { |
| 173 | char *ptr; |
| 174 | char buf[256] = {0}; |
| 175 | |
| 176 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 177 | { |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | if(0 == buf[0]) |
| 182 | { |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | if(buf[0] == '\n') |
| 187 | { |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | ptr = strchr(buf, '\n'); |
| 192 | if(ptr) |
| 193 | { |
| 194 | ptr[0] = 0; |
| 195 | } |
| 196 | |
| 197 | strncpy(str_buf, buf, str_len-1); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /*-----------------------------------------------------------------------------------------------*/ |
| 203 | /** |
| 204 | @brief Read a list of int values from stdin |
| 205 | @param[out] val, Return read datas |
| 206 | @param[out&in] val, Input buffer length, output the number of read |
| 207 | @return |
| 208 | 0 - successful |
| 209 | 1 - read an enter |
| 210 | -1 - invalid input |
| 211 | */ |
| 212 | /*-----------------------------------------------------------------------------------------------*/ |
| 213 | int t_get_int_list(int *dat_buf, int *dat_len) |
| 214 | { |
| 215 | int idx = 0; |
| 216 | int len; |
| 217 | int dat; |
| 218 | char *ptr, *ptr_save; |
| 219 | char *ptr_end; |
| 220 | char buf[256] = {0}; |
| 221 | |
| 222 | if(!dat_buf || !dat_len) |
| 223 | { |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | len = dat_len[0]; |
| 228 | |
| 229 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 230 | { |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | if(0 == buf[0]) |
| 235 | { |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | if(buf[0] == '\n') |
| 240 | { |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | for(ptr=strtok_r(buf, ",.: \t\r\n", &ptr_save); |
| 245 | ptr!=NULL; |
| 246 | ptr=strtok_r(NULL, ",.: \t\r\n", &ptr_save)) |
| 247 | { |
| 248 | dat = strtol(ptr, &ptr_end, 10); |
| 249 | if(ptr_end!=NULL && ptr_end[0]!=0) |
| 250 | { |
| 251 | return -1; |
| 252 | } |
| 253 | if(idx >= len) |
| 254 | { |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | dat_buf[idx] = dat; |
| 259 | idx++; |
| 260 | } |
| 261 | |
| 262 | dat_len[0] = idx; |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /*-----------------------------------------------------------------------------------------------*/ |
| 267 | /** |
| 268 | @brief Read a list of float values from stdin |
| 269 | @param[out] val, Return read datas |
| 270 | @param[out&in] val, Input buffer length, output the number of read |
| 271 | @return |
| 272 | 0 - successful |
| 273 | 1 - read an enter |
| 274 | -1 - invalid input |
| 275 | */ |
| 276 | /*-----------------------------------------------------------------------------------------------*/ |
| 277 | int t_get_float_list(float *dat_buf, int *dat_len) |
| 278 | { |
| 279 | int idx = 0; |
| 280 | int len; |
| 281 | float dat; |
| 282 | char *ptr, *ptr_save; |
| 283 | char *ptr_end; |
| 284 | char buf[256] = {0}; |
| 285 | |
| 286 | if(!dat_buf || !dat_len) |
| 287 | { |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | len = dat_len[0]; |
| 292 | |
| 293 | if(fgets(buf, sizeof(buf)-1, stdin) == NULL) |
| 294 | { |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | if(0 == buf[0]) |
| 299 | { |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | if(buf[0] == '\n') |
| 304 | { |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | for(ptr=strtok_r(buf, ",: \t\r\n", &ptr_save); |
| 309 | ptr!=NULL; |
| 310 | ptr=strtok_r(NULL, ",: \t\r\n", &ptr_save)) |
| 311 | { |
| 312 | dat = strtof(ptr, &ptr_end); |
| 313 | if(ptr_end!=NULL && ptr_end[0]!=0) |
| 314 | { |
| 315 | return -1; |
| 316 | } |
| 317 | if(idx >= len) |
| 318 | { |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | dat_buf[idx] = dat; |
| 323 | idx++; |
| 324 | } |
| 325 | |
| 326 | dat_len[0] = idx; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | |