mavlink: refactor mavlink_log

This moves the mavlink_log interface from ioctl to uORB and enables the
mavlink statusmessage output for Snapdragon. The API changes will lead
to changes in all modules that are using it.
This commit is contained in:
Julian Oes
2016-03-15 18:21:52 +00:00
parent 790fa7667c
commit 2d4179a35b
9 changed files with 203 additions and 309 deletions
+24 -77
View File
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (c) 2012, 2015 PX4 Development Team. All rights reserved.
* Copyright (c) 2012-2016 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -44,93 +44,40 @@
#include <stdio.h>
#include <stdarg.h>
#include <mavlink/mavlink_log.h>
#include <uORB/topics/mavlink_log.h>
#include "mavlink_log.h"
__EXPORT void mavlink_logbuffer_init(struct mavlink_logbuffer *lb, int size)
__EXPORT void mavlink_vasprintf(int severity, orb_advert_t *mavlink_log_pub, const char *fmt, ...)
{
lb->size = size;
lb->start = 0;
lb->count = 0;
lb->elems = calloc(lb->size, sizeof(struct mavlink_logmessage));
}
// TODO: add compile check for maxlen
__EXPORT void mavlink_logbuffer_destroy(struct mavlink_logbuffer *lb)
{
lb->size = 0;
lb->start = 0;
lb->count = 0;
free(lb->elems);
}
__EXPORT int mavlink_logbuffer_is_full(struct mavlink_logbuffer *lb)
{
return lb->count == (int)lb->size;
}
__EXPORT int mavlink_logbuffer_is_empty(struct mavlink_logbuffer *lb)
{
return lb->count == 0;
}
__EXPORT void mavlink_logbuffer_write(struct mavlink_logbuffer *lb, const struct mavlink_logmessage *elem)
{
int end = (lb->start + lb->count) % lb->size;
memcpy(&(lb->elems[end]), elem, sizeof(struct mavlink_logmessage));
if (mavlink_logbuffer_is_full(lb)) {
lb->start = (lb->start + 1) % lb->size; /* full, overwrite */
} else {
++lb->count;
}
}
__EXPORT int mavlink_logbuffer_read(struct mavlink_logbuffer *lb, struct mavlink_logmessage *elem)
{
if (!mavlink_logbuffer_is_empty(lb)) {
memcpy(elem, &(lb->elems[lb->start]), sizeof(struct mavlink_logmessage));
lb->start = (lb->start + 1) % lb->size;
--lb->count;
return 0;
} else {
return 1;
}
}
__EXPORT void mavlink_logbuffer_vasprintf(struct mavlink_logbuffer *lb, int severity, const char *fmt, ...)
{
if (!fmt) {
return;
}
va_list ap;
va_start(ap, fmt);
int end = (lb->start + lb->count) % lb->size;
lb->elems[end].severity = severity;
vsnprintf(lb->elems[end].text, sizeof(lb->elems[end].text), fmt, ap);
va_end(ap);
/* increase count */
if (mavlink_logbuffer_is_full(lb)) {
lb->start = (lb->start + 1) % lb->size; /* full, overwrite */
} else {
++lb->count;
}
}
__EXPORT void mavlink_vasprintf(int _fd, int severity, const char *fmt, ...)
{
if (!fmt) {
if (mavlink_log_pub == NULL) {
return;
}
struct mavlink_log_s log_msg;
log_msg.severity = severity;
va_list ap;
va_start(ap, fmt);
char text[MAVLINK_LOG_MAXLEN + 1];
vsnprintf(text, sizeof(text), fmt, ap);
vsnprintf((char *)log_msg.text, sizeof(log_msg.text), fmt, ap);
va_end(ap);
px4_ioctl(_fd, severity, (unsigned long)&text[0]);
if (*mavlink_log_pub != NULL) {
orb_publish(ORB_ID(mavlink_log), *mavlink_log_pub, &log_msg);
} else {
*mavlink_log_pub = orb_advertise(ORB_ID(mavlink_log), &log_msg);
}
}
+140
View File
@@ -0,0 +1,140 @@
/****************************************************************************
*
* Copyright (c) 2012-2016 PX4 Development Team. All rights reserved.
*
* 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 mavlink_log.h
* MAVLink text logging.
*
* @author Lorenz Meier <lorenz@px4.io>
* @author Julian Oes <julian@oes.ch>
*/
#pragma once
#include <uORB/uORB.h>
/**
* The maximum string length supported.
*/
#define MAVLINK_LOG_MAXLEN 50
#ifdef __cplusplus
extern "C" {
#endif
__EXPORT void mavlink_vasprintf(int severity, orb_advert_t *mavlink_log_pub, const char *fmt, ...);
#ifdef __cplusplus
}
#endif
/*
* The va_args implementation here is not beautiful, but obviously we run into the same issues
* the GCC devs saw, and are using their solution:
*
* http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
*/
/**
* Send a mavlink emergency message.
*
* @param _pub Pointer to the uORB advert;
* @param _text The text to log;
*/
#define mavlink_log_emergency(_pub, _text, ...) mavlink_vasprintf(3, _pub, _text, ##__VA_ARGS__);
/**
* Send a mavlink critical message.
*
* @param _pub Pointer to the uORB advert;
* @param _text The text to log;
*/
#define mavlink_log_critical(_pub, _text, ...) mavlink_vasprintf(2, _pub, _text, ##__VA_ARGS__);
/**
* Send a mavlink info message.
*
* @param _pub Pointer to the uORB advert;
* @param _text The text to log;
*/
#define mavlink_log_info(_pub, _text, ...) mavlink_vasprintf(1, _pub, _text, ##__VA_ARGS__);
/**
* Send a mavlink emergency message and print to console.
*
* @param _pub Pointer to the uORB advert;
* @param _text The text to log;
*/
#define mavlink_and_console_log_emergency(_pub, _text, ...) \
do { \
mavlink_log_emergency(_pub, _text, ##__VA_ARGS__); \
PX4_ERR("telem> "); \
PX4_ERR(_text, ##__VA_ARGS__); \
} while(0);
/**
* Send a mavlink critical message and print to console.
*
* @param _pub Pointer to the uORB advert;
* @param _text The text to log;
*/
#define mavlink_and_console_log_critical(_pub, _text, ...) \
do { \
mavlink_log_critical(_pub, _text, ##__VA_ARGS__); \
PX4_WARN("telem> "); \
PX4_WARN(_text, ##__VA_ARGS__); \
} while(0);
/**
* Send a mavlink emergency message and print to console.
*
* @param _pub Pointer to the uORB advert;
* @param _text The text to log;
*/
#define mavlink_and_console_log_info(_pub, _text, ...) \
do { \
mavlink_log_info(_pub, _text, ##__VA_ARGS__); \
PX4_INFO("telem> "); \
PX4_INFO(_text, ##__VA_ARGS__); \
} while(0);
struct mavlink_logmessage {
char text[MAVLINK_LOG_MAXLEN + 1];
unsigned char severity;
};
struct mavlink_logbuffer {
unsigned int start;
unsigned int size;
int count;
struct mavlink_logmessage *elems;
};