2 * Copyright (C) 2018, 2019 "IoT.bzh"
3 * Author "Arthur Guyader" <arthur.guyader@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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/ioctl.h>
24 #include "./socketcan-j1939.hpp"
25 #include "socketcan-j1939-addressclaiming.hpp"
29 std::mutex socketcan_j1939_t::mutex_claiming_;
30 std::condition_variable socketcan_j1939_t::signal_address_claiming_;
33 * @brief Apply a filter to the socket
35 * @param name - The name you want to receive
36 * @param pgn - The pgn you want to receive
37 * @param addr - The addr you want to receive
38 * @param name_mask - The mask to apply to the name (No mask : J1939_NO_NAME)
39 * @param pgn_mask - The mask to apply to the pgn (No mask : J1939_NO_PGN)
40 * @param addr_mask - The mask to apply to the addr (No mask : J1939_NO_ADDR)
42 void socketcan_j1939_t::add_filter(name_t name, pgn_t pgn, uint8_t addr, name_t name_mask, pgn_t pgn_mask, uint8_t addr_mask)
44 // AFB_DEBUG("[socketcan_j1939_t][add_filter] PGN : %" PRIu32 " ; NAME : %" PRIu64 " ; ADDR : %" PRIu8,pgn,(long unsigned int)name,addr);
45 // AFB_DEBUG("PGN_MASK : %" PRIu32 " ; NAME_MASK : %" PRIu64 "; ADDR_MASK : %" PRIu8,pgn_mask,(long unsigned int)name_mask,addr_mask);
47 struct j1939_filter filter;
48 memset(&filter, 0, sizeof(filter));
52 if(name_mask != J1939_NO_NAME)
54 filter.name_mask = name_mask;
57 filter.name_mask = ~0ULL;
65 if(addr_mask != J1939_NO_ADDR)
67 filter.addr_mask = addr_mask;
70 filter.addr_mask = ~0;
74 if (pgn <= J1939_PGN_MAX)
77 if(pgn_mask != J1939_NO_PGN)
79 filter.pgn_mask = pgn_mask;
88 setopt(SOL_CAN_J1939, SO_J1939_FILTER, &filter, sizeof(filter));
93 * @brief Define some parameters on the socket
95 * @param promisc - Allows to accept all packets that the socket receives even if they are not addressed directly to it
96 * @param recv_own_msgs - Allows you to receive your own packets
97 * @param broadcast - Allows to write message with address brodcast (255)
99 void socketcan_j1939_t::define_opt(bool promisc, bool recv_own_msgs, bool broadcast)
102 int recv_own_msgs_i = 0;
105 if(promisc) promisc_i = 1;
106 if(recv_own_msgs) recv_own_msgs_i=1;
107 if(broadcast) broadcast_i = 1;
109 setopt(SOL_CAN_J1939, SO_J1939_PROMISC, &promisc_i, sizeof(promisc_i));
110 setopt(SOL_CAN_J1939, SO_J1939_RECV_OWN, &recv_own_msgs_i, sizeof(recv_own_msgs_i));
111 setopt(SOL_SOCKET, SO_BROADCAST, &broadcast_i, sizeof(broadcast_i));
116 * @brief Define the tx address for the bind function
118 * @param device_name - The device can that you want to bind
119 * @param name - The name that you want to bind
120 * @param pgn - The pgn that you want to bind
121 * @param addr - The addr that you want to bindthat you want to bind
123 void socketcan_j1939_t::define_tx_address(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
126 ::strcpy(ifr_.ifr_name, device_name.c_str());
127 AFB_DEBUG("ifr_name is : %s", ifr_.ifr_name);
130 if(::ioctl(socket_, SIOCGIFINDEX, &ifr_) < 0)
132 AFB_ERROR("ioctl failed. Error was : %s", strerror(errno));
137 tx_address_.can_ifindex = ifr_.ifr_ifindex;
140 tx_address_.can_family = AF_CAN;
143 if(addr <= 0 || addr >= UINT8_MAX )
145 tx_address_.can_addr.j1939.addr = J1939_NO_ADDR;
149 tx_address_.can_addr.j1939.addr = addr;
152 if(name <= 0 || name >= UINT64_MAX )
154 tx_address_.can_addr.j1939.name = J1939_NO_NAME;
158 tx_address_.can_addr.j1939.name = name;
161 if(pgn <= 0 || pgn > J1939_PGN_MAX)
163 tx_address_.can_addr.j1939.pgn = J1939_NO_PGN;
167 tx_address_.can_addr.j1939.pgn = pgn;
174 * @brief Open default socket
176 * @param device_name The device name to open the socket
177 * @return int The number of the socket
179 int socketcan_j1939_t::open(std::string device_name)
181 return open(device_name,0,0,0);
185 * @brief Open a socket with name pgn and address
187 * @param device_name The device name to open the socket
188 * @param name - The name that you want to bind
189 * @param pgn - The pgn that you want to bind
190 * @param addr - The addr that you want to bindthat you want to bind
191 * @return int The number of the socket
193 int socketcan_j1939_t::open(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
196 socket_ = socketcan_t::open(PF_CAN, SOCK_DGRAM, CAN_J1939);
198 define_tx_address(device_name,name,pgn,addr);
200 if(bind((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
202 AFB_ERROR("Bind failed. %s", strerror(errno));
210 * @brief Launch recvfrom on the socket with blocking flag
212 * @return std::shared_ptr<message_t> The j1939 message that is read
214 std::shared_ptr<message_t> socketcan_j1939_t::read_message()
216 return read_message(0);
220 * @brief Launch recvfrom on the socket with the flag you want
222 * @param flag The flag param for the recvfrom
223 * @return std::shared_ptr<message_t> The j1939 message that is read
225 std::shared_ptr<message_t> socketcan_j1939_t::read_message(int flag)
227 socklen_t peernamelen;
228 std::shared_ptr<j1939_message_t> jm = std::make_shared<j1939_message_t>();
229 uint8_t data[128] = {0};
231 struct sockaddr_can peername;
232 peernamelen = sizeof(peername);
233 ssize_t nbytes = recvfrom(socket_, &data, sizeof(data), flag, (struct sockaddr *)&peername, &peernamelen);
239 //AFB_DEBUG("Data available: %i bytes read", (int)nbytes);
242 ioctl(socket(), SIOCGSTAMP, &tv);
243 uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
244 jm = j1939_message_t::convert_from_addr(peername, data , nbytes, timestamp);
245 jm->set_sub_id((int)socket());
250 * @brief Write a j1939 message
252 * @param pgn The pgn that you want to emmit
253 * @param data The data that you want to emmit
254 * @param len_data The size of the data to emmit
255 * @return int 0 if the message is correctly send
257 int socketcan_j1939_t::write_j1939_message(pgn_t pgn, std::vector<uint8_t> &data, uint32_t len_data)
259 j1939_message_t msg = j1939_message_t(len_data, data, 0, 0, pgn, 0);
260 msg.set_sockname(pgn,J1939_NO_NAME,J1939_NO_ADDR);
261 return write_message(msg);
265 * @brief Write a j1939 message
267 * @param m A j1939 message
268 * @return int 0 if the message is correctly send
270 int socketcan_j1939_t::write_message(message_t& m)
272 j1939_message_t& jm = reinterpret_cast<j1939_message_t&>(m);
273 sockaddr_can sockname = jm.get_sockname();
274 uint8_t data[jm.get_data_vector().size()];
276 for(int i=0; i<jm.get_data_vector().size(); i++)
278 data[i] = jm.get_data_vector()[i];
281 if (sendto(socket_, &data, sizeof(data), 0, (const struct sockaddr *)&sockname, sizeof(sockname)) < 0)
283 AFB_ERROR("Error sending : %i %s", errno, ::strerror(errno));