ms5525: add argc check and use px4_getopt

This commit is contained in:
Beat Küng 2018-06-04 14:24:48 +02:00 committed by Lorenz Meier
parent d3f9f419f1
commit 4b8658a318
1 changed files with 24 additions and 10 deletions

View File

@ -33,6 +33,8 @@
#include "MS5525.hpp"
#include <px4_getopt.h>
// Driver 'main' command.
extern "C" __EXPORT int ms5525_airspeed_main(int argc, char *argv[]);
@ -151,36 +153,48 @@ ms5525_airspeed_main(int argc, char *argv[])
{
uint8_t i2c_bus = PX4_I2C_BUS_DEFAULT;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--bus") == 0) {
if (argc > i + 1) {
i2c_bus = atoi(argv[i + 1]);
}
int myoptind = 1;
int ch;
const char *myoptarg = nullptr;
while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'b':
i2c_bus = atoi(myoptarg);
break;
default:
ms5525_airspeed_usage();
return 0;
}
}
if (myoptind >= argc) {
ms5525_airspeed_usage();
return -1;
}
/*
* Start/load the driver.
*/
if (!strcmp(argv[1], "start")) {
if (!strcmp(argv[myoptind], "start")) {
return ms5525_airspeed::start(i2c_bus);
}
/*
* Stop the driver
*/
if (!strcmp(argv[1], "stop")) {
if (!strcmp(argv[myoptind], "stop")) {
return ms5525_airspeed::stop();
}
/*
* Reset the driver.
*/
if (!strcmp(argv[1], "reset")) {
if (!strcmp(argv[myoptind], "reset")) {
return ms5525_airspeed::reset();
}
ms5525_airspeed_usage();
return PX4_OK;
return 0;
}