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 <array>
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <linux/i2c-dev.h>
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30
31 #include <aidl/android/hardware/light/BnLights.h>
32 #include <android-base/logging.h>
33 #include <android/binder_manager.h>
34 #include <android/binder_process.h>
35
36 using ::aidl::android::hardware::light::BnLights;
37 using ::aidl::android::hardware::light::HwLight;
38 using ::aidl::android::hardware::light::HwLightState;
39 using ::aidl::android::hardware::light::ILights;
40 using ::aidl::android::hardware::light::LightType;
41 using ::ndk::ScopedAStatus;
42 using ::ndk::SharedRefBase;
43
44 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
45
46 struct LightAddress {
47 uint8_t red;
48 uint8_t green;
49 uint8_t blue;
50 };
51
52 char const* const RED_LED_FILE = "/sys/class/leds/sei610:red:power/brightness";
53
54 char const* const BLUE_LED_FILE = "/sys/class/leds/sei610:blue:bt/brightness";
55
56 std::array<LightAddress, 4> const lightAddrs = {
57 LightAddress{0x20, 0x21, 0x22}, LightAddress{0x23, 0x24, 0x25},
58 LightAddress{0x26, 0x27, 0x28}, LightAddress{0x29, 0x2A, 0x2B}};
59
60 #define LA_P0_ENABLE 0x12
61 #define LA_P1_ENABLE 0x13
62 #define LA_RESET_ADDR 0x7F
63 char const* const ARRAY_LED_DEVICE = "/dev/i2c-0";
64 const int i2c_dev_addr = (0xB6 >> 1); /* 0x5B */
65
sys_write_int(int fd,int value)66 static int sys_write_int(int fd, int value) {
67 char buffer[16];
68 size_t bytes;
69 ssize_t amount;
70
71 bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
72 if (bytes >= sizeof(buffer)) return -EINVAL;
73 amount = write(fd, buffer, bytes);
74 return amount == -1 ? -errno : 0;
75 }
76
write8reg8(int fd,uint8_t regaddr,uint8_t cmd)77 static int write8reg8(int fd, uint8_t regaddr, uint8_t cmd) {
78 uint8_t buf[2];
79
80 buf[0] = regaddr;
81 buf[1] = cmd;
82 if (write(fd, buf, 2) != 2) return -1;
83 return 0;
84 }
85
86 class Lights : public BnLights {
87 private:
88 std::vector<HwLight> availableLights;
89
addLight(LightType const type,int const ordinal)90 void addLight(LightType const type, int const ordinal) {
91 HwLight light{};
92 light.id = availableLights.size();
93 light.type = type;
94 light.ordinal = ordinal;
95 availableLights.emplace_back(light);
96 }
97
rgbToBrightness(int color)98 int rgbToBrightness(int color) {
99 int const r = ((color >> 16) & 0xFF) * 77 / 255;
100 int const g = ((color >> 8) & 0xFF) * 150 / 255;
101 int const b = (color & 0xFF) * 29 / 255;
102 return (r << 16) | (g << 8) | b;
103 }
104
writeLedArray(const char * path,LightAddress const & addr,int color)105 int writeLedArray(const char* path, LightAddress const& addr, int color) {
106 int const fd = open(path, O_RDWR);
107 if (fd < 0) {
108 LOG(ERROR) << "COULD NOT OPEN ARRAY_LED_DEVICE " << path;
109 return fd;
110 }
111 if (ioctl(fd, I2C_SLAVE, i2c_dev_addr) < 0) {
112 LOG(ERROR) << "Error setting slave addr";
113 close(fd);
114 return -errno;
115 }
116
117 write8reg8(fd, addr.red, ((color >> 16) & 0xFF));
118 write8reg8(fd, addr.green, ((color >> 8) & 0xFF));
119 write8reg8(fd, addr.blue, (color)&0xFF);
120
121 write8reg8(fd, LA_P0_ENABLE, 0x00);
122 write8reg8(fd, LA_P1_ENABLE, 0x00);
123
124 close(fd);
125 return 0;
126 }
127
writeLed(const char * path,int color)128 void writeLed(const char* path, int color) {
129 int fd = open(path, O_WRONLY);
130 if (fd < 0) {
131 LOG(ERROR) << "COULD NOT OPEN LED_DEVICE " << path;
132 return;
133 }
134
135 sys_write_int(fd, color);
136 close(fd);
137 }
138
139 public:
Lights()140 Lights() : BnLights() {
141 pthread_mutex_init(&g_lock, NULL);
142
143 addLight(LightType::BACKLIGHT, 0);
144 addLight(LightType::KEYBOARD, 0);
145 addLight(LightType::BUTTONS, 0);
146 addLight(LightType::BATTERY, 0);
147 addLight(LightType::NOTIFICATIONS, 0);
148 addLight(LightType::ATTENTION, 0);
149 addLight(LightType::BLUETOOTH, 0);
150 addLight(LightType::WIFI, 0);
151
152 for (int i = 0; i < 4; i++) {
153 addLight(LightType::MICROPHONE, i);
154 }
155
156 writeLed(RED_LED_FILE, rgbToBrightness(0x00000000));
157 writeLed(BLUE_LED_FILE, rgbToBrightness(0xFFFFFFFF));
158 }
159
setLightState(int id,const HwLightState & state)160 ScopedAStatus setLightState(int id, const HwLightState& state) override {
161 if (!(0 <= id && id < availableLights.size())) {
162 LOG(ERROR) << "Light id " << (int32_t)id << " does not exist.";
163 return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
164 }
165
166 int const color = rgbToBrightness(state.color);
167 HwLight const& light = availableLights[id];
168
169 int ret = 0;
170
171 switch (light.type) {
172 case LightType::MICROPHONE:
173 ret = writeLedArray(ARRAY_LED_DEVICE, lightAddrs[light.ordinal], color);
174 break;
175 case LightType::BATTERY:
176 writeLed(RED_LED_FILE, color);
177 break;
178 case LightType::BLUETOOTH:
179 writeLed(BLUE_LED_FILE, color);
180 break;
181 default:
182 break;
183 }
184
185 if (ret == 0) {
186 return ScopedAStatus::ok();
187 } else {
188 return ScopedAStatus::fromServiceSpecificError(ret);
189 }
190 }
191
getLights(std::vector<HwLight> * lights)192 ScopedAStatus getLights(std::vector<HwLight>* lights) override {
193 for (auto i = availableLights.begin(); i != availableLights.end(); i++) {
194 lights->push_back(*i);
195 }
196 return ScopedAStatus::ok();
197 }
198 };
199
main()200 int main() {
201 ABinderProcess_setThreadPoolMaxThreadCount(0);
202
203 std::shared_ptr<Lights> light = SharedRefBase::make<Lights>();
204
205 const std::string instance = std::string() + ILights::descriptor + "/default";
206 binder_status_t status = AServiceManager_addService(light->asBinder().get(), instance.c_str());
207
208 if (status != STATUS_OK) {
209 LOG(ERROR) << "Could not register" << instance;
210 // should abort, but don't want crash loop for local testing
211 }
212
213 ABinderProcess_joinThreadPool();
214
215 return 1; // should not reach
216 }
217