blob: 30a2924942075a552fe93537cda158d345c82f26 [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>
27#include <thread>
28
29namespace android {
30namespace hardware {
31namespace automotive {
32namespace can {
33namespace V1_0 {
34namespace implementation {
35
36struct CanBus : public ICanBus {
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080037 using ErrorCallback = std::function<void()>;
38
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070039 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 Wasilczyka9061962019-11-04 12:53:09 -080044 Return<sp<ICloseHandle>> listenForErrors(const sp<ICanErrorListener>& listener) override;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070045
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080046 void setErrorCallback(ErrorCallback errcb);
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070047 ICanController::Result up();
48 bool down();
49
50 protected:
51 CanBus(const std::string& ifname);
52
53 /**
54 * Prepare the SocketCAN interface.
55 *
56 * After calling this method, mIfname network interface is available and ready to be brought up.
57 */
58 virtual ICanController::Result preUp();
59
60 /**
61 * Cleanup after bringing the interface down.
62 *
63 * This is a counterpart to preUp().
64 */
65 virtual bool postDown();
66
67 /** Network interface name. */
68 const std::string mIfname;
69
70 private:
71 struct CanMessageListener {
72 sp<ICanMessageListener> callback;
73 hidl_vec<CanMessageFilter> filter;
74 wp<ICloseHandle> closeHandle;
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080075 bool failedOnce = false;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070076 };
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080077 void clearMsgListeners();
78 void clearErrListeners();
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070079
80 void onRead(const struct canfd_frame& frame, std::chrono::nanoseconds timestamp);
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080081 void onError(int errnoVal);
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070082
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080083 std::mutex mMsgListenersGuard;
84 std::vector<CanMessageListener> mMsgListeners GUARDED_BY(mMsgListenersGuard);
85
86 std::mutex mErrListenersGuard;
87 std::vector<sp<ICanErrorListener>> mErrListeners GUARDED_BY(mErrListenersGuard);
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070088
89 std::unique_ptr<CanSocket> mSocket;
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080090 bool mDownAfterUse;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070091
92 /**
93 * Guard for up flag is required to be held for entire time when the interface is being used
94 * (i.e. message being sent), because we don't want the interface to be torn down while
95 * executing that operation.
96 */
97 std::mutex mIsUpGuard;
98 bool mIsUp GUARDED_BY(mIsUpGuard) = false;
Tomasz Wasilczyka9061962019-11-04 12:53:09 -080099
100 ErrorCallback mErrCb;
Tomasz Wasilczyk87329672019-07-12 11:43:00 -0700101};
102
103} // namespace implementation
104} // namespace V1_0
105} // namespace can
106} // namespace automotive
107} // namespace hardware
108} // namespace android