blob: aafbeccdb8cb2e4e0246b73b68ef9bca58818615 [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 "CanBusNative.h"
18
19#include <android-base/logging.h>
20#include <libnetdevice/can.h>
21#include <libnetdevice/libnetdevice.h>
22
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -080023namespace android::hardware::automotive::can::V1_0::implementation {
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070024
chrisweir204b8f92020-01-09 15:25:45 -080025CanBusNative::CanBusNative(const std::string& ifname, uint32_t bitrate)
26 : CanBus(ifname), mBitrate(bitrate) {}
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070027
28ICanController::Result CanBusNative::preUp() {
29 if (!netdevice::exists(mIfname)) {
30 LOG(ERROR) << "Interface " << mIfname << " doesn't exist";
Tomasz Wasilczykf3da9b62020-02-14 10:54:28 -080031 return ICanController::Result::BAD_INTERFACE_ID;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070032 }
33
chrisweir204b8f92020-01-09 15:25:45 -080034 if (mBitrate == 0) {
chrisweir39187db2020-01-06 15:21:07 -080035 // interface is already up and we just want to register it
36 return ICanController::Result::OK;
37 }
38
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070039 if (!netdevice::down(mIfname)) {
40 LOG(ERROR) << "Can't bring " << mIfname << " down (to configure it)";
41 return ICanController::Result::UNKNOWN_ERROR;
42 }
43
chrisweir204b8f92020-01-09 15:25:45 -080044 if (!netdevice::can::setBitrate(mIfname, mBitrate)) {
45 LOG(ERROR) << "Can't set bitrate " << mBitrate << " for " << mIfname;
46 return ICanController::Result::BAD_BITRATE;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070047 }
48
49 return ICanController::Result::OK;
50}
51
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -080052} // namespace android::hardware::automotive::can::V1_0::implementation