xref: /aosp_15_r20/hardware/interfaces/automotive/can/aidl/default/CanBusNative.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright 2022, 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 
23 namespace aidl::android::hardware::automotive::can {
24 
25 using namespace ::android;
26 
CanBusNative(const std::string & ifname,uint32_t bitrate)27 CanBusNative::CanBusNative(const std::string& ifname, uint32_t bitrate)
28     : CanBus(ifname), mBitrate(bitrate) {}
29 
preUp()30 Result CanBusNative::preUp() {
31     if (!netdevice::exists(mIfname)) {
32         LOG(ERROR) << "Interface " << mIfname << " doesn't exist";
33         return Result::BAD_INTERFACE_ID;
34     }
35 
36     if (mBitrate == 0) {
37         // interface is already up and we just want to register it
38         return Result::OK;
39     }
40 
41     if (!netdevice::down(mIfname)) {
42         LOG(ERROR) << "Can't bring " << mIfname << " down (to configure it)";
43         return Result::UNKNOWN_ERROR;
44     }
45 
46     if (!netdevice::can::setBitrate(mIfname, mBitrate)) {
47         LOG(ERROR) << "Can't set bitrate " << mBitrate << " for " << mIfname;
48         return Result::BAD_BITRATE;
49     }
50 
51     return Result::OK;
52 }
53 
54 }  // namespace aidl::android::hardware::automotive::can
55