diff --git a/light/RawLightNotifier.cpp b/light/RawLightNotifier.cpp index 5480905..0422b64 100644 --- a/light/RawLightNotifier.cpp +++ b/light/RawLightNotifier.cpp @@ -47,7 +47,8 @@ class RawLightSensorCallback : public IEventQueueCallback { } // namespace -RawLightNotifier::RawLightNotifier(sp manager) : SensorNotifier(manager) { +RawLightNotifier::RawLightNotifier(sp manager) : SensorNotifier(manager), + isEnable(false) { initializeSensorQueue("xiaomi.sensor.ambientlight.factory", false, new RawLightSensorCallback()); } @@ -69,20 +70,30 @@ void RawLightNotifier::notify() { res = mQueue->enableSensor(mSensorHandle, 20000 /* sample period */, 0 /* latency */); if (res != Result::OK) { LOG(ERROR) << "failed to enable sensor"; - } + } else isEnable = true; // 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); + const std::vector notifyEvents = {MI_DISP_EVENT_POWER, MI_DISP_EVENT_FPS, + MI_DISP_EVENT_51_BRIGHTNESS, + MI_DISP_EVENT_HBM, MI_DISP_EVENT_DC}; + + for (const disp_event_type& event : notifyEvents) { + disp_event_req req; + req.base.flag = 0; + req.base.disp_id = MI_DISP_PRIMARY; + 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) { @@ -95,28 +106,58 @@ void RawLightNotifier::notify() { continue; } - if (response->base.type != MI_DISP_EVENT_POWER) { - LOG(ERROR) << "unexpected display event: " << response->base.type; - continue; + if (response->base.type == MI_DISP_EVENT_POWER) { + notifyType = POWER_STATE; + value = response->data[0]; + switch (response->data[0]) { + case MI_DISP_POWER_ON: + if (!isEnable) { + res = mQueue->enableSensor(mSensorHandle, 20000 /* sample period */, + 0 /* latency */); + if (res != Result::OK) { + LOG(ERROR) << "failed to enable sensor"; + } else isEnable = true; + } + break; + default: + if (isEnable) { + res = mQueue->disableSensor(mSensorHandle); + if (res != Result::OK) { + LOG(ERROR) << "failed to disable sensor"; + } else isEnable = false; + } + break; + } + } else { + switch (response->base.type) { + 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; + } } + msg->sensorType = kSensorTypeAmbientlightRaw; + msg->notifyType = notifyType; + msg->notifyTypeFloat = notifyType; + msg->value = value; + msg->unknown1 = 1; + msg->unknown2 = 5; - int value = response->data[0]; - LOG(VERBOSE) << "received data: " << std::bitset<8>(value); - - switch (response->data[0]) { - case MI_DISP_POWER_ON: - res = mQueue->enableSensor(mSensorHandle, 20000 /* sample period */, - 0 /* latency */); - if (res != Result::OK) { - LOG(ERROR) << "failed to enable sensor"; - } - break; - default: - res = mQueue->disableSensor(mSensorHandle); - if (res != Result::OK) { - LOG(ERROR) << "failed to disable sensor"; - } - break; - } + SscCalApiWrapper::getInstance().processMsg(msg); } } diff --git a/light/RawLightNotifier.h b/light/RawLightNotifier.h index fa82395..c29658a 100644 --- a/light/RawLightNotifier.h +++ b/light/RawLightNotifier.h @@ -15,4 +15,6 @@ class RawLightNotifier : public SensorNotifier { protected: void notify(); + + bool isEnable; };