#
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

menuconfig ZIGBEE
	bool "Enable Zigbee stack"
	imply FPU
	select APP_LINK_WITH_ZBOSS
	select PM_SINGLE_IMAGE
	select NET_L2_ZIGBEE
	select NETWORKING
	select NET_PKT_TXTIME
	select REBOOT
	imply ENTROPY_GENERATOR
	imply POLL
	imply FLASH
	imply FLASH_PAGE_LAYOUT
	imply FLASH_MAP
	imply MPU_ALLOW_FLASH_WRITE
	depends on (SOC_NRF52840 || SOC_NRF52833 || SOC_NRF5340_CPUAPP)

if ZIGBEE

rsource "lib/Kconfig"

# Override libc implementation
choice LIBC_IMPLEMENTATION
	default MINIMAL_LIBC
	help
	  Use minimal libc implementation with Zigbee.
endchoice

config NET_L2_ZIGBEE
	bool "Zigbee L2"
	depends on NETWORKING
	select NET_L2_CUSTOM_IEEE802154
	select NET_PKT_TIMESTAMP
	select IEEE802154_NRF5_FCS_IN_LENGTH

choice ZIGBEE_CHANNEL_SELECTION_MODE
	prompt "Zigbee channel selection mode"
	default ZIGBEE_CHANNEL_SELECTION_MODE_SINGLE

config ZIGBEE_CHANNEL_SELECTION_MODE_SINGLE
	bool "Single channel"
	help
	  "Zigbee uses exactly one channel specified by its number"

config ZIGBEE_CHANNEL_SELECTION_MODE_MULTI
	bool "Group of channels"
	help
	  "Zigbee uses a group of channels specified by bitwise mask"

endchoice # "Zigbee channel selection mode"

config ZIGBEE_CHANNEL
	int "802.15.4 channel used by Zigbee <11-26>"
	range 11 26
	default 16
	depends on ZIGBEE_CHANNEL_SELECTION_MODE_SINGLE
	help
	  802.15.4 channel used by Zigbee. Defaults to 16.

config ZIGBEE_CHANNEL_MASK
	hex "Mask of 802.15.4 channels used by Zigbee"
	range 0x800 0x7FFF800
	default 0x7FFF800
	depends on ZIGBEE_CHANNEL_SELECTION_MODE_MULTI
	help
	  Bitwise "OR" mask of channels used by Zigbee.
	  The default setting enables all channels.

	  Available channels:
	   - bits 31..27: UNUSED, should be cleared
	   - bits 26..11: bit n enables its corresponding channel
	   - bits 10..0: UNUSED, should be cleared

choice
	prompt "Select Zigbee device role"

config ZIGBEE_ROLE_END_DEVICE
	bool "End Device role"

config ZIGBEE_ROLE_ROUTER
	bool "Router role"

config ZIGBEE_ROLE_COORDINATOR
	bool "Coordinator role"

endchoice

config ZBOSS_DEFAULT_THREAD_PRIORITY
	int "Set default ZBOSS thread priority"
	default 3

config ZBOSS_DEFAULT_THREAD_STACK_SIZE
	int "Stack size of ZBOSS Zephyr task"
	default 2048
	help
	  Stack size of ZBOSS Zephyr task. This is the task zboss_main_loop_iteration is called from.

config ZIGBEE_APP_CB_QUEUE_LENGTH
	int "Length of the application callback and alarm queue"
	default 10
	help
	  This queue is used to pass application callbacks and alarms from other
	  threads/ISR to the ZBOSS main loop context.
	  Elements from this queue are flushed right after ZBOSS context awakes,
	  before the actual callback execution.

config ZIGBEE_DEBUG_FUNCTIONS
	bool "Include Zigbee debug functions"
	help
	  Include functions to suspend/resume ZBOSS thread.
	  It may be helpful when debugging but using this functions can cause instability of the device.

# Configure default behavior of zb_osif_abort() function called as ZBOSS assert handler
choice
	prompt "Default behavior for ZBOSS stack internal assert handler"
	default ZBOSS_RESET_ON_ASSERT

	config ZBOSS_RESET_ON_ASSERT
		bool "Reset device when a ZBOSS assert occurs"
		depends on REBOOT

	config ZBOSS_HALT_ON_ASSERT
		bool "Halt device when a ZBOSS assert occurs. Use only for testing and debugging"

endchoice

menu "Zigbee Log configuration"

config ZBOSS_ERROR_PRINT_TO_LOG
	bool "Log ZBOSS errors"
	default y
	select ZIGBEE_ERROR_TO_STRING_ENABLED
	help
	  Log ZBOSS errors.

