Skip to content

Commit d975ed3

Browse files
committed
Fix reconnect issues when changing device type
1 parent 2e69a79 commit d975ed3

File tree

12 files changed

+46
-19
lines changed

12 files changed

+46
-19
lines changed

src/device_config/config_nv.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ void device_config_write_to_nv() {
3434
}
3535
}
3636

37-
void device_config_remove_from_nv() { hal_nvm_delete(NV_ITEM_DEVICE_CONFIG); }
38-
3937
void device_config_read_from_nv() {
4038
hal_nvm_status_t st = 0;
4139

src/device_config/config_parser.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "base_components/led.h"
1414
#include "base_components/network_indicator.h"
1515
#include "config_nv.h"
16+
#include "device_config/reset.h"
1617
#include "hal/system.h"
1718
#include "hal/zigbee.h"
1819
#include "hal/zigbee_ota.h"
@@ -52,12 +53,11 @@ uint8_t relay_clusters_cnt = 0;
5253
hal_zigbee_cluster clusters[32];
5354
hal_zigbee_endpoint endpoints[10];
5455

55-
void reset_to_default_config();
5656
uint32_t parse_int(const char *s);
5757
char *seek_until(char *cursor, char needle);
5858
char *extract_next_entry(char **cursor);
5959

60-
void onResetClicked(void *_) { hal_zigbee_leave_network(); }
60+
void on_reset_clicked(void *_) { reset_all(); }
6161

6262
void parse_config() {
6363
device_config_read_from_nv();
@@ -68,7 +68,7 @@ void parse_config() {
6868
basic_cluster.manuName[0] = strlen(zb_manufacturer);
6969
if (basic_cluster.manuName[0] > 31) {
7070
printf("Manufacturer too big\r\n");
71-
reset_to_default_config();
71+
reset_all();
7272
}
7373
memcpy(basic_cluster.manuName + 1, zb_manufacturer,
7474
basic_cluster.manuName[0]);
@@ -77,7 +77,7 @@ void parse_config() {
7777
basic_cluster.modelId[0] = strlen(zb_model);
7878
if (basic_cluster.modelId[0] > 31) {
7979
printf("Model too big\r\n");
80-
reset_to_default_config();
80+
reset_all();
8181
}
8282
memcpy(basic_cluster.modelId + 1, zb_model, basic_cluster.modelId[0]);
8383

@@ -93,7 +93,7 @@ void parse_config() {
9393
buttons[buttons_cnt].pin = pin;
9494
buttons[buttons_cnt].long_press_duration_ms = 2000;
9595
buttons[buttons_cnt].multi_press_duration_ms = 800;
96-
buttons[buttons_cnt].on_long_press = onResetClicked;
96+
buttons[buttons_cnt].on_long_press = on_reset_clicked;
9797
buttons_cnt++;
9898
} else if (entry[0] == 'L') {
9999
hal_gpio_pin_t pin = hal_gpio_parse_pin(entry + 1);
@@ -277,12 +277,6 @@ void init_reporting() {
277277

278278
// Helper functions
279279

280-
__attribute__((noreturn)) void reset_to_default_config() {
281-
printf("RESET reset_to_default_config\r\n");
282-
device_config_remove_from_nv();
283-
hal_system_reset();
284-
}
285-
286280
char *seek_until(char *cursor, char needle) {
287281
while (*cursor != needle && *cursor != '\0') {
288282
cursor++;

src/device_config/reset.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#include "reset.h"
3+
#include "hal/nvm.h"
4+
#include "hal/printf_selector.h"
5+
#include "hal/system.h"
6+
7+
__attribute__((noreturn)) void reset_all() {
8+
printf("RESET ALL!\r\n");
9+
hal_nvm_clear_all();
10+
hal_system_reset();
11+
}

src/device_config/reset.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef RESET_H
2+
#define RESET_H
3+
4+
__attribute__((noreturn)) void reset_all();
5+
void leave_network();
6+
7+
#endif // RESET_H

src/silabs/zigbee.slcp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ source:
3636
- {path: ../device_config/config_parser.c}
3737
- {path: ../device_config/config_parser.h}
3838
- {path: ../device_config/nvm_items.h}
39+
- {path: ../device_config/reset.c}
40+
- {path: ../device_config/reset.h}
3941
- {path: ../zigbee/basic_cluster.c}
4042
- {path: ../zigbee/general_commands.c}
4143
- {path: ../zigbee/group_cluster.c}

src/silabs/zigbee_end_device.slcp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ source:
3636
- {path: ../device_config/config_parser.c}
3737
- {path: ../device_config/config_parser.h}
3838
- {path: ../device_config/nvm_items.h}
39+
- {path: ../device_config/reset.c}
40+
- {path: ../device_config/reset.h}
3941
- {path: ../zigbee/basic_cluster.c}
4042
- {path: ../zigbee/general_commands.c}
4143
- {path: ../zigbee/group_cluster.c}

src/stub/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ SOURCES := \
4949
$(SRC_DIR)/base_components/network_indicator.c \
5050
$(SRC_DIR)/device_config/config_parser.c \
5151
$(SRC_DIR)/device_config/config_nv.c \
52+
$(SRC_DIR)/device_config/reset.c \
5253
$(SRC_DIR)/zigbee/basic_cluster.c \
5354
$(SRC_DIR)/zigbee/relay_cluster.c \
5455
$(SRC_DIR)/zigbee/switch_cluster.c \

src/telink/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ COMMON_SOURCES := \
145145
$(SRC_DIR)/base_components/network_indicator.c \
146146
$(SRC_DIR)/base_components/relay.c \
147147
$(SRC_DIR)/device_config/config_nv.c \
148+
$(SRC_DIR)/device_config/reset.c \
148149
$(SRC_DIR)/device_config/config_parser.c \
149150
$(SRC_DIR)/zigbee/basic_cluster.c \
150151
$(SRC_DIR)/zigbee/general_commands.c \

src/telink/main.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ int real_main(startup_state_e state) {
6464
if (timerEvt) {
6565
sleepDuration = timerEvt->timeout;
6666
}
67-
printf("Enter low power (%d ms)\r\n", sleepDuration);
6867
drv_pm_sleep(PM_SLEEP_MODE_SUSPEND,
6968
PM_WAKEUP_SRC_PAD | PM_WAKEUP_SRC_TIMER, sleepDuration);
70-
printf("Exit low power\r\n");
7169
}
7270
#endif
7371
}

src/zigbee/switch_cluster.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#include "consts.h"
66
#include "device_config/nvm_items.h"
77
#include "hal/nvm.h"
8+
89
#include "hal/printf_selector.h"
10+
#include "hal/system.h"
11+
#include "hal/tasks.h"
912
#include "relay_cluster.h"
1013
#include "zigbee_commands.h"
1114

@@ -371,10 +374,19 @@ void switch_cluster_on_button_long_press(zigbee_switch_cluster *cluster) {
371374
ZCL_ATTR_MULTISTATE_INPUT_PRESENT_VALUE);
372375
}
373376

377+
hal_task_t restart_task;
378+
379+
void reset_tasks_handler(void *arg) { hal_system_reset(); }
380+
374381
void switch_cluster_on_button_multi_press(zigbee_switch_cluster *cluster,
375382
uint8_t press_count) {
376383
if (press_count > MULTI_PRESS_CNT_TO_RESET) {
377384
hal_zigbee_leave_network();
385+
// Give it time to leave network before restart
386+
restart_task.handler = reset_tasks_handler;
387+
restart_task.arg = NULL;
388+
hal_tasks_init(&restart_task);
389+
hal_tasks_schedule(&restart_task, 2000);
378390
}
379391
}
380392

0 commit comments

Comments
 (0)