~aleteoryx/muditaos

5f34f5ad7cefdcc72d4a9bac1d9554885c0ac0af — Kuba 4 years ago 992b0ea
[EGD-6889] Fix Fixed network time handling

Fixed Network time handling. Automati Date and Time setting
is handled in service-cellular. Enabling and diasbling time
repotring is added.
M module-services/service-cellular/CMakeLists.txt => module-services/service-cellular/CMakeLists.txt +1 -0
@@ 6,6 6,7 @@ set(SOURCES
    src/State.cpp
    src/SimCard.cpp
    src/CallManager.cpp
    src/NetworkTime.cpp

    CellularCall.cpp
    CellularServiceAPI.cpp

M module-services/service-cellular/CellularUrcHandler.cpp => module-services/service-cellular/CellularUrcHandler.cpp +1 -10
@@ 14,11 14,6 @@

using namespace at::urc;

// this static function will be replaced by Settings API
static bool isSettingsAutomaticTimeSyncEnabled()
{
    return true;
}

void CellularUrcHandler::Handle(Clip &urc)
{


@@ 103,14 98,10 @@ void CellularUrcHandler::Handle(Ctze &urc)
        return;
    }

    if (isSettingsAutomaticTimeSyncEnabled()) {
        auto msg = std::make_shared<CellularTimeNotificationMessage>(
            urc.getGMTTime(), urc.getTimeZoneOffset(), urc.getTimeZoneString());
        cellularService.bus.sendUnicast(msg, service::name::service_time);
    }
    else {
        LOG_DEBUG("Timezone sync disabled.");
    }

    urc.setHandled(true);
}


M module-services/service-cellular/ServiceCellular.cpp => module-services/service-cellular/ServiceCellular.cpp +8 -11
@@ 73,6 73,7 @@
#include <service-desktop/DesktopMessages.hpp>
#include <service-desktop/DeveloperModeMessage.hpp>
#include <service-appmgr/model/ApplicationManager.hpp>
#include <service-time/service-time/TimeMessage.hpp>
#include <task.h>
#include <ucs2/UCS2.hpp>
#include <utf8/UTF8.hpp>


@@ 184,12 185,6 @@ ServiceCellular::~ServiceCellular()
    LOG_INFO("[ServiceCellular] Cleaning resources");
}

// this static function will be replaced by Settings API
static bool isSettingsAutomaticTimeSyncEnabled()
{
    return true;
}

