blob: cc59fa97b80de35d57fddd235044193e20ecf0a4 [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 "CanBusVirtual.h"
18
19#include <android-base/logging.h>
20#include <libnetdevice/libnetdevice.h>
21
22namespace android {
23namespace hardware {
24namespace automotive {
25namespace can {
26namespace V1_0 {
27namespace implementation {
28
29CanBusVirtual::CanBusVirtual(const std::string& ifname) : CanBus(ifname) {}
30
31ICanController::Result CanBusVirtual::preUp() {
32 if (netdevice::exists(mIfname)) return ICanController::Result::OK;
33
34 LOG(DEBUG) << "Virtual interface " << mIfname << " doesn't exist, creating...";
35 mWasCreated = true;
36 if (!netdevice::add(mIfname, "vcan")) {
37 LOG(ERROR) << "Can't create vcan interface " << mIfname;
38 return ICanController::Result::UNKNOWN_ERROR;
39 }
40
41 return ICanController::Result::OK;
42}
43
44bool CanBusVirtual::postDown() {
45 if (mWasCreated) {
46 mWasCreated = false;
47 if (!netdevice::del(mIfname)) {
48 LOG(ERROR) << "Couldn't remove vcan interface " << mIfname;
49 return false;
50 }
51 }
52 return true;
53}
54
55} // namespace implementation
56} // namespace V1_0
57} // namespace can
58} // namespace automotive
59} // namespace hardware
60} // namespace android