3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2018 Nest Labs, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 * Provides the implementation of the Device Layer ConfigurationManager object
25 /* this file behaves like a config.h, comes first */
26 #include <platform/internal/CHIPDeviceLayerInternal.h>
28 #include <core/CHIPKeyIds.h>
29 #include <platform/ConfigurationManager.h>
30 #include <platform/ESP32/ESP32Config.h>
31 #include <platform/internal/GenericConfigurationManagerImpl.cpp>
32 #include <support/CodeUtils.h>
36 #include "nvs_flash.h"
38 namespace DeviceLayer {
40 using namespace ::chip::DeviceLayer::Internal;
46 kChipProduct_Connect = 0x0016
49 } // unnamed namespace
51 // TODO: Define a Singleton instance of CHIP Group Key Store here (#1266)
53 /** Singleton instance of the ConfigurationManager implementation object for the ESP32.
55 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
57 CHIP_ERROR ConfigurationManagerImpl::_Init()
62 // Force initialization of NVS namespaces if they doesn't already exist.
63 err = EnsureNamespace(kConfigNamespace_ChipFactory);
65 err = EnsureNamespace(kConfigNamespace_ChipConfig);
67 err = EnsureNamespace(kConfigNamespace_ChipCounters);
70 // Initialize the generic implementation base class.
71 err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::_Init();
74 // TODO: Initialize the global GroupKeyStore object here (#1266)
76 #if CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
79 FactoryProvisioning factoryProv;
80 uint8_t * const kInternalSRAM12Start = (uint8_t *) 0x3FFAE000;
81 uint8_t * const kInternalSRAM12End = kInternalSRAM12Start + (328 * 1024) - 1;
83 // Scan ESP32 Internal SRAM regions 1 and 2 for injected provisioning data and save
84 // to persistent storage if found.
85 err = factoryProv.ProvisionDeviceFromRAM(kInternalSRAM12Start, kInternalSRAM12End);
89 #endif // CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
91 // If the fail-safe was armed when the device last shutdown, initiate a factory reset.
92 if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
94 ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
95 _InitiateFactoryReset();
103 CHIP_ERROR ConfigurationManagerImpl::_GetPrimaryWiFiMACAddress(uint8_t * buf)
105 return esp_wifi_get_mac(WIFI_IF_STA, buf);
108 bool ConfigurationManagerImpl::_CanFactoryReset()
110 // TODO: query the application to determine if factory reset is allowed.
114 void ConfigurationManagerImpl::_InitiateFactoryReset()
116 PlatformMgr().ScheduleWork(DoFactoryReset);
119 CHIP_ERROR ConfigurationManagerImpl::_ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
121 ESP32Config::Key configKey{ kConfigNamespace_ChipCounters, key };
123 CHIP_ERROR err = ReadConfigValue(configKey, value);
124 if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
126 err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
131 CHIP_ERROR ConfigurationManagerImpl::_WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
133 ESP32Config::Key configKey{ kConfigNamespace_ChipCounters, key };
134 return WriteConfigValue(configKey, value);
137 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
141 ChipLogProgress(DeviceLayer, "Performing factory reset");
143 // Erase all values in the chip-config NVS namespace.
144 err = ClearNamespace(kConfigNamespace_ChipConfig);
145 if (err != CHIP_NO_ERROR)
147 ChipLogError(DeviceLayer, "ClearNamespace(ChipConfig) failed: %s", chip::ErrorStr(err));
150 // Restore WiFi persistent settings to default values.
151 err = esp_wifi_restore();
154 ChipLogError(DeviceLayer, "esp_wifi_restore() failed: %s", chip::ErrorStr(err));
157 // Restart the system.
158 ChipLogProgress(DeviceLayer, "System restarting");
162 } // namespace DeviceLayer