~aleteoryx/muditaos

ref: 76c924fd5ee9b3d99ba61d40bd5fa932e40715c0 muditaos/module-services/service-bluetooth/service-bluetooth/BluetoothDevicesModel.cpp -rw-r--r-- 3.4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BluetoothDevicesModel.hpp"
#include <Service/Service.hpp>
#include <service-bluetooth/messages/BondedDevices.hpp>

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

void BluetoothDevicesModel::mergeDevicesList(const std::vector<Devicei> &devicesList)
{
    devices.insert(std::end(devices), std::begin(devicesList), std::end(devicesList));

    // remove duplicates
    auto end = std::end(devices);
    for (auto it = std::begin(devices); it != end; ++it) {
        end = std::remove(it + 1, end, *it);
    }
    devices.erase(end, std::end(devices));
}

void BluetoothDevicesModel::insertDevice(const Devicei &device)
{
    devices.emplace_back(device);
    syncDevicesWithApp();
}
auto BluetoothDevicesModel::getDeviceByAddress(const std::string &address)
    -> std::optional<std::reference_wrapper<Devicei>>
{

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

    if (deviceIt == std::end(devices)) {
        return std::nullopt;
    }
    return std::ref(*deviceIt);
}
auto BluetoothDevicesModel::getDeviceByAddress(const bd_addr_t address)
    -> std::optional<std::reference_wrapper<Devicei>>
{

    auto deviceIt = std::find_if(std::begin(devices), std::end(devices), [address](const Devicei &device) {
        return std::memcmp(address, device.address, sizeof(bd_addr_t)) == 0;
    });

    if (deviceIt == std::end(devices)) {
        return std::nullopt;
    }
    return std::ref(*deviceIt);
}
void BluetoothDevicesModel::removeDevice(const Devicei &device)
{
    auto position = std::find(std::begin(devices), std::end(devices), device);
    if (position != std::end(devices)) {
        devices.erase(position);
    }
    syncDevicesWithApp();
}
auto BluetoothDevicesModel::getDevices() -> std::vector<Devicei> &
{
    return devices;
}
void BluetoothDevicesModel::syncDevicesWithApp()
{
    if (service != nullptr) {
        service->bus.sendMulticast(std::make_shared<::message::bluetooth::SyncDevices>(devices),
                                   sys::BusChannel::BluetoothNotifications);
    }
}

void BluetoothDevicesModel::setInternalDeviceState(const Devicei &device, const DeviceState &state)
{
    auto dev                      = getDeviceByAddress(device.address);
    if (not dev) {
        LOG_ERROR("no such device - ignored");
        return;
    }
    dev.value().get().deviceState = state;
}

void BluetoothDevicesModel::mergeInternalDeviceState(const Devicei &device)
{
    try {
        auto deviceInModel = getDeviceByAddress(device.address).value().get();

        if ((deviceInModel.deviceState == DeviceState::ConnectedVoice &&
             device.deviceState == DeviceState::ConnectedAudio) ||
            (device.deviceState == DeviceState::ConnectedVoice &&
             deviceInModel.deviceState == DeviceState::ConnectedAudio)) {
            setInternalDeviceState(device, DeviceState::ConnectedBoth);
        }
        else {
            setInternalDeviceState(device, device.deviceState);
        }
    }
    catch (std::bad_optional_access &e) {
        LOG_ERROR("Can't get device by address! Unable to update device state. %s", e.what());
        setInternalDeviceState(device, device.deviceState);
    }
}