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 | #pragma once |
| 17 | |
| 18 | #include <aidl/android/hardware/automotive/can/Result.h> |
| 19 | |
| 20 | #include <android-base/macros.h> |
| 21 | #include <utils/Mutex.h> |
| 22 | |
| 23 | #include <atomic> |
| 24 | #include <mutex> |
| 25 | |
| 26 | namespace aidl::android::hardware::automotive::can { |
| 27 | |
| 28 | class CanBus { |
| 29 | public: |
| 30 | /** |
| 31 | * Some interface types (such as SLCAN) don't get an interface name until after being |
| 32 | * initialized, hence ifname is optional. |
| 33 | * |
| 34 | * You MUST ensure mIfname is initialized prior to the completion of preUp(). |
| 35 | */ |
| 36 | CanBus(std::string_view ifname = std::string_view{""}); |
| 37 | |
| 38 | virtual ~CanBus(); |
| 39 | |
| 40 | Result up(); |
| 41 | Result down(); |
| 42 | std::string getIfaceName(); |
| 43 | |
| 44 | protected: |
| 45 | /** |
| 46 | * Prepare the SocketCAN interface. |
| 47 | * |
| 48 | * After calling this method, mIfname network interface is available and ready to be brought up. |
| 49 | * |
| 50 | * \return true upon success and false upon failure |
| 51 | */ |
| 52 | virtual Result preUp(); |
| 53 | |
| 54 | /** |
| 55 | * Cleanup after bringing the interface down. |
| 56 | * |
| 57 | * This is a counterpart to preUp(). |
| 58 | * |
| 59 | * \return true upon success and false upon failure |
| 60 | */ |
| 61 | virtual bool postDown(); |
| 62 | |
| 63 | /** Network interface name. */ |
| 64 | std::string mIfname; |
| 65 | |
| 66 | private: |
| 67 | /** |
| 68 | * Guard for up flag is required to be held for entire time when the interface is being used |
| 69 | * because we don't want the interface to be torn down while executing that operation. |
| 70 | */ |
| 71 | std::mutex mIsUpGuard; |
| 72 | bool mIsUp GUARDED_BY(mIsUpGuard) = false; |
| 73 | |
| 74 | bool mDownAfterUse; |
| 75 | }; |
| 76 | |
| 77 | } // namespace aidl::android::hardware::automotive::can |