Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame^] | 1 | /* |
| 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 | #pragma once |
| 18 | |
| 19 | #include "CanSocket.h" |
| 20 | |
| 21 | #include <android-base/unique_fd.h> |
| 22 | #include <android/hardware/automotive/can/1.0/ICanBus.h> |
| 23 | #include <android/hardware/automotive/can/1.0/ICanController.h> |
| 24 | #include <utils/Mutex.h> |
| 25 | |
| 26 | #include <atomic> |
| 27 | #include <thread> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | namespace automotive { |
| 32 | namespace can { |
| 33 | namespace V1_0 { |
| 34 | namespace implementation { |
| 35 | |
| 36 | struct CanBus : public ICanBus { |
| 37 | virtual ~CanBus(); |
| 38 | |
| 39 | Return<Result> send(const CanMessage& message) override; |
| 40 | Return<void> listen(const hidl_vec<CanMessageFilter>& filter, |
| 41 | const sp<ICanMessageListener>& listener, listen_cb _hidl_cb) override; |
| 42 | |
| 43 | ICanController::Result up(); |
| 44 | bool down(); |
| 45 | |
| 46 | protected: |
| 47 | CanBus(const std::string& ifname); |
| 48 | |
| 49 | /** |
| 50 | * Prepare the SocketCAN interface. |
| 51 | * |
| 52 | * After calling this method, mIfname network interface is available and ready to be brought up. |
| 53 | */ |
| 54 | virtual ICanController::Result preUp(); |
| 55 | |
| 56 | /** |
| 57 | * Cleanup after bringing the interface down. |
| 58 | * |
| 59 | * This is a counterpart to preUp(). |
| 60 | */ |
| 61 | virtual bool postDown(); |
| 62 | |
| 63 | /** Network interface name. */ |
| 64 | const std::string mIfname; |
| 65 | |
| 66 | private: |
| 67 | struct CanMessageListener { |
| 68 | sp<ICanMessageListener> callback; |
| 69 | hidl_vec<CanMessageFilter> filter; |
| 70 | wp<ICloseHandle> closeHandle; |
| 71 | }; |
| 72 | void clearListeners(); |
| 73 | |
| 74 | void onRead(const struct canfd_frame& frame, std::chrono::nanoseconds timestamp); |
| 75 | void onError(); |
| 76 | |
| 77 | std::mutex mListenersGuard; |
| 78 | std::vector<CanMessageListener> mListeners GUARDED_BY(mListenersGuard); |
| 79 | |
| 80 | std::unique_ptr<CanSocket> mSocket; |
| 81 | bool mWasUpInitially; |
| 82 | |
| 83 | /** |
| 84 | * Guard for up flag is required to be held for entire time when the interface is being used |
| 85 | * (i.e. message being sent), because we don't want the interface to be torn down while |
| 86 | * executing that operation. |
| 87 | */ |
| 88 | std::mutex mIsUpGuard; |
| 89 | bool mIsUp GUARDED_BY(mIsUpGuard) = false; |
| 90 | }; |
| 91 | |
| 92 | } // namespace implementation |
| 93 | } // namespace V1_0 |
| 94 | } // namespace can |
| 95 | } // namespace automotive |
| 96 | } // namespace hardware |
| 97 | } // namespace android |