blob: 207c09369f97333420a290b95f1e7c2f809f30fb [file] [log] [blame]
Yu Shan562c0f82022-08-04 17:10:38 -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 <aidl/android/hardware/automotive/remoteaccess/ApState.h>
20#include <aidl/android/hardware/automotive/remoteaccess/BnRemoteAccess.h>
Yu Shandc54b8c2022-09-14 17:48:15 -070021#include <aidl/android/hardware/automotive/remoteaccess/BnRemoteTaskCallback.h>
Yu Shan562c0f82022-08-04 17:10:38 -070022#include <aidl/android/hardware/automotive/remoteaccess/IRemoteTaskCallback.h>
Yu Shanb138c422022-09-06 19:11:23 -070023#include <android-base/thread_annotations.h>
Yu Shandc54b8c2022-09-14 17:48:15 -070024#include <android/binder_auto_utils.h>
25#include <utils/SystemClock.h>
Yu Shane2974562022-08-10 18:37:03 -070026#include <wakeup_client.grpc.pb.h>
Yu Shan562c0f82022-08-04 17:10:38 -070027
28#include <string>
Yu Shane2974562022-08-10 18:37:03 -070029#include <thread>
Yu Shan562c0f82022-08-04 17:10:38 -070030
31namespace android {
32namespace hardware {
33namespace automotive {
34namespace remoteaccess {
35
Yu Shandc54b8c2022-09-14 17:48:15 -070036// A IRemoteTaskCallback implementation for debug purpose.
37class DebugRemoteTaskCallback final
38 : public aidl::android::hardware::automotive::remoteaccess::BnRemoteTaskCallback {
39 public:
40 DebugRemoteTaskCallback() { mStartTimeMillis = android::uptimeMillis(); };
41
42 ndk::ScopedAStatus onRemoteTaskRequested(const std::string& clientId,
43 const std::vector<uint8_t>& data) override;
44 std::string printTasks();
45
46 private:
47 struct TaskData {
48 std::string clientId;
49 std::vector<uint8_t> data;
50 };
51
52 std::mutex mLock;
53 int64_t mStartTimeMillis;
54 std::vector<TaskData> mTasks;
55};
56
Yu Shan562c0f82022-08-04 17:10:38 -070057class RemoteAccessService
58 : public aidl::android::hardware::automotive::remoteaccess::BnRemoteAccess {
59 public:
Yu Shanb138c422022-09-06 19:11:23 -070060 explicit RemoteAccessService(WakeupClient::StubInterface* grpcStub);
61
62 ~RemoteAccessService();
Yu Shane2974562022-08-10 18:37:03 -070063
Yu Shan562c0f82022-08-04 17:10:38 -070064 ndk::ScopedAStatus getDeviceId(std::string* deviceId) override;
65
Yu Shane2974562022-08-10 18:37:03 -070066 ndk::ScopedAStatus getWakeupServiceName(std::string* wakeupServiceName) override;
Yu Shan562c0f82022-08-04 17:10:38 -070067
68 ndk::ScopedAStatus setRemoteTaskCallback(
69 const std::shared_ptr<
70 aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>&
71 callback) override;
72
73 ndk::ScopedAStatus clearRemoteTaskCallback() override;
74
75 ndk::ScopedAStatus notifyApStateChange(
76 const aidl::android::hardware::automotive::remoteaccess::ApState& newState) override;
Yu Shane2974562022-08-10 18:37:03 -070077
Yu Shandc54b8c2022-09-14 17:48:15 -070078 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
79
Yu Shane2974562022-08-10 18:37:03 -070080 private:
Yu Shanb138c422022-09-06 19:11:23 -070081 // For testing.
82 friend class RemoteAccessServiceUnitTest;
Yu Shane2974562022-08-10 18:37:03 -070083
Yu Shandc54b8c2022-09-14 17:48:15 -070084 static bool checkDumpPermission();
85
Yu Shanb138c422022-09-06 19:11:23 -070086 WakeupClient::StubInterface* mGrpcStub;
87 std::thread mThread;
88 std::mutex mLock;
89 std::condition_variable mCv;
90 std::shared_ptr<aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>
91 mRemoteTaskCallback GUARDED_BY(mLock);
92 std::unique_ptr<grpc::ClientContext> mGetRemoteTasksContext GUARDED_BY(mLock);
93 // Associated with mCv to notify the task loop to stop waiting and exit.
94 bool mTaskWaitStopped GUARDED_BY(mLock);
95 // A mutex to make sure startTaskLoop does not overlap with stopTaskLoop.
96 std::mutex mStartStopTaskLoopLock;
97 bool mTaskLoopRunning GUARDED_BY(mStartStopTaskLoopLock);
98 // Default wait time before retry connecting to remote access client is 10s.
99 size_t mRetryWaitInMs = 10'000;
Yu Shandc54b8c2022-09-14 17:48:15 -0700100 std::shared_ptr<DebugRemoteTaskCallback> mDebugCallback;
Yu Shanb138c422022-09-06 19:11:23 -0700101
102 void runTaskLoop();
103 void maybeStartTaskLoop();
104 void maybeStopTaskLoop();
105
106 void setRetryWaitInMs(size_t retryWaitInMs) { mRetryWaitInMs = retryWaitInMs; }
Yu Shandc54b8c2022-09-14 17:48:15 -0700107 void dumpHelp(int fd);
Yu Shan562c0f82022-08-04 17:10:38 -0700108};
109
110} // namespace remoteaccess
111} // namespace automotive
112} // namespace hardware
113} // namespace android