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 { |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 37 | using ErrorCallback = std::function<void()>; |
| 38 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 39 | virtual ~CanBus(); |
| 40 | |
| 41 | Return<Result> send(const CanMessage& message) override; |
| 42 | Return<void> listen(const hidl_vec<CanMessageFilter>& filter, |
| 43 | const sp<ICanMessageListener>& listener, listen_cb _hidl_cb) override; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 44 | Return<sp<ICloseHandle>> listenForErrors(const sp<ICanErrorListener>& listener) override; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 45 | |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 46 | void setErrorCallback(ErrorCallback errcb); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 47 | ICanController::Result up(); |
| 48 | bool down(); |
| 49 | |
| 50 | protected: |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Blank constructor, since some interface types (such as SLCAN) don't get a name until after |
| 53 | * being initialized. |
| 54 | * |
| 55 | * If using this constructor, you MUST initialize mIfname prior to the completion of preUp(). |
| 56 | */ |
| 57 | CanBus(); |
| 58 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 59 | CanBus(const std::string& ifname); |
| 60 | |
| 61 | /** |
| 62 | * Prepare the SocketCAN interface. |
| 63 | * |
| 64 | * After calling this method, mIfname network interface is available and ready to be brought up. |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 65 | * |
| 66 | * \return OK on success, or an error state on failure. See ICanController::Result |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 67 | */ |
| 68 | virtual ICanController::Result preUp(); |
| 69 | |
| 70 | /** |
| 71 | * Cleanup after bringing the interface down. |
| 72 | * |
| 73 | * This is a counterpart to preUp(). |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 74 | * |
| 75 | * \return true upon success and false upon failure |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 76 | */ |
| 77 | virtual bool postDown(); |
| 78 | |
| 79 | /** Network interface name. */ |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 80 | std::string mIfname; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | struct CanMessageListener { |
| 84 | sp<ICanMessageListener> callback; |
| 85 | hidl_vec<CanMessageFilter> filter; |
| 86 | wp<ICloseHandle> closeHandle; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 87 | bool failedOnce = false; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 88 | }; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 89 | void clearMsgListeners(); |
| 90 | void clearErrListeners(); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 91 | |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame^] | 92 | void notifyErrorListeners(ErrorEvent err, bool isFatal); |
| 93 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 94 | void onRead(const struct canfd_frame& frame, std::chrono::nanoseconds timestamp); |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 95 | void onError(int errnoVal); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 96 | |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 97 | std::mutex mMsgListenersGuard; |
| 98 | std::vector<CanMessageListener> mMsgListeners GUARDED_BY(mMsgListenersGuard); |
| 99 | |
| 100 | std::mutex mErrListenersGuard; |
| 101 | std::vector<sp<ICanErrorListener>> mErrListeners GUARDED_BY(mErrListenersGuard); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 102 | |
| 103 | std::unique_ptr<CanSocket> mSocket; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 104 | bool mDownAfterUse; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Guard for up flag is required to be held for entire time when the interface is being used |
| 108 | * (i.e. message being sent), because we don't want the interface to be torn down while |
| 109 | * executing that operation. |
| 110 | */ |
| 111 | std::mutex mIsUpGuard; |
| 112 | bool mIsUp GUARDED_BY(mIsUpGuard) = false; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 113 | |
| 114 | ErrorCallback mErrCb; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace implementation |
| 118 | } // namespace V1_0 |
| 119 | } // namespace can |
| 120 | } // namespace automotive |
| 121 | } // namespace hardware |
| 122 | } // namespace android |