Skip to content

Commit

Permalink
fix(zigbee): Increase timeout, commision again on failure + setScanDu…
Browse files Browse the repository at this point in the history
…ration (#10651)

* fix(zigbee): Increase timeout, commision again on failure
* fix(zigbee): Update library keywords
  • Loading branch information
P-R-O-C-H-Y authored Nov 25, 2024
1 parent 0bcdd2c commit 0f3191e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions libraries/Zigbee/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ getRadioConfig KEYWORD2
setHostConfig KEYWORD2
getHostConfig KEYWORD2
setPrimaryChannelMask KEYWORD2
setScanDuration KEYWORD2
getScanDuration KEYWORD2
setRebootOpenNetwork KEYWORD2
openNetwork KEYWORD2
scanNetworks KEYWORD2
Expand Down
17 changes: 14 additions & 3 deletions libraries/Zigbee/src/ZigbeeCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "ZigbeeHandlers.cpp"
#include "Arduino.h"

#define ZB_INIT_TIMEOUT 10000 // 10 seconds
#define ZB_INIT_TIMEOUT 30000 // 30 seconds

extern "C" void zb_set_ed_node_descriptor(bool power_src, bool rx_on_when_idle, bool alloc_addr);
static bool edBatteryPowered = false;
Expand All @@ -20,6 +20,7 @@ ZigbeeCore::ZigbeeCore() {
_scan_status = ZB_SCAN_FAILED;
_started = false;
_connected = false;
_scan_duration = 4; // maximum scan duration
if (!lock) {
lock = xSemaphoreCreateBinary();
if (lock == NULL) {
Expand Down Expand Up @@ -90,6 +91,8 @@ void ZigbeeCore::addEndpoint(ZigbeeEP *ep) {
}

static void esp_zb_task(void *pvParameters) {
esp_zb_bdb_set_scan_duration(Zigbee.getScanDuration());

/* initialize Zigbee stack */
ESP_ERROR_CHECK(esp_zb_start(false));

Expand Down Expand Up @@ -178,6 +181,14 @@ void ZigbeeCore::setPrimaryChannelMask(uint32_t mask) {
_primary_channel_mask = mask;
}

void ZigbeeCore::setScanDuration(uint8_t duration) {
if (duration < 1 || duration > 4) {
log_e("Invalid scan duration, must be between 1 and 4");
return;
}
_scan_duration = duration;
}

void ZigbeeCore::setRebootOpenNetwork(uint8_t time) {
_open_network = time;
}
Expand Down Expand Up @@ -235,8 +246,8 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
}
} else {
/* commissioning failed */
log_e("Failed to initialize Zigbee stack (status: %s)", esp_err_to_name(err_status));
xSemaphoreGive(Zigbee.lock);
log_w("Commissioning failed, trying again...", esp_err_to_name(err_status));
esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_INITIALIZATION, 500);
}
break;
case ESP_ZB_BDB_SIGNAL_FORMATION: // Coordinator
Expand Down
8 changes: 7 additions & 1 deletion libraries/Zigbee/src/ZigbeeCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ZigbeeCore {
esp_zb_host_config_t _host_config;
uint32_t _primary_channel_mask;
int16_t _scan_status;
uint8_t _scan_duration;

esp_zb_ep_list_t *_zb_ep_list;
zigbee_role_t _role;
Expand Down Expand Up @@ -109,7 +110,12 @@ class ZigbeeCore {
void setHostConfig(esp_zb_host_config_t config);
esp_zb_host_config_t getHostConfig();

void setPrimaryChannelMask(uint32_t mask);
void setPrimaryChannelMask(uint32_t mask); // By default all channels are scanned (11-26) -> mask 0x07FFF800
void setScanDuration(uint8_t duration); // Can be set from 1 - 4. 1 is fastest, 4 is slowest
uint8_t getScanDuration() {
return _scan_duration;
}

void setRebootOpenNetwork(uint8_t time);
void openNetwork(uint8_t time);

Expand Down

0 comments on commit 0f3191e

Please sign in to comment.