AodNotifier.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2024 The LineageOS Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #define LOG_TAG "AodNotifier"
  7. #include "AodNotifier.h"
  8. #include <android-base/logging.h>
  9. #include <android-base/unique_fd.h>
  10. #include <display/drm/mi_disp.h>
  11. #include <poll.h>
  12. #include <sys/ioctl.h>
  13. #include "SensorNotifierUtils.h"
  14. static const std::string kDispFeatureDevice = "/dev/mi_display/disp_feature";
  15. using android::hardware::Return;
  16. using android::hardware::Void;
  17. using android::hardware::sensors::V1_0::Event;
  18. namespace {
  19. void requestDozeBrightness(int fd, __u32 doze_brightness) {
  20. disp_doze_brightness_req req;
  21. req.base.flag = 0;
  22. req.base.disp_id = MI_DISP_PRIMARY;
  23. req.doze_brightness = doze_brightness;
  24. ioctl(fd, MI_DISP_IOCTL_SET_DOZE_BRIGHTNESS, &req);
  25. }
  26. class AodSensorCallback : public IEventQueueCallback {
  27. public:
  28. AodSensorCallback() {
  29. disp_fd_ = android::base::unique_fd(open(kDispFeatureDevice.c_str(), O_RDWR));
  30. if (disp_fd_.get() == -1) {
  31. LOG(ERROR) << "failed to open " << kDispFeatureDevice;
  32. }
  33. }
  34. Return<void> onEvent(const Event& e) {
  35. requestDozeBrightness(disp_fd_.get(), (e.u.scalar == 3 || e.u.scalar == 5)
  36. ? DOZE_BRIGHTNESS_LBM
  37. : DOZE_BRIGHTNESS_HBM);
  38. return Void();
  39. }
  40. private:
  41. android::base::unique_fd disp_fd_;
  42. };
  43. } // namespace
  44. AodNotifier::AodNotifier(sp<ISensorManager> manager) : SensorNotifier(manager) {
  45. initializeSensorQueue("xiaomi.sensor.aod", true, new AodSensorCallback());
  46. }
  47. AodNotifier::~AodNotifier() {
  48. deactivate();
  49. }
  50. void AodNotifier::notify() {
  51. Result res;
  52. android::base::unique_fd disp_fd_ =
  53. android::base::unique_fd(open(kDispFeatureDevice.c_str(), O_RDWR));
  54. if (disp_fd_.get() == -1) {
  55. LOG(ERROR) << "failed to open " << kDispFeatureDevice;
  56. }
  57. // Register for power events
  58. disp_event_req req;
  59. req.base.flag = 0;
  60. req.base.disp_id = MI_DISP_PRIMARY;
  61. req.type = MI_DISP_EVENT_POWER;
  62. ioctl(disp_fd_.get(), MI_DISP_IOCTL_REGISTER_EVENT, &req);
  63. struct pollfd dispEventPoll = {
  64. .fd = disp_fd_.get(),
  65. .events = POLLIN,
  66. .revents = 0,
  67. };
  68. while (mActive) {
  69. int rc = poll(&dispEventPoll, 1, -1);
  70. if (rc < 0) {
  71. LOG(ERROR) << "failed to poll " << kDispFeatureDevice << ", err: " << rc;
  72. continue;
  73. }
  74. struct disp_event_resp* response = parseDispEvent(disp_fd_.get());
  75. if (response == nullptr) {
  76. continue;
  77. }
  78. if (response->base.type != MI_DISP_EVENT_POWER) {
  79. LOG(ERROR) << "unexpected display event: " << response->base.type;
  80. continue;
  81. }
  82. int value = response->data[0];
  83. LOG(VERBOSE) << "received data: " << std::bitset<8>(value);
  84. switch (response->data[0]) {
  85. case MI_DISP_POWER_LP1:
  86. FALLTHROUGH_INTENDED;
  87. case MI_DISP_POWER_LP2:
  88. res = mQueue->enableSensor(mSensorHandle, 20000 /* sample period */,
  89. 0 /* latency */);
  90. if (res != Result::OK) {
  91. LOG(ERROR) << "failed to enable sensor";
  92. }
  93. break;
  94. case MI_DISP_POWER_ON:
  95. requestDozeBrightness(disp_fd_.get(), DOZE_TO_NORMAL);
  96. FALLTHROUGH_INTENDED;
  97. default:
  98. res = mQueue->disableSensor(mSensorHandle);
  99. if (res != Result::OK) {
  100. LOG(DEBUG) << "failed to disable sensor";
  101. }
  102. break;
  103. }
  104. }
  105. }