diff --git a/testing/sensortest/Kconfig b/testing/sensortest/Kconfig new file mode 100644 index 000000000..01f924291 --- /dev/null +++ b/testing/sensortest/Kconfig @@ -0,0 +1,29 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config TESTING_SENSORTEST + tristate "Sensor driver test" + default n + ---help--- + Enable the Sensor driver test + +if TESTING_SENSORTEST + +config TESTING_SENSORTEST_PROGNAME + string "Program name" + default "sensortest" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config TESTING_SENSORTEST_PRIORITY + int "Sensor driver test task priority" + default 100 + +config TESTING_SENSORTEST_STACKSIZE + int "Sensor driver test stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/testing/sensortest/Make.defs b/testing/sensortest/Make.defs new file mode 100644 index 000000000..3de46b106 --- /dev/null +++ b/testing/sensortest/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# testing/sensortest/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_TESTING_SENSORTEST),) +CONFIGURED_APPS += $(APPDIR)/testing/sensortest +endif diff --git a/testing/sensortest/Makefile b/testing/sensortest/Makefile new file mode 100644 index 000000000..4130abe67 --- /dev/null +++ b/testing/sensortest/Makefile @@ -0,0 +1,30 @@ +############################################################################ +# testing/sensortest/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +PROGNAME = $(CONFIG_TESTING_SENSORTEST_PROGNAME) +PRIORITY = $(CONFIG_TESTING_SENSORTEST_PRIORITY) +STACKSIZE = $(CONFIG_TESTING_SENSORTEST_STACKSIZE) +MODULE = $(CONFIG_TESTING_SENSORTEST) + +MAINSRC = sensortest.c + +include $(APPDIR)/Application.mk diff --git a/testing/sensortest/sensortest.c b/testing/sensortest/sensortest.c new file mode 100644 index 000000000..ccc2f5f52 --- /dev/null +++ b/testing/sensortest/sensortest.c @@ -0,0 +1,314 @@ +/**************************************************************************** + * testing/sensortest.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define DEVNAME_FMT "/dev/sensor/%s" +#define DEVNAME_MAX 64 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef void (*data_print)(FAR const char *buffer, FAR const char *name); +struct sensor_info +{ + data_print print; + const uint8_t esize; + FAR const char *name; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void print_vec3(FAR const char *buffer, FAR const char *name); +static void print_valf3(FAR const char *buffer, FAR const char *name); +static void print_valf2(FAR const char *buffer, FAR const char *name); +static void print_valf(FAR const char *buffer, FAR const char *name); +static void print_valb(FAR const char *buffer, FAR const char *name); +static void print_gps(FAR const char *buffer, FAR const char *name); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static bool g_should_exit = false; + +static const struct sensor_info g_sensor_info[] = +{ + {print_vec3, sizeof(struct sensor_event_accel), "accel"}, + {print_vec3, sizeof(struct sensor_event_mag), "mag"}, + {print_vec3, sizeof(struct sensor_event_gyro), "gyro"}, + {print_valf2, sizeof(struct sensor_event_baro), "baro"}, + {print_valf, sizeof(struct sensor_event_light), "light"}, + {print_valf, sizeof(struct sensor_event_prox), "prox"}, + {print_valf, sizeof(struct sensor_event_humi), "humi"}, + {print_valf, sizeof(struct sensor_event_temp), "temp"}, + {print_valf3, sizeof(struct sensor_event_rgb), "rgb"}, + {print_valb, sizeof(struct sensor_event_hall), "hall"}, + {print_valf, sizeof(struct sensor_event_ir), "ir"}, + {print_gps, sizeof(struct sensor_event_gps), "gps"}, + {print_valf, sizeof(struct sensor_event_uv), "uv"}, + {print_valf, sizeof(struct sensor_event_noise), "noise"}, + {print_valf, sizeof(struct sensor_event_pm25), "pm25"}, + {print_valf, sizeof(struct sensor_event_pm1p0), "pm1p0"}, + {print_valf, sizeof(struct sensor_event_pm10), "pm10"}, + {print_valf, sizeof(struct sensor_event_co2), "co2"}, + {print_valf, sizeof(struct sensor_event_hcho), "hcho"}, + {print_valf, sizeof(struct sensor_event_tvoc), "tvoc"}, + {print_valf, sizeof(struct sensor_event_ph), "ph"}, + {print_valf, sizeof(struct sensor_event_dust), "dust"}, + {print_valf, sizeof(struct sensor_event_hrate), "hrate"}, + {print_valf, sizeof(struct sensor_event_hbeat), "hbeat"}, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void print_vec3(const char *buffer, const char *name) +{ + struct sensor_event_accel *event = (struct sensor_event_accel *)buffer; + printf("%s: timestamp:%llu x:%.2f y:%.2f z:%.2f, temperature:%.2f\n", + name, event->timestamp, event->x, event->y, + event->z, event->temperature); +} + +static void print_valb(const char *buffer, const char *name) +{ + struct sensor_event_hall *event = (struct sensor_event_hall *)buffer; + printf("%s: timestamp:%llu value:%d\n", + name, event->timestamp, event->hall); +} + +static void print_valf(const char *buffer, const char *name) +{ + struct sensor_event_prox *event = (struct sensor_event_prox *)buffer; + printf("%s: timestamp:%llu value:%.2f\n", + name, event->timestamp, event->proximity); +} + +static void print_valf2(const char *buffer, const char *name) +{ + struct sensor_event_baro *event = (struct sensor_event_baro *)buffer; + printf("%s: timestamp:%llu value1:%.2f value2:%.2f\n", + name, event->timestamp, event->pressure, event->temperature); +} + +static void print_valf3(const char *buffer, const char *name) +{ + struct sensor_event_rgb *event = (struct sensor_event_rgb *)buffer; + printf("%s: timestamp:%llu value1:%.2f value2:%.2f, value3:%.2f\n", + name, event->timestamp, event->r, event->g, event->b); +} + +static void print_gps(const char *buffer, const char *name) +{ + struct sensor_event_gps *event = (struct sensor_event_gps *)buffer; + + printf("%s: year: %d month: %d day: %d hour: %d min: %d sec: %d msec: %d", + name, event->year, event->month, event->day, event->hour, + event->min, event->sec, event->msec); + printf("%s: yaw: %.4f height: %.4f speed: %.4f latitude: %.4f" + "longitude: %.4f", name, event->yaw, event->height, event->speed, + event->latitude, event->longitude); +} + +static void usage(void) +{ + printf("sensortest [arguments...]\n"); + printf(" Commands:\n"); + printf("\t ex, accel0(/dev/sensor/accel0)\n"); + + printf("\t[-h ] sensotest commands help\n"); + printf("\t[-i ] The output data period of sensor in us\n"); + printf("\t default: 1000000\n"); + printf("\t[-b ] The maximum report latency of sensor in us\n"); + printf("\t default: 0\n"); +} + +static void exit_handler(int signo) +{ + g_should_exit = true; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * sensortest_main + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + unsigned int interval = 1000000; + unsigned int latency = 0; + char devname[PATH_MAX]; + struct pollfd fds; + FAR char *buffer; + FAR char *name; + int len = 0; + int fd; + int idx; + int ret; + + if (argc <= 1) + { + usage(); + return -EINVAL; + } + + if (signal(SIGINT, exit_handler) == SIG_ERR) + { + return -errno; + } + + g_should_exit = false; + name = argv[1]; + for (idx = 0; idx < SENSOR_TYPE_COUNT; idx++) + { + if (!strncmp(name, g_sensor_info[idx].name, + strlen(g_sensor_info[idx].name))) + { + len = g_sensor_info[idx].esize; + buffer = calloc(1, len); + break; + } + } + + if (!len) + { + printf("The sensor node name:%s is invaild\n", name); + usage(); + return -EINVAL; + } + + if (!buffer) + { + return -ENOMEM; + } + + while ((ret = getopt(argc, argv, "i:b:h")) != EOF) + { + switch (ret) + { + case 'i': + interval = strtol(optarg, NULL, 0); + break; + + case 'b': + latency = strtol(optarg, NULL, 0); + break; + + case 'h': + default: + usage(); + goto opt_err; + break; + } + } + + snprintf(devname, PATH_MAX, DEVNAME_FMT, name); + fd = open(devname, O_RDONLY | O_NONBLOCK); + if (fd < 0) + { + ret = -errno; + printf("Failed to open device:%s, ret:%s\n", + devname, strerror(errno)); + goto opt_err; + } + + ret = ioctl(fd, SNIOC_ACTIVATE, 1); + if (ret < 0) + { + ret = -errno; + printf("Failed to enable sensor:%s, ret:%s\n", + devname, strerror(errno)); + goto ctl_err; + } + + ret = ioctl(fd, SNIOC_SET_INTERVAL, &interval); + if (ret < 0) + { + ret = -errno; + snerr("Failed to set interval for sensor:%s, ret:%s\n", + devname, strerror(errno)); + goto ctl_err; + } + + ret = ioctl(fd, SNIOC_BATCH, &latency); + if (ret < 0) + { + ret = -errno; + snerr("Failed to batch for sensor:%s, ret:%s\n", + devname, strerror(errno)); + goto ctl_err; + } + + printf("SensorTest: Test %s with interval(%uus), latency(%uus)\n", + devname, interval, latency); + + fds.fd = fd; + fds.events = POLLIN; + + while (!g_should_exit) + { + if (poll(&fds, 1, -1) > 0) + { + if (read(fd, buffer, len) >= len) + { + g_sensor_info[idx].print(buffer, name); + } + } + } + + ret = ioctl(fd, SNIOC_ACTIVATE, 0); + if (ret < 0) + { + ret = -errno; + printf("Failed to disable sensor:%s, ret:%s\n", + devname, strerror(errno)); + goto ctl_err; + } + +ctl_err: + close(fd); +opt_err: + free(buffer); + optind = 0; + return ret; +}