Skip to content

Commit e70134a

Browse files
robertlong13tridge
authored andcommitted
AP_SLCANIface: prevent hardfault on arm
1 parent cf2714e commit e70134a

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

libraries/AP_CANManager/AP_SLCANIface.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ bool SLCAN::CANIface::init_passthrough(uint8_t i)
287287
*/
288288
int16_t SLCAN::CANIface::reportFrame(const AP_HAL::CANFrame& frame, uint64_t timestamp_usec)
289289
{
290-
if (_port == nullptr) {
290+
if (!is_enabled()) {
291291
return -1;
292292
}
293293
#if HAL_CANFD_SUPPORTED
@@ -377,7 +377,7 @@ int16_t SLCAN::CANIface::reportFrame(const AP_HAL::CANFrame& frame, uint64_t tim
377377
const char* SLCAN::CANIface::processCommand(char* cmd)
378378
{
379379

380-
if (_port == nullptr) {
380+
if (!is_enabled()) {
381381
return nullptr;
382382
}
383383

@@ -461,7 +461,7 @@ const char* SLCAN::CANIface::processCommand(char* cmd)
461461
// add bytes to parse the received SLCAN Data stream
462462
inline void SLCAN::CANIface::addByte(const uint8_t byte)
463463
{
464-
if (_port == nullptr) {
464+
if (!is_enabled()) {
465465
return;
466466
}
467467
if ((byte >= 32 && byte <= 126)) { // Normal printable ASCII character
@@ -497,18 +497,20 @@ void SLCAN::CANIface::update_slcan_port()
497497
{
498498
const bool armed = hal.util->get_soft_armed();
499499
if (_set_by_sermgr) {
500-
if (armed && _port != nullptr) {
500+
if (armed && is_enabled()) {
501501
// auto-disable when armed
502502
_port->lock_port(0, 0);
503-
_port = nullptr;
503+
_enabled = false;
504504
_set_by_sermgr = false;
505505
}
506506
return;
507507
}
508-
if (_port == nullptr && !armed) {
509-
_port = AP::serialmanager().find_serial(AP_SerialManager::SerialProtocol_SLCAN, 0);
510-
if (_port != nullptr) {
508+
if (!is_enabled() && !armed) {
509+
auto new_port = AP::serialmanager().find_serial(AP_SerialManager::SerialProtocol_SLCAN, 0);
510+
if (new_port != nullptr) {
511+
_port = new_port;
511512
_port->lock_port(_serial_lock_key, _serial_lock_key);
513+
_enabled = true;
512514
_set_by_sermgr = true;
513515
return;
514516
}
@@ -521,23 +523,25 @@ void SLCAN::CANIface::update_slcan_port()
521523
if (((AP_HAL::millis() - _slcan_start_req_time) < ((uint32_t)_slcan_start_delay*1000))) {
522524
return;
523525
}
524-
_port = AP::serialmanager().get_serial_by_id(_slcan_ser_port);
525-
if (_port == nullptr) {
526+
auto new_port = AP::serialmanager().get_serial_by_id(_slcan_ser_port);
527+
if (new_port == nullptr) {
526528
_slcan_ser_port.set_and_save(-1);
527529
return;
528530
}
531+
_port = new_port;
529532
_port->lock_port(_serial_lock_key, _serial_lock_key);
533+
_enabled = true;
530534
_prev_ser_port = _slcan_ser_port;
531535
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "CANManager: Starting SLCAN Passthrough on Serial %d with CAN%d", _slcan_ser_port.get(), _iface_num);
532536
_last_had_activity = AP_HAL::millis();
533537
}
534-
if (_port == nullptr) {
538+
if (!is_enabled()) {
535539
return;
536540
}
537541
if (((AP_HAL::millis() - _last_had_activity) > ((uint32_t)_slcan_timeout*1000)) &&
538542
(uint32_t)_slcan_timeout != 0) {
539543
_port->lock_port(0, 0);
540-
_port = nullptr;
544+
_enabled = false;
541545
_slcan_ser_port.set_and_save(-1);
542546
_prev_ser_port = -1;
543547
_slcan_start_req = false;
@@ -637,7 +641,7 @@ bool SLCAN::CANIface::select(bool &read, bool &write, const AP_HAL::CANFrame* co
637641
ret = _can_iface->select(read, write, pending_tx, blocking_deadline);
638642
}
639643

640-
if (_port == nullptr) {
644+
if (!is_enabled()) {
641645
return ret;
642646
}
643647

@@ -665,7 +669,7 @@ int16_t SLCAN::CANIface::send(const AP_HAL::CANFrame& frame, uint64_t tx_deadlin
665669
ret = _can_iface->send(frame, tx_deadline, flags);
666670
}
667671

668-
if (_port == nullptr) {
672+
if (!is_enabled()) {
669673
return ret;
670674
}
671675

@@ -699,7 +703,7 @@ int16_t SLCAN::CANIface::receive(AP_HAL::CANFrame& out_frame, uint64_t& rx_time,
699703
}
700704

701705
// We found nothing in HAL's CANIface receive, so look in SLCANIface
702-
if (_port == nullptr) {
706+
if (!is_enabled()) {
703707
return 0;
704708
}
705709

libraries/AP_CANManager/AP_SLCANIface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class CANIface: public AP_HAL::CANIface
6767
char buf_[SLCAN_BUFFER_SIZE + 1]; // buffer to record raw frame nibbles before parsing
6868
int16_t pos_ = 0; // position in the buffer recording nibble frames before parsing
6969
AP_HAL::UARTDriver* _port; // UART interface port reference to be used for SLCAN iface
70+
bool _enabled; // Flag to check whether we are allowed to use _port
7071

7172
ObjectBuffer<AP_HAL::CANIface::CanRxItem> rx_queue_; // Parsed Rx Frame queue
7273

@@ -137,6 +138,9 @@ class CANIface: public AP_HAL::CANIface
137138
bool add_to_rx_queue(const AP_HAL::CANIface::CanRxItem &frm) override {
138139
return rx_queue_.push(frm);
139140
}
141+
bool is_enabled() const {
142+
return (_port != nullptr) && _enabled;
143+
}
140144
};
141145

142146
}

0 commit comments

Comments
 (0)