~aleteoryx/muditaos

0fad72313568af2b82161eb832edfa2b97569c19 — SP2FET 5 years ago 20c7efd
[EGD-5457] Add harness test for bluetooth on/off

There is no functional tests related to the Bluetooth. As the
refactor is on the way,we need to have a tool to check if it works
By the way fixed worker deinit
M module-bluetooth/Bluetooth/BluetoothWorker.cpp => module-bluetooth/Bluetooth/BluetoothWorker.cpp +4 -2
@@ 89,8 89,6 @@ auto BluetoothWorker::run() -> bool
        bluetooth::GAP::set_visibility(
            std::visit(bluetooth::BoolVisitor(), settings->getValue(bluetooth::Settings::Visibility)));

        settings->setValue(bluetooth::Settings::State, static_cast<int>(BluetoothStatus::State::On));

        settings->onLinkKeyAdded = [this](std::string addr) {
            for (auto &device : bluetooth::GAP::devices) {
                if (bd_addr_to_str(device.address) == addr) {


@@ 290,3 288,7 @@ void BluetoothWorker::setDeviceAddress(bd_addr_t addr)
    bluetooth::GAP::do_pairing(addr);
    currentProfile->setDeviceAddress(addr);
}
auto BluetoothWorker::deinit() -> bool
{
    return Worker::deinit();
}

M module-bluetooth/Bluetooth/BluetoothWorker.hpp => module-bluetooth/Bluetooth/BluetoothWorker.hpp +1 -1
@@ 117,7 117,7 @@ class BluetoothWorker : private sys::Worker
    unsigned long active_features;
    void stopScan();
    void setDeviceAddress(bd_addr_t addr);

    auto deinit() -> bool override;
    std::shared_ptr<bluetooth::Profile> currentProfile;
    std::shared_ptr<bluetooth::SettingsHolder> settings;
    std::vector<Devicei> pairedDevices;

M module-bluetooth/Bluetooth/interface/BluetoothDriver.cpp => module-bluetooth/Bluetooth/interface/BluetoothDriver.cpp +2 -0
@@ 91,6 91,8 @@ namespace bluetooth
            }
            gap_local_bd_addr(addr);
            LOG_INFO("BTstack up and running at %s", bd_addr_to_str(addr));
            bluetooth::KeyStorage::settings->setValue(bluetooth::Settings::State,
                                                      static_cast<int>(BluetoothStatus::State::On));
            break;
        case HCI_EVENT_COMMAND_COMPLETE:
            if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)) {

M module-services/service-bluetooth/CMakeLists.txt => module-services/service-bluetooth/CMakeLists.txt +1 -0
@@ 19,5 19,6 @@ target_include_directories(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
    PUBLIC
        module-bluetooth
        service-desktop
        module-sys
)

M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +19 -4
@@ 21,6 21,8 @@

#include <bits/exception.h>
#include <utility>
#include <service-desktop/service-desktop/DesktopMessages.hpp>
#include <service-desktop/service-desktop/Constants.hpp>

ServiceBluetooth::ServiceBluetooth() : sys::Service(service::name::bluetooth)
{


@@ 80,11 82,24 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
        return std::make_shared<message::bluetooth::ResponseStatus>(btStatus);
    });

    connect(sdesktop::developerMode::DeveloperModeRequest(), [&](sys::Message *msg) {
        using namespace sdesktop::developerMode;
        auto req = static_cast<DeveloperModeRequest *>(msg);
        if (typeid(*req->event) == typeid(BluetoothStatusRequestEvent)) {
            auto state   = std::visit(bluetooth::IntVisitor(), settingsHolder->getValue(bluetooth::Settings::State));
            auto event   = std::make_unique<BluetoothStatusRequestEvent>(state);
            auto message = std::make_shared<DeveloperModeRequest>(std::move(event));
            sys::Bus::SendUnicast(std::move(message), service::name::service_desktop, this);
        }

        return sys::MessageNone{};
    });

    settingsHolder->onStateChange = [this]() {
        auto initialState =
            std::visit(bluetooth::IntVisitor(), this->settingsHolder->getValue(bluetooth::Settings::State));
        auto initialState = std::visit(bluetooth::IntVisitor(), settingsHolder->getValue(bluetooth::Settings::State));
        if (static_cast<BluetoothStatus::State>(initialState) == BluetoothStatus::State::On) {
            this->worker->run();
            settingsHolder->setValue(bluetooth::Settings::State, static_cast<int>(BluetoothStatus::State::Off));
            worker->run();
        }
    };



@@ 93,7 108,7 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()

sys::ReturnCodes ServiceBluetooth::DeinitHandler()
{

    worker->deinit();
    return sys::ReturnCodes::Success;
}


