Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | /* * Copyright (c) 2018 Diego Sueiro * * SPDX-License-Identifier: Apache-2.0 */ #include <ctype.h> #include <stdlib.h> #include <string.h> #include <zephyr/device.h> #include <zephyr/drivers/sensor.h> #include <zephyr/rtio/rtio.h> #include <zephyr/shell/shell.h> #include <zephyr/sys/iterable_sections.h> LOG_MODULE_REGISTER(sensor_shell); #define SENSOR_GET_HELP \ "Get sensor data. Channel names are optional. All channels are read " \ "when no channels are provided. Syntax:\n" \ "<device_name> <channel name 0> .. <channel name N>" #define SENSOR_ATTR_GET_HELP \ "Get the sensor's channel attribute. Syntax:\n" \ "<device_name> [<channel_name 0> <attribute_name 0> .. " \ "<channel_name N> <attribute_name N>]" #define SENSOR_ATTR_SET_HELP \ "Set the sensor's channel attribute.\n" \ "<device_name> <channel_name> <attribute_name> <value>" #define SENSOR_INFO_HELP "Get sensor info, such as vendor and model name, for all sensors." #define SENSOR_TRIG_HELP \ "Get or set the trigger type on a sensor. Currently only supports `data_ready`.\n" \ "<device_name> <on/off> <trigger_name>" const char *sensor_channel_name[SENSOR_CHAN_ALL] = { [SENSOR_CHAN_ACCEL_X] = "accel_x", [SENSOR_CHAN_ACCEL_Y] = "accel_y", [SENSOR_CHAN_ACCEL_Z] = "accel_z", [SENSOR_CHAN_ACCEL_XYZ] = "accel_xyz", [SENSOR_CHAN_GYRO_X] = "gyro_x", [SENSOR_CHAN_GYRO_Y] = "gyro_y", [SENSOR_CHAN_GYRO_Z] = "gyro_z", [SENSOR_CHAN_GYRO_XYZ] = "gyro_xyz", [SENSOR_CHAN_MAGN_X] = "magn_x", [SENSOR_CHAN_MAGN_Y] = "magn_y", [SENSOR_CHAN_MAGN_Z] = "magn_z", [SENSOR_CHAN_MAGN_XYZ] = "magn_xyz", [SENSOR_CHAN_DIE_TEMP] = "die_temp", [SENSOR_CHAN_AMBIENT_TEMP] = "ambient_temp", [SENSOR_CHAN_PRESS] = "press", [SENSOR_CHAN_PROX] = "prox", [SENSOR_CHAN_HUMIDITY] = "humidity", [SENSOR_CHAN_LIGHT] = "light", [SENSOR_CHAN_IR] = "ir", [SENSOR_CHAN_RED] = "red", [SENSOR_CHAN_GREEN] = "green", [SENSOR_CHAN_BLUE] = "blue", [SENSOR_CHAN_ALTITUDE] = "altitude", [SENSOR_CHAN_PM_1_0] = "pm_1_0", [SENSOR_CHAN_PM_2_5] = "pm_2_5", [SENSOR_CHAN_PM_10] = "pm_10", [SENSOR_CHAN_DISTANCE] = "distance", [SENSOR_CHAN_CO2] = "co2", [SENSOR_CHAN_VOC] = "voc", [SENSOR_CHAN_GAS_RES] = "gas_resistance", [SENSOR_CHAN_VOLTAGE] = "voltage", [SENSOR_CHAN_CURRENT] = "current", [SENSOR_CHAN_POWER] = "power", [SENSOR_CHAN_RESISTANCE] = "resistance", [SENSOR_CHAN_ROTATION] = "rotation", [SENSOR_CHAN_POS_DX] = "pos_dx", [SENSOR_CHAN_POS_DY] = "pos_dy", [SENSOR_CHAN_POS_DZ] = "pos_dz", [SENSOR_CHAN_RPM] = "rpm", [SENSOR_CHAN_GAUGE_VOLTAGE] = "gauge_voltage", [SENSOR_CHAN_GAUGE_AVG_CURRENT] = "gauge_avg_current", [SENSOR_CHAN_GAUGE_STDBY_CURRENT] = "gauge_stdby_current", [SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT] = "gauge_max_load_current", [SENSOR_CHAN_GAUGE_TEMP] = "gauge_temp", [SENSOR_CHAN_GAUGE_STATE_OF_CHARGE] = "gauge_state_of_charge", [SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY] = "gauge_full_cap", [SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY] = "gauge_remaining_cap", [SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY] = "gauge_nominal_cap", [SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY] = "gauge_full_avail_cap", [SENSOR_CHAN_GAUGE_AVG_POWER] = "gauge_avg_power", [SENSOR_CHAN_GAUGE_STATE_OF_HEALTH] = "gauge_state_of_health", [SENSOR_CHAN_GAUGE_TIME_TO_EMPTY] = "gauge_time_to_empty", [SENSOR_CHAN_GAUGE_TIME_TO_FULL] = "gauge_time_to_full", [SENSOR_CHAN_GAUGE_CYCLE_COUNT] = "gauge_cycle_count", [SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE] = "gauge_design_voltage", [SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE] = "gauge_desired_voltage", [SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT] = "gauge_desired_charging_current", }; static const char *sensor_attribute_name[SENSOR_ATTR_COMMON_COUNT] = { [SENSOR_ATTR_SAMPLING_FREQUENCY] = "sampling_frequency", [SENSOR_ATTR_LOWER_THRESH] = "lower_thresh", [SENSOR_ATTR_UPPER_THRESH] = "upper_thresh", [SENSOR_ATTR_SLOPE_TH] = "slope_th", [SENSOR_ATTR_SLOPE_DUR] = "slope_dur", [SENSOR_ATTR_HYSTERESIS] = "hysteresis", [SENSOR_ATTR_OVERSAMPLING] = "oversampling", [SENSOR_ATTR_FULL_SCALE] = "full_scale", [SENSOR_ATTR_OFFSET] = "offset", [SENSOR_ATTR_CALIB_TARGET] = "calib_target", [SENSOR_ATTR_CONFIGURATION] = "configuration", [SENSOR_ATTR_CALIBRATION] = "calibration", [SENSOR_ATTR_FEATURE_MASK] = "feature_mask", [SENSOR_ATTR_ALERT] = "alert", [SENSOR_ATTR_FF_DUR] = "ff_dur", }; /* Forward declaration */ static void data_ready_trigger_handler(const struct device *sensor, const struct sensor_trigger *trigger); #define TRIGGER_DATA_ENTRY(trig_enum, str_name, handler_func) \ [(trig_enum)] = {.name = #str_name, \ .handler = (handler_func), \ .trigger = {.chan = SENSOR_CHAN_ALL, .type = (trig_enum)}} /** * @brief This table stores a mapping of string trigger names along with the sensor_trigger struct * that gets passed to the driver to enable that trigger, plus a function pointer to a handler. If * that pointer is NULL, this indicates there is not currently support for that trigger type in the * sensor shell. */ static const struct { const char *name; sensor_trigger_handler_t handler; struct sensor_trigger trigger; } sensor_trigger_table[SENSOR_TRIG_COMMON_COUNT] = { TRIGGER_DATA_ENTRY(SENSOR_TRIG_TIMER, timer, NULL), TRIGGER_DATA_ENTRY(SENSOR_TRIG_DATA_READY, data_ready, data_ready_trigger_handler), TRIGGER_DATA_ENTRY(SENSOR_TRIG_DELTA, delta, NULL), TRIGGER_DATA_ENTRY(SENSOR_TRIG_NEAR_FAR, near_far, NULL), TRIGGER_DATA_ENTRY(SENSOR_TRIG_THRESHOLD, threshold, NULL), TRIGGER_DATA_ENTRY(SENSOR_TRIG_TAP, tap, NULL), TRIGGER_DATA_ENTRY(SENSOR_TRIG_DOUBLE_TAP, double_tap, NULL), TRIGGER_DATA_ENTRY(SENSOR_TRIG_FREEFALL, freefall, NULL), TRIGGER_DATA_ENTRY(SENSOR_TRIG_MOTION, motion, NULL), TRIGGER_DATA_ENTRY(SENSOR_TRIG_STATIONARY, stationary, NULL), }; enum dynamic_command_context { NONE, CTX_GET, CTX_ATTR_GET_SET, }; static enum dynamic_command_context current_cmd_ctx = NONE; /* Crate a single common config for one-shot reading */ static enum sensor_channel iodev_sensor_shell_channels[SENSOR_CHAN_ALL]; static struct sensor_read_config iodev_sensor_shell_read_config = { .sensor = NULL, .channels = iodev_sensor_shell_channels, .count = 0, .max = ARRAY_SIZE(iodev_sensor_shell_channels), }; RTIO_IODEV_DEFINE(iodev_sensor_shell_read, &__sensor_iodev_api, &iodev_sensor_shell_read_config); /* Create the RTIO context to service the reading */ RTIO_DEFINE_WITH_MEMPOOL(sensor_read_rtio, 8, 8, 32, 64, 4); static int parse_named_int(const char *name, const char *heystack[], size_t count) { char *endptr; int i; /* Attempt to parse channel name as a number first */ i = strtoul(name, &endptr, 0); if (*endptr == '\0') { return i; } /* Channel name is not a number, look it up */ for (i = 0; i < count; i++) { if (strcmp(name, heystack[i]) == 0) { return i; } } return -ENOTSUP; } static int parse_sensor_value(const char *val_str, struct sensor_value *out) { const bool is_negative = val_str[0] == '-'; const char *decimal_pos = strchr(val_str, '.'); long value; char *endptr; /* Parse int portion */ value = strtol(val_str, &endptr, 0); if (*endptr != '\0' && *endptr != '.') { return -EINVAL; } if (value > INT32_MAX || value < INT32_MIN) { return -EINVAL; } out->val1 = (int32_t)value; if (decimal_pos == NULL) { return 0; } /* Parse the decimal portion */ value = strtoul(decimal_pos + 1, &endptr, 0); if (*endptr != '\0') { return -EINVAL; } while (value < 100000) { value *= 10; } if (value > INT32_C(999999)) { return -EINVAL; } out->val2 = (int32_t)value; if (is_negative) { out->val2 *= -1; } return 0; } struct sensor_shell_processing_context { const struct device *dev; const struct shell *sh; }; static void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t buf_len, void *userdata) { struct sensor_shell_processing_context *ctx = userdata; const struct sensor_decoder_api *decoder; sensor_frame_iterator_t fit = {0}; sensor_channel_iterator_t cit = {0}; uint64_t timestamp; enum sensor_channel channel; q31_t q; int rc; ARG_UNUSED(buf_len); if (result < 0) { shell_error(ctx->sh, "Read failed"); return; } rc = sensor_get_decoder(ctx->dev, &decoder); if (rc != 0) { shell_error(ctx->sh, "Failed to get decoder for '%s'", ctx->dev->name); return; } rc = decoder->get_timestamp(buf, ×tamp); if (rc != 0) { shell_error(ctx->sh, "Failed to get fetch timestamp for '%s'", ctx->dev->name); return; } shell_print(ctx->sh, "Got samples at %" PRIu64 " ns", timestamp); while (decoder->decode(buf, &fit, &cit, &channel, &q, 1) > 0) { int8_t shift; rc = decoder->get_shift(buf, channel, &shift); if (rc != 0) { shell_error(ctx->sh, "Failed to get bitshift for channel %d", channel); continue; } int64_t scaled_value = (int64_t)q << shift; bool is_negative = scaled_value < 0; int numerator; int denominator; scaled_value = llabs(scaled_value); numerator = (int)FIELD_GET(GENMASK64(31 + shift, 31), scaled_value); denominator = (int)((FIELD_GET(GENMASK64(30, 0), scaled_value) * 1000000) / INT32_MAX); if (channel >= ARRAY_SIZE(sensor_channel_name)) { shell_print(ctx->sh, "channel idx=%d value=%s%d.%06d", channel, is_negative ? "-" : "", numerator, denominator); } else { shell_print(ctx->sh, "channel idx=%d %s value=%s%d.%06d", channel, sensor_channel_name[channel], is_negative ? "-" : "", numerator, denominator); } } } static int cmd_get_sensor(const struct shell *sh, size_t argc, char *argv[]) { const struct device *dev; int err; dev = device_get_binding(argv[1]); if (dev == NULL) { shell_error(sh, "Device unknown (%s)", argv[1]); return -ENODEV; } if (argc == 2) { /* read all channels */ int count = 0; for (int i = 0; i < ARRAY_SIZE(iodev_sensor_shell_channels); ++i) { if (SENSOR_CHANNEL_3_AXIS(i)) { continue; } iodev_sensor_shell_channels[count++] = i; } iodev_sensor_shell_read_config.count = count; } else { /* read specific channels */ iodev_sensor_shell_read_config.count = 0; for (int i = 2; i < argc; ++i) { int chan = parse_named_int(argv[i], sensor_channel_name, ARRAY_SIZE(sensor_channel_name)); if (chan < 0) { shell_error(sh, "Failed to read channel (%s)", argv[i]); continue; } iodev_sensor_shell_channels[iodev_sensor_shell_read_config.count++] = chan; } } if (iodev_sensor_shell_read_config.count == 0) { shell_error(sh, "No channels to read, bailing"); return -EINVAL; } iodev_sensor_shell_read_config.sensor = dev; struct sensor_shell_processing_context ctx = { .dev = dev, .sh = sh, }; err = sensor_read(&iodev_sensor_shell_read, &sensor_read_rtio, &ctx); if (err < 0) { shell_error(sh, "Failed to read sensor: %d", err); } sensor_processing_with_callback(&sensor_read_rtio, sensor_shell_processing_callback); return 0; } static int cmd_sensor_attr_set(const struct shell *shell_ptr, size_t argc, char *argv[]) { const struct device *dev; int rc; dev = device_get_binding(argv[1]); if (dev == NULL) { shell_error(shell_ptr, "Device unknown (%s)", argv[1]); return -ENODEV; } for (size_t i = 2; i < argc; i += 3) { int channel = parse_named_int(argv[i], sensor_channel_name, ARRAY_SIZE(sensor_channel_name)); int attr = parse_named_int(argv[i + 1], sensor_attribute_name, ARRAY_SIZE(sensor_attribute_name)); struct sensor_value value = {0}; if (channel < 0) { shell_error(shell_ptr, "Channel '%s' unknown", argv[i]); return -EINVAL; } if (attr < 0) { shell_error(shell_ptr, "Attribute '%s' unknown", argv[i + 1]); return -EINVAL; } if (parse_sensor_value(argv[i + 2], &value)) { shell_error(shell_ptr, "Sensor value '%s' invalid", argv[i + 2]); return -EINVAL; } rc = sensor_attr_set(dev, channel, attr, &value); if (rc) { shell_error(shell_ptr, "Failed to set channel(%s) attribute(%s): %d", sensor_channel_name[channel], sensor_attribute_name[attr], rc); continue; } shell_info(shell_ptr, "%s channel=%s, attr=%s set to value=%s", dev->name, sensor_channel_name[channel], sensor_attribute_name[attr], argv[i + 2]); } return 0; } static void cmd_sensor_attr_get_handler(const struct shell *shell_ptr, const struct device *dev, const char *channel_name, const char *attr_name, bool print_missing_attribute) { int channel = parse_named_int(channel_name, sensor_channel_name, ARRAY_SIZE(sensor_channel_name)); int attr = parse_named_int(attr_name, sensor_attribute_name, ARRAY_SIZE(sensor_attribute_name)); struct sensor_value value = {0}; int rc; if (channel < 0) { shell_error(shell_ptr, "Channel '%s' unknown", channel_name); return; } if (attr < 0) { shell_error(shell_ptr, "Attribute '%s' unknown", attr_name); return; } rc = sensor_attr_get(dev, channel, attr, &value); if (rc != 0) { if (rc == -EINVAL && !print_missing_attribute) { return; } shell_error(shell_ptr, "Failed to get channel(%s) attribute(%s): %d", sensor_channel_name[channel], sensor_attribute_name[attr], rc); return; } shell_info(shell_ptr, "%s(channel=%s, attr=%s) value=%.6f", dev->name, sensor_channel_name[channel], sensor_attribute_name[attr], sensor_value_to_double(&value)); } static int cmd_sensor_attr_get(const struct shell *shell_ptr, size_t argc, char *argv[]) { const struct device *dev; dev = device_get_binding(argv[1]); if (dev == NULL) { shell_error(shell_ptr, "Device unknown (%s)", argv[1]); return -ENODEV; } if (argc > 2) { for (size_t i = 2; i < argc; i += 2) { cmd_sensor_attr_get_handler(shell_ptr, dev, argv[i], argv[i + 1], /*print_missing_attribute=*/true); } } else { for (size_t channel_idx = 0; channel_idx < ARRAY_SIZE(sensor_channel_name); ++channel_idx) { for (size_t attr_idx = 0; attr_idx < ARRAY_SIZE(sensor_attribute_name); ++attr_idx) { cmd_sensor_attr_get_handler(shell_ptr, dev, sensor_channel_name[channel_idx], sensor_attribute_name[attr_idx], /*print_missing_attribute=*/false); } } } return 0; } static void channel_name_get(size_t idx, struct shell_static_entry *entry); SHELL_DYNAMIC_CMD_CREATE(dsub_channel_name, channel_name_get); static void attribute_name_get(size_t idx, struct shell_static_entry *entry) { int cnt = 0; entry->syntax = NULL; entry->handler = NULL; entry->help = NULL; entry->subcmd = &dsub_channel_name; for (int i = 0; i < SENSOR_ATTR_COMMON_COUNT; i++) { if (sensor_attribute_name[i] != NULL) { if (cnt == idx) { entry->syntax = sensor_attribute_name[i]; break; } cnt++; } } } SHELL_DYNAMIC_CMD_CREATE(dsub_attribute_name, attribute_name_get); static void channel_name_get(size_t idx, struct shell_static_entry *entry) { int cnt = 0; entry->syntax = NULL; entry->handler = NULL; entry->help = NULL; if (current_cmd_ctx == CTX_GET) { entry->subcmd = &dsub_channel_name; } else if (current_cmd_ctx == CTX_ATTR_GET_SET) { entry->subcmd = &dsub_attribute_name; } else { entry->subcmd = NULL; } for (int i = 0; i < SENSOR_CHAN_ALL; i++) { if (sensor_channel_name[i] != NULL) { if (cnt == idx) { entry->syntax = sensor_channel_name[i]; break; } cnt++; } } } static void device_name_get(size_t idx, struct shell_static_entry *entry); SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get); static void device_name_get(size_t idx, struct shell_static_entry *entry) { const struct device *dev = shell_device_lookup(idx, NULL); current_cmd_ctx = CTX_GET; entry->syntax = (dev != NULL) ? dev->name : NULL; entry->handler = NULL; entry->help = NULL; entry->subcmd = &dsub_channel_name; } static void device_name_get_for_attr(size_t idx, struct shell_static_entry *entry) { const struct device *dev = shell_device_lookup(idx, NULL); current_cmd_ctx = CTX_ATTR_GET_SET; entry->syntax = (dev != NULL) ? dev->name : NULL; entry->handler = NULL; entry->help = NULL; entry->subcmd = &dsub_channel_name; } SHELL_DYNAMIC_CMD_CREATE(dsub_device_name_for_attr, device_name_get_for_attr); static void trigger_name_get(size_t idx, struct shell_static_entry *entry) { int cnt = 0; entry->syntax = NULL; entry->handler = NULL; entry->help = NULL; entry->subcmd = NULL; for (int i = 0; i < SENSOR_TRIG_COMMON_COUNT; i++) { if (sensor_trigger_table[i].name != NULL) { if (cnt == idx) { entry->syntax = sensor_trigger_table[i].name; break; } cnt++; } } } SHELL_DYNAMIC_CMD_CREATE(dsub_trigger_name, trigger_name_get); static void trigger_on_off_get(size_t idx, struct shell_static_entry *entry) { entry->handler = NULL; entry->help = NULL; entry->subcmd = &dsub_trigger_name; switch (idx) { case 0: entry->syntax = "on"; break; case 1: entry->syntax = "off"; break; default: entry->syntax = NULL; break; } } SHELL_DYNAMIC_CMD_CREATE(dsub_trigger_onoff, trigger_on_off_get); static void device_name_get_for_trigger(size_t idx, struct shell_static_entry *entry) { const struct device *dev = shell_device_lookup(idx, NULL); entry->syntax = (dev != NULL) ? dev->name : NULL; entry->handler = NULL; entry->help = NULL; entry->subcmd = &dsub_trigger_onoff; } SHELL_DYNAMIC_CMD_CREATE(dsub_trigger, device_name_get_for_trigger); static int cmd_get_sensor_info(const struct shell *sh, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); #ifdef CONFIG_SENSOR_INFO const char *null_str = "(null)"; STRUCT_SECTION_FOREACH(sensor_info, sensor) { shell_print(sh, "device name: %s, vendor: %s, model: %s, " "friendly name: %s", sensor->dev->name, sensor->vendor ? sensor->vendor : null_str, sensor->model ? sensor->model : null_str, sensor->friendly_name ? sensor->friendly_name : null_str); } return 0; #else return -EINVAL; #endif } enum sample_stats_state { SAMPLE_STATS_STATE_UNINITIALIZED = 0, SAMPLE_STATS_STATE_ENABLED, SAMPLE_STATS_STATE_DISABLED, }; struct sample_stats { int64_t accumulator; uint32_t count; uint64_t sample_window_start; enum sample_stats_state state; }; static void data_ready_trigger_handler(const struct device *sensor, const struct sensor_trigger *trigger) { static struct sample_stats stats[SENSOR_CHAN_ALL]; const int64_t now = k_uptime_get(); struct sensor_value value; if (sensor_sample_fetch(sensor)) { LOG_ERR("Failed to fetch samples on data ready handler"); } for (int i = 0; i < SENSOR_CHAN_ALL; ++i) { int rc; /* Skip disabled channels */ if (stats[i].state == SAMPLE_STATS_STATE_DISABLED) { continue; } /* Skip 3 axis channels */ if (i == SENSOR_CHAN_ACCEL_XYZ || i == SENSOR_CHAN_GYRO_XYZ || i == SENSOR_CHAN_MAGN_XYZ) { continue; } rc = sensor_channel_get(sensor, i, &value); if (rc == -ENOTSUP && stats[i].state == SAMPLE_STATS_STATE_UNINITIALIZED) { /* Stop reading this channel if the driver told us it's not supported. */ stats[i].state = SAMPLE_STATS_STATE_DISABLED; } if (rc != 0) { /* Skip on any error. */ continue; } /* Do something with the data */ stats[i].accumulator += value.val1 * INT64_C(1000000) + value.val2; if (stats[i].count++ == 0) { stats[i].sample_window_start = now; } else if (now > stats[i].sample_window_start + CONFIG_SENSOR_SHELL_TRIG_PRINT_TIMEOUT_MS) { int64_t micro_value = stats[i].accumulator / stats[i].count; value.val1 = micro_value / 1000000; value.val2 = (int32_t)llabs(micro_value - (value.val1 * 1000000)); LOG_INF("chan=%d, num_samples=%u, data=%d.%06d", i, stats[i].count, value.val1, value.val2); stats[i].accumulator = 0; stats[i].count = 0; } } } static int cmd_trig_sensor(const struct shell *sh, size_t argc, char **argv) { const struct device *dev; enum sensor_trigger_type trigger; int err; if (argc < 4) { shell_error(sh, "Wrong number of args"); return -EINVAL; } /* Parse device name */ dev = device_get_binding(argv[1]); if (dev == NULL) { shell_error(sh, "Device unknown (%s)", argv[1]); return -ENODEV; } /* Map the trigger string to an enum value */ for (trigger = 0; trigger < ARRAY_SIZE(sensor_trigger_table); trigger++) { if (strcmp(argv[3], sensor_trigger_table[trigger].name) == 0) { break; } } if (trigger >= SENSOR_TRIG_COMMON_COUNT || sensor_trigger_table[trigger].handler == NULL) { shell_error(sh, "Unsupported trigger type (%s)", argv[3]); return -ENOTSUP; } /* Parse on/off */ if (strcmp(argv[2], "on") == 0) { err = sensor_trigger_set(dev, &sensor_trigger_table[trigger].trigger, sensor_trigger_table[trigger].handler); } else if (strcmp(argv[2], "off") == 0) { /* Clear the handler for the given trigger on this device */ err = sensor_trigger_set(dev, &sensor_trigger_table[trigger].trigger, NULL); } else { shell_error(sh, "Pass 'on' or 'off' to enable/disable trigger"); return -EINVAL; } if (err) { shell_error(sh, "Error while setting trigger %d on device %s (%d)", trigger, argv[1], err); } return err; } /* clang-format off */ SHELL_STATIC_SUBCMD_SET_CREATE(sub_sensor, SHELL_CMD_ARG(get, &dsub_device_name, SENSOR_GET_HELP, cmd_get_sensor, 2, 255), SHELL_CMD_ARG(attr_set, &dsub_device_name_for_attr, SENSOR_ATTR_SET_HELP, cmd_sensor_attr_set, 2, 255), SHELL_CMD_ARG(attr_get, &dsub_device_name_for_attr, SENSOR_ATTR_GET_HELP, cmd_sensor_attr_get, 2, 255), SHELL_COND_CMD(CONFIG_SENSOR_INFO, info, NULL, SENSOR_INFO_HELP, cmd_get_sensor_info), SHELL_CMD_ARG(trig, &dsub_trigger, SENSOR_TRIG_HELP, cmd_trig_sensor, 2, 255), SHELL_SUBCMD_SET_END ); /* clang-format on */ SHELL_CMD_REGISTER(sensor, &sub_sensor, "Sensor commands", NULL); |