Chris Weir | d76e07e | 2022-07-08 09:52:52 -0700 | [diff] [blame] | 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 "CanBus.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <libnetdevice/libnetdevice.h> |
| 21 | |
| 22 | namespace aidl::android::hardware::automotive::can { |
| 23 | |
| 24 | CanBus::CanBus(std::string_view ifname) : mIfname(ifname) {} |
| 25 | |
| 26 | CanBus::~CanBus() { |
| 27 | std::lock_guard<std::mutex> lck(mIsUpGuard); |
| 28 | CHECK(!mIsUp) << "Interface is still up while being destroyed"; |
| 29 | } |
| 30 | |
| 31 | Result CanBus::preUp() { |
| 32 | return Result::OK; |
| 33 | } |
| 34 | |
| 35 | bool CanBus::postDown() { |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | std::string CanBus::getIfaceName() { |
| 40 | return mIfname; |
| 41 | } |
| 42 | |
| 43 | Result CanBus::up() { |
| 44 | std::lock_guard<std::mutex> lck(mIsUpGuard); |
| 45 | |
| 46 | if (mIsUp) { |
| 47 | LOG(WARNING) << "Interface is already up"; |
| 48 | return Result::INVALID_STATE; |
| 49 | } |
| 50 | |
| 51 | const auto preResult = preUp(); |
| 52 | if (preResult != Result::OK) return preResult; |
| 53 | |
| 54 | const auto isUp = ::android::netdevice::isUp(mIfname); |
| 55 | if (!isUp.has_value()) { |
| 56 | // preUp() should prepare the interface (either create or make sure it's there) |
| 57 | LOG(ERROR) << "Interface " << mIfname << " didn't get prepared"; |
| 58 | return Result::BAD_INTERFACE_ID; |
| 59 | } |
| 60 | |
| 61 | if (!*isUp && !::android::netdevice::up(mIfname)) { |
| 62 | LOG(ERROR) << "Can't bring " << mIfname << " up"; |
| 63 | return Result::UNKNOWN_ERROR; |
| 64 | } |
| 65 | mDownAfterUse = !*isUp; |
| 66 | |
| 67 | mIsUp = true; |
| 68 | return Result::OK; |
| 69 | } |
| 70 | |
| 71 | Result CanBus::down() { |
| 72 | std::lock_guard<std::mutex> lck(mIsUpGuard); |
| 73 | |
| 74 | if (!mIsUp) { |
| 75 | LOG(WARNING) << "Interface is already down"; |
| 76 | return Result::INVALID_STATE; |
| 77 | } |
| 78 | mIsUp = false; |
| 79 | |
| 80 | Result success = Result::OK; |
| 81 | |
| 82 | if (mDownAfterUse && !::android::netdevice::down(mIfname)) { |
| 83 | LOG(ERROR) << "Can't bring " << mIfname << " down"; |
| 84 | // don't return yet, let's try to do best-effort cleanup |
| 85 | success = Result::UNKNOWN_ERROR; |
| 86 | } |
| 87 | |
| 88 | if (!postDown()) success = Result::UNKNOWN_ERROR; |
| 89 | |
| 90 | return success; |
| 91 | } |
| 92 | |
| 93 | } // namespace aidl::android::hardware::automotive::can |