Skip to content

Commit

Permalink
Add Wifi and General diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
pankore committed Nov 25, 2021
1 parent 914b58e commit 6e00faf
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 4 deletions.
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 6e00faf

Please sign in to comment.