Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Shan | bc2ed2a | 2022-09-15 19:01:52 -0700 | [diff] [blame] | 19 | #include <IVhalClient.h> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 20 | #include <aidl/android/hardware/automotive/remoteaccess/ApState.h> |
| 21 | #include <aidl/android/hardware/automotive/remoteaccess/BnRemoteAccess.h> |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 22 | #include <aidl/android/hardware/automotive/remoteaccess/BnRemoteTaskCallback.h> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 23 | #include <aidl/android/hardware/automotive/remoteaccess/IRemoteTaskCallback.h> |
Yu Shan | 06ddbc6 | 2023-08-23 18:05:26 -0700 | [diff] [blame] | 24 | #include <aidl/android/hardware/automotive/remoteaccess/ScheduleInfo.h> |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 25 | #include <android-base/thread_annotations.h> |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 26 | #include <android/binder_auto_utils.h> |
| 27 | #include <utils/SystemClock.h> |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 28 | #include <wakeup_client.grpc.pb.h> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 29 | |
| 30 | #include <string> |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 31 | #include <thread> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace hardware { |
| 35 | namespace automotive { |
| 36 | namespace remoteaccess { |
| 37 | |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 38 | // A IRemoteTaskCallback implementation for debug purpose. |
| 39 | class 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 Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 59 | class RemoteAccessService |
| 60 | : public aidl::android::hardware::automotive::remoteaccess::BnRemoteAccess { |
| 61 | public: |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 62 | explicit RemoteAccessService(WakeupClient::StubInterface* grpcStub); |
| 63 | |
| 64 | ~RemoteAccessService(); |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 65 | |
Eric Jeong | 6c3a1d8 | 2023-03-16 00:45:40 -0700 | [diff] [blame] | 66 | ndk::ScopedAStatus getVehicleId(std::string* vehicleId) override; |
| 67 | |
| 68 | ndk::ScopedAStatus getProcessorId(std::string* processorId) override; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 69 | |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 70 | ndk::ScopedAStatus getWakeupServiceName(std::string* wakeupServiceName) override; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 71 | |
| 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 Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 81 | |
Yu Shan | 06ddbc6 | 2023-08-23 18:05:26 -0700 | [diff] [blame] | 82 | 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 Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 101 | binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; |
| 102 | |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 103 | private: |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 104 | // For testing. |
| 105 | friend class RemoteAccessServiceUnitTest; |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 106 | |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 107 | static bool checkDumpPermission(); |
| 108 | |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 109 | 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 Shan | 9549368 | 2023-03-21 18:00:17 -0700 | [diff] [blame] | 120 | 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 Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 124 | // Default wait time before retry connecting to remote access client is 10s. |
| 125 | size_t mRetryWaitInMs = 10'000; |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 126 | std::shared_ptr<DebugRemoteTaskCallback> mDebugCallback; |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 127 | |
Yu Shan | 194757d | 2023-04-19 11:44:35 -0700 | [diff] [blame] | 128 | std::thread mInjectDebugTaskThread; |
| 129 | |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 130 | void runTaskLoop(); |
| 131 | void maybeStartTaskLoop(); |
| 132 | void maybeStopTaskLoop(); |
Eric Jeong | 6c3a1d8 | 2023-03-16 00:45:40 -0700 | [diff] [blame] | 133 | ndk::ScopedAStatus getVehicleIdWithClient( |
| 134 | android::frameworks::automotive::vhal::IVhalClient& client, std::string* vehicleId); |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 135 | |
| 136 | void setRetryWaitInMs(size_t retryWaitInMs) { mRetryWaitInMs = retryWaitInMs; } |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 137 | void dumpHelp(int fd); |
Yu Shan | 9549368 | 2023-03-21 18:00:17 -0700 | [diff] [blame] | 138 | void printCurrentStatus(int fd); |
| 139 | std::string clientIdToTaskCountToStringLocked() REQUIRES(mLock); |
| 140 | void debugInjectTask(int fd, std::string_view clientId, std::string_view taskData); |
Yu Shan | 194757d | 2023-04-19 11:44:35 -0700 | [diff] [blame] | 141 | void debugInjectTaskNextReboot(int fd, std::string_view clientId, std::string_view taskData, |
| 142 | const char* latencyInSecStr); |
Yu Shan | 9549368 | 2023-03-21 18:00:17 -0700 | [diff] [blame] | 143 | void updateGrpcConnected(bool connected); |
| 144 | android::base::Result<void> deliverRemoteTaskThroughCallback(const std::string& clientId, |
| 145 | std::string_view taskData); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | } // namespace remoteaccess |
| 149 | } // namespace automotive |
| 150 | } // namespace hardware |
| 151 | } // namespace android |