| /* |
| * |
| * Copyright (c) 2020 Project CHIP Authors |
| * Copyright (c) 2019 Nest Labs, Inc. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| /** |
| * @file |
| * Provides the implementation of the Device Layer ConfigurationManager object |
| * for mbed platforms. |
| */ |
| /* this file behaves like a config.h, comes first */ |
| |
| // FIXME: Undefine the `sleep()` function included by the CHIPDeviceLayer.h |
| // from unistd.h to avoid a conflicting declaration with the `sleep()` provided |
| // by Mbed-OS in mbed_power_mgmt.h. |
| #define sleep unistd_sleep |
| #include <platform/internal/CHIPDeviceLayerInternal.h> |
| #undef sleep |
| |
| #include <platform/internal/GenericConfigurationManagerImpl.ipp> |
| |
| #include <platform/ConfigurationManager.h> |
| |
| // mbed-os headers |
| #include "platform/mbed_power_mgmt.h" |
| #include <net_common.h> |
| |
| namespace chip { |
| namespace DeviceLayer { |
| |
| using namespace ::chip::DeviceLayer::Internal; |
| |
| ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() |
| { |
| static ConfigurationManagerImpl sInstance; |
| return sInstance; |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::Init() |
| { |
| CHIP_ERROR err; |
| uint32_t rebootCount; |
| |
| if (MbedConfig::ConfigValueExists(MbedConfig::kCounterKey_RebootCount)) |
| { |
| err = GetRebootCount(rebootCount); |
| SuccessOrExit(err); |
| |
| err = StoreRebootCount(rebootCount + 1); |
| SuccessOrExit(err); |
| } |
| else |
| { |
| // The first boot after factory reset of the Node. |
| err = StoreRebootCount(1); |
| SuccessOrExit(err); |
| } |
| |
| if (!MbedConfig::ConfigValueExists(MbedConfig::kCounterKey_TotalOperationalHours)) |
| { |
| err = StoreTotalOperationalHours(0); |
| SuccessOrExit(err); |
| } |
| |
| // Initialize the generic implementation base class. |
| err = Internal::GenericConfigurationManagerImpl<MbedConfig>::Init(); |
| SuccessOrExit(err); |
| |
| exit: |
| return err; |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::GetRebootCount(uint32_t & rebootCount) |
| { |
| return ReadConfigValue(MbedConfig::kCounterKey_RebootCount, rebootCount); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::StoreRebootCount(uint32_t rebootCount) |
| { |
| return WriteConfigValue(MbedConfig::kCounterKey_RebootCount, rebootCount); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours) |
| { |
| return ReadConfigValue(MbedConfig::kCounterKey_TotalOperationalHours, totalOperationalHours); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::StoreTotalOperationalHours(uint32_t totalOperationalHours) |
| { |
| return WriteConfigValue(MbedConfig::kCounterKey_TotalOperationalHours, totalOperationalHours); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf) |
| { |
| auto net_if = get_mbed_net_if(); |
| if (net_if == nullptr || net_if->wifiInterface() == nullptr) |
| { |
| ChipLogError(DeviceLayer, "Failed to extract the MAC address: WiFi interface not available"); |
| return CHIP_ERROR_INTERNAL; |
| } |
| |
| auto * mac_address = net_if->wifiInterface()->get_mac_address(); |
| if (mac_address) |
| { |
| int last = -1; |
| int rc = |
| sscanf(mac_address, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n", buf + 5, buf + 4, buf + 3, buf + 2, buf + 1, buf + 0, &last); |
| if (rc != NSAPI_MAC_BYTES || last != (NSAPI_MAC_SIZE - 1)) |
| { |
| ChipLogError(DeviceLayer, "Failed to extract the MAC address: %s, rc = %d, last = %d", mac_address, rc, last); |
| return CHIP_ERROR_INTERNAL; |
| } |
| else |
| { |
| ChipLogError(DeviceLayer, "Extract the MAC address: %s", mac_address); |
| return CHIP_NO_ERROR; |
| } |
| } |
| else |
| { |
| ChipLogError(DeviceLayer, "Failed to extract the MAC address: nothing returned by the interface"); |
| return CHIP_ERROR_INTERNAL; |
| } |
| } |
| |
| bool ConfigurationManagerImpl::CanFactoryReset() |
| { |
| return true; |
| } |
| |
| void ConfigurationManagerImpl::InitiateFactoryReset(void) |
| { |
| PlatformMgr().ScheduleWork(DoFactoryReset); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value) |
| { |
| CHIP_ERROR err = ReadConfigValue(key, value); |
| if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND) |
| { |
| err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND; |
| } |
| return err; |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) |
| { |
| return MbedConfig::WriteCounter(key, value); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, bool & val) |
| { |
| return MbedConfig::ReadConfigValue(key, val); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint32_t & val) |
| { |
| return MbedConfig::ReadConfigValue(key, val); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint64_t & val) |
| { |
| return MbedConfig::ReadConfigValue(key, val); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen) |
| { |
| return MbedConfig::ReadConfigValueStr(key, buf, bufSize, outLen); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen) |
| { |
| return MbedConfig::ReadConfigValueBin(key, buf, bufSize, outLen); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, bool val) |
| { |
| return MbedConfig::WriteConfigValue(key, val); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint32_t val) |
| { |
| return MbedConfig::WriteConfigValue(key, val); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint64_t val) |
| { |
| return MbedConfig::WriteConfigValue(key, val); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueStr(Key key, const char * str) |
| { |
| return MbedConfig::WriteConfigValueStr(key, str); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueStr(Key key, const char * str, size_t strLen) |
| { |
| return MbedConfig::WriteConfigValueStr(key, str, strLen); |
| } |
| |
| CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) |
| { |
| return MbedConfig::WriteConfigValueBin(key, data, dataLen); |
| } |
| |
| void ConfigurationManagerImpl::RunConfigUnitTest(void) |
| { |
| MbedConfig::RunConfigUnitTest(); |
| } |
| |
| void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg) |
| { |
| ChipLogProgress(DeviceLayer, "Performing factory reset"); |
| const CHIP_ERROR err = MbedConfig::FactoryResetConfig(); |
| if (err != CHIP_NO_ERROR) |
| { |
| ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err)); |
| } |
| |
| // Restart the system. |
| ChipLogProgress(DeviceLayer, "System restarting"); |
| system_reset(); |
| } |
| |
| ConfigurationManager & ConfigurationMgrImpl() |
| { |
| return ConfigurationManagerImpl::GetDefaultInstance(); |
| } |
| |
| } // namespace DeviceLayer |
| } // namespace chip |