Rename and handling write on socket using stream instead of specific method
[apps/low-level-can-service.git] / CAN-binder / low-can-binding / utils / socketcan.cpp
1 /*
2  * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loïc Collignon" <loic.collignon@iot.bzh>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <unistd.h>
19 #include <string>
20 #include <string.h>
21 #include <linux/can/raw.h>
22 #include <net/if.h>
23 #include <sys/ioctl.h>
24
25 #include "socketcan.hpp"
26 #include "low-can-binding.hpp"
27
28 namespace utils
29 {
30
31         /// @brief Construct a default, invalid, socket.
32         socketcan_t::socketcan_t()
33                 : socket_{INVALID_SOCKET}
34         {}
35
36         /// @brief Construct a socket by moving an existing one.
37         socketcan_t::socketcan_t(socketcan_t&& s)
38                 : socket_{s.socket_}
39         {
40                 s.socket_ = INVALID_SOCKET;
41         }
42
43         /// @brief Destruct the socket.
44         socketcan_t::~socketcan_t()
45         {
46                 if(socket_ != INVALID_SOCKET)
47                         ::close(socket_);
48         }
49
50         const struct sockaddr_can& socketcan_t::get_tx_address() const
51         {
52                 return tx_address_;
53         }
54
55         /// @brief Test if socket is valid.
56         /// @return true if valid, false otherwise.
57         socketcan_t::operator bool() const
58         {
59                 return socket_ != INVALID_SOCKET;
60         }
61
62         /// @brief Open the socket.
63         /// @param[in] domain Specifies the communications domain in which a socket is to be created.
64         /// @param[in] type Specifies the type of socket to be created.
65         /// @param[in] protocol Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type.
66         /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
67         int socketcan_t::open(int domain, int type, int protocol)
68         {
69                 close();
70                 socket_ = ::socket(domain, type, protocol);
71                 return socket_;
72         }
73
74         /// @brief Close the socket.
75         /// @return 0 if success.
76         int socketcan_t::close()
77         {
78                 return socket_ != INVALID_SOCKET ? ::close(socket_) : 0;
79         }
80
81         /// @brief Set socket option.
82         /// @return 0 if success.
83         int socketcan_t::setopt(int level, int optname, const void* optval, socklen_t optlen)
84         {
85                 return socket_ != INVALID_SOCKET ? ::setsockopt(socket_, level, optname, optval, optlen) : 0;
86         }
87
88         /// @brief Bind the socket.
89         /// @return 0 if success.
90         int socketcan_t::bind(const struct sockaddr* addr, socklen_t len)
91         {
92                 return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
93         }
94
95         /// @brief Connect the socket.
96         /// @return 0 if success.
97         int socketcan_t::connect(const struct sockaddr* addr, socklen_t len)
98         {
99                 return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0;
100         }
101
102         /// @brief Get the file descriptor.
103         /// @return The socket's file descriptor
104         int socketcan_t::socket() const
105         {
106                 return socket_;
107         }
108
109         /// @brief Open a raw socket CAN.
110         /// @param[in] device_name is the kernel network device name of the CAN interface.
111         ///
112         /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
113         int socketcan_t::open(std::string device_name)
114         {
115                 close();
116                 
117                 struct ifreq ifr;
118                 socket_ = ::socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
119
120                 // Attempts to open a socket to CAN bus
121                 ::strcpy(ifr.ifr_name, device_name.c_str());
122                 DEBUG(binder_interface, "%s: ifr_name is : %s", __FUNCTION__, ifr.ifr_name);
123                 if(::ioctl(socket_, SIOCGIFINDEX, &ifr) < 0)
124                 {
125                         ERROR(binder_interface, "%s: ioctl failed. Error was : %s", __FUNCTION__, strerror(errno));
126                         close();
127                 }
128                 else
129                 {
130                         tx_address_.can_family = AF_CAN;
131                         tx_address_.can_ifindex = ifr.ifr_ifindex;
132
133                         if(connect((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
134                         {
135                                 ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno));
136                                 close();
137                         }
138                 }
139                 return socket_;
140         }
141
142         socketcan_t& operator<<(socketcan_t& s, const struct bcm_msg_head& obj)
143         {
144                 struct sockaddr_can addr = s.get_tx_address();
145                 ::sendto(s.socket(), &obj, sizeof(bcm_msg_head), 0, (struct sockaddr*)&addr, sizeof(addr));
146                 return s;
147         }
148
149         socketcan_t& operator<<(socketcan_t& s, const struct canfd_frame& obj)
150         {
151                 struct sockaddr_can addr = s.get_tx_address();
152                 ::sendto(s.socket(), &obj, sizeof(canfd_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
153                 return s;
154         }
155
156         socketcan_t& operator<<(socketcan_t& s, const struct can_frame& obj)
157         {
158                 struct sockaddr_can addr = s.get_tx_address();
159                 ::sendto(s.socket(), &obj, sizeof(can_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
160                 return s;
161         }
162
163         socketcan_t& operator<<(socketcan_t& s, const struct basic_bcm_msg<struct can_frame>& obj)
164         {
165                 s << obj.msg_head;
166                 s << obj.frames;
167                 return s;
168         }
169
170         socketcan_t& operator<<(socketcan_t& s, const struct canfd_bcm_msg& obj)
171         {
172                 s << obj.msg_head;
173                 s << obj.frames;
174                 return s;
175         }
176 }