~aleteoryx/muditaos

ref: 3cc3f50f7b9364ffac38349238cd6d9aa243dc23 muditaos/module-services/service-cellular/call/CallMachine.hpp -rw-r--r-- 13.6 KiB
3cc3f50f — Maciej Gibowicz [BH-1809][BH-1835] Add date format setting 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <boost/sml.hpp>
#include <sml-utils/Logger.hpp>
#include "call/CellularCall.hpp"
#include "log/log.hpp"
#include "magic_enum.hpp"
#include <cxxabi.h>

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

    const auto setIncomingCall = [](Dependencies &di, CallData &call, const utils::PhoneNumber::View &view) {
        call.record.type   = CallType::CT_INCOMING;
        call.record.isRead = false;
        setNumber(call, view);
    };

    const auto handleClipCommon = [](Dependencies &di, const call::event::CLIP &clip, CallData &call) {
        const utils::PhoneNumber::View &nr = clip.number;
        setIncomingCall(di, call, nr);
        call.record.date = std::time(nullptr);

        di.gui->notifyCLIP(nr);
        di.multicast->notifyIdentifiedCall(nr);
        di.db->startCall(call.record);
        di.sentinel->HoldMinimumFrequency(bsp::CpuFrequencyMHz::Level_6);
    };

    struct RING
    {
        bool operator()(CallData &call)
        {
            LOG_DEBUG("Called RING, status: %s",
                      call.mode == sys::phone_modes::PhoneMode::Connected ? "true" : "false");
            return call.mode == sys::phone_modes::PhoneMode::Connected;
        }
    } constexpr RING;

    struct Tethering
    {
        bool operator()(CallData &call)
        {
            return call.tethering == sys::phone_modes::Tethering::On;
        }
    } Tethering;

    struct HandleInit
    {
        void operator()(Dependencies &di, CallData &call)
        {
            call           = CallData{};
            call.mode      = di.modem->getMode();
            call.tethering = di.modem->getTethering();
            di.sentinel->ReleaseMinimumFrequency();
            di.timer->stop();
        }
    } constexpr HandleInit;

    struct HandleRing
    {
        void operator()(Dependencies &di, CallData &call)
        {
            auto nr = utils::PhoneNumber::View();
            setIncomingCall(di, call, nr);
            call.record.date = std::time(nullptr);

            di.sentinel->HoldMinimumFrequency(bsp::CpuFrequencyMHz::Level_6);
            di.ringTimer->start();
        }
    } constexpr HandleRing;

    struct HandleRingTimeout
    {
        void operator()(Dependencies &di, CallData &call)
        {
            di.multicast->notifyIncomingCall();
            di.db->startCall(call.record);
            di.gui->notifyRING();
            di.audio->play();
        }
    } constexpr HandleRingTimeout;

    struct ClipConnected
    {
        bool operator()(CallData &call)
        {
            return call.mode == sys::phone_modes::PhoneMode::Connected;
        }
    } constexpr ClipConnected;

    struct HandleClipWithoutRing
    {
        void operator()(Dependencies &di, const call::event::CLIP &clip, CallData &call)
        {
            handleClipCommon(di, clip, call);
            di.audio->play();
        };
    } constexpr HandleClipWithoutRing;

    struct HandleClipConnected
    {
        /// does everything that clip would do + plays ring
        void operator()(Dependencies &di, const call::event::CLIP &clip, CallData &call)
        {
            handleClipCommon(di, clip, call);
        };
    } constexpr HandleClipConnected;

    struct ClipDND_OK
    {
        bool operator()(const call::event::CLIP &clip, Dependencies &di, CallData &call)
        {
            LOG_DEBUG("Called ClipDND_OK!");
            return call.mode == sys::phone_modes::PhoneMode::DoNotDisturb &&
                   di.modem->areCallsFromFavouritesEnabled() && di.db->isNumberInFavourites(clip.number);
        }
    } constexpr ClipDND_OK;

    struct HandleClipDND
    {
        void operator()(const call::event::CLIP &clip, Dependencies &di, CallData &call)
        {
            LOG_DEBUG("Called HandleClipDND!");
            handleClipCommon(di, clip, call);
            di.audio->play();
        }
    } constexpr HandleClipDND;

    struct ClipDND_NOK
    {
        bool operator()(const call::event::CLIP &clip, Dependencies &di, CallData &call)
        {
            LOG_DEBUG("Called ClipDND_NOK!");
            return call.mode == sys::phone_modes::PhoneMode::DoNotDisturb &&
                   not(di.modem->areCallsFromFavouritesEnabled() && di.db->isNumberInFavourites(clip.number));
        }
    } constexpr ClipDND_NOK;

    struct HandleDND_Reject
    {
        void operator()(Dependencies &di, const call::event::CLIP &clip, CallData &call)
        {
            di.db->startCall(call.record);

            LOG_DEBUG("Called HandleDND_Reject!");
            setNumber(call, clip.number);
            call.record.date = std::time(nullptr);
            call.record.type = CallType::CT_MISSED;

#ifdef TARGET_RT1051
            /* Workaround for a DND mode failed rejections.
             * Probably modem needs some time to process
             * the request internally, so there is a delay
             * required before issuing call rejecting.
             * The 750ms value has been obtained experimentally. */
            constexpr auto modemProcessingTimeDelayMs = 750;
            vTaskDelay(pdMS_TO_TICKS(modemProcessingTimeDelayMs));
#endif
            di.modem->rejectCall();
            di.db->endCall(call.record);
        }
    } constexpr HandleDND_Reject;

    /// when we have ongoing call - handle incoming CLIP information
    /// this happens when we answer call before first CLIP is received
    struct HandleAddID
    {
        void operator()(Dependencies &di, const call::event::CLIP &clip, CallData &call)
        {
            const utils::PhoneNumber::View &number = clip.number;
            setNumber(call, number);
            di.gui->notifyCLIP(number);
            di.multicast->notifyIdentifiedCall(number);
        }
    } constexpr HandleAddID;

    struct HandleAnswerCall
    {
        void operator()(Dependencies &di, CallData &call)
        {
            if (not di.modem->answerIncomingCall()) {
                throw std::runtime_error("modem failure!");
            }
            di.audio->routingStart();
            di.multicast->notifyCallActive();
            di.timer->start();
        }
    } constexpr HandleAnswerCall;

    struct CallIncoming
    {
        bool operator()(CallData &call)
        {
            return call.record.type == CallType::CT_INCOMING;
        }
    } constexpr CallIncoming;

    struct HandleCallTimer
    {
        void operator()(Dependencies &di, CallData &call)
        {
            di.multicast->notifyCallDurationUpdate(di.timer->duration());
        }
    } constexpr HandleCallTimer;

    struct HandleStartCall
    {
        void operator()(Dependencies &di, const call::event::StartCall &start, CallData &call)
        {
            call.record.type        = start.type;
            call.record.isRead      = true;
            call.record.phoneNumber = start.number;
            call.record.date        = std::time(nullptr);

            di.audio->routingStart();
            di.db->startCall(call.record);
            di.multicast->notifyCallStarted(call.record.phoneNumber, call.record.type);
            di.sentinel->HoldMinimumFrequency(bsp::CpuFrequencyMHz::Level_6);
        }
    } constexpr HandleStartCall;

    struct HandleStartedCall
    {
        void operator()(Dependencies &di, CallData &call)
        {
            di.multicast->notifyOutgoingCallAnswered();
            di.timer->start();
        }
    } constexpr HandleStartedCall;

    struct HandleRejectCall
    {
        void operator()(Dependencies &di, CallData &call)
        {
            di.audio->stop();
            call.record.type   = CallType::CT_REJECTED;
            call.record.isRead = false;
            di.db->endCall(call.record);
            di.multicast->notifyCallEnded();
            di.modem->rejectCall();
        }
    } constexpr HandleRejectCall;

    struct HandleMissedCall
    {
        void operator()(Dependencies &di, CallData &call)
        {
            di.audio->stop();
            call.record.type   = CallType::CT_MISSED;
            call.record.isRead = false;
            di.db->endCall(call.record);
            di.multicast->notifyCallMissed();
            di.modem->rejectCall();
        };
    } constexpr HandleMissedCall;

    struct HandleEndCall
    {
        void operator()(Dependencies &di, CallData &call)
        {
            di.audio->stop();
            call.record.duration = di.timer->duration();
            call.record.isRead   = true;
            di.db->endCall(call.record);
            di.multicast->notifyCallEnded();
            di.timer->stop();
            di.modem->hangupCall();
        }
    } constexpr HandleEndCall;

    struct HandleAudioRequest
    {
        void operator()(Dependencies &di, const call::event::AudioRequest &request)
        {
            const auto event = request.event;
            switch (event) {
            case cellular::CallAudioEventRequest::EventType::Mute:
                di.audio->muteCall();
                break;
            case cellular::CallAudioEventRequest::EventType::Unmute:
                di.audio->unmuteCall();
                break;
            case cellular::CallAudioEventRequest::EventType::LoudspeakerOn:
                di.audio->setLoudspeakerOn();
                break;
            case cellular::CallAudioEventRequest::EventType::LoudspeakerOff:
                di.audio->setLoudspeakerOff();
                break;
            }
        }
    } constexpr HandleAudioRequest;

    // show exception in log and notify the world about ended call
    struct ExceptionHandler
    {
        void operator()(const std::runtime_error &err, Dependencies &di)
        {
            di.multicast->notifyCallAborted();
            di.multicast->notifyCallEnded();
            LOG_FATAL("EXCEPTION %s", err.what());
        }
    } constexpr ExceptionHandler;

    struct SM
    {
        auto operator()() const
        {
            namespace evt = call::event;
            using namespace boost::sml;

            return make_transition_table(
                *"Idle"_s + on_entry<_> / HandleInit,

                "Idle"_s + boost::sml::event<evt::RING>[RING and not Tethering] / HandleRing = "RingDelay"_s,

                "Idle"_s + boost::sml::event<evt::CLIP>[Tethering or ClipDND_NOK] / HandleDND_Reject = "Idle"_s,

                "Idle"_s + boost::sml::event<evt::CLIP>[ClipConnected] / HandleClipWithoutRing = "HaveId"_s,
                "Idle"_s + boost::sml::event<evt::CLIP>[ClipDND_OK] / HandleClipDND            = "HaveId"_s,
                // outgoing call: Pure is Ringing (called from: handleCellularRingingMessage)
                "Idle"_s + boost::sml::event<evt::StartCall> / HandleStartCall = "Starting"_s,

                "Starting"_s + boost::sml::event<evt::AudioRequest> / HandleAudioRequest = "Starting"_s,
                "Starting"_s + boost::sml::event<evt::Ended> / HandleEndCall             = "Idle"_s,
                "Starting"_s + boost::sml::event<evt::Reject> / HandleEndCall            = "Idle"_s,
                "Starting"_s + boost::sml::event<evt::Answer> / HandleStartedCall        = "Ongoing"_s,

                "RingDelay"_s + boost::sml::event<evt::RingTimeout> / HandleRingTimeout = "Ringing"_s,
                // here we do not need guard - RingDelay state is already guarded on entry
                "RingDelay"_s + boost::sml::event<evt::CLIP> / HandleClipWithoutRing = "HaveId"_s,

                "Ringing"_s + boost::sml::event<evt::Answer> / HandleAnswerCall                 = "Ongoing"_s,
                "Ringing"_s + boost::sml::event<evt::Reject> / HandleRejectCall                 = "Idle"_s,
                "Ringing"_s + boost::sml::event<evt::Ended> / HandleRejectCall                  = "Idle"_s,
                "Ringing"_s + boost::sml::event<evt::CLIP>[ClipConnected] / HandleClipConnected = "HaveId"_s,
                "Ringing"_s + boost::sml::event<evt::CLIP>[ClipDND_OK] / HandleClipDND          = "HaveId"_s,
                "Ringing"_s + boost::sml::event<evt::CLIP>[ClipDND_NOK] / HandleDND_Reject      = "Idle"_s,

                "HaveId"_s + boost::sml::event<evt::Answer>[CallIncoming] / HandleAnswerCall = "Ongoing"_s,
                "HaveId"_s + boost::sml::event<evt::Ended> / HandleMissedCall                = "Idle"_s,
                "HaveId"_s + boost::sml::event<evt::Reject> / HandleRejectCall               = "Idle"_s,

                "Ongoing"_s + boost::sml::event<evt::CLIP> / HandleAddID                = "Ongoing"_s,
                "Ongoing"_s + boost::sml::event<evt::OngoingTimer> / HandleCallTimer    = "Ongoing"_s,
                "Ongoing"_s + boost::sml::event<evt::AudioRequest> / HandleAudioRequest = "Ongoing"_s,
                // end call and reject when call is ongoing is same
                "Ongoing"_s + boost::sml::event<evt::Ended> / HandleEndCall  = "Idle"_s,
                "Ongoing"_s + boost::sml::event<evt::Reject> / HandleEndCall = "Idle"_s,

                *("ExceptionsHandling"_s) + exception<std::runtime_error> / ExceptionHandler = "Idle"_s,
                "ExceptionsHandling"_s + exception<std::runtime_error> / ExceptionHandler    = "Idle"_s);
        }
    };

    struct StateMachine
    {
        CallData call{};
        Dependencies di{};
        Logger logger;

        explicit StateMachine(Dependencies di) : di(std::move(di))
        {}

        bool active() const
        {
            using namespace boost::sml;
            return not machine.is("Idle"_s);
        }

        boost::sml::sm<SM, boost::sml::logger<Logger>> machine{logger, di, call};
    };

} // namespace call