Steven Moreland | 64a7afb | 2018-01-19 12:59:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <iostream> |
| 18 | #include <string> |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android/hardware/light/2.0/ILight.h> |
Steven Moreland | bb89a95 | 2019-07-25 17:13:52 -0700 | [diff] [blame] | 22 | #include <android/hardware/light/ILights.h> |
| 23 | #include <binder/IServiceManager.h> |
| 24 | |
| 25 | using android::sp; |
| 26 | using android::waitForVintfService; |
| 27 | using android::binder::Status; |
| 28 | using android::hardware::hidl_vec; |
| 29 | |
| 30 | namespace V2_0 = android::hardware::light::V2_0; |
| 31 | namespace aidl = android::hardware::light; |
Steven Moreland | 64a7afb | 2018-01-19 12:59:09 -0800 | [diff] [blame] | 32 | |
| 33 | void error(const std::string& msg) { |
| 34 | LOG(ERROR) << msg; |
| 35 | std::cerr << msg << std::endl; |
| 36 | } |
| 37 | |
Steven Moreland | bb89a95 | 2019-07-25 17:13:52 -0700 | [diff] [blame] | 38 | int parseArgs(int argc, char* argv[], unsigned int* color) { |
Dmitry Shmidt | 34ed387 | 2019-10-07 12:43:36 -0700 | [diff] [blame] | 39 | if (argc > 2) { |
| 40 | error("Usage: blank_screen [color]"); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | if (argc > 1) { |
| 45 | char* col_ptr; |
Dmitry Shmidt | 34ed387 | 2019-10-07 12:43:36 -0700 | [diff] [blame] | 46 | |
Steven Moreland | bb89a95 | 2019-07-25 17:13:52 -0700 | [diff] [blame] | 47 | *color = strtoul(argv[1], &col_ptr, 0); |
Dmitry Shmidt | 34ed387 | 2019-10-07 12:43:36 -0700 | [diff] [blame] | 48 | if (*col_ptr != '\0') { |
| 49 | error("Failed to convert " + std::string(argv[1]) + " to number"); |
| 50 | return -1; |
| 51 | } |
Steven Moreland | bb89a95 | 2019-07-25 17:13:52 -0700 | [diff] [blame] | 52 | |
| 53 | return 0; |
Dmitry Shmidt | 34ed387 | 2019-10-07 12:43:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Steven Moreland | bb89a95 | 2019-07-25 17:13:52 -0700 | [diff] [blame] | 56 | *color = 0u; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | void setToColorAidl(sp<aidl::ILights> hal, unsigned int color) { |
| 61 | static aidl::HwLightState off; |
| 62 | off.color = color; |
| 63 | off.flashMode = aidl::FlashMode::NONE; |
| 64 | off.brightnessMode = aidl::BrightnessMode::USER; |
| 65 | |
| 66 | std::vector<aidl::HwLight> lights; |
| 67 | Status status = hal->getLights(&lights); |
| 68 | if (!status.isOk()) { |
| 69 | error("Failed to list lights"); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | for (auto light : lights) { |
| 74 | Status setStatus = hal->setLightState(light.id, off); |
| 75 | if (!setStatus.isOk()) { |
| 76 | error("Failed to shut off light id " + std::to_string(light.id)); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void setToColorHidl(sp<V2_0::ILight> hal, unsigned int color) { |
| 82 | static V2_0::LightState off = { |
| 83 | .color = color, |
| 84 | .flashMode = V2_0::Flash::NONE, |
| 85 | .brightnessMode = V2_0::Brightness::USER, |
| 86 | }; |
| 87 | |
| 88 | hal->getSupportedTypes([&](const hidl_vec<V2_0::Type>& types) { |
| 89 | for (auto type : types) { |
| 90 | V2_0::Status ret = hal->setLight(type, off); |
| 91 | if (ret != V2_0::Status::SUCCESS) { |
| 92 | error("Failed to shut off light for type " + |
Steven Moreland | b944d69 | 2018-03-16 10:27:18 -0700 | [diff] [blame] | 93 | std::to_string(static_cast<int>(type))); |
| 94 | } |
| 95 | } |
| 96 | }); |
Steven Moreland | bb89a95 | 2019-07-25 17:13:52 -0700 | [diff] [blame] | 97 | } |
Steven Moreland | b944d69 | 2018-03-16 10:27:18 -0700 | [diff] [blame] | 98 | |
Steven Moreland | bb89a95 | 2019-07-25 17:13:52 -0700 | [diff] [blame] | 99 | int main(int argc, char* argv[]) { |
| 100 | unsigned int inputColor; |
| 101 | int result = parseArgs(argc, argv, &inputColor); |
| 102 | if (result != 0) { |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | auto aidlHal = waitForVintfService<aidl::ILights>(); |
| 107 | if (aidlHal != nullptr) { |
| 108 | setToColorAidl(aidlHal, inputColor); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | sp<V2_0::ILight> hidlHal = V2_0::ILight::getService(); |
| 113 | if (hidlHal != nullptr) { |
| 114 | setToColorHidl(hidlHal, inputColor); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | error("Could not retrieve light service."); |
| 119 | return -1; |
Steven Moreland | 64a7afb | 2018-01-19 12:59:09 -0800 | [diff] [blame] | 120 | } |