on xenial help sign in
Test and use hfd if Android SDK indicates it
e0e4713Florian Leeber
plugins/Lights/CMakeLists.txt+5−1
@@ -1,5 +1,6 @@
1pkg_check_modules(ANDROID_HEADERS REQUIRED android-headers)
1pkg_check_modules(ANDROID_HEADERS REQUIRED android-headers)
2pkg_check_modules(ANDROID_HARDWARE REQUIRED libhardware)
2pkg_check_modules(ANDROID_HARDWARE REQUIRED libhardware)
3+pkg_check_modules(ANDROID_PROPERTIES REQUIRED libandroid-properties)
3
4
4include_directories(
5include_directories(
5 SYSTEM
6 SYSTEM
@@ -9,11 +10,14 @@ include_directories(
9add_library(Lights-qml MODULE
10add_library(Lights-qml MODULE
10 plugin.cpp
11 plugin.cpp
11 Lights.cpp
12 Lights.cpp
13+ LegacyLights.cpp
14+ HfdLights.cpp
12 )
15 )
13
16
14target_link_libraries(Lights-qml
17target_link_libraries(Lights-qml
15− Qt5::Qml Qt5::Gui
18+ Qt5::Qml Qt5::Gui Qt5::DBus
16 ${ANDROID_HARDWARE_LIBRARIES}
19 ${ANDROID_HARDWARE_LIBRARIES}
20+ ${ANDROID_PROPERTIES_LIBRARIES}
17 )
21 )
18
22
19add_unity8_plugin(Lights 0.1 Lights TARGETS Lights-qml)
23add_unity8_plugin(Lights 0.1 Lights TARGETS Lights-qml)
plugins/Lights/HfdLights.cppnew file+63
@@ -0,0 +1,63 @@
1+/*
2+ * Copyright (C) 2021 UBports Foundation.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Florian Leeber <florian@ubports.com>
17+ */
18+
19+#include "HfdLights.h"
20+#include <QtCore/QDebug>
21+#include <QProcess>
22+#include <QDBusArgument>
23+#include <QDBusConnection>
24+#include <iostream>
25+
26+extern "C" {
27+#include <string.h>
28+}
29+
30+HfdLights::HfdLights(QObject* parent)
31+ : Lights(parent)
32+{
33+ m_hfdInterface = new QDBusInterface(
34+ QStringLiteral(HFD_SERVICE_NAME),
35+ QStringLiteral(HFD_SERVICE_PATH),
36+ QStringLiteral(HFD_SERVICE_INTERFACE),
37+ QDBusConnection::systemBus(),
38+ this
39+ );
40+}
41+
42+bool HfdLights::init()
43+{
44+ // hfd does not need any initalization to be used
45+ return true;
46+}
47+
48+void HfdLights::turnOn()
49+{
50+ m_hfdInterface->call(QStringLiteral("setState"), 0);
51+ m_hfdInterface->call(QStringLiteral("setColor"), (quint32)m_color.rgba());
52+ m_hfdInterface->call(QStringLiteral("setOnMs"), m_onMs);
53+ m_hfdInterface->call(QStringLiteral("setOffMs"), m_offMs);
54+ m_hfdInterface->call(QStringLiteral("setState"), 1);
55+}
56+
57+void HfdLights::turnOff()
58+{
59+ m_hfdInterface->call(QStringLiteral("setState"), 0);
60+ m_hfdInterface->call(QStringLiteral("setColor"), (quint32)0);
61+ m_hfdInterface->call(QStringLiteral("setOnMs"), 0);
62+ m_hfdInterface->call(QStringLiteral("setOffMs"), 0);
63+}
plugins/Lights/HfdLights.hnew file+47
@@ -0,0 +1,47 @@
1+/*
2+ * Copyright (C) 2021 UBports Foundation.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Florian Leeber <florian@ubports.com>
17+ */
18+
19+#ifndef UNITY_HFDLIGHTS_H
20+#define UNITY_HFDLIGHTS_H
21+
22+#include <QtCore/QObject>
23+#include <QDBusInterface>
24+#include <QtGui/QColor>
25+#include "Lights.h"
26+
27+#define HFD_SERVICE_NAME "com.lomiri.hfd"
28+#define HFD_SERVICE_PATH "/com/lomiri/hfd"
29+#define HFD_SERVICE_INTERFACE "com.lomiri.hfd.Leds"
30+
31+class HfdLights: public Lights
32+{
33+ Q_OBJECT
34+
35+public:
36+ explicit HfdLights(QObject *parent = 0);
37+
38+protected:
39+ bool init() override;
40+ void turnOff() override;
41+ void turnOn() override;
42+
43+private:
44+ QDBusInterface* m_hfdInterface;
45+};
46+
47+#endif
plugins/Lights/LegacyLights.cppnew file+90
@@ -0,0 +1,90 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Renato Araujo Oliveira Filho <renato.filho@canonical.com>
17+ */
18+
19+#include "LegacyLights.h"
20+#include <QtCore/QDebug>
21+
22+extern "C" {
23+#include "android-hardware.h"
24+#include <string.h>
25+}
26+
27+LegacyLights::~LegacyLights()
28+{
29+ if (m_lightDevice) {
30+ hw_device_t* device = (hw_device_t*) m_lightDevice;
31+ device->close(device);
32+ }
33+}
34+
35+bool LegacyLights::init()
36+{
37+ if (m_lightDevice) {
38+ return true;
39+ }
40+
41+ int err;
42+ hw_module_t* module;
43+
44+ err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
45+ if (err == 0) {
46+ hw_device_t* device;
47+ err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
48+ if (err == 0) {
49+ m_lightDevice = (light_device_t*)device;
50+ turnOff();
51+ return true;
52+ } else {
53+ qWarning() << "Failed to access notification lights";
54+ }
55+ } else {
56+ qWarning() << "Failed to initialize lights hardware.";
57+ }
58+ return false;
59+}
60+
61+void LegacyLights::turnOn()
62+{
63+ // pulse
64+ light_state_t state;
65+ memset(&state, 0, sizeof(light_state_t));
66+ state.color = m_color.rgba();
67+ state.flashMode = LIGHT_FLASH_TIMED;
68+ state.flashOnMS = m_onMs;
69+ state.flashOffMS = m_offMs;
70+ state.brightnessMode = BRIGHTNESS_MODE_USER;
71+
72+ if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
73+ qWarning() << "Failed to turn the light off";
74+ }
75+}
76+
77+void LegacyLights::turnOff()
78+{
79+ light_state_t state;
80+ memset(&state, 0, sizeof(light_state_t));
81+ state.color = 0x00000000;
82+ state.flashMode = LIGHT_FLASH_NONE;
83+ state.flashOnMS = 0;
84+ state.flashOffMS = 0;
85+ state.brightnessMode = 0;
86+
87+ if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
88+ qWarning() << "Failed to turn the light off";
89+ }
90+}
plugins/Lights/LegacyLights.hnew file+46
@@ -0,0 +1,46 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Renato Araujo Oliveira Filho <renato.filho@canonical.com>
17+ */
18+
19+#ifndef UNITY_LEGACYLIGHTS_H
20+#define UNITY_LEGACYLIGHTS_H
21+
22+#include <QtCore/QObject>
23+#include <QDBusInterface>
24+#include <QtGui/QColor>
25+#include "Lights.h"
26+
27+struct light_device_t;
28+
29+class LegacyLights: public Lights
30+{
31+ Q_OBJECT
32+
33+public:
34+ ~LegacyLights();
35+
36+protected:
37+ bool init() override;
38+ void turnOff() override;
39+ void turnOn() override;
40+
41+private:
42+ light_device_t* m_lightDevice;
43+
44+};
45+
46+#endif
plugins/Lights/Lights.cpp−65
@@ -26,20 +26,12 @@ extern "C" {
26
26
27Lights::Lights(QObject* parent)
27Lights::Lights(QObject* parent)
28 : QObject(parent),
28 : QObject(parent),
29− m_lightDevice(0),
30 m_color("blue"),
29 m_color("blue"),
31 m_state(Lights::Off),
30 m_state(Lights::Off),
32 m_onMs(1000),
31 m_onMs(1000),
33 m_offMs(3000)
32 m_offMs(3000)
34{
33{
35−}
36
34
37−Lights::~Lights()
38−{
39− if (m_lightDevice) {
40− hw_device_t* device = (hw_device_t*) m_lightDevice;
41− device->close(device);
42− }
43}
35}
44
36
45void Lights::setState(Lights::State newState)
37void Lights::setState(Lights::State newState)
@@ -107,60 +99,3 @@ void Lights::setOffMillisec(int offMs)
107 // FIXME: update the property if the light is already on
99 // FIXME: update the property if the light is already on
108 }
100 }
109}
101}
110−
111−bool Lights::init()
112−{
113− if (m_lightDevice) {
114− return true;
115− }
116−
117− int err;
118− hw_module_t* module;
119−
120− err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
121− if (err == 0) {
122− hw_device_t* device;
123− err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
124− if (err == 0) {
125− m_lightDevice = (light_device_t*)device;
126− turnOff();
127− return true;
128− } else {
129− qWarning() << "Failed to access notification lights";
130− }
131− } else {
132− qWarning() << "Failed to initialize lights hardware.";
133− }
134− return false;
135−}
136−
137−void Lights::turnOn()
138−{
139− // pulse
140− light_state_t state;
141− memset(&state, 0, sizeof(light_state_t));
142− state.color = m_color.rgba();
143− state.flashMode = LIGHT_FLASH_TIMED;
144− state.flashOnMS = m_onMs;
145− state.flashOffMS = m_offMs;
146− state.brightnessMode = BRIGHTNESS_MODE_USER;
147−
148− if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
149− qWarning() << "Failed to turn the light off";
150− }
151−}
152−
153−void Lights::turnOff()
154−{
155− light_state_t state;
156− memset(&state, 0, sizeof(light_state_t));
157− state.color = 0x00000000;
158− state.flashMode = LIGHT_FLASH_NONE;
159− state.flashOnMS = 0;
160− state.flashOffMS = 0;
161− state.brightnessMode = 0;
162−
163− if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
164− qWarning() << "Failed to turn the light off";
165− }
166−}
plugins/Lights/Lights.h+5−8
@@ -20,10 +20,9 @@
20#define UNITY_LIGHTS_H
20#define UNITY_LIGHTS_H
21
21
22#include <QtCore/QObject>
22#include <QtCore/QObject>
23+#include <QDBusInterface>
23#include <QtGui/QColor>
24#include <QtGui/QColor>
24
25
25−struct light_device_t;
26−
27class Lights: public QObject
26class Lights: public QObject
28{
27{
29 Q_OBJECT
28 Q_OBJECT
@@ -40,7 +39,6 @@ public:
40 Q_ENUM(State)
39 Q_ENUM(State)
41
40
42 explicit Lights(QObject *parent = 0);
41 explicit Lights(QObject *parent = 0);
43− ~Lights();
44
42
45 void setState(State newState);
43 void setState(State newState);
46 State state() const;
44 State state() const;
@@ -60,16 +58,15 @@ Q_SIGNALS:
60 void onMillisecChanged(int onMs);
58 void onMillisecChanged(int onMs);
61 void offMillisecChanged(int offMs);
59 void offMillisecChanged(int offMs);
62
60
63−private:
61+protected:
64− light_device_t* m_lightDevice;
65 QColor m_color;
62 QColor m_color;
66 State m_state;
63 State m_state;
67 int m_onMs;
64 int m_onMs;
68 int m_offMs;
65 int m_offMs;
69
66
70− bool init();
67+ virtual bool init() = 0;
71− void turnOff();
68+ virtual void turnOff() = 0;
72− void turnOn();
69+ virtual void turnOn() = 0;
73};
70};
74
71
75#endif
72#endif
plugins/Lights/plugin.cpp+20−1
@@ -18,6 +18,11 @@
18
18
19#include "plugin.h"
19#include "plugin.h"
20#include "Lights.h"
20#include "Lights.h"
21+#include "HfdLights.h"
22+#include "LegacyLights.h"
23+
24+// libandroid-properties
25+#include <hybris/properties/properties.h>
21
26
22#include <QtQml/qqml.h>
27#include <QtQml/qqml.h>
23
28
@@ -25,7 +30,21 @@ static QObject *lights_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
25{
30{
26 Q_UNUSED(engine)
31 Q_UNUSED(engine)
27 Q_UNUSED(scriptEngine)
32 Q_UNUSED(scriptEngine)
33+
34+ bool use_hfd = false;
35+ char buffer[20];
36+ auto result = property_get("ro.build.version.sdk", buffer, "0");
37+ if (result) {
38+ auto sdkVersion = QString(buffer).toInt();
39+ if (sdkVersion >= 27 || sdkVersion == 0) {
40+ use_hfd = true;
41+ }
42+ }
43+ if (use_hfd) {
28− return new Lights();
44+ return new HfdLights();
45+ } else {
46+ return new LegacyLights();
47+ }
29}
48}
30
49
31void LightsPlugin::registerTypes(const char *uri)
50void LightsPlugin::registerTypes(const char *uri)