Skip to content

Commit

Permalink
[Ameba] Update ConnectivityManagerImpl and ConfigurationManagerImpl (#…
Browse files Browse the repository at this point in the history
…12076)

* Add Wifi and General diagnostics

* Do not show QRcode url if wifi connected

* Correct usage of ConfigValueExists
  • Loading branch information
pankore authored and pull[bot] committed Mar 25, 2023
1 parent 549fd00 commit 46586da
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 21 deletions.
13 changes: 7 additions & 6 deletions examples/all-clusters-app/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ std::string createSetupPayload()
ChipLogError(DeviceLayer, "Couldn't get discriminator: %s\r\n", ErrorStr(err));
return result;
}
ChipLogProgress(DeviceLayer, "Setup discriminator: %u (0x%x)\r\n", discriminator, discriminator);
ChipLogProgress(DeviceLayer, "Setup discriminator: %d (0x%x)\r\n", discriminator, discriminator);

uint32_t setupPINCode;
err = ConfigurationMgr().GetSetupPinCode(setupPINCode);
Expand All @@ -107,7 +107,7 @@ std::string createSetupPayload()
ChipLogError(DeviceLayer, "Couldn't get setupPINCode: %s\r\n", ErrorStr(err));
return result;
}
ChipLogProgress(DeviceLayer, "Setup PIN code: %u (0x%x)\r\n", setupPINCode, setupPINCode);
ChipLogProgress(DeviceLayer, "Setup PIN code: %lu (0x%lx)\r\n", setupPINCode, setupPINCode);

uint16_t vendorId;
err = ConfigurationMgr().GetVendorId(vendorId);
Expand Down Expand Up @@ -180,7 +180,7 @@ std::string createSetupPayload()

if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Couldn't get payload string %\r\n" CHIP_ERROR_FORMAT, err.Format());
ChipLogError(DeviceLayer, "Couldn't get payload string %lu\r\n" CHIP_ERROR_FORMAT, err.Format());
}
return result;
};
Expand Down Expand Up @@ -209,10 +209,11 @@ extern "C" void ChipTest(void)
// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());

std::string qrCodeText = createSetupPayload();
ChipLogProgress(DeviceLayer, "QR CODE Text: '%s'\r\n", qrCodeText.c_str());

