~aleteoryx/muditaos

8204cba1ab2566d53736e729b3929df59f0889cd — Wojtek Rzepecki 5 years ago 61f4ab3
[EGD-5575] Add battery file test

Battery file existence test for
harness test suit. Minor changes to
file saving applied.
M module-bsp/board/linux/battery-charger/battery_charger.cpp => module-bsp/board/linux/battery-charger/battery_charger.cpp +5 -2
@@ 49,8 49,8 @@ namespace bsp::battery_charger
                    std::uint8_t notification = 0;
                    switch (static_cast<char>(buff[0])) {
                    case chargerPlugStateChange:
                        notification = static_cast<std::uint8_t>(batteryIRQSource::INOKB);
                        plugged      = !plugged;
                        notification      = static_cast<std::uint8_t>(batteryIRQSource::INOKB);
                        plugged           = !plugged;
                        targetQueueHandle = IRQQueueHandle;
                        break;
                    case batteryLevelUp:


@@ 144,4 144,7 @@ namespace bsp::battery_charger
        return static_cast<std::uint16_t>(batteryINTBSource::SOCOnePercentChange);
    }

    void chargingFinishedAction()
    {}

} // namespace bsp::battery_charger

M module-bsp/board/rt1051/bsp/battery-charger/battery_charger.cpp => module-bsp/board/rt1051/bsp/battery-charger/battery_charger.cpp +47 -24
@@ 20,7 20,7 @@ namespace bsp::battery_charger
    {
        constexpr std::uint32_t i2cSubaddresSize = 1;

        const auto cfgFile              = purefs::dir::getCurrentOSPath() / "batteryFuelGaugeConfig.cfg";
        const auto cfgFile              = purefs::dir::getUserDiskPath() / "batteryFuelGaugeConfig.cfg";
        constexpr auto registersToStore = 0xFF + 1;

        constexpr std::uint16_t ENABLE_ALL_IRQ_MASK = 0xF8;


@@ 37,7 37,7 @@ namespace bsp::battery_charger
        constexpr std::uint8_t maxTemperatureDegrees = 50;
        constexpr std::uint8_t minTemperatureDegrees = 5;

        constexpr std::uint16_t maxVoltagemV = 4350;
        constexpr std::uint16_t maxVoltagemV = 4400;
        constexpr std::uint16_t minVoltagemV = 3600;

        constexpr auto currentSenseGain = 0.15625;  // mA


@@ 227,36 227,22 @@ namespace bsp::battery_charger
            return batteryRetval::OK;
        }

        batteryRetval loadConfiguration()
        batteryRetval resetFuelGaugeModel()
        {
            std::ifstream file(cfgFile.c_str(), std::ios::binary | std::ios::in);
            if (!file.is_open()) {
                LOG_WARN("Configuration file [%s] could not be opened. Loading initial configuration.",
                         cfgFile.c_str());
                if (configureFuelGaugeBatteryModel() == batteryRetval::OK) {
                    return batteryRetval::OK;
                }
                else {
                    return batteryRetval::ChargerError;
                }
            }
            auto regVal = fuelGaugeRead(Registers::CONFIG2_REG);

            std::uint16_t regVal;
            for (auto i = 0; i < registersToStore; ++i) {
                file.read(reinterpret_cast<char *>(&regVal), sizeof(std::uint16_t));
                if (fuelGaugeWrite(static_cast<Registers>(i), regVal) != kStatus_Success) {
                    LOG_ERROR("Writing register 0x%x failed.", i);
                    file.close();
                    return batteryRetval::ChargerError;
                }
            }
            file.close();
            std::uint16_t toWrite = regVal.second | static_cast<std::uint16_t>(CONFIG2::LdMdl);

            if (fuelGaugeWrite(Registers::CONFIG2_REG, toWrite) != kStatus_Success) {
                LOG_ERROR("resetFuelGaugeModel failed.");
                return batteryRetval::ChargerError;
            }
            return batteryRetval::OK;
        }

        batteryRetval storeConfiguration()
        {
            LOG_INFO("Storing fuel gauge configuration...");
            std::ofstream file(cfgFile.c_str(), std::ios::binary | std::ios::out);
            if (!file.is_open()) {
                LOG_WARN("Configuration file [%s] could not be opened.", cfgFile.c_str());


@@ 277,6 263,37 @@ namespace bsp::battery_charger
            return batteryRetval::OK;
        }

        batteryRetval loadConfiguration()
        {
            std::ifstream file(cfgFile.c_str(), std::ios::binary | std::ios::in);
            if (!file.is_open()) {
                LOG_WARN("Configuration file [%s] could not be opened. Loading initial configuration.",
                         cfgFile.c_str());
                if (configureFuelGaugeBatteryModel() == batteryRetval::OK) {
                    storeConfiguration();
                    resetFuelGaugeModel();
                    return batteryRetval::OK;
                }
                else {
                    return batteryRetval::ChargerError;
                }
            }

            std::uint16_t regVal;
            for (auto i = 0; i < registersToStore; ++i) {
                file.read(reinterpret_cast<char *>(&regVal), sizeof(std::uint16_t));
                if (fuelGaugeWrite(static_cast<Registers>(i), regVal) != kStatus_Success) {
                    LOG_ERROR("Writing register 0x%x failed.", i);
                    file.close();
                    return batteryRetval::ChargerError;
                }
            }
            file.close();
            resetFuelGaugeModel();

            return batteryRetval::OK;
        }

        batteryRetval setChargingDischargingThresholds(std::uint8_t chargedThresholdPercent,
                                                       std::uint8_t dischargedThresholdPercent)
        {


@@ 534,6 551,12 @@ namespace bsp::battery_charger
        fuelGaugeWrite(Registers::STATUS_REG, 0x0000);
    }

    void chargingFinishedAction()
    {
        LOG_DEBUG("Charging finished.");
        storeConfiguration();
    }

    BaseType_t INOKB_IRQHandler()
    {
        BaseType_t xHigherPriorityTaskWoken = pdFALSE;

M module-bsp/bsp/battery-charger/battery_charger.hpp => module-bsp/bsp/battery-charger/battery_charger.hpp +2 -0
@@ 58,6 58,8 @@ namespace bsp::battery_charger

	std::uint16_t getStatusRegister();

	void chargingFinishedAction();

	BaseType_t INOKB_IRQHandler();

	BaseType_t INTB_IRQHandler();

M module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp => module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp +8 -1
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "FilesystemEndpoint.hpp"


@@ 64,6 64,13 @@ auto FilesystemEndpoint::run(Context &context) -> sys::ReturnCodes
            LOG_ERROR("download command failed, can't write %" PRIu32 " bytes to: %s", fileSize, tmpFilePath.c_str());
        }
    }
    else if (cmd == parserFSM::json::filesystem::commands::checkFile) {
        fs::path filePath = context.getBody()[parserFSM::json::fileName].string_value();
        LOG_DEBUG("Checking file: %s", filePath.c_str());

        context.setResponseBody(json11::Json::object{{json::fileExists, std::filesystem::exists(filePath)}});
        returnCode = sys::ReturnCodes::Success;
    }
    else {
        LOG_ERROR("unknown command: %s", cmd.c_str());
    }

