~aleteoryx/muditaos

ref: 21a7855d8c229f7893bc60a9b2fab1ae0615cbf6 muditaos/module-apps/application-settings/windows/CellularPassthroughWindow.cpp -rw-r--r-- 10.3 KiB
21a7855d — Kuba [EGD-6973] Fix Auto date is on by default 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
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CellularPassthroughWindow.hpp"
#include <i18n/i18n.hpp>
#include <module-bsp/bsp/cellular/bsp_cellular.hpp>
#include "Label.hpp"
#include <cassert>

using namespace bsp::cellular::USB;

namespace gui
{

    CellularPassthroughWindow::CellularPassthroughWindow(app::Application *app)
        : AppWindow(app, window::cellular_passthrough::window_name)
    {
        buildInterface();
    }

    void CellularPassthroughWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
    }

    void CellularPassthroughWindow::buildInterface()
    {
        AppWindow::buildInterface();
        bottomBar->setActive(BottomBar::Side::CENTER, true);
        bottomBar->setActive(BottomBar::Side::RIGHT, true);
        bottomBar->setText(BottomBar::Side::CENTER, utils::translate(style::strings::common::select));
        bottomBar->setText(BottomBar::Side::RIGHT, utils::translate(style::strings::common::back));

        setTitle(utils::translate("app_settings_cellular_passthrough"));

        layout = new VBox(
            this, 0, style::window::default_vertical_pos, style::window_width, 8 * style::window::label::big_h);
        layout->setPenFocusWidth(style::window::default_border_no_focus_w);

        statusHbox = new gui::HBox(layout,
                                   style::window::default_left_margin,
                                   0,
                                   style::window_width -
                                       (style::window::default_left_margin + style::window::default_right_margin),
                                   style::window::label::big_h);
        statusHbox->setPenWidth(0);
        statusHbox->activeItem = false;

        currentStateConst = new Label(nullptr, 0, 0, 0, style::window::label::big_h, "State:");
        style::window::decorate(currentStateConst);
        currentStateConst->setFont(style::window::font::big);
        currentStateConst->setSize(currentStateConst->getTextNeedSpace(), currentStateConst->getHeight());

        currentStateVariable = new Label(nullptr, 0, 0, 0, style::window::label::big_h);
        style::window::decorate(currentStateVariable);
        currentStateVariable->setFont(style::window::font::bigbold);
        currentStateVariable->setAlignment(gui::Alignment::Horizontal::Right);
        currentStateVariable->setSize(statusHbox->getWidth() - currentStateConst->getTextNeedSpace(),
                                      currentStateVariable->getHeight());

        statusHbox->addWidget(currentStateConst);
        statusHbox->addWidget(currentStateVariable);

        addSeparator(layout);

        // init label buttons
        // enable buttons
        passNormal = new Label(nullptr, 0, 0, layout->getWidth(), style::window::label::big_h);
        style::window::decorateOption(passNormal);
        passNormal->setPadding(Padding(style::window::default_left_margin, 0, style::window::default_right_margin, 0));
        passNormal->setText(window::cellular_passthrough::passNormalDescription);
        passNormal->activatedCallback = [=](gui::Item &) {
            if (set(PassthroughState::ENABLED, BootPinState::NORMAL_BOOT)) {
                setWindowState(State::PASS_NORMAL);
            }
            else {
                setWindowState(State::FAIL);
            }
            return true;
        };
        layout->addWidget(passNormal);

        passDFU = new Label(nullptr, 0, 0, layout->getWidth(), style::window::label::big_h);
        style::window::decorateOption(passDFU);
        passDFU->setPadding(Padding(style::window::default_left_margin, 0, style::window::default_right_margin, 0));
        passDFU->setText(window::cellular_passthrough::passDFUDescription);
        passDFU->activatedCallback = [=](gui::Item &) {
            if (set(PassthroughState::ENABLED, BootPinState::FIRMWARE_UPGRADE)) {
                setWindowState(State::PASS_DFU);
            }
            else {
                setWindowState(State::FAIL);
            }
            return true;
        };
        layout->addWidget(passDFU);

        // disable buttons
        noPass = new Label(nullptr, 0, 0, layout->getWidth(), style::window::label::big_h);
        style::window::decorateOption(noPass);
        noPass->setPadding(Padding(style::window::default_left_margin, 0, style::window::default_right_margin, 0));
        noPass->setText(window::cellular_passthrough::noPassDescription);
        noPass->activatedCallback = [=](gui::Item &) {
            if (set(PassthroughState::DISABLED, BootPinState::NORMAL_BOOT)) {
                setWindowState(State::NO_PASS);
            }
            else {
                setWindowState(State::FAIL);
            }
            return true;
        };
        layout->addWidget(noPass);

        layout->resizeItems();

        setFocusItem(layout);

        // init note
        auto noteSeparator = new Rect(layout, 0, 0, layout->getWidth(), 50);
        noteSeparator->setPenWidth(style::window::default_border_no_focus_w);
        noteSeparator->activeItem = false;

        note = new Label(layout,
                         2 * style::window::default_left_margin,
                         0,
                         layout->getWidth() -
                             2 * (style::window::default_left_margin + style::window::default_right_margin),
                         style::window::label::default_h);
        note->setFont(style::window::font::small);
        note->setFilled(true);
        note->setTextColor(gui::ColorFullWhite);
        note->setFillColor(gui::ColorFullBlack);
        note->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        note->activeItem = false;

        // get initial state
        setWindowState(getInitialState());
    }

    void CellularPassthroughWindow::destroyInterface()
    {
        erase();
        invalidate();
    }

    void CellularPassthroughWindow::invalidate() noexcept
    {
        layout               = nullptr;
        passNormal           = nullptr;
        passDFU              = nullptr;
        noPass               = nullptr;
        statusHbox           = nullptr;
        currentStateConst    = nullptr;
        currentStateVariable = nullptr;
        note                 = nullptr;
    }

    void CellularPassthroughWindow::addSeparator(VBox *layout)
    {
        assert(layout != nullptr);
        uint16_t height = style::window::label::small_h * 2;
        auto separatorLayout =
            new VBox(nullptr,
                     style::window::default_left_margin,
                     0,
                     (style::window_width - (style::window::default_left_margin + style::window::default_right_margin)),
                     height);
        separatorLayout->setPenWidth(0);

        auto separatorUpper = new Rect(nullptr, 0, 0, separatorLayout->getWidth(), height / 2);
        auto separatorLower = new Rect(nullptr, 0, 0, separatorLayout->getWidth(), height / 2);
        separatorUpper->setBorderColor(gui::ColorFullBlack);
        separatorLower->setBorderColor(gui::ColorFullBlack);
        separatorUpper->setPenWidth(1);
        separatorLower->setPenWidth(1);
        separatorUpper->setEdges(RectangleEdge::Bottom);
        separatorLower->setEdges(RectangleEdge::Top);

        separatorLayout->addWidget(separatorUpper);
        separatorLayout->addWidget(separatorLower);
        separatorLayout->activeItem = false;
        layout->addWidget(separatorLayout);
    }

    CellularPassthroughWindow::State CellularPassthroughWindow::getInitialState()
    {
        auto pass    = getPassthrough();
        auto bootPin = getBootPin();

        auto ret = State::UNKNOWN;

        if (pass == PassthroughState::ENABLED) {
            if (bootPin == BootPinState::FIRMWARE_UPGRADE) {
                ret = State::PASS_DFU;
            }
            else {
                ret = State::PASS_NORMAL;
            }
        }
        else if (pass == PassthroughState::DISABLED) {
            ret = State::NO_PASS;
        }
        return ret;
    }

    bool CellularPassthroughWindow::set(PassthroughState pass_to_set, BootPinState dfu_to_set)
    {
        setPassthrough(pass_to_set);
        setBootPin(dfu_to_set);
        // in order to really enter DFU mode, modem needs be restarted

        // verify if new state has been applied with "Software Input On Field" loopback function
        vTaskDelay(pdMS_TO_TICKS(wait_for_pin_change_state));
        return (getPassthrough() == pass_to_set && getBootPin() == dfu_to_set);
    }

    void CellularPassthroughWindow::setWindowState(CellularPassthroughWindow::State state)
    {
        note->setVisible(false);

        if (state == State::PASS_NORMAL) {
            currentStateVariable->setText(window::cellular_passthrough::passNormalLabel);
            passNormal->setVisible(false);
            passDFU->setVisible(false);
            noPass->setVisible(true);
            if (previousState == State::PASS_DFU) {
                note->setText(window::cellular_passthrough::noPassNote);
                note->setVisible(true);
            }
            // no additional action is needed
        }
        else if (state == State::PASS_DFU) {
            currentStateVariable->setText(window::cellular_passthrough::passDFULabel);
            passNormal->setVisible(false);
            passDFU->setVisible(false);
            noPass->setVisible(true);
            // here modem needs be reset. PWK_KEY reset (aka. nice reset) should be enough
            note->setText(window::cellular_passthrough::passDFUNote);
            note->setVisible(true);
        }
        else if (state == State::NO_PASS) {
            currentStateVariable->setText(window::cellular_passthrough::noPassLabel);
            passNormal->setVisible(true);
            passDFU->setVisible(true);
            noPass->setVisible(false);
            if (previousState == State::PASS_DFU) {
                // reset with a reset key or even Vcc reset i.e. force reset
                note->setText(window::cellular_passthrough::noPassNote);
                note->setVisible(true);
            }
        }
        else if (state == State::FAIL) {
            currentStateVariable->setText("FAIL");
        }
        statusHbox->resizeItems();
        layout->setVisible(true);

        previousState = state;
    }

} // namespace gui