~aleteoryx/muditaos

6e9030e9428f5318397ae551f4c02a44c35382f6 — Lefucjusz 3 years ago 2d7f9ad
[MOS-528] Fixed BT disconnect option

Fix of the issue that after connecting
to Bluetooth device it was impossible
to disconnect using Disconnect button.
Refactor of connect/disconnect events
naming (and all names related).
M module-bluetooth/Bluetooth/AbstractController.hpp => module-bluetooth/Bluetooth/AbstractController.hpp +4 -4
@@ 11,8 11,8 @@ namespace bluetooth::event
    struct GetDevicesAvailable;
    struct VisibilityOn;
    struct VisibilityOff;
    struct ConnectAudio;
    struct DisconnectAudio;
    struct Connect;
    struct Disconnect;
    struct PowerOn;
    struct PowerOff;
    struct ShutDown;


@@ 47,8 47,8 @@ namespace bluetooth
        virtual void handle(const bluetooth::event::GetDevicesAvailable &evt) = 0;
        virtual void handle(const bluetooth::event::VisibilityOn &evt)        = 0;
        virtual void handle(const bluetooth::event::VisibilityOff &evt)       = 0;
        virtual void handle(const bluetooth::event::ConnectAudio &evt)        = 0;
        virtual void handle(const bluetooth::event::DisconnectAudio &evt)     = 0;
        virtual void handle(const bluetooth::event::Connect &evt)             = 0;
        virtual void handle(const bluetooth::event::Disconnect &evt)          = 0;
        virtual void handle(const bluetooth::event::PowerOn &evt)             = 0;
        virtual void handle(const bluetooth::event::PowerOff &evt)            = 0;
        virtual void handle(const bluetooth::event::ShutDown &evt)            = 0;

M module-bluetooth/Bluetooth/BluetoothStateMachine.hpp => module-bluetooth/Bluetooth/BluetoothStateMachine.hpp +15 -14
@@ 151,11 151,20 @@ namespace bluetooth
        }
    } constexpr HandleDrop;

    struct HandleConnect
    {
        void operator()(std::shared_ptr<AbstractCommandHandler> handler, bluetooth::event::Connect evt)
        {
            handler->connect(evt.device);
        }

    } constexpr HandleConnect;

    struct HandleDisconnect
    {
        void operator()(std::shared_ptr<AbstractCommandHandler> handler)
        {
            handler->disconnectAudioConnection();
            handler->disconnect();
        }

    } constexpr HandleDisconnect;


@@ 178,15 187,6 @@ namespace bluetooth

    } constexpr HandleUnsetVisibility;

    struct EstablishAudioConnection
    {
        void operator()(std::shared_ptr<AbstractCommandHandler> handler, bluetooth::event::ConnectAudio evt)
        {
            handler->establishAudioConnection(evt.device);
        }

    } constexpr EstablishAudioConnection;

    struct StartRinging
    {
        void operator()(std::shared_ptr<bluetooth::BaseProfileManager> profileManager)


@@ 354,7 354,8 @@ namespace bluetooth

                        state<Idle> + sml::event<bluetooth::event::VisibilityOn> / HandleSetVisibility = state<Idle>,
                        state<Idle> + sml::event<bluetooth::event::VisibilityOff> / HandleUnsetVisibility = state<Idle>,
                        state<Idle> + sml::event<bluetooth::event::ConnectAudio> / EstablishAudioConnection = state<Idle>,
                        state<Idle> + sml::event<bluetooth::event::Connect> / HandleConnect = state<Idle>,
                        state<Idle> + sml::event<bluetooth::event::Disconnect> / HandleDisconnect = state<Idle>,

                        state<Idle> + sml::event<bluetooth::event::SignalStrengthData> / SignalStrength = state<Idle>,
                        state<Idle> + sml::event<bluetooth::event::OperatorNameData>/  SetOperatorName = state<Idle>,


@@ 363,10 364,10 @@ namespace bluetooth
                        state<Idle> + sml::event<bluetooth::event::StartStream>/ StartAudio = state<Idle>,
                        state<Idle> + sml::event<bluetooth::event::StopStream>/ StopAudio = state<Idle>,

                        state<Idle> + sml::event<bluetooth::event::StartRouting> / forwardEvent= state<Call>,
                        state<Idle> + sml::event<bluetooth::event::StartRouting> / forwardEvent = state<Call>,

                        state<Idle> + sml::event<bluetooth::event::IncomingCallNumber>  / forwardEvent  = state<Call>,
                        state<Idle> + sml::event<bluetooth::event::CallStarted> / forwardEvent= state<Call>,
                        state<Idle> + sml::event<bluetooth::event::IncomingCallNumber>  / forwardEvent = state<Call>,
                        state<Idle> + sml::event<bluetooth::event::CallStarted> / forwardEvent = state<Call>,
                        state<Call> = state<Idle> // this one is needed to go out from Call substate properly!

                            );

M module-bluetooth/Bluetooth/CommandHandler.cpp => module-bluetooth/Bluetooth/CommandHandler.cpp +4 -4
@@ 66,17 66,17 @@ namespace bluetooth
        return Error::Success;
    }

    Error::Code CommandHandler::establishAudioConnection(const DataVariant &data)
    Error::Code CommandHandler::connect(const DataVariant &data)
    {
        auto device = std::get<Devicei>(data);
        LOG_INFO("Connecting audio");
        LOG_INFO("Connecting device");
        profileManager->connect(device);
        return Error::Success;
    }

    Error::Code CommandHandler::disconnectAudioConnection()
    Error::Code CommandHandler::disconnect()
    {
        LOG_INFO("Disconnecting audio");
        LOG_INFO("Disconnecting device");
        profileManager->disconnect();
        return Error::Success;
    }

M module-bluetooth/Bluetooth/CommandHandler.hpp => module-bluetooth/Bluetooth/CommandHandler.hpp +4 -4
@@ 30,8 30,8 @@ namespace bluetooth
        virtual Error::Code scan()                                            = 0;
        virtual Error::Code stopScan()                                        = 0;
        virtual Error::Code setVisibility(bool visibility)                    = 0;
        virtual Error::Code establishAudioConnection(const DataVariant &data) = 0;
        virtual Error::Code disconnectAudioConnection()                       = 0;
        virtual Error::Code connect(const DataVariant &data)                  = 0;
        virtual Error::Code disconnect()                                      = 0;
        virtual Error::Code pair(const DataVariant &data)                     = 0;
        virtual Error::Code unpair(const DataVariant &data)                   = 0;
        virtual Error::Code availableDevices()                                = 0;


@@ 48,8 48,8 @@ namespace bluetooth
        Error::Code scan() override;
        Error::Code stopScan() override;
        Error::Code setVisibility(bool visibility) override;
        Error::Code establishAudioConnection(const DataVariant &data) override;
        Error::Code disconnectAudioConnection() override;
        Error::Code connect(const DataVariant &data) override;
        Error::Code disconnect() override;
        Error::Code pair(const DataVariant &data) override;
        Error::Code unpair(const DataVariant &data) override;
        Error::Code availableDevices() override;

M module-bluetooth/Bluetooth/WorkerController.cpp => module-bluetooth/Bluetooth/WorkerController.cpp +2 -2
@@ 59,11 59,11 @@ namespace bluetooth
    {
        pimpl->sm.process_event(evt);
    };
    void StatefulController::handle(const bluetooth::event::ConnectAudio &evt)
    void StatefulController::handle(const bluetooth::event::Connect &evt)
    {
        pimpl->sm.process_event(evt);
    };
    void StatefulController::handle(const bluetooth::event::DisconnectAudio &evt)
    void StatefulController::handle(const bluetooth::event::Disconnect &evt)
    {
        pimpl->sm.process_event(evt);
    };

M module-bluetooth/Bluetooth/WorkerController.hpp => module-bluetooth/Bluetooth/WorkerController.hpp +2 -2
@@ 38,8 38,8 @@ namespace bluetooth
        void handle(const bluetooth::event::GetDevicesAvailable &evt) override;
        void handle(const bluetooth::event::VisibilityOn &evt) override;
        void handle(const bluetooth::event::VisibilityOff &evt) override;
        void handle(const bluetooth::event::ConnectAudio &evt) override;
        void handle(const bluetooth::event::DisconnectAudio &evt) override;
        void handle(const bluetooth::event::Connect &evt) override;
        void handle(const bluetooth::event::Disconnect &evt) override;
        void handle(const bluetooth::event::PowerOn &evt) override;
        void handle(const bluetooth::event::PowerOff &evt) override;
        void handle(const bluetooth::event::ShutDown &evt) override;

M module-bluetooth/Bluetooth/command/event/Events.hpp => module-bluetooth/Bluetooth/command/event/Events.hpp +3 -3
@@ 56,9 56,9 @@ namespace bluetooth::event
            controler->handle(*this);
        }
    };
    struct ConnectAudio : public Base
    struct Connect : public Base
    {
        explicit ConnectAudio(const Devicei &dev) : device(dev)
        explicit Connect(const Devicei &dev) : device(dev)
        {}
        const Devicei device;



@@ 67,7 67,7 @@ namespace bluetooth::event
            controler->handle(*this);
        }
    };
    struct DisconnectAudio : public Base
    struct Disconnect : public Base
    {
        void dispatch(bluetooth::AbstractController *controler) const override
        {

M module-bluetooth/tests/tests-StatefulController.cpp => module-bluetooth/tests/tests-StatefulController.cpp +20 -2
@@ 40,8 40,8 @@ namespace mock
        fakeit::When(Method(mock, scan)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, stopScan)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, setVisibility)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, establishAudioConnection)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, disconnectAudioConnection)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, connect)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, disconnect)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, pair)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, unpair)).AlwaysReturn(Error::Code::Success);
        fakeit::When(Method(mock, availableDevices)).AlwaysReturn(Error::Code::Success);


@@ 153,6 153,24 @@ TEST_CASE("pair/unpair")
    fakeit::Verify(Method(sm.h, unpair)).Exactly(1);
}

TEST_CASE("connect/disconnect")
{
    using namespace boost::sml;

    auto sm         = mock::SM();
    auto controller = sm.get();
    auto device     = Devicei{"lol"};

    REQUIRE(controller.sm.process_event(bluetooth::event::PowerOn{}));
    REQUIRE(controller.sm.is(state<bluetooth::On>));

    REQUIRE(controller.sm.process_event(bluetooth::event::Connect{device}));
    fakeit::Verify(Method(sm.h, connect)).Exactly(1);

    REQUIRE(controller.sm.process_event(bluetooth::event::Disconnect{}));
    fakeit::Verify(Method(sm.h, disconnect)).Exactly(1);
}

TEST_CASE("start/stop scan")
{
    using namespace boost::sml;

M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +5 -5
@@ 139,7 139,7 @@ sys::ReturnCodes ServiceBluetooth::DeinitHandler()

void ServiceBluetooth::ProcessCloseReason(sys::CloseReason closeReason)
{
    sendWorkerCommand(std::make_unique<bluetooth::event::DisconnectAudio>());
    sendWorkerCommand(std::make_unique<bluetooth::event::Disconnect>());
    sendWorkerCommand(std::make_unique<bluetooth::event::PowerOff>());
}



@@ 299,7 299,7 @@ auto ServiceBluetooth::handle(message::bluetooth::SetDeviceName *msg) -> std::sh
auto ServiceBluetooth::handle(message::bluetooth::Connect *msg) -> std::shared_ptr<sys::Message>
{
    auto device = msg->getDevice();
    sendWorkerCommand(std::make_unique<bluetooth::event::ConnectAudio>(device));
    sendWorkerCommand(std::make_unique<bluetooth::event::Connect>(device));
    bluetoothDevicesModel->setInternalDeviceState(device, DeviceState::Connecting);
    bluetoothDevicesModel->syncDevicesWithApp();
    return sys::MessageNone{};


@@ 349,7 349,7 @@ auto ServiceBluetooth::handle(message::bluetooth::ConnectResult *msg) -> std::sh

auto ServiceBluetooth::handle([[maybe_unused]] message::bluetooth::Disconnect *msg) -> std::shared_ptr<sys::Message>
{
    sendWorkerCommand(std::make_unique<bluetooth::event::DisconnectAudio>());
    sendWorkerCommand(std::make_unique<bluetooth::event::Disconnect>());
    return sys::MessageNone{};
}



@@ 430,7 430,7 @@ auto ServiceBluetooth::handle(BluetoothMessage *msg) -> std::shared_ptr<sys::Mes
        sendWorkerCommand(std::make_unique<bluetooth::event::StartStream>());
        break;
    case BluetoothMessage::Disconnect:
        sendWorkerCommand(std::make_unique<bluetooth::event::DisconnectAudio>());
        sendWorkerCommand(std::make_unique<bluetooth::event::Disconnect>());
        break;
    case BluetoothMessage::Stop:
        sendWorkerCommand(std::make_unique<bluetooth::event::StopStream>());


@@ 444,7 444,7 @@ auto ServiceBluetooth::handle(BluetoothMessage *msg) -> std::shared_ptr<sys::Mes

auto ServiceBluetooth::handle(BluetoothAddrMessage *msg) -> std::shared_ptr<sys::Message>
{
    sendWorkerCommand(std::make_unique<bluetooth::event::ConnectAudio>(msg->device));
    sendWorkerCommand(std::make_unique<bluetooth::event::Connect>(msg->device));
    return std::make_shared<sys::ResponseMessage>();
}