diff --git a/examples/all-clusters-app/ameba/main/chipinterface.cpp b/examples/all-clusters-app/ameba/main/chipinterface.cpp index d3059b23ca9764..e49d28aaca6c24 100644 --- a/examples/all-clusters-app/ameba/main/chipinterface.cpp +++ b/examples/all-clusters-app/ameba/main/chipinterface.cpp @@ -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); @@ -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); @@ -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; }; @@ -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 qrCode(3 * qrCodeText.size() + 1); err = EncodeQRCodeToUrl(qrCodeText.c_str(), qrCodeText.size(), qrCode.data(), qrCode.max_size()); if (err == CHIP_NO_ERROR) diff --git a/src/platform/Ameba/AmebaConfig.cpp b/src/platform/Ameba/AmebaConfig.cpp index f40809dd1249e2..02d0580ca82c4c 100644 --- a/src/platform/Ameba/AmebaConfig.cpp +++ b/src/platform/Ameba/AmebaConfig.cpp @@ -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) diff --git a/src/platform/Ameba/ConfigurationManagerImpl.cpp b/src/platform/Ameba/ConfigurationManagerImpl.cpp index a99924bbe56040..ad0c2f4fde70ed 100644 --- a/src/platform/Ameba/ConfigurationManagerImpl.cpp +++ b/src/platform/Ameba/ConfigurationManagerImpl.cpp @@ -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; diff --git a/src/platform/Ameba/DiagnosticDataProviderImpl.cpp b/src/platform/Ameba/DiagnosticDataProviderImpl.cpp index e447b56d7ca175..9aa1fc058b06db 100644 --- a/src/platform/Ameba/DiagnosticDataProviderImpl.cpp +++ b/src/platform/Ameba/DiagnosticDataProviderImpl.cpp @@ -27,6 +27,8 @@ #include #include +#include + namespace chip { namespace DeviceLayer { @@ -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; } diff --git a/src/platform/Ameba/DiagnosticDataProviderImpl.h b/src/platform/Ameba/DiagnosticDataProviderImpl.h index 047d55bdac9b3f..9190c1a3bdc9e8 100644 --- a/src/platform/Ameba/DiagnosticDataProviderImpl.h +++ b/src/platform/Ameba/DiagnosticDataProviderImpl.h @@ -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;