[nrfconnect] Introduced several platform configuration fixes  (#33194)

* [nrfconnect] Disable Zephyr socket extensions

The recvmsg() is now implemented natively in Zephyr,
so we are not supposed to define a custom one.

Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no>

* [nrfconnect] Remove D_SYS__PTHREADTYPES_H_ flag

... from the gnu17 configuration to avoid missing
pthread types definitions (_pthreadtypes.h).

This tricked the preprocessor the header was already
included, hence we didn't get its content.

Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no>

* [nrfconnect] Use newlib as a default libc implementation.

The PICOLIB is now used by the default.
NEWLIB_LIBC symbol is no longer selected by hostap.
OpenThread selects NEWLIB_LIBC anyway.

Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no>

* [zephyr] Added OpenThread dependencies to a few Kconfigs

Co-authored-by: Damian Krolik <damian.krolik@nordicsemi.no>

* [zephyr] Lock Thread stack before factory reset

This fixes a problem with Thread activity interrupting factory
reset. This activity used to led to “Factory reset fail: -6”.
Writing to the cleared nvm flash pages caused the problem.

Signed-off-by: Patryk Lipinski <patryk.lipinski@nordicsemi.no>
Signed-off-by: Michał Szablowski <michal.szablowski@nordicsemi.no>

* [nrfconnect] Increase default number of packet buffers and stack size

This commit reverts the previous optimization of reducing the number
of packet buffers in the system. Additionally increase of stack size
is needed to accomodate LTO.

Signed-off-by: Łukasz Duda <lukasz.duda@nordicsemi.no>

* [nrfconnect] Select Experimental for persistent subscriptions

Currently, there is an issue with persistent subscriptions when
multiple controllers create subscriptions.

* [nrfconnect] Fixed mcuboot default configuration

Removed the defaults from Kconfig.mcuboot.defaults configuration
that should not be set there:
* BOOT_ENCRYPT_X - are configs without a prompt, so it should
not be modified outside of the mcuboot module
* SPI_NOR and NORDIC_QSPI_NOR - are set based on the device
tree configuration, so we should not set it, as it may lead
to configuration and dts mismatch.

Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no>

* Revert "[nrfconnect] Disable Zephyr socket extensions"

This reverts commit bb09061209748fe634a4b837f4db1db4f8b2d140.

---------

Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no>
Signed-off-by: Patryk Lipinski <patryk.lipinski@nordicsemi.no>
Signed-off-by: Michał Szablowski <michal.szablowski@nordicsemi.no>
Signed-off-by: Łukasz Duda <lukasz.duda@nordicsemi.no>
Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no>
Co-authored-by: Marcin Kajor <marcin.kajor@nordicsemi.no>
Co-authored-by: Damian Krolik <damian.krolik@nordicsemi.no>
Co-authored-by: Patryk Lipinski <patryk.lipinski@nordicsemi.no>
Co-authored-by: Łukasz Duda <lukasz.duda@nordicsemi.no>
Co-authored-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
diff --git a/config/nrfconnect/app/enable-gnu-std.cmake b/config/nrfconnect/app/enable-gnu-std.cmake
index 5f31e1a..4019bbd 100644
--- a/config/nrfconnect/app/enable-gnu-std.cmake
+++ b/config/nrfconnect/app/enable-gnu-std.cmake
@@ -1,6 +1,5 @@
 add_library(gnu17 INTERFACE)
 target_compile_options(gnu17
     INTERFACE 
-        $<$<COMPILE_LANGUAGE:CXX>:-std=gnu++17>
-        -D_SYS__PTHREADTYPES_H_)
+        $<$<COMPILE_LANGUAGE:CXX>:-std=gnu++17>)
 target_link_libraries(app PRIVATE gnu17)
