| Yifan Hong | 7dcf7b0 | 2019-10-08 17:27:11 -0700 | [diff] [blame] | 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 |  | 
|  | 17 | #include "charger_utils.h" | 
|  | 18 |  | 
|  | 19 | #include <android-base/logging.h> | 
|  | 20 | #include <android/hidl/manager/1.0/IServiceManager.h> | 
|  | 21 | #include <health/utils.h> | 
|  | 22 | #include <health2impl/Health.h> | 
|  | 23 | #include <hidl/ServiceManagement.h> | 
|  | 24 |  | 
|  | 25 | using android::hardware::getPassthroughServiceManager; | 
|  | 26 | using android::hidl::base::V1_0::IBase; | 
|  | 27 | using android::hidl::manager::V1_0::IServiceManager; | 
|  | 28 |  | 
|  | 29 | namespace android { | 
|  | 30 | namespace hardware { | 
|  | 31 | namespace health { | 
|  | 32 | sp<V2_1::IHealth> GetPassthroughHealthImpl() { | 
|  | 33 | // Not using getService() because there is no hwservicemanager in charger mode. | 
|  | 34 | sp<IServiceManager> pm = getPassthroughServiceManager(); | 
|  | 35 | if (pm == nullptr) { | 
|  | 36 | LOG(WARNING) << "Cannot get passthrough service manager."; | 
|  | 37 | return nullptr; | 
|  | 38 | } | 
|  | 39 | sp<IBase> base = pm->get(V2_0::IHealth::descriptor, "default"); | 
|  | 40 | if (base == nullptr) { | 
|  | 41 | LOG(WARNING) << "Cannot find passthrough implementation of health 2.0 HAL for instance " | 
|  | 42 | "'default' on the device."; | 
|  | 43 | return nullptr; | 
|  | 44 | } | 
|  | 45 | sp<V2_1::IHealth> service = V2_1::IHealth::castFrom(base); | 
|  | 46 | if (service == nullptr) { | 
|  | 47 | LOG(WARNING) | 
|  | 48 | << "Cannot cast passthrough implementation of health 2.0 HAL to 2.1 for instance " | 
|  | 49 | "'default' on the device."; | 
|  | 50 | return nullptr; | 
|  | 51 | } | 
|  | 52 | return service; | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | sp<V2_1::IHealth> GetPassthroughHealth() { | 
|  | 56 | auto impl = GetPassthroughHealthImpl(); | 
|  | 57 | if (impl == nullptr) { | 
|  | 58 | LOG(WARNING) << "Charger uses system defaults."; | 
|  | 59 | auto config = std::make_unique<healthd_config>(); | 
|  | 60 | InitHealthdConfig(config.get()); | 
|  | 61 | impl = new V2_1::implementation::Health(std::move(config)); | 
|  | 62 | } | 
|  | 63 | return impl; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | }  // namespace health | 
|  | 67 | }  // namespace hardware | 
|  | 68 | }  // namespace android |