config ZIGBEE_ERROR_TO_STRING_ENABLED
	bool "Include functions for mapping ZBOSS errors to string"
	default y
	help
	  Include functions for mapping ZBOSS errors to string.

# Configure ZBOSS_TRACE_LOG_LEVEL
choice
	prompt "Max compiled-in log level for ZBOSS trace"
	default ZBOSS_TRACE_LOG_LEVEL_OFF

config ZBOSS_TRACE_LOG_LEVEL_OFF
	bool "Off"

config ZBOSS_TRACE_LOG_LEVEL_ERR
	depends on LOG
	bool "Error"

config ZBOSS_TRACE_LOG_LEVEL_WRN
	depends on LOG
	bool "Warning"

config ZBOSS_TRACE_LOG_LEVEL_INF
	depends on LOG
	bool "Info"

config ZBOSS_TRACE_LOG_LEVEL_DBG
	depends on LOG
	bool "Debug"

endchoice

config ZBOSS_TRACE_LOG_LEVEL
	int
	default 0 if ZBOSS_TRACE_LOG_LEVEL_OFF
	default 1 if ZBOSS_TRACE_LOG_LEVEL_ERR
	default 2 if ZBOSS_TRACE_LOG_LEVEL_WRN
	default 3 if ZBOSS_TRACE_LOG_LEVEL_INF
	default 4 if ZBOSS_TRACE_LOG_LEVEL_DBG

config ZBOSS_TRACE_MASK
	hex "Trace mask of ZBOSS stack logs"
	default 0x0000
	range 0x00000000 0xFFFFFFFF
	help
	  Selectively enable Zigbee binary trace logs.
	  The mask value is expected to be a bitwise OR of values assigned to selected modules.

	  Available modules:

	  - 0x04000000 Reserved
	  - 0x02000000 Reserved
	  - 0x01000000 Reserved
	  - 0x00800000 Reserved
	  - 0x00400000 Reserved
	  - 0x00200000 Zigbee NCP: transport (LL)
	  - 0x00100000 Zigbee NCP: command adapters (HL)
	  - 0x00080000 ZCL: Firmware over-the-air upgrade
	  - 0x00040000 IAS zone device: battery monitoring
	  - 0x00020000 Reserved
	  - 0x00010000 MAC lower layer
	  - 0x00008000 MAC layer API calls
	  - 0x00004000 Zigbee Green Power
	  - 0x00002000 Custom components
	  - 0x00001000 Reserved
	  - 0x00000800 Application
	  - 0x00000400 Reserved
	  - 0x00000200 Zigbee Light Link
	  - 0x00000100 Zigbee Cluster Library
	  - 0x00000080 Security
	  - 0x00000040 Zigbee Device Object
	  - 0x00000020 Zigbee Smart Energy
	  - 0x00000010 Application support layer
	  - 0x00000008 Network layer
	  - 0x00000004 MAC layer
	  - 0x00000002 Memory management
	  - 0x00000001 Common

	  For example, to enable traces related to OTA DFU, set this option to 0x00080100.

	  Note: For general debugging purposes, use 0x00000C48.

config ZBOSS_TRAF_DUMP
	bool "Enable logging received 802.15.4 frames over ZBOSS traces"
	depends on !ZBOSS_TRACE_LOG_LEVEL_OFF
	help
	  Dumps all packets destined to the node over ZBOSS binary trace protocol

if ZIGBEE_HAVE_SERIAL

choice
	prompt "Select ZBOSS trace logging method: hexdump, binary, NCP transport"
	default ZBOSS_TRACE_HEXDUMP_LOGGING

config ZBOSS_TRACE_HEXDUMP_LOGGING
	bool "Enable logging hexdumped ZBOSS Traces"
	select RING_BUFFER

config ZBOSS_TRACE_BINARY_LOGGING
	bool "Enable logging binary ZBOSS Traces"
	select SERIAL
	select RING_BUFFER

config ZBOSS_TRACE_BINARY_NCP_TRANSPORT_LOGGING
	bool "Enable logging binary ZBOSS Traces over the NCP transport channel"
	depends on ZIGBEE_HAVE_ASYNC_SERIAL

endchoice
endif # ZIGBEE_HAVE_SERIAL

if (ZBOSS_TRACE_HEXDUMP_LOGGING || ZBOSS_TRACE_BINARY_LOGGING)

config ZBOSS_TRACE_LOGGER_BUFFER_SIZE
	int "Size of ring buffer to store ZBOSS Traces"
	default 4096
	range 256 2147483648

endif # (ZBOSS_TRACE_HEXDUMP_LOGGING || ZBOSS_TRACE_BINARY_LOGGING)