diff --git a/config/nrfconnect/chip-module/Kconfig b/config/nrfconnect/chip-module/Kconfig
index 5d03e82..e3c5a86 100644
--- a/config/nrfconnect/chip-module/Kconfig
+++ b/config/nrfconnect/chip-module/Kconfig
@@ -302,4 +302,9 @@
 	  Perform factory reset of the device if the operational key for Fabric has not been migrated
 	  properly to PSA ITS storage.
 
+config CHIP_PERSISTENT_SUBSCRIPTIONS
+	default n
+	# selecting experimental for this feature since there is an issue with multiple controllers.
+	select EXPERIMENTAL
+
 endif # CHIP
diff --git a/config/nrfconnect/chip-module/Kconfig.defaults b/config/nrfconnect/chip-module/Kconfig.defaults
index 562755b..7e3c2a6 100644
--- a/config/nrfconnect/chip-module/Kconfig.defaults
+++ b/config/nrfconnect/chip-module/Kconfig.defaults
@@ -24,6 +24,10 @@
 # System configuration
 # ==============================================================================
 
+choice LIBC_IMPLEMENTATION
+    default NEWLIB_LIBC
+endchoice
+
 config ASSERT
     default y
 
@@ -318,13 +322,13 @@
 config PSA_USE_CC3XX_HASH_DRIVER
     default n
 
-endif
+endif # PSA_CRYPTO_DRIVER_CC3XX && PSA_CRYPTO_DRIVER_OBERON
 
 # Spake2+ support
 config MBEDTLS_MD_C
     default y
 
-endif
+endif # CHIP_CRYPTO_PSA
 
 if !CHIP_CRYPTO_PSA
 
@@ -367,7 +371,7 @@
 config MBEDTLS_ECP_DP_SECP256R1_ENABLED
     default y
 
-endif
+endif # !CHIP_CRYPTO_PSA
 
 config MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
     default n if CHIP_WIFI
@@ -487,4 +491,4 @@
 
 endif # SHELL
 
-endif
\ No newline at end of file
+endif # CHIP
diff --git a/config/nrfconnect/chip-module/Kconfig.features b/config/nrfconnect/chip-module/Kconfig.features
index 369f992..e6a4e52 100644
--- a/config/nrfconnect/chip-module/Kconfig.features
+++ b/config/nrfconnect/chip-module/Kconfig.features
@@ -42,15 +42,13 @@
 
 config CHIP_QSPI_NOR
 	bool "Enable QSPI NOR feature set"
+	imply NORDIC_QSPI_NOR
 	help
 	  Enables QSPI NOR flash with a set of options for configuring pages and
 	  buffer sizes.
 
 if CHIP_QSPI_NOR
 
-config NORDIC_QSPI_NOR
-	default y
-
 config NORDIC_QSPI_NOR_STACK_WRITE_BUFFER_SIZE
 	default 16
 
diff --git a/config/nrfconnect/chip-module/Kconfig.mcuboot.defaults b/config/nrfconnect/chip-module/Kconfig.mcuboot.defaults
index 31759be..224dc57 100644
--- a/config/nrfconnect/chip-module/Kconfig.mcuboot.defaults
+++ b/config/nrfconnect/chip-module/Kconfig.mcuboot.defaults
@@ -19,16 +19,7 @@
 config MAIN_STACK_SIZE
     default 10240
 
-config BOOT_SWAP_SAVE_ENCTLV
-    default n
-
-config BOOT_ENCRYPT_RSA
-    default n
-
-config BOOT_ENCRYPT_EC256
-    default n
-
-config BOOT_ENCRYPT_X25519
+config BOOT_ENCRYPT_IMAGE
     default n
 
 config BOOT_BOOTSTRAP
@@ -53,9 +44,6 @@
 config SPI
     default y
 
-config SPI_NOR
-    default y
-
 choice SPI_NOR_SFDP
 	default SPI_NOR_SFDP_DEVICETREE
 endchoice
@@ -74,9 +62,6 @@
 # All boards beside nRF7002DK use QSPI NOR external flash
 if BOARD_NRF5340DK_NRF5340_CPUAPP || BOARD_NRF52840DK_NRF52840
 