if (RTW_SUCCESS != wifi_is_connected_to_ap())
{
std::string qrCodeText = createSetupPayload();
ChipLogProgress(DeviceLayer, "QR CODE Text: '%s'\r\n", qrCodeText.c_str());

std::vector<char> qrCode(3 * qrCodeText.size() + 1);
err = EncodeQRCodeToUrl(qrCodeText.c_str(), qrCodeText.size(), qrCode.data(), qrCode.max_size());
if (err == CHIP_NO_ERROR)
Expand Down
12 changes: 1 addition & 11 deletions src/platform/Ameba/AmebaConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,7 @@ CHIP_ERROR AmebaConfig::ClearConfigValue(Key key)

bool AmebaConfig::ConfigValueExists(Key key)
{
int32_t exist;
CHIP_ERROR err;

exist = checkExist(key.Namespace, key.Name);

if (exist == 1)
err = CHIP_NO_ERROR;
else
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;

return (err == CHIP_NO_ERROR);
return checkExist(key.Namespace, key.Name);
}

CHIP_ERROR AmebaConfig::EnsureNamespace(const char * ns)
Expand Down
10 changes: 9 additions & 1 deletion src/platform/Ameba/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
int i = 0;

wifi_get_mac_address(temp);
sscanf(temp, "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);

char * token = strtok(temp, ":");
while (token != NULL)
{
mac[i] = (uint32_t) strtol(token, NULL, 16);
token = strtok(NULL, ":");
i++;
}

for (i = 0; i < ETH_ALEN; i++)
buf[i] = mac[i] & 0xFF;

Expand Down
170 changes: 167 additions & 3 deletions src/platform/Ameba/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <platform/Ameba/DiagnosticDataProviderImpl.h>
#include <platform/DiagnosticDataProvider.h>

#include <lwip_netconf.h>

namespace chip {
namespace DeviceLayer {

Expand Down Expand Up @@ -116,22 +118,184 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason)
return err;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** netifpp)
{
CHIP_ERROR err = CHIP_ERROR_READ_FAILED;
NetworkInterface * head = NULL;
struct ifaddrs * ifaddr = nullptr;

if (xnetif == NULL)
{
ChipLogError(DeviceLayer, "Failed to get network interfaces");
}
else
{
for (struct netif * ifa = xnetif; ifa != NULL; ifa = ifa->next)
{
NetworkInterface * ifp = new NetworkInterface();

strncpy(ifp->Name, ifa->name, Inet::InterfaceId::kMaxIfNameLength);
ifp->Name[Inet::InterfaceId::kMaxIfNameLength - 1] = '\0';

ifp->name = CharSpan(ifp->Name, strlen(ifp->Name));
ifp->fabricConnected = true;
if ((ifa->flags) & NETIF_FLAG_ETHERNET)
ifp->type = EMBER_ZCL_INTERFACE_TYPE_ETHERNET;
else
ifp->type = EMBER_ZCL_INTERFACE_TYPE_WI_FI;
ifp->offPremiseServicesReachableIPv4 = false;
ifp->offPremiseServicesReachableIPv6 = false;

memcpy(ifp->MacAddress, ifa->hwaddr, sizeof(ifa->hwaddr));

if (0)
{
ChipLogError(DeviceLayer, "Failed to get network hardware address");
}
else
{
// Set 48-bit IEEE MAC Address
ifp->hardwareAddress = ByteSpan(ifp->MacAddress, 6);
}

ifp->Next = head;
head = ifp;
}
}

*netifpp = head;
return CHIP_NO_ERROR;
}

void DiagnosticDataProviderImpl::ReleaseNetworkInterfaces(NetworkInterface * netifp)
{
while (netifp)
{
NetworkInterface * del = netifp;
netifp = netifp->Next;
delete del;
}
}

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiBssId(ByteSpan & BssId)
{
CHIP_ERROR err = CHIP_ERROR_READ_FAILED;
static uint8_t ameba_bssid[6];

if (wifi_get_ap_bssid(ameba_bssid) == 0)
{
err = CHIP_NO_ERROR;
ChipLogProgress(DeviceLayer, "%02x,%02x,%02x,%02x,%02x,%02x\n", ameba_bssid[0], ameba_bssid[1], ameba_bssid[2],
ameba_bssid[3], ameba_bssid[4], ameba_bssid[5]);
}

BssId = ameba_bssid;

return CHIP_NO_ERROR;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiVersion(uint8_t & wifiVersion)
{
// Support 802.11a/n Wi-Fi in AmebaD chipset
wifiVersion = EMBER_ZCL_WI_FI_VERSION_TYPE_802__11N;
return CHIP_NO_ERROR;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiSecurityType(uint8_t & securityType)
{
securityType = 0;
unsigned int _auth_type;
unsigned short _security = 0;
rtw_wifi_setting_t setting;

#ifdef CONFIG_PLATFORM_8721D
if (wext_get_enc_ext("wlan0", &_security, &setting.key_idx, setting.password) < 0)
{
securityType = 0;
}
else
{
switch (_security)
{
case IW_ENCODE_ALG_NONE:
setting.security_type = EMBER_ZCL_SECURITY_TYPE_NONE;
break;
case IW_ENCODE_ALG_WEP:
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WEP;
break;
case IW_ENCODE_ALG_TKIP:
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WPA;
break;
case IW_ENCODE_ALG_CCMP:
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WPA2;
break;
default:
setting.security_type = EMBER_ZCL_SECURITY_TYPE_UNSPECIFIED;
break;
}
securityType = setting.security_type;
}
#else
wext_get_enc_ext("wlan0", &_security, &setting.key_idx, setting.password);
if (wext_get_auth_type("wlan0", &_auth_type) < 0)
{
securityType = 0;
}
else
{
switch (_security)
{
case IW_ENCODE_ALG_NONE:
setting.security_type = EMBER_ZCL_SECURITY_TYPE_NONE;
break;
case IW_ENCODE_ALG_WEP:
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WEP;
break;
case IW_ENCODE_ALG_TKIP:
if (_auth_type == WPA_SECURITY)
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WPA;
else if (_auth_type == WPA2_SECURITY)
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WPA2;
break;
case IW_ENCODE_ALG_CCMP:
if (_auth_type == WPA_SECURITY)
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WPA;
else if (_auth_type == WPA2_SECURITY)
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WPA2;
else if (_auth_type == WPA3_SECURITY)
setting.security_type = EMBER_ZCL_SECURITY_TYPE_WPA3;
break;
default:
setting.security_type = EMBER_ZCL_SECURITY_TYPE_UNSPECIFIED;
break;
}
securityType = setting.security_type;
}
#endif

return CHIP_NO_ERROR;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiChannelNumber(uint16_t & channelNumber)
{
channelNumber = 0;
unsigned char channel;

if (wext_get_channel("wlan0", &channel) < 0)
channelNumber = 0;
else
channelNumber = (uint16_t) channel;

return CHIP_NO_ERROR;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiRssi(int8_t & rssi)
{
rssi = 0;
int _rssi = 0;
if (wifi_get_rssi(&_rssi) < 0)
rssi = 0;
else
rssi = _rssi;

return CHIP_NO_ERROR;
}

Expand Down
5 changes: 5 additions & 0 deletions src/platform/Ameba/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider
CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override;
CHIP_ERROR GetBootReason(uint8_t & bootReason) override;

CHIP_ERROR GetNetworkInterfaces(NetworkInterface ** netifpp) override;
void ReleaseNetworkInterfaces(NetworkInterface * netifp) override;

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
CHIP_ERROR GetWiFiBssId(ByteSpan & BssId) override;
CHIP_ERROR GetWiFiSecurityType(uint8_t & securityType) override;
CHIP_ERROR GetWiFiVersion(uint8_t & wifiVersion) override;
CHIP_ERROR GetWiFiChannelNumber(uint16_t & channelNumber) override;
CHIP_ERROR GetWiFiRssi(int8_t & rssi) override;
CHIP_ERROR GetWiFiBeaconLostCount(uint32_t & beaconLostCount) override;
Expand Down

0 comments on commit 46586da

Please sign in to comment.