M module-services/service-bluetooth/service-bluetooth/BluetoothMessage.hpp => module-services/service-bluetooth/service-bluetooth/BluetoothMessage.hpp +1 -1
@@ 23,8 23,8 @@ struct BluetoothStatus
{
    enum class State
    {
        On,
        Off,
        On,
        Error,
        None
    } state;

M module-services/service-desktop/DesktopMessages.cpp => module-services/service-desktop/DesktopMessages.cpp +7 -0
@@ 40,4 40,11 @@ namespace sdesktop::developerMode
    DeveloperModeRequest::DeveloperModeRequest() : sys::DataMessage(MessageType::DeveloperModeRequest)
    {}

    BluetoothStatusRequestEvent::BluetoothStatusRequestEvent(int state)
    {
        context.setResponseStatus(http::Code::OK);
        context.setEndpoint(EndpointType::developerMode);
        context.setResponseBody(json11::Json::object{{json::developerMode::btState, state}});
    }

} // namespace sdesktop::developerMode

M module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp => module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp +22 -0
@@ 10,6 10,7 @@
#include <Service/Bus.hpp>
#include <service-cellular/CellularMessage.hpp>
#include <service-cellular/ServiceCellular.hpp>
#include <service-bluetooth/messages/Status.hpp>

#include <gui/Common.hpp>
#include <service-appmgr/Actions.hpp>


@@ 47,6 48,27 @@ auto DeveloperModeHelper::processPutRequest(Context &context) -> sys::ReturnCode
        auto msg   = std::make_shared<sdesktop::developerMode::DeveloperModeRequest>(std::move(event));
        sys::Bus::SendUnicast(std::move(msg), "ApplicationDesktop", ownerServicePtr);
    }
    else if (body[json::developerMode::btState].bool_value()) {

        auto event = std::make_unique<sdesktop::developerMode::BluetoothStatusRequestEvent>();
        auto msg   = std::make_shared<sdesktop::developerMode::DeveloperModeRequest>(std::move(event));
        sys::Bus::SendUnicast(std::move(msg), "ServiceBluetooth", ownerServicePtr);
    }
    else if (auto state = body[json::developerMode::btCommand].string_value(); !state.empty()) {
        BluetoothMessage::Request request;
        if (state == json::developerMode::btOn) {
            request = BluetoothMessage::Request::Start;
            LOG_INFO("turning on BT from harness!");
        }
        else {
            request = BluetoothMessage::Request::Stop;
            LOG_INFO("turning off BT from harness!");
        }
        std::shared_ptr<BluetoothMessage> msg = std::make_shared<BluetoothMessage>(request);
        sys::Bus::SendUnicast(std::move(msg), "ServiceBluetooth", ownerServicePtr);

        MessageHandler::putToSendQueue(context.createSimpleResponse());
    }
    else {
        context.setResponseStatus(http::Code::BadRequest);
        MessageHandler::putToSendQueue(context.createSimpleResponse());

M module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.hpp => module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.hpp +3 -0
@@ 39,5 39,8 @@ namespace parserFSM
        inline constexpr auto AT            = "AT";
        inline constexpr auto focus         = "focus";
        inline constexpr auto isLocked      = "isLocked";
        inline constexpr auto btState       = "btState";
        inline constexpr auto btOn          = "on";
        inline constexpr auto btCommand     = "btCommand";
    }
} // namespace parserFSM

M module-services/service-desktop/service-desktop/DesktopMessages.hpp => module-services/service-desktop/service-desktop/DesktopMessages.hpp +7 -0
@@ 98,6 98,13 @@ namespace sdesktop
            explicit ScreenlockCheckEvent(bool isLocked);
        };

        class BluetoothStatusRequestEvent : public Event
        {
          public:
            BluetoothStatusRequestEvent() = default;
            explicit BluetoothStatusRequestEvent(int state);
        };

        class DeveloperModeRequest : public sys::DataMessage
        {
          public:

M test/pytest/conftest.py => test/pytest/conftest.py +1 -1
@@ 54,7 54,7 @@ def harness(request):
    Try to init one Pure phone with serial port path or automatically
    '''
    port_name = request.config.option.port
    TIMEOUT = min(1, request.config.option.timeout)
    TIMEOUT = max(1, request.config.option.timeout)
    timeout_started = time.time()

    RETRY_EVERY_SECONDS = 1.0

A test/pytest/service-desktop/test_bluetooth.py => test/pytest/service-desktop/test_bluetooth.py +33 -0
@@ 0,0 1,33 @@
# 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
from harness import log
import time


@pytest.mark.rt1051
@pytest.mark.service_desktop_test
def test_bluetooth(harness):

    body = {"btState": True}
    ret = harness.endpoint_request("developerMode", "put", body)
    print(ret)
    assert ret["status"] == status["OK"]
    if ret["body"]["btState"] == 0:

        log.info("BT turned off, turning on...")
        body = {"btCommand": "on"}
        ret = harness.endpoint_request("developerMode", "put", body)

        time.sleep(5)
        body = {"btState": True}
        ret = harness.endpoint_request("developerMode", "put", body)

        assert ret["status"] == status["OK"]

    assert ret["body"]["btState"] == 1