Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
ux_stack.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_pin.h"
21#include "os_seed.h"
22#include "os_screen.h"
23#include "os_types.h"
24#include "os_utils.h"
25#include "os_io_seph_ux.h"
26#include <string.h>
27
28// return true (stack slot +1) if an element
29unsigned int ux_stack_is_element_array_present(const bagl_element_t *element_array)
30{
31 unsigned int i, j;
32
33 // looks for a element array though the ux stack
34 for (i = 0; i < ARRAYLEN(G_ux.stack) && i < G_ux.stack_count; i++) {
35 for (j = 0; j < UX_STACK_SLOT_ARRAY_COUNT && j < G_ux.stack[i].element_arrays_count; j++) {
36 if (G_ux.stack[i].element_arrays[j].element_array == element_array) {
37 return i + 1;
38 }
39 }
40 }
41
42 return 0;
43}
44
45unsigned int ux_stack_push(void)
46{
47 // only push if an available slot exists
48 if (G_ux.stack_count < ARRAYLEN(G_ux.stack)) {
49 memset(&G_ux.stack[G_ux.stack_count], 0, sizeof(G_ux.stack[0]));
50#ifdef HAVE_UX_FLOW
51 memset(&G_ux.flow_stack[G_ux.stack_count], 0, sizeof(G_ux.flow_stack[0]));
52#endif // HAVE_UX_FLOW
53 G_ux.stack_count++;
54 }
55 // return the stack top index
56 return G_ux.stack_count - 1;
57}
58
59unsigned int ux_stack_pop(void)
60{
61 unsigned int exit_code = BOLOS_UX_OK;
62 // only pop if more than two stack entry (0 and 1,top is an index not a count)
63 if (G_ux.stack_count > 0) {
64 G_ux.stack_count--;
65 if (G_ux.stack_count < UX_STACK_SLOT_COUNT) {
66 exit_code = G_ux.stack[G_ux.stack_count].exit_code_after_elements_displayed;
67 // wipe popped slot
68 memset(&G_ux.stack[G_ux.stack_count], 0, sizeof(G_ux.stack[0]));
69#ifdef HAVE_UX_FLOW
70 memset(&G_ux.flow_stack[G_ux.stack_count], 0, sizeof(G_ux.flow_stack[0]));
71#endif // HAVE_UX_FLOW
72 }
73 }
74
75 // prepare output code when popping the last stack screen
76 if (G_ux.stack_count == 0) {
77 G_ux.exit_code = exit_code;
78 }
79 // ask for a complete redraw (optimisation due to blink must be avoided as we're returning from
80 // a modal, and within the bolos ux screen stack)
81 else {
82 // prepare to redraw the slot when asked
83 G_ux.stack[G_ux.stack_count - 1].element_index = 0;
84 }
85 // return the stack top index (or -1 if no top)
86 return G_ux.stack_count - 1;
87}
88
90{
91 // the screen is redrawn from scratch (e.g. after an OS modal or a stack pop), it may differ
92 // from the one on which an ongoing gesture started
94
95#ifdef HAVE_UX_FLOW
96 // an ux step has been relayout on the screen, don't flicker by redisplaying here
97 if (ux_flow_relayout()) {
98 return;
99 }
100#endif // HAVE_UX_FLOW
101
102 // check if any screen is schedule for displaying
103 if (G_ux.stack_count > 0 && G_ux.stack_count <= ARRAYLEN(G_ux.stack)) {
104 G_ux.stack[G_ux.stack_count - 1].element_index = 0;
105 ux_stack_display(G_ux.stack_count - 1);
106 }
107 // else return redraw for the app
108 else if (G_ux.stack_count == 0) {
109 if (G_ux.exit_code == BOLOS_UX_OK) {
110 G_ux.exit_code = BOLOS_UX_REDRAW;
111 }
112 }
113}
114
115void ux_stack_insert(unsigned int stack_slot)
116{
117 if (stack_slot >= ARRAYLEN(G_ux.stack)) {
118 return; // arbitrary, this is feil that's it
119 }
120 // not a full stack
121 if (G_ux.stack_count < ARRAYLEN(G_ux.stack)) {
122 // if not inserting as top of stack, then perform move
123 if (stack_slot != ARRAYLEN(G_ux.stack) - 1) {
124 memmove(&G_ux.stack[stack_slot + 1],
125 &G_ux.stack[stack_slot],
126 (ARRAYLEN(G_ux.stack) - (stack_slot + 1)) * sizeof(G_ux.stack[0]));
127#ifdef HAVE_UX_FLOW
128 memmove(&G_ux.flow_stack[stack_slot + 1],
129 &G_ux.flow_stack[stack_slot],
130 (ARRAYLEN(G_ux.flow_stack) - (stack_slot + 1)) * sizeof(G_ux.flow_stack[0]));
131#endif // HAVE_UX_FLOW
132 }
133 memset(&G_ux.stack[stack_slot], 0, sizeof(ux_stack_slot_t));
134#ifdef HAVE_UX_FLOW
135 memset(&G_ux.flow_stack[stack_slot], 0, sizeof(ux_flow_state_t));
136#endif // HAVE_UX_FLOW
137 // add the slot
138 G_ux.stack_count++;
139 // slot not wiped
140 }
141 // feil, that's it
142}
143
144void ux_stack_remove(unsigned int stack_slot)
145{
146 if (stack_slot > ARRAYLEN(G_ux.stack) - 1) {
147 stack_slot = ARRAYLEN(G_ux.stack) - 1;
148 }
149
150 // removing something not in stack
151 if (stack_slot >= G_ux.stack_count) {
152 return;
153 }
154
155 // before: | screenz | removed screen | other screenz |
156 // after: | screenz | other screenz |
157
158 if (stack_slot != ARRAYLEN(G_ux.stack) - 1) {
159 memmove(&G_ux.stack[stack_slot],
160 &G_ux.stack[stack_slot + 1],
161 (ARRAYLEN(G_ux.stack) - (stack_slot + 1)) * sizeof(G_ux.stack[0]));
162#ifdef HAVE_UX_FLOW
163 // also move the flow attached to the popped screen
164 memmove(&G_ux.flow_stack[stack_slot],
165 &G_ux.flow_stack[stack_slot + 1],
166 (ARRAYLEN(G_ux.flow_stack) - (stack_slot + 1)) * sizeof(G_ux.flow_stack[0]));
167#endif // HAVE_UX_FLOW
168 }
169
170 // wipe last slot
171 ux_stack_pop();
172}
173
174// common code for all screens
175void ux_stack_init(unsigned int stack_slot)
176{
177 /*
178 // mismatch, the stack slot is not accessible !!
179 if (G_ux.stack_count<stack_slot+1) {
180 reset();
181 }
182 */
183
184 if (stack_slot < UX_STACK_SLOT_COUNT) {
185#ifdef HAVE_UX_STACK_INIT_KEEP_TICKER
186 callback_int_t ticker_callback = G_ux.stack[stack_slot].ticker_callback;
187 unsigned int ticker_value = G_ux.stack[stack_slot].ticker_value;
188 unsigned int ticker_interval = G_ux.stack[stack_slot].ticker_interval;
189#endif // HAVE_UX_STACK_INIT_KEEP_TICKER
190
191 // wipe the slot to be displayed just in case
192 memset(&G_ux.stack[stack_slot], 0, sizeof(G_ux.stack[0]));
193
194#ifdef HAVE_UX_STACK_INIT_KEEP_TICKER
195 G_ux.stack[stack_slot].ticker_callback = ticker_callback;
196 G_ux.stack[stack_slot].ticker_value = ticker_value;
197 G_ux.stack[stack_slot].ticker_interval = ticker_interval;
198#endif // HAVE_UX_STACK_INIT_KEEP_TICKER
199
200 // init current screen state
201 G_ux.stack[stack_slot].exit_code_after_elements_displayed = BOLOS_UX_CONTINUE;
202 }
203}
204
205// check to process keyboard callback before screen generic callback
207{
208 const bagl_element_t *el;
209 if (G_ux.stack_count) {
210 if (G_ux.stack[G_ux.stack_count - 1].screen_before_element_display_callback) {
211 el = G_ux.stack[G_ux.stack_count - 1].screen_before_element_display_callback(element);
212 if (!el) {
213 return 0;
214 }
215 // legacy forvery old ux on blue and nano s
216 if ((unsigned int) el != 1) {
217 element = el;
218 }
219 }
220 }
221 // consider good to be displayed by default
222 return element;
223}
224
226{
227 unsigned int elem_idx;
228 unsigned int total_element_count;
229 const bagl_element_t *element;
230 unsigned int i;
231
232 total_element_count = 0;
233 // can't display UX of the app, when PIN is not validated
234#ifndef HAVE_BOLOS
235 if ((os_perso_isonboarded() != BOLOS_UX_OK || os_global_pin_is_validated() == BOLOS_UX_OK))
236#endif // HAVE_BOLOS
237 {
238 for (i = 0; i < UX_STACK_SLOT_ARRAY_COUNT && i < slot->element_arrays_count; i++) {
239 // compute elem_idx in the current array (element_index refers to the total number of
240 // element to display)
241 elem_idx = slot->element_index - total_element_count;
242 total_element_count += slot->element_arrays[i].element_array_count;
243 // check if we're sending from this array or not
244 while (elem_idx < slot->element_arrays[i].element_array_count) {
245 const bagl_element_t *el;
246 // pre inc before callback to allow callback to change the next element to be drawn
247 slot->element_index++;
248
249 element = &slot->element_arrays[i].element_array[elem_idx];
251 if (!el) {
252 // skip display if requested to
253 if (G_ux.exit_code != BOLOS_UX_CONTINUE) {
254 return;
255 }
256 }
257 else {
258 // legacy support
259 if ((unsigned int) el != 1) {
260 element = el;
261 }
262 io_seph_ux_display_bagl_element(element);
263 }
264 elem_idx++;
265 }
266 }
267
268 // TODO draw the status bar
269
270 if (slot->element_index == total_element_count) {
271 // if screen has special stuff todo on exit
272 screen_update();
273 // screen marked as displayed
274 slot->element_index++;
275
276 // check if a displayed callback is requiring redraw
277 if (slot->displayed_callback) {
278 // if screen displayed callback requested one more round, then set CONTINUE exit
279 // code
280 if (!slot->displayed_callback(0)) {
281 slot->element_index = 0;
282 G_ux.exit_code = BOLOS_UX_CONTINUE;
283 return; // break;
284 }
285 }
287 }
288 }
289}
290
291// common code for all screens
292void ux_stack_display(unsigned int stack_slot)
293{
294 // don't display any elements of a previous screen replacement
295 if (G_ux.stack_count > 0 && stack_slot + 1 == G_ux.stack_count) {
296 // at worse a redisplay of the current screen has been requested, ensure to redraw it
297 // correctly
298 G_ux.stack[stack_slot].element_index = 0;
299 ux_stack_display_elements(&G_ux.stack[stack_slot]); // on balenos, no need to wait for the
300 // display processed event
301 }
302 // asking to redraw below top screen (likely the app below the ux)
303 else if (stack_slot == -1UL || G_ux.stack_count == 0) {
304 if (G_ux.exit_code == BOLOS_UX_OK) {
305 G_ux.exit_code = BOLOS_UX_REDRAW;
306 }
307 }
308 // else don't draw (in stack insertion)
309}
unsigned char element_arrays_count
Definition ux_bagl.h:261
bolos_task_status_t exit_code_after_elements_displayed
Definition ux_bagl.h:260
unsigned short element_index
Definition ux_bagl.h:262
const bagl_element_t * element_array
Definition ux_bagl.h:265
struct ux_stack_slot_s::@166304236013336006265026002007073137100166151357 element_arrays[UX_STACK_SLOT_ARRAY_COUNT]
unsigned char element_array_count
Definition ux_bagl.h:266
static void ux_discard_button_gesture(void)
Definition ux_bagl.h:564
#define UX_STACK_SLOT_COUNT
Definition ux_bagl.h:151
struct bagl_element_e bagl_element_t
Definition ux_bagl.h:51
#define G_ux
Definition ux_bagl.h:323
struct ux_stack_slot_s ux_stack_slot_t
Definition ux_bagl.h:160
#define UX_STACK_SLOT_ARRAY_COUNT
Definition ux_bagl.h:155
unsigned int(* callback_int_t)(unsigned int)
Definition ux_bagl.h:158
unsigned int ux_flow_relayout(void)
unsigned int ux_stack_push(void)
Definition ux_stack.c:45
unsigned int ux_stack_is_element_array_present(const bagl_element_t *element_array)
Definition ux_stack.c:29
const bagl_element_t * ux_stack_display_element_callback(const bagl_element_t *element)
Definition ux_stack.c:206
void ux_stack_display(unsigned int stack_slot)
Definition ux_stack.c:292
void ux_stack_remove(unsigned int stack_slot)
Definition ux_stack.c:144
void ux_stack_insert(unsigned int stack_slot)
Definition ux_stack.c:115
void ux_stack_init(unsigned int stack_slot)
Definition ux_stack.c:175
void ux_stack_display_elements(ux_stack_slot_t *slot)
Definition ux_stack.c:225
void ux_stack_redisplay(void)
Definition ux_stack.c:89
unsigned int ux_stack_pop(void)
Definition ux_stack.c:59