// 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 "BluetoothSettingsModel.hpp" #include #include #include #include #include #include #include #include BluetoothSettingsModel::BluetoothSettingsModel(sys::Service *service) : service{service} {} void BluetoothSettingsModel::requestStatus() { service->bus.sendUnicast(std::make_shared<::message::bluetooth::RequestStatus>(), service::name::bluetooth); } void BluetoothSettingsModel::updateStatus(const bool desiredBluetoothState, const bool desiredVisibility) { status.state = desiredBluetoothState ? BluetoothStatus::State::On : BluetoothStatus::State::Off; status.visibility = desiredVisibility; } void BluetoothSettingsModel::setStatus(const bool desiredBluetoothState, const bool desiredVisibility) { updateStatus(desiredBluetoothState, 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(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::Request::Scan), service::name::bluetooth); } void BluetoothSettingsModel::stopScan() { service->bus.sendUnicast(std::make_shared(BluetoothMessage::Request::StopScan), service::name::bluetooth); } void BluetoothSettingsModel::requestDevicePair(const Devicei &device) { service->bus.sendUnicast(std::make_shared(device), service::name::bluetooth); } void BluetoothSettingsModel::requestDeviceUnpair(const Devicei &device) { service->bus.sendUnicast(std::make_shared(device), service::name::bluetooth); } void BluetoothSettingsModel::requestConnection(const Devicei &device) { service->bus.sendUnicast(std::make_shared(device), service::name::bluetooth); } void BluetoothSettingsModel::requestDisconnection() { service->bus.sendUnicast(std::make_shared(), service::name::bluetooth); } void BluetoothSettingsModel::replaceDevicesList(const std::vector &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> { try { return devices.at(activeDeviceIndex); } catch (const std::out_of_range &oor) { LOG_WARN("Device not found!"); return std::nullopt; } } auto BluetoothSettingsModel::getSelectedDevice() -> std::optional> { try { return devices.at(selectedDeviceIndex); } catch (const std::out_of_range &oor) { LOG_WARN("Device not 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 & { 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(); }