Skip to content

Commit

Permalink
Implement DeviceNetworkProvisioningDelegate API for WiFi on Linux pla…
Browse files Browse the repository at this point in the history
…tform (#3595)
  • Loading branch information
yufengwangca authored and pull[bot] committed Nov 13, 2020
1 parent 5852f6c commit 2580594
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
"Linux/ConfigurationManagerImpl.h",
"Linux/ConnectivityManagerImpl.cpp",
"Linux/ConnectivityManagerImpl.h",
"Linux/DeviceNetworkProvisioningDelegateImpl.cpp",
"Linux/DeviceNetworkProvisioningDelegateImpl.h",
"Linux/InetPlatformConfig.h",
"Linux/Logging.cpp",
Expand Down
69 changes: 69 additions & 0 deletions src/platform/Linux/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,74 @@ void ConnectivityManagerImpl::_OnWpaProxyReady(GObject * source_object, GAsyncRe
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WPA

CHIP_ERROR ConnectivityManagerImpl::ProvisionWiFiNetwork(const char * ssid, const char * key)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WPA
CHIP_ERROR ret = CHIP_NO_ERROR;
GError * err = nullptr;
GVariant * args = nullptr;
GVariantBuilder builder;

// Clean up current network if exists
if (mWpaSupplicant.networkPath)
{
g_object_unref(mWpaSupplicant.networkPath);
mWpaSupplicant.networkPath = nullptr;
}

g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&builder, "{sv}", "ssid", g_variant_new_string(ssid));
g_variant_builder_add(&builder, "{sv}", "psk", g_variant_new_string(key));
g_variant_builder_add(&builder, "{sv}", "key_mgmt", g_variant_new_string("WPA-PSK"));
args = g_variant_builder_end(&builder);

gboolean result = wpa_fi_w1_wpa_supplicant1_interface_call_add_network_sync(mWpaSupplicant.iface, args,
&mWpaSupplicant.networkPath, nullptr, &err);

if (result)
{
GError * error = nullptr;

ChipLogProgress(DeviceLayer, "wpa_supplicant: added network: SSID: %s: %s", ssid, mWpaSupplicant.networkPath);

result = wpa_fi_w1_wpa_supplicant1_interface_call_select_network_sync(mWpaSupplicant.iface, mWpaSupplicant.networkPath,
nullptr, &error);
if (result)
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: connected to network: SSID: %s", ssid);
}
else
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: failed to connect to network: SSID: %s: %s", ssid,
error ? error->message : "unknown error");
}

if (error != nullptr)
g_error_free(error);

ret = CHIP_NO_ERROR;
}
else
{
ChipLogProgress(DeviceLayer, "wpa_supplicant: failed to add network: %s: %s", ssid, err ? err->message : "unknown error");

if (mWpaSupplicant.networkPath)
{
g_object_unref(mWpaSupplicant.networkPath);
mWpaSupplicant.networkPath = nullptr;
}

ret = CHIP_ERROR_INTERNAL;
}

if (err != nullptr)
g_error_free(err);

return ret;
#else
return CHIP_ERROR_NOT_IMPLEMENTED;
#endif
}

} // namespace DeviceLayer
} // namespace chip
3 changes: 3 additions & 0 deletions src/platform/Linux/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
// the implementation methods provided by this class.
friend class ConnectivityManager;

public:
CHIP_ERROR ProvisionWiFiNetwork(const char * ssid, const char * key);

private:
// ===== Members that implement the ConnectivityManager abstract interface.

Expand Down
43 changes: 43 additions & 0 deletions src/platform/Linux/DeviceNetworkProvisioningDelegateImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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.
*/

#include <support/ErrorStr.h>
#include <support/logging/CHIPLogging.h>

#include "DeviceNetworkProvisioningDelegateImpl.h"

namespace chip {
namespace DeviceLayer {

CHIP_ERROR DeviceNetworkProvisioningDelegateImpl::_ProvisionWiFiNetwork(const char * ssid, const char * key)
{
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogProgress(NetworkProvisioning, "LinuxNetworkProvisioningDelegate: SSID: %s", ssid);

err = ConnectivityMgrImpl().ProvisionWiFiNetwork(ssid, key);

if (err != CHIP_NO_ERROR)
{
ChipLogError(NetworkProvisioning, "Failed to connect to WiFi network: %s", chip::ErrorStr(err));
}

return err;
}

} // namespace DeviceLayer
} // namespace chip
2 changes: 1 addition & 1 deletion src/platform/Linux/DeviceNetworkProvisioningDelegateImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DeviceNetworkProvisioningDelegateImpl final
friend class GenericDeviceNetworkProvisioningDelegateImpl<DeviceNetworkProvisioningDelegateImpl>;

private:
CHIP_ERROR _ProvisionWiFiNetwork(const char * ssid, const char * passwd) { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR _ProvisionWiFiNetwork(const char * ssid, const char * passwd);
CHIP_ERROR _ProvisionThreadNetwork(DeviceLayer::Internal::DeviceNetworkInfo & threadData) { return CHIP_ERROR_NOT_IMPLEMENTED; }
};

Expand Down

0 comments on commit 2580594

Please sign in to comment.