Midnight Sun Firmware
Loading...
Searching...
No Matches
CanListener

Class that handles message listening over a SocketCAN interface. More...

#include <can_listener.h>

Public Member Functions

 CanListener ()
 Constructs a CanListener object. More...
 
 ~CanListener ()
 Destructs a CanListener object. More...
 
void listenCanBus ()
 Initiate the CAN Bus listener on a Raw SocketCAN port. More...
 
void listenCanBusProcedure ()
 Thread procedure for listening to the CAN bus. More...
 
void updateJSONProcedure ()
 Thread procedure for updating the global JSON object. More...
 

Private Member Functions

void canMessageHandler (uint32_t id, const uint8_t *data)
 Handles receiving new CAN messages. More...
 

Private Attributes

const std::string CAN_INTERFACE_NAME = "vcan0"
 
const std::string CAN_JSON_NAME = "CANListener"
 
std::unordered_map< std::string, nlohmann::json > m_canInfo
 
pthread_mutex_t m_mutex
 
pthread_t m_listenCanBusId
 
pthread_t m_updateJSONId
 
int m_rawCanSocket
 
std::atomic< bool > m_isListening
 

Static Private Attributes

static const constexpr unsigned int UPDATE_CAN_JSON_PERIOD_MS = 1000U
 

Detailed Description

Class that handles message listening over a SocketCAN interface.

This class is responsible listening to CAN messages and updating a JSON file This class shall cache the latest data and update the JSON every UPDATE_CAN_JSON_PERIOD_MS

Constructor & Destructor Documentation

◆ CanListener()

Constructs a CanListener object.

Initializes the CanListener. The constructor sets up internal variables The constructor shall intialize the mutex

◆ ~CanListener()

Destructs a CanListener object.

Initializes the CanListener. The constructor sets up internal variables The destructor shall release the mutex

Member Function Documentation

◆ canMessageHandler()

void canMessageHandler ( uint32_t  id,
const uint8_t *  data 
)
private

Handles receiving new CAN messages.

Main CAN message Handler.

Boolean flag to track the CAN bus connection status

This function is called by the listeningCanBusProcedure This function is autogenerated

This function shall dynamically allocate a struct based on the CAN ID This function shall decode the data, then update the CAN Cache 'm_canInfo' with JSON data

Parameters
idCan message ID
dataPointer to the CAN message data

◆ listenCanBus()

void listenCanBus ( )

Initiate the CAN Bus listener on a Raw SocketCAN port.

This shall start the listenCanBusProcedure and updateJSONProcedure

◆ listenCanBusProcedure()

void listenCanBusProcedure ( )

Thread procedure for listening to the CAN bus.

This thread shall store the latest CAN data in the m_canInfo unordered map

◆ updateJSONProcedure()

void updateJSONProcedure ( )

Thread procedure for updating the global JSON object.

This thread shall update the JSON at UPDATE_CAN_JSON_PERIOD_MS

Member Data Documentation

◆ CAN_INTERFACE_NAME

const std::string CAN_INTERFACE_NAME = "vcan0"
private

SocketCAN interface name

◆ CAN_JSON_NAME

const std::string CAN_JSON_NAME = "CANListener"
private

CAN JSON file name

◆ m_canInfo

std::unordered_map<std::string, nlohmann::json> m_canInfo
private

Hash-map to cache the latest CAN data

◆ m_listenCanBusId

pthread_t m_listenCanBusId
private

Thread Id for listening to the CAN bus

◆ m_mutex

pthread_mutex_t m_mutex
private

Mutex to protect m_canInfo

◆ m_rawCanSocket

int m_rawCanSocket
private

Raw SocketCAN FD

◆ m_updateJSONId

pthread_t m_updateJSONId
private

Thread Id for updating the CAN JSON

◆ UPDATE_CAN_JSON_PERIOD_MS

const constexpr unsigned int UPDATE_CAN_JSON_PERIOD_MS = 1000U
staticconstexprprivate

JSON Update period in milliseconds


The documentation for this class was generated from the following files: