Skip to content

Commit 0d14309

Browse files
committed
Support for changing the wake time in the client
1 parent e5d615d commit 0d14309

File tree

8 files changed

+109
-11
lines changed

8 files changed

+109
-11
lines changed

firmware/application/src/app_cmd.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static data_frame_tx_t *cmd_processor_reset_settings(uint16_t cmd, uint16_t stat
128128
}
129129

130130
static data_frame_tx_t *cmd_processor_get_device_settings(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) {
131-
uint8_t settings[7 + BLE_PAIRING_KEY_LEN] = {};
131+
uint8_t settings[8 + BLE_PAIRING_KEY_LEN] = {};
132132
settings[0] = SETTINGS_CURRENT_VERSION; // current version
133133
settings[1] = settings_get_animation_config(); // animation mode
134134
settings[2] = settings_get_button_press_config('A'); // short A button press mode
@@ -137,7 +137,8 @@ static data_frame_tx_t *cmd_processor_get_device_settings(uint16_t cmd, uint16_t
137137
settings[5] = settings_get_long_button_press_config('B'); // long B button press mode
138138
settings[6] = settings_get_ble_pairing_enable(); // is device require pairing
139139
memcpy(settings + 7, settings_get_ble_connect_key(), BLE_PAIRING_KEY_LEN);
140-
return data_frame_make(cmd, STATUS_SUCCESS, 7 + BLE_PAIRING_KEY_LEN, settings);
140+
settings[7 + BLE_PAIRING_KEY_LEN] = settings_get_sleep_timeout() / 1000U; // wake timeout in seconds
141+
return data_frame_make(cmd, STATUS_SUCCESS, 8 + BLE_PAIRING_KEY_LEN, settings);
141142
}
142143

143144
static data_frame_tx_t *cmd_processor_set_animation_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) {
@@ -195,6 +196,19 @@ static data_frame_tx_t *cmd_processor_set_long_button_press_config(uint16_t cmd,
195196
return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL);
196197
}
197198

199+
static data_frame_tx_t *cmd_processor_get_sleep_timeout(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) {
200+
uint8_t seconds = settings_get_sleep_timeout() / 1000U;
201+
return data_frame_make(cmd, STATUS_SUCCESS, 1, &seconds);
202+
}
203+
204+
static data_frame_tx_t *cmd_processor_set_sleep_timeout(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) {
205+
if (length != 1 || data[0] < SETTINGS_SLEEP_TIMEOUT_MIN_S || data[0] > SETTINGS_SLEEP_TIMEOUT_MAX_S) {
206+
return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL);
207+
}
208+
settings_set_sleep_timeout(data[0]);
209+
return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL);
210+
}
211+
198212
static data_frame_tx_t *cmd_processor_get_ble_pairing_enable(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) {
199213
uint8_t is_enable = settings_get_ble_pairing_enable();
200214
return data_frame_make(cmd, STATUS_SUCCESS, 1, &is_enable);
@@ -1767,6 +1781,8 @@ static cmd_data_map_t m_data_cmd_map[] = {
17671781
{ DATA_CMD_GET_DEVICE_CAPABILITIES, NULL, cmd_processor_get_device_capabilities, NULL },
17681782
{ DATA_CMD_GET_BLE_PAIRING_ENABLE, NULL, cmd_processor_get_ble_pairing_enable, NULL },
17691783
{ DATA_CMD_SET_BLE_PAIRING_ENABLE, NULL, cmd_processor_set_ble_pairing_enable, NULL },
1784+
{ DATA_CMD_GET_SLEEP_TIMEOUT, NULL, cmd_processor_get_sleep_timeout, NULL },
1785+
{ DATA_CMD_SET_SLEEP_TIMEOUT, NULL, cmd_processor_set_sleep_timeout, NULL },
17701786
{ DATA_CMD_GET_ALL_SLOT_NICKS, NULL, cmd_processor_get_all_slot_nicks, NULL },
17711787

17721788
#if defined(PROJECT_CHAMELEON_ULTRA)

firmware/application/src/app_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ static void check_wakeup_src(void) {
479479
light_up_by_slot();
480480

481481
// If no operation follows, wait for the timeout and then deep hibernate
482-
sleep_timer_start(SLEEP_DELAY_MS_BUTTON_WAKEUP);
482+
sleep_timer_start(settings_get_sleep_timeout());
483483
} else if ((m_reset_source & (NRF_POWER_RESETREAS_NFC_MASK | NRF_POWER_RESETREAS_LPCOMP_MASK)) ||
484484
(m_gpregret_val & RESET_ON_LF_FIELD_EXISTS_Msk)) {
485485
NRF_LOG_INFO("WakeUp from rfid field");

firmware/application/src/data_cmd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#define DATA_CMD_GET_BLE_PAIRING_ENABLE (1036)
4747
#define DATA_CMD_SET_BLE_PAIRING_ENABLE (1037)
4848
#define DATA_CMD_GET_ALL_SLOT_NICKS (1038)
49+
#define DATA_CMD_GET_SLEEP_TIMEOUT (1039)
50+
#define DATA_CMD_SET_SLEEP_TIMEOUT (1040)
4951

5052
//
5153
// ******************************************************************

firmware/application/src/settings.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,19 @@ void settings_init_ble_pairing_enable_config(void) {
5353
config.ble_pairing_enable = false;
5454
}
5555

56+
// add on version6
57+
void settings_init_sleep_timeout_config(void) {
58+
config.sleep_timeout = SETTINGS_SLEEP_TIMEOUT_DEFAULT_S;
59+
}
60+
5661
void settings_init_config(void) {
5762
settings_update_version_for_config();
5863
config.animation_config = SettingsAnimationModeFull; // add on version1
5964
settings_init_button_press_config();
6065
settings_init_button_long_press_config();
6166
settings_init_ble_connect_key_config();
6267
settings_init_ble_pairing_enable_config();
68+
settings_init_sleep_timeout_config();
6369
}
6470

6571
void settings_migrate(void) {
@@ -80,6 +86,9 @@ void settings_migrate(void) {
8086
case 4:
8187
settings_init_ble_pairing_enable_config();
8288

89+
case 5:
90+
settings_init_sleep_timeout_config();
91+
8392
/*
8493
* Add new migration steps ABOVE THIS COMMENT
8594
* `settings_update_version_for_config()` and `break` statements should only be used on the last migration step, all the previous steps must fall
@@ -291,3 +300,11 @@ bool settings_get_ble_pairing_enable(void) {
291300
bool settings_get_ble_pairing_enable_first_load(void) {
292301
return m_ble_pairing_enable_first_load_value;
293302
}
303+
304+
uint32_t settings_get_sleep_timeout(void) {
305+
return config.sleep_timeout * 1000U;
306+
}
307+
308+
void settings_set_sleep_timeout(uint8_t seconds) {
309+
config.sleep_timeout = seconds;
310+
}

firmware/application/src/settings.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
#include "utils.h"
77

8-
#define SETTINGS_CURRENT_VERSION 5
8+
#define SETTINGS_CURRENT_VERSION 6
9+
#define SETTINGS_SLEEP_TIMEOUT_DEFAULT_S 8 // default wake timeout in seconds (matches SLEEP_DELAY_MS_BUTTON_WAKEUP)
10+
#define SETTINGS_SLEEP_TIMEOUT_MIN_S 5
11+
#define SETTINGS_SLEEP_TIMEOUT_MAX_S 60
912
#define BLE_PAIRING_KEY_LEN 6
1013
#define DEFAULT_BLE_PAIRING_KEY "123456" // length must == 6
1114

@@ -51,8 +54,8 @@ typedef struct ALIGN_U32 {
5154
// 6 byte
5255
uint8_t ble_connect_key[6];
5356

54-
// 1 byte
55-
uint8_t reserved1; // see bottom.
57+
// 1 byte (add on version6)
58+
uint8_t sleep_timeout; // wake timeout in seconds after button wakeup
5659

5760
/*
5861
* Warning !!!!!!!!!!!!!!!!!!!!!! <-------------
@@ -78,4 +81,7 @@ void settings_set_ble_connect_key(uint8_t *key);
7881
void settings_set_ble_pairing_enable(bool enable);
7982
bool settings_get_ble_pairing_enable(void);
8083
bool settings_get_ble_pairing_enable_first_load(void);
84+
uint32_t settings_get_sleep_timeout(void);
85+
void settings_set_sleep_timeout(uint8_t seconds);
86+
void settings_init_sleep_timeout_config(void);
8187
#endif

software/script/chameleon_cli_unit.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6291,6 +6291,40 @@ def on_exec(self, args: argparse.Namespace):
62916291
print(AnimationMode(self.cmd.get_animation_mode()))
62926292

62936293

6294+
@hw_settings.command("sleeptimeout")
6295+
class HWSettingsSleepTimeout(DeviceRequiredUnit):
6296+
def args_parser(self) -> ArgumentParserNoExit:
6297+
parser = ArgumentParserNoExit()
6298+
parser.description = "Get or set the wake timeout after a button press (5-60 seconds)"
6299+
parser.add_argument(
6300+
"-s",
6301+
"--seconds",
6302+
type=int,
6303+
required=False,
6304+
help="Wake timeout in seconds (5-60)",
6305+
metavar="SECONDS",
6306+
)
6307+
return parser
6308+
6309+
def on_exec(self, args: argparse.Namespace):
6310+
if args.seconds is not None:
6311+
seconds = args.seconds
6312+
if seconds < 5:
6313+
print(color_string((CR, "Error: value is too low. Please enter a value between 5 and 60 seconds.")))
6314+
return
6315+
if seconds > 60:
6316+
print(color_string((CR, "Error: value is too high. Please enter a value between 5 and 60 seconds.")))
6317+
return
6318+
if seconds >= 30:
6319+
print(color_string((CY, "Warning: a long wake timeout will drain the battery faster.")))
6320+
self.cmd.set_sleep_timeout(seconds)
6321+
print(f"Wake timeout set to {seconds} seconds.")
6322+
print(color_string((CY, "Do not forget to store your settings in flash!")))
6323+
else:
6324+
current = self.cmd.get_sleep_timeout()
6325+
print(f"Current wake timeout: {current} seconds")
6326+
6327+
62946328
@hw_settings.command("bleclearbonds")
62956329
class HWSettingsBleClearBonds(DeviceRequiredUnit):
62966330

software/script/chameleon_cmd.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from chameleon_enum import ButtonPressFunction, ButtonType, MifareClassicDarksideStatus
99
from chameleon_enum import MfcKeyType, MfcValueBlockOperator
1010

11-
CURRENT_VERSION_SETTINGS = 5
11+
CURRENT_VERSION_SETTINGS = 6
1212

1313
new_key = b'\x20\x20\x66\x66'
1414
old_keys = [b'\x51\x24\x36\x48', b'\x19\x92\x04\x27']
@@ -1201,6 +1201,24 @@ def set_animation_mode(self, value: int):
12011201
data = struct.pack('!B', value)
12021202
return self.device.send_cmd_sync(Command.SET_ANIMATION_MODE, data)
12031203

1204+
@expect_response(Status.SUCCESS)
1205+
def get_sleep_timeout(self):
1206+
"""
1207+
Get the wake timeout (in seconds) after a button wakeup
1208+
"""
1209+
resp = self.device.send_cmd_sync(Command.GET_SLEEP_TIMEOUT)
1210+
if resp.status == Status.SUCCESS:
1211+
resp.parsed = struct.unpack('!B', resp.data)[0]
1212+
return resp
1213+
1214+
@expect_response(Status.SUCCESS)
1215+
def set_sleep_timeout(self, seconds: int):
1216+
"""
1217+
Set the wake timeout (in seconds) after a button wakeup
1218+
"""
1219+
data = struct.pack('!B', seconds)
1220+
return self.device.send_cmd_sync(Command.SET_SLEEP_TIMEOUT, data)
1221+
12041222
@expect_response(Status.SUCCESS)
12051223
def reset_settings(self):
12061224
"""
@@ -1340,7 +1358,7 @@ def get_device_model(self):
13401358
def get_device_settings(self):
13411359
"""
13421360
Get all possible settings
1343-
For version 5:
1361+
For version 6:
13441362
settings[0] = SETTINGS_CURRENT_VERSION; // current version
13451363
settings[1] = settings_get_animation_config(); // animation mode
13461364
settings[2] = settings_get_button_press_config('A'); // short A button press mode
@@ -1349,6 +1367,7 @@ def get_device_settings(self):
13491367
settings[5] = settings_get_long_button_press_config('B'); // long B button press mode
13501368
settings[6] = settings_get_ble_pairing_enable(); // does device require pairing
13511369
settings[7:13] = settings_get_ble_pairing_key(); // BLE pairing key
1370+
settings[13] = sleep_timeout in seconds; // wake timeout after button wakeup
13521371
"""
13531372
resp = self.device.send_cmd_sync(Command.GET_DEVICE_SETTINGS)
13541373
if resp.status == Status.SUCCESS:
@@ -1359,16 +1378,17 @@ def get_device_settings(self):
13591378
raise ValueError("Settings version in app newer than Chameleon. "
13601379
"Please upgrade Chameleon firmware")
13611380
settings_version, animation_mode, btn_press_A, btn_press_B, btn_long_press_A, \
1362-
btn_long_press_B, ble_pairing_enable, ble_pairing_key = \
1363-
struct.unpack('!BBBBBBB6s', resp.data)
1381+
btn_long_press_B, ble_pairing_enable, ble_pairing_key, sleep_timeout = \
1382+
struct.unpack('!BBBBBBB6sB', resp.data)
13641383
resp.parsed = {'settings_version': settings_version,
13651384
'animation_mode': animation_mode,
13661385
'btn_press_A': btn_press_A,
13671386
'btn_press_B': btn_press_B,
13681387
'btn_long_press_A': btn_long_press_A,
13691388
'btn_long_press_B': btn_long_press_B,
13701389
'ble_pairing_enable': ble_pairing_enable,
1371-
'ble_pairing_key': ble_pairing_key}
1390+
'ble_pairing_key': ble_pairing_key,
1391+
'sleep_timeout': sleep_timeout}
13721392
return resp
13731393

13741394
@expect_response(Status.SUCCESS)

software/script/chameleon_enum.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class Command(enum.IntEnum):
5757
GET_BLE_PAIRING_ENABLE = 1036
5858
SET_BLE_PAIRING_ENABLE = 1037
5959

60+
GET_SLEEP_TIMEOUT = 1039
61+
SET_SLEEP_TIMEOUT = 1040
62+
6063
HF14A_SCAN = 2000
6164
MF1_DETECT_SUPPORT = 2001
6265
MF1_DETECT_PRNG = 2002

0 commit comments

Comments
 (0)