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>
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;
32
error(const std::string & msg)33 void error(const std::string& msg) {
34 LOG(ERROR) << msg;
35 std::cerr << msg << std::endl;
36 }
37
parseArgs(int argc,char * argv[],unsigned int * color)38 int parseArgs(int argc, char* argv[], unsigned int* color) {
39 if (argc > 2) {
40 error("Usage: blank_screen [color]");
41 return -1;
42 }
43
44 if (argc > 1) {
45 char* col_ptr;
46
47 *color = strtoul(argv[1], &col_ptr, 0);
48 if (*col_ptr != '\0') {
49 error("Failed to convert " + std::string(argv[1]) + " to number");
50 return -1;
51 }
52
53 return 0;
54 }
55
56 *color = 0u;
57 return 0;
58 }
59
setToColorAidl(sp<aidl::ILights> hal,unsigned int color)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
setToColorHidl(sp<V2_0::ILight> hal,unsigned int color)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 " +
93 std::to_string(static_cast<int>(type)));
94 }
95 }
96 });
97 }
98
main(int argc,char * argv[])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;
120 }
121