c7cd5c91631403682227dcdf1954d7ffe285d90c
[apps/agl-service-can-low-level.git] / CAN-binder / low-can-binding / utils / socketcan.hpp
1 #pragma once
2
3 /*
4  * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
5  * Author "Romain Forlot" <romain.forlot@iot.bzh>
6  * Author "Loïc Collignon" <loic.collignon@iot.bzh>
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <vector>
21
22 #include <sys/socket.h>
23 #include <linux/can/bcm.h>
24
25 #include "../can/can-message.hpp"
26
27 #define INVALID_SOCKET -1
28
29 namespace utils
30 {
31
32         template <typename T>
33         struct basic_bcm_msg
34         {
35                 bcm_msg_head msg_head;
36                 std::vector<T> frames;
37         };
38
39         struct canfd_bcm_msg : public basic_bcm_msg<canfd_frame>
40         {
41         canfd_bcm_msg() { msg_head.flags |= CAN_FD_FRAME; }
42         };
43
44         class socketcan_t
45         {
46         public:
47                 socketcan_t();
48                 socketcan_t(const socketcan_t&) = delete;
49                 socketcan_t(socketcan_t&&);
50                 ~socketcan_t();
51
52                 const struct sockaddr_can& get_tx_address() const;
53                 explicit operator bool() const;
54
55                 int socket() const;
56                 int open(std::string device_name);
57                 int setopt(int level, int optname, const void* optval, socklen_t optlen);
58                 int close();
59
60         private:
61                 int socket_;
62                 struct sockaddr_can tx_address_;
63
64                 int open(int domain, int type, int protocol);
65                 int bind(const struct sockaddr* addr, socklen_t len);
66                 int connect(const struct sockaddr* addr, socklen_t len);
67         };
68
69         template <typename T>
70         socketcan_t& operator<<(socketcan_t& s, const std::vector<T>& vobj)
71         {
72                 for(const auto& obj : vobj)
73                         s << obj;
74                 return s;
75         }
76         socketcan_t& operator<<(socketcan_t& s, const canfd_frame& frame);
77         socketcan_t& operator<<(socketcan_t& s, const can_frame& frame);
78         socketcan_t& operator<<(socketcan_t& s, const struct basic_bcm_msg<can_frame>& obj);
79         socketcan_t& operator<<(socketcan_t& s, const struct canfd_bcm_msg& obj);
80         socketcan_t& operator<<(socketcan_t& s, const struct bcm_msg_head& obj);
81
82         socketcan_t& operator>>(socketcan_t& s, const can_message_t& cm);
83 }