Skip to content

Commit

Permalink
Support persistent storage of WiFi Credentials for Infineon P6 (proje…
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenCY authored and step0035 committed Feb 8, 2022
1 parent 84d5803 commit 80c47e5
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 66 deletions.
7 changes: 3 additions & 4 deletions examples/lighting-app/p6/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ void AppTask::AppTaskMain(void * pvParameter)

while (true)
{
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10));
while (eventReceived == pdTRUE)
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, portMAX_DELAY);
if (eventReceived == pdTRUE)
{
sAppTask.DispatchEvent(&event);
eventReceived = xQueueReceive(sAppEventQueue, &event, 0);
}

// Collect connectivity and configuration state from the CHIP stack. Because
Expand Down Expand Up @@ -335,7 +334,7 @@ void AppTask::FunctionHandler(AppEvent * aEvent)
// FACTORY_RESET_TRIGGER_TIMEOUT to signal factory reset has been initiated.
// To cancel factory reset: release the APP_FUNCTION_BUTTON once all LEDs
// start blinking within the FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (aEvent->ButtonEvent.Action == APP_BUTTON_PRESSED)
if (aEvent->ButtonEvent.Action == APP_BUTTON_RELEASED)
{
if (!sAppTask.mFunctionTimerActive && sAppTask.mFunction == Function::kNoneSelected)
{
Expand Down
7 changes: 3 additions & 4 deletions examples/lock-app/p6/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,10 @@ void AppTask::AppTaskMain(void * pvParameter)

while (true)
{
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10));
while (eventReceived == pdTRUE)
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, portMAX_DELAY);
if (eventReceived == pdTRUE)
{
sAppTask.DispatchEvent(&event);
eventReceived = xQueueReceive(sAppEventQueue, &event, 0);
}
// Collect connectivity and configuration state from the CHIP stack. Because
// the CHIP event loop is being run in a separate task, the stack must be
Expand Down Expand Up @@ -334,7 +333,7 @@ void AppTask::FunctionHandler(AppEvent * event)
// FACTORY_RESET_TRIGGER_TIMEOUT to signal factory reset has been initiated.
// To cancel factory reset: release the APP_FUNCTION_BUTTON once all LEDs
// start blinking within the FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (event->ButtonEvent.Action == APP_BUTTON_PRESSED)
if (event->ButtonEvent.Action == APP_BUTTON_RELEASED)
{
if (!sAppTask.mFunctionTimerActive && sAppTask.mFunction == Function::kNoneSelected)
{
Expand Down
30 changes: 14 additions & 16 deletions src/platform/P6/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ ConnectivityManagerImpl ConnectivityManagerImpl::sInstance;

ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMode(void)
{
uint32_t curWiFiMode;
mWiFiStationMode = (Internal::P6Utils::wifi_get_mode(curWiFiMode) == CHIP_NO_ERROR &&
(curWiFiMode == WIFI_MODE_APSTA || curWiFiMode == WIFI_MODE_STA))
? kWiFiStationMode_Enabled
: kWiFiStationMode_Disabled;
return mWiFiStationMode;
}

Expand Down Expand Up @@ -181,7 +186,6 @@ CHIP_ERROR ConnectivityManagerImpl::_GetAndLogWiFiStatsCounters(void)
CHIP_ERROR ConnectivityManagerImpl::_Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
cy_rslt_t result = CY_RSLT_SUCCESS;
mLastStationConnectFailTime = System::Clock::kZero;
mLastAPDemandTime = System::Clock::kZero;
mWiFiStationMode = kWiFiStationMode_Disabled;
Expand All @@ -208,13 +212,8 @@ CHIP_ERROR ConnectivityManagerImpl::_Init()
memcpy(wifiConfig.sta.password, CHIP_DEVICE_CONFIG_DEFAULT_STA_PASSWORD,
min(strlen(CHIP_DEVICE_CONFIG_DEFAULT_STA_PASSWORD), sizeof(wifiConfig.sta.password)));
wifiConfig.sta.security = CHIP_DEVICE_CONFIG_DEFAULT_STA_SECURITY;
result = Internal::P6Utils::p6_wifi_set_config(WIFI_IF_STA, &wifiConfig);
if (result != CY_RSLT_SUCCESS)
{
ChipLogError(DeviceLayer, "p6_wifi_set_config() failed: %d", (int) result);
SuccessOrExit(CHIP_ERROR_INTERNAL);
}
err = CHIP_NO_ERROR;
err = Internal::P6Utils::p6_wifi_set_config(WIFI_IF_STA, &wifiConfig);
SuccessOrExit(err);
}
// Force AP mode off for now.
err = Internal::P6Utils::SetAPMode(false);
Expand Down Expand Up @@ -323,7 +322,6 @@ CHIP_ERROR ConnectivityManagerImpl::ConfigureWiFiAP()
{
CHIP_ERROR err = CHIP_NO_ERROR;
wifi_config_t wifiConfig;
cy_rslt_t result = CY_RSLT_SUCCESS;

memset(&wifiConfig.ap, 0, sizeof(wifi_config_ap_t));
snprintf((char *) wifiConfig.ap.ssid, sizeof(wifiConfig.ap.ssid), "%s-%04X-%04X", CHIP_DEVICE_CONFIG_WIFI_AP_SSID_PREFIX,
Expand All @@ -336,13 +334,8 @@ CHIP_ERROR ConnectivityManagerImpl::ConfigureWiFiAP()
wifiConfig.ap.ip_settings.gateway = ap_mode_ip_settings.gateway;

ChipLogProgress(DeviceLayer, "Configuring WiFi AP: SSID %s, channel %u", wifiConfig.ap.ssid, wifiConfig.ap.channel);
result = Internal::P6Utils::p6_wifi_set_config(WIFI_IF_AP, &wifiConfig);
if (result != CY_RSLT_SUCCESS)
{
ChipLogError(DeviceLayer, "p6_wifi_set_config(WIFI_IF_AP) failed: %d", (int) result);
err = CHIP_ERROR_INTERNAL;
SuccessOrExit(err);
}
err = Internal::P6Utils::p6_wifi_set_config(WIFI_IF_AP, &wifiConfig);
SuccessOrExit(err);

err = Internal::P6Utils::p6_start_ap();
if (err != CHIP_NO_ERROR)
Expand Down Expand Up @@ -478,6 +471,9 @@ void ConnectivityManagerImpl::DriveStationState()
CHIP_ERROR err = CHIP_NO_ERROR;
bool stationConnected;

// Refresh the current station mode by reading the configuration from storage.
GetWiFiStationMode();

// If the station interface is NOT under application control...
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
{
Expand Down Expand Up @@ -609,6 +605,7 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
event.Type = DeviceEventType::kInterfaceIpAddressChanged;
event.InterfaceIpAddressChanged.Type = InterfaceIpChangeType::kIpV4_Assigned;
PlatformMgr().PostEventOrDie(&event);
ChipLogProgress(DeviceLayer, "IPv4 Address Assigned : %s", ip4addr_ntoa(netif_ip4_addr(net_interface)));
}
// Search among the IPv6 addresses assigned to the interface for a Global Unicast
// address (2000::/3) that is in the valid state. If such an address is found...
Expand All @@ -622,6 +619,7 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
event.Type = DeviceEventType::kInterfaceIpAddressChanged;
event.InterfaceIpAddressChanged.Type = InterfaceIpChangeType::kIpV6_Assigned;
PlatformMgr().PostEventOrDie(&event);
ChipLogProgress(DeviceLayer, "IPv6 Address Assigned : %s", ip6addr_ntoa(netif_ip6_addr(net_interface, i)));
}
}
}
Expand Down
11 changes: 3 additions & 8 deletions src/platform/P6/DeviceNetworkProvisioningDelegateImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace DeviceLayer {
CHIP_ERROR DeviceNetworkProvisioningDelegateImpl::_ProvisionWiFiNetwork(const char * ssid, const char * passwd)
{
CHIP_ERROR err = CHIP_NO_ERROR;
cy_rslt_t rslt = CY_RSLT_SUCCESS;

ChipLogProgress(NetworkProvisioning, "P6NetworkProvisioningDelegate: SSID: %s", ssid);
err = ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled);
Expand All @@ -36,14 +35,10 @@ CHIP_ERROR DeviceNetworkProvisioningDelegateImpl::_ProvisionWiFiNetwork(const ch
// Set the wifi configuration
wifi_config_t wifi_config;
Internal::P6Utils::populate_wifi_config_t(&wifi_config, WIFI_IF_STA, (const cy_wcm_ssid_t *) ssid,
(const cy_wcm_passphrase_t *) passwd, CHIP_DEVICE_CONFIG_DEFAULT_STA_SECURITY);
(const cy_wcm_passphrase_t *) passwd,
(strlen(passwd)) ? CHIP_DEVICE_CONFIG_DEFAULT_STA_SECURITY : CY_WCM_SECURITY_OPEN);

rslt = Internal::P6Utils::p6_wifi_set_config(WIFI_IF_STA, &wifi_config);
if (rslt != CY_RSLT_SUCCESS)
{
err = CHIP_ERROR_INTERNAL;
ChipLogError(DeviceLayer, "p6_wifi_set_config() failed");
}
err = Internal::P6Utils::p6_wifi_set_config(WIFI_IF_STA, &wifi_config);
SuccessOrExit(err);

err = ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Enabled);
Expand Down
11 changes: 8 additions & 3 deletions src/platform/P6/P6Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ const P6Config::Key P6Config::kConfigKey_ActiveLocale = { kConfigNamespace
const P6Config::Key P6Config::kConfigKey_Breadcrumb = { kConfigNamespace_ChipConfig, "breadcrumb" };
const P6Config::Key P6Config::kConfigKey_HourFormat = { kConfigNamespace_ChipConfig, "hour-format" };
const P6Config::Key P6Config::kConfigKey_CalendarType = { kConfigNamespace_ChipConfig, "calendar-type" };
const P6Config::Key P6Config::kConfigKey_WiFiSSID = { kConfigNamespace_ChipConfig, "wifi-ssid" };
const P6Config::Key P6Config::kConfigKey_WiFiPassword = { kConfigNamespace_ChipConfig, "wifi-password" };
const P6Config::Key P6Config::kConfigKey_WiFiSecurity = { kConfigNamespace_ChipConfig, "wifi-security" };
const P6Config::Key P6Config::kConfigKey_WiFiMode = { kConfigNamespace_ChipConfig, "wifimode" };

// Keys stored in the Chip-counters namespace
const P6Config::Key P6Config::kCounterKey_RebootCount = { kConfigNamespace_ChipCounters, "reboot-count" };
Expand Down Expand Up @@ -212,9 +216,10 @@ bool P6Config::ConfigValueExists(Key key)
// Clear out keys in config namespace
CHIP_ERROR P6Config::FactoryResetConfig(void)
{
const Key * config_keys[] = { &kConfigKey_FabricId, &kConfigKey_ServiceConfig, &kConfigKey_PairedAccountId,
&kConfigKey_ServiceId, &kConfigKey_GroupKeyIndex, &kConfigKey_LastUsedEpochKeyId,
&kConfigKey_FailSafeArmed, &kConfigKey_WiFiStationSecType };
const Key * config_keys[] = { &kConfigKey_FabricId, &kConfigKey_ServiceConfig, &kConfigKey_PairedAccountId,
&kConfigKey_ServiceId, &kConfigKey_GroupKeyIndex, &kConfigKey_LastUsedEpochKeyId,
&kConfigKey_FailSafeArmed, &kConfigKey_WiFiStationSecType, &kConfigKey_WiFiSSID,
&kConfigKey_WiFiPassword, &kConfigKey_WiFiSecurity, &kConfigKey_WiFiMode };

for (uint32_t i = 0; i < (sizeof(config_keys) / sizeof(config_keys[0])); i++)
{
Expand Down
4 changes: 4 additions & 0 deletions src/platform/P6/P6Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class P6Config
static const Key kConfigKey_Breadcrumb;
static const Key kConfigKey_HourFormat;
static const Key kConfigKey_CalendarType;
static const Key kConfigKey_WiFiSSID;
static const Key kConfigKey_WiFiPassword;
static const Key kConfigKey_WiFiSecurity;
static const Key kConfigKey_WiFiMode;

// CHIP Counter keys
static const Key kCounterKey_RebootCount;
Expand Down
Loading

0 comments on commit 80c47e5

Please sign in to comment.