Add gitlab issue/merge request templates
[apps/agl-service-can-low-level.git] / low-can-binding / utils / socketcan.cpp
1 /*
2  * Copyright (C) 2015, 2016 , 2017, 2018, 2019 "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 <linux/can/raw.h>
21 #include <net/if.h>
22 #include <sys/ioctl.h>
23
24 #include "socketcan.hpp"
25
26 namespace utils
27 {
28
29         /// @brief Construct a default, invalid, socket.
30         socketcan_t::socketcan_t()
31                 : socket_{INVALID_SOCKET}
32         {
33                 ::memset(&tx_address_, 0, sizeof(tx_address_));
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                 ::memset(&tx_address_, 0, sizeof(tx_address_));
41         }
42
43         socketcan_t& socketcan_t::operator=(const socketcan_t& s)
44         {
45                 socket_ = std::move(s.socket_);
46                 return *this;
47         }
48
49         socketcan_t::~socketcan_t()
50         {
51                 close();
52                 socket_ = INVALID_SOCKET;
53         }
54
55         const struct sockaddr_can& socketcan_t::get_tx_address() const
56         {
57                 return tx_address_;
58         }
59
60         /// @brief Test if socket is valid.
61         /// @return true if valid, false otherwise.
62         socketcan_t::operator bool() const
63         {
64                 return socket_ != INVALID_SOCKET;
65         }
66
67         /// @brief Open the socket.
68         /// @param[in] domain Specifies the communications domain in which a socket is to be created.
69         /// @param[in] type Specifies the type of socket to be created.
70         /// @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.
71         /// @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.
72         int socketcan_t::open(int domain, int type, int protocol)
73         {
74                 close();
75                 socket_ = ::socket(domain, type, protocol);
76                 if (socket_ < 0)
77                         AFB_ERROR("Open failed. %s", strerror(errno));
78
79                 return socket_;
80         }
81
82         /// @brief Close the socket.
83         /// @return 0 if success.
84         int socketcan_t::close()
85         {
86                 return socket_ != INVALID_SOCKET ? ::close(socket_) : 0;
87         }
88
89         /// @brief Set socket option.
90         /// @return 0 if success.
91         int socketcan_t::setopt(int level, int optname, const void* optval, socklen_t optlen)
92         {
93                 return socket_ != INVALID_SOCKET ? ::setsockopt(socket_, level, optname, optval, optlen) : 0;
94         }
95
96         /// @brief Get the file descriptor.
97         /// @return The socket's file descriptor
98         int socketcan_t::socket() const
99         {
100                 return socket_;
101         }
102
103         /// @brief Connect the socket.
104         /// @return 0 if success.
105         int socketcan_t::connect(const struct sockaddr* addr, socklen_t len)
106         {
107                 return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0;
108         }
109
110         /// @brief Bind the socket.
111         /// @return 0 if success.
112         int socketcan_t::bind(const struct sockaddr* addr, socklen_t len)
113         {
114                 return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
115         }
116
117         int socketcan_t::write_message(std::vector<message_t>& vobj)
118         {
119                 for(int i=0;i<vobj.size();i++)
120                 {
121                         if(write_message(vobj[i])<0)
122                         {
123                                 AFB_ERROR("Error send message %d", i);
124                                 return -1;
125                         }
126                 }
127                 return 0;
128         }
129
130 }