blob: c6f1b0413a31951ef3e07cabef72f55b2eac07f4 [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
Tomasz Wasilczykad107412020-05-29 16:32:43 -070017#include <libnetdevice/can.h>
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070018
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070019#include "common.h"
20
21#include <android-base/logging.h>
22#include <android-base/unique_fd.h>
Tomasz Wasilczyk16954592020-07-28 09:42:55 -070023#include <libnl++/NetlinkRequest.h>
24#include <libnl++/NetlinkSocket.h>
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070025
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 Wasilczyk390c7112020-07-30 11:08:29 -070030#include <linux/rtnetlink.h>
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070031
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -080032namespace android::netdevice::can {
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070033
chrisweirbee3d2c2019-11-19 10:23:37 -080034static constexpr can_err_mask_t kErrMask = CAN_ERR_MASK;
35
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070036base::unique_fd socket(const std::string& ifname) {
37 struct sockaddr_can addr = {};
38 addr.can_family = AF_CAN;
39 addr.can_ifindex = nametoindex(ifname);
40 if (addr.can_ifindex == 0) {
41 LOG(ERROR) << "Interface " << ifname << " doesn't exists";
42 return {};
43 }
44
45 base::unique_fd sock(::socket(PF_CAN, SOCK_RAW, CAN_RAW));
46 if (!sock.ok()) {
47 LOG(ERROR) << "Failed to create CAN socket";
48 return {};
49 }
50
chrisweirbee3d2c2019-11-19 10:23:37 -080051 if (setsockopt(sock.get(), SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &kErrMask, sizeof(kErrMask)) < 0) {
chrisweir1173a722020-02-26 14:39:56 -080052 PLOG(ERROR) << "Can't receive error frames, CAN setsockpt failed";
chrisweirbee3d2c2019-11-19 10:23:37 -080053 return {};
54 }
55
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070056 if (0 != fcntl(sock.get(), F_SETFL, O_RDWR | O_NONBLOCK)) {
57 LOG(ERROR) << "Couldn't put CAN socket in non-blocking mode";
58 return {};
59 }
60
61 if (0 != bind(sock.get(), reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) {
62 LOG(ERROR) << "Can't bind to CAN interface " << ifname;
63 return {};
64 }
65
66 return sock;
67}
68
69bool setBitrate(std::string ifname, uint32_t bitrate) {
70 struct can_bittiming bt = {};
71 bt.bitrate = bitrate;
72
Tomasz Wasilczyk1d5beb82020-07-29 09:37:44 -070073 nl::NetlinkRequest<struct ifinfomsg> req(RTM_NEWLINK, NLM_F_REQUEST);
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070074
75 const auto ifidx = nametoindex(ifname);
76 if (ifidx == 0) {
77 LOG(ERROR) << "Can't find interface " << ifname;
78 return false;
79 }
80 req.data().ifi_index = ifidx;
81
82 {
83 auto linkinfo = req.nest(IFLA_LINKINFO);
84 req.addattr(IFLA_INFO_KIND, "can");
85 {
86 auto infodata = req.nest(IFLA_INFO_DATA);
87 /* For CAN FD, it would require to add IFLA_CAN_DATA_BITTIMING
88 * and IFLA_CAN_CTRLMODE as well. */
89 req.addattr(IFLA_CAN_BITTIMING, bt);
90 }
91 }
92
Tomasz Wasilczyk1d5beb82020-07-29 09:37:44 -070093 nl::NetlinkSocket sock(NETLINK_ROUTE);
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070094 return sock.send(req) && sock.receiveAck();
95}
96
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -080097} // namespace android::netdevice::can