~aleteoryx/muditaos

ref: 8021ebb5c1bf03ef2b6b483c48e44d03bc2d2f14 muditaos/module-bluetooth/Bluetooth/interface/profiles/ProfileManager.cpp -rw-r--r-- 5.8 KiB
8021ebb5 — Lefucjusz [MOS-1006] Fix modifying data in .text section when setting SN 2 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <service-bluetooth/ServiceBluetooth.hpp>
#include "ProfileManager.hpp"
#include <GAP/GAP.hpp>

namespace bluetooth
{

    ProfileManager::ProfileManager(sys::Service *ownerService) : ownerService(ownerService)
    {}

    auto ProfileManager::init() -> Error::Code
    {
        if (!initialized) {
            profilesList = {{AudioProfile::A2DP, std::make_shared<bluetooth::A2DP>()},
                            {AudioProfile::HSP, nullptr},
                            {AudioProfile::HFP, std::make_shared<bluetooth::HFP>()},
                            {AudioProfile::None, nullptr}};

            for (auto &[profileName, ptr] : profilesList) {
                if (ptr != nullptr) {
                    ptr->setOwnerService(ownerService);
                    ptr->init();
                }
            }

            musicProfilePtr = dynamic_cast<MusicProfile *>(profilesList[AudioProfile::A2DP].get());
            callProfilePtr  = dynamic_cast<CallProfile *>(profilesList[AudioProfile::HFP].get());

            if (auto serviceBt = dynamic_cast<ServiceBluetooth *>(ownerService); serviceBt != nullptr) {
                serviceBt->profileManagerPtr = this;
            }
            initialized = true;
        }
        return Error::Success;
    }

    auto ProfileManager::connect(const Devicei &device) -> Error::Code
    {
        for (auto &[profileName, ptr] : profilesList) {
            if (ptr != nullptr) {
                ptr->setDevice(device);
                ptr->connect();
            }
        }
        return Error::Success;
    }

    auto ProfileManager::disconnect() -> Error::Code
    {
        for (auto &[profileName, ptr] : profilesList) {
            if (ptr != nullptr) {
                ptr->disconnect();
            }
        }
        return Error::Success;
    }

    auto ProfileManager::start() -> Error::Code
    {
        musicProfilePtr->start();
        return Error::Success;
    }

    auto ProfileManager::stop() -> Error::Code
    {
        musicProfilePtr->stop();
        return Error::Success;
    }

    auto ProfileManager::startRinging() -> Error::Code
    {
        return callProfilePtr->startRinging();
    }

    auto ProfileManager::stopRinging() -> Error::Code
    {
        return callProfilePtr->stopRinging();
    }

    auto ProfileManager::initializeCall() -> Error::Code
    {
        return callProfilePtr->initializeCall();
    }
    auto ProfileManager::terminateCall() -> Error::Code
    {
        return callProfilePtr->terminateCall();
    }

    auto ProfileManager::setAudioDevice(std::shared_ptr<BluetoothAudioDevice> device) -> Error::Code
    {
        auto profileType = device->getProfileType();

        if (profilesList[profileType] == nullptr) {
            return Error::NotReady;
        }

        profilesList[profileType]->setAudioDevice(device);
        LOG_ERROR("AudioDevice for profile: %s set!", magic_enum::enum_name(profileType).data());
        return Error::Success;
    }
    auto ProfileManager::callAnswered() -> Error::Code
    {
        return callProfilePtr->callActive();
    }
    auto ProfileManager::setIncomingCallNumber(const utils::PhoneNumber &nr) -> Error::Code
    {
        if (callProfilePtr) {
            return callProfilePtr->setIncomingCallNumber(nr.getView().getE164());
        }
        LOG_ERROR("No profile, returning!");
        return Error::NotReady;
    }
    auto ProfileManager::setSignalStrengthData(const DataVariant &data) -> Error::Code
    {
        auto signalData = std::get<Store::SignalStrength>(data);
        if (callProfilePtr) {
            return callProfilePtr->setSignalStrength(static_cast<int>(signalData.rssiBar));
        }
        LOG_ERROR("No profile, returning!");
        return Error::NotReady;
    }
    auto ProfileManager::setOperatorNameData(const DataVariant &data) -> Error::Code
    {
        auto operatorName = std::get<OperatorName>(data);
        if (callProfilePtr) {
            return callProfilePtr->setOperatorName(operatorName.getName());
        }
        LOG_ERROR("No profile, returning!");
        return Error::NotReady;
    }
    auto ProfileManager::setBatteryLevelData(unsigned int level) -> Error::Code
    {
        auto batteryLevel = BatteryLevel(level);
        if (callProfilePtr) {
            return callProfilePtr->setBatteryLevel(batteryLevel);
        }
        LOG_ERROR("No profile, returning!");
        return Error::NotReady;
    }
    auto ProfileManager::setNetworkStatusData(const DataVariant &data) -> Error::Code
    {
        auto status = std::get<Store::Network::Status>(data);
        if (callProfilePtr) {
            switch (status) {
            case Store::Network::Status::RegisteredRoaming:
                callProfilePtr->setRoamingStatus(true);
                [[fallthrough]];
            case Store::Network::Status::RegisteredHomeNetwork:
                return callProfilePtr->setNetworkRegistrationStatus(true);
            default:
                return callProfilePtr->setNetworkRegistrationStatus(false);
            }
        }
        LOG_ERROR("No profile, returning!");
        return Error::NotReady;
    }
    auto ProfileManager::callStarted(const utils::PhoneNumber &nr) -> Error::Code
    {
        if (callProfilePtr) {
            return callProfilePtr->callStarted(nr.getView().getE164());
        }
        LOG_ERROR("No profile, returning!");
        return Error::NotReady;
    }
    void ProfileManager::deInit()
    {
        for (auto &[profileName, ptr] : profilesList) {
            if (ptr != nullptr) {
                ptr.reset();
            }
        }
        callProfilePtr  = nullptr;
        musicProfilePtr = nullptr;
        initialized     = false;

        LOG_DEBUG("ProfileManager deinit done");
    }

} // namespace bluetooth