LightNotifier.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2024 The LineageOS Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #define LOG_TAG "LightNotifier"
  7. #include "LightNotifier.h"
  8. #include <android-base/logging.h>
  9. #include <android-base/properties.h>
  10. #include <android-base/unique_fd.h>
  11. #include <display/drm/mi_disp.h>
  12. #include <poll.h>
  13. #include <sys/ioctl.h>
  14. #include "SensorNotifierUtils.h"
  15. #include "SscCalApi.h"
  16. static const std::string kDispFeatureDevice = "/dev/mi_display/disp_feature";
  17. LightNotifier::LightNotifier(sp<ISensorManager> manager) : SensorNotifier(manager) {
  18. std::stringstream lightSensorsPrimary(
  19. android::base::GetProperty("ro.vendor.sensors.notifier.light_sensors.primary", ""));
  20. std::stringstream lightSensorsSecondary(
  21. android::base::GetProperty("ro.vendor.sensors.notifier.light_sensors.secondary", ""));
  22. std::string sensor;
  23. while (std::getline(lightSensorsPrimary, sensor, ',')) {
  24. mLightSensorsPrimary.push_back(std::stoi(sensor));
  25. }
  26. while (std::getline(lightSensorsSecondary, sensor, ',')) {
  27. mLightSensorsSecondary.push_back(std::stoi(sensor));
  28. }
  29. }
  30. LightNotifier::~LightNotifier() {
  31. deactivate();
  32. }
  33. void LightNotifier::notify() {
  34. if (mLightSensorsPrimary.empty() && mLightSensorsSecondary.empty()) {
  35. LOG(DEBUG) << "no light sensors to notify defined, skip light notifications";
  36. mActive = false;
  37. return;
  38. }
  39. android::base::unique_fd disp_fd_ =
  40. android::base::unique_fd(open(kDispFeatureDevice.c_str(), O_RDWR));
  41. if (disp_fd_.get() == -1) {
  42. LOG(ERROR) << "failed to open " << kDispFeatureDevice;
  43. mActive = false;
  44. return;
  45. }
  46. std::vector<disp_display_type> displays;
  47. if (!mLightSensorsPrimary.empty()) {
  48. displays.push_back(MI_DISP_PRIMARY);
  49. }
  50. if (!mLightSensorsSecondary.empty()) {
  51. displays.push_back(MI_DISP_SECONDARY);
  52. }
  53. const std::vector<disp_event_type> notifyEvents = {MI_DISP_EVENT_POWER, MI_DISP_EVENT_FPS,
  54. MI_DISP_EVENT_51_BRIGHTNESS,
  55. MI_DISP_EVENT_HBM, MI_DISP_EVENT_DC};
  56. // Register for events
  57. for (const disp_display_type& display : displays) {
  58. for (const disp_event_type& event : notifyEvents) {
  59. disp_event_req req;
  60. req.base.flag = 0;
  61. req.base.disp_id = display;
  62. req.type = event;
  63. ioctl(disp_fd_.get(), MI_DISP_IOCTL_REGISTER_EVENT, &req);
  64. }
  65. }
  66. struct pollfd dispEventPoll = {
  67. .fd = disp_fd_.get(),
  68. .events = POLLIN,
  69. };
  70. _oem_msg* msg = new _oem_msg;
  71. notify_t notifyType;
  72. float value;
  73. while (mActive) {
  74. int rc = poll(&dispEventPoll, 1, -1);
  75. if (rc < 0) {
  76. LOG(ERROR) << "failed to poll " << kDispFeatureDevice << ", err: " << rc;
  77. continue;
  78. }
  79. struct disp_event_resp* response = parseDispEvent(disp_fd_.get());
  80. if (response == nullptr) {
  81. continue;
  82. }
  83. std::vector<int>& sensorsToNotify = mLightSensorsPrimary;
  84. switch (response->base.disp_id) {
  85. case MI_DISP_PRIMARY:
  86. break;
  87. case MI_DISP_SECONDARY:
  88. sensorsToNotify = mLightSensorsSecondary;
  89. break;
  90. default:
  91. LOG(ERROR) << "got notified for unknown display: " << response->base.disp_id;
  92. continue;
  93. }
  94. switch (response->base.type) {
  95. case MI_DISP_EVENT_POWER:
  96. notifyType = POWER_STATE;
  97. value = response->data[0];
  98. break;
  99. case MI_DISP_EVENT_FPS:
  100. notifyType = DISPLAY_FREQUENCY;
  101. value = response->data[0];
  102. break;
  103. case MI_DISP_EVENT_51_BRIGHTNESS:
  104. notifyType = BRIGHTNESS;
  105. value = *(uint16_t*)response->data;
  106. break;
  107. case MI_DISP_EVENT_HBM:
  108. notifyType = BRIGHTNESS;
  109. value = response->data[0] ? -1 : -2;
  110. break;
  111. case MI_DISP_EVENT_DC:
  112. notifyType = DC_STATE;
  113. value = response->data[0];
  114. break;
  115. default:
  116. LOG(ERROR) << "got unknown event: " << response->base.type;
  117. continue;
  118. };
  119. for (const auto sensorId : sensorsToNotify) {
  120. msg->sensorType = sensorId;
  121. msg->notifyType = notifyType;
  122. msg->notifyTypeFloat = notifyType;
  123. msg->value = value;
  124. msg->unknown1 = 1;
  125. msg->unknown2 = 5;
  126. SscCalApiWrapper::getInstance().processMsg(msg);
  127. }
  128. }
  129. }