~aleteoryx/muditaos

ref: 55c41de597d14ba4946de0bf2580c469f4738cf8 muditaos/module-bluetooth/Bluetooth/CommandHandler.hpp -rw-r--r-- 2.7 KiB
55c41de5 — Wiktor S. Ovalle Correa [EGD-6575] Rename Unicast with timeout as UnicastSync 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

#pragma once

#include "Error.hpp"
#include "interface/profiles/ProfileManager.hpp"
#include "interface/BluetoothDriver.hpp"

#include <cstdint>

namespace sys
{
    class Service;
    class Message;
}

namespace bluetooth
{
    class SettingsHolder;
    class Command
    {
      public:
        enum Type
        {
            StartScan,
            StopScan,
            getDevicesAvailable,
            StartPan,
            VisibilityOn,
            VisibilityOff,
            ConnectAudio,
            DisconnectAudio,
            PowerOn,
            PowerOff,
            Pair,
            Unpair,
            StartStream,
            StopStream,
            SwitchProfile,
            None,
        };

        explicit Command(Command::Type type, std::optional<uint8_t *> addr = std::nullopt) : type(type)
        {
            if (addr) {
                bd_addr_copy(address, addr.value());
            }
            else {
                memset(address, 0, sizeof(bd_addr_t));
            }
        }

        auto getType() const noexcept -> Command::Type
        {
            return type;
        }

        auto getAddress() -> uint8_t *
        {
            return address;
        }

      private:
        bd_addr_t address{};
        Type type;
    };

    class AbstractCommandHandler
    {
      public:
        virtual ~AbstractCommandHandler() noexcept = default;

        virtual auto handle(Command command) -> Error::Code = 0;
    };

    class CommandHandler : public AbstractCommandHandler
    {
      public:
        explicit CommandHandler(sys::Service *service,
                                std::shared_ptr<bluetooth::SettingsHolder> settings,
                                std::shared_ptr<bluetooth::ProfileManager> profileManager,
                                std::shared_ptr<bluetooth::AbstractDriver> driver);

        auto handle(Command command) -> Error::Code override;

      private:
        Error::Code scan();
        Error::Code stopScan();
        Error::Code startPan();
        Error::Code setVisibility(bool visibility);
        Error::Code establishAudioConnection(bd_addr_t addr);
        Error::Code disconnectAudioConnection();
        Error::Code pair(bd_addr_t addr);
        Error::Code unpair(bd_addr_t addr);
        Error::Code availableDevices();
        Error::Code switchAudioProfile();
        sys::Service *service;
        std::shared_ptr<bluetooth::SettingsHolder> settings;
        std::shared_ptr<bluetooth::ProfileManager> profileManager;
        std::shared_ptr<AbstractDriver> driver;
    };
} // namespace bluetooth