POSIX Sim drivers: Fix formatting

This commit is contained in:
Lorenz Meier
2015-10-19 13:35:32 +02:00
parent 480bc2f3c6
commit 0b7f6902a9
10 changed files with 617 additions and 372 deletions
+130 -95
View File
@@ -99,7 +99,7 @@ class ACCELSIM_mag;
class ACCELSIM : public device::VDev
{
public:
ACCELSIM(const char* path, enum Rotation rotation);
ACCELSIM(const char *path, enum Rotation rotation);
virtual ~ACCELSIM();
virtual int init();
@@ -310,8 +310,8 @@ private:
int mag_set_samplerate(unsigned frequency);
/* this class cannot be copied */
ACCELSIM(const ACCELSIM&);
ACCELSIM operator=(const ACCELSIM&);
ACCELSIM(const ACCELSIM &);
ACCELSIM operator=(const ACCELSIM &);
};
/*
@@ -350,12 +350,12 @@ private:
void measure_trampoline(void *arg);
/* this class does not allow copying due to ptr data members */
ACCELSIM_mag(const ACCELSIM_mag&);
ACCELSIM_mag operator=(const ACCELSIM_mag&);
ACCELSIM_mag(const ACCELSIM_mag &);
ACCELSIM_mag operator=(const ACCELSIM_mag &);
};
ACCELSIM::ACCELSIM(const char* path, enum Rotation rotation) :
ACCELSIM::ACCELSIM(const char *path, enum Rotation rotation) :
VDev("ACCELSIM", path),
_mag(new ACCELSIM_mag(this)),
_accel_call{},
@@ -397,7 +397,7 @@ ACCELSIM::ACCELSIM(const char* path, enum Rotation rotation) :
_debug_enabled = false;
_device_id.devid_s.devtype = DRV_ACC_DEVTYPE_ACCELSIM;
/* Prime _mag with parents devid. */
_mag->_device_id.devid = _device_id.devid;
_mag->_device_id.devid_s.devtype = DRV_MAG_DEVTYPE_ACCELSIM;
@@ -425,13 +425,17 @@ ACCELSIM::~ACCELSIM()
stop();
/* free any existing reports */
if (_accel_reports != nullptr)
if (_accel_reports != nullptr) {
delete _accel_reports;
if (_mag_reports != nullptr)
delete _mag_reports;
}
if (_accel_class_instance != -1)
if (_mag_reports != nullptr) {
delete _mag_reports;
}
if (_accel_class_instance != -1) {
unregister_class_devname(ACCEL_BASE_DEVICE_PATH, _accel_class_instance);
}
delete _mag;
@@ -460,18 +464,21 @@ ACCELSIM::init()
/* allocate basic report buffers */
_accel_reports = new ringbuffer::RingBuffer(2, sizeof(accel_report));
if (_accel_reports == nullptr)
if (_accel_reports == nullptr) {
goto out;
}
_mag_reports = new ringbuffer::RingBuffer(2, sizeof(mag_report));
if (_mag_reports == nullptr)
if (_mag_reports == nullptr) {
goto out;
}
reset();
/* do VDev init for the mag device node */
ret = _mag->init();
if (ret != OK) {
PX4_WARN("MAG init failed");
goto out;
@@ -485,7 +492,7 @@ ACCELSIM::init()
/* measurement will have generated a report, publish */
_mag->_mag_topic = orb_advertise_multi(ORB_ID(sensor_mag), &mrp,
&_mag->_mag_orb_class_instance, ORB_PRIO_LOW);
&_mag->_mag_orb_class_instance, ORB_PRIO_LOW);
if (_mag->_mag_topic == nullptr) {
PX4_WARN("ADVERT ERR");
@@ -499,7 +506,7 @@ ACCELSIM::init()
/* measurement will have generated a report, publish */
_accel_topic = orb_advertise_multi(ORB_ID(sensor_accel), &arp,
&_accel_orb_class_instance, ORB_PRIO_DEFAULT);
&_accel_orb_class_instance, ORB_PRIO_DEFAULT);
if (_accel_topic == nullptr) {
PX4_WARN("ADVERT ERR");
@@ -522,16 +529,20 @@ ACCELSIM::transfer(uint8_t *send, uint8_t *recv, unsigned len)
if (cmd & DIR_READ) {
// Get data from the simulator
Simulator *sim = Simulator::getInstance();
if (sim == NULL)
if (sim == NULL) {
return ENODEV;
}
// FIXME - not sure what interrupt status should be
recv[1] = 0;
// skip cmd and status bytes
if (cmd & ACC_READ) {
sim->getRawAccelReport(&recv[2], len-2);
sim->getRawAccelReport(&recv[2], len - 2);
} else if (cmd & MAG_READ) {
sim->getMagReport(&recv[2], len-2);
sim->getMagReport(&recv[2], len - 2);
}
}
@@ -546,8 +557,9 @@ ACCELSIM::read(device::file_t *filp, char *buffer, size_t buflen)
int ret = 0;
/* buffer must be large enough */
if (count < 1)
if (count < 1) {
return -ENOSPC;
}
/* if automatic measurement is enabled */
if (_call_accel_interval > 0) {
@@ -569,8 +581,9 @@ ACCELSIM::read(device::file_t *filp, char *buffer, size_t buflen)
measure();
/* measurement will have generated a report, copy it out */
if (_accel_reports->get(arb))
if (_accel_reports->get(arb)) {
ret = sizeof(*arb);
}
return ret;
}
@@ -583,8 +596,9 @@ ACCELSIM::mag_read(device::file_t *filp, char *buffer, size_t buflen)
int ret = 0;
/* buffer must be large enough */
if (count < 1)
if (count < 1) {
return -ENOSPC;
}
/* if automatic measurement is enabled */
if (_call_mag_interval > 0) {
@@ -608,8 +622,9 @@ ACCELSIM::mag_read(device::file_t *filp, char *buffer, size_t buflen)
_mag->measure();
/* measurement will have generated a report, copy it out */
if (_mag_reports->get(mrb))
if (_mag_reports->get(mrb)) {
ret = sizeof(*mrb);
}
return ret;
}
@@ -620,7 +635,7 @@ ACCELSIM::ioctl(device::file_t *filp, int cmd, unsigned long arg)
switch (cmd) {
case SENSORIOCSPOLLRATE: {
switch (arg) {
switch (arg) {
/* switching to manual polling */
case SENSOR_POLLRATE_MANUAL:
@@ -642,52 +657,56 @@ ACCELSIM::ioctl(device::file_t *filp, int cmd, unsigned long arg)
case SENSOR_POLLRATE_DEFAULT:
return ioctl(filp, SENSORIOCSPOLLRATE, ACCELSIM_ACCEL_DEFAULT_RATE);
/* adjust to a legal polling interval in Hz */
/* adjust to a legal polling interval in Hz */
default: {
/* do we need to start internal polling? */
bool want_start = (_call_accel_interval == 0);
/* do we need to start internal polling? */
bool want_start = (_call_accel_interval == 0);
/* convert hz to hrt interval via microseconds */
unsigned period = 1000000 / arg;
/* convert hz to hrt interval via microseconds */
unsigned period = 1000000 / arg;
/* check against maximum sane rate */
if (period < 500)
return -EINVAL;
/* check against maximum sane rate */
if (period < 500) {
return -EINVAL;
}
/* adjust filters */
accel_set_driver_lowpass_filter((float)arg, _accel_filter_x.get_cutoff_freq());
/* adjust filters */
accel_set_driver_lowpass_filter((float)arg, _accel_filter_x.get_cutoff_freq());
/* update interval for next measurement */
/* XXX this is a bit shady, but no other way to adjust... */
_accel_call.period = _call_accel_interval = period;
/* update interval for next measurement */
/* XXX this is a bit shady, but no other way to adjust... */
_accel_call.period = _call_accel_interval = period;
/* if we need to start the poll state machine, do it */
if (want_start)
start();
/* if we need to start the poll state machine, do it */
if (want_start) {
start();
}
return OK;
return OK;
}
}
}
}
case SENSORIOCGPOLLRATE:
if (_call_accel_interval == 0)
if (_call_accel_interval == 0) {
return SENSOR_POLLRATE_MANUAL;
}
return 1000000 / _call_accel_interval;
case SENSORIOCSQUEUEDEPTH: {
/* lower bound is mandatory, upper bound is a sanity check */
if ((arg < 1) || (arg > 100))
return -EINVAL;
/* lower bound is mandatory, upper bound is a sanity check */
if ((arg < 1) || (arg > 100)) {
return -EINVAL;
}
if (!_accel_reports->resize(arg)) {
return -ENOMEM;
if (!_accel_reports->resize(arg)) {
return -ENOMEM;
}
return OK;
}
return OK;
}
case SENSORIOCGQUEUEDEPTH:
return _accel_reports->size();
@@ -702,20 +721,22 @@ ACCELSIM::ioctl(device::file_t *filp, int cmd, unsigned long arg)
return _accel_samplerate;
case ACCELIOCSLOWPASS: {
return accel_set_driver_lowpass_filter((float)_accel_samplerate, (float)arg);
}
return accel_set_driver_lowpass_filter((float)_accel_samplerate, (float)arg);
}
case ACCELIOCSSCALE: {
/* copy scale, but only if off by a few percent */
struct accel_scale *s = (struct accel_scale *) arg;
float sum = s->x_scale + s->y_scale + s->z_scale;
if (sum > 2.0f && sum < 4.0f) {
memcpy(&_accel_scale, s, sizeof(_accel_scale));
return OK;
} else {
return -EINVAL;
/* copy scale, but only if off by a few percent */
struct accel_scale *s = (struct accel_scale *) arg;
float sum = s->x_scale + s->y_scale + s->z_scale;
if (sum > 2.0f && sum < 4.0f) {
memcpy(&_accel_scale, s, sizeof(_accel_scale));
return OK;
} else {
return -EINVAL;
}
}
}
case ACCELIOCSRANGE:
/* arg needs to be in G */
@@ -723,7 +744,7 @@ ACCELSIM::ioctl(device::file_t *filp, int cmd, unsigned long arg)
case ACCELIOCGRANGE:
/* convert to m/s^2 and return rounded in G */
return (unsigned long)((_accel_range_m_s2)/ACCELSIM_ONE_G + 0.5f);
return (unsigned long)((_accel_range_m_s2) / ACCELSIM_ONE_G + 0.5f);
case ACCELIOCGSCALE:
/* copy scale out */
@@ -745,7 +766,7 @@ ACCELSIM::mag_ioctl(device::file_t *filp, int cmd, unsigned long arg)
switch (cmd) {
case SENSORIOCSPOLLRATE: {
switch (arg) {
switch (arg) {
/* switching to manual polling */
case SENSOR_POLLRATE_MANUAL:
@@ -775,8 +796,9 @@ ACCELSIM::mag_ioctl(device::file_t *filp, int cmd, unsigned long arg)
unsigned period = 1000000 / arg;
/* check against maximum sane rate (1ms) */
if (period < 10000)
if (period < 10000) {
return -EINVAL;
}
/* update interval for next measurement */
/* XXX this is a bit shady, but no other way to adjust... */
@@ -785,8 +807,9 @@ ACCELSIM::mag_ioctl(device::file_t *filp, int cmd, unsigned long arg)
//PX4_INFO("SET _call_mag_interval=%u", _call_mag_interval);
/* if we need to start the poll state machine, do it */
if (want_start)
if (want_start) {
start();
}
return OK;
}
@@ -794,23 +817,25 @@ ACCELSIM::mag_ioctl(device::file_t *filp, int cmd, unsigned long arg)
}
case SENSORIOCGPOLLRATE:
if (_call_mag_interval == 0)
if (_call_mag_interval == 0) {
return SENSOR_POLLRATE_MANUAL;
}
return 1000000 / _call_mag_interval;
case SENSORIOCSQUEUEDEPTH: {
/* lower bound is mandatory, upper bound is a sanity check */
if ((arg < 1) || (arg > 100))
return -EINVAL;
/* lower bound is mandatory, upper bound is a sanity check */
if ((arg < 1) || (arg > 100)) {
return -EINVAL;
}
if (!_mag_reports->resize(arg)) {
return -ENOMEM;
if (!_mag_reports->resize(arg)) {
return -ENOMEM;
}
return OK;
}
return OK;
}
case SENSORIOCGQUEUEDEPTH:
return _mag_reports->size();
@@ -854,6 +879,7 @@ ACCELSIM::mag_ioctl(device::file_t *filp, int cmd, unsigned long arg)
case MAGIOCSELFTEST:
return OK;
default:
/* give it to the superclass */
return VDev::ioctl(filp, cmd, arg);
@@ -888,7 +914,8 @@ void
ACCELSIM::write_checked_reg(unsigned reg, uint8_t value)
{
write_reg(reg, value);
for (uint8_t i=0; i<ACCELSIM_NUM_CHECKED_REGISTERS; i++) {
for (uint8_t i = 0; i < ACCELSIM_NUM_CHECKED_REGISTERS; i++) {
if (reg == _checked_registers[i]) {
_checked_values[i] = value;
}
@@ -1053,9 +1080,9 @@ ACCELSIM::measure()
// register reads and bad values. This allows the higher level
// code to decide if it should use this sensor based on
// whether it has had failures
accel_report.error_count = perf_event_count(_bad_registers) + perf_event_count(_bad_values);
accel_report.error_count = perf_event_count(_bad_registers) + perf_event_count(_bad_values);
accel_report.x_raw = (int16_t)(raw_accel_report.x/_accel_range_scale);
accel_report.x_raw = (int16_t)(raw_accel_report.x / _accel_range_scale);
accel_report.y_raw = (int16_t)(raw_accel_report.y / _accel_range_scale);
accel_report.z_raw = (int16_t)(raw_accel_report.z / _accel_range_scale);
@@ -1157,7 +1184,7 @@ ACCELSIM::mag_measure()
memset(&raw_mag_report, 0, sizeof(raw_mag_report));
raw_mag_report.cmd = DIR_READ | MAG_READ;
if(OK != transfer((uint8_t *)&raw_mag_report, (uint8_t *)&raw_mag_report, sizeof(raw_mag_report))) {
if (OK != transfer((uint8_t *)&raw_mag_report, (uint8_t *)&raw_mag_report, sizeof(raw_mag_report))) {
return;
}
@@ -1234,8 +1261,9 @@ ACCELSIM_mag::ACCELSIM_mag(ACCELSIM *parent) :
ACCELSIM_mag::~ACCELSIM_mag()
{
if (_mag_class_instance != -1)
if (_mag_class_instance != -1) {
unregister_class_devname(MAG_BASE_DEVICE_PATH, _mag_class_instance);
}
}
int
@@ -1244,8 +1272,10 @@ ACCELSIM_mag::init()
int ret;
ret = VDev::init();
if (ret != OK)
if (ret != OK) {
goto out;
}
_mag_class_instance = register_class_devname(MAG_BASE_DEVICE_PATH);
@@ -1269,11 +1299,12 @@ int
ACCELSIM_mag::ioctl(device::file_t *filp, int cmd, unsigned long arg)
{
switch (cmd) {
case DEVIOCGDEVICEID:
return (int)VDev::ioctl(filp, cmd, arg);
break;
default:
return _parent->mag_ioctl(filp, cmd, arg);
case DEVIOCGDEVICEID:
return (int)VDev::ioctl(filp, cmd, arg);
break;
default:
return _parent->mag_ioctl(filp, cmd, arg);
}
}
@@ -1311,8 +1342,9 @@ int
start(enum Rotation rotation)
{
int fd, fd_mag;
if (g_dev != nullptr) {
PX4_WARN( "already started");
PX4_WARN("already started");
return 0;
}
@@ -1339,7 +1371,7 @@ start(enum Rotation rotation)
if (px4_ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
PX4_ERR("ioctl SENSORIOCSPOLLRATE %s failed", ACCELSIM_DEVICE_PATH_ACCEL);
px4_close(fd);
px4_close(fd);
goto fail;
}
@@ -1350,14 +1382,13 @@ start(enum Rotation rotation)
if (px4_ioctl(fd_mag, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
PX4_ERR("ioctl SENSORIOCSPOLLRATE %s failed", ACCELSIM_DEVICE_PATH_ACCEL);
}
}
else
{
} else {
PX4_ERR("ioctl SENSORIOCSPOLLRATE %s failed", ACCELSIM_DEVICE_PATH_ACCEL);
}
px4_close(fd);
px4_close(fd_mag);
px4_close(fd);
px4_close(fd_mag);
return 0;
fail:
@@ -1405,7 +1436,7 @@ accelsim_main(int argc, char *argv[])
enum Rotation rotation = ROTATION_NONE;
int ret;
int myoptind = 1;
const char * myoptarg = NULL;
const char *myoptarg = NULL;
/* jump over start/off/etc and look at options first */
while ((ch = px4_getopt(argc, argv, "R:", &myoptind, &myoptarg)) != EOF) {
@@ -1413,6 +1444,7 @@ accelsim_main(int argc, char *argv[])
case 'R':
rotation = (enum Rotation)atoi(myoptarg);
break;
default:
accelsim::usage();
return 0;
@@ -1424,18 +1456,21 @@ accelsim_main(int argc, char *argv[])
/*
* Start/load the driver.
*/
if (!strcmp(verb, "start"))
if (!strcmp(verb, "start")) {
ret = accelsim::start(rotation);
}
/*
* Print driver information.
*/
else if (!strcmp(verb, "info"))
else if (!strcmp(verb, "info")) {
ret = accelsim::info();
}
else {
accelsim::usage();
return 1;
}
return ret;
}