summaryrefslogtreecommitdiff
path: root/light
diff options
context:
space:
mode:
authordev-harsh1998 <harshit.india@linaro.org>2018-05-02 17:12:53 +0530
committerIsaac Chen <isaacchen@isaacchen.cn>2018-07-13 00:03:17 +0000
commit9cfa4c0be9451ea33d54e0173b9b4978d7ca9906 (patch)
treea07eda51bceaeea4dfbdcbfe81f2482a2ee5d034 /light
parent5d5c84331b465e5c1428c68ff62dee0cad02bf41 (diff)
wayne: Convert light HAL into a HIDL service
Signed-off-by: dev-harsh1998 <harshit.india@linaro.org>
Diffstat (limited to 'light')
-rw-r--r--light/Android.bp32
-rw-r--r--light/Light.cpp132
-rw-r--r--light/Light.h56
-rw-r--r--light/android.hardware.light@2.0-service.xiaomi_wayne.rc9
-rw-r--r--light/service.cpp50
5 files changed, 279 insertions, 0 deletions
diff --git a/light/Android.bp b/light/Android.bp
new file mode 100644
index 0000000..a60fcf8
--- /dev/null
+++ b/light/Android.bp
@@ -0,0 +1,32 @@
+//
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_binary {
+ defaults: ["hidl_defaults"],
+ name: "android.hardware.light@2.0-service.xiaomi_wayne",
+ init_rc: ["android.hardware.light@2.0-service.xiaomi_wayne.rc"],
+ srcs: ["service.cpp", "Light.cpp"],
+ shared_libs: [
+ "libhardware",
+ "libhidlbase",
+ "libhidltransport",
+ "liblog",
+ "libhwbinder",
+ "libutils",
+ "android.hardware.light@2.0",
+ ],
+ relative_install_path : "hw",
+ proprietary: true,
+}
diff --git a/light/Light.cpp b/light/Light.cpp
new file mode 100644
index 0000000..c170ae2
--- /dev/null
+++ b/light/Light.cpp
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//Author := dev_harsh1998, Isaac Chen
+
+#define LOG_TAG "android.hardware.light@2.0-service.xiaomi_wayne"
+
+#include <log/log.h>
+#include <fstream>
+#include "Light.h"
+
+namespace android {
+namespace hardware {
+namespace light {
+namespace V2_0 {
+namespace implementation {
+
+#define LEDS "/sys/class/leds/"
+#define LCD_LED LEDS "lcd-backlight/"
+#define BRIGHTNESS "brightness"
+#define RED LEDS "red/"
+#define BLINK "blink"
+
+/*
+ * Write value to path and close file.
+ */
+static void set(std::string path, std::string value) {
+ std::ofstream file(path);
+ file << value;
+}
+
+static void set(std::string path, int value) {
+ set(path, std::to_string(value));
+}
+
+static void handleWayneBacklight(const LightState& state) {
+ uint32_t brightness = state.color & 0xFF;
+ set(LCD_LED BRIGHTNESS, brightness);
+}
+
+static void handleWayneNotification(const LightState& state) {
+ uint32_t redBrightness, brightness, blink;
+
+ /*
+ * Extract brightness from RGB.
+ */
+ redBrightness = (state.color >> 16) & 0xFF;
+ brightness = (state.color >> 24) & 0xFF;
+
+ /*
+ * Scale RGB brightness if the Alpha brightness is not 0xFF.
+ */
+ if (brightness != 0xFF) {
+ redBrightness = (redBrightness * brightness) / 0xFF;
+ }
+
+ /* Turn off the leds (initially) */
+ set(RED BRIGHTNESS, 0);
+ set(RED BLINK, 0);
+
+ if (state.flashMode == Flash::TIMED) {
+ uint32_t onMS = state.flashOnMs;
+ uint32_t offMS = state.flashOffMs;
+
+ if (onMS > 0 && offMS > 0) {
+ blink = 1;
+ } else {
+ blink = 0;
+ }
+
+ /* Red(Actually White) */
+ set(RED BLINK, blink);
+ set(RED BRIGHTNESS, 0);
+
+ } else {
+ set(RED BRIGHTNESS, redBrightness);
+ }
+}
+
+static std::map<Type, std::function<void(const LightState&)>> lights = {
+ {Type::BACKLIGHT, handleWayneBacklight},
+ {Type::NOTIFICATIONS, handleWayneNotification},
+ {Type::BATTERY, handleWayneNotification},
+ {Type::ATTENTION, handleWayneNotification},
+};
+
+Light::Light() {}
+
+Return<Status> Light::setLight(Type type, const LightState& state) {
+ auto it = lights.find(type);
+
+ if (it == lights.end()) {
+ return Status::LIGHT_NOT_SUPPORTED;
+ }
+
+ /*
+ * Lock global mutex until light state is updated.
+ */
+
+ std::lock_guard<std::mutex> lock(globalLock);
+ it->second(state);
+ return Status::SUCCESS;
+}
+
+Return<void> Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) {
+ std::vector<Type> types;
+
+ for (auto const& light : lights) types.push_back(light.first);
+
+ _hidl_cb(types);
+
+ return Void();
+}
+
+} // namespace implementation
+} // namespace V2_0
+} // namespace light
+} // namespace hardware
+} // namespace android
diff --git a/light/Light.h b/light/Light.h
new file mode 100644
index 0000000..311b7de
--- /dev/null
+++ b/light/Light.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2017 The LineageOS Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H
+#define ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H
+
+#include <android/hardware/light/2.0/ILight.h>
+#include <hardware/lights.h>
+#include <hidl/Status.h>
+#include <map>
+#include <mutex>
+
+namespace android {
+namespace hardware {
+namespace light {
+namespace V2_0 {
+namespace implementation {
+
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::light::V2_0::ILight;
+using ::android::hardware::light::V2_0::LightState;
+using ::android::hardware::light::V2_0::Status;
+using ::android::hardware::light::V2_0::Type;
+
+class Light : public ILight {
+ public:
+ Light();
+
+ Return<Status> setLight(Type type, const LightState& state) override;
+ Return<void> getSupportedTypes(getSupportedTypes_cb _hidl_cb) override;
+
+ private:
+ std::mutex globalLock;
+};
+
+} // namespace implementation
+} // namespace V2_0
+} // namespace light
+} // namespace hardware
+} // namespace android
+
+#endif // ANDROID_HARDWARE_LIGHT_V2_0_LIGHT_H
diff --git a/light/android.hardware.light@2.0-service.xiaomi_wayne.rc b/light/android.hardware.light@2.0-service.xiaomi_wayne.rc
new file mode 100644
index 0000000..0ebe09b
--- /dev/null
+++ b/light/android.hardware.light@2.0-service.xiaomi_wayne.rc
@@ -0,0 +1,9 @@
+on boot
+ # Notification LED
+ chown system system /sys/class/leds/red/blink
+ chown system system /sys/class/leds/red/brightness
+
+service light-hal-2-0 /vendor/bin/hw/android.hardware.light@2.0-service.xiaomi_wayne
+ class hal
+ user system
+ group system
diff --git a/light/service.cpp b/light/service.cpp
new file mode 100644
index 0000000..abefdc0
--- /dev/null
+++ b/light/service.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "android.hardware.light@2.0-service.xiaomi_wayne"
+
+#include <hidl/HidlTransportSupport.h>
+
+#include "Light.h"
+
+using android::hardware::configureRpcThreadpool;
+using android::hardware::joinRpcThreadpool;
+
+using android::hardware::light::V2_0::ILight;
+using android::hardware::light::V2_0::implementation::Light;
+
+using android::OK;
+using android::sp;
+using android::status_t;
+
+int main() {
+ android::sp<ILight> service = new Light();
+
+ configureRpcThreadpool(1, true);
+
+ status_t status = service->registerAsService();
+ if (status != OK) {
+ ALOGE("Cannot register Light HAL service.");
+ return 1;
+ }
+
+ ALOGI("Light HAL service ready.");
+
+ joinRpcThreadpool();
+
+ ALOGI("Light HAL service failed to join thread pool.");
+ return 1;
+}