| /* | |
| * | |
| * Copyright (c) 2021-2022 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. | |
| */ | |
| /** | |
| * @file | |
| * Provides an implementation of the DiagnosticDataProvider object | |
| * for Linux platform. | |
| */ | |
| #include <platform/internal/CHIPDeviceLayerInternal.h> | |
| #include <app/data-model/List.h> | |
| #include <lib/support/CHIPMem.h> | |
| #include <lib/support/CHIPMemString.h> | |
| #include <lib/support/Defer.h> | |
| #include <lib/support/ParseDecimal.h> | |
| #include <lib/support/logging/CHIPLogging.h> | |
| #include <platform/DiagnosticDataProvider.h> | |
| #include <platform/Linux/ConnectivityUtils.h> | |
| #include <platform/Linux/DiagnosticDataProviderImpl.h> | |
| #include <arpa/inet.h> | |
| #include <dirent.h> | |
| #include <fstream> | |
| #include <ifaddrs.h> | |
| #include <linux/ethtool.h> | |
| #include <linux/if_link.h> | |
| #include <linux/netlink.h> | |
| #include <linux/rtnetlink.h> | |
| #include <malloc.h> | |
| #include <net/if.h> | |
| #include <netinet/in.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| using namespace ::chip; | |
| using namespace ::chip::app; | |
| using namespace ::chip::TLV; | |
| using namespace ::chip::DeviceLayer; | |
| using namespace ::chip::DeviceLayer::Internal; | |
| using namespace ::chip::app::Clusters::GeneralDiagnostics; | |
| namespace { | |
| enum class EthernetStatsCountType | |
| { | |
| kEthPacketRxCount, | |
| kEthPacketTxCount, | |
| kEthTxErrCount, | |
| kEthCollisionCount, | |
| kEthOverrunCount | |
| }; | |
| enum class WiFiStatsCountType | |
| { | |
| kWiFiUnicastPacketRxCount, | |
| kWiFiUnicastPacketTxCount, | |
| kWiFiMulticastPacketRxCount, | |
| kWiFiMulticastPacketTxCount, | |
| kWiFiOverrunCount | |
| }; | |
| #if defined(__GLIBC__) | |
| // Static variable to store the maximum heap size | |
| static uint64_t maxHeapHighWatermark = 0; | |
| #endif | |
| CHIP_ERROR GetThreadName(pid_t tid, char * nameBuf, size_t nameBufSize) | |
| { | |
| char path[64]; | |
| snprintf(path, sizeof(path), "/proc/self/task/%u/comm", tid); | |
| std::ifstream commFile(path); | |
| VerifyOrReturnError(commFile.is_open(), CHIP_ERROR_READ_FAILED); | |
| commFile.getline(nameBuf, nameBufSize); | |
| VerifyOrReturnError(commFile.gcount() > 0, CHIP_ERROR_READ_FAILED); | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR GetEthernetStatsCount(EthernetStatsCountType type, uint64_t & count) | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetEthernetIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| struct ifaddrs * ifa = nullptr; | |
| VerifyOrReturnError(getifaddrs(&ifa) != -1, CHIP_ERROR_READ_FAILED, | |
| ChipLogError(DeviceLayer, "Failed to get network interfaces: %s", strerror(errno))); | |
| auto deferClose = MakeDefer([ifa]() { freeifaddrs(ifa); }); | |
| for (; ifa != nullptr; ifa = ifa->ifa_next) | |
| { | |
| if (strcmp(ifa->ifa_name, ifName) == 0 && ifa->ifa_data != nullptr) | |
| { | |
| auto stats = static_cast<struct rtnl_link_stats *>(ifa->ifa_data); | |
| switch (type) | |
| { | |
| case EthernetStatsCountType::kEthPacketRxCount: | |
| count = stats->rx_packets; | |
| return CHIP_NO_ERROR; | |
| case EthernetStatsCountType::kEthPacketTxCount: | |
| count = stats->tx_packets; | |
| return CHIP_NO_ERROR; | |
| case EthernetStatsCountType::kEthTxErrCount: | |
| count = stats->tx_errors; | |
| return CHIP_NO_ERROR; | |
| case EthernetStatsCountType::kEthCollisionCount: | |
| count = stats->collisions; | |
| return CHIP_NO_ERROR; | |
| case EthernetStatsCountType::kEthOverrunCount: | |
| count = stats->rx_over_errors; | |
| return CHIP_NO_ERROR; | |
| default: | |
| ChipLogError(DeviceLayer, "Unknown Ethernet statistic metric type"); | |
| break; | |
| } | |
| } | |
| } | |
| return CHIP_ERROR_READ_FAILED; | |
| } | |
| #if CHIP_DEVICE_CONFIG_ENABLE_WIFI | |
| CHIP_ERROR GetWiFiStatsCount(WiFiStatsCountType type, uint64_t & count) | |
| { | |
| CHIP_ERROR err = CHIP_ERROR_READ_FAILED; | |
| struct ifaddrs * ifaddr = nullptr; | |
| if (getifaddrs(&ifaddr) == -1) | |
| { | |
| ChipLogError(DeviceLayer, "Failed to get network interfaces"); | |
| } | |
| else | |
| { | |
| struct ifaddrs * ifa = nullptr; | |
| // Walk through linked list, maintaining head pointer so we can free list later. | |
| for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) | |
| { | |
| if (ConnectivityUtils::GetInterfaceConnectionType(ifa->ifa_name) == InterfaceTypeEnum::kWiFi) | |
| { | |
| ChipLogProgress(DeviceLayer, "Found the primary WiFi interface:%s", StringOrNullMarker(ifa->ifa_name)); | |
| break; | |
| } | |
| } | |
| if (ifa != nullptr) | |
| { | |
| if (ifa->ifa_addr->sa_family == AF_PACKET && ifa->ifa_data != nullptr) | |
| { | |
| // The usecase of this function is embedded devices,on which we can interact with the WiFi | |
| // driver to get the accurate number of muticast and unicast packets accurately. | |
| // On Linux simulation, we can only get the total packets received, the total bytes transmitted, | |
| // the multicast packets received and receiver ring buff overflow. | |
| struct rtnl_link_stats * stats = (struct rtnl_link_stats *) ifa->ifa_data; | |
| switch (type) | |
| { | |
| case WiFiStatsCountType::kWiFiUnicastPacketRxCount: | |
| count = stats->rx_packets; | |
| err = CHIP_NO_ERROR; | |
| break; | |
| case WiFiStatsCountType::kWiFiUnicastPacketTxCount: | |
| count = stats->tx_packets; | |
| err = CHIP_NO_ERROR; | |
| break; | |
| case WiFiStatsCountType::kWiFiMulticastPacketRxCount: | |
| count = stats->multicast; | |
| err = CHIP_NO_ERROR; | |
| break; | |
| case WiFiStatsCountType::kWiFiMulticastPacketTxCount: | |
| count = 0; | |
| err = CHIP_NO_ERROR; | |
| break; | |
| case WiFiStatsCountType::kWiFiOverrunCount: | |
| count = stats->rx_over_errors; | |
| err = CHIP_NO_ERROR; | |
| break; | |
| default: | |
| ChipLogError(DeviceLayer, "Unknown WiFi statistic metric type"); | |
| break; | |
| } | |
| } | |
| } | |
| freeifaddrs(ifaddr); | |
| } | |
| return err; | |
| } | |
| #endif // #if CHIP_DEVICE_CONFIG_ENABLE_WIFI | |
| } // namespace | |
| namespace chip { | |
| namespace DeviceLayer { | |
| DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance() | |
| { | |
| static DiagnosticDataProviderImpl sInstance; | |
| return sInstance; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree) | |
| { | |
| #if defined(__GLIBC__) | |
| struct mallinfo mallocInfo = mallinfo(); | |
| // Get the current amount of heap memory, in bytes, that are not being utilized | |
| // by the current running program. | |
| currentHeapFree = mallocInfo.fordblks; | |
| return CHIP_NO_ERROR; | |
| #else | |
| return CHIP_ERROR_NOT_IMPLEMENTED; | |
| #endif | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed) | |
| { | |
| #if defined(__GLIBC__) | |
| struct mallinfo mallocInfo = mallinfo(); | |
| // Get the current amount of heap memory, in bytes, that are being used by | |
| // the current running program. | |
| currentHeapUsed = mallocInfo.uordblks; | |
| // Update the maximum heap high watermark if the current heap usage exceeds it. | |
| if (currentHeapUsed > maxHeapHighWatermark) | |
| { | |
| maxHeapHighWatermark = currentHeapUsed; | |
| } | |
| return CHIP_NO_ERROR; | |
| #else | |
| return CHIP_ERROR_NOT_IMPLEMENTED; | |
| #endif | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) | |
| { | |
| #if defined(__GLIBC__) | |
| struct mallinfo mallocInfo = mallinfo(); | |
| // The usecase of this function is embedded devices,on which we would need to intercept | |
| // malloc/calloc/free and then record the maximum amount of heap memory,in bytes, that | |
| // has been used by the Node. | |
| // On Linux, since it uses virtual memory, whereby a page of memory could be copied to | |
| // the hard disk, called swap space, and free up that page of memory. So it is impossible | |
| // to know accurately peak physical memory it use. | |
| // Update the maximum heap high watermark if the current heap usage exceeds it. | |
| if (mallocInfo.uordblks > static_cast<int>(maxHeapHighWatermark)) | |
| { | |
| maxHeapHighWatermark = mallocInfo.uordblks; | |
| } | |
| // Set the current heap high watermark. | |
| currentHeapHighWatermark = maxHeapHighWatermark; | |
| return CHIP_NO_ERROR; | |
| #else | |
| return CHIP_ERROR_NOT_IMPLEMENTED; | |
| #endif | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::ResetWatermarks() | |
| { | |
| // If implemented, the server SHALL set the value of the CurrentHeapHighWatermark attribute to the | |
| // value of the CurrentHeapUsed. | |
| #if defined(__GLIBC__) | |
| // Get the current amount of heap memory, in bytes, that are being used by | |
| // the current running program and reset the max heap high watermark to current heap amount. | |
| struct mallinfo mallocInfo = mallinfo(); | |
| maxHeapHighWatermark = mallocInfo.uordblks; | |
| #endif | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetThreadMetrics(ThreadMetrics ** threadMetricsOut) | |
| { | |
| CHIP_ERROR err = CHIP_ERROR_READ_FAILED; | |
| DIR * proc_dir = opendir("/proc/self/task"); | |
| if (proc_dir == nullptr) | |
| { | |
| ChipLogError(DeviceLayer, "Failed to open current process task directory"); | |
| } | |
| else | |
| { | |
| ThreadMetrics * head = nullptr; | |
| struct dirent * entry; | |
| /* proc available, iterate through tasks... */ | |
| while ((entry = readdir(proc_dir)) != nullptr) | |
| { | |
| pid_t tid; | |
| if (!ParseDecimal(entry->d_name, tid)) | |
| { | |
| continue; | |
| } | |
| ThreadMetrics * thread = new ThreadMetrics(); | |
| thread->id = tid; | |
| if (GetThreadName(tid, thread->NameBuf, sizeof(thread->NameBuf)) == CHIP_NO_ERROR) | |
| { | |
| thread->name.Emplace(CharSpan::fromCharString(thread->NameBuf)); | |
| } | |
| // TODO: Get stack info of each thread: thread->stackFreeCurrent, | |
| // thread->stackFreeMinimum, thread->stackSize. | |
| thread->Next = head; | |
| head = thread; | |
| } | |
| closedir(proc_dir); | |
| *threadMetricsOut = head; | |
| err = CHIP_NO_ERROR; | |
| } | |
| return err; | |
| } | |
| void DiagnosticDataProviderImpl::ReleaseThreadMetrics(ThreadMetrics * threadMetrics) | |
| { | |
| while (threadMetrics) | |
| { | |
| ThreadMetrics * del = threadMetrics; | |
| threadMetrics = threadMetrics->Next; | |
| delete del; | |
| } | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetRebootCount(uint16_t & rebootCount) | |
| { | |
| uint32_t count = 0; | |
| CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); | |
| if (err == CHIP_NO_ERROR) | |
| { | |
| VerifyOrReturnError(count <= UINT16_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| rebootCount = static_cast<uint16_t>(count); | |
| } | |
| return err; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetUpTime(uint64_t & upTime) | |
| { | |
| System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp(); | |
| System::Clock::Timestamp startTime = PlatformMgrImpl().GetStartTime(); | |
| if (currentTime >= startTime) | |
| { | |
| upTime = std::chrono::duration_cast<System::Clock::Seconds64>(currentTime - startTime).count(); | |
| return CHIP_NO_ERROR; | |
| } | |
| return CHIP_ERROR_INVALID_TIME; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours) | |
| { | |
| uint64_t upTime = 0; | |
| if (GetUpTime(upTime) == CHIP_NO_ERROR) | |
| { | |
| uint32_t totalHours = 0; | |
| if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) | |
| { | |
| VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| totalOperationalHours = totalHours + static_cast<uint32_t>(upTime / 3600); | |
| return CHIP_NO_ERROR; | |
| } | |
| } | |
| return CHIP_ERROR_INVALID_TIME; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(BootReasonType & bootReason) | |
| { | |
| uint32_t reason = 0; | |
| CHIP_ERROR err = ConfigurationMgr().GetBootReason(reason); | |
| if (err == CHIP_NO_ERROR) | |
| { | |
| VerifyOrReturnError(reason <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| bootReason = static_cast<BootReasonType>(reason); | |
| } | |
| return err; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetActiveHardwareFaults(GeneralFaults<kMaxHardwareFaults> & hardwareFaults) | |
| { | |
| #if CHIP_CONFIG_TEST | |
| // On Linux Simulation, set following hardware faults statically. | |
| ReturnErrorOnFailure(hardwareFaults.add(to_underlying(HardwareFaultEnum::kRadio))); | |
| ReturnErrorOnFailure(hardwareFaults.add(to_underlying(HardwareFaultEnum::kSensor))); | |
| ReturnErrorOnFailure(hardwareFaults.add(to_underlying(HardwareFaultEnum::kPowerSource))); | |
| ReturnErrorOnFailure(hardwareFaults.add(to_underlying(HardwareFaultEnum::kUserInterfaceFault))); | |
| #endif | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetActiveRadioFaults(GeneralFaults<kMaxRadioFaults> & radioFaults) | |
| { | |
| #if CHIP_CONFIG_TEST | |
| // On Linux Simulation, set following radio faults statically. | |
| ReturnErrorOnFailure(radioFaults.add(to_underlying(RadioFaultEnum::kWiFiFault))); | |
| ReturnErrorOnFailure(radioFaults.add(to_underlying(RadioFaultEnum::kCellularFault))); | |
| ReturnErrorOnFailure(radioFaults.add(to_underlying(RadioFaultEnum::kThreadFault))); | |
| ReturnErrorOnFailure(radioFaults.add(to_underlying(RadioFaultEnum::kNFCFault))); | |
| #endif | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetActiveNetworkFaults(GeneralFaults<kMaxNetworkFaults> & networkFaults) | |
| { | |
| #if CHIP_CONFIG_TEST | |
| // On Linux Simulation, set following radio faults statically. | |
| ReturnErrorOnFailure(networkFaults.add(to_underlying(NetworkFaultEnum::kHardwareFailure))); | |
| ReturnErrorOnFailure(networkFaults.add(to_underlying(NetworkFaultEnum::kNetworkJammed))); | |
| ReturnErrorOnFailure(networkFaults.add(to_underlying(NetworkFaultEnum::kConnectionFailed))); | |
| #endif | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** netifpp) | |
| { | |
| CHIP_ERROR err = CHIP_ERROR_READ_FAILED; | |
| struct ifaddrs * ifaddr = nullptr; | |
| if (getifaddrs(&ifaddr) == -1) | |
| { | |
| ChipLogError(DeviceLayer, "Failed to get network interfaces"); | |
| } | |
| else | |
| { | |
| NetworkInterface * head = nullptr; | |
| // Walk through linked list, maintaining head pointer so we can free list later. | |
| for (struct ifaddrs * ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) | |
| { | |
| if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_PACKET) | |
| { | |
| uint8_t size = 0; | |
| NetworkInterface * ifp = new NetworkInterface(); | |
| Platform::CopyString(ifp->Name, ifa->ifa_name); | |
| ifp->name = CharSpan::fromCharString(ifp->Name); | |
| ifp->isOperational = ifa->ifa_flags & IFF_RUNNING; | |
| ifp->type = ConnectivityUtils::GetInterfaceConnectionType(ifa->ifa_name); | |
| ifp->offPremiseServicesReachableIPv4.SetNull(); | |
| ifp->offPremiseServicesReachableIPv6.SetNull(); | |
| if (ConnectivityUtils::GetInterfaceIPv4Addrs(ifa->ifa_name, size, ifp) == CHIP_NO_ERROR) | |
| { | |
| if (size > 0) | |
| { | |
| ifp->IPv4Addresses = DataModel::List<const chip::ByteSpan>(ifp->Ipv4AddressSpans, size); | |
| } | |
| } | |
| if (ConnectivityUtils::GetInterfaceIPv6Addrs(ifa->ifa_name, size, ifp) == CHIP_NO_ERROR) | |
| { | |
| if (size > 0) | |
| { | |
| ifp->IPv6Addresses = DataModel::List<const chip::ByteSpan>(ifp->Ipv6AddressSpans, size); | |
| } | |
| } | |
| if (ConnectivityUtils::GetInterfaceHardwareAddrs(ifa->ifa_name, ifp->MacAddress, kMaxHardwareAddrSize) != | |
| CHIP_NO_ERROR) | |
| { | |
| 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; | |
| err = CHIP_NO_ERROR; | |
| freeifaddrs(ifaddr); | |
| } | |
| return err; | |
| } | |
| void DiagnosticDataProviderImpl::ReleaseNetworkInterfaces(NetworkInterface * netifp) | |
| { | |
| while (netifp) | |
| { | |
| NetworkInterface * del = netifp; | |
| netifp = netifp->Next; | |
| delete del; | |
| } | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthPHYRate(app::Clusters::EthernetNetworkDiagnostics::PHYRateEnum & pHYRate) | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetEthernetIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| return ConnectivityUtils::GetEthPHYRate(ifName, pHYRate); | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthFullDuplex(bool & fullDuplex) | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetEthernetIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| return ConnectivityUtils::GetEthFullDuplex(ifName, fullDuplex); | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthTimeSinceReset(uint64_t & timeSinceReset) | |
| { | |
| uint64_t currentUptime = 0; | |
| ReturnErrorOnFailure(GetDiagnosticDataProvider().GetUpTime(currentUptime)); | |
| VerifyOrReturnError(currentUptime >= mEthTimeSinceResetBaseline, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| timeSinceReset = currentUptime - mEthTimeSinceResetBaseline; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthPacketRxCount(uint64_t & packetRxCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetEthernetStatsCount(EthernetStatsCountType::kEthPacketRxCount, count)); | |
| VerifyOrReturnError(count >= mEthPacketRxCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| packetRxCount = count - mEthPacketRxCount; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthPacketTxCount(uint64_t & packetTxCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetEthernetStatsCount(EthernetStatsCountType::kEthPacketTxCount, count)); | |
| VerifyOrReturnError(count >= mEthPacketTxCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| packetTxCount = count - mEthPacketTxCount; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthTxErrCount(uint64_t & txErrCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetEthernetStatsCount(EthernetStatsCountType::kEthTxErrCount, count)); | |
| VerifyOrReturnError(count >= mEthTxErrCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| txErrCount = count - mEthTxErrCount; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthCollisionCount(uint64_t & collisionCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetEthernetStatsCount(EthernetStatsCountType::kEthCollisionCount, count)); | |
| VerifyOrReturnError(count >= mEthCollisionCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| collisionCount = count - mEthCollisionCount; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthOverrunCount(uint64_t & overrunCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetEthernetStatsCount(EthernetStatsCountType::kEthOverrunCount, count)); | |
| VerifyOrReturnError(count >= mEthOverrunCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| overrunCount = count - mEthOverrunCount; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetEthCarrierDetect(bool & carrierDetect) | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetEthernetIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| return ConnectivityUtils::GetEthCarrierDetect(ifName, carrierDetect); | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::ResetEthNetworkDiagnosticsCounts() | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetEthernetIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| uint64_t currentUptime = 0; | |
| ReturnErrorOnFailure(GetDiagnosticDataProvider().GetUpTime(currentUptime)); | |
| struct ifaddrs * ifa = nullptr; | |
| VerifyOrReturnError(getifaddrs(&ifa) != -1, CHIP_ERROR_READ_FAILED, | |
| ChipLogError(DeviceLayer, "Failed to get network interfaces: %s", strerror(errno))); | |
| auto deferClose = MakeDefer([ifa]() { freeifaddrs(ifa); }); | |
| for (; ifa != nullptr; ifa = ifa->ifa_next) | |
| { | |
| if (strcmp(ifa->ifa_name, ifName) == 0 && ifa->ifa_data != nullptr) | |
| { | |
| auto stats = static_cast<struct rtnl_link_stats *>(ifa->ifa_data); | |
| mEthPacketRxCount = stats->rx_packets; | |
| mEthPacketTxCount = stats->tx_packets; | |
| mEthTxErrCount = stats->tx_errors; | |
| mEthCollisionCount = stats->collisions; | |
| mEthOverrunCount = stats->rx_over_errors; | |
| mEthTimeSinceResetBaseline = currentUptime; | |
| return CHIP_NO_ERROR; | |
| } | |
| } | |
| return CHIP_ERROR_READ_FAILED; | |
| } | |
| #if CHIP_DEVICE_CONFIG_ENABLE_WIFI | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiChannelNumber(uint16_t & channelNumber) | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetWiFiIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| return ConnectivityUtils::GetWiFiChannelNumber(ifName, channelNumber); | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiRssi(int8_t & rssi) | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetWiFiIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| return ConnectivityUtils::GetWiFiRssi(ifName, rssi); | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiBeaconLostCount(uint32_t & beaconLostCount) | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetWiFiIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| uint32_t count; | |
| ReturnErrorOnFailure(ConnectivityUtils::GetWiFiBeaconLostCount(ifName, count)); | |
| VerifyOrReturnError(count >= mBeaconLostCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| beaconLostCount = count - mBeaconLostCount; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiCurrentMaxRate(uint64_t & currentMaxRate) | |
| { | |
| const char * ifName = ConnectivityMgrImpl().GetWiFiIfName(); | |
| VerifyOrReturnError(ifName != nullptr, CHIP_ERROR_READ_FAILED); | |
| return ConnectivityUtils::GetWiFiCurrentMaxRate(ifName, currentMaxRate); | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiPacketMulticastRxCount(uint32_t & packetMulticastRxCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetWiFiStatsCount(WiFiStatsCountType::kWiFiMulticastPacketRxCount, count)); | |
| VerifyOrReturnError(count >= mPacketMulticastRxCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| count -= mPacketMulticastRxCount; | |
| VerifyOrReturnError(count <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| packetMulticastRxCount = static_cast<uint32_t>(count); | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiPacketMulticastTxCount(uint32_t & packetMulticastTxCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetWiFiStatsCount(WiFiStatsCountType::kWiFiMulticastPacketTxCount, count)); | |
| VerifyOrReturnError(count >= mPacketMulticastTxCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| count -= mPacketMulticastTxCount; | |
| VerifyOrReturnError(count <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| packetMulticastTxCount = static_cast<uint32_t>(count); | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiPacketUnicastRxCount(uint32_t & packetUnicastRxCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetWiFiStatsCount(WiFiStatsCountType::kWiFiUnicastPacketRxCount, count)); | |
| VerifyOrReturnError(count >= mPacketUnicastRxCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| count -= mPacketUnicastRxCount; | |
| VerifyOrReturnError(count <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| packetUnicastRxCount = static_cast<uint32_t>(count); | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiPacketUnicastTxCount(uint32_t & packetUnicastTxCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetWiFiStatsCount(WiFiStatsCountType::kWiFiUnicastPacketTxCount, count)); | |
| VerifyOrReturnError(count >= mPacketUnicastTxCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| count -= mPacketUnicastTxCount; | |
| VerifyOrReturnError(count <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| packetUnicastTxCount = static_cast<uint32_t>(count); | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiOverrunCount(uint64_t & overrunCount) | |
| { | |
| uint64_t count; | |
| ReturnErrorOnFailure(GetWiFiStatsCount(WiFiStatsCountType::kWiFiOverrunCount, count)); | |
| VerifyOrReturnError(count >= mOverrunCount, CHIP_ERROR_INVALID_INTEGER_VALUE); | |
| overrunCount = count - mOverrunCount; | |
| return CHIP_NO_ERROR; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiBeaconRxCount(uint32_t & beaconRxCount) | |
| { | |
| if (DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted()) | |
| { | |
| beaconRxCount = mBeaconRxCount; | |
| return CHIP_NO_ERROR; | |
| } | |
| return CHIP_ERROR_NOT_IMPLEMENTED; | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::ResetWiFiNetworkDiagnosticsCounts() | |
| { | |
| CHIP_ERROR err = CHIP_ERROR_READ_FAILED; | |
| struct ifaddrs * ifaddr = nullptr; | |
| ReturnErrorOnFailure(GetWiFiBeaconLostCount(mBeaconLostCount)); | |
| if (getifaddrs(&ifaddr) == -1) | |
| { | |
| ChipLogError(DeviceLayer, "Failed to get network interfaces"); | |
| } | |
| else | |
| { | |
| struct ifaddrs * ifa = nullptr; | |
| // Walk through linked list, maintaining head pointer so we can free list later. | |
| for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) | |
| { | |
| if (ConnectivityUtils::GetInterfaceConnectionType(ifa->ifa_name) == InterfaceTypeEnum::kWiFi) | |
| { | |
| ChipLogProgress(DeviceLayer, "Found the primary WiFi interface:%s", StringOrNullMarker(ifa->ifa_name)); | |
| break; | |
| } | |
| } | |
| if (ifa != nullptr) | |
| { | |
| if (ifa->ifa_addr->sa_family == AF_PACKET && ifa->ifa_data != nullptr) | |
| { | |
| struct rtnl_link_stats * stats = (struct rtnl_link_stats *) ifa->ifa_data; | |
| mPacketMulticastRxCount = stats->multicast; | |
| mPacketMulticastTxCount = 0; | |
| mPacketUnicastRxCount = stats->rx_packets; | |
| mPacketUnicastTxCount = stats->tx_packets; | |
| mOverrunCount = stats->rx_over_errors; | |
| err = CHIP_NO_ERROR; | |
| } | |
| } | |
| freeifaddrs(ifaddr); | |
| } | |
| return err; | |
| } | |
| #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI | |
| #if CHIP_DEVICE_CONFIG_ENABLE_WPA | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiVersion(app::Clusters::WiFiNetworkDiagnostics::WiFiVersionEnum & wiFiVersion) | |
| { | |
| return ConnectivityMgrImpl().GetWiFiVersion(wiFiVersion); | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiBssId(MutableByteSpan & value) | |
| { | |
| return ConnectivityMgrImpl().GetWiFiBssId(value); | |
| } | |
| CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiSecurityType(app::Clusters::WiFiNetworkDiagnostics::SecurityTypeEnum & securityType) | |
| { | |
| return ConnectivityMgrImpl().GetWiFiSecurityType(securityType); | |
| } | |
| #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA | |
| DiagnosticDataProvider & GetDiagnosticDataProviderImpl() | |
| { | |
| return DiagnosticDataProviderImpl::GetDefaultInstance(); | |
| } | |
| } // namespace DeviceLayer | |
| } // namespace chip |