Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / ESP32 / ConfigurationManagerImpl.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2018 Nest Labs, Inc.
5  *    All rights reserved.
6  *
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 /**
21  *    @file
22  *          Provides the implementation of the Device Layer ConfigurationManager object
23  *          for the ESP32.
24  */
25 /* this file behaves like a config.h, comes first */
26 #include <platform/internal/CHIPDeviceLayerInternal.h>
27
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>
33
34 #include "esp_wifi.h"
35 #include "nvs.h"
36 #include "nvs_flash.h"
37 namespace chip {
38 namespace DeviceLayer {
39
40 using namespace ::chip::DeviceLayer::Internal;
41
42 namespace {
43
44 enum
45 {
46     kChipProduct_Connect = 0x0016
47 };
48
49 } // unnamed namespace
50
51 // TODO: Define a Singleton instance of CHIP Group Key Store here (#1266)
52
53 /** Singleton instance of the ConfigurationManager implementation object for the ESP32.
54  */
55 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
56
57 CHIP_ERROR ConfigurationManagerImpl::_Init()
58 {
59     CHIP_ERROR err;
60     bool failSafeArmed;
61
62     // Force initialization of NVS namespaces if they doesn't already exist.
63     err = EnsureNamespace(kConfigNamespace_ChipFactory);
64     SuccessOrExit(err);
65     err = EnsureNamespace(kConfigNamespace_ChipConfig);
66     SuccessOrExit(err);
67     err = EnsureNamespace(kConfigNamespace_ChipCounters);
68     SuccessOrExit(err);
69
70     // Initialize the generic implementation base class.
71     err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::_Init();
72     SuccessOrExit(err);
73
74     // TODO: Initialize the global GroupKeyStore object here (#1266)
75
76 #if CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
77
78     {
79         FactoryProvisioning factoryProv;
80         uint8_t * const kInternalSRAM12Start = (uint8_t *) 0x3FFAE000;
81         uint8_t * const kInternalSRAM12End   = kInternalSRAM12Start + (328 * 1024) - 1;
82
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);
86         SuccessOrExit(err);
87     }
88
89 #endif // CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
90
91     // If the fail-safe was armed when the device last shutdown, initiate a factory reset.
92     if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
93     {
94         ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
95         _InitiateFactoryReset();
96     }
97     err = CHIP_NO_ERROR;
98
99 exit:
100     return err;
101 }
102
103 CHIP_ERROR ConfigurationManagerImpl::_GetPrimaryWiFiMACAddress(uint8_t * buf)
104 {
105     return esp_wifi_get_mac(WIFI_IF_STA, buf);
106 }
107
108 bool ConfigurationManagerImpl::_CanFactoryReset()
109 {
110     // TODO: query the application to determine if factory reset is allowed.
111     return true;
112 }
113
114 void ConfigurationManagerImpl::_InitiateFactoryReset()
115 {
116     PlatformMgr().ScheduleWork(DoFactoryReset);
117 }
118
119 CHIP_ERROR ConfigurationManagerImpl::_ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
120 {
121     ESP32Config::Key configKey{ kConfigNamespace_ChipCounters, key };
122
123     CHIP_ERROR err = ReadConfigValue(configKey, value);
124     if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
125     {
126         err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
127     }
128     return err;
129 }
130
131 CHIP_ERROR ConfigurationManagerImpl::_WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
132 {
133     ESP32Config::Key configKey{ kConfigNamespace_ChipCounters, key };
134     return WriteConfigValue(configKey, value);
135 }
136
137 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
138 {
139     CHIP_ERROR err;
140
141     ChipLogProgress(DeviceLayer, "Performing factory reset");
142
143     // Erase all values in the chip-config NVS namespace.
144     err = ClearNamespace(kConfigNamespace_ChipConfig);
145     if (err != CHIP_NO_ERROR)
146     {
147         ChipLogError(DeviceLayer, "ClearNamespace(ChipConfig) failed: %s", chip::ErrorStr(err));
148     }
149
150     // Restore WiFi persistent settings to default values.
151     err = esp_wifi_restore();
152     if (err != ESP_OK)
153     {
154         ChipLogError(DeviceLayer, "esp_wifi_restore() failed: %s", chip::ErrorStr(err));
155     }
156
157     // Restart the system.
158     ChipLogProgress(DeviceLayer, "System restarting");
159     esp_restart();
160 }
161
162 } // namespace DeviceLayer
163 } // namespace chip