/*
 * Copyright (c) Atmosphère-NX
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
#include 
#include "impl/powctl_i_power_control_driver.hpp"
#include "impl/powctl_device_management.hpp"
namespace ams::powctl {
    namespace {
        impl::SessionImpl &GetOpenSessionImpl(Session &session) {
            /* AMS_ASSERT(session.has_session); */
            auto &impl = GetReference(session.impl_storage);
            AMS_ASSERT(impl.IsOpen());
            return impl;
        }
    }
    Result GetBatteryChargePercentage(float *out_percent, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryChargePercentage(out_percent, std::addressof(device)));
    }
    Result GetBatteryVoltageFuelGaugePercentage(float *out_percent, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryVoltageFuelGaugePercentage(out_percent, std::addressof(device)));
    }
    Result GetBatteryFullCapacity(int *out_mah, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryFullCapacity(out_mah, std::addressof(device)));
    }
    Result GetBatteryRemainingCapacity(int *out_mah, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryRemainingCapacity(out_mah, std::addressof(device)));
    }
    Result SetBatteryChargePercentageMinimumAlertThreshold(Session &session, float percentage) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryChargePercentageMinimumAlertThreshold(std::addressof(device), percentage));
    }
    Result SetBatteryChargePercentageMaximumAlertThreshold(Session &session, float percentage) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryChargePercentageMaximumAlertThreshold(std::addressof(device), percentage));
    }
    Result SetBatteryVoltageFuelGaugePercentageMinimumAlertThreshold(Session &session, float percentage) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryVoltageFuelGaugePercentageMinimumAlertThreshold(std::addressof(device), percentage));
    }
    Result SetBatteryVoltageFuelGaugePercentageMaximumAlertThreshold(Session &session, float percentage) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryVoltageFuelGaugePercentageMaximumAlertThreshold(std::addressof(device), percentage));
    }
    Result SetBatteryFullChargeThreshold(Session &session, float percentage) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryFullChargeThreshold(std::addressof(device), percentage));
    }
    Result GetBatteryAverageCurrent(int *out_ma, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryAverageCurrent(out_ma, std::addressof(device)));
    }
    Result GetBatteryCurrent(int *out_ma, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryCurrent(out_ma, std::addressof(device)));
    }
    Result GetBatteryInternalState(void *dst, size_t *out_size, Session &session, size_t dst_size) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryInternalState(dst, out_size, std::addressof(device), dst_size));
    }
    Result SetBatteryInternalState(Session &session, const void *src, size_t src_size) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryInternalState(std::addressof(device), src, src_size));
    }
    Result GetBatteryNeedToRestoreParameters(bool *out, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryNeedToRestoreParameters(out, std::addressof(device)));
    }
    Result SetBatteryNeedToRestoreParameters(Session &session, bool en) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryNeedToRestoreParameters(std::addressof(device), en));
    }
    Result IsBatteryI2cShutdownEnabled(bool *out, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().IsBatteryI2cShutdownEnabled(out, std::addressof(device)));
    }
    Result SetBatteryI2cShutdownEnabled(Session &session, bool en) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryI2cShutdownEnabled(std::addressof(device), en));
    }
    Result IsBatteryPresent(bool *out, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().IsBatteryPresent(out, std::addressof(device)));
    }
    Result GetBatteryCycles(int *out, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryCycles(out, std::addressof(device)));
    }
    Result SetBatteryCycles(Session &session, int cycles) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryCycles(std::addressof(device), cycles));
    }
    Result GetBatteryAge(float *out_percent, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryAge(out_percent, std::addressof(device)));
    }
    Result GetBatteryTemperature(float *out_c, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryTemperature(out_c, std::addressof(device)));
    }
    Result GetBatteryMaximumTemperature(float *out_c, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryMaximumTemperature(out_c, std::addressof(device)));
    }
    Result SetBatteryTemperatureMinimumAlertThreshold(Session &session, float c) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryTemperatureMinimumAlertThreshold(std::addressof(device), c));
    }
    Result SetBatteryTemperatureMaximumAlertThreshold(Session &session, float c) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryTemperatureMaximumAlertThreshold(std::addressof(device), c));
    }
    Result GetBatteryVCell(int *out_mv, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryVCell(out_mv, std::addressof(device)));
    }
    Result GetBatteryAverageVCell(int *out_mv, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryAverageVCell(out_mv, std::addressof(device)));
    }
    Result GetBatteryAverageVCellTime(TimeSpan *out, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryAverageVCellTime(out, std::addressof(device)));
    }
    Result GetBatteryOpenCircuitVoltage(int *out_mv, Session &session) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().GetBatteryOpenCircuitVoltage(out_mv, std::addressof(device)));
    }
    Result SetBatteryVoltageMinimumAlertThreshold(Session &session, int mv) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryVoltageMinimumAlertThreshold(std::addressof(device), mv));
    }
    Result SetBatteryVoltageMaximumAlertThreshold(Session &session, int mv) {
        /* Get the session impl. */
        auto &impl = GetOpenSessionImpl(session);
        /* Check the access mode. */
        R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied());
        /* Get the device. */
        auto &device = impl.GetDevice().SafeCastTo();
        /* Call into the driver. */
        R_RETURN(device.GetDriver().SafeCastTo().SetBatteryVoltageMaximumAlertThreshold(std::addressof(device), mv));
    }
}