mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-20 11:23:06 +08:00
Merge branch 'master' of github.com:PX4/Firmware
This commit is contained in:
+2
-2
@@ -83,7 +83,7 @@ else
|
||||
#
|
||||
# Are we attached to a PX4IOAR (AR.Drone carrier board)?
|
||||
#
|
||||
if boardinfo -t 7
|
||||
if boardinfo test name PX4IOAR
|
||||
then
|
||||
set BOARD PX4IOAR
|
||||
if [ -f /etc/init.d/rc.PX4IOAR ]
|
||||
@@ -99,7 +99,7 @@ else
|
||||
#
|
||||
# Are we attached to a PX4IO?
|
||||
#
|
||||
if boardinfo -t 6
|
||||
if boardinfo test name PX4IO
|
||||
then
|
||||
set BOARD PX4IO
|
||||
if [ -f /etc/init.d/rc.PX4IO ]
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Character device base class.
|
||||
* @file cdev.cpp
|
||||
*
|
||||
* Character device base class.
|
||||
*/
|
||||
|
||||
#include "device.h"
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Fundamental driver base class for the device framework.
|
||||
* @file device.cpp
|
||||
*
|
||||
* Fundamental driver base class for the device framework.
|
||||
*/
|
||||
|
||||
#include "device.h"
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Definitions for the generic base classes in the device framework.
|
||||
* @file device.h
|
||||
*
|
||||
* Definitions for the generic base classes in the device framework.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_DEVICE_H
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Base class for devices attached via the I2C bus.
|
||||
* @file i2c.cpp
|
||||
*
|
||||
* Base class for devices attached via the I2C bus.
|
||||
*
|
||||
* @todo Bus frequency changes; currently we do nothing with the value
|
||||
* that is supplied. Should we just depend on the bus knowing?
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Base class for devices connected via I2C.
|
||||
* @file i2c.h
|
||||
*
|
||||
* Base class for devices connected via I2C.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_I2C_H
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Base class for devices accessed via PIO to registers.
|
||||
* @file pio.cpp
|
||||
*
|
||||
* Base class for devices accessed via PIO to registers.
|
||||
*/
|
||||
|
||||
#include "device.h"
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Base class for devices connected via SPI.
|
||||
* @file spi.cpp
|
||||
*
|
||||
* Base class for devices connected via SPI.
|
||||
*
|
||||
* @todo Work out if caching the mode/frequency would save any time.
|
||||
*
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Base class for devices connected via SPI.
|
||||
* @file spi.h
|
||||
*
|
||||
* Base class for devices connected via SPI.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_SPI_H
|
||||
|
||||
@@ -32,7 +32,9 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file Generic GPIO ioctl interface.
|
||||
* @file drv_gpio.h
|
||||
*
|
||||
* Generic GPIO ioctl interface.
|
||||
*/
|
||||
|
||||
#ifndef _DRV_GPIO_H
|
||||
@@ -78,7 +80,7 @@
|
||||
* Note that there may be board-specific relationships between GPIOs;
|
||||
* applications using GPIOs should be aware of this.
|
||||
*/
|
||||
#define _GPIOCBASE 0x6700
|
||||
#define _GPIOCBASE 0x2700
|
||||
#define GPIOC(_x) _IOC(_GPIOCBASE, _x)
|
||||
|
||||
/** reset all board GPIOs to their default state */
|
||||
|
||||
@@ -1045,6 +1045,7 @@ int HMC5883::check_calibration()
|
||||
_calibrated = false;
|
||||
// XXX Notify system via uORB
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HMC5883::set_excitement(unsigned enable)
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
|
||||
* Author: @author Lorenz Meier <lm@inf.ethz.ch>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file tests_bson.c
|
||||
*
|
||||
* Tests for the bson en/decoder
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <systemlib/err.h>
|
||||
#include <systemlib/bson/tinybson.h>
|
||||
|
||||
#include "tests.h"
|
||||
|
||||
static const bool sample_bool = true;
|
||||
static const int32_t sample_small_int = 123;
|
||||
static const int64_t sample_big_int = (int64_t)INT_MAX + 123LL;
|
||||
static const double sample_double = 2.5f;
|
||||
static const char *sample_string = "this is a test";
|
||||
static const uint8_t sample_data[256];
|
||||
//static const char *sample_filename = "/fs/microsd/bson.test";
|
||||
|
||||
static int
|
||||
encode(bson_encoder_t encoder)
|
||||
{
|
||||
if (bson_encoder_append_bool(encoder, "bool1", sample_bool) != 0)
|
||||
warnx("FAIL: encoder: append bool failed");
|
||||
if (bson_encoder_append_int(encoder, "int1", sample_small_int) != 0)
|
||||
warnx("FAIL: encoder: append int failed");
|
||||
if (bson_encoder_append_int(encoder, "int2", sample_big_int) != 0)
|
||||
warnx("FAIL: encoder: append int failed");
|
||||
if (bson_encoder_append_double(encoder, "double1", sample_double) != 0)
|
||||
warnx("FAIL: encoder: append double failed");
|
||||
if (bson_encoder_append_string(encoder, "string1", sample_string) != 0)
|
||||
warnx("FAIL: encoder: append string failed");
|
||||
if (bson_encoder_append_binary(encoder, "data1", BSON_BIN_BINARY, sizeof(sample_data), sample_data) != 0)
|
||||
warnx("FAIL: encoder: append data failed");
|
||||
|
||||
bson_encoder_fini(encoder);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
decode_callback(bson_decoder_t decoder, void *private, bson_node_t node)
|
||||
{
|
||||
unsigned len;
|
||||
|
||||
if (!strcmp(node->name, "bool1")) {
|
||||
if (node->type != BSON_BOOL) {
|
||||
warnx("FAIL: decoder: bool1 type %d, expected %d", node->type, BSON_BOOL);
|
||||
return 1;
|
||||
}
|
||||
if (node->b != sample_bool) {
|
||||
warnx("FAIL: decoder: bool1 value %s, expected %s",
|
||||
(node->b ? "true" : "false"),
|
||||
(sample_bool ? "true" : "false"));
|
||||
return 1;
|
||||
}
|
||||
warnx("PASS: decoder: bool1");
|
||||
return 1;
|
||||
}
|
||||
if (!strcmp(node->name, "int1")) {
|
||||
if (node->type != BSON_INT32) {
|
||||
warnx("FAIL: decoder: int1 type %d, expected %d", node->type, BSON_INT32);
|
||||
return 1;
|
||||
}
|
||||
if (node->i != sample_small_int) {
|
||||
warnx("FAIL: decoder: int1 value %lld, expected %d", node->i, sample_small_int);
|
||||
return 1;
|
||||
}
|
||||
warnx("PASS: decoder: int1");
|
||||
return 1;
|
||||
}
|
||||
if (!strcmp(node->name, "int2")) {
|
||||
if (node->type != BSON_INT64) {
|
||||
warnx("FAIL: decoder: int2 type %d, expected %d", node->type, BSON_INT64);
|
||||
return 1;
|
||||
}
|
||||
if (node->i != sample_big_int) {
|
||||
warnx("FAIL: decoder: int2 value %lld, expected %lld", node->i, sample_big_int);
|
||||
return 1;
|
||||
}
|
||||
warnx("PASS: decoder: int2");
|
||||
return 1;
|
||||
}
|
||||
if (!strcmp(node->name, "double1")) {
|
||||
if (node->type != BSON_DOUBLE) {
|
||||
warnx("FAIL: decoder: double1 type %d, expected %d", node->type, BSON_DOUBLE);
|
||||
return 1;
|
||||
}
|
||||
if (node->d != sample_double) {
|
||||
warnx("FAIL: decoder: double1 value %f, expected %f", node->d, sample_double);
|
||||
return 1;
|
||||
}
|
||||
warnx("PASS: decoder: double1");
|
||||
return 1;
|
||||
}
|
||||
if (!strcmp(node->name, "string1")) {
|
||||
if (node->type != BSON_STRING) {
|
||||
warnx("FAIL: decoder: string1 type %d, expected %d", node->type, BSON_STRING);
|
||||
return 1;
|
||||
}
|
||||
|
||||
len = bson_decoder_data_pending(decoder);
|
||||
|
||||
if (len != strlen(sample_string) + 1) {
|
||||
warnx("FAIL: decoder: string1 length %d wrong, expected %d", len, strlen(sample_string) + 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char sbuf[len];
|
||||
|
||||
if (bson_decoder_copy_data(decoder, sbuf)) {
|
||||
warnx("FAIL: decoder: string1 copy failed");
|
||||
return 1;
|
||||
}
|
||||
if (bson_decoder_data_pending(decoder) != 0) {
|
||||
warnx("FAIL: decoder: string1 copy did not exhaust all data");
|
||||
return 1;
|
||||
}
|
||||
if (sbuf[len - 1] != '\0') {
|
||||
warnx("FAIL: decoder: string1 not 0-terminated");
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(sbuf, sample_string)) {
|
||||
warnx("FAIL: decoder: string1 value '%s', expected '%s'", sbuf, sample_string);
|
||||
return 1;
|
||||
}
|
||||
warnx("PASS: decoder: string1");
|
||||
return 1;
|
||||
}
|
||||
if (!strcmp(node->name, "data1")) {
|
||||
if (node->type != BSON_BINDATA) {
|
||||
warnx("FAIL: decoder: data1 type %d, expected %d", node->type, BSON_BINDATA);
|
||||
return 1;
|
||||
}
|
||||
|
||||
len = bson_decoder_data_pending(decoder);
|
||||
|
||||
if (len != sizeof(sample_data)) {
|
||||
warnx("FAIL: decoder: data1 length %d, expected %d", len, sizeof(sample_data));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (node->subtype != BSON_BIN_BINARY) {
|
||||
warnx("FAIL: decoder: data1 subtype %d, expected %d", node->subtype, BSON_BIN_BINARY);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t dbuf[len];
|
||||
|
||||
if (bson_decoder_copy_data(decoder, dbuf)) {
|
||||
warnx("FAIL: decoder: data1 copy failed");
|
||||
return 1;
|
||||
}
|
||||
if (bson_decoder_data_pending(decoder) != 0) {
|
||||
warnx("FAIL: decoder: data1 copy did not exhaust all data");
|
||||
return 1;
|
||||
}
|
||||
if (memcmp(sample_data, dbuf, len)) {
|
||||
warnx("FAIL: decoder: data1 compare fail");
|
||||
return 1;
|
||||
}
|
||||
warnx("PASS: decoder: data1");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (node->type != BSON_EOO)
|
||||
warnx("FAIL: decoder: unexpected node name '%s'", node->name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
decode(bson_decoder_t decoder)
|
||||
{
|
||||
int result;
|
||||
|
||||
do {
|
||||
result = bson_decoder_next(decoder);
|
||||
} while (result > 0);
|
||||
}
|
||||
|
||||
int
|
||||
test_bson(int argc, char *argv[])
|
||||
{
|
||||
struct bson_encoder_s encoder;
|
||||
struct bson_decoder_s decoder;
|
||||
void *buf;
|
||||
int len;
|
||||
|
||||
/* encode data to a memory buffer */
|
||||
if (bson_encoder_init_buf(&encoder, NULL, 0))
|
||||
errx(1, "FAIL: bson_encoder_init_buf");
|
||||
encode(&encoder);
|
||||
len = bson_encoder_buf_size(&encoder);
|
||||
if (len <= 0)
|
||||
errx(1, "FAIL: bson_encoder_buf_len");
|
||||
buf = bson_encoder_buf_data(&encoder);
|
||||
if (buf == NULL)
|
||||
errx(1, "FAIL: bson_encoder_buf_data");
|
||||
|
||||
/* now test-decode it */
|
||||
if (bson_decoder_init_buf(&decoder, buf, len, decode_callback, NULL))
|
||||
errx(1, "FAIL: bson_decoder_init_buf");
|
||||
decode(&decoder);
|
||||
free(buf);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
@@ -39,6 +39,7 @@
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -112,7 +112,7 @@ int test_uart_send(int argc, char *argv[])
|
||||
|
||||
char sample_test_uart[25];// = {'S', 'A', 'M', 'P', 'L', 'E', ' ', '\n'};
|
||||
|
||||
int i, r, n;
|
||||
int i, n;
|
||||
|
||||
uint64_t start_time = hrt_absolute_time();
|
||||
|
||||
|
||||
@@ -96,6 +96,7 @@ extern int test_time(int argc, char *argv[]);
|
||||
extern int test_uart_console(int argc, char *argv[]);
|
||||
extern int test_jig_voltages(int argc, char *argv[]);
|
||||
extern int test_param(int argc, char *argv[]);
|
||||
extern int test_bson(int argc, char *argv[]);
|
||||
extern int test_file(int argc, char *argv[]);
|
||||
|
||||
#endif /* __APPS_PX4_TESTS_H */
|
||||
|
||||
@@ -109,6 +109,7 @@ struct {
|
||||
{"all", test_all, OPT_NOALLTEST | OPT_NOJIGTEST, 0},
|
||||
{"jig", test_jig, OPT_NOJIGTEST | OPT_NOALLTEST, 0},
|
||||
{"param", test_param, 0, 0},
|
||||
{"bson", test_bson, 0, 0},
|
||||
{"file", test_file, 0, 0},
|
||||
{"help", test_help, OPT_NOALLTEST | OPT_NOHELP | OPT_NOJIGTEST, 0},
|
||||
{NULL, NULL, 0, 0}
|
||||
|
||||
@@ -73,8 +73,6 @@ test_param(int argc, char *argv[])
|
||||
if ((uint32_t)val != 0xa5a5a5a5)
|
||||
errx(1, "parameter value mismatch after write");
|
||||
|
||||
param_export(-1, false);
|
||||
|
||||
warnx("parameter test PASS");
|
||||
|
||||
return 0;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+216
-35
@@ -39,44 +39,74 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <err.h>
|
||||
|
||||
#include "tinybson.h"
|
||||
|
||||
|
||||
#if 0
|
||||
# define debug(fmt, args...) do { warnx("BSON: " fmt, ##args); } while(0)
|
||||
#else
|
||||
# define debug(fmt, args...) do { } while(0)
|
||||
#endif
|
||||
|
||||
#define CODER_CHECK(_c) do { if (_c->fd == -1) return -1; } while(0)
|
||||
#define CODER_KILL(_c, _reason) do { debug("killed: %s", _reason); _c->fd = -1; return -1; } while(0)
|
||||
#define CODER_CHECK(_c) do { if (_c->dead) { debug("coder dead"); return -1; }} while(0)
|
||||
#define CODER_KILL(_c, _reason) do { debug("killed: %s", _reason); _c->dead = true; return -1; } while(0)
|
||||
|
||||
static int
|
||||
read_x(bson_decoder_t decoder, void *p, size_t s)
|
||||
{
|
||||
CODER_CHECK(decoder);
|
||||
|
||||
if (decoder->fd > -1)
|
||||
return (read(decoder->fd, p, s) == (int)s) ? 0 : -1;
|
||||
|
||||
if (decoder->buf != NULL) {
|
||||
/* staged operations to avoid integer overflow for corrupt data */
|
||||
if (s >= decoder->bufsize)
|
||||
CODER_KILL(decoder, "buffer too small for read");
|
||||
if ((decoder->bufsize - s) < decoder->bufpos)
|
||||
CODER_KILL(decoder, "not enough data for read");
|
||||
memcpy(p, (decoder->buf + decoder->bufpos), s);
|
||||
decoder->bufpos += s;
|
||||
return 0;
|
||||
}
|
||||
debug("no source");
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
read_int8(bson_decoder_t decoder, int8_t *b)
|
||||
{
|
||||
return (read(decoder->fd, b, sizeof(*b)) == sizeof(*b)) ? 0 : -1;
|
||||
return read_x(decoder, b, sizeof(*b));
|
||||
}
|
||||
|
||||
static int
|
||||
read_int32(bson_decoder_t decoder, int32_t *i)
|
||||
{
|
||||
return (read(decoder->fd, i, sizeof(*i)) == sizeof(*i)) ? 0 : -1;
|
||||
return read_x(decoder, i, sizeof(*i));
|
||||
}
|
||||
|
||||
static int
|
||||
read_int64(bson_decoder_t decoder, int64_t *i)
|
||||
{
|
||||
return read_x(decoder, i, sizeof(*i));
|
||||
}
|
||||
|
||||
static int
|
||||
read_double(bson_decoder_t decoder, double *d)
|
||||
{
|
||||
return (read(decoder->fd, d, sizeof(*d)) == sizeof(*d)) ? 0 : -1;
|
||||
return read_x(decoder, d, sizeof(*d));
|
||||
}
|
||||
|
||||
int
|
||||
bson_decoder_init(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *private)
|
||||
bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *private)
|
||||
{
|
||||
int32_t junk;
|
||||
|
||||
decoder->fd = fd;
|
||||
decoder->buf = NULL;
|
||||
decoder->dead = false;
|
||||
decoder->callback = callback;
|
||||
decoder->private = private;
|
||||
decoder->nesting = 1;
|
||||
@@ -85,7 +115,42 @@ bson_decoder_init(bson_decoder_t decoder, int fd, bson_decoder_callback callback
|
||||
|
||||
/* read and discard document size */
|
||||
if (read_int32(decoder, &junk))
|
||||
CODER_KILL(decoder, "failed discarding length");
|
||||
|
||||
/* ready for decoding */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_decoder_callback callback, void *private)
|
||||
{
|
||||
int32_t len;
|
||||
|
||||
/* argument sanity */
|
||||
if ((buf == NULL) || (callback == NULL))
|
||||
return -1;
|
||||
|
||||
decoder->fd = -1;
|
||||
decoder->buf = (uint8_t *)buf;
|
||||
decoder->dead = false;
|
||||
if (bufsize == 0) {
|
||||
decoder->bufsize = *(uint32_t *)buf;
|
||||
debug("auto-detected %u byte object", decoder->bufsize);
|
||||
} else {
|
||||
decoder->bufsize = bufsize;
|
||||
}
|
||||
decoder->bufpos = 0;
|
||||
decoder->callback = callback;
|
||||
decoder->private = private;
|
||||
decoder->nesting = 1;
|
||||
decoder->pending = 0;
|
||||
decoder->node.type = BSON_UNDEFINED;
|
||||
|
||||
/* read and discard document size */
|
||||
if (read_int32(decoder, &len))
|
||||
CODER_KILL(decoder, "failed reading length");
|
||||
if ((len > 0) && (len > (int)decoder->bufsize))
|
||||
CODER_KILL(decoder, "document length larger than buffer");
|
||||
|
||||
/* ready for decoding */
|
||||
return 0;
|
||||
@@ -95,6 +160,7 @@ int
|
||||
bson_decoder_next(bson_decoder_t decoder)
|
||||
{
|
||||
int8_t tbyte;
|
||||
int32_t tint;
|
||||
unsigned nlen;
|
||||
|
||||
CODER_CHECK(decoder);
|
||||
@@ -133,7 +199,9 @@ bson_decoder_next(bson_decoder_t decoder)
|
||||
debug("got type byte 0x%02x", decoder->node.type);
|
||||
|
||||
/* EOO is special; it has no name/data following */
|
||||
if (decoder->node.type != BSON_EOO) {
|
||||
if (decoder->node.type == BSON_EOO) {
|
||||
decoder->node.name[0] = '\0';
|
||||
} else {
|
||||
|
||||
/* get the node name */
|
||||
nlen = 0;
|
||||
@@ -154,8 +222,20 @@ bson_decoder_next(bson_decoder_t decoder)
|
||||
debug("got name '%s'", decoder->node.name);
|
||||
|
||||
switch (decoder->node.type) {
|
||||
case BSON_INT:
|
||||
if (read_int32(decoder, &decoder->node.i))
|
||||
case BSON_BOOL:
|
||||
if (read_int8(decoder, &tbyte))
|
||||
CODER_KILL(decoder, "read error on BSON_BOOL");
|
||||
decoder->node.b = (tbyte != 0);
|
||||
break;
|
||||
|
||||
case BSON_INT32:
|
||||
if (read_int32(decoder, &tint))
|
||||
CODER_KILL(decoder, "read error on BSON_INT");
|
||||
decoder->node.i = tint;
|
||||
break;
|
||||
|
||||
case BSON_INT64:
|
||||
if (read_int64(decoder, &decoder->node.i))
|
||||
CODER_KILL(decoder, "read error on BSON_INT");
|
||||
|
||||
break;
|
||||
@@ -169,7 +249,6 @@ bson_decoder_next(bson_decoder_t decoder)
|
||||
case BSON_STRING:
|
||||
if (read_int32(decoder, &decoder->pending))
|
||||
CODER_KILL(decoder, "read error on BSON_STRING length");
|
||||
|
||||
break;
|
||||
|
||||
case BSON_BINDATA:
|
||||
@@ -199,14 +278,10 @@ bson_decoder_copy_data(bson_decoder_t decoder, void *buf)
|
||||
|
||||
CODER_CHECK(decoder);
|
||||
|
||||
/* if data already copied, return zero bytes */
|
||||
if (decoder->pending == 0)
|
||||
return 0;
|
||||
/* copy data */
|
||||
result = read_x(decoder, buf, decoder->pending);
|
||||
|
||||
/* copy bytes per the node size */
|
||||
result = read(decoder->fd, buf, decoder->pending);
|
||||
|
||||
if (result != decoder->pending)
|
||||
if (result != 0)
|
||||
CODER_KILL(decoder, "read error on copy_data");
|
||||
|
||||
/* pending count is discharged */
|
||||
@@ -220,25 +295,57 @@ bson_decoder_data_pending(bson_decoder_t decoder)
|
||||
return decoder->pending;
|
||||
}
|
||||
|
||||
static int
|
||||
write_x(bson_encoder_t encoder, const void *p, size_t s)
|
||||
{
|
||||
CODER_CHECK(encoder);
|
||||
|
||||
if (encoder->fd > -1)
|
||||
return (write(encoder->fd, p, s) == (int)s) ? 0 : -1;
|
||||
|
||||
/* do we need to extend the buffer? */
|
||||
while ((encoder->bufpos + s) > encoder->bufsize) {
|
||||
if (!encoder->realloc_ok)
|
||||
CODER_KILL(encoder, "fixed-size buffer overflow");
|
||||
|
||||
uint8_t *newbuf = realloc(encoder->buf, encoder->bufsize + BSON_BUF_INCREMENT);
|
||||
if (newbuf == NULL)
|
||||
CODER_KILL(encoder, "could not grow buffer");
|
||||
|
||||
encoder->buf = newbuf;
|
||||
encoder->bufsize += BSON_BUF_INCREMENT;
|
||||
debug("allocated %d bytes", BSON_BUF_INCREMENT);
|
||||
}
|
||||
|
||||
memcpy(encoder->buf + encoder->bufpos, p, s);
|
||||
encoder->bufpos += s;
|
||||
debug("appended %d bytes", s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
write_int8(bson_encoder_t encoder, int8_t b)
|
||||
{
|
||||
debug("write_int8 %d", b);
|
||||
return (write(encoder->fd, &b, sizeof(b)) == sizeof(b)) ? 0 : -1;
|
||||
return write_x(encoder, &b, sizeof(b));
|
||||
}
|
||||
|
||||
static int
|
||||
write_int32(bson_encoder_t encoder, int32_t i)
|
||||
{
|
||||
debug("write_int32 %d", i);
|
||||
return (write(encoder->fd, &i, sizeof(i)) == sizeof(i)) ? 0 : -1;
|
||||
return write_x(encoder, &i, sizeof(i));
|
||||
}
|
||||
|
||||
static int
|
||||
write_int64(bson_encoder_t encoder, int64_t i)
|
||||
{
|
||||
return write_x(encoder, &i, sizeof(i));
|
||||
}
|
||||
|
||||
static int
|
||||
write_double(bson_encoder_t encoder, double d)
|
||||
{
|
||||
debug("write_double");
|
||||
return (write(encoder->fd, &d, sizeof(d)) == sizeof(d)) ? 0 : -1;
|
||||
return write_x(encoder, &d, sizeof(d));
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -247,16 +354,17 @@ write_name(bson_encoder_t encoder, const char *name)
|
||||
size_t len = strlen(name);
|
||||
|
||||
if (len > BSON_MAXNAME)
|
||||
return -1;
|
||||
CODER_KILL(encoder, "node name too long");
|
||||
|
||||
debug("write name '%s' len %d", name, len);
|
||||
return (write(encoder->fd, name, len + 1) == (int)(len + 1)) ? 0 : -1;
|
||||
return write_x(encoder, name, len + 1);
|
||||
}
|
||||
|
||||
int
|
||||
bson_encoder_init(bson_encoder_t encoder, int fd)
|
||||
bson_encoder_init_file(bson_encoder_t encoder, int fd)
|
||||
{
|
||||
encoder->fd = fd;
|
||||
encoder->buf = NULL;
|
||||
encoder->dead = false;
|
||||
|
||||
if (write_int32(encoder, 0))
|
||||
CODER_KILL(encoder, "write error on document length");
|
||||
@@ -264,6 +372,27 @@ bson_encoder_init(bson_encoder_t encoder, int fd)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize)
|
||||
{
|
||||
encoder->fd = -1;
|
||||
encoder->buf = (uint8_t *)buf;
|
||||
encoder->bufpos = 0;
|
||||
encoder->dead = false;
|
||||
if (encoder->buf == NULL) {
|
||||
encoder->bufsize = 0;
|
||||
encoder->realloc_ok = true;
|
||||
} else {
|
||||
encoder->bufsize = bufsize;
|
||||
encoder->realloc_ok = false;
|
||||
}
|
||||
|
||||
if (write_int32(encoder, 0))
|
||||
CODER_KILL(encoder, "write error on document length");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
bson_encoder_fini(bson_encoder_t encoder)
|
||||
{
|
||||
@@ -272,17 +401,69 @@ bson_encoder_fini(bson_encoder_t encoder)
|
||||
if (write_int8(encoder, BSON_EOO))
|
||||
CODER_KILL(encoder, "write error on document terminator");
|
||||
|
||||
/* hack to fix up length for in-buffer documents */
|
||||
if (encoder->buf != NULL) {
|
||||
int32_t len = bson_encoder_buf_size(encoder);
|
||||
memcpy(encoder->buf, &len, sizeof(len));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
bson_encoder_append_int(bson_encoder_t encoder, const char *name, int32_t value)
|
||||
bson_encoder_buf_size(bson_encoder_t encoder)
|
||||
{
|
||||
CODER_CHECK(encoder);
|
||||
|
||||
if (write_int8(encoder, BSON_INT) ||
|
||||
if (encoder->fd > -1)
|
||||
return -1;
|
||||
|
||||
return encoder->bufpos;
|
||||
}
|
||||
|
||||
void *
|
||||
bson_encoder_buf_data(bson_encoder_t encoder)
|
||||
{
|
||||
/* note, no CODER_CHECK here as the caller has to clean up dead buffers */
|
||||
|
||||
if (encoder->fd > -1)
|
||||
return NULL;
|
||||
|
||||
return encoder->buf;
|
||||
}
|
||||
|
||||
int bson_encoder_append_bool(bson_encoder_t encoder, const char *name, bool value)
|
||||
{
|
||||
CODER_CHECK(encoder);
|
||||
|
||||
if (write_int8(encoder, BSON_BOOL) ||
|
||||
write_name(encoder, name) ||
|
||||
write_int32(encoder, value))
|
||||
write_int8(encoder, value ? 1 : 0))
|
||||
CODER_KILL(encoder, "write error on BSON_BOOL");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value)
|
||||
{
|
||||
bool result;
|
||||
|
||||
CODER_CHECK(encoder);
|
||||
|
||||
/* use the smallest encoding that will hold the value */
|
||||
if (value == (int32_t)value) {
|
||||
debug("encoding %lld as int32", value);
|
||||
result = write_int8(encoder, BSON_INT32) ||
|
||||
write_name(encoder, name) ||
|
||||
write_int32(encoder, value);
|
||||
} else {
|
||||
debug("encoding %lld as int64", value);
|
||||
result = write_int8(encoder, BSON_INT64) ||
|
||||
write_name(encoder, name) ||
|
||||
write_int64(encoder, value);
|
||||
}
|
||||
if (result)
|
||||
CODER_KILL(encoder, "write error on BSON_INT");
|
||||
|
||||
return 0;
|
||||
@@ -309,12 +490,12 @@ bson_encoder_append_string(bson_encoder_t encoder, const char *name, const char
|
||||
|
||||
CODER_CHECK(encoder);
|
||||
|
||||
len = strlen(string);
|
||||
len = strlen(string) + 1; /* include trailing nul */
|
||||
|
||||
if (write_int8(encoder, BSON_DOUBLE) ||
|
||||
if (write_int8(encoder, BSON_STRING) ||
|
||||
write_name(encoder, name) ||
|
||||
write_int32(encoder, len) ||
|
||||
write(encoder->fd, name, len + 1) != (int)(len + 1))
|
||||
write_x(encoder, string, len))
|
||||
CODER_KILL(encoder, "write error on BSON_STRING");
|
||||
|
||||
return 0;
|
||||
@@ -329,7 +510,7 @@ bson_encoder_append_binary(bson_encoder_t encoder, const char *name, bson_binary
|
||||
write_name(encoder, name) ||
|
||||
write_int32(encoder, size) ||
|
||||
write_int8(encoder, subtype) ||
|
||||
write(encoder->fd, data, size) != (int)(size))
|
||||
write_x(encoder, data, size))
|
||||
CODER_KILL(encoder, "write error on BSON_BINDATA");
|
||||
|
||||
return 0;
|
||||
|
||||
+105
-10
@@ -59,9 +59,8 @@ typedef enum {
|
||||
BSON_BOOL = 8,
|
||||
BSON_DATE = 9,
|
||||
BSON_NULL = 10,
|
||||
BSON_INT = 16,
|
||||
BSON_TIMESTAMP = 17,
|
||||
BSON_LONG = 18
|
||||
BSON_INT32 = 16,
|
||||
BSON_INT64 = 18
|
||||
} bson_type_t;
|
||||
|
||||
typedef enum bson_binary_subtype {
|
||||
@@ -74,6 +73,11 @@ typedef enum bson_binary_subtype {
|
||||
*/
|
||||
#define BSON_MAXNAME 32
|
||||
|
||||
/**
|
||||
* Buffer growth increment when writing to a buffer.
|
||||
*/
|
||||
#define BSON_BUF_INCREMENT 128
|
||||
|
||||
/**
|
||||
* Node structure passed to the callback.
|
||||
*/
|
||||
@@ -82,7 +86,7 @@ typedef struct bson_node_s {
|
||||
bson_type_t type;
|
||||
bson_binary_subtype_t subtype;
|
||||
union {
|
||||
int32_t i;
|
||||
int64_t i;
|
||||
double d;
|
||||
bool b;
|
||||
};
|
||||
@@ -92,11 +96,21 @@ typedef struct bson_decoder_s *bson_decoder_t;
|
||||
|
||||
/**
|
||||
* Node callback.
|
||||
*
|
||||
* The node callback function's return value is returned by bson_decoder_next.
|
||||
*/
|
||||
typedef int (* bson_decoder_callback)(bson_decoder_t decoder, void *private, bson_node_t node);
|
||||
|
||||
struct bson_decoder_s {
|
||||
/* file reader state */
|
||||
int fd;
|
||||
|
||||
/* buffer reader state */
|
||||
uint8_t *buf;
|
||||
size_t bufsize;
|
||||
unsigned bufpos;
|
||||
|
||||
bool dead;
|
||||
bson_decoder_callback callback;
|
||||
void *private;
|
||||
unsigned nesting;
|
||||
@@ -105,7 +119,7 @@ struct bson_decoder_s {
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialise the decoder.
|
||||
* Initialise the decoder to read from a file.
|
||||
*
|
||||
* @param decoder Decoder state structure to be initialised.
|
||||
* @param fd File to read BSON data from.
|
||||
@@ -113,7 +127,21 @@ struct bson_decoder_s {
|
||||
* @param private Callback private data, stored in node.
|
||||
* @return Zero on success.
|
||||
*/
|
||||
__EXPORT int bson_decoder_init(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *private);
|
||||
__EXPORT int bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *private);
|
||||
|
||||
/**
|
||||
* Initialise the decoder to read from a buffer in memory.
|
||||
*
|
||||
* @param decoder Decoder state structure to be initialised.
|
||||
* @param buf Buffer to read from.
|
||||
* @param bufsize Size of the buffer (BSON object may be smaller). May be
|
||||
* passed as zero if the buffer size should be extracted from the
|
||||
* BSON header only.
|
||||
* @param callback Callback to be invoked by bson_decoder_next
|
||||
* @param private Callback private data, stored in node.
|
||||
* @return Zero on success.
|
||||
*/
|
||||
__EXPORT int bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_decoder_callback callback, void *private);
|
||||
|
||||
/**
|
||||
* Process the next node from the stream and invoke the callback.
|
||||
@@ -142,37 +170,104 @@ __EXPORT size_t bson_decoder_data_pending(bson_decoder_t decoder);
|
||||
* Encoder state structure.
|
||||
*/
|
||||
typedef struct bson_encoder_s {
|
||||
/* file writer state */
|
||||
int fd;
|
||||
|
||||
/* buffer writer state */
|
||||
uint8_t *buf;
|
||||
unsigned bufsize;
|
||||
unsigned bufpos;
|
||||
|
||||
bool realloc_ok;
|
||||
bool dead;
|
||||
|
||||
} *bson_encoder_t;
|
||||
|
||||
/**
|
||||
* Initialze the encoder.
|
||||
* Initialze the encoder for writing to a file.
|
||||
*
|
||||
* @param encoder Encoder state structure to be initialised.
|
||||
* @param fd File to write to.
|
||||
* @return Zero on success.
|
||||
*/
|
||||
__EXPORT int bson_encoder_init(bson_encoder_t encoder, int fd);
|
||||
__EXPORT int bson_encoder_init_file(bson_encoder_t encoder, int fd);
|
||||
|
||||
/**
|
||||
* Initialze the encoder for writing to a buffer.
|
||||
*
|
||||
* @param encoder Encoder state structure to be initialised.
|
||||
* @param buf Buffer pointer to use, or NULL if the buffer
|
||||
* should be allocated by the encoder.
|
||||
* @param bufsize Maximum buffer size, or zero for no limit. If
|
||||
* the buffer is supplied, the size of the supplied buffer.
|
||||
* @return Zero on success.
|
||||
*/
|
||||
__EXPORT int bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize);
|
||||
|
||||
/**
|
||||
* Finalise the encoded stream.
|
||||
*
|
||||
* @param encoder The encoder to finalise.
|
||||
*/
|
||||
__EXPORT int bson_encoder_fini(bson_encoder_t encoder);
|
||||
|
||||
/**
|
||||
* Append an integer to the encoded stream.
|
||||
* Fetch the size of the encoded object; only valid for buffer operations.
|
||||
*/
|
||||
__EXPORT int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int32_t value);
|
||||
__EXPORT int bson_encoder_buf_size(bson_encoder_t encoder);
|
||||
|
||||
/**
|
||||
* Get a pointer to the encoded object buffer.
|
||||
*
|
||||
* Note that if the buffer was allocated by the encoder, it is the caller's responsibility
|
||||
* to free this buffer.
|
||||
*/
|
||||
__EXPORT void *bson_encoder_buf_data(bson_encoder_t encoder);
|
||||
|
||||
/**
|
||||
* Append a boolean to the encoded stream.
|
||||
*
|
||||
* @param encoder Encoder state.
|
||||
* @param name Node name.
|
||||
* @param value Value to be encoded.
|
||||
*/
|
||||
__EXPORT int bson_encoder_append_bool(bson_encoder_t encoder, const char *name, bool value);
|
||||
|
||||
/**
|
||||
* Append an integer to the encoded stream.
|
||||
*
|
||||
* @param encoder Encoder state.
|
||||
* @param name Node name.
|
||||
* @param value Value to be encoded.
|
||||
*/
|
||||
__EXPORT int bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value);
|
||||
|
||||
/**
|
||||
* Append a double to the encoded stream
|
||||
*
|
||||
* @param encoder Encoder state.
|
||||
* @param name Node name.
|
||||
* @param value Value to be encoded.
|
||||
*/
|
||||
__EXPORT int bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value);
|
||||
|
||||
/**
|
||||
* Append a string to the encoded stream.
|
||||
*
|
||||
* @param encoder Encoder state.
|
||||
* @param name Node name.
|
||||
* @param string Nul-terminated C string.
|
||||
*/
|
||||
__EXPORT int bson_encoder_append_string(bson_encoder_t encoder, const char *name, const char *string);
|
||||
|
||||
/**
|
||||
* Append a binary blob to the encoded stream.
|
||||
*
|
||||
* @param encoder Encoder state.
|
||||
* @param name Node name.
|
||||
* @param subtype Binary data subtype.
|
||||
* @param size Data size.
|
||||
* @param data Buffer containing data to be encoded.
|
||||
*/
|
||||
__EXPORT int bson_encoder_append_binary(bson_encoder_t encoder, const char *name, bson_binary_subtype_t subtype, size_t size, const void *data);
|
||||
|
||||
|
||||
@@ -452,7 +452,7 @@ param_reset(param_t param)
|
||||
|
||||
/* if we found one, erase it */
|
||||
if (s != NULL) {
|
||||
int pos = utarry_eltidx(param_values, s);
|
||||
int pos = utarray_eltidx(param_values, s);
|
||||
utarray_erase(param_values, pos, 1);
|
||||
}
|
||||
}
|
||||
@@ -489,7 +489,7 @@ param_export(int fd, bool only_unsaved)
|
||||
|
||||
param_lock();
|
||||
|
||||
bson_encoder_init(&encoder, fd);
|
||||
bson_encoder_init_file(&encoder, fd);
|
||||
|
||||
/* no modified parameters -> we are done */
|
||||
if (param_values == NULL) {
|
||||
@@ -600,7 +600,7 @@ param_import_callback(bson_decoder_t decoder, void *private, bson_node_t node)
|
||||
*/
|
||||
|
||||
switch (node->type) {
|
||||
case BSON_INT:
|
||||
case BSON_INT32:
|
||||
if (param_type(param) != PARAM_TYPE_INT32) {
|
||||
debug("unexpected type for '%s", node->name);
|
||||
goto out;
|
||||
@@ -680,7 +680,7 @@ param_import_internal(int fd, bool mark_saved)
|
||||
int result = -1;
|
||||
struct param_import_state state;
|
||||
|
||||
if (bson_decoder_init(&decoder, fd, param_import_callback, &state)) {
|
||||
if (bson_decoder_init_file(&decoder, fd, param_import_callback, &state)) {
|
||||
debug("decoder init failed");
|
||||
goto out;
|
||||
}
|
||||
|
||||
@@ -110,76 +110,3 @@ int task_spawn(const char *name, int scheduler, int priority, int stack_size, ma
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
#define PX4_BOARD_ID_FMU (5)
|
||||
|
||||
int fmu_get_board_info(struct fmu_board_info_s *info)
|
||||
{
|
||||
/* Check which FMU version we're on */
|
||||
struct stat sb;
|
||||
int statres;
|
||||
|
||||
/* Copy version-specific fields */
|
||||
statres = stat("/dev/bma180", &sb);
|
||||
|
||||
if (statres == OK) {
|
||||
/* BMA180 indicates a v1.5-v1.6 board */
|
||||
strcpy(info->board_name, "FMU v1.6");
|
||||
info->board_version = 16;
|
||||
|
||||
} else {
|
||||
statres = stat("/dev/accel", &sb);
|
||||
|
||||
if (statres == OK) {
|
||||
/* MPU-6000 indicates a v1.7+ board */
|
||||
strcpy(info->board_name, "FMU v1.7");
|
||||
info->board_version = 17;
|
||||
|
||||
} else {
|
||||
/* If no BMA and no MPU is present, it is a v1.3 board */
|
||||
strcpy(info->board_name, "FMU v1.3");
|
||||
info->board_version = 13;
|
||||
}
|
||||
}
|
||||
|
||||
/* Copy general FMU fields */
|
||||
memcpy(info->header, "PX4", 3);
|
||||
info->board_id = PX4_BOARD_ID_FMU;
|
||||
|
||||
return sizeof(struct fmu_board_info_s);
|
||||
}
|
||||
|
||||
int carrier_store_board_info(const struct carrier_board_info_s *info)
|
||||
{
|
||||
int ret;
|
||||
int fd = open("/dev/eeprom", O_RDWR | O_NONBLOCK);
|
||||
|
||||
if (fd < 0) fprintf(stderr, "[boardinfo carrier] ERROR opening carrier eeprom\n");
|
||||
|
||||
/* Enforce correct header */
|
||||
ret = write(fd, info, sizeof(struct carrier_board_info_s));
|
||||
//ret = write(fd, "PX4", 3);
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int carrier_get_board_info(struct carrier_board_info_s *info)
|
||||
{
|
||||
int ret;
|
||||
int fd = open("/dev/eeprom", O_RDONLY | O_NONBLOCK);
|
||||
|
||||
if (fd < 0)
|
||||
return -1; /* no board */
|
||||
|
||||
ret = read(fd, info, sizeof(struct carrier_board_info_s));
|
||||
|
||||
/* Enforce NUL termination of human-readable string */
|
||||
if (ret == sizeof(struct carrier_board_info_s)) {
|
||||
info->board_name[sizeof(info->board_name) - 1] = '\0';
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -117,11 +117,6 @@ struct __multiport_info {
|
||||
};
|
||||
__EXPORT extern const struct __multiport_info multiport_info;
|
||||
|
||||
__EXPORT int carrier_store_board_info(const struct carrier_board_info_s *info);
|
||||
__EXPORT int carrier_get_board_info(struct carrier_board_info_s *info);
|
||||
|
||||
__EXPORT int fmu_get_board_info(struct fmu_board_info_s *info);
|
||||
|
||||
__END_DECLS
|
||||
|
||||
#endif /* SYSTEMLIB_H_ */
|
||||
|
||||
Reference in New Issue
Block a user