blob: 1fc4037ae255898072524566f034a17ba53acc7e [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
Yu Shanbc2ed2a2022-09-15 19:01:52 -070019#include <IVhalClient.h>
Yu Shan562c0f82022-08-04 17:10:38 -070020#include <aidl/android/hardware/automotive/remoteaccess/ApState.h>
21#include <aidl/android/hardware/automotive/remoteaccess/BnRemoteAccess.h>
Yu Shandc54b8c2022-09-14 17:48:15 -070022#include <aidl/android/hardware/automotive/remoteaccess/BnRemoteTaskCallback.h>
Yu Shan562c0f82022-08-04 17:10:38 -070023#include <aidl/android/hardware/automotive/remoteaccess/IRemoteTaskCallback.h>
Yu Shan06ddbc62023-08-23 18:05:26 -070024#include <aidl/android/hardware/automotive/remoteaccess/ScheduleInfo.h>
Yu Shanb138c422022-09-06 19:11:23 -070025#include <android-base/thread_annotations.h>
Yu Shandc54b8c2022-09-14 17:48:15 -070026#include <android/binder_auto_utils.h>
27#include <utils/SystemClock.h>
Yu Shane2974562022-08-10 18:37:03 -070028#include <wakeup_client.grpc.pb.h>
Yu Shan562c0f82022-08-04 17:10:38 -070029
30#include <string>
Yu Shane2974562022-08-10 18:37:03 -070031#include <thread>
Yu Shan562c0f82022-08-04 17:10:38 -070032
33namespace android {
34namespace hardware {
35namespace automotive {
36namespace remoteaccess {
37
Yu Shandc54b8c2022-09-14 17:48:15 -070038// A IRemoteTaskCallback implementation for debug purpose.
39class DebugRemoteTaskCallback final
40 : public aidl::android::hardware::automotive::remoteaccess::BnRemoteTaskCallback {
41 public:
42 DebugRemoteTaskCallback() { mStartTimeMillis = android::uptimeMillis(); };
43
44 ndk::ScopedAStatus onRemoteTaskRequested(const std::string& clientId,
45 const std::vector<uint8_t>& data) override;
46 std::string printTasks();
47
48 private:
49 struct TaskData {
50 std::string clientId;
51 std::vector<uint8_t> data;
52 };
53
54 std::mutex mLock;
55 int64_t mStartTimeMillis;
56 std::vector<TaskData> mTasks;
57};
58
Yu Shan562c0f82022-08-04 17:10:38 -070059class RemoteAccessService
60 : public aidl::android::hardware::automotive::remoteaccess::BnRemoteAccess {
61 public:
Yu Shanb138c422022-09-06 19:11:23 -070062 explicit RemoteAccessService(WakeupClient::StubInterface* grpcStub);
63
64 ~RemoteAccessService();
Yu Shane2974562022-08-10 18:37:03 -070065
Eric Jeong6c3a1d82023-03-16 00:45:40 -070066 ndk::ScopedAStatus getVehicleId(std::string* vehicleId) override;
67
68 ndk::ScopedAStatus getProcessorId(std::string* processorId) override;
Yu Shan562c0f82022-08-04 17:10:38 -070069
Yu Shane2974562022-08-10 18:37:03 -070070 ndk::ScopedAStatus getWakeupServiceName(std::string* wakeupServiceName) override;
Yu Shan562c0f82022-08-04 17:10:38 -070071
72 ndk::ScopedAStatus setRemoteTaskCallback(
73 const std::shared_ptr<
74 aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>&
75 callback) override;
76
77 ndk::ScopedAStatus clearRemoteTaskCallback() override;
78
79 ndk::ScopedAStatus notifyApStateChange(
80 const aidl::android::hardware::automotive::remoteaccess::ApState& newState) override;
Yu Shane2974562022-08-10 18:37:03 -070081
Yu Shan06ddbc62023-08-23 18:05:26 -070082 ndk::ScopedAStatus isTaskScheduleSupported(bool* out) override;
83
84 ndk::ScopedAStatus scheduleTask(
85 const aidl::android::hardware::automotive::remoteaccess::ScheduleInfo& scheduleInfo)
86 override;
87
88 ndk::ScopedAStatus unscheduleTask(const std::string& clientId,
89 const std::string& scheduleId) override;
90
91 ndk::ScopedAStatus unscheduleAllTasks(const std::string& clientId) override;
92
93 ndk::ScopedAStatus isTaskScheduled(const std::string& clientId, const std::string& scheduleId,
94 bool* out) override;
95
96 ndk::ScopedAStatus getAllScheduledTasks(
97 const std::string& clientId,
98 std::vector<aidl::android::hardware::automotive::remoteaccess::ScheduleInfo>* out)
99 override;
100
Yu Shandc54b8c2022-09-14 17:48:15 -0700101 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
102
Yu Shane2974562022-08-10 18:37:03 -0700103 private:
Yu Shanb138c422022-09-06 19:11:23 -0700104 // For testing.
105 friend class RemoteAccessServiceUnitTest;
Yu Shane2974562022-08-10 18:37:03 -0700106
Yu Shandc54b8c2022-09-14 17:48:15 -0700107 static bool checkDumpPermission();
108
Yu Shanb138c422022-09-06 19:11:23 -0700109 WakeupClient::StubInterface* mGrpcStub;
110 std::thread mThread;
111 std::mutex mLock;
112 std::condition_variable mCv;
113 std::shared_ptr<aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>
114 mRemoteTaskCallback GUARDED_BY(mLock);
115 std::unique_ptr<grpc::ClientContext> mGetRemoteTasksContext GUARDED_BY(mLock);
116 // Associated with mCv to notify the task loop to stop waiting and exit.
117 bool mTaskWaitStopped GUARDED_BY(mLock);
118 // A mutex to make sure startTaskLoop does not overlap with stopTaskLoop.
119 std::mutex mStartStopTaskLoopLock;
Yu Shan95493682023-03-21 18:00:17 -0700120 bool mTaskLoopRunning GUARDED_BY(mStartStopTaskLoopLock) = false;
121 bool mGrpcConnected GUARDED_BY(mLock) = false;
122 std::unordered_map<std::string, size_t> mClientIdToTaskCount GUARDED_BY(mLock);
123
Yu Shanb138c422022-09-06 19:11:23 -0700124 // Default wait time before retry connecting to remote access client is 10s.
125 size_t mRetryWaitInMs = 10'000;
Yu Shandc54b8c2022-09-14 17:48:15 -0700126 std::shared_ptr<DebugRemoteTaskCallback> mDebugCallback;
Yu Shanb138c422022-09-06 19:11:23 -0700127
Yu Shan194757d2023-04-19 11:44:35 -0700128 std::thread mInjectDebugTaskThread;
129
Yu Shanb138c422022-09-06 19:11:23 -0700130 void runTaskLoop();
131 void maybeStartTaskLoop();
132 void maybeStopTaskLoop();
Eric Jeong6c3a1d82023-03-16 00:45:40 -0700133 ndk::ScopedAStatus getVehicleIdWithClient(
134 android::frameworks::automotive::vhal::IVhalClient& client, std::string* vehicleId);
Yu Shanb138c422022-09-06 19:11:23 -0700135
136 void setRetryWaitInMs(size_t retryWaitInMs) { mRetryWaitInMs = retryWaitInMs; }
Yu Shandc54b8c2022-09-14 17:48:15 -0700137 void dumpHelp(int fd);
Yu Shan95493682023-03-21 18:00:17 -0700138 void printCurrentStatus(int fd);
139 std::string clientIdToTaskCountToStringLocked() REQUIRES(mLock);
140 void debugInjectTask(int fd, std::string_view clientId, std::string_view taskData);
Yu Shan194757d2023-04-19 11:44:35 -0700141 void debugInjectTaskNextReboot(int fd, std::string_view clientId, std::string_view taskData,
142 const char* latencyInSecStr);
Yu Shan95493682023-03-21 18:00:17 -0700143 void updateGrpcConnected(bool connected);
144 android::base::Result<void> deliverRemoteTaskThroughCallback(const std::string& clientId,
145 std::string_view taskData);
Yu Shan562c0f82022-08-04 17:10:38 -0700146};
147
148} // namespace remoteaccess
149} // namespace automotive
150} // namespace hardware
151} // namespace android