mirror of
https://github.com/Keychron/qmk_firmware.git
synced 2024-11-22 08:27:56 +06:00
9632360caa
* Add ARRAY_SIZE and CEILING utility macros * Apply a coccinelle patch to use ARRAY_SIZE * fix up some straggling items * Fix 'make test:secure' * Enhance ARRAY_SIZE macro to reject acting on pointers The previous definition would not produce a diagnostic for ``` int *p; size_t num_elem = ARRAY_SIZE(p) ``` but the new one will. * explicitly get definition of ARRAY_SIZE * Convert to ARRAY_SIZE when const is involved The following spatch finds additional instances where the array is const and the division is by the size of the type, not the size of the first element: ``` @ rule5a using "empty.iso" @ type T; const T[] E; @@ - (sizeof(E)/sizeof(T)) + ARRAY_SIZE(E) @ rule6a using "empty.iso" @ type T; const T[] E; @@ - sizeof(E)/sizeof(T) + ARRAY_SIZE(E) ``` * New instances of ARRAY_SIZE added since initial spatch run * Use `ARRAY_SIZE` in docs (found by grep) * Manually use ARRAY_SIZE hs_set is expected to be the same size as uint16_t, though it's made of two 8-bit integers * Just like char, sizeof(uint8_t) is guaranteed to be 1 This is at least true on any plausible system where qmk is actually used. Per my understanding it's universally true, assuming that uint8_t exists: https://stackoverflow.com/questions/48655310/can-i-assume-that-sizeofuint8-t-1 * Run qmk-format on core C files touched in this branch Co-authored-by: Stefan Kerkmann <karlk90@pm.me>
104 lines
2.4 KiB
C
104 lines
2.4 KiB
C
// Copyright 2022 QMK
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include "secure.h"
|
|
#include "timer.h"
|
|
#include "util.h"
|
|
|
|
#ifndef SECURE_UNLOCK_TIMEOUT
|
|
# define SECURE_UNLOCK_TIMEOUT 5000
|
|
#endif
|
|
|
|
#ifndef SECURE_IDLE_TIMEOUT
|
|
# define SECURE_IDLE_TIMEOUT 60000
|
|
#endif
|
|
|
|
#ifndef SECURE_UNLOCK_SEQUENCE
|
|
# define SECURE_UNLOCK_SEQUENCE \
|
|
{ \
|
|
{ 0, 0 } \
|
|
}
|
|
#endif
|
|
|
|
static secure_status_t secure_status = SECURE_LOCKED;
|
|
static uint32_t unlock_time = 0;
|
|
static uint32_t idle_time = 0;
|
|
|
|
static void secure_hook(secure_status_t secure_status) {
|
|
secure_hook_quantum(secure_status);
|
|
secure_hook_kb(secure_status);
|
|
}
|
|
|
|
secure_status_t secure_get_status(void) {
|
|
return secure_status;
|
|
}
|
|
|
|
void secure_lock(void) {
|
|
secure_status = SECURE_LOCKED;
|
|
secure_hook(secure_status);
|
|
}
|
|
|
|
void secure_unlock(void) {
|
|
secure_status = SECURE_UNLOCKED;
|
|
idle_time = timer_read32();
|
|
secure_hook(secure_status);
|
|
}
|
|
|
|
void secure_request_unlock(void) {
|
|
if (secure_status == SECURE_LOCKED) {
|
|
secure_status = SECURE_PENDING;
|
|
unlock_time = timer_read32();
|
|
}
|
|
secure_hook(secure_status);
|
|
}
|
|
|
|
void secure_activity_event(void) {
|
|
if (secure_status == SECURE_UNLOCKED) {
|
|
idle_time = timer_read32();
|
|
}
|
|
}
|
|
|
|
void secure_keypress_event(uint8_t row, uint8_t col) {
|
|
static const uint8_t sequence[][2] = SECURE_UNLOCK_SEQUENCE;
|
|
static const uint8_t sequence_len = ARRAY_SIZE(sequence);
|
|
|
|
static uint8_t offset = 0;
|
|
if ((sequence[offset][0] == row) && (sequence[offset][1] == col)) {
|
|
offset++;
|
|
if (offset == sequence_len) {
|
|
offset = 0;
|
|
secure_unlock();
|
|
}
|
|
} else {
|
|
offset = 0;
|
|
secure_lock();
|
|
}
|
|
}
|
|
|
|
void secure_task(void) {
|
|
#if SECURE_UNLOCK_TIMEOUT != 0
|
|
// handle unlock timeout
|
|
if (secure_status == SECURE_PENDING) {
|
|
if (timer_elapsed32(unlock_time) >= SECURE_UNLOCK_TIMEOUT) {
|
|
secure_lock();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if SECURE_IDLE_TIMEOUT != 0
|
|
// handle idle timeout
|
|
if (secure_status == SECURE_UNLOCKED) {
|
|
if (timer_elapsed32(idle_time) >= SECURE_IDLE_TIMEOUT) {
|
|
secure_lock();
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
__attribute__((weak)) bool secure_hook_user(secure_status_t secure_status) {
|
|
return true;
|
|
}
|
|
__attribute__((weak)) bool secure_hook_kb(secure_status_t secure_status) {
|
|
return secure_hook_user(secure_status);
|
|
}
|