1 /*
2  * Copyright (C) 2019 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 #pragma once
17 
18 #include <android/hardware/health/2.1/IHealth.h>
19 #include <android/hardware/health/2.1/IHealthInfoCallback.h>
20 
21 using ::android::sp;
22 using ::android::hardware::Return;
23 using ::android::hidl::base::V1_0::IBase;
24 
25 namespace android {
26 namespace hardware {
27 namespace health {
28 namespace V2_1 {
29 namespace implementation {
30 
31 // Wraps an IHealthInfoCallback.
32 class Callback {
33   public:
~Callback()34     virtual ~Callback() {}
35     virtual Return<void> Notify(const HealthInfo&) = 0;
36     virtual sp<IBase> Get() = 0;
37 };
38 
39 class Callback_2_0 : public Callback {
40   public:
Callback_2_0(const sp<V2_0::IHealthInfoCallback> & callback)41     Callback_2_0(const sp<V2_0::IHealthInfoCallback>& callback) : callback_(callback) {}
Notify(const HealthInfo & info)42     Return<void> Notify(const HealthInfo& info) override {
43         return callback_->healthInfoChanged(info.legacy);
44     }
Get()45     sp<IBase> Get() override { return callback_; }
46 
47   private:
48     sp<V2_0::IHealthInfoCallback> callback_;
49 };
50 
51 class Callback_2_1 : public Callback {
52   public:
Callback_2_1(const sp<IHealthInfoCallback> & callback)53     Callback_2_1(const sp<IHealthInfoCallback>& callback) : callback_(callback) {}
Notify(const HealthInfo & info)54     Return<void> Notify(const HealthInfo& info) override {
55         return callback_->healthInfoChanged_2_1(info);
56     }
Get()57     sp<IBase> Get() override { return callback_; }
58 
59   private:
60     sp<IHealthInfoCallback> callback_;
61 };
62 
Wrap(const sp<V2_0::IHealthInfoCallback> & callback_2_0)63 inline std::unique_ptr<Callback> Wrap(const sp<V2_0::IHealthInfoCallback>& callback_2_0) {
64     auto callback_2_1 = IHealthInfoCallback::castFrom(callback_2_0).withDefault(nullptr);
65     if (callback_2_1) return std::make_unique<Callback_2_1>(callback_2_1);
66     return std::make_unique<Callback_2_0>(callback_2_0);
67 }
68 
69 }  // namespace implementation
70 }  // namespace V2_1
71 }  // namespace health
72 }  // namespace hardware
73 }  // namespace android
74