~aleteoryx/muditaos

ref: a405cad694b867fcd2498984830bd97d4b9bde2f muditaos/module-services/service-desktop/endpoints/bluetooth/BluetoothHelper.cpp -rw-r--r-- 5.9 KiB
a405cad6Aleteoryx trim readme 7 days 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <endpoints/bluetooth/BluetoothHelper.hpp>
#include <endpoints/bluetooth/BluetoothEventMessages.hpp>
#include <service-bluetooth/BluetoothMessage.hpp>
#include <endpoints/message/Sender.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>

#include <log/log.hpp>

namespace sdesktop::endpoints
{
    using sender::putToSendQueue;

    namespace btConsts = json::bluetooth;

    auto BluetoothHelper::processPutRequest(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));
        putToSendQueue(context.createSimpleResponse());
        return sys::ReturnCodes::Unresolved;
    }
    auto BluetoothHelper::processGetRequest(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 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()) {
            auto device = Devicei("DevMode device");
            sscanf_bd_addr(address.c_str(), device.address);
            msg = std::make_unique<::message::bluetooth::Connect>(device);
        }
        else if (auto address = body[json::bluetooth::pair].string_value(); !address.empty()) {
            auto device = Devicei("DevMode device");
            sscanf_bd_addr(address.c_str(), device.address);
            LOG_INFO("Requesting pairing form harness");
            msg = std::make_unique<BluetoothPairMessage>(device);
        }

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

    auto 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");
            auto device = Devicei("DevMode device");
            sscanf_bd_addr(address.c_str(), device.address);
            msg = std::make_unique<::message::bluetooth::Unpair>(device);
        }
        sendRequest(context, std::move(msg));
        putToSendQueue(context.createSimpleResponse());
        return sys::ReturnCodes::Unresolved;
    }
    void BluetoothHelper::sendRequest(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 ...");
            putToSendQueue(context.createSimpleResponse());
        }
    }

} // namespace sdesktop::endpoints