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 | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 24 | #include <android-base/thread_annotations.h> |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 25 | #include <android/binder_auto_utils.h> |
| 26 | #include <utils/SystemClock.h> |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 27 | #include <wakeup_client.grpc.pb.h> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 28 | |
| 29 | #include <string> |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 30 | #include <thread> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | namespace automotive { |
| 35 | namespace remoteaccess { |
| 36 | |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 37 | // A IRemoteTaskCallback implementation for debug purpose. |
| 38 | class 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 | |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 58 | class RemoteAccessService |
| 59 | : public aidl::android::hardware::automotive::remoteaccess::BnRemoteAccess { |
| 60 | public: |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 61 | explicit RemoteAccessService(WakeupClient::StubInterface* grpcStub); |
| 62 | |
| 63 | ~RemoteAccessService(); |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 64 | |
Eric Jeong | 6c3a1d8 | 2023-03-16 00:45:40 -0700 | [diff] [blame] | 65 | ndk::ScopedAStatus getVehicleId(std::string* vehicleId) override; |
| 66 | |
| 67 | ndk::ScopedAStatus getProcessorId(std::string* processorId) override; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 68 | |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 69 | ndk::ScopedAStatus getWakeupServiceName(std::string* wakeupServiceName) override; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 70 | |
| 71 | ndk::ScopedAStatus setRemoteTaskCallback( |
| 72 | const std::shared_ptr< |
| 73 | aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>& |
| 74 | callback) override; |
| 75 | |
| 76 | ndk::ScopedAStatus clearRemoteTaskCallback() override; |
| 77 | |
| 78 | ndk::ScopedAStatus notifyApStateChange( |
| 79 | const aidl::android::hardware::automotive::remoteaccess::ApState& newState) override; |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 80 | |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 81 | binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; |
| 82 | |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 83 | private: |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 84 | // For testing. |
| 85 | friend class RemoteAccessServiceUnitTest; |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 86 | |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 87 | static bool checkDumpPermission(); |
| 88 | |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 89 | WakeupClient::StubInterface* mGrpcStub; |
| 90 | std::thread mThread; |
| 91 | std::mutex mLock; |
| 92 | std::condition_variable mCv; |
| 93 | std::shared_ptr<aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback> |
| 94 | mRemoteTaskCallback GUARDED_BY(mLock); |
| 95 | std::unique_ptr<grpc::ClientContext> mGetRemoteTasksContext GUARDED_BY(mLock); |
| 96 | // Associated with mCv to notify the task loop to stop waiting and exit. |
| 97 | bool mTaskWaitStopped GUARDED_BY(mLock); |
| 98 | // A mutex to make sure startTaskLoop does not overlap with stopTaskLoop. |
| 99 | std::mutex mStartStopTaskLoopLock; |
Yu Shan | 9549368 | 2023-03-21 18:00:17 -0700 | [diff] [blame] | 100 | bool mTaskLoopRunning GUARDED_BY(mStartStopTaskLoopLock) = false; |
| 101 | bool mGrpcConnected GUARDED_BY(mLock) = false; |
| 102 | std::unordered_map<std::string, size_t> mClientIdToTaskCount GUARDED_BY(mLock); |
| 103 | |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 104 | // Default wait time before retry connecting to remote access client is 10s. |
| 105 | size_t mRetryWaitInMs = 10'000; |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 106 | std::shared_ptr<DebugRemoteTaskCallback> mDebugCallback; |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 107 | |
Yu Shan | 194757d | 2023-04-19 11:44:35 -0700 | [diff] [blame] | 108 | std::thread mInjectDebugTaskThread; |
| 109 | |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 110 | void runTaskLoop(); |
| 111 | void maybeStartTaskLoop(); |
| 112 | void maybeStopTaskLoop(); |
Eric Jeong | 6c3a1d8 | 2023-03-16 00:45:40 -0700 | [diff] [blame] | 113 | ndk::ScopedAStatus getVehicleIdWithClient( |
| 114 | android::frameworks::automotive::vhal::IVhalClient& client, std::string* vehicleId); |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 115 | |
| 116 | void setRetryWaitInMs(size_t retryWaitInMs) { mRetryWaitInMs = retryWaitInMs; } |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame] | 117 | void dumpHelp(int fd); |
Yu Shan | 9549368 | 2023-03-21 18:00:17 -0700 | [diff] [blame] | 118 | void printCurrentStatus(int fd); |
| 119 | std::string clientIdToTaskCountToStringLocked() REQUIRES(mLock); |
| 120 | void debugInjectTask(int fd, std::string_view clientId, std::string_view taskData); |
Yu Shan | 194757d | 2023-04-19 11:44:35 -0700 | [diff] [blame] | 121 | void debugInjectTaskNextReboot(int fd, std::string_view clientId, std::string_view taskData, |
| 122 | const char* latencyInSecStr); |
Yu Shan | 9549368 | 2023-03-21 18:00:17 -0700 | [diff] [blame] | 123 | void updateGrpcConnected(bool connected); |
| 124 | android::base::Result<void> deliverRemoteTaskThroughCallback(const std::string& clientId, |
| 125 | std::string_view taskData); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | } // namespace remoteaccess |
| 129 | } // namespace automotive |
| 130 | } // namespace hardware |
| 131 | } // namespace android |