void ServiceCellular::SleepTimerHandler()
{
    auto currentTime                = cpp_freertos::Ticks::TicksToMs(cpp_freertos::Ticks::GetTicks());


@@ 302,6 297,7 @@ void ServiceCellular::registerMessageHandlers()
    });

    priv->connectSimCard();
    priv->connectNetworkTime();

    connect(typeid(CellularStartOperatorsScanMessage), [&](sys::Message *request) -> sys::MessagePointer {
        auto msg = static_cast<CellularStartOperatorsScanMessage *>(request);


@@ 386,6 382,8 @@ void ServiceCellular::registerMessageHandlers()
        auto msg = static_cast<sdesktop::developerMode::DeveloperModeRequest *>(request);
        if (typeid(*msg->event.get()) == typeid(sdesktop::developerMode::CellularHotStartEvent)) {
            priv->simCard->setChannel(nullptr);
            priv->networkTime->setChannel(nullptr);

            cmux->closeChannels();
            ///> change state - simulate hot start
            handle_power_up_request();


@@ 854,7 852,7 @@ bool ServiceCellular::handle_audio_conf_procedure()
            LOG_DEBUG("[ServiceCellular] Modem is fully operational");

            priv->simCard->setChannel(cmux->get(CellularMux::Channel::Commands));

            priv->networkTime->setChannel(cmux->get(CellularMux::Channel::Commands));
            // open channel - notifications
            DLCChannel *notificationsChannel = cmux->get(CellularMux::Channel::Notifications);
            if (notificationsChannel != nullptr) {


@@ 1398,10 1396,9 @@ bool ServiceCellular::handle_URCReady()
{
    auto channel = cmux->get(CellularMux::Channel::Commands);
    bool ret     = true;
    if (isSettingsAutomaticTimeSyncEnabled()) {
        ret = ret && channel->cmd(at::AT::ENABLE_TIME_ZONE_UPDATE);
        ret = ret && channel->cmd(at::AT::SET_TIME_ZONE_REPORTING);
    }

    priv->requestNetworkTimeSettings();

    ret = ret && channel->cmd(at::AT::ENABLE_NETWORK_REGISTRATION_URC);

    bus.sendMulticast<cellular::msg::notification::ModemStateChanged>(cellular::api::ModemState::Ready);

A module-services/service-cellular/src/NetworkTime.cpp => module-services/service-cellular/src/NetworkTime.cpp +62 -0
@@ 0,0 1,62 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NetworkTime.hpp"

#include <modem/BaseChannel.hpp>
#include <at/ATFactory.hpp>
#include <at/UrcFactory.hpp>

#include <service-time/service-time/TimeMessage.hpp>

#include <log/Logger.hpp>
namespace cellular::service
{

    void NetworkTime::setChannel(at::BaseChannel *channel)
    {
        this->channel = channel;
    }

    std::shared_ptr<sys::Message> NetworkTime::createSettingsRequest()
    {
        return std::make_shared<stm::message::GetAutomaticDateAndTimeRequest>();
    }

    void NetworkTime::processSettings(bool newValue)
    {
        if (isAutomaticDateAndTime == newValue) {
            return;
        }
        isAutomaticDateAndTime = newValue;

        if (isAutomaticDateAndTime) {
            enableTimeReporting();
        }
        else {
            disableTimeReporting();
        }
    }

    void NetworkTime::enableTimeReporting()
    {
        if (channel == nullptr) {
            LOG_ERROR("No channel provided. Request ignored");
            return;
        }
        channel->cmd(at::AT::ENABLE_TIME_ZONE_UPDATE);
        channel->cmd(at::AT::SET_TIME_ZONE_REPORTING);
        channel->cmd(at::AT::CFUN_DISABLE_TRANSMITTING);
        channel->cmd(at::AT::CFUN_FULL_FUNCTIONALITY);
    }

    void NetworkTime::disableTimeReporting()
    {
        if (channel == nullptr) {
            LOG_ERROR("No channel provided. Request ignored");
            return;
        }
        channel->cmd(at::AT::DISABLE_TIME_ZONE_UPDATE);
        channel->cmd(at::AT::DISABLE_TIME_ZONE_REPORTING);
    }
}; // namespace cellular::service

A module-services/service-cellular/src/NetworkTime.hpp => module-services/service-cellular/src/NetworkTime.hpp +49 -0
@@ 0,0 1,49 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <memory>

namespace at
{
    class Cmd;
    class BaseChannel;
} // namespace at

namespace sys
{
    class Message;
}
namespace cellular::service
{
    class NetworkTime
    {
      public:
        /** Set cmd channel
         * \param channel channel (or nullptr to block communication)
         */
        void setChannel(at::BaseChannel *channel);
        /**
         * Requests settings related to Network Time synchronisation
         */
        std::shared_ptr<sys::Message> createSettingsRequest();
        /**
         * Handles settings related to Network Time synchronisation change
         * @param newValue Autoamitc Date and Time setting value
         */
        void processSettings(bool newValue);

      private:
        at::BaseChannel *channel    = nullptr;
        bool isAutomaticDateAndTime = false;
        /**
         * It enables Network Time reporting
         */
        void enableTimeReporting();
        /**
         * It disables Network Time reporting
         */
        void disableTimeReporting();
    };
} // namespace cellular::service

M module-services/service-cellular/src/ServiceCellularPriv.cpp => module-services/service-cellular/src/ServiceCellularPriv.cpp +28 -1
@@ 9,10 9,16 @@
#include <service-evtmgr/EVMessages.hpp>
#include <service-evtmgr/Constants.hpp>

#include <service-time/service-time/TimeMessage.hpp>
#include <service-time/Constants.hpp>

using service::name::service_time;

namespace cellular::internal
{
    ServiceCellularPriv::ServiceCellularPriv(ServiceCellular *owner)
        : owner{owner}, simCard{std::make_unique<SimCard>()}, state{std::make_unique<State>(owner)}
        : owner{owner}, simCard{std::make_unique<SimCard>()}, state{std::make_unique<State>(owner)},
          networkTime{std::make_unique<NetworkTime>()}
    {
        initSimCard();
    }


@@ 88,4 94,25 @@ namespace cellular::internal
                       });
    }

    void ServiceCellularPriv::connectNetworkTime()
    {
        owner->connect(typeid(stm::message::AutomaticDateAndTimeChangedMessage),
                       [&](sys::Message *request) -> sys::MessagePointer {
                           auto message = static_cast<stm::message::AutomaticDateAndTimeChangedMessage *>(request);
                           networkTime->processSettings(message->getValue());
                           return sys::MessageNone{};
                       });
        owner->connect(typeid(stm::message::GetAutomaticDateAndTimeResponse),
                       [&](sys::Message *request) -> sys::MessagePointer {
                           auto message = static_cast<stm::message::GetAutomaticDateAndTimeResponse *>(request);
                           networkTime->processSettings(message->isAutomaticDateAndTime());
                           return sys::MessageNone{};
                       });
    }

    void ServiceCellularPriv::requestNetworkTimeSettings()
    {
        owner->bus.sendUnicast(networkTime->createSettingsRequest(), service_time);
    }

} // namespace cellular::internal

M module-services/service-cellular/src/ServiceCellularPriv.hpp => module-services/service-cellular/src/ServiceCellularPriv.hpp +6 -0
@@ 7,9 7,11 @@
#include <service-cellular/State.hpp>

#include "SimCard.hpp"
#include "NetworkTime.hpp"

namespace cellular::internal
{
    using service::NetworkTime;
    using service::SimCard;
    using service::State;



@@ 19,6 21,7 @@ namespace cellular::internal

        std::unique_ptr<SimCard> simCard;
        std::unique_ptr<State> state;
        std::unique_ptr<NetworkTime> networkTime;

        State::PowerState nextPowerState = State::PowerState::Off;



@@ 26,6 29,9 @@ namespace cellular::internal
        ServiceCellularPriv(ServiceCellular *owner);

        void connectSimCard();
        void connectNetworkTime();

        void requestNetworkTimeSettings();

      private:
        void initSimCard();

M module-services/service-time/ServiceTime.cpp => module-services/service-time/ServiceTime.cpp +7 -0
@@ 104,6 104,8 @@ namespace stm
            timeManager->handleTimeChangeRequest(message->getTime());
            return std::make_shared<sys::ResponseMessage>();
        });
        connect(typeid(stm::message::GetAutomaticDateAndTimeRequest),
                [&](sys::Message *request) -> sys::MessagePointer { return handleGetAutomaticDateAndTimeRequest(); });
    }

    auto ServiceTime::handleSetAutomaticDateAndTimeRequest(sys::Message *request)


