LCOV - code coverage report
Current view: top level - AP_HAL_SITL - CANSocketIface.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 12 16 75.0 %
Date: 2026-09-09 11:02:08 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /*
       2             :    This program is free software: you can redistribute it and/or modify
       3             :    it under the terms of the GNU General Public License as published by
       4             :    the Free Software Foundation, either version 3 of the License, or
       5             :    (at your option) any later version.
       6             : 
       7             :    This program is distributed in the hope that it will be useful,
       8             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
       9             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      10             :    GNU General Public License for more details.
      11             : 
      12             :    You should have received a copy of the GNU General Public License
      13             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      14             : */
      15             : 
      16             : #pragma once
      17             : 
      18             : #include "AP_HAL_SITL.h"
      19             : 
      20             : #if HAL_NUM_CAN_IFACES
      21             : 
      22             : #include <AP_HAL/CANIface.h>
      23             : #include <AP_HAL/utility/RingBuffer.h>
      24             : #include <string>
      25             : #include <memory>
      26             : #include <map>
      27             : #include <unordered_set>
      28             : #include <poll.h>
      29             : #include "CAN_Transport.h"
      30             : 
      31             : namespace HALSITL {
      32             : 
      33             : class CANIface: public AP_HAL::CANIface {
      34             : public:
      35           5 :     CANIface(int index)
      36          10 :       : _self_index(index)
      37           5 :       , _frames_in_socket_tx_queue(0)
      38             :     {
      39           5 :         _num_interfaces++;
      40           5 :     }
      41             :     CANIface() : CANIface(_num_interfaces) {}
      42             :     static uint8_t _num_interfaces;
      43             : 
      44             :     ~CANIface() { }
      45             : 
      46             :     // Initialise CAN Peripheral
      47             :     bool init(const uint32_t bitrate, const uint32_t fdbitrate) override;
      48             :     bool init(const uint32_t bitrate) override;
      49             : 
      50             :     // number of enabled interfaces
      51   187275482 :     static uint8_t num_interfaces(void) {
      52   187275482 :         return _num_interfaces;
      53             :     }
      54             : 
      55             :     // Put frame into Tx FIFO returns negative on error, 0 on buffer full, 
      56             :     // 1 on successfully pushing a frame into FIFO
      57             :     int16_t send(const AP_HAL::CANFrame& frame, uint64_t tx_deadline,
      58             :                  CanIOFlags flags) override;
      59             : 
      60             :     // Receive frame from Rx Buffer, returns negative on error, 0 on nothing available, 
      61             :     // 1 on successfully poping a frame
      62             :     int16_t receive(AP_HAL::CANFrame& out_frame, uint64_t& out_timestamp_us,
      63             :                     CanIOFlags& out_flags) override;
      64             : 
      65             :     // Always return false, there's no busoff condition in virtual CAN
      66           0 :     bool is_busoff() const override
      67             :     {
      68           0 :         return false;
      69             :     }
      70             : 
      71             :     void flush_tx() override;
      72             : 
      73             :     void clear_rx() override;
      74             : 
      75             :     // Get total number of Errors discovered
      76             :     uint32_t getErrorCount() const override;
      77             : 
      78             :     // returns true if init was successfully called
      79             :     bool is_initialized() const override;
      80             : 
      81             :     /******************************************
      82             :      * Select Method                          *
      83             :      * ****************************************/
      84             :     // wait until selected event is available, false when timed out waiting else true
      85             :     bool select(bool &read, bool &write,
      86             :                 const AP_HAL::CANFrame* const pending_tx,
      87             :                 uint64_t blocking_deadline) override;
      88             :     
      89             :     // setup event handle for waiting on events
      90             :     bool set_event_handle(AP_HAL::BinarySemaphore *handle) override;
      91             : 
      92             :     // fetch stats text and return the size of the same,
      93             :     // results available via @SYS/can0_stats.txt or @SYS/can1_stats.txt 
      94             :     void get_stats(ExpandingString &str) override;
      95             : 
      96             :     /*
      97             :       return statistics structure
      98             :      */
      99        8926 :     const bus_stats_t *get_statistics(void) const override {
     100        8926 :         return &stats;
     101             :     }
     102             :     
     103             : private:
     104             :     void _pollWrite();
     105             : 
     106             :     bool _pollRead();
     107             : 
     108             :     void _confirmSentFrame();
     109             : 
     110             :     bool _hasReadyTx();
     111             : 
     112             :     bool _hasReadyRx();
     113             : 
     114             :     void _poll(bool read, bool write);
     115             : 
     116             :     int _openSocket(const std::string& iface_name);
     117             : 
     118             :     void _updateDownStatusFromPollResult(const pollfd& pfd);
     119             : 
     120             :     CAN_Transport *transport;
     121             : 
     122             :     const uint8_t _self_index;
     123             : 
     124             :     unsigned _frames_in_socket_tx_queue;
     125             :     uint32_t _tx_frame_counter;
     126             :     AP_HAL::BinarySemaphore *sem_handle;
     127             : 
     128             :     pollfd _pollfd;
     129             :     ObjectArray<CanTxItem> _tx_queue{100};
     130             :     ObjectArray<CanRxItem> _rx_queue{100};
     131             : 
     132             :     /*
     133             :       bus statistics
     134             :      */
     135             :     AP_HAL::CANIface::bus_stats_t stats;
     136             : 
     137             :     HAL_Semaphore sem;
     138             : 
     139         152 :     bool add_to_rx_queue(const CanRxItem &rx_item) override {
     140         152 :         _rx_queue.push(rx_item);
     141         152 :         return true;
     142             :     }
     143             : 
     144           0 :     int8_t get_iface_num(void) const override {
     145           0 :         return _self_index;
     146             :     }
     147             : };
     148             : 
     149             : }
     150             : 
     151             : #endif //#if HAL_NUM_CAN_IFACES

Generated by: LCOV version 1.14