~aleteoryx/muditaos

ref: 76c924fd5ee9b3d99ba61d40bd5fa932e40715c0 muditaos/module-apps/application-settings/models/bluetooth/BluetoothSettingsModel.cpp -rw-r--r-- 5.2 KiB
76c924fd — Adam Dobrowolski [EGD-8002] Added SSP, fixed some minor issues, removed obsolete code 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BluetoothSettingsModel.hpp"

#include <service-bluetooth/Constants.hpp>
#include <service-bluetooth/messages/BondedDevices.hpp>
#include <service-bluetooth/messages/Connect.hpp>
#include <service-bluetooth/messages/DeviceName.hpp>
#include <service-bluetooth/messages/Disconnect.hpp>
#include <service-bluetooth/messages/SetStatus.hpp>
#include <service-bluetooth/messages/SetDeviceName.hpp>
#include <service-bluetooth/messages/Passkey.hpp>
#include <service-bluetooth/messages/Unpair.hpp>
#include <service-bluetooth/messages/SyncDevices.hpp>

BluetoothSettingsModel::BluetoothSettingsModel(sys::Service *service) : service{service}
{}

void BluetoothSettingsModel::requestStatus()
{
    service->bus.sendUnicast(std::make_shared<::message::bluetooth::RequestStatus>(), service::name::bluetooth);
}

void BluetoothSettingsModel::setStatus(const bool desiredBluetoothState, const bool desiredVisibility)
{
    status.state      = desiredBluetoothState ? BluetoothStatus::State::On : BluetoothStatus::State::Off;
    status.visibility = desiredVisibility;
    message::bluetooth::SetStatus setStatus(status);
    service->bus.sendUnicast(std::make_shared<::message::bluetooth::SetStatus>(std::move(setStatus)),
                             service::name::bluetooth);
}

void BluetoothSettingsModel::requestDeviceName()
{
    service->bus.sendUnicast(std::make_shared<::message::bluetooth::RequestDeviceName>(), service::name::bluetooth);
}

void BluetoothSettingsModel::setDeviceName(const UTF8 &deviceName)
{
    service->bus.sendUnicast(std::make_shared<message::bluetooth::SetDeviceName>(deviceName), service::name::bluetooth);
}

void BluetoothSettingsModel::requestBondedDevices()
{
    service->bus.sendUnicast(std::make_shared<::message::bluetooth::RequestBondedDevices>(), service::name::bluetooth);
}

void BluetoothSettingsModel::requestScan()
{
    service->bus.sendUnicast(std::make_shared<BluetoothMessage>(BluetoothMessage::Request::Scan),
                             service::name::bluetooth);
}

void BluetoothSettingsModel::stopScan()
{
    service->bus.sendUnicast(std::make_shared<BluetoothMessage>(BluetoothMessage::Request::StopScan),
                             service::name::bluetooth);
}

void BluetoothSettingsModel::requestDevicePair(const Devicei &device)
{
    service->bus.sendUnicast(std::make_shared<BluetoothPairMessage>(device), service::name::bluetooth);
}

void BluetoothSettingsModel::requestDeviceUnpair(const Devicei &device)
{
    service->bus.sendUnicast(std::make_shared<message::bluetooth::Unpair>(device), service::name::bluetooth);
}

void BluetoothSettingsModel::responsePasskey(const std::string &passkey)
{
    service->bus.sendUnicast(std::make_shared<message::bluetooth::ResponsePasskey>(passkey, pinRequestor),
                             service::name::bluetooth);
}

void BluetoothSettingsModel::requestConnection(const Devicei &device)
{
    service->bus.sendUnicast(std::make_shared<message::bluetooth::Connect>(device), service::name::bluetooth);
}

void BluetoothSettingsModel::requestDisconnection()
{
    service->bus.sendUnicast(std::make_shared<message::bluetooth::Disconnect>(), service::name::bluetooth);
}
void BluetoothSettingsModel::replaceDevicesList(const std::vector<Devicei> &devicesList)
{
    devices = devicesList;
}
void BluetoothSettingsModel::setActiveDeviceState(const DeviceState &state)
{
    auto activeDevice = getActiveDevice();
    if (activeDevice.has_value()) {
        activeDevice.value().get().deviceState = state;
    }
}
auto BluetoothSettingsModel::getActiveDevice() -> std::optional<std::reference_wrapper<Devicei>>
{
    try {
        return devices.at(activeDeviceIndex);
    }
    catch (const std::out_of_range &oor) {
        LOG_WARN("NO DEVICE FOUND!");
        return std::nullopt;
    }
}
auto BluetoothSettingsModel::getSelectedDevice() -> std::optional<std::reference_wrapper<Devicei>>
{
    try {
        return devices.at(selectedDeviceIndex);
    }
    catch (const std::out_of_range &oor) {
        LOG_WARN("NO DEVICE FOUND!");
        return std::nullopt;
    }
}
void BluetoothSettingsModel::setActiveDevice(const Devicei &device)
{
    auto itr          = std::find(std::begin(devices), std::end(devices), device);
    activeDeviceIndex = std::distance(std::begin(devices), itr);
}
void BluetoothSettingsModel::setSelectedDevice(const Devicei &device)
{
    auto itr            = std::find(std::begin(devices), std::end(devices), device);
    selectedDeviceIndex = std::distance(std::begin(devices), itr);
}

auto BluetoothSettingsModel::getDevices() -> std::vector<Devicei> &
{
    return devices;
}
auto BluetoothSettingsModel::isDeviceConnecting() -> bool
{

    auto deviceIt = std::find_if(std::begin(devices), std::end(devices), [](const Devicei &device) {
        return device.deviceState == DeviceState::Connecting;
    });

    if (deviceIt != std::end(devices)) {
        return true;
    }
    return false;
}
auto BluetoothSettingsModel::getStatus() const -> const BluetoothStatus
{
    return status;
}
auto BluetoothSettingsModel::isDeviceListEmpty() const -> bool
{
    return devices.empty();
}