Renaming confusing member "address_" to "index_"
[apps/agl-service-can-low-level.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 #include "can-message.hpp"
28
29 namespace utils
30 {
31
32         /// @brief Construct a default, invalid, socket.
33         socketcan_t::socketcan_t()
34                 : socket_{INVALID_SOCKET}
35         {}
36
37         /// @brief Construct a socket by moving an existing one.
38         socketcan_t::socketcan_t(socketcan_t&& s)
39                 : socket_{s.socket_}
40         {
41                 s.socket_ = INVALID_SOCKET;
42         }
43
44         /// @brief Destruct the socket.
45         socketcan_t::~socketcan_t()
46         {
47                 if(socket_ != INVALID_SOCKET)
48                         ::close(socket_);
49         }
50
51         const struct sockaddr_can& socketcan_t::get_tx_address() const
52         {
53                 return tx_address_;
54         }
55
56         /// @brief Test if socket is valid.
57         /// @return true if valid, false otherwise.
58         socketcan_t::operator bool() const
59         {
60                 return socket_ != INVALID_SOCKET;
61         }
62
63         /// @brief Open the socket.
64         /// @param[in] domain Specifies the communications domain in which a socket is to be created.
65         /// @param[in] type Specifies the type of socket to be created.
66         /// @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.
67         /// @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.
68         int socketcan_t::open(int domain, int type, int protocol)
69         {
70                 close();
71                 socket_ = ::socket(domain, type, protocol);
72                 return socket_;
73         }
74
75         /// @brief Close the socket.
76         /// @return 0 if success.
77         int socketcan_t::close()
78         {
79                 return socket_ != INVALID_SOCKET ? ::close(socket_) : 0;
80         }
81
82         /// @brief Set socket option.
83         /// @return 0 if success.
84         int socketcan_t::setopt(int level, int optname, const void* optval, socklen_t optlen)
85         {
86                 return socket_ != INVALID_SOCKET ? ::setsockopt(socket_, level, optname, optval, optlen) : 0;
87         }
88
89         /// @brief Bind the socket.
90         /// @return 0 if success.
91         int socketcan_t::bind(const struct sockaddr* addr, socklen_t len)
92         {
93                 return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
94         }
95
96         /// @brief Connect the socket.
97         /// @return 0 if success.
98         int socketcan_t::connect(const struct sockaddr* addr, socklen_t len)
99         {
100                 return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0;
101         }
102
103         /// @brief Get the file descriptor.
104         /// @return The socket's file descriptor
105         int socketcan_t::socket() const
106         {
107                 return socket_;
108         }
109
110         /// @brief Open a raw socket CAN.
111         /// @param[in] device_name is the kernel network device name of the CAN interface.
112         ///
113         /// @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.
114         int socketcan_t::open(std::string device_name)
115         {
116                 close();
117                 
118                 struct ifreq ifr;
119                 socket_ = ::socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
120
121                 // Attempts to open a socket to CAN bus
122                 ::strcpy(ifr.ifr_name, device_name.c_str());
123                 DEBUG(binder_interface, "%s: ifr_name is : %s", __FUNCTION__, ifr.ifr_name);
124                 if(::ioctl(socket_, SIOCGIFINDEX, &ifr) < 0)
125                 {
126                         ERROR(binder_interface, "%s: ioctl failed. Error was : %s", __FUNCTION__, strerror(errno));
127                         close();
128                 }
129                 else
130                 {
131                         tx_address_.can_family = AF_CAN;
132                         tx_address_.can_ifindex = ifr.ifr_ifindex;
133
134                         if(connect((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
135                         {
136                                 ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno));
137                                 close();
138                         }
139                 }
140                 return socket_;
141         }
142
143         socketcan_t& operator>>(socketcan_t& s, const can_message_t& cm)
144         {
145                 struct {
146                         struct bcm_msg_head msg_head;
147                         struct canfd_frame frames;
148                 } msg;
149
150                 struct sockaddr_can addr = s.get_tx_address();
151                 socklen_t addrlen = sizeof(addr);
152                 struct ifreq ifr;
153
154                 ssize_t nbytes = ::recvfrom(s.socket(), &msg, sizeof(msg), 0, (struct sockaddr*)&addr, &addrlen);
155                 ifr.ifr_ifindex = addr.can_ifindex;
156                 ioctl(s.socket(), SIOCGIFNAME, &ifr);
157
158                 printf("Data available: %i bytes read\n", (int)nbytes);
159                 printf("read: Found on bus %s:\n id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X\n", ifr.ifr_name, msg.msg_head.can_id, msg.frames.len,
160                         msg.frames.data[0], msg.frames.data[1], msg.frames.data[2], msg.frames.data[3], msg.frames.data[4], msg.frames.data[5], msg.frames.data[6], msg.frames.data[7]);
161                 return s;
162         }
163
164         socketcan_t& operator<<(socketcan_t& s, const struct bcm_msg_head& obj)
165         {
166                 struct sockaddr_can addr = s.get_tx_address();
167                 ::sendto(s.socket(), &obj, sizeof(bcm_msg_head), 0, (struct sockaddr*)&addr, sizeof(addr));
168                 return s;
169         }
170
171         socketcan_t& operator<<(socketcan_t& s, const struct canfd_frame& obj)
172         {
173                 struct sockaddr_can addr = s.get_tx_address();
174                 ::sendto(s.socket(), &obj, sizeof(canfd_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
175                 return s;
176         }
177
178         socketcan_t& operator<<(socketcan_t& s, const struct can_frame& obj)
179         {
180                 struct sockaddr_can addr = s.get_tx_address();
181                 ::sendto(s.socket(), &obj, sizeof(can_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
182                 return s;
183         }
184
185         socketcan_t& operator<<(socketcan_t& s, const struct basic_bcm_msg<struct can_frame>& obj)
186         {
187                 s << obj.msg_head;
188                 s << obj.frames;
189                 return s;
190         }
191
192         socketcan_t& operator<<(socketcan_t& s, const struct canfd_bcm_msg& obj)
193         {
194                 s << obj.msg_head;
195                 s << obj.frames;
196                 return s;
197         }
198 }