@@ 215,4 217,9 @@ namespace stm
        timeManager->handleTimezoneChangeRequest(timezoneRules);
    }

    auto ServiceTime::handleGetAutomaticDateAndTimeRequest() -> std::shared_ptr<sys::ResponseMessage>
    {
        return std::make_shared<stm::message::GetAutomaticDateAndTimeResponse>(stm::api::isAutomaticDateAndTime());
    }

} /* namespace stm */

M module-services/service-time/ServiceTime.hpp => module-services/service-time/ServiceTime.hpp +1 -0
@@ 39,6 39,7 @@ namespace stm
        auto handleSetDateFormatRequest(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>;
        auto handleSetTimezoneRequest(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>;
        auto handleCellularTimeNotificationMessage(sys::Message *request) -> std::shared_ptr<sys::ResponseMessage>;
        auto handleGetAutomaticDateAndTimeRequest() -> std::shared_ptr<sys::ResponseMessage>;
        void initStaticData();

      public:

M module-services/service-time/service-time/TimeMessage.hpp => module-services/service-time/service-time/TimeMessage.hpp +22 -0
@@ 162,4 162,26 @@ namespace stm::message
      private:
        std::string timezone;
    };

    class GetAutomaticDateAndTimeRequest : public sys::DataMessage
    {
      public:
        explicit GetAutomaticDateAndTimeRequest() : sys::DataMessage(MessageType::MessageTypeUninitialized)
        {}
    };

    class GetAutomaticDateAndTimeResponse : public sys::ResponseMessage
    {
      public:
        explicit GetAutomaticDateAndTimeResponse(bool settingValue)
            : sys::ResponseMessage(), isAutomaticDateAndTimeOn(settingValue)
        {}
        auto isAutomaticDateAndTime() const noexcept
        {
            return isAutomaticDateAndTimeOn;
        }

      private:
        bool isAutomaticDateAndTimeOn = false;
    };
} // namespace stm::message