2021-12-16 04:42:19 -05:00
|
|
|
/*
|
2023-01-27 02:24:27 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
|
2021-12-16 04:42:19 -05:00
|
|
|
*
|
2023-01-27 02:24:27 -05:00
|
|
|
* SPDX-License-Identifier: LicenseRef-Included
|
2021-12-16 04:42:19 -05:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form, except as embedded into a Espressif Systems
|
|
|
|
* integrated circuit in a product or a software update for such product,
|
|
|
|
* must reproduce the above copyright notice, this list of conditions and
|
|
|
|
* the following disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* 4. Any software provided in binary form under this license must not be reverse
|
|
|
|
* engineered, decompiled, modified and/or disassembled.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2023-05-16 05:04:47 -04:00
|
|
|
#include <fcntl.h>
|
2021-12-16 04:42:19 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2023-04-04 00:06:01 -04:00
|
|
|
#include "esp_netif.h"
|
|
|
|
#include "esp_vfs_eventfd.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "protocol_examples_common.h"
|
|
|
|
#include "esp_coexist_internal.h"
|
2021-12-16 04:42:19 -05:00
|
|
|
#include "esp_zigbee_gateway.h"
|
|
|
|
|
2023-05-16 05:04:47 -04:00
|
|
|
#include "esp_vfs_dev.h"
|
|
|
|
#include "esp_vfs_usb_serial_jtag.h"
|
|
|
|
#include "driver/usb_serial_jtag.h"
|
|
|
|
|
2021-12-16 04:42:19 -05:00
|
|
|
#if (!defined ZB_MACSPLIT_HOST && defined ZB_MACSPLIT_DEVICE)
|
2022-09-22 02:51:30 -04:00
|
|
|
#error Only Zigbee gateway host device should be defined
|
2021-12-16 04:42:19 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *TAG = "ESP_ZB_GATEWAY";
|
|
|
|
|
2023-05-16 05:04:47 -04:00
|
|
|
/* Note: Please select the correct console output port based on the development board in menuconfig */
|
|
|
|
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
|
|
|
|
esp_err_t esp_zb_gateway_console_init(void)
|
|
|
|
{
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
/* Disable buffering on stdin */
|
|
|
|
setvbuf(stdin, NULL, _IONBF, 0);
|
|
|
|
|
|
|
|
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
|
|
|
|
esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
|
|
|
|
/* Move the caret to the beginning of the next line on '\n' */
|
|
|
|
esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
|
|
|
|
|
|
|
|
/* Enable non-blocking mode on stdin and stdout */
|
|
|
|
fcntl(fileno(stdout), F_SETFL, O_NONBLOCK);
|
|
|
|
fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
usb_serial_jtag_driver_config_t usb_serial_jtag_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
|
|
|
|
ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
|
|
|
|
esp_vfs_usb_serial_jtag_use_driver();
|
|
|
|
esp_vfs_dev_uart_register();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-16 04:42:19 -05:00
|
|
|
/********************* Define functions **************************/
|
2022-09-22 02:51:30 -04:00
|
|
|
static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask)
|
2021-12-16 04:42:19 -05:00
|
|
|
{
|
2022-09-22 02:51:30 -04:00
|
|
|
ESP_ERROR_CHECK(esp_zb_bdb_start_top_level_commissioning(mode_mask));
|
2021-12-16 04:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-10-26 02:40:02 -04:00
|
|
|
void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
|
2021-12-16 04:42:19 -05:00
|
|
|
{
|
2022-10-26 02:40:02 -04:00
|
|
|
uint32_t *p_sg_p = signal_struct->p_app_signal;
|
|
|
|
esp_err_t err_status = signal_struct->esp_err_status;
|
|
|
|
esp_zb_app_signal_type_t sig_type = *p_sg_p;
|
|
|
|
esp_zb_zdo_signal_device_annce_params_t *dev_annce_params = NULL;
|
|
|
|
esp_zb_zdo_signal_macsplit_dev_boot_params_t *rcp_version = NULL;
|
2021-12-16 04:42:19 -05:00
|
|
|
|
2022-10-26 02:40:02 -04:00
|
|
|
switch (sig_type) {
|
|
|
|
case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP:
|
2021-12-16 04:42:19 -05:00
|
|
|
ESP_LOGI(TAG, "Zigbee stack initialized");
|
2022-10-26 02:40:02 -04:00
|
|
|
esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_INITIALIZATION);
|
2021-12-16 04:42:19 -05:00
|
|
|
break;
|
2022-10-26 02:40:02 -04:00
|
|
|
case ESP_ZB_MACSPLIT_DEVICE_BOOT:
|
2021-12-16 04:42:19 -05:00
|
|
|
ESP_LOGI(TAG, "Zigbee rcp device booted");
|
2022-10-26 02:40:02 -04:00
|
|
|
rcp_version = (esp_zb_zdo_signal_macsplit_dev_boot_params_t*)esp_zb_app_signal_get_params(p_sg_p);
|
2022-09-22 02:51:30 -04:00
|
|
|
ESP_LOGI(TAG, "Running RCP Version:%s", rcp_version->version_str);
|
2021-12-16 04:42:19 -05:00
|
|
|
break;
|
2022-10-26 02:40:02 -04:00
|
|
|
case ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START:
|
|
|
|
case ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT:
|
|
|
|
if (err_status == ESP_OK) {
|
2021-12-16 04:42:19 -05:00
|
|
|
ESP_LOGI(TAG, "Start network formation");
|
2022-10-26 02:40:02 -04:00
|
|
|
esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_FORMATION);
|
2021-12-16 04:42:19 -05:00
|
|
|
} else {
|
2022-10-26 02:40:02 -04:00
|
|
|
ESP_LOGE(TAG, "Failed to initialize Zigbee stack (status: %d)", err_status);
|
2021-12-16 04:42:19 -05:00
|
|
|
}
|
|
|
|
break;
|
2022-10-26 02:40:02 -04:00
|
|
|
case ESP_ZB_BDB_SIGNAL_FORMATION:
|
|
|
|
if (err_status == ESP_OK) {
|
|
|
|
esp_zb_ieee_addr_t ieee_address;
|
2022-09-22 02:51:30 -04:00
|
|
|
esp_zb_get_long_address(ieee_address);
|
2023-03-20 05:04:37 -04:00
|
|
|
ESP_LOGI(TAG, "Formed network successfully (ieee_address: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx, Channel:%d)",
|
2021-12-16 04:42:19 -05:00
|
|
|
ieee_address[7], ieee_address[6], ieee_address[5], ieee_address[4],
|
|
|
|
ieee_address[3], ieee_address[2], ieee_address[1], ieee_address[0],
|
2023-03-20 05:04:37 -04:00
|
|
|
esp_zb_get_pan_id(), esp_zb_get_current_channel());
|
2022-10-26 02:40:02 -04:00
|
|
|
esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING);
|
2021-12-16 04:42:19 -05:00
|
|
|
} else {
|
2022-10-26 02:40:02 -04:00
|
|
|
ESP_LOGI(TAG, "Restart network formation (status: %d)", err_status);
|
|
|
|
esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_FORMATION, 1000);
|
2021-12-16 04:42:19 -05:00
|
|
|
}
|
|
|
|
break;
|
2022-10-26 02:40:02 -04:00
|
|
|
case ESP_ZB_BDB_SIGNAL_STEERING:
|
|
|
|
if (err_status == ESP_OK) {
|
2021-12-16 04:42:19 -05:00
|
|
|
ESP_LOGI(TAG, "Network steering started");
|
|
|
|
}
|
|
|
|
break;
|
2022-10-26 02:40:02 -04:00
|
|
|
case ESP_ZB_ZDO_SIGNAL_DEVICE_ANNCE:
|
|
|
|
dev_annce_params = (esp_zb_zdo_signal_device_annce_params_t *)esp_zb_app_signal_get_params(p_sg_p);
|
2021-12-16 04:42:19 -05:00
|
|
|
ESP_LOGI(TAG, "New device commissioned or rejoined (short: 0x%04hx)", dev_annce_params->device_short_addr);
|
2022-01-11 02:52:34 -05:00
|
|
|
break;
|
2021-12-16 04:42:19 -05:00
|
|
|
default:
|
2022-10-26 02:40:02 -04:00
|
|
|
ESP_LOGI(TAG, "ZDO signal: %d, status: %d", sig_type, err_status);
|
2021-12-16 04:42:19 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 02:51:30 -04:00
|
|
|
static void esp_zb_task(void *pvParameters)
|
2021-12-16 04:42:19 -05:00
|
|
|
{
|
2022-09-22 02:51:30 -04:00
|
|
|
/* initialize Zigbee stack with Zigbee coordinator config */
|
|
|
|
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZC_CONFIG();
|
|
|
|
esp_zb_init(&zb_nwk_cfg);
|
2021-12-16 04:42:19 -05:00
|
|
|
/* initiate Zigbee Stack start without zb_send_no_autostart_signal auto-start */
|
2023-03-20 05:04:37 -04:00
|
|
|
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
|
2022-09-22 02:51:30 -04:00
|
|
|
ESP_ERROR_CHECK(esp_zb_start(false));
|
|
|
|
esp_zb_main_loop_iteration();
|
2021-12-16 04:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-01-11 02:52:34 -05:00
|
|
|
void app_main(void)
|
2021-12-16 04:42:19 -05:00
|
|
|
{
|
2022-09-22 02:51:30 -04:00
|
|
|
esp_zb_platform_config_t config = {
|
|
|
|
.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
|
|
|
|
.host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
|
2021-12-16 04:42:19 -05:00
|
|
|
};
|
|
|
|
/* load Zigbee gateway platform config to initialization */
|
2022-09-22 02:51:30 -04:00
|
|
|
ESP_ERROR_CHECK(esp_zb_platform_config(&config));
|
2023-04-04 00:06:01 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2023-05-16 05:04:47 -04:00
|
|
|
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
|
|
|
|
ESP_ERROR_CHECK(esp_zb_gateway_console_init());
|
|
|
|
#endif
|
2023-04-04 00:06:01 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
#if CONFIG_ESP_COEX_SW_COEXIST_ENABLE
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM));
|
|
|
|
coex_enable();
|
|
|
|
coex_schm_status_bit_set(1, 1);
|
|
|
|
#else
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
|
|
|
|
#endif
|
|
|
|
#endif
|
2022-09-22 02:51:30 -04:00
|
|
|
xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
|
2021-12-16 04:42:19 -05:00
|
|
|
}
|