-config NORDIC_QSPI_NOR
-    default y
-
 config NORDIC_QSPI_NOR_FLASH_LAYOUT_PAGE_SIZE
     default 4096
 
diff --git a/config/zephyr/Kconfig b/config/zephyr/Kconfig
index bdfa709..c806cee 100644
--- a/config/zephyr/Kconfig
+++ b/config/zephyr/Kconfig
@@ -309,7 +309,8 @@
 
 config CHIP_ENABLE_DNSSD_SRP
 	bool "OpenThread Service Registration Protocol"
-	default y if NET_L2_OPENTHREAD
+	default y
+	depends on NET_L2_OPENTHREAD
 	imply OPENTHREAD_ECDSA
 	imply OPENTHREAD_SRP_CLIENT
 	help
@@ -318,7 +319,8 @@
 
 config CHIP_ENABLE_DNS_CLIENT
 	bool "OpenThread DNS client"
-	default y if NET_L2_OPENTHREAD
+	default y
+	depends on NET_L2_OPENTHREAD
 	imply OPENTHREAD_DNS_CLIENT
 	help
 	  Enables using the OpenThread DNS client for the Matter service discovery.
@@ -413,6 +415,7 @@
 
 config CHIP_OPENTHREAD_CONFIG
 	string "Custom OpenThread configuration file"
+	depends on NET_L2_OPENTHREAD
 	help
 	  Provides a path to an OpenThread configuration file. The path can be
 	  either absolute or relative to the application directory. When this option
diff --git a/src/platform/Zephyr/ConfigurationManagerImpl.cpp b/src/platform/Zephyr/ConfigurationManagerImpl.cpp
index f41eed0..2e937c9 100644
--- a/src/platform/Zephyr/ConfigurationManagerImpl.cpp
+++ b/src/platform/Zephyr/ConfigurationManagerImpl.cpp
@@ -40,6 +40,10 @@
 #include <zephyr/settings/settings.h>
 #endif
 
+#ifdef CONFIG_NET_L2_OPENTHREAD
+#include <platform/ThreadStackManager.h>
+#endif
+
 namespace chip {
 namespace DeviceLayer {
 
@@ -179,6 +183,11 @@
 {
     ChipLogProgress(DeviceLayer, "Performing factory reset");
 
+// Lock the Thread stack to avoid unwanted interaction with settings NVS during factory reset.
+#ifdef CONFIG_NET_L2_OPENTHREAD
+    ThreadStackMgr().LockThreadStack();
+#endif
+
 #ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS
     void * storage = nullptr;
     int status     = settings_storage_get(&storage);
diff --git a/src/platform/nrfconnect/CHIPDevicePlatformConfig.h b/src/platform/nrfconnect/CHIPDevicePlatformConfig.h
index f9e445f..2e3c932 100644
--- a/src/platform/nrfconnect/CHIPDevicePlatformConfig.h
+++ b/src/platform/nrfconnect/CHIPDevicePlatformConfig.h
@@ -161,7 +161,11 @@
 #endif // CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY
 
 #ifndef CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE
+#if defined(CONFIG_LTO)
+#define CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE 10240
+#else
 #define CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE 6144
+#endif // CONFIG_LTO
 #endif // CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE
 
 #define CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE 64
diff --git a/src/platform/nrfconnect/SystemPlatformConfig.h b/src/platform/nrfconnect/SystemPlatformConfig.h
index 60d9c35..fb54446 100644
--- a/src/platform/nrfconnect/SystemPlatformConfig.h
+++ b/src/platform/nrfconnect/SystemPlatformConfig.h
@@ -48,7 +48,7 @@
 #define CHIP_SYSTEM_CONFIG_USE_SOCKETS 1
 
 #ifndef CONFIG_ARCH_POSIX
-#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 8
+#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 15
 #endif
 
 #ifndef CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX