~aleteoryx/muditaos

ref: 1da915ace94b631914c768efcb878e0a6393195e muditaos/module-services/service-desktop/endpoints/bluetooth/BluetoothHelper.cpp -rw-r--r-- 5.2 KiB
1da915ac — Wojtek Rzepecki [BH-777] Add Bell specific ld script configuration 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BluetoothHelper.hpp"
#include "BluetoothEventMessages.hpp"
#include <service-bluetooth/BluetoothMessage.hpp>
#include <parser/MessageHandler.hpp>
#include <service-bluetooth/messages/SetStatus.hpp>
#include <service-desktop/DeveloperModeMessage.hpp>

#include <service-bluetooth/service-bluetooth/messages/Connect.hpp>
#include <service-bluetooth/service-bluetooth/messages/Disconnect.hpp>
#include <service-bluetooth/service-bluetooth/messages/Status.hpp>
#include <service-bluetooth/service-bluetooth/messages/BondedDevices.hpp>
#include <service-bluetooth/service-bluetooth/messages/Unpair.hpp>

namespace btConsts = parserFSM::json::bluetooth;

auto parserFSM::BluetoothHelper::processPutRequest(parserFSM::Context &context) -> sys::ReturnCodes
{
    auto body = context.getBody();
    std::unique_ptr<sys::Message> msg;
    if (auto command = body[btConsts::command].string_value(); !command.empty()) {
        if (command == btConsts::commands::scanOn) {
            msg = std::make_unique<BluetoothMessage>(BluetoothMessage::Request::Scan);
        }
        else if (command == btConsts::commands::scanOff) {
            msg = std::make_unique<BluetoothMessage>(BluetoothMessage::Request::StopScan);
        }
        else if (command == btConsts::commands::changeVisibility) {
            msg = std::make_unique<BluetoothMessage>(BluetoothMessage::Request::Visible);
        }
    }
    else if (auto state = body[btConsts::state].object_items(); !state.empty()) {
        BluetoothStatus status{};
        const auto &power = state[btConsts::states::power];
        if (power == json::bluetooth::on) {
            status.state = BluetoothStatus::State::On;
            LOG_INFO("turning on BT from harness!");
        }
        else if (power == json::bluetooth::off) {
            status.state = BluetoothStatus::State::Off;
            LOG_INFO("turning off BT from harness!");
        }
        const auto &visibility = state[btConsts::states::visibility];
        status.visibility      = visibility == json::bluetooth::on;

        ::message::bluetooth::SetStatus setStatus(status);
        msg = std::make_unique<::message::bluetooth::SetStatus>(std::move(setStatus));
    }

    sendRequest(context, std::move(msg));
    MessageHandler::putToSendQueue(context.createSimpleResponse());
    return sys::ReturnCodes::Unresolved;
}
auto parserFSM::BluetoothHelper::processGetRequest(parserFSM::Context &context) -> sys::ReturnCodes
{
    auto body = context.getBody();
    std::unique_ptr<sys::Message> msg;
    if (body[json::bluetooth::state].bool_value()) {
        msg = std::make_unique<::message::bluetooth::RequestStatus>();
    }
    else if (auto devices = body[json::bluetooth::devices].string_value(); !devices.empty()) {
        if (devices == json::bluetooth::devicesValues::scanned) {
            auto event = std::make_unique<sdesktop::bluetooth::GetAvailableDevicesEvent>();
            msg        = std::make_unique<sdesktop::developerMode::DeveloperModeRequest>(std::move(event));
        }
        else if (devices == json::bluetooth::devicesValues::bonded) {
            msg = std::make_unique<::message::bluetooth::RequestBondedDevices>();
        }
    }

    sendRequest(context, std::move(msg));
    return sys::ReturnCodes::Unresolved;
}

auto parserFSM::BluetoothHelper::processPostRequest(Context &context) -> sys::ReturnCodes
{
    auto body = context.getBody();
    std::unique_ptr<sys::Message> msg;
    if (auto address = body[json::bluetooth::connect].string_value(); !address.empty()) {
        msg = std::make_unique<::message::bluetooth::Connect>(std::move(address));
    }
    else if (auto address = body[json::bluetooth::pair].string_value(); !address.empty()) {
        LOG_INFO("Requesting pairing form harness");
        msg = std::make_unique<BluetoothPairMessage>(std::move(address));
    }

    sendRequest(context, std::move(msg));
    MessageHandler::putToSendQueue(context.createSimpleResponse());
    return sys::ReturnCodes::Unresolved;
}

auto parserFSM::BluetoothHelper::processDeleteRequest(Context &context) -> sys::ReturnCodes
{
    auto body = context.getBody();
    std::unique_ptr<sys::Message> msg;
    if (auto command = body[json::bluetooth::command].string_value(); !command.empty()) {
        if (command == json::bluetooth::commands::disconnect) {
            msg = std::make_unique<::message::bluetooth::Disconnect>();
        }
    }
    else if (auto address = body[json::bluetooth::unpair].string_value(); !address.empty()) {
        LOG_INFO("Requesting pairing form harness");
        msg = std::make_unique<::message::bluetooth::Unpair>(std::move(address));
    }
    sendRequest(context, std::move(msg));
    MessageHandler::putToSendQueue(context.createSimpleResponse());
    return sys::ReturnCodes::Unresolved;
}
void parserFSM::BluetoothHelper::sendRequest(parserFSM::Context &context, std::unique_ptr<sys::Message> msg)
{
    if (msg != nullptr) {
        ownerServicePtr->bus.sendUnicast(std::move(msg), "ServiceBluetooth");
    }
    else {
        LOG_ERROR("No valid request created, returning simpleResponse ...");
        MessageHandler::putToSendQueue(context.createSimpleResponse());
    }
}