~aleteoryx/muditaos

ref: 4c661d4f2c736d3cbc13614864511873efc48678 muditaos/module-apps/application-call/model/CallModel.cpp -rw-r--r-- 5.3 KiB
4c661d4f — Lukasz Mastalerz [BH-1412] Improve system shutdown procedure 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
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CallModel.hpp"

#include <service-cellular/service-cellular/CellularServiceAPI.hpp>
#include <service-evtmgr/service-evtmgr/EVMessages.hpp>
#include <service-evtmgr/Constants.hpp>

namespace app::call
{
    time_t CallModel::getTime()
    {
        return callTime;
    }

    void CallModel::setTime(time_t newTime)
    {
        callTime = newTime;

        if (notifyPresenterOnDurationChange != nullptr) {
            notifyPresenterOnDurationChange();
        }
    }

    CallState CallModel::getState()
    {
        return callState;
    }

    void CallModel::setState(CallState newState)
    {
        if (callState == newState) {
            LOG_INFO("Dropping call state change");
        }

        if (((callState == CallState::Incoming) || (callState == CallState::Disconnecting)) &&
            (newState == CallState::Ended) && callWasRejected) {
            callWasRejected = false;
            callState       = CallState::Rejected;
        }
        else {
            callState = newState;
        }

        if (callState == CallState::Active) {
            turnOnKeypadBacklight();
        }
        else {
            turnOffKeypadBacklight();
        }

        if (notifyPresenterOnCallStateChange != nullptr) {
            notifyPresenterOnCallStateChange();
        }
    }

    void CallModel::hangUpCall()
    {
        CellularServiceAPI::HangupCall(application);
        if (callState == CallState::Incoming) {
            callWasRejected = true;
        }
    }

    void CallModel::answerCall()
    {
        CellularServiceAPI::AnswerIncomingCall(application);
    }

    bool CallModel::sendSms(const UTF8 &smsBody)
    {
        if (phoneNumber.getView().getEntered().empty() || smsBody.length() == 0) {
            LOG_WARN("Number or sms body is empty");
            return false;
        }
        SMSRecord record;
        record.number = phoneNumber.getView();
        record.body   = smsBody;
        record.type   = SMSType::QUEUED;
        record.date   = std::time(nullptr);

        using db::query::SMSAdd;
        const auto [succeed, _] =
            DBServiceAPI::GetQuery(application, db::Interface::Name::SMS, std::make_unique<SMSAdd>(record));
        return succeed;
    }

    void CallModel::transmitDtmfTone(const uint8_t &digitCode)
    {
        try {
            CellularServiceAPI::TransmitDtmfTones(application, DTMFCode(static_cast<char>(digitCode)));
        }
        catch (std::out_of_range &e) {
            LOG_ERROR("Can't send DTMF code for digit: %c", static_cast<char>(digitCode));
        }
    }

    utils::PhoneNumber CallModel::getPhoneNumber()
    {
        return phoneNumber;
    }

    void CallModel::muteCall()
    {
        CellularServiceAPI::CallAudioMuteEvent(application);
    }

    void CallModel::unmuteCall()
    {
        CellularServiceAPI::CallAudioUnmuteEvent(application);
    }

    void CallModel::turnLoudspeakerOn()
    {
        CellularServiceAPI::CallAudioLoudspeakerOnEvent(application);
    }

    void CallModel::turnLoudspeakerOff()
    {
        CellularServiceAPI::CallAudioLoudspeakerOffEvent(application);
    }

    void CallModel::turnOnKeypadBacklight()
    {
        application->bus.sendUnicast(
            std::make_shared<sevm::KeypadBacklightMessage>(bsp::keypad_backlight::Action::turnOnCallMode),
            service::name::evt_manager);
    }

    void CallModel::turnOffKeypadBacklight()
    {
        application->bus.sendUnicast(
            std::make_shared<sevm::KeypadBacklightMessage>(bsp::keypad_backlight::Action::turnOffCallMode),
            service::name::evt_manager);
    }

    void CallModel::clear()
    {
        callState = CallState::None;
        callTime  = 0;
        callerId.clear();
        callWasRejected = false;
        phoneNumber     = utils::PhoneNumber();

        notifyPresenterOnDurationChange  = nullptr;
        notifyPresenterOnCallStateChange = nullptr;
    }
    void CallModel::attachDurationChangeCallback(std::function<void()> callback)
    {
        notifyPresenterOnDurationChange = callback;
    }

    void CallModel::attachCallStateChangeCallback(std::function<void()> callback)
    {
        notifyPresenterOnCallStateChange = callback;
    }

    void CallModel::setPhoneNumber(const utils::PhoneNumber number)
    {
        if (phoneNumber.getFormatted() == number.getFormatted()) {
            LOG_INFO("Dropping number duplication.");
            return;
        }

        phoneNumber = number;

        if (notifyPresenterOnCallStateChange != nullptr) {
            notifyPresenterOnCallStateChange();
        }
    }

    UTF8 CallModel::getCallerId()
    {
        if (phoneNumber.getFormatted().empty()) {
            LOG_INFO("No number provided");
            callerId = UTF8();
            return callerId;
        }

        auto contact = DBServiceAPI::MatchContactByPhoneNumber(application, phoneNumber.getView());
        if (contact && !contact->isTemporary()) {
            LOG_INFO("number recognized as contact id = %" PRIu32, contact->ID);
            callerId = contact->getFormattedName();
        }
        else {
            LOG_INFO("number was not recognized as any valid contact");
            callerId = UTF8(phoneNumber.getFormatted());
        }

        return callerId;
    }
} // namespace app::call