Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
ux_flow_engine.c
Go to the documentation of this file.
1
2/*******************************************************************************
3 * Ledger Nano S - Secure firmware
4 * (c) 2022 Ledger
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ********************************************************************************/
18
19#include "ux.h"
20#include "os_helpers.h"
21#include "os_pic.h"
22#include "ux_flow_engine.h"
23
24#include <string.h>
25
26#ifdef HAVE_UX_FLOW
27
28// check if flow is valid (has valid length and in correct stack slot)
29static unsigned int ux_flow_check_valid(void)
30{
31 unsigned int top_stack_slot = G_ux.stack_count - 1;
32
33 if (G_ux.stack_count > UX_STACK_SLOT_COUNT || G_ux.flow_stack[top_stack_slot].length == 0) {
34 return 0;
35 }
36 return 1;
37}
38
39// to hide the left tick or not
40unsigned int ux_flow_is_first(void)
41{
42 // no previous ?
43 unsigned int top_stack_slot = G_ux.stack_count - 1;
44 if (!ux_flow_check_valid() || G_ux.flow_stack[top_stack_slot].steps == NULL
45 || (G_ux.flow_stack[top_stack_slot].index == 0
46 && G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].length - 1]
47 != FLOW_LOOP)) {
48 return 1;
49 }
50
51 // previous is a flow barrier ?
52 if (G_ux.flow_stack[top_stack_slot].length > 0
53 && G_ux.flow_stack[top_stack_slot].index < G_ux.flow_stack[top_stack_slot].length
54 && G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index - 1]
55 == FLOW_BARRIER) {
56 return 1;
57 }
58
59 // not the first, for sure
60 return 0;
61}
62
63unsigned int ux_flow_is_last(void)
64{
65 // last ?
66 unsigned int top_stack_slot = G_ux.stack_count - 1;
67
68 if (!ux_flow_check_valid() || G_ux.flow_stack[top_stack_slot].steps == NULL
69 || G_ux.flow_stack[top_stack_slot].length == 0
70 || G_ux.flow_stack[top_stack_slot].index >= G_ux.flow_stack[top_stack_slot].length - 1) {
71 return 1;
72 }
73
74 // followed by a flow barrier ?
75 if (G_ux.flow_stack[top_stack_slot].length > 0
76 && G_ux.flow_stack[top_stack_slot].index < G_ux.flow_stack[top_stack_slot].length - 2
77 && G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index + 1]
78 == FLOW_BARRIER) {
79 return 1;
80 }
81
82 // is not last
83 return 0;
84}
85
87{
88 unsigned int top_stack_slot = G_ux.stack_count - 1;
89
90 if (G_ux.stack_count) {
91 if (G_ux.flow_stack[top_stack_slot].index > G_ux.flow_stack[top_stack_slot].prev_index) {
93 }
94 else if (G_ux.flow_stack[top_stack_slot].index
95 < G_ux.flow_stack[top_stack_slot].prev_index) {
97 }
98 }
100}
101
103{
104 unsigned int top_stack_slot = G_ux.stack_count - 1;
105
106 if (!ux_flow_check_valid() || G_ux.flow_stack[top_stack_slot].steps == NULL
107 || G_ux.flow_stack[top_stack_slot].index >= G_ux.flow_stack[top_stack_slot].length
108 || G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index]
109 == FLOW_BARRIER
110 || G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index] == FLOW_LOOP
111 || G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index]
112 == FLOW_END_STEP) {
113 return NULL;
114 }
115 return STEPPIC(G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index]);
116}
117
118static void ux_flow_engine_layout_step(unsigned int stack_slot)
119{
120 // invalid ux_flow_length ??? (previous check shall have exited earlier)
121 if (G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index] == FLOW_END_STEP) {
122 return;
123 }
124 // this shall not have occurred due to previous checks
125 if (G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index] == FLOW_BARRIER) {
126 return;
127 }
128 // this shall not have occurred due to previous checks
129 if (G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index] == FLOW_LOOP) {
130 return;
131 }
132 // if init function is set, call it
133 if (STEPPIC(G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index])->init) {
134 INITPIC(STEPPIC(G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index])->init)
135 (stack_slot);
136 }
137 else {
138 // if init method is not set, jump to referenced flow and step
140 stack_slot,
141 STEPSPIC(STEPPIC(G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index])
142 ->validate_flow),
143 (const ux_flow_step_t *) PIC(
144 STEPPIC(G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index])
145 ->params));
146 }
147}
148
149// (re)display a step: a button gesture started on the previous screen must not act on it
150static void ux_flow_engine_init_step(unsigned int stack_slot)
151{
153 ux_flow_engine_layout_step(stack_slot);
154}
155
156static void ux_flow_next_internal(unsigned int display_step)
157{
158 unsigned int top_stack_slot = G_ux.stack_count - 1;
159
160 // last reached already (need validation, not next)
161 if (!ux_flow_check_valid() || G_ux.flow_stack[top_stack_slot].steps == NULL
162 || G_ux.flow_stack[top_stack_slot].length <= 1
163 || G_ux.flow_stack[top_stack_slot].index >= G_ux.flow_stack[top_stack_slot].length - 1) {
164 return;
165 }
166
167 // followed by a flow barrier ? => need validation instead of next
168 if (G_ux.flow_stack[top_stack_slot].index <= G_ux.flow_stack[top_stack_slot].length - 2) {
169 if (G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index + 1]
170 == FLOW_BARRIER) {
171 return;
172 }
173
174 // followed by a flow barrier ? => need validation instead of next
175 if (G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index + 1]
176 == FLOW_LOOP) {
177 // display first step, fake direction as forward
178 G_ux.flow_stack[top_stack_slot].prev_index = G_ux.flow_stack[top_stack_slot].index = 0;
179 ux_flow_engine_init_step(top_stack_slot);
180 return;
181 }
182 }
183
184 // advance flow pointer and display it (skip META STEPS)
185 G_ux.flow_stack[top_stack_slot].prev_index = G_ux.flow_stack[top_stack_slot].index;
186 G_ux.flow_stack[top_stack_slot].index++;
187 if (display_step) {
188 ux_flow_engine_init_step(top_stack_slot);
189 }
190}
191
193{
194 ux_flow_next_internal(0);
195}
196
197void ux_flow_next(void)
198{
199 ux_flow_next_internal(1);
200}
201
202void ux_flow_prev(void)
203{
204 unsigned int top_stack_slot = G_ux.stack_count - 1;
205
206 // first reached already
207 if (!ux_flow_check_valid() || G_ux.flow_stack[top_stack_slot].steps == NULL
208 || G_ux.flow_stack[top_stack_slot].length <= 1
209 || (G_ux.flow_stack[top_stack_slot].index == 0
210 && G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].length - 1]
211 != FLOW_LOOP)) {
212 return;
213 }
214
215 // loop in flow (before checking barrier as there is no prestep when looping)
216 if (G_ux.flow_stack[top_stack_slot].index == 0
217 && G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].length - 1]
218 == FLOW_LOOP) {
219 // display last step (shall skip BARRIER if any, but a flow finishing with a BARRIER is
220 // cryptic)
221 G_ux.flow_stack[top_stack_slot].index = G_ux.flow_stack[top_stack_slot].length - 2;
222 // fact direction as backward
223 G_ux.flow_stack[top_stack_slot].prev_index = G_ux.flow_stack[top_stack_slot].index + 1;
224 ux_flow_engine_init_step(top_stack_slot);
225 return;
226 }
227
228 // previous item is a flow barrier ?
229 if (G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index - 1]
230 == FLOW_BARRIER) {
231 return;
232 }
233
234 // advance flow pointer and display it (skip META STEPS)
235 G_ux.flow_stack[top_stack_slot].prev_index = G_ux.flow_stack[top_stack_slot].index;
236 G_ux.flow_stack[top_stack_slot].index--;
237
238 ux_flow_engine_init_step(top_stack_slot);
239}
240
241void ux_flow_validate(void)
242{
243 unsigned int top_stack_slot = G_ux.stack_count - 1;
244
245 // no flow ?
246 if (!ux_flow_check_valid() || G_ux.flow_stack[top_stack_slot].steps == NULL
247 || G_ux.flow_stack[top_stack_slot].length == 0
248 || G_ux.flow_stack[top_stack_slot].index >= G_ux.flow_stack[top_stack_slot].length) {
249 return;
250 }
251
252 // no validation flow ?
253 if (STEPPIC(G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index])
254 ->validate_flow
255 != NULL) {
256 // execute validation flow
257 ux_flow_init(top_stack_slot,
258 STEPSPIC(STEPPIC(G_ux.flow_stack[top_stack_slot]
259 .steps[G_ux.flow_stack[top_stack_slot].index])
260 ->validate_flow),
261 NULL);
262 }
263 else {
264 // if next is a barrier, then proceed to the item after the barrier
265 // if NOT followed by a barrier, then validation is only performed through
266 // a validate_flow specified in the step, else ignored
267 if (G_ux.flow_stack[top_stack_slot].length > 0
268 && G_ux.flow_stack[top_stack_slot].index
269 <= G_ux.flow_stack[top_stack_slot].length - 2) {
270 if (G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index + 1]
271 == FLOW_BARRIER) {
272 // take into account multi barrier at once, kthx poor code review
273 while (G_ux.flow_stack[top_stack_slot].length > 0
274 && G_ux.flow_stack[top_stack_slot].index
275 <= G_ux.flow_stack[top_stack_slot].length - 2
276 && G_ux.flow_stack[top_stack_slot]
277 .steps[G_ux.flow_stack[top_stack_slot].index + 1]
278 == FLOW_BARRIER) {
279 G_ux.flow_stack[top_stack_slot].index++;
280 }
281 // skip to next step
282 G_ux.flow_stack[top_stack_slot].prev_index = G_ux.flow_stack[top_stack_slot].index;
283 G_ux.flow_stack[top_stack_slot].index++;
284
285 // execute reached step
286 ux_flow_engine_init_step(top_stack_slot);
287 }
288 // reached the last step, but step if FLOW_LOOP
289 else if (G_ux.flow_stack[top_stack_slot]
290 .steps[G_ux.flow_stack[top_stack_slot].index + 1]
291 == FLOW_LOOP) {
292 // we go the forward direction
293 G_ux.flow_stack[top_stack_slot].prev_index = G_ux.flow_stack[top_stack_slot].index
294 = 0;
295 // execute reached step
296 ux_flow_engine_init_step(top_stack_slot);
297 }
298 }
299 }
300}
301
302void ux_flow_error(unsigned int error)
303{
304 UNUSED(error);
305
306 unsigned int top_stack_slot = G_ux.stack_count - 1;
307
308 if (G_ux.flow_stack[top_stack_slot].steps == NULL || G_ux.flow_stack[top_stack_slot].length == 0
309 || G_ux.flow_stack[top_stack_slot].index >= G_ux.flow_stack[top_stack_slot].length) {
310 return;
311 }
312
313 if (STEPPIC(G_ux.flow_stack[top_stack_slot].steps[G_ux.flow_stack[top_stack_slot].index])
314 ->error_flow
315 != NULL) {
316 ux_flow_init(top_stack_slot,
317 STEPSPIC(STEPPIC(G_ux.flow_stack[top_stack_slot]
318 .steps[G_ux.flow_stack[top_stack_slot].index])
319 ->error_flow),
320 NULL);
321 }
322}
323
327void ux_flow_init(unsigned int stack_slot,
328 const ux_flow_step_t *const *steps,
329 const ux_flow_step_t *const start_step)
330{
331 if (stack_slot >= UX_STACK_SLOT_COUNT) {
332 return;
333 }
334
335 G_ux.flow_stack[stack_slot].length = G_ux.flow_stack[stack_slot].prev_index
336 = G_ux.flow_stack[stack_slot].index = 0;
337 G_ux.flow_stack[stack_slot].steps = NULL;
338
339 // reset paging to avoid troubles if first step is a paginated step
340 memset(&G_ux.layout_paging, 0, sizeof(G_ux.layout_paging));
341
342 if (steps) {
343 G_ux.flow_stack[stack_slot].steps = STEPSPIC(steps);
344 while (G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].length]
345 != FLOW_END_STEP) {
346 G_ux.flow_stack[stack_slot].length++;
347 }
348 if (start_step != NULL) {
349 const ux_flow_step_t *const start_step2 = STEPPIC(start_step);
350 while (G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index]
352 && STEPPIC(G_ux.flow_stack[stack_slot].steps[G_ux.flow_stack[stack_slot].index])
353 != start_step2) {
354 G_ux.flow_stack[stack_slot].prev_index = G_ux.flow_stack[stack_slot].index;
355 G_ux.flow_stack[stack_slot].index++;
356 }
357 }
358
359 // init step
360 ux_flow_engine_init_step(stack_slot);
361 }
362}
363
364void ux_flow_uninit(unsigned int stack_slot)
365{
366 if (stack_slot < UX_STACK_SLOT_COUNT) {
367 memset(&G_ux.flow_stack[stack_slot], 0, sizeof(G_ux.flow_stack[stack_slot]));
368 }
369}
370
371unsigned int ux_flow_button_callback(unsigned int button_mask, unsigned int button_mask_counter)
372{
373 UNUSED(button_mask_counter);
374 switch (button_mask) {
376 ux_flow_prev();
377 break;
379 ux_flow_next();
380 break;
383 break;
384 }
385 return 0;
386}
387
388const void *ux_stack_get_step_params(unsigned int stack_slot)
389{
390 if (stack_slot >= UX_STACK_SLOT_COUNT) {
391 return NULL;
392 }
393
394 if (G_ux.flow_stack[stack_slot].length == 0) {
395 return NULL;
396 }
397
398 if (G_ux.flow_stack[stack_slot].index >= G_ux.flow_stack[stack_slot].length) {
399 return NULL;
400 }
401
402 return PIC(
403 STEPPIC(STEPSPIC(G_ux.flow_stack[stack_slot].steps)[G_ux.flow_stack[stack_slot].index])
404 ->params);
405}
406
407const void *ux_stack_get_current_step_params(void)
408{
409 unsigned int top_stack_slot = G_ux.stack_count - 1;
410
411 return ux_stack_get_step_params(top_stack_slot);
412}
413
414unsigned int ux_flow_relayout(void)
415{
416 // if a flow is defined and valid
417 if (ux_flow_get_current() != NULL) {
418 unsigned int top_stack_slot = G_ux.stack_count - 1;
419
420 // the step layout is reinitialized and may display another content than before (step
421 // advanced by ux_flow_next_no_display(), paging reset to its first or last page, step
422 // re-exposed after a modal): an ongoing gesture must not act on it
423 ux_flow_engine_init_step(top_stack_slot);
424 return 1;
425 }
426 return 0;
427}
428
429#endif // HAVE_UX_FLOW
static void ux_discard_button_gesture(void)
Definition ux_bagl.h:564
#define UX_STACK_SLOT_COUNT
Definition ux_bagl.h:151
#define BUTTON_RIGHT
Definition ux_bagl.h:130
#define BUTTON_EVT_RELEASED
Definition ux_bagl.h:133
#define G_ux
Definition ux_bagl.h:323
#define BUTTON_LEFT
Definition ux_bagl.h:129
void ux_flow_init(unsigned int stack_slot, const ux_flow_step_t *const *steps, const ux_flow_step_t *const start_step)
ux_flow_direction_t
@ FLOW_DIRECTION_BACKWARD
@ FLOW_DIRECTION_FORWARD
@ FLOW_DIRECTION_START
#define FLOW_LOOP
ux_flow_direction_t ux_flow_direction(void)
void ux_flow_next(void)
const ux_flow_step_t * ux_flow_get_current(void)
void ux_flow_prev(void)
unsigned int ux_flow_is_first(void)
void ux_flow_error(unsigned int error)
#define FLOW_BARRIER
void ux_flow_next_no_display(void)
#define STEPPIC(x)
void ux_flow_uninit(unsigned int stack_slot)
#define INITPIC(x)
unsigned int ux_flow_button_callback(unsigned int button_mask, unsigned int button_mask_counter)
struct ux_flow_step_s ux_flow_step_t
#define STEPSPIC(x)
const void * ux_stack_get_step_params(unsigned int stack_slot)
unsigned int ux_flow_is_last(void)
void ux_flow_validate(void)
const void * ux_stack_get_current_step_params(void)
#define FLOW_END_STEP
unsigned int ux_flow_relayout(void)