Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 1 | /* |
| 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> |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 27 | #include <linux/can/error.h> |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 28 | #include <linux/can/netlink.h> |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 29 | #include <linux/can/raw.h> |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 30 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 31 | namespace android::netdevice::can { |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 32 | |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 33 | static constexpr can_err_mask_t kErrMask = CAN_ERR_MASK; |
| 34 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 35 | base::unique_fd socket(const std::string& ifname) { |
| 36 | struct sockaddr_can addr = {}; |
| 37 | addr.can_family = AF_CAN; |
| 38 | addr.can_ifindex = nametoindex(ifname); |
| 39 | if (addr.can_ifindex == 0) { |
| 40 | LOG(ERROR) << "Interface " << ifname << " doesn't exists"; |
| 41 | return {}; |
| 42 | } |
| 43 | |
| 44 | base::unique_fd sock(::socket(PF_CAN, SOCK_RAW, CAN_RAW)); |
| 45 | if (!sock.ok()) { |
| 46 | LOG(ERROR) << "Failed to create CAN socket"; |
| 47 | return {}; |
| 48 | } |
| 49 | |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 50 | if (setsockopt(sock.get(), SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &kErrMask, sizeof(kErrMask)) < 0) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 51 | PLOG(ERROR) << "Can't receive error frames, CAN setsockpt failed"; |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 52 | return {}; |
| 53 | } |
| 54 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 55 | if (0 != fcntl(sock.get(), F_SETFL, O_RDWR | O_NONBLOCK)) { |
| 56 | LOG(ERROR) << "Couldn't put CAN socket in non-blocking mode"; |
| 57 | return {}; |
| 58 | } |
| 59 | |
| 60 | if (0 != bind(sock.get(), reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) { |
| 61 | LOG(ERROR) << "Can't bind to CAN interface " << ifname; |
| 62 | return {}; |
| 63 | } |
| 64 | |
| 65 | return sock; |
| 66 | } |
| 67 | |
| 68 | bool setBitrate(std::string ifname, uint32_t bitrate) { |
| 69 | struct can_bittiming bt = {}; |
| 70 | bt.bitrate = bitrate; |
| 71 | |
| 72 | NetlinkRequest<struct ifinfomsg> req(RTM_NEWLINK, NLM_F_REQUEST); |
| 73 | |
| 74 | const auto ifidx = nametoindex(ifname); |
| 75 | if (ifidx == 0) { |
| 76 | LOG(ERROR) << "Can't find interface " << ifname; |
| 77 | return false; |
| 78 | } |
| 79 | req.data().ifi_index = ifidx; |
| 80 | |
| 81 | { |
| 82 | auto linkinfo = req.nest(IFLA_LINKINFO); |
| 83 | req.addattr(IFLA_INFO_KIND, "can"); |
| 84 | { |
| 85 | auto infodata = req.nest(IFLA_INFO_DATA); |
| 86 | /* For CAN FD, it would require to add IFLA_CAN_DATA_BITTIMING |
| 87 | * and IFLA_CAN_CTRLMODE as well. */ |
| 88 | req.addattr(IFLA_CAN_BITTIMING, bt); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | NetlinkSocket sock(NETLINK_ROUTE); |
| 93 | return sock.send(req) && sock.receiveAck(); |
| 94 | } |
| 95 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 96 | } // namespace android::netdevice::can |