blob: 806440a2dff59231ba513dfa0a7283ab896fb917 [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>
21#include <aidl/android/hardware/automotive/remoteaccess/IRemoteTaskCallback.h>
Yu Shanb138c422022-09-06 19:11:23 -070022#include <android-base/thread_annotations.h>
Yu Shane2974562022-08-10 18:37:03 -070023#include <wakeup_client.grpc.pb.h>
Yu Shan562c0f82022-08-04 17:10:38 -070024
25#include <string>
Yu Shane2974562022-08-10 18:37:03 -070026#include <thread>
Yu Shan562c0f82022-08-04 17:10:38 -070027
28namespace android {
29namespace hardware {
30namespace automotive {
31namespace remoteaccess {
32
33class RemoteAccessService
34 : public aidl::android::hardware::automotive::remoteaccess::BnRemoteAccess {
35 public:
Yu Shanb138c422022-09-06 19:11:23 -070036 explicit RemoteAccessService(WakeupClient::StubInterface* grpcStub);
37
38 ~RemoteAccessService();
Yu Shane2974562022-08-10 18:37:03 -070039
Yu Shan562c0f82022-08-04 17:10:38 -070040 ndk::ScopedAStatus getDeviceId(std::string* deviceId) override;
41
Yu Shane2974562022-08-10 18:37:03 -070042 ndk::ScopedAStatus getWakeupServiceName(std::string* wakeupServiceName) override;
Yu Shan562c0f82022-08-04 17:10:38 -070043
44 ndk::ScopedAStatus setRemoteTaskCallback(
45 const std::shared_ptr<
46 aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>&
47 callback) override;
48
49 ndk::ScopedAStatus clearRemoteTaskCallback() override;
50
51 ndk::ScopedAStatus notifyApStateChange(
52 const aidl::android::hardware::automotive::remoteaccess::ApState& newState) override;
Yu Shane2974562022-08-10 18:37:03 -070053
54 private:
Yu Shanb138c422022-09-06 19:11:23 -070055 // For testing.
56 friend class RemoteAccessServiceUnitTest;
Yu Shane2974562022-08-10 18:37:03 -070057
Yu Shanb138c422022-09-06 19:11:23 -070058 WakeupClient::StubInterface* mGrpcStub;
59 std::thread mThread;
60 std::mutex mLock;
61 std::condition_variable mCv;
62 std::shared_ptr<aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback>
63 mRemoteTaskCallback GUARDED_BY(mLock);
64 std::unique_ptr<grpc::ClientContext> mGetRemoteTasksContext GUARDED_BY(mLock);
65 // Associated with mCv to notify the task loop to stop waiting and exit.
66 bool mTaskWaitStopped GUARDED_BY(mLock);
67 // A mutex to make sure startTaskLoop does not overlap with stopTaskLoop.
68 std::mutex mStartStopTaskLoopLock;
69 bool mTaskLoopRunning GUARDED_BY(mStartStopTaskLoopLock);
70 // Default wait time before retry connecting to remote access client is 10s.
71 size_t mRetryWaitInMs = 10'000;
72
73 void runTaskLoop();
74 void maybeStartTaskLoop();
75 void maybeStopTaskLoop();
76
77 void setRetryWaitInMs(size_t retryWaitInMs) { mRetryWaitInMs = retryWaitInMs; }
Yu Shan562c0f82022-08-04 17:10:38 -070078};
79
80} // namespace remoteaccess
81} // namespace automotive
82} // namespace hardware
83} // namespace android