2020-05-31 09:32:19 -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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "AP_HAL_SITL.h"
|
|
|
|
|
|
|
|
#if HAL_NUM_CAN_IFACES
|
|
|
|
|
|
|
|
#include <AP_HAL/CANIface.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <queue>
|
|
|
|
#include <memory>
|
|
|
|
#include <map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <poll.h>
|
2023-08-25 21:52:28 -03:00
|
|
|
#include "CAN_Transport.h"
|
2020-05-31 09:32:19 -03:00
|
|
|
|
|
|
|
namespace HALSITL {
|
|
|
|
|
|
|
|
class CANIface: public AP_HAL::CANIface {
|
|
|
|
public:
|
|
|
|
CANIface(int index)
|
|
|
|
: _self_index(index)
|
|
|
|
, _frames_in_socket_tx_queue(0)
|
2023-08-17 17:18:36 -03:00
|
|
|
{
|
|
|
|
_num_interfaces++;
|
|
|
|
}
|
|
|
|
CANIface() : CANIface(_num_interfaces) {}
|
|
|
|
static uint8_t _num_interfaces;
|
2021-03-14 22:05:07 -03:00
|
|
|
|
2020-05-31 09:32:19 -03:00
|
|
|
~CANIface() { }
|
|
|
|
|
|
|
|
// Initialise CAN Peripheral
|
2022-03-26 16:41:40 -03:00
|
|
|
bool init(const uint32_t bitrate, const uint32_t fdbitrate, const OperatingMode mode) override;
|
2020-05-31 09:32:19 -03:00
|
|
|
bool init(const uint32_t bitrate, const OperatingMode mode) override;
|
|
|
|
|
2023-08-17 17:18:36 -03:00
|
|
|
// number of enabled interfaces
|
|
|
|
static uint8_t num_interfaces(void) {
|
|
|
|
return _num_interfaces;
|
|
|
|
}
|
|
|
|
|
2020-05-31 09:32:19 -03:00
|
|
|
// Put frame into Tx FIFO returns negative on error, 0 on buffer full,
|
|
|
|
// 1 on successfully pushing a frame into FIFO
|
|
|
|
int16_t send(const AP_HAL::CANFrame& frame, uint64_t tx_deadline,
|
|
|
|
CanIOFlags flags) override;
|
|
|
|
|
|
|
|
// Receive frame from Rx Buffer, returns negative on error, 0 on nothing available,
|
|
|
|
// 1 on successfully poping a frame
|
|
|
|
int16_t receive(AP_HAL::CANFrame& out_frame, uint64_t& out_timestamp_us,
|
|
|
|
CanIOFlags& out_flags) override;
|
|
|
|
|
|
|
|
// Always return false, there's no busoff condition in virtual CAN
|
|
|
|
bool is_busoff() const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush_tx() override;
|
|
|
|
|
|
|
|
void clear_rx() override;
|
|
|
|
|
|
|
|
// Get total number of Errors discovered
|
|
|
|
uint32_t getErrorCount() const override;
|
|
|
|
|
|
|
|
// returns true if init was successfully called
|
|
|
|
bool is_initialized() const override;
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
* Select Method *
|
|
|
|
* ****************************************/
|
|
|
|
// wait until selected event is available, false when timed out waiting else true
|
|
|
|
bool select(bool &read, bool &write,
|
|
|
|
const AP_HAL::CANFrame* const pending_tx,
|
|
|
|
uint64_t blocking_deadline) override;
|
|
|
|
|
|
|
|
// setup event handle for waiting on events
|
|
|
|
bool set_event_handle(AP_HAL::EventHandle* handle) override;
|
|
|
|
|
|
|
|
// fetch stats text and return the size of the same,
|
|
|
|
// results available via @SYS/can0_stats.txt or @SYS/can1_stats.txt
|
2020-12-30 02:23:27 -04:00
|
|
|
void get_stats(ExpandingString &str) override;
|
2020-05-31 09:32:19 -03:00
|
|
|
|
2022-12-06 22:35:59 -04:00
|
|
|
/*
|
|
|
|
return statistics structure
|
|
|
|
*/
|
|
|
|
const bus_stats_t *get_statistics(void) const override {
|
|
|
|
return &stats;
|
|
|
|
}
|
|
|
|
|
2020-05-31 09:32:19 -03:00
|
|
|
class CANSocketEventSource : public AP_HAL::EventSource {
|
|
|
|
friend class CANIface;
|
|
|
|
CANIface *_ifaces[HAL_NUM_CAN_IFACES];
|
|
|
|
|
|
|
|
public:
|
|
|
|
// we just poll fd, no signaling is done
|
|
|
|
void signal(uint32_t evt_mask) override { return; }
|
2023-04-17 01:34:22 -03:00
|
|
|
bool wait(uint16_t duration_us, AP_HAL::EventHandle* evt_handle) override;
|
2020-05-31 09:32:19 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _pollWrite();
|
|
|
|
|
|
|
|
bool _pollRead();
|
|
|
|
|
|
|
|
void _confirmSentFrame();
|
|
|
|
|
2022-02-06 17:22:54 -04:00
|
|
|
bool _hasReadyTx();
|
2020-05-31 09:32:19 -03:00
|
|
|
|
2022-02-06 17:22:54 -04:00
|
|
|
bool _hasReadyRx();
|
2020-05-31 09:32:19 -03:00
|
|
|
|
|
|
|
void _poll(bool read, bool write);
|
|
|
|
|
|
|
|
int _openSocket(const std::string& iface_name);
|
|
|
|
|
|
|
|
void _updateDownStatusFromPollResult(const pollfd& pfd);
|
|
|
|
|
2023-08-25 21:52:28 -03:00
|
|
|
CAN_Transport *transport;
|
2020-05-31 09:32:19 -03:00
|
|
|
|
|
|
|
const uint8_t _self_index;
|
|
|
|
|
|
|
|
unsigned _frames_in_socket_tx_queue;
|
|
|
|
uint32_t _tx_frame_counter;
|
|
|
|
AP_HAL::EventHandle *_evt_handle;
|
|
|
|
static CANSocketEventSource evt_can_socket[HAL_NUM_CAN_IFACES];
|
|
|
|
|
|
|
|
pollfd _pollfd;
|
|
|
|
std::priority_queue<CanTxItem> _tx_queue;
|
|
|
|
std::queue<CanRxItem> _rx_queue;
|
|
|
|
|
2022-12-06 22:35:59 -04:00
|
|
|
/*
|
2023-08-25 21:52:28 -03:00
|
|
|
bus statistics
|
2022-12-06 22:35:59 -04:00
|
|
|
*/
|
2023-08-25 21:52:28 -03:00
|
|
|
AP_HAL::CANIface::bus_stats_t stats;
|
2022-02-06 17:22:54 -04:00
|
|
|
|
|
|
|
HAL_Semaphore sem;
|
|
|
|
|
|
|
|
bool add_to_rx_queue(const CanRxItem &rx_item) override {
|
|
|
|
_rx_queue.push(rx_item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t get_iface_num(void) const override {
|
|
|
|
return _self_index;
|
|
|
|
}
|
2020-05-31 09:32:19 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //#if HAL_NUM_CAN_IFACES
|