testing/sensortest: fix bug because getopt and add debug log

Change-Id: Id9c3499dd56690c60b2f6579d128850ce7522a6e
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu 2020-12-09 12:36:46 +08:00 committed by Abdelatif Guettouche
parent 89376af38d
commit 89bceba6b0

View File

@ -152,15 +152,17 @@ static void print_gps(const char *buffer, const char *name)
static void usage(void)
{
printf("sensortest <command> [arguments...]\n");
printf(" Commands:\n");
printf("\t<sensor_node_name> ex, accel0(/dev/sensor/accel0)\n");
printf("sensortest [arguments...] <command>\n");
printf("\t[-h ] sensotest commands help\n");
printf("\t[-i <val>] The output data period of sensor in us\n");
printf("\t default: 1000000\n");
printf("\t[-b <val>] The maximum report latency of sensor in us\n");
printf("\t default: 0\n");
printf("\t[-n <val>] The specify number of output data\n");
printf("\t default: 0\n");
printf(" Commands:\n");
printf("\t<sensor_node_name> ex, accel0(/dev/sensor/accel0)\n");
}
static void exit_handler(int signo)
@ -179,7 +181,9 @@ static void exit_handler(int signo)
int main(int argc, FAR char *argv[])
{
unsigned int interval = 1000000;
unsigned int received = 0;
unsigned int latency = 0;
unsigned int count = 0;
char devname[PATH_MAX];
struct pollfd fds;
FAR char *buffer;
@ -201,50 +205,62 @@ int main(int argc, FAR char *argv[])
}
g_should_exit = false;
name = argv[1];
for (idx = 0; idx < ARRAYSIZE(g_sensor_info); 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)
while ((ret = getopt(argc, argv, "i:b:n:h")) != EOF)
{
switch (ret)
{
case 'i':
interval = strtol(optarg, NULL, 0);
interval = strtoul(optarg, NULL, 0);
break;
case 'b':
latency = strtol(optarg, NULL, 0);
latency = strtoul(optarg, NULL, 0);
break;
case 'n':
count = strtoul(optarg, NULL, 0);
break;
case 'h':
default:
usage();
goto opt_err;
break;
optind = 0;
return 0;
}
}
if (optind < argc)
{
name = argv[optind];
for (idx = 0; idx < ARRAYSIZE(g_sensor_info); 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;
}
}
else
{
usage();
return -EINVAL;
}
snprintf(devname, PATH_MAX, DEVNAME_FMT, name);
fd = open(devname, O_RDONLY | O_NONBLOCK);
if (fd < 0)
@ -252,7 +268,7 @@ int main(int argc, FAR char *argv[])
ret = -errno;
printf("Failed to open device:%s, ret:%s\n",
devname, strerror(errno));
goto opt_err;
goto open_err;
}
ret = ioctl(fd, SNIOC_ACTIVATE, 1);
@ -297,17 +313,21 @@ int main(int argc, FAR char *argv[])
fds.fd = fd;
fds.events = POLLIN;
while (!g_should_exit)
while ((!count || received < count) && !g_should_exit)
{
if (poll(&fds, 1, -1) > 0)
{
if (read(fd, buffer, len) >= len)
{
received++;
g_sensor_info[idx].print(buffer, name);
}
}
}
printf("SensorTest: Received message: %s, number:%d/%d\n",
name, received, count);
ret = ioctl(fd, SNIOC_ACTIVATE, 0);
if (ret < 0)
{
@ -319,8 +339,7 @@ int main(int argc, FAR char *argv[])
ctl_err:
close(fd);
opt_err:
open_err:
free(buffer);
optind = 0;
return ret;
}