socketcan.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2021, Qiayuan Liao
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *******************************************************************************/
33 
34 //
35 // Created by qiayuan on 3/3/21.
36 //
37 
38 #pragma once
39 
40 #include <linux/can.h>
41 #include <net/if.h>
42 // Multi-threading
43 #include <pthread.h>
44 #include <boost/function.hpp>
45 
46 namespace can
47 {
48 class SocketCAN
49 {
50 private:
51  ifreq interface_request_{};
52  sockaddr_can address_{};
53  pthread_t receiver_thread_id_{};
54 
55 public:
59  int sock_fd_ = -1;
63  bool terminate_receiver_thread_ = false;
64  bool receiver_thread_running_ = false;
65 
66  SocketCAN() = default;
67  ~SocketCAN();
68 
76  bool open(const std::string& interface, boost::function<void(const can_frame& frame)> handler, int thread_priority);
80  void close();
85  bool isOpen() const;
90  void write(can_frame* frame) const;
94  bool startReceiverThread(int thread_priority);
99  boost::function<void(const can_frame& frame)> reception_handler;
100 };
101 
102 } // namespace can
can::SocketCAN::open
bool open(const std::string &interface, boost::function< void(const can_frame &frame)> handler, int thread_priority)
Open and bind socket.
Definition: socketcan.cpp:88
can::SocketCAN::isOpen
bool isOpen() const
Returns whether the socket is open or closed.
Definition: socketcan.cpp:137
can::SocketCAN::receiver_thread_running_
bool receiver_thread_running_
Definition: socketcan.h:126
can::SocketCAN::~SocketCAN
~SocketCAN()
Definition: socketcan.cpp:82
can::SocketCAN::interface_request_
ifreq interface_request_
Definition: socketcan.h:113
can::SocketCAN::SocketCAN
SocketCAN()=default
can::SocketCAN::startReceiverThread
bool startReceiverThread(int thread_priority)
Starts a new thread, that will wait for socket events.
Definition: socketcan.cpp:193
can::SocketCAN::sock_fd_
int sock_fd_
Definition: socketcan.h:121
can::SocketCAN::reception_handler
boost::function< void(const can_frame &frame)> reception_handler
Definition: socketcan.h:161
can
Definition: socketcan.h:46
can::SocketCAN::close
void close()
Close and unbind socket.
Definition: socketcan.cpp:124
can::SocketCAN
Definition: socketcan.h:79
can::SocketCAN::write
void write(can_frame *frame) const
Sends the referenced frame to the bus.
Definition: socketcan.cpp:142
can::SocketCAN::terminate_receiver_thread_
bool terminate_receiver_thread_
Definition: socketcan.h:125
can::SocketCAN::address_
sockaddr_can address_
Definition: socketcan.h:114
can::SocketCAN::receiver_thread_id_
pthread_t receiver_thread_id_
Definition: socketcan.h:115


rm_hw
Author(s): Qiayuan Liao
autogenerated on Tue May 6 2025 02:23:44