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