blob: 977bfcc6cb8a2c21c27bf410c41ef5cbfd058e83 [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#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>
Pirama Arumuga Nainar12129022024-06-04 23:10:12 +000027#include <mutex>
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070028#include <thread>
29
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -080030namespace android::hardware::automotive::can::V1_0::implementation {
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070031
32struct CanBus : public ICanBus {
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080033 using ErrorCallback = std::function<void()>;
34
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070035 virtual ~CanBus();
36
37 Return<Result> send(const CanMessage& message) override;
38 Return<void> listen(const hidl_vec<CanMessageFilter>& filter,
39 const sp<ICanMessageListener>& listener, listen_cb _hidl_cb) override;
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080040 Return<sp<ICloseHandle>> listenForErrors(const sp<ICanErrorListener>& listener) override;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070041
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080042 void setErrorCallback(ErrorCallback errcb);
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070043 ICanController::Result up();
44 bool down();
45
46 protected:
chrisweircf36cea2019-11-08 16:41:02 -080047 /**
48 * Blank constructor, since some interface types (such as SLCAN) don't get a name until after
49 * being initialized.
50 *
51 * If using this constructor, you MUST initialize mIfname prior to the completion of preUp().
52 */
53 CanBus();
54
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070055 CanBus(const std::string& ifname);
56
57 /**
58 * Prepare the SocketCAN interface.
59 *
60 * After calling this method, mIfname network interface is available and ready to be brought up.
chrisweircf36cea2019-11-08 16:41:02 -080061 *
62 * \return OK on success, or an error state on failure. See ICanController::Result
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070063 */
64 virtual ICanController::Result preUp();
65
66 /**
67 * Cleanup after bringing the interface down.
68 *
69 * This is a counterpart to preUp().
chrisweircf36cea2019-11-08 16:41:02 -080070 *
71 * \return true upon success and false upon failure
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070072 */
73 virtual bool postDown();
74
75 /** Network interface name. */
chrisweircf36cea2019-11-08 16:41:02 -080076 std::string mIfname;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070077
78 private:
79 struct CanMessageListener {
80 sp<ICanMessageListener> callback;
81 hidl_vec<CanMessageFilter> filter;
82 wp<ICloseHandle> closeHandle;
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080083 bool failedOnce = false;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070084 };
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080085 void clearMsgListeners();
86 void clearErrListeners();
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070087
chrisweirbee3d2c2019-11-19 10:23:37 -080088 void notifyErrorListeners(ErrorEvent err, bool isFatal);
89
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070090 void onRead(const struct canfd_frame& frame, std::chrono::nanoseconds timestamp);
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080091 void onError(int errnoVal);
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070092
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080093 std::mutex mMsgListenersGuard;
94 std::vector<CanMessageListener> mMsgListeners GUARDED_BY(mMsgListenersGuard);
95
96 std::mutex mErrListenersGuard;
97 std::vector<sp<ICanErrorListener>> mErrListeners GUARDED_BY(mErrListenersGuard);
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070098
99 std::unique_ptr<CanSocket> mSocket;
Tomasz Wasilczyka9061962019-11-04 12:53:09 -0800100 bool mDownAfterUse;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -0700101
102 /**
103 * Guard for up flag is required to be held for entire time when the interface is being used
104 * (i.e. message being sent), because we don't want the interface to be torn down while
105 * executing that operation.
106 */
107 std::mutex mIsUpGuard;
108 bool mIsUp GUARDED_BY(mIsUpGuard) = false;
Tomasz Wasilczyka9061962019-11-04 12:53:09 -0800109
110 ErrorCallback mErrCb;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -0700111};
112
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -0800113} // namespace android::hardware::automotive::can::V1_0::implementation