~aleteoryx/muditaos

ref: cc32dd30dc716cd3f6a70142ea8be795398abdcf muditaos/module-services/service-cellular/call/CellularCall.cpp -rw-r--r-- 6.6 KiB
cc32dd30 — Bartosz Cichocki [MOS-366] Connect cellular events to HFP 3 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "call/CellularCall.hpp"
#include "service-cellular/call/CallRingGuard.hpp"
#include "service-cellular/ServiceCellular.hpp"
#include "service-db/agents/settings/SystemSettings.hpp"

#include <queries/notifications/QueryNotificationsIncrement.hpp>
#include <CalllogRecord.hpp>
#include <PhoneNumber.hpp>
#include <Utils.hpp>
#include <log/log.hpp>

#include <cinttypes>
#include <ctime>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

namespace CellularCall
{
    Call::Call(ServiceCellular &owner) : owner(owner), audio(owner), multicast(owner), gui(owner), db(owner)
    {
        utils::PhoneNumber::View number = utils::PhoneNumber::View();
        const CallType type             = CallType::CT_NONE;
        const time_t date               = 0;
        const time_t duration           = 0;

        clear();

        this->call.phoneNumber = number;
        this->call.date        = date;
        this->call.duration    = duration;
        this->call.type        = type;
        this->call.name        = number.getEntered(); // temporary set number as name
    }

    bool Call::handleRING()
    {
        if (callRingGuard(*this)) {
            startCall(utils::PhoneNumber::View(), CallType::CT_INCOMING);
            audio.play();

            if (!wasRinging) {
                wasRinging = true;
                gui.notifyRING();
                multicast.notifyIncommingCall();
            }
            return true;
        }
        return false;
    }

    bool Call::handleCLIP(const utils::PhoneNumber::View &number)
    {
        setNumber(number);
        if (callClipGuard(*this)) {
            startCall(number, CallType::CT_INCOMING);
            audio.play();
        }

        if (callClipGuard(*this) || callRingGuard(*this)) {
            if (!isNumberDisplayed) {
                isNumberDisplayed = true;
                gui.notifyCLIP(number);
                multicast.notifyIdentifiedCall(number);
            }
            return true;
        }

        if (callDNDGuard(*this)) {
            db.incrementNotAnsweredCallsNotification(number);
        }

        return false;
    }

    bool Call::startOutgoing(const utils::PhoneNumber::View &number)
    {
        return startCall(number, CallType::CT_OUTGOING);
    }

    bool Call::startCall(const utils::PhoneNumber::View &number, const CallType type)
    {
        LOG_INFO("starting call");

        if (isValid()) {
            LOG_ERROR("call already established");
            return false;
        }

        if (cpuSentinel) {
            cpuSentinel->HoldMinimumFrequency(bsp::CpuFrequencyMHz::Level_6);
        }

        clear();
        CalllogRecord callRec{type, number};
        call = startCallAction ? startCallAction(callRec) : CalllogRecord();
        if (!call.isValid()) {
            LOG_ERROR("failed to obtain a call log record");
            clear();
            if (cpuSentinel) {
                cpuSentinel->ReleaseMinimumFrequency();
            }
            LOG_INFO("failed to start call");
            return false;
        }

        gui.notifyCallStarted(number, type);
        LOG_INFO("call started");
        return true;
    }

    bool Call::setActive()
    {
        if (isValid()) {
            startActiveTime = utils::time::getCurrentTimestamp();
            isActiveCall    = true;
            gui.notifyCallActive();
            LOG_INFO("set call active");
            return true;
        }
        LOG_ERROR("no valid call to activate");
        return false;
    }

    bool Call::endCall(Forced forced)
    {
        audio.stop();
        LOG_INFO("ending call");
        if (!isValid()) {
            LOG_ERROR("no call to end");
            return false;
        }

        if (cpuSentinel) {
            cpuSentinel->ReleaseMinimumFrequency();
        }

        if (isActiveCall) {
            auto endTime  = utils::time::getCurrentTimestamp();
            call.duration = (endTime - startActiveTime).get();
        }
        else {
            auto callType = call.type;
            switch (callType) {
            case CallType::CT_INCOMING: {
                if (forced == Forced::True) {
                    setType(CallType::CT_REJECTED);
                }
                else {
                    setType(CallType::CT_MISSED);
                    markUnread();
                }
            } break;

            case CallType::CT_OUTGOING: {
                // do nothing
            } break;

            default:
                LOG_ERROR("Not a valid call type %u", static_cast<int>(callType));
                return false;
            }
        }

        if (!(endCallAction && endCallAction(call))) {
            LOG_ERROR("CalllogUpdate failed, id %" PRIu32, call.ID);
            return false;
        }

        // Calllog entry was updated, ongoingCall can be cleared
        clear();
        gui.notifyCallEnded();
        LOG_INFO("call ended");
        return true;
    }

    void Call::setNumber(const utils::PhoneNumber::View &number)
    {
        call.presentation = number.getFormatted().empty() ? PresentationType::PR_UNKNOWN : PresentationType::PR_ALLOWED;
        call.phoneNumber  = number;
    }

    void Call::setCpuSentinel(std::shared_ptr<sys::CpuSentinel> sentinel)
    {
        cpuSentinel = std::move(sentinel);
    }

    void Call::handleCallAudioEventRequest(cellular::CallAudioEventRequest::EventType event)
    {
        switch (event) {
        case cellular::CallAudioEventRequest::EventType::Mute:
            audio.muteCall();
            break;
        case cellular::CallAudioEventRequest::EventType::Unmute:
            audio.unmuteCall();
            break;
        case cellular::CallAudioEventRequest::EventType::LoudspeakerOn:
            audio.setLaudspeakerOn();
            break;
        case cellular::CallAudioEventRequest::EventType::LoudspeakerOff:
            audio.setLaudspeakerOff();
            break;
        }
    }
    void Call::handleCallDurationTimer()
    {
        if (not isActiveCall) {
            return;
        }
        auto now         = utils::time::getCurrentTimestamp();
        callDurationTime = (now - startActiveTime).getSeconds();
        gui.notifyCallDurationUpdate(callDurationTime);
    }

    bool Call::Operations::areCallsFromFavouritesEnabled()
    {
        return call.owner.areCallsFromFavouritesEnabled();
    }

    bool Call::Operations::isNumberInFavourites()
    {
        return DBServiceAPI::IsContactInFavourites(&call.owner, call.call.phoneNumber);
    }

} // namespace CellularCall