Embedded SDK
Embedded SDK
ux.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 "seproxyhal_protocol.h"
20 #include "ux.h"
21 #include "nbgl_touch.h"
22 #include "nbgl_buttons.h"
23 #include "os_io.h"
24 #ifndef HAVE_BOLOS
25 // number of 100ms ticks since the start-up of the app
26 static uint32_t nbTicks;
27 
35 static bool ux_forward_event(bool ignoring_app_if_ux_busy)
36 {
37  G_ux_params.ux_id = BOLOS_UX_EVENT;
38  G_ux_params.len = 0;
39  os_ux(&G_ux_params);
40  G_ux_params.len = os_sched_last_status(TASK_BOLOS_UX);
41  if (G_ux_params.len == BOLOS_UX_REDRAW) {
42  // enable drawing according to UX decision
45  nbgl_refresh();
46  }
47  else if (!ignoring_app_if_ux_busy
48  || ((G_ux_params.len != BOLOS_UX_IGNORE) && (G_ux_params.len != BOLOS_UX_CONTINUE))) {
49  return true;
50  }
51  return false;
52 }
53 
54 #ifdef HAVE_SE_TOUCH
55 static nbgl_touchStatePosition_t pos;
56 
64 void ux_process_finger_event(uint8_t seph_packet[])
65 {
66  bool displayEnabled = ux_forward_event(true);
67  // enable/disable drawing according to UX decision
68  nbgl_objAllowDrawing(displayEnabled);
69 
70  // if the event is not fully consumed by UX, use it for NBGL
71  if (displayEnabled) {
72  pos.state = (seph_packet[3] == SEPROXYHAL_TAG_FINGER_EVENT_TOUCH) ? PRESSED : RELEASED;
73  pos.x = (seph_packet[4] << 8) + seph_packet[5];
74  pos.y = (seph_packet[6] << 8) + seph_packet[7];
75 #ifdef HAVE_HW_TOUCH_SWIPE
76  pos.swipe = seph_packet[10];
77 #endif // HAVE_HW_TOUCH_SWIPE
78  nbgl_touchHandler(false, &pos, nbTicks * 100);
79  nbgl_refresh();
80  }
81 }
82 #else // HAVE_SE_TOUCH
90 void ux_process_button_event(uint8_t seph_packet[])
91 {
92  bool displayEnabled = ux_forward_event(true);
93  // enable/disable drawing according to UX decision
94  nbgl_objAllowDrawing(displayEnabled);
95 
96  // if the event is not fully consumed by UX, use it for NBGL
97  if (displayEnabled) {
98  uint8_t buttons_state = seph_packet[3] >> 1;
99  nbgl_buttonsHandler(buttons_state, nbTicks * 100);
100  nbgl_refresh();
101  }
102 }
103 #endif // HAVE_SE_TOUCH
104 
111 {
112  nbTicks++;
113  // forward to UX
114  bool displayEnabled = ux_forward_event(true);
115 
116  // enable/disable drawing according to UX decision
117  nbgl_objAllowDrawing(displayEnabled);
118 
119  // do not do any action on screens if display is disabled, because
120  // UX has the hand
121  if (!displayEnabled) {
122  return;
123  }
124 
125  // update ticker in NBGL
126  nbgl_screenHandler(100);
127 
128 #ifdef HAVE_SE_TOUCH
129  // handle touch only if detected as pressed in last touch message
130  if (pos.state == PRESSED) {
131  io_touch_info_t touch_info;
132  touch_get_last_info(&touch_info);
133  pos.state = (touch_info.state == SEPROXYHAL_TAG_FINGER_EVENT_TOUCH) ? PRESSED : RELEASED;
134  pos.x = touch_info.x;
135  pos.y = touch_info.y;
136  // Send current touch position to nbgl
137  nbgl_touchHandler(false, &pos, nbTicks * 100);
138  }
139 #endif // HAVE_SE_TOUCH
140  nbgl_refresh();
141 }
142 
147 {
148  // forward to UX
149  ux_forward_event(false);
150 }
151 
152 #endif // HAVE_BOLOS
void nbgl_buttonsHandler(uint8_t buttonState, uint32_t currentTimeMs)
void nbgl_refresh(void)
This functions refreshes the actual screen on display with what has changed since the last refresh.
Definition: nbgl_obj.c:1561
void nbgl_objAllowDrawing(bool enable)
This functions enables or disables drawing/refresh for all further calls.
Definition: nbgl_obj.c:1651
void nbgl_screenRedraw(void)
This function redraws the whole screen on top of stack and its children.
Definition: nbgl_screen.c:66
void nbgl_screenHandler(uint32_t intervaleMs)
Function to be called periodically by system to enable using ticker.
Definition: nbgl_screen.c:475
void nbgl_touchHandler(bool fromUx, nbgl_touchStatePosition_t *touchEvent, uint32_t currentTimeMs)
Function to be called periodically to check touchscreen state and coordinates.
Definition: nbgl_touch.c:285
@ PRESSED
the finger is currently pressing the screen
Definition: nbgl_types.h:202
@ RELEASED
the finger has been released from the screen
Definition: nbgl_types.h:201
The low level Touchscreen event, coming from driver.
Definition: nbgl_obj.h:206
int16_t y
vertical position of the touch (or for a RELEASED the last touched point)
Definition: nbgl_obj.h:213
int16_t x
horizontal position of the touch (or for a RELEASED the last touched point)
Definition: nbgl_obj.h:212
nbgl_touchState_t state
state of the touch event, e.g PRESSED or RELEASED
Definition: nbgl_obj.h:207
unsigned char uint8_t
Definition: usbd_conf.h:53
void ux_process_default_event(void)
Definition: ux.c:146
void ux_process_finger_event(uint8_t seph_packet[])
Process finger event.
Definition: ux.c:64
void ux_process_ticker_event(void)
Process the ticker_event to the os ux handler. Ticker event callback is always called whatever the re...
Definition: ux.c:110
#define G_ux_params
Definition: ux_bagl.h:346