2014-04-03 23:55:18 -03:00
|
|
|
/*
|
|
|
|
MAVLink SERIAL_CONTROL handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "GCS.h"
|
|
|
|
#include <DataFlash/DataFlash.h>
|
2014-04-03 23:55:18 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
handle a SERIAL_CONTROL message
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::handle_serial_control(mavlink_message_t *msg, AP_GPS &gps)
|
|
|
|
{
|
|
|
|
mavlink_serial_control_t packet;
|
|
|
|
mavlink_msg_serial_control_decode(msg, &packet);
|
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
AP_HAL::UARTDriver *port = nullptr;
|
|
|
|
AP_HAL::Stream *stream = nullptr;
|
2014-04-03 23:55:18 -03:00
|
|
|
|
|
|
|
if (packet.flags & SERIAL_CONTROL_FLAG_REPLY) {
|
|
|
|
// how did this packet get to us?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool exclusive = (packet.flags & SERIAL_CONTROL_FLAG_EXCLUSIVE) != 0;
|
|
|
|
|
|
|
|
switch (packet.device) {
|
|
|
|
case SERIAL_CONTROL_DEV_TELEM1:
|
2015-06-17 04:05:48 -03:00
|
|
|
stream = port = hal.uartC;
|
2014-04-03 23:55:18 -03:00
|
|
|
lock_channel(MAVLINK_COMM_1, exclusive);
|
|
|
|
break;
|
|
|
|
case SERIAL_CONTROL_DEV_TELEM2:
|
2015-06-17 04:05:48 -03:00
|
|
|
stream = port = hal.uartD;
|
2014-04-03 23:55:18 -03:00
|
|
|
lock_channel(MAVLINK_COMM_2, exclusive);
|
|
|
|
break;
|
|
|
|
case SERIAL_CONTROL_DEV_GPS1:
|
2015-06-17 04:05:48 -03:00
|
|
|
stream = port = hal.uartB;
|
2014-04-03 23:55:18 -03:00
|
|
|
gps.lock_port(0, exclusive);
|
|
|
|
break;
|
|
|
|
case SERIAL_CONTROL_DEV_GPS2:
|
2015-06-17 04:05:48 -03:00
|
|
|
stream = port = hal.uartE;
|
2014-04-03 23:55:18 -03:00
|
|
|
gps.lock_port(1, exclusive);
|
|
|
|
break;
|
2015-06-17 04:05:48 -03:00
|
|
|
case SERIAL_CONTROL_DEV_SHELL:
|
|
|
|
stream = hal.util->get_shell_stream();
|
|
|
|
break;
|
2014-04-03 23:55:18 -03:00
|
|
|
default:
|
|
|
|
// not supported yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (exclusive && port != nullptr) {
|
2014-04-03 23:55:18 -03:00
|
|
|
// force flow control off for exclusive access. This protocol
|
|
|
|
// is used to talk to bootloaders which may not have flow
|
|
|
|
// control support
|
|
|
|
port->set_flow_control(AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// optionally change the baudrate
|
2016-10-30 02:24:21 -03:00
|
|
|
if (packet.baudrate != 0 && port != nullptr) {
|
2014-04-03 23:55:18 -03:00
|
|
|
port->begin(packet.baudrate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the data
|
|
|
|
if (packet.count != 0) {
|
|
|
|
if ((packet.flags & SERIAL_CONTROL_FLAG_BLOCKING) == 0) {
|
2015-06-17 04:05:48 -03:00
|
|
|
stream->write(packet.data, packet.count);
|
2014-04-03 23:55:18 -03:00
|
|
|
} else {
|
|
|
|
const uint8_t *data = &packet.data[0];
|
|
|
|
uint8_t count = packet.count;
|
|
|
|
while (count > 0) {
|
2015-06-17 04:05:48 -03:00
|
|
|
while (stream->txspace() <= 0) {
|
2014-04-03 23:55:18 -03:00
|
|
|
hal.scheduler->delay(5);
|
|
|
|
}
|
2015-06-17 04:05:48 -03:00
|
|
|
uint16_t n = stream->txspace();
|
2014-04-03 23:55:18 -03:00
|
|
|
if (n > packet.count) {
|
|
|
|
n = packet.count;
|
|
|
|
}
|
2015-06-17 04:05:48 -03:00
|
|
|
stream->write(data, n);
|
2014-04-03 23:55:18 -03:00
|
|
|
data += n;
|
|
|
|
count -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((packet.flags & SERIAL_CONTROL_FLAG_RESPOND) == 0) {
|
|
|
|
// no response expected
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t flags = packet.flags;
|
|
|
|
|
|
|
|
more_data:
|
|
|
|
// sleep for the timeout
|
|
|
|
while (packet.timeout != 0 &&
|
2015-06-17 04:05:48 -03:00
|
|
|
stream->available() < (int16_t)sizeof(packet.data)) {
|
2014-04-03 23:55:18 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
packet.timeout--;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.flags = SERIAL_CONTROL_FLAG_REPLY;
|
|
|
|
|
|
|
|
// work out how many bytes are available
|
2015-06-17 04:05:48 -03:00
|
|
|
int16_t available = stream->available();
|
2014-04-03 23:55:18 -03:00
|
|
|
if (available < 0) {
|
|
|
|
available = 0;
|
|
|
|
}
|
|
|
|
if (available > (int16_t)sizeof(packet.data)) {
|
|
|
|
available = sizeof(packet.data);
|
|
|
|
}
|
2015-06-17 04:05:48 -03:00
|
|
|
if (available == 0 && (flags & SERIAL_CONTROL_FLAG_BLOCKING) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet.flags & SERIAL_CONTROL_FLAG_BLOCKING) {
|
2016-04-05 01:09:47 -03:00
|
|
|
while (!HAVE_PAYLOAD_SPACE(chan, SERIAL_CONTROL)) {
|
2015-06-17 04:05:48 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-05 01:09:47 -03:00
|
|
|
if (!HAVE_PAYLOAD_SPACE(chan, SERIAL_CONTROL)) {
|
2015-06-17 04:05:48 -03:00
|
|
|
// no space for reply
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 23:55:18 -03:00
|
|
|
|
|
|
|
// read any reply data
|
|
|
|
packet.count = 0;
|
2015-03-06 22:54:58 -04:00
|
|
|
memset(packet.data, 0, sizeof(packet.data));
|
2014-04-03 23:55:18 -03:00
|
|
|
while (available > 0) {
|
2015-06-17 04:05:48 -03:00
|
|
|
packet.data[packet.count++] = (uint8_t)stream->read();
|
2014-04-03 23:55:18 -03:00
|
|
|
available--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and send the reply
|
|
|
|
_mav_finalize_message_chan_send(chan,
|
2016-04-04 00:07:27 -03:00
|
|
|
MAVLINK_MSG_ID_SERIAL_CONTROL,
|
2014-04-03 23:55:18 -03:00
|
|
|
(const char *)&packet,
|
2016-04-06 06:30:03 -03:00
|
|
|
MAVLINK_MSG_ID_SERIAL_CONTROL_MIN_LEN,
|
2014-04-03 23:55:18 -03:00
|
|
|
MAVLINK_MSG_ID_SERIAL_CONTROL_LEN,
|
|
|
|
MAVLINK_MSG_ID_SERIAL_CONTROL_CRC);
|
|
|
|
if ((flags & SERIAL_CONTROL_FLAG_MULTI) && packet.count != 0) {
|
2015-06-17 04:05:48 -03:00
|
|
|
if (flags & SERIAL_CONTROL_FLAG_BLOCKING) {
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
2014-04-03 23:55:18 -03:00
|
|
|
goto more_data;
|
|
|
|
}
|
|
|
|
}
|