sm8450-common: Add hals.conf to load sensors.xiaomi to odm and move notifier to subdir
Change-Id: I9e16531e60fa6a0122330ee352cddd0840feb39c
This commit is contained in:
79
sensors/sensor-notifier/Android.bp
Normal file
79
sensors/sensor-notifier/Android.bp
Normal file
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// Copyright (C) 2024 The LineageOS Project
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
soong_config_module_type {
|
||||
name: "xiaomi_sm8450_sensor_notifier",
|
||||
module_type: "cc_defaults",
|
||||
config_namespace: "xiaomiSm8450SensorVars",
|
||||
value_variables: ["extensionLibs"],
|
||||
properties: [
|
||||
"whole_static_libs",
|
||||
],
|
||||
}
|
||||
|
||||
xiaomi_sm8450_sensor_notifier {
|
||||
name: "xiaomi_sm8450_sensor_notifier_defaults",
|
||||
soong_config_variables: {
|
||||
extensionLibs: {
|
||||
whole_static_libs: ["%s"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
cc_library_headers {
|
||||
name: "xiaomi_sm8450_sensor_notifier_headers",
|
||||
export_include_dirs: ["include"],
|
||||
vendor: true,
|
||||
}
|
||||
|
||||
cc_binary {
|
||||
name: "sensor-notifier",
|
||||
defaults: [
|
||||
"xiaomi_sm8450_sensor_notifier_defaults",
|
||||
],
|
||||
vendor: true,
|
||||
|
||||
init_rc: ["sensor-notifier.rc"],
|
||||
|
||||
srcs: [
|
||||
"main.cpp",
|
||||
"notifiers/AodNotifier.cpp",
|
||||
"notifiers/LightNotifier.cpp",
|
||||
"notifiers/NonUiNotifier.cpp",
|
||||
"SensorNotifier.cpp",
|
||||
"utils/SensorNotifierUtils.cpp",
|
||||
"utils/SscCalApiWrapper.cpp",
|
||||
],
|
||||
|
||||
shared_libs: [
|
||||
"libbase",
|
||||
"libhidlbase",
|
||||
"libutils",
|
||||
"android.frameworks.sensorservice@1.0",
|
||||
],
|
||||
|
||||
header_libs: [
|
||||
"generated_kernel_headers",
|
||||
"xiaomi_sm8450_sensor_notifier_headers",
|
||||
],
|
||||
}
|
||||
|
||||
cc_library_static {
|
||||
name: "libsensor-notifier-ext",
|
||||
vendor: true,
|
||||
|
||||
srcs: [
|
||||
"SensorNotifierExt.cpp",
|
||||
],
|
||||
|
||||
shared_libs: [
|
||||
"android.frameworks.sensorservice@1.0",
|
||||
],
|
||||
|
||||
header_libs: [
|
||||
"xiaomi_sm8450_sensor_notifier_headers",
|
||||
],
|
||||
}
|
86
sensors/sensor-notifier/SensorNotifier.cpp
Normal file
86
sensors/sensor-notifier/SensorNotifier.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define LOG_TAG "SensorNotifier"
|
||||
|
||||
#include "SensorNotifier.h"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
|
||||
using android::hardware::sensors::V1_0::SensorFlagBits;
|
||||
using android::hardware::sensors::V1_0::SensorInfo;
|
||||
|
||||
SensorNotifier::SensorNotifier(sp<ISensorManager> manager) : mManager(manager) {}
|
||||
|
||||
SensorNotifier::~SensorNotifier() {
|
||||
if (mQueue != nullptr) {
|
||||
/*
|
||||
* Free the event queue.
|
||||
* kernel calls decStrong() on server side implementation of IEventQueue,
|
||||
* hence resources (including the callback) are freed as well.
|
||||
*/
|
||||
mQueue = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Result SensorNotifier::initializeSensorQueue(std::string typeAsString, bool wakeup,
|
||||
sp<IEventQueueCallback> callback) {
|
||||
Result res;
|
||||
std::vector<SensorInfo> sensorList;
|
||||
|
||||
mManager->getSensorList([&sensorList, &res](const auto& l, auto r) {
|
||||
sensorList = l;
|
||||
res = r;
|
||||
});
|
||||
if (res != Result::OK) {
|
||||
LOG(ERROR) << "failed to get sensors list";
|
||||
return res;
|
||||
}
|
||||
auto it = std::find_if(sensorList.begin(), sensorList.end(),
|
||||
[this, &typeAsString, &wakeup](const SensorInfo& sensor) {
|
||||
return (sensor.typeAsString == typeAsString) &&
|
||||
((sensor.flags & SensorFlagBits::WAKE_UP) == wakeup);
|
||||
});
|
||||
|
||||
if (it != sensorList.end()) {
|
||||
mSensorHandle = it->sensorHandle;
|
||||
} else {
|
||||
LOG(ERROR) << "failed to get " << typeAsString << " sensor with wake-up: " << wakeup;
|
||||
return Result::NOT_EXIST;
|
||||
}
|
||||
|
||||
mManager->createEventQueue(callback, [this, &res](const auto& q, auto r) {
|
||||
this->mQueue = q;
|
||||
res = r;
|
||||
});
|
||||
if (res != Result::OK) {
|
||||
LOG(ERROR) << "failed to create event queue";
|
||||
return res;
|
||||
}
|
||||
|
||||
return Result::OK;
|
||||
}
|
||||
|
||||
void SensorNotifier::activate() {
|
||||
if (mActive) {
|
||||
return;
|
||||
}
|
||||
mActive = true;
|
||||
mThread = std::thread(&SensorNotifier::notify, this);
|
||||
}
|
||||
|
||||
void SensorNotifier::deactivate() {
|
||||
if (!mActive) {
|
||||
return;
|
||||
}
|
||||
mActive = false;
|
||||
if (mThread.joinable()) {
|
||||
mThread.join();
|
||||
}
|
||||
if (mQueue != nullptr) {
|
||||
mQueue->disableSensor(mSensorHandle);
|
||||
}
|
||||
}
|
9
sensors/sensor-notifier/SensorNotifierExt.cpp
Normal file
9
sensors/sensor-notifier/SensorNotifierExt.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "SensorNotifierExt.h"
|
||||
|
||||
SensorNotifierExt::SensorNotifierExt(sp<ISensorManager> /* manager */) {}
|
37
sensors/sensor-notifier/include/SensorNotifier.h
Normal file
37
sensors/sensor-notifier/include/SensorNotifier.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <android/frameworks/sensorservice/1.0/ISensorManager.h>
|
||||
#include <thread>
|
||||
|
||||
using android::sp;
|
||||
using android::frameworks::sensorservice::V1_0::IEventQueue;
|
||||
using android::frameworks::sensorservice::V1_0::IEventQueueCallback;
|
||||
using android::frameworks::sensorservice::V1_0::ISensorManager;
|
||||
using android::frameworks::sensorservice::V1_0::Result;
|
||||
|
||||
class SensorNotifier {
|
||||
public:
|
||||
SensorNotifier(sp<ISensorManager> manager);
|
||||
virtual ~SensorNotifier();
|
||||
|
||||
void activate();
|
||||
void deactivate();
|
||||
|
||||
protected:
|
||||
Result initializeSensorQueue(std::string typeAsString, bool wakeup, sp<IEventQueueCallback>);
|
||||
virtual void notify() = 0;
|
||||
|
||||
sp<IEventQueue> mQueue;
|
||||
int32_t mSensorHandle = -1;
|
||||
bool mActive = false;
|
||||
|
||||
private:
|
||||
sp<ISensorManager> mManager;
|
||||
std::thread mThread;
|
||||
};
|
20
sensors/sensor-notifier/include/SensorNotifierExt.h
Normal file
20
sensors/sensor-notifier/include/SensorNotifierExt.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <android/frameworks/sensorservice/1.0/ISensorManager.h>
|
||||
|
||||
#include "SensorNotifier.h"
|
||||
|
||||
using android::sp;
|
||||
using android::frameworks::sensorservice::V1_0::ISensorManager;
|
||||
|
||||
class SensorNotifierExt {
|
||||
public:
|
||||
SensorNotifierExt(sp<ISensorManager> manager);
|
||||
std::vector<std::unique_ptr<SensorNotifier>> mNotifiers;
|
||||
};
|
12
sensors/sensor-notifier/include/SensorNotifierUtils.h
Normal file
12
sensors/sensor-notifier/include/SensorNotifierUtils.h
Normal file
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <display/drm/mi_disp.h>
|
||||
|
||||
bool readBool(int fd);
|
||||
disp_event_resp* parseDispEvent(int fd);
|
46
sensors/sensor-notifier/include/SscCalApi.h
Normal file
46
sensors/sensor-notifier/include/SscCalApi.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum notify_t {
|
||||
BRIGHTNESS = 17,
|
||||
DC_STATE = 18,
|
||||
DISPLAY_FREQUENCY = 20,
|
||||
REPORT_VALUE = 201,
|
||||
POWER_STATE = 202,
|
||||
};
|
||||
|
||||
struct _oem_msg {
|
||||
uint32_t sensorType;
|
||||
notify_t notifyType;
|
||||
float unknown1;
|
||||
float unknown2;
|
||||
float notifyTypeFloat;
|
||||
float value;
|
||||
float unused[10];
|
||||
};
|
||||
|
||||
typedef void (*init_current_sensors_t)(bool debug);
|
||||
typedef void (*process_msg_t)(_oem_msg* msg);
|
||||
|
||||
class SscCalApiWrapper {
|
||||
public:
|
||||
static SscCalApiWrapper& getInstance();
|
||||
|
||||
void initCurrentSensors(bool debug);
|
||||
void processMsg(_oem_msg* msg);
|
||||
|
||||
private:
|
||||
SscCalApiWrapper();
|
||||
~SscCalApiWrapper();
|
||||
|
||||
void* mSscCalApiHandle;
|
||||
process_msg_t process_msg;
|
||||
init_current_sensors_t init_current_sensors;
|
||||
};
|
49
sensors/sensor-notifier/main.cpp
Normal file
49
sensors/sensor-notifier/main.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define LOG_TAG "SensorNotifier"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/properties.h>
|
||||
|
||||
#include "SensorNotifierExt.h"
|
||||
#include "SscCalApi.h"
|
||||
#include "notifiers/AodNotifier.h"
|
||||
#include "notifiers/LightNotifier.h"
|
||||
#include "notifiers/NonUiNotifier.h"
|
||||
|
||||
int main() {
|
||||
sp<ISensorManager> manager = ISensorManager::getService();
|
||||
if (manager == nullptr) {
|
||||
LOG(ERROR) << "failed to get ISensorManager";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
SscCalApiWrapper::getInstance().initCurrentSensors(
|
||||
android::base::GetBoolProperty("persist.vendor.debug.ssccalapi", false));
|
||||
|
||||
std::vector<std::unique_ptr<SensorNotifier>> notifiers;
|
||||
notifiers.push_back(std::make_unique<AodNotifier>(manager));
|
||||
notifiers.push_back(std::make_unique<LightNotifier>(manager));
|
||||
notifiers.push_back(std::make_unique<NonUiNotifier>(manager));
|
||||
for (const auto& notifier : notifiers) {
|
||||
notifier->activate();
|
||||
}
|
||||
|
||||
std::unique_ptr<SensorNotifierExt> sensorNotifierExt =
|
||||
std::make_unique<SensorNotifierExt>(manager);
|
||||
for (const auto& notifier : sensorNotifierExt->mNotifiers) {
|
||||
notifier->activate();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// Sleep to keep the notifiers alive
|
||||
std::this_thread::sleep_for(std::chrono::seconds(10));
|
||||
}
|
||||
|
||||
// Should never reach this
|
||||
return EXIT_SUCCESS;
|
||||
}
|
128
sensors/sensor-notifier/notifiers/AodNotifier.cpp
Normal file
128
sensors/sensor-notifier/notifiers/AodNotifier.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define LOG_TAG "AodNotifier"
|
||||
|
||||
#include "AodNotifier.h"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
#include <display/drm/mi_disp.h>
|
||||
#include <poll.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "SensorNotifierUtils.h"
|
||||
|
||||
static const std::string kDispFeatureDevice = "/dev/mi_display/disp_feature";
|
||||
|
||||
using android::hardware::Return;
|
||||
using android::hardware::Void;
|
||||
using android::hardware::sensors::V1_0::Event;
|
||||
|
||||
namespace {
|
||||
|
||||
void requestDozeBrightness(int fd, __u32 doze_brightness) {
|
||||
disp_doze_brightness_req req;
|
||||
req.base.flag = 0;
|
||||
req.base.disp_id = MI_DISP_PRIMARY;
|
||||
req.doze_brightness = doze_brightness;
|
||||
ioctl(fd, MI_DISP_IOCTL_SET_DOZE_BRIGHTNESS, &req);
|
||||
}
|
||||
|
||||
class AodSensorCallback : public IEventQueueCallback {
|
||||
public:
|
||||
AodSensorCallback() {
|
||||
disp_fd_ = android::base::unique_fd(open(kDispFeatureDevice.c_str(), O_RDWR));
|
||||
if (disp_fd_.get() == -1) {
|
||||
LOG(ERROR) << "failed to open " << kDispFeatureDevice;
|
||||
}
|
||||
}
|
||||
|
||||
Return<void> onEvent(const Event& e) {
|
||||
requestDozeBrightness(disp_fd_.get(), (e.u.scalar == 3 || e.u.scalar == 5)
|
||||
? DOZE_BRIGHTNESS_LBM
|
||||
: DOZE_BRIGHTNESS_HBM);
|
||||
return Void();
|
||||
}
|
||||
|
||||
private:
|
||||
android::base::unique_fd disp_fd_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
AodNotifier::AodNotifier(sp<ISensorManager> manager) : SensorNotifier(manager) {
|
||||
initializeSensorQueue("xiaomi.sensor.aod", true, new AodSensorCallback());
|
||||
}
|
||||
|
||||
AodNotifier::~AodNotifier() {
|
||||
deactivate();
|
||||
}
|
||||
|
||||
void AodNotifier::notify() {
|
||||
Result res;
|
||||
|
||||
android::base::unique_fd disp_fd_ =
|
||||
android::base::unique_fd(open(kDispFeatureDevice.c_str(), O_RDWR));
|
||||
if (disp_fd_.get() == -1) {
|
||||
LOG(ERROR) << "failed to open " << kDispFeatureDevice;
|
||||
}
|
||||
|
||||
// Register for power events
|
||||
disp_event_req req;
|
||||
req.base.flag = 0;
|
||||
req.base.disp_id = MI_DISP_PRIMARY;
|
||||
req.type = MI_DISP_EVENT_POWER;
|
||||
ioctl(disp_fd_.get(), MI_DISP_IOCTL_REGISTER_EVENT, &req);
|
||||
|
||||
struct pollfd dispEventPoll = {
|
||||
.fd = disp_fd_.get(),
|
||||
.events = POLLIN,
|
||||
.revents = 0,
|
||||
};
|
||||
|
||||
while (mActive) {
|
||||
int rc = poll(&dispEventPoll, 1, -1);
|
||||
if (rc < 0) {
|
||||
LOG(ERROR) << "failed to poll " << kDispFeatureDevice << ", err: " << rc;
|
||||
continue;
|
||||
}
|
||||
|
||||
struct disp_event_resp* response = parseDispEvent(disp_fd_.get());
|
||||
if (response == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (response->base.type != MI_DISP_EVENT_POWER) {
|
||||
LOG(ERROR) << "unexpected display event: " << response->base.type;
|
||||
continue;
|
||||
}
|
||||
|
||||
int value = response->data[0];
|
||||
LOG(VERBOSE) << "received data: " << std::bitset<8>(value);
|
||||
|
||||
switch (response->data[0]) {
|
||||
case MI_DISP_POWER_LP1:
|
||||
FALLTHROUGH_INTENDED;
|
||||
case MI_DISP_POWER_LP2:
|
||||
res = mQueue->enableSensor(mSensorHandle, 20000 /* sample period */,
|
||||
0 /* latency */);
|
||||
if (res != Result::OK) {
|
||||
LOG(ERROR) << "failed to enable sensor";
|
||||
}
|
||||
break;
|
||||
case MI_DISP_POWER_ON:
|
||||
requestDozeBrightness(disp_fd_.get(), DOZE_TO_NORMAL);
|
||||
FALLTHROUGH_INTENDED;
|
||||
default:
|
||||
res = mQueue->disableSensor(mSensorHandle);
|
||||
if (res != Result::OK) {
|
||||
LOG(DEBUG) << "failed to disable sensor";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
18
sensors/sensor-notifier/notifiers/AodNotifier.h
Normal file
18
sensors/sensor-notifier/notifiers/AodNotifier.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SensorNotifier.h"
|
||||
|
||||
class AodNotifier : public SensorNotifier {
|
||||
public:
|
||||
AodNotifier(sp<ISensorManager> manager);
|
||||
~AodNotifier();
|
||||
|
||||
protected:
|
||||
void notify();
|
||||
};
|
150
sensors/sensor-notifier/notifiers/LightNotifier.cpp
Normal file
150
sensors/sensor-notifier/notifiers/LightNotifier.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define LOG_TAG "LightNotifier"
|
||||
|
||||
#include "LightNotifier.h"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/properties.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
#include <display/drm/mi_disp.h>
|
||||
#include <poll.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "SensorNotifierUtils.h"
|
||||
#include "SscCalApi.h"
|
||||
|
||||
static const std::string kDispFeatureDevice = "/dev/mi_display/disp_feature";
|
||||
|
||||
LightNotifier::LightNotifier(sp<ISensorManager> manager) : SensorNotifier(manager) {
|
||||
std::stringstream lightSensorsPrimary(
|
||||
android::base::GetProperty("ro.vendor.sensors.notifier.light_sensors.primary", ""));
|
||||
std::stringstream lightSensorsSecondary(
|
||||
android::base::GetProperty("ro.vendor.sensors.notifier.light_sensors.secondary", ""));
|
||||
|
||||
std::string sensor;
|
||||
while (std::getline(lightSensorsPrimary, sensor, ',')) {
|
||||
mLightSensorsPrimary.push_back(std::stoi(sensor));
|
||||
}
|
||||
while (std::getline(lightSensorsSecondary, sensor, ',')) {
|
||||
mLightSensorsSecondary.push_back(std::stoi(sensor));
|
||||
}
|
||||
}
|
||||
|
||||
LightNotifier::~LightNotifier() {
|
||||
deactivate();
|
||||
}
|
||||
|
||||
void LightNotifier::notify() {
|
||||
if (mLightSensorsPrimary.empty() && mLightSensorsSecondary.empty()) {
|
||||
LOG(DEBUG) << "no light sensors to notify defined, skip light notifications";
|
||||
mActive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
android::base::unique_fd disp_fd_ =
|
||||
android::base::unique_fd(open(kDispFeatureDevice.c_str(), O_RDWR));
|
||||
if (disp_fd_.get() == -1) {
|
||||
LOG(ERROR) << "failed to open " << kDispFeatureDevice;
|
||||
mActive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<disp_display_type> displays;
|
||||
if (!mLightSensorsPrimary.empty()) {
|
||||
displays.push_back(MI_DISP_PRIMARY);
|
||||
}
|
||||
if (!mLightSensorsSecondary.empty()) {
|
||||
displays.push_back(MI_DISP_SECONDARY);
|
||||
}
|
||||
|
||||
const std::vector<disp_event_type> notifyEvents = {MI_DISP_EVENT_POWER, MI_DISP_EVENT_FPS,
|
||||
MI_DISP_EVENT_51_BRIGHTNESS,
|
||||
MI_DISP_EVENT_HBM, MI_DISP_EVENT_DC};
|
||||
|
||||
// Register for events
|
||||
for (const disp_display_type& display : displays) {
|
||||
for (const disp_event_type& event : notifyEvents) {
|
||||
disp_event_req req;
|
||||
req.base.flag = 0;
|
||||
req.base.disp_id = display;
|
||||
req.type = event;
|
||||
ioctl(disp_fd_.get(), MI_DISP_IOCTL_REGISTER_EVENT, &req);
|
||||
}
|
||||
}
|
||||
|
||||
struct pollfd dispEventPoll = {
|
||||
.fd = disp_fd_.get(),
|
||||
.events = POLLIN,
|
||||
};
|
||||
|
||||
_oem_msg* msg = new _oem_msg;
|
||||
notify_t notifyType;
|
||||
float value;
|
||||
|
||||
while (mActive) {
|
||||
int rc = poll(&dispEventPoll, 1, -1);
|
||||
if (rc < 0) {
|
||||
LOG(ERROR) << "failed to poll " << kDispFeatureDevice << ", err: " << rc;
|
||||
continue;
|
||||
}
|
||||
|
||||
struct disp_event_resp* response = parseDispEvent(disp_fd_.get());
|
||||
if (response == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<int>& sensorsToNotify = mLightSensorsPrimary;
|
||||
switch (response->base.disp_id) {
|
||||
case MI_DISP_PRIMARY:
|
||||
break;
|
||||
case MI_DISP_SECONDARY:
|
||||
sensorsToNotify = mLightSensorsSecondary;
|
||||
break;
|
||||
default:
|
||||
LOG(ERROR) << "got notified for unknown display: " << response->base.disp_id;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (response->base.type) {
|
||||
case MI_DISP_EVENT_POWER:
|
||||
notifyType = POWER_STATE;
|
||||
value = response->data[0];
|
||||
break;
|
||||
case MI_DISP_EVENT_FPS:
|
||||
notifyType = DISPLAY_FREQUENCY;
|
||||
value = response->data[0];
|
||||
break;
|
||||
case MI_DISP_EVENT_51_BRIGHTNESS:
|
||||
notifyType = BRIGHTNESS;
|
||||
value = *(uint16_t*)response->data;
|
||||
break;
|
||||
case MI_DISP_EVENT_HBM:
|
||||
notifyType = BRIGHTNESS;
|
||||
value = response->data[0] ? -1 : -2;
|
||||
break;
|
||||
case MI_DISP_EVENT_DC:
|
||||
notifyType = DC_STATE;
|
||||
value = response->data[0];
|
||||
break;
|
||||
default:
|
||||
LOG(ERROR) << "got unknown event: " << response->base.type;
|
||||
continue;
|
||||
};
|
||||
|
||||
for (const auto sensorId : sensorsToNotify) {
|
||||
msg->sensorType = sensorId;
|
||||
msg->notifyType = notifyType;
|
||||
msg->notifyTypeFloat = notifyType;
|
||||
msg->value = value;
|
||||
msg->unknown1 = 1;
|
||||
msg->unknown2 = 5;
|
||||
|
||||
SscCalApiWrapper::getInstance().processMsg(msg);
|
||||
}
|
||||
}
|
||||
}
|
24
sensors/sensor-notifier/notifiers/LightNotifier.h
Normal file
24
sensors/sensor-notifier/notifiers/LightNotifier.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <android-base/unique_fd.h>
|
||||
|
||||
#include "SensorNotifier.h"
|
||||
|
||||
class LightNotifier : public SensorNotifier {
|
||||
public:
|
||||
LightNotifier(sp<ISensorManager> manager);
|
||||
~LightNotifier();
|
||||
|
||||
protected:
|
||||
void notify();
|
||||
|
||||
private:
|
||||
std::vector<int> mLightSensorsPrimary;
|
||||
std::vector<int> mLightSensorsSecondary;
|
||||
};
|
102
sensors/sensor-notifier/notifiers/NonUiNotifier.cpp
Normal file
102
sensors/sensor-notifier/notifiers/NonUiNotifier.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define LOG_TAG "NonUiNotifier"
|
||||
|
||||
#include "NonUiNotifier.h"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
#include <linux/xiaomi_touch.h>
|
||||
#include <poll.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "SensorNotifierUtils.h"
|
||||
|
||||
static const std::string kTouchDevice = "/dev/xiaomi-touch";
|
||||
|
||||
using android::hardware::Return;
|
||||
using android::hardware::Void;
|
||||
using android::hardware::sensors::V1_0::Event;
|
||||
|
||||
namespace {
|
||||
|
||||
class NonUiSensorCallback : public IEventQueueCallback {
|
||||
public:
|
||||
NonUiSensorCallback() {
|
||||
touch_fd_ = android::base::unique_fd(open(kTouchDevice.c_str(), O_RDWR));
|
||||
if (touch_fd_.get() == -1) {
|
||||
LOG(ERROR) << "failed to open " << kTouchDevice;
|
||||
}
|
||||
}
|
||||
|
||||
Return<void> onEvent(const Event& e) {
|
||||
int buf[MAX_BUF_SIZE] = {0, Touch_Nonui_Mode, static_cast<int>(e.u.scalar)};
|
||||
ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &buf);
|
||||
|
||||
return Void();
|
||||
}
|
||||
|
||||
private:
|
||||
android::base::unique_fd touch_fd_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
NonUiNotifier::NonUiNotifier(sp<ISensorManager> manager) : SensorNotifier(manager) {
|
||||
initializeSensorQueue("xiaomi.sensor.nonui", true, new NonUiSensorCallback());
|
||||
}
|
||||
|
||||
NonUiNotifier::~NonUiNotifier() {
|
||||
deactivate();
|
||||
}
|
||||
|
||||
void NonUiNotifier::notify() {
|
||||
Result res;
|
||||
|
||||
// Enable states of touchscreen sensors
|
||||
const std::vector<const char*> paths = {
|
||||
"/sys/class/touch/touch_dev/fod_longpress_gesture_enabled",
|
||||
"/sys/class/touch/touch_dev/gesture_single_tap_enabled",
|
||||
"/sys/class/touch/touch_dev/gesture_double_tap_enabled"};
|
||||
|
||||
pollfd* pollfds = new pollfd[paths.size()];
|
||||
for (size_t i = 0; i < paths.size(); ++i) {
|
||||
int fd = open(paths[i], O_RDONLY);
|
||||
if (fd < 0) {
|
||||
LOG(ERROR) << "failed to open " << paths[i] << " , err: " << fd;
|
||||
mActive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
pollfds[i].fd = fd;
|
||||
pollfds[i].events = POLLPRI;
|
||||
}
|
||||
|
||||
while (mActive) {
|
||||
int rc = poll(pollfds, paths.size(), -1);
|
||||
if (rc < 0) {
|
||||
LOG(ERROR) << "failed to poll, err: " << rc;
|
||||
continue;
|
||||
}
|
||||
|
||||
bool enabled = false;
|
||||
for (size_t i = 0; i < paths.size(); ++i) {
|
||||
enabled = enabled || readBool(pollfds[i].fd);
|
||||
}
|
||||
if (enabled) {
|
||||
res = mQueue->enableSensor(mSensorHandle, 20000 /* sample period */, 0 /* latency */);
|
||||
if (res != Result::OK) {
|
||||
LOG(ERROR) << "failed to enable sensor";
|
||||
}
|
||||
} else {
|
||||
res = mQueue->disableSensor(mSensorHandle);
|
||||
if (res != Result::OK) {
|
||||
LOG(DEBUG) << "failed to disable sensor";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
18
sensors/sensor-notifier/notifiers/NonUiNotifier.h
Normal file
18
sensors/sensor-notifier/notifiers/NonUiNotifier.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SensorNotifier.h"
|
||||
|
||||
class NonUiNotifier : public SensorNotifier {
|
||||
public:
|
||||
NonUiNotifier(sp<ISensorManager> manager);
|
||||
~NonUiNotifier();
|
||||
|
||||
protected:
|
||||
void notify();
|
||||
};
|
10
sensors/sensor-notifier/sensor-notifier.rc
Normal file
10
sensors/sensor-notifier/sensor-notifier.rc
Normal file
@@ -0,0 +1,10 @@
|
||||
#
|
||||
# Copyright (C) 2024 The LineageOS Project
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
service vendor.sensor-notifier /vendor/bin/sensor-notifier
|
||||
class main
|
||||
user system
|
||||
group system
|
57
sensors/sensor-notifier/utils/SensorNotifierUtils.cpp
Normal file
57
sensors/sensor-notifier/utils/SensorNotifierUtils.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define LOG_TAG "SensorNotifierUtils"
|
||||
|
||||
#include "SensorNotifierUtils.h"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
|
||||
bool readBool(int fd) {
|
||||
char c;
|
||||
int rc;
|
||||
|
||||
rc = lseek(fd, 0, SEEK_SET);
|
||||
if (rc) {
|
||||
LOG(ERROR) << "failed to seek fd, err: " << rc;
|
||||
return false;
|
||||
}
|
||||
|
||||
rc = read(fd, &c, sizeof(char));
|
||||
if (rc != 1) {
|
||||
LOG(ERROR) << "failed to read bool from fd, err: " << rc;
|
||||
return false;
|
||||
}
|
||||
|
||||
return c != '0';
|
||||
}
|
||||
|
||||
disp_event_resp* parseDispEvent(int fd) {
|
||||
disp_event header;
|
||||
ssize_t headerSize = read(fd, &header, sizeof(header));
|
||||
if (headerSize < sizeof(header)) {
|
||||
LOG(ERROR) << "unexpected display event header size: " << headerSize;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct disp_event_resp* response =
|
||||
reinterpret_cast<struct disp_event_resp*>(malloc(header.length));
|
||||
response->base = header;
|
||||
|
||||
int dataLength = response->base.length - sizeof(response->base);
|
||||
if (dataLength < 0) {
|
||||
LOG(ERROR) << "invalid data length: " << response->base.length;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ssize_t dataSize = read(fd, &response->data, dataLength);
|
||||
if (dataSize < dataLength) {
|
||||
LOG(ERROR) << "unexpected display event data size: " << dataSize;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
53
sensors/sensor-notifier/utils/SscCalApiWrapper.cpp
Normal file
53
sensors/sensor-notifier/utils/SscCalApiWrapper.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The LineageOS Project
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define LOG_TAG "SscCalApiWrapper"
|
||||
|
||||
#include "SscCalApi.h"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
SscCalApiWrapper::SscCalApiWrapper() {
|
||||
mSscCalApiHandle = dlopen("libssccalapi@2.0.so", RTLD_NOW);
|
||||
if (mSscCalApiHandle) {
|
||||
init_current_sensors =
|
||||
(init_current_sensors_t)dlsym(mSscCalApiHandle, "_Z20init_current_sensorsb");
|
||||
if (init_current_sensors == NULL) {
|
||||
LOG(ERROR) << "could not find init_current_sensors: " << dlerror();
|
||||
}
|
||||
|
||||
process_msg = (process_msg_t)dlsym(mSscCalApiHandle, "_Z11process_msgP8_oem_msg");
|
||||
if (process_msg == NULL) {
|
||||
LOG(ERROR) << "could not find process_msg: " << dlerror();
|
||||
}
|
||||
} else {
|
||||
LOG(INFO) << "could not dlopen libssccalapi@2.0.so: " << dlerror();
|
||||
}
|
||||
}
|
||||
|
||||
SscCalApiWrapper::~SscCalApiWrapper() {
|
||||
dlclose(mSscCalApiHandle);
|
||||
}
|
||||
|
||||
SscCalApiWrapper& SscCalApiWrapper::getInstance() {
|
||||
static SscCalApiWrapper instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void SscCalApiWrapper::initCurrentSensors(bool debug) {
|
||||
if (init_current_sensors != NULL) {
|
||||
init_current_sensors(debug);
|
||||
}
|
||||
}
|
||||
|
||||
void SscCalApiWrapper::processMsg(_oem_msg* msg) {
|
||||
if (process_msg != NULL) {
|
||||
LOG(DEBUG) << "sending oem_msg for sensor " << msg->sensorType
|
||||
<< " with type: " << msg->notifyType << " and value: " << msg->value;
|
||||
process_msg(msg);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user