Files
2026-08-24 10:03:24 +08:00

270 lines
8.3 KiB
C++

/*
* Copyright (C) 2014-present Amlogic, Inc. All rights reserved.
*
* All information contained herein is Amlogic confidential.
*
* This software is provided to you pursuant to Software License Agreement
* (SLA) with Amlogic Inc ("Amlogic"). This software may be used
* only in accordance with the terms of this agreement.
*
* Redistribution and use in source and binary forms, with or without
* modification is strictly prohibited without prior written permission from
* Amlogic.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <systemd/sd-bus.h>
#include "dbus_defines.h"
#define LOG_MODULE_TAG "PQ"
#define LOG_CLASS_TAG "PqService"
#include "CPQControl.h"
#include "CPQLog.h"
static CPQControl *gs_pqctrl = nullptr;
// functions defined inside dbus_wrapper.cpp
#include "dbus_api_wrapper.cpp"
class PqObserver : public CPQControl::ICPQControlObserver {
public:
PqObserver(sd_bus *bus) { m_bus = bus; }
~PqObserver() = default;
private:
void GetCbDataFromLibpq(CPQControlCb &cb_data) {
int cbType = cb_data.getCbType();
LOGD("%s: cbType: %d\n", __FUNCTION__, cbType);
switch (cbType) {
case CPQControlCb::PQ_CB_TYPE_HDRTYPE:
SignalHdrType(cb_data);
break;
case CPQControlCb::PQ_CB_TYPE_ALLM_GAME_MODE:
SignalAllmGameMode(cb_data);
break;
case CPQControlCb::PQ_CB_TYPE_FILM_MAKER_MODE:
SignalFilmMakerMode(cb_data);
break;
case CPQControlCb::PQ_CB_TYPE_REFRESH_RATE:
SignalRefreshRate(cb_data);
break;
case CPQControlCb::PQ_CB_TYPE_PQ_MODE_AUTO_CHANGED:
SignalPqModeAutoChanged(cb_data);
break;
case CPQControlCb::PQ_CB_TYPE_PQ_MODE_MANUAL_CHANGED:
NotifyPqModeManualChanged(cb_data);
break;
default:
LOGE("%s invalie callback type\n", __FUNCTION__);
break;
}
return;
}
void SignalHdrType(CPQControlCb &cb_data) {
sd_bus_message *msg = nullptr;
int r = sd_bus_message_new_signal(m_bus, &msg, PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE,
PQ_SERVICE_SIGNAL_HDR_TYPE);
if (r >= 0) {
PQControlCb::HdrTypeCb *cb = (PQControlCb::HdrTypeCb *)(&cb_data);
r = sd_bus_message_append(msg, "i", cb->mHdrType);
if (r >= 0)
r = sd_bus_send(m_bus, msg, NULL);
if (r < 0) {
LOGE("Failed to send signal on %s %s %d\n", PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE, r);
}
}
if (msg)
sd_bus_message_unref(msg);
return;
}
void SignalAllmGameMode(CPQControlCb &cb_data) {
sd_bus_message *msg = nullptr;
int r = sd_bus_message_new_signal(m_bus, &msg, PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE,
PQ_SERVICE_SIGNAL_ALLM_GAME_MODE);
if (r >= 0) {
PQControlCb::AllmGameModeCb *cb =
(PQControlCb::AllmGameModeCb *)(&cb_data);
r = sd_bus_message_append(msg, "i", cb->mAllmGameMode);
if (r >= 0)
r = sd_bus_send(m_bus, msg, NULL);
if (r < 0) {
LOGE("Failed to send signal on %s %s %d\n", PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE, r);
}
}
if (msg)
sd_bus_message_unref(msg);
return;
}
void SignalFilmMakerMode(CPQControlCb &cb_data) {
sd_bus_message *msg = nullptr;
int r = sd_bus_message_new_signal(m_bus, &msg, PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE,
PQ_SERVICE_SIGNAL_FILM_MAKER_MODE);
if (r >= 0) {
PQControlCb::FilmMakerModeCb *cb =
(PQControlCb::FilmMakerModeCb *)(&cb_data);
r = sd_bus_message_append(msg, "i", cb->mFilmMakerMode);
if (r >= 0)
r = sd_bus_send(m_bus, msg, NULL);
if (r < 0) {
LOGE("Failed to send signal on %s %s %d\n", PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE, r);
}
}
if (msg)
sd_bus_message_unref(msg);
return;
}
void SignalRefreshRate(CPQControlCb &cb_data) {
sd_bus_message *msg = nullptr;
int r = sd_bus_message_new_signal(m_bus, &msg, PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE,
PQ_SERVICE_SIGNAL_REFRESH_RATE);
if (r >= 0) {
PQControlCb::RefreshRateCb *cb = (PQControlCb::RefreshRateCb *)(&cb_data);
r = sd_bus_message_append(msg, "i", cb->mRefreshRate);
if (r >= 0)
r = sd_bus_send(m_bus, msg, NULL);
if (r < 0) {
LOGE("Failed to send signal on %s %s %d\n", PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE, r);
}
}
if (msg)
sd_bus_message_unref(msg);
return;
}
void SignalPqModeAutoChanged(CPQControlCb &cb_data) {
sd_bus_message *msg = nullptr;
int r = sd_bus_message_new_signal(m_bus, &msg, PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE,
PQ_SERVICE_SIGNAL_PICTURE_MODE);
if (r >= 0) {
PQControlCb::PqModeChangedCb *cb = (PQControlCb::PqModeChangedCb *)(&cb_data);
r = sd_bus_message_append(msg, "iiii", cb->pq_mode_info.pq_mode, cb->pq_mode_info.hdr_type,
cb->pq_mode_info.input_src, cb->pq_mode_info.low_latency);
if (r >= 0)
r = sd_bus_send(m_bus, msg, NULL);
if (r < 0) {
LOGE("Failed to send signal on %s %s %d\n", PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE, r);
}
}
if (msg)
sd_bus_message_unref(msg);
return;
}
void NotifyPqModeManualChanged(CPQControlCb &cb_data) {
sd_bus_message *msg = nullptr;
int r = sd_bus_message_new_signal(m_bus, &msg, PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE,
PQ_SERVICE_SIGNAL_PICTURE_MODE_MANUAL);
if (r >= 0) {
PQControlCb::PictureModeCb *cb = (PQControlCb::PictureModeCb *)(&cb_data);
r = sd_bus_message_append(msg, "i", cb->mPictureMode);
if (r >= 0)
r = sd_bus_send(m_bus, msg, NULL);
if (r < 0) {
LOGE("Failed to send signal on %s %s %d\n", PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE, r);
}
}
if (msg)
sd_bus_message_unref(msg);
return;
}
private:
sd_bus *m_bus;
};
int main() {
sd_bus *bus = nullptr;
sd_event *event = nullptr;
init_pq_logging();
int r = sd_bus_default_system(&bus);
if (r < 0) {
LOGE("Failed to create bus: %s\n", strerror(-r));
goto cleanup;
}
r = sd_bus_add_object_vtable(bus, NULL, PQ_SERVICE_DBUS_OBJECT,
PQ_SERVICE_DBUS_INTERFACE, pqservice_vtable,
NULL);
if (r < 0) {
LOGE("Failed to add object vtable: %s\n", strerror(-r));
goto cleanup;
}
r = sd_bus_request_name(bus, PQ_SERVICE_DBUS_SERVICE, 0LL);
if (r < 0) {
LOGE("Failed to request name: %s\n", strerror(-r));
goto cleanup;
}
r = sd_event_default(&event);
if (r < 0) {
LOGE("Failed to create event: %s\n", strerror(-r));
goto cleanup;
}
r = sd_bus_attach_event(bus, event, 0);
if (r < 0) {
LOGE("Failed to attach event: %s\n", strerror(-r));
goto cleanup;
}
gs_pqctrl = CPQControl::GetInstance();
gs_pqctrl->CPQControlInit();
gs_pqctrl->setObserver(new PqObserver(bus));
sd_event_loop(event);
cleanup:
LOGE("PqService exit!\n");
if (gs_pqctrl) {
gs_pqctrl->CPQControlUnInit();
gs_pqctrl = nullptr;
}
if (event) {
sd_event_exit(event, 0);
sd_event_unref(event);
event = nullptr;
}
if (bus) {
sd_bus_unref(bus);
bus = nullptr;
}
return 0;
}