if ZBOSS_TRACE_BINARY_LOGGING

choice
	prompt "Backend used to log ZBOSS Traces"
	default ZBOSS_TRACE_UART_LOGGING

config ZBOSS_TRACE_UART_LOGGING
	bool "UART Serial"
	select UART_INTERRUPT_DRIVEN

config ZBOSS_TRACE_USB_CDC_LOGGING
	bool "UART USB CDC (supported only for Zigbee NCP sample)"
	select USB_DEVICE_STACK
	select USB_CDC_ACM
	select UART_LINE_CTRL
	select UART_INTERRUPT_DRIVEN

endchoice

endif # ZBOSS_TRACE_BINARY_LOGGING

# Configure ZBOSS_OSIF_LOG_LEVEL
module = ZBOSS_OSIF
module-str = ZBOSS OSIF layer
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"

endmenu #menu "Zigbee Log configuration"

menu "ZBOSS OSIF configuration"

config ZIGBEE_HAVE_SERIAL
	bool "UART serial abstract for ZBOSS OSIF"
	default y

menuconfig ZIGBEE_HAVE_ASYNC_SERIAL
	bool "Asynchronous UART serial abstract for ZBOSS OSIF"
	select SERIAL
	select UART_INTERRUPT_DRIVEN
	select RING_BUFFER
	depends on ZIGBEE_HAVE_SERIAL

if ZIGBEE_HAVE_ASYNC_SERIAL

config ZIGBEE_UART_SUPPORTS_FLOW_CONTROL
	bool "Set to true to inform application that Zigbee UART device supports flow control"
	default n
	depends on UART_LINE_CTRL

config ZIGBEE_UART_TX_TIMEOUT
	int "Timeout value for starting asynchronous transmission, in milliseconds"
	default 10

config ZIGBEE_UART_PARTIAL_TX_TIMEOUT
	int "Timeout value between the last transmitted byte and the TX ready event if only a part of TX buffer was transmitted, in milliseconds"
	default 10

config ZIGBEE_UART_RX_TIMEOUT
	int "Timeout value for starting asynchronous reception, in milliseconds"
	default 10

config ZIGBEE_UART_PARTIAL_RX_TIMEOUT
	int "Timeout value between the last received byte and the RX event if only a part of RX buffer was received, in milliseconds"
	default 10

config ZIGBEE_UART_RX_BUF_LEN
	int "Size of the asynchronous receive buffer"
	default 16

config ZIGBEE_UART_TX_BUF_LEN
	int "Size of the synchronous transmit buffer"
	default 128

endif #ZIGBEE_HAVE_ASYNC_SERIAL

config ZIGBEE_USE_SOFTWARE_AES
	bool "Use software based AES"
	select NRF_OBERON
	default n

config ZIGBEE_USE_LEDS
	bool "LEDs abstract for ZBOSS OSIF"
	imply GPIO
	imply DK_LIBRARY

config ZIGBEE_USE_DIMMABLE_LED
	bool "Dimmable LED (PWM) abstract for ZBOSS OSIF"
	imply GPIO
	imply PWM

config ZIGBEE_USE_BUTTONS
	bool "Buttons abstract for ZBOSS OSIF"
	imply GPIO
	imply DK_LIBRARY

config ZIGBEE_NVRAM_PAGE_COUNT
	int "The number of ZBOSS NVRAM pages"
	default 2
	range 2 16

config ZIGBEE_NVRAM_PAGE_SIZE
	depends on !FLASH_MAP
	int "The size of a single ZBOSS NVRAM page"
	default 512

config ZIGBEE_TC_REJOIN_ENABLED
	bool "Enables Trust Center Rejoin"
	default y

DT_CHOSEN_NCS_ZIGBEE_TIMER := ncs,zigbee-timer

choice
	prompt "ZBOSS time source"
	default ZIGBEE_TIME_COUNTER if ZIGBEE_LIBRARY_PRODUCTION && \
		$(dt_chosen_enabled,$(DT_CHOSEN_NCS_ZIGBEE_TIMER))

config ZIGBEE_TIME_COUNTER
	bool "Dedicated timer-based counter"
	depends on $(dt_chosen_enabled,$(DT_CHOSEN_NCS_ZIGBEE_TIMER))
	imply COUNTER

config ZIGBEE_TIME_KTIMER
	bool "Kernel (system) time"

endchoice

endmenu #menu "ZBOSS osif configuration"

config APP_LINK_WITH_ZIGBEE
	bool
	default y
	help
	  Link application with Zigbee subsystem

endif #ZIGBEE