M module-services/service-desktop/parser/ParserUtils.hpp => module-services/service-desktop/parser/ParserUtils.hpp +3 -1
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once


@@ 131,6 131,7 @@ namespace parserFSM
        inline constexpr auto errorCode        = "errorCode";
        inline constexpr auto statusCode       = "statusCode";
        inline constexpr auto updateHistory    = "updateHistory";
        inline constexpr auto fileExists       = "fileExists";

        namespace filesystem
        {


@@ 140,6 141,7 @@ namespace parserFSM
                inline constexpr auto upload   = "upload";
                inline constexpr auto rm       = "rm";
                inline constexpr auto download = "download";
                inline constexpr auto checkFile = "checkFile";
            } // namespace commands
        }     // namespace filesystem


M module-services/service-evtmgr/WorkerEvent.cpp => module-services/service-evtmgr/WorkerEvent.cpp +3 -0
@@ 109,6 109,9 @@ bool WorkerEvent::handleMessage(uint32_t queueID)
            bsp::battery_charger::clearAllIRQs();
            auto message     = std::make_shared<sevm::BatteryPlugMessage>();
            message->plugged = bsp::battery_charger::getChargeStatus();
            if (!message->plugged) {
                bsp::battery_charger::chargingFinishedAction();
            }
            sys::Bus::SendUnicast(message, service::name::evt_manager, this->service);
        }
    }

A test/pytest/service-desktop/test_battery_file.py => test/pytest/service-desktop/test_battery_file.py +13 -0
@@ 0,0 1,13 @@
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
import pytest
from harness.interface.defs import status

@pytest.mark.rt1051
@pytest.mark.service_desktop_test
def test_battery_file(harness):
    body = {"command" : "checkFile" , "fileName" : "/sys/user/batteryFuelGaugeConfig.cfg"}
    ret = harness.endpoint_request("filesystemUpload", "post", body)

    assert ret["status"] == status["OK"]
    assert ret["body"]["fileExists"] == True