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 | |
| 19 | #include <aidl/android/hardware/automotive/remoteaccess/ApState.h> |
| 20 | #include <aidl/android/hardware/automotive/remoteaccess/BnRemoteAccess.h> |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame^] | 21 | #include <aidl/android/hardware/automotive/remoteaccess/BnRemoteTaskCallback.h> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 22 | #include <aidl/android/hardware/automotive/remoteaccess/IRemoteTaskCallback.h> |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame^] | 24 | #include <android/binder_auto_utils.h> |
| 25 | #include <utils/SystemClock.h> |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 26 | #include <wakeup_client.grpc.pb.h> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 27 | |
| 28 | #include <string> |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 29 | #include <thread> |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace automotive { |
| 34 | namespace remoteaccess { |
| 35 | |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame^] | 36 | // A IRemoteTaskCallback implementation for debug purpose. |
| 37 | class 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 Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 57 | class RemoteAccessService |
| 58 | : public aidl::android::hardware::automotive::remoteaccess::BnRemoteAccess { |
| 59 | public: |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 60 | explicit RemoteAccessService(WakeupClient::StubInterface* grpcStub); |
| 61 | |
| 62 | ~RemoteAccessService(); |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 63 | |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 64 | ndk::ScopedAStatus getDeviceId(std::string* deviceId) override; |
| 65 | |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 66 | ndk::ScopedAStatus getWakeupServiceName(std::string* wakeupServiceName) override; |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 67 | |
| 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 Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 77 | |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame^] | 78 | binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; |
| 79 | |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 80 | private: |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 81 | // For testing. |
| 82 | friend class RemoteAccessServiceUnitTest; |
Yu Shan | e297456 | 2022-08-10 18:37:03 -0700 | [diff] [blame] | 83 | |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame^] | 84 | static bool checkDumpPermission(); |
| 85 | |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 86 | 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 Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame^] | 100 | std::shared_ptr<DebugRemoteTaskCallback> mDebugCallback; |
Yu Shan | b138c42 | 2022-09-06 19:11:23 -0700 | [diff] [blame] | 101 | |
| 102 | void runTaskLoop(); |
| 103 | void maybeStartTaskLoop(); |
| 104 | void maybeStopTaskLoop(); |
| 105 | |
| 106 | void setRetryWaitInMs(size_t retryWaitInMs) { mRetryWaitInMs = retryWaitInMs; } |
Yu Shan | dc54b8c | 2022-09-14 17:48:15 -0700 | [diff] [blame^] | 107 | void dumpHelp(int fd); |
Yu Shan | 562c0f8 | 2022-08-04 17:10:38 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace remoteaccess |
| 111 | } // namespace automotive |
| 112 | } // namespace hardware |
| 113 | } // namespace android |