blob: 6452d9b8d13ef6597464f3d00bc04528f29ed0c3 [file] [log] [blame]
Tomasz Wasilczyk87329672019-07-12 11:43:00 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <libnetdevice/libnetdevice.h>
18
19#include "NetlinkRequest.h"
20#include "NetlinkSocket.h"
21#include "common.h"
22
23#include <android-base/logging.h>
24#include <android-base/unique_fd.h>
25
26#include <linux/can.h>
chrisweirbee3d2c2019-11-19 10:23:37 -080027#include <linux/can/error.h>
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070028#include <linux/can/netlink.h>
chrisweirbee3d2c2019-11-19 10:23:37 -080029#include <linux/can/raw.h>
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070030
31namespace android {
32namespace netdevice {
33namespace can {
34
chrisweirbee3d2c2019-11-19 10:23:37 -080035static constexpr can_err_mask_t kErrMask = CAN_ERR_MASK;
36
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070037base::unique_fd socket(const std::string& ifname) {
38 struct sockaddr_can addr = {};
39 addr.can_family = AF_CAN;
40 addr.can_ifindex = nametoindex(ifname);
41 if (addr.can_ifindex == 0) {
42 LOG(ERROR) << "Interface " << ifname << " doesn't exists";
43 return {};
44 }
45
46 base::unique_fd sock(::socket(PF_CAN, SOCK_RAW, CAN_RAW));
47 if (!sock.ok()) {
48 LOG(ERROR) << "Failed to create CAN socket";
49 return {};
50 }
51
chrisweirbee3d2c2019-11-19 10:23:37 -080052 if (setsockopt(sock.get(), SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &kErrMask, sizeof(kErrMask)) < 0) {
53 LOG(ERROR) << "Can't receive error frames, CAN setsockpt failed: " << strerror(errno);
54 return {};
55 }
56
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070057 if (0 != fcntl(sock.get(), F_SETFL, O_RDWR | O_NONBLOCK)) {
58 LOG(ERROR) << "Couldn't put CAN socket in non-blocking mode";
59 return {};
60 }
61
62 if (0 != bind(sock.get(), reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) {
63 LOG(ERROR) << "Can't bind to CAN interface " << ifname;
64 return {};
65 }
66
67 return sock;
68}
69
70bool setBitrate(std::string ifname, uint32_t bitrate) {
71 struct can_bittiming bt = {};
72 bt.bitrate = bitrate;
73
74 NetlinkRequest<struct ifinfomsg> req(RTM_NEWLINK, NLM_F_REQUEST);
75
76 const auto ifidx = nametoindex(ifname);
77 if (ifidx == 0) {
78 LOG(ERROR) << "Can't find interface " << ifname;
79 return false;
80 }
81 req.data().ifi_index = ifidx;
82
83 {
84 auto linkinfo = req.nest(IFLA_LINKINFO);
85 req.addattr(IFLA_INFO_KIND, "can");
86 {
87 auto infodata = req.nest(IFLA_INFO_DATA);
88 /* For CAN FD, it would require to add IFLA_CAN_DATA_BITTIMING
89 * and IFLA_CAN_CTRLMODE as well. */
90 req.addattr(IFLA_CAN_BITTIMING, bt);
91 }
92 }
93
94 NetlinkSocket sock(NETLINK_ROUTE);
95 return sock.send(req) && sock.receiveAck();
96}
97
98} // namespace can
99} // namespace netdevice
100} // namespace android