blob: 74c2af4c9fb7270859c51de6530f299b899d92f9 [file] [log] [blame]
Yu Shan7a5283f2022-10-25 18:01:05 -07001/*
2 * Copyright (C) 2022 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 <IVhalClient.h>
20#include <aidl/android/hardware/automotive/remoteaccess/ApState.h>
21#include <aidl/android/hardware/automotive/remoteaccess/BnRemoteAccess.h>
22#include <aidl/android/hardware/automotive/remoteaccess/BnRemoteTaskCallback.h>
23#include <aidl/android/hardware/automotive/remoteaccess/IRemoteTaskCallback.h>
24#include <android-base/thread_annotations.h>
25#include <android/binder_auto_utils.h>
26#include <utils/SystemClock.h>
27#include <wakeup_client.grpc.pb.h>
28
29#include <string>
30#include <thread>
31
32namespace android {
33namespace hardware {
34namespace automotive {
35namespace remoteaccess {
36
37// A IRemoteTaskCallback implementation for debug purpose.
38class DebugRemoteTaskCallback final
39 : public aidl::android::hardware::automotive::remoteaccess::BnRemoteTaskCallback {
40 public:
41 DebugRemoteTaskCallback() { mStartTimeMillis = android::uptimeMillis(); };
42
43 ndk::ScopedAStatus onRemoteTaskRequested(const std::string& clientId,
44 const std::vector<uint8_t>& data) override;
45 std::string printTasks();
46
47 private:
48 struct TaskData {
49 std::string clientId;
50 std::vector<uint8_t> data;
51 };
52
53 std::mutex mLock;
54 int64_t mStartTimeMillis;
55 std::vector<TaskData> mTasks;
56};
57
58class RemoteAccessService
59 : public aidl::android::hardware::automotive::remoteaccess::BnRemoteAccess {
60 public:
61 explicit RemoteAccessService(WakeupClient::StubInterface* grpcStub);
62
63 ~RemoteAccessService();
64
65 ndk::ScopedAStatus getDeviceId(std::string* deviceId) override;
66
67 ndk::ScopedAStatus getWakeupServiceName(std::string* wakeupServiceName) override;
68
69 ndk::ScopedAStatus setRemoteTaskCallback(
70 const std::shared_ptr<
71 aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>&
72 callback) override;
73
74 ndk::ScopedAStatus clearRemoteTaskCallback() override;
75
76 ndk::ScopedAStatus notifyApStateChange(
77 const aidl::android::hardware::automotive::remoteaccess::ApState& newState) override;
78
79 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
80
81 private:
82 // For testing.
83 friend class RemoteAccessServiceUnitTest;
84
85 static bool checkDumpPermission();
86
87 WakeupClient::StubInterface* mGrpcStub;
88 std::thread mThread;
89 std::mutex mLock;
90 std::condition_variable mCv;
91 std::shared_ptr<aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>
92 mRemoteTaskCallback GUARDED_BY(mLock);
93 std::unique_ptr<grpc::ClientContext> mGetRemoteTasksContext GUARDED_BY(mLock);
94 // Associated with mCv to notify the task loop to stop waiting and exit.
95 bool mTaskWaitStopped GUARDED_BY(mLock);
96 // A mutex to make sure startTaskLoop does not overlap with stopTaskLoop.
97 std::mutex mStartStopTaskLoopLock;
98 bool mTaskLoopRunning GUARDED_BY(mStartStopTaskLoopLock);
99 // Default wait time before retry connecting to remote access client is 10s.
100 size_t mRetryWaitInMs = 10'000;
101 std::shared_ptr<DebugRemoteTaskCallback> mDebugCallback;
102
103 void runTaskLoop();
104 void maybeStartTaskLoop();
105 void maybeStopTaskLoop();
106 ndk::ScopedAStatus getDeviceIdWithClient(
107 android::frameworks::automotive::vhal::IVhalClient& client, std::string* deviceId);
108
109 void setRetryWaitInMs(size_t retryWaitInMs) { mRetryWaitInMs = retryWaitInMs; }
110 void dumpHelp(int fd);
111};
112
113} // namespace remoteaccess
114} // namespace automotive
115} // namespace hardware
116} // namespace android