Fix: wrong include local header path
[apps/low-level-can-service.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 #include <string.h>
25
26 #include "../can/can-message.hpp"
27 #include "../low-can-binding.hpp"
28
29 #define INVALID_SOCKET -1
30
31 namespace utils
32 {
33         struct simple_bcm_msg
34         {
35                 struct bcm_msg_head msg_head;
36                 struct can_frame frames;
37         };
38
39         struct canfd_bcm_msg
40         {
41                 struct bcm_msg_head msg_head;
42                 struct canfd_frame frames;
43                 canfd_bcm_msg() { msg_head.flags |= CAN_FD_FRAME; }
44         };
45
46         class socketcan_t
47         {
48         public:
49                 socketcan_t();
50                 socketcan_t(const socketcan_t&) = delete;
51                 socketcan_t(socketcan_t&&);
52                 ~socketcan_t();
53
54                 const struct sockaddr_can& get_tx_address() const;
55                 explicit operator bool() const;
56
57                 int socket() const;
58                 int open(std::string device_name);
59                 int setopt(int level, int optname, const void* optval, socklen_t optlen);
60                 int close();
61
62         private:
63                 int socket_;
64                 struct sockaddr_can tx_address_;
65
66                 int open(int domain, int type, int protocol);
67                 int bind(const struct sockaddr* addr, socklen_t len);
68                 int connect(const struct sockaddr* addr, socklen_t len);
69         };
70
71         template <typename T>
72         socketcan_t& operator<<(socketcan_t& s, const std::vector<T>& vobj)
73         {
74                 for(const auto& obj : vobj)
75                         s << obj;
76                 return s;
77         }
78
79         template <typename T>
80         socketcan_t& operator<<(socketcan_t& s, const T& obj)
81         {
82                 if (::sendto(s.socket(), &obj, sizeof(obj), 0, (const struct sockaddr*)&s.get_tx_address(), sizeof(s.get_tx_address())) < 0)
83                         ERROR(binder_interface, "%s: Error sending : %i %s", __FUNCTION__, errno, ::strerror(errno));
84                 return s;
85         }
86         
87         socketcan_t& operator<<(socketcan_t& s, const struct simple_bcm_msg& obj);
88         socketcan_t& operator<<(socketcan_t& s, const struct canfd_bcm_msg& obj);
89
90         socketcan_t& operator>>(socketcan_t& s, can_message_t& cm);
91 }