Changed device::px4_device_handle_t to device::file_t

This change allowed the _posix.cpp file changes to be merged
back into the original files.

Signed-off-by: Mark Charlebois <charlebm@gmail.com>
This commit is contained in:
Mark Charlebois
2015-04-24 17:49:35 -07:00
parent 20d35e33da
commit d913ec8dc9
28 changed files with 600 additions and 1327 deletions
@@ -40,6 +40,7 @@
*/
#include <px4_config.h>
#include <px4_getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -50,7 +51,9 @@
#include <assert.h>
#include <math.h>
#include <poll.h>
#ifndef __PX4_QURT
#include <termios.h>
#endif
#include <time.h>
#include <math.h> /* isinf / isnan checks */
@@ -68,7 +71,7 @@
#include <systemlib/systemlib.h>
#include <geo/geo.h>
#include <dataman/dataman.h>
#include <mathlib/mathlib.h>
//#include <mathlib/mathlib.h>
#include <mavlink/mavlink_log.h>
#include <uORB/topics/parameter_update.h>
@@ -96,8 +99,10 @@ static const int ERROR = -1;
static Mavlink *_mavlink_instances = nullptr;
#ifdef __PX4_NUTTX
/* TODO: if this is a class member it crashes */
static struct file_operations fops;
#endif
static const uint8_t mavlink_message_lengths[256] = MAVLINK_MESSAGE_LENGTHS;
static const uint8_t mavlink_message_crcs[256] = MAVLINK_MESSAGE_CRCS;
@@ -114,6 +119,9 @@ extern mavlink_system_t mavlink_system;
static void usage(void);
Mavlink::Mavlink() :
#ifndef __PX4_NUTTX
VDev("mavlink-log", MAVLINK_LOG_DEVICE),
#endif
_device_name(DEFAULT_DEVICE_NAME),
_task_should_exit(false),
next(nullptr),
@@ -140,7 +148,9 @@ Mavlink::Mavlink() :
_forwarding_on(false),
_passing_on(false),
_ftp_on(false),
#ifndef __PX4_QURT
_uart_fd(-1),
#endif
_baudrate(57600),
_datarate(1000),
_datarate_events(500),
@@ -175,7 +185,9 @@ Mavlink::Mavlink() :
_loop_perf(perf_alloc(PC_ELAPSED, "mavlink_el")),
_txerr_perf(perf_alloc(PC_COUNT, "mavlink_txe"))
{
#ifdef __PX4_NUTTX
fops.ioctl = (int (*)(file *, int, long unsigned int))&mavlink_dev_ioctl;
#endif
_instance_id = Mavlink::instance_count();
@@ -216,7 +228,8 @@ Mavlink::Mavlink() :
#endif
default:
errx(1, "instance ID is out of range");
warnx("instance ID is out of range");
px4_task_exit(1);
break;
}
@@ -398,6 +411,7 @@ Mavlink::forward_message(const mavlink_message_t *msg, Mavlink *self)
}
}
#ifndef __PX4_QURT
int
Mavlink::get_uart_fd(unsigned index)
{
@@ -415,6 +429,7 @@ Mavlink::get_uart_fd()
{
return _uart_fd;
}
#endif // __PX4_QURT
int
Mavlink::get_instance_id()
@@ -433,7 +448,11 @@ Mavlink::get_channel()
****************************************************************************/
int
Mavlink::mavlink_dev_ioctl(struct file *filep, int cmd, unsigned long arg)
#ifdef __PX4_NUTTX
Mavlink::mavlink_dev_ioctl(struct file *filp, int cmd, unsigned long arg)
#else
Mavlink::ioctl(device::file_t *filp, int cmd, unsigned long arg)
#endif
{
switch (cmd) {
case (int)MAVLINK_IOC_SEND_TEXT_INFO:
@@ -549,6 +568,7 @@ int Mavlink::get_component_id()
return mavlink_system.compid;
}
#ifndef __PX4_QURT
int Mavlink::mavlink_open_uart(int baud, const char *uart_name, struct termios *uart_config_original, bool *is_usb)
{
/* process baud rate */
@@ -604,7 +624,7 @@ int Mavlink::mavlink_open_uart(int baud, const char *uart_name, struct termios *
}
/* open uart */
_uart_fd = open(uart_name, O_RDWR | O_NOCTTY);
_uart_fd = ::open(uart_name, O_RDWR | O_NOCTTY);
if (_uart_fd < 0) {
return _uart_fd;
@@ -619,7 +639,7 @@ int Mavlink::mavlink_open_uart(int baud, const char *uart_name, struct termios *
/* Back up the original uart configuration to restore it after exit */
if ((termios_state = tcgetattr(_uart_fd, uart_config_original)) < 0) {
warnx("ERR GET CONF %s: %d\n", uart_name, termios_state);
close(_uart_fd);
::close(_uart_fd);
return -1;
}
@@ -635,7 +655,7 @@ int Mavlink::mavlink_open_uart(int baud, const char *uart_name, struct termios *
/* Set baud rate */
if (cfsetispeed(&uart_config, speed) < 0 || cfsetospeed(&uart_config, speed) < 0) {
warnx("ERR SET BAUD %s: %d\n", uart_name, termios_state);
close(_uart_fd);
::close(_uart_fd);
return -1;
}
@@ -643,7 +663,7 @@ int Mavlink::mavlink_open_uart(int baud, const char *uart_name, struct termios *
if ((termios_state = tcsetattr(_uart_fd, TCSANOW, &uart_config)) < 0) {
warnx("ERR SET CONF %s\n", uart_name);
close(_uart_fd);
::close(_uart_fd);
return -1;
}
@@ -653,7 +673,11 @@ int Mavlink::mavlink_open_uart(int baud, const char *uart_name, struct termios *
* which is not an issue, but requires a separate call so we can fail silently.
*/
(void)tcgetattr(_uart_fd, &uart_config);
#ifdef CRTS_IFLOW
uart_config.c_cflag |= CRTS_IFLOW;
#else
uart_config.c_cflag |= CRTSCTS;
#endif
(void)tcsetattr(_uart_fd, TCSANOW, &uart_config);
/* setup output flow control */
@@ -696,6 +720,8 @@ Mavlink::enable_flow_control(bool enabled)
return ret;
}
#endif
int
Mavlink::set_hil_enabled(bool hil_enabled)
{
@@ -727,7 +753,13 @@ Mavlink::get_free_tx_buf()
* flow control if it continues to be full
*/
int buf_free = 0;
#ifndef __PX4_QURT
// No FIONWRITE on Linux
#if !defined(__PX4_LINUX)
(void) ioctl(_uart_fd, FIONWRITE, (unsigned long)&buf_free);
#endif
if (get_flow_control_enabled() && buf_free < FLOW_CONTROL_DISABLE_THRESHOLD) {
/* Disable hardware flow control:
@@ -741,6 +773,7 @@ Mavlink::get_free_tx_buf()
enable_flow_control(false);
}
}
#endif
return buf_free;
}
@@ -795,8 +828,9 @@ Mavlink::send_message(const uint8_t msgid, const void *msg, uint8_t component_ID
buf[MAVLINK_NUM_HEADER_BYTES + payload_len] = (uint8_t)(checksum & 0xFF);
buf[MAVLINK_NUM_HEADER_BYTES + payload_len + 1] = (uint8_t)(checksum >> 8);
#ifndef __PX4_QURT
/* send message to UART */
ssize_t ret = write(_uart_fd, buf, packet_len);
ssize_t ret = ::write(_uart_fd, buf, packet_len);
if (ret != (int) packet_len) {
count_txerr();
@@ -806,6 +840,7 @@ Mavlink::send_message(const uint8_t msgid, const void *msg, uint8_t component_ID
_last_write_success_time = _last_write_try_time;
count_txbytes(packet_len);
}
#endif
pthread_mutex_unlock(&_send_mutex);
}
@@ -845,8 +880,9 @@ Mavlink::resend_message(mavlink_message_t *msg)
buf[MAVLINK_NUM_HEADER_BYTES + msg->len] = (uint8_t)(msg->checksum & 0xFF);
buf[MAVLINK_NUM_HEADER_BYTES + msg->len + 1] = (uint8_t)(msg->checksum >> 8);
#ifndef __PX4_QURT
/* send message to UART */
ssize_t ret = write(_uart_fd, buf, packet_len);
ssize_t ret = ::write(_uart_fd, buf, packet_len);
if (ret != (int) packet_len) {
count_txerr();
@@ -856,6 +892,7 @@ Mavlink::resend_message(mavlink_message_t *msg)
_last_write_success_time = _last_write_try_time;
count_txbytes(packet_len);
}
#endif
pthread_mutex_unlock(&_send_mutex);
}
@@ -1195,38 +1232,42 @@ Mavlink::task_main(int argc, char *argv[])
_datarate = 0;
_mode = MAVLINK_MODE_NORMAL;
#ifdef __PX4_NUTTX
/* work around some stupidity in task_create's argv handling */
argc -= 2;
argv += 2;
#endif
/* don't exit from getopt loop to leave getopt global variables in consistent state,
* set error flag instead */
bool err_flag = false;
int myoptind=1;
const char *myoptarg = NULL;
while ((ch = getopt(argc, argv, "b:r:d:m:fpvwx")) != EOF) {
while ((ch = px4_getopt(argc, argv, "b:r:d:m:fpvwx", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'b':
_baudrate = strtoul(optarg, NULL, 10);
_baudrate = strtoul(myoptarg, NULL, 10);
if (_baudrate < 9600 || _baudrate > 921600) {
warnx("invalid baud rate '%s'", optarg);
warnx("invalid baud rate '%s'", myoptarg);
err_flag = true;
}
break;
case 'r':
_datarate = strtoul(optarg, NULL, 10);
_datarate = strtoul(myoptarg, NULL, 10);
if (_datarate < 10 || _datarate > MAX_DATA_RATE) {
warnx("invalid data rate '%s'", optarg);
warnx("invalid data rate '%s'", myoptarg);
err_flag = true;
}
break;
case 'd':
_device_name = optarg;
_device_name = myoptarg;
break;
// case 'e':
@@ -1234,13 +1275,13 @@ Mavlink::task_main(int argc, char *argv[])
// break;
case 'm':
if (strcmp(optarg, "custom") == 0) {
if (strcmp(myoptarg, "custom") == 0) {
_mode = MAVLINK_MODE_CUSTOM;
} else if (strcmp(optarg, "camera") == 0) {
} else if (strcmp(myoptarg, "camera") == 0) {
// left in here for compatibility
_mode = MAVLINK_MODE_ONBOARD;
} else if (strcmp(optarg, "onboard") == 0) {
} else if (strcmp(myoptarg, "onboard") == 0) {
_mode = MAVLINK_MODE_ONBOARD;
}
@@ -1296,6 +1337,7 @@ Mavlink::task_main(int argc, char *argv[])
/* flush stdout in case MAVLink is about to take it over */
fflush(stdout);
#ifndef __PX4_QURT
struct termios uart_config_original;
/* default values for arguments */
@@ -1305,6 +1347,7 @@ Mavlink::task_main(int argc, char *argv[])
warn("could not open %s", _device_name);
return ERROR;
}
#endif
/* initialize send mutex */
pthread_mutex_init(&_send_mutex, NULL);
@@ -1319,7 +1362,8 @@ Mavlink::task_main(int argc, char *argv[])
* marker ring buffer approach.
*/
if (OK != message_buffer_init(2 * sizeof(mavlink_message_t) + 1)) {
errx(1, "msg buf:");
warnx("msg buf:");
return 1;
}
/* initialize message buffer mutex */
@@ -1327,10 +1371,14 @@ Mavlink::task_main(int argc, char *argv[])
}
/* create the device node that's used for sending text log messages, etc. */
#ifdef __PX4_NUTTX
register_driver(MAVLINK_LOG_DEVICE, &fops, 0666, NULL);
#else
register_driver(MAVLINK_LOG_DEVICE, NULL);
#endif
/* initialize logging device */
_mavlink_fd = open(MAVLINK_LOG_DEVICE, 0);
_mavlink_fd = px4_open(MAVLINK_LOG_DEVICE, 0);
/* Initialize system properties */
mavlink_update_system();
@@ -1556,14 +1604,16 @@ Mavlink::task_main(int argc, char *argv[])
/* wait for threads to complete */
pthread_join(_receive_thread, NULL);
#ifndef __PX4_QURT
/* reset the UART flags to original state */
tcsetattr(_uart_fd, TCSANOW, &uart_config_original);
/* close UART */
close(_uart_fd);
::close(_uart_fd);
#endif
/* close mavlink logging device */
close(_mavlink_fd);
px4_close(_mavlink_fd);
if (_passing_on || _ftp_on) {
message_buffer_destroy();
@@ -1618,11 +1668,11 @@ Mavlink::start(int argc, char *argv[])
// between the starting task and the spawned
// task - start_helper() only returns
// when the started task exits.
task_spawn_cmd(buf,
px4_task_spawn_cmd(buf,
SCHED_DEFAULT,
SCHED_PRIORITY_DEFAULT,
2400,
(main_t)&Mavlink::start_helper,
(px4_main_t)&Mavlink::start_helper,
(char * const *)argv);
// Ensure that this shell command
@@ -1653,7 +1703,7 @@ Mavlink::display_status()
{
if (_rstatus.heartbeat_time > 0) {
printf("\tGCS heartbeat:\t%llu us ago\n", hrt_elapsed_time(&_rstatus.heartbeat_time));
printf("\tGCS heartbeat:\t%llu us ago\n", (unsigned long long)hrt_elapsed_time(&_rstatus.heartbeat_time));
}
printf("\tmavlink chan: #%u\n", _channel);
@@ -1743,11 +1793,13 @@ Mavlink::stream_command(int argc, char *argv[])
// If the link is not running we should complain, but not fall over
// because this is so easy to get wrong and not fatal. Warning is sufficient.
errx(0, "mavlink for device %s is not running", device_name);
warnx("mavlink for device %s is not running", device_name);
return 0;
}
} else {
errx(1, "usage: mavlink stream [-d device] -s stream -r rate");
warnx("usage: mavlink stream [-d device] -s stream -r rate");
return 1;
}
return OK;
@@ -1762,7 +1814,7 @@ int mavlink_main(int argc, char *argv[])
{
if (argc < 2) {
usage();
exit(1);
return 1;
}
if (!strcmp(argv[1], "start")) {
@@ -1771,7 +1823,7 @@ int mavlink_main(int argc, char *argv[])
} else if (!strcmp(argv[1], "stop")) {
warnx("mavlink stop is deprecated, use stop-all instead");
usage();
exit(1);
return 1;
} else if (!strcmp(argv[1], "stop-all")) {
return Mavlink::destroy_all_instances();
@@ -1784,7 +1836,7 @@ int mavlink_main(int argc, char *argv[])
} else {
usage();
exit(1);
return 1;
}
return 0;
+1 -1
View File
@@ -421,7 +421,7 @@ private:
#ifdef __PX4_NUTTX
static int mavlink_dev_ioctl(struct file *filep, int cmd, unsigned long arg);
#else
virtual int ioctl(device::px4_dev_handle_t *handlep, int cmd, unsigned long arg);
virtual int ioctl(device::file_t *filp, int cmd, unsigned long arg);
#endif
/**
+1 -1
View File
@@ -437,7 +437,7 @@ Mavlink::get_channel()
****************************************************************************/
int
Mavlink::ioctl(device::px4_dev_handle_t *handlep, int cmd, unsigned long arg)
Mavlink::ioctl(device::file_t *filp, int cmd, unsigned long arg)
{
switch (cmd) {
case (int)MAVLINK_IOC_SEND_TEXT_INFO:
+1 -7
View File
@@ -35,14 +35,8 @@
# MAVLink protocol to uORB interface process
#
MODULE_COMMAND = mavlink
ifeq ($(PX4_TARGET_OS),nuttx)
SRCS += mavlink_main_nuttx.cpp
else
SRCS += mavlink_main_posix.cpp
endif
SRCS += mavlink.c \
mavlink_main.cpp \
mavlink_mission.cpp \
mavlink_parameters.cpp \
mavlink_orb_subscription.cpp \