2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-08-08 23:06:21 -03:00
|
|
|
|
|
|
|
/// @file GCS_MAVLink.cpp
|
|
|
|
|
|
|
|
/*
|
|
|
|
This provides some support code and variables for MAVLink enabled sketches
|
|
|
|
|
|
|
|
*/
|
2015-12-23 12:34:55 -04:00
|
|
|
#include "GCS.h"
|
|
|
|
#include "GCS_MAVLink.h"
|
2012-08-08 23:06:21 -03:00
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_GPS/AP_GPS.h>
|
2015-12-23 12:34:55 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-10-26 09:01:52 -03:00
|
|
|
|
2012-08-08 23:06:21 -03:00
|
|
|
|
2012-08-30 23:09:52 -03:00
|
|
|
#ifdef MAVLINK_SEPARATE_HELPERS
|
2016-01-11 18:22:05 -04:00
|
|
|
#include "include/mavlink/v2.0/mavlink_helpers.h"
|
|
|
|
#endif
|
2012-08-30 23:09:52 -03:00
|
|
|
|
2015-05-15 02:40:23 -03:00
|
|
|
AP_HAL::UARTDriver *mavlink_comm_port[MAVLINK_COMM_NUM_BUFFERS];
|
2012-08-08 23:06:21 -03:00
|
|
|
|
2014-11-17 20:20:28 -04:00
|
|
|
mavlink_system_t mavlink_system = {7,1};
|
2012-08-08 23:06:21 -03:00
|
|
|
|
2014-04-03 23:55:18 -03:00
|
|
|
// mask of serial ports disabled to allow for SERIAL_CONTROL
|
|
|
|
static uint8_t mavlink_locked_mask;
|
|
|
|
|
2014-12-09 23:02:08 -04:00
|
|
|
// routing table
|
|
|
|
MAVLink_routing GCS_MAVLINK::routing;
|
|
|
|
|
2016-02-21 20:05:49 -04:00
|
|
|
// static dataflash pointer to support logging text messages
|
|
|
|
DataFlash_Class *GCS_MAVLINK::dataflash_p;
|
|
|
|
|
2016-10-26 19:06:54 -03:00
|
|
|
// static frsky_telem pointer to support queueing text messages
|
|
|
|
AP_Frsky_Telem *GCS_MAVLINK::frsky_telemetry_p;
|
|
|
|
|
2016-05-16 01:05:09 -03:00
|
|
|
// static AP_SerialManager pointer
|
|
|
|
const AP_SerialManager *GCS_MAVLINK::serialmanager_p;
|
|
|
|
|
2014-12-09 23:31:31 -04:00
|
|
|
// snoop function for vehicle types that want to see messages for
|
|
|
|
// other targets
|
2016-10-30 02:24:21 -03:00
|
|
|
void (*GCS_MAVLINK::msg_snoop)(const mavlink_message_t* msg) = nullptr;
|
2014-12-09 23:31:31 -04:00
|
|
|
|
2014-04-03 23:55:18 -03:00
|
|
|
/*
|
|
|
|
lock a channel, preventing use by MAVLink
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::lock_channel(mavlink_channel_t _chan, bool lock)
|
|
|
|
{
|
2016-03-28 11:33:59 -03:00
|
|
|
if (!valid_channel(chan)) {
|
2014-04-03 23:55:18 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lock) {
|
|
|
|
mavlink_locked_mask |= (1U<<(unsigned)_chan);
|
|
|
|
} else {
|
|
|
|
mavlink_locked_mask &= ~(1U<<(unsigned)_chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-08 23:22:46 -03:00
|
|
|
// return a MAVLink variable type given a AP_Param type
|
|
|
|
uint8_t mav_var_type(enum ap_var_type t)
|
|
|
|
{
|
|
|
|
if (t == AP_PARAM_INT8) {
|
|
|
|
return MAVLINK_TYPE_INT8_T;
|
|
|
|
}
|
|
|
|
if (t == AP_PARAM_INT16) {
|
|
|
|
return MAVLINK_TYPE_INT16_T;
|
|
|
|
}
|
|
|
|
if (t == AP_PARAM_INT32) {
|
|
|
|
return MAVLINK_TYPE_INT32_T;
|
|
|
|
}
|
|
|
|
// treat any others as float
|
|
|
|
return MAVLINK_TYPE_FLOAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 23:19:15 -03:00
|
|
|
/// Read a byte from the nominated MAVLink channel
|
|
|
|
///
|
|
|
|
/// @param chan Channel to receive on
|
|
|
|
/// @returns Byte read
|
|
|
|
///
|
|
|
|
uint8_t comm_receive_ch(mavlink_channel_t chan)
|
|
|
|
{
|
2016-03-28 11:33:59 -03:00
|
|
|
if (!valid_channel(chan)) {
|
2015-05-15 02:40:23 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (uint8_t)mavlink_comm_port[chan]->read();
|
2014-04-03 23:19:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check for available transmit space on the nominated MAVLink channel
|
|
|
|
///
|
|
|
|
/// @param chan Channel to check
|
|
|
|
/// @returns Number of bytes available
|
|
|
|
uint16_t comm_get_txspace(mavlink_channel_t chan)
|
|
|
|
{
|
2016-03-28 11:33:59 -03:00
|
|
|
if (!valid_channel(chan)) {
|
2015-05-15 02:40:23 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-03 23:55:18 -03:00
|
|
|
if ((1U<<chan) & mavlink_locked_mask) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-15 02:40:23 -03:00
|
|
|
int16_t ret = mavlink_comm_port[chan]->txspace();
|
2014-04-03 23:19:15 -03:00
|
|
|
if (ret < 0) {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return (uint16_t)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check for available data on the nominated MAVLink channel
|
|
|
|
///
|
|
|
|
/// @param chan Channel to check
|
|
|
|
/// @returns Number of bytes available
|
|
|
|
uint16_t comm_get_available(mavlink_channel_t chan)
|
|
|
|
{
|
2016-03-28 11:33:59 -03:00
|
|
|
if (!valid_channel(chan)) {
|
2015-05-15 02:40:23 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-03 23:55:18 -03:00
|
|
|
if ((1U<<chan) & mavlink_locked_mask) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-15 02:40:23 -03:00
|
|
|
int16_t bytes = mavlink_comm_port[chan]->available();
|
2014-04-03 23:19:15 -03:00
|
|
|
if (bytes == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (uint16_t)bytes;
|
|
|
|
}
|
|
|
|
|
2013-01-08 00:31:01 -04:00
|
|
|
/*
|
|
|
|
send a buffer out a MAVLink channel
|
|
|
|
*/
|
|
|
|
void comm_send_buffer(mavlink_channel_t chan, const uint8_t *buf, uint8_t len)
|
|
|
|
{
|
2016-03-28 11:33:59 -03:00
|
|
|
if (!valid_channel(chan)) {
|
2015-05-15 02:40:23 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mavlink_comm_port[chan]->write(buf, len);
|
2013-01-08 00:31:01 -04:00
|
|
|
}
|
|
|
|
|
2013-03-21 07:55:12 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
/*
|
|
|
|
return true if the MAVLink parser is idle, so there is no partly parsed
|
|
|
|
MAVLink message being processed
|
|
|
|
*/
|
|
|
|
bool comm_is_idle(mavlink_channel_t chan)
|
|
|
|
{
|
|
|
|
mavlink_status_t *status = mavlink_get_channel_status(chan);
|
2016-10-30 02:24:21 -03:00
|
|
|
return status == nullptr || status->parse_state <= MAVLINK_PARSE_STATE_IDLE;
|
2013-03-21 07:55:12 -03:00
|
|
|
}
|