Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[telemetry] define key telemetries and their extraction logic #1973

Merged
merged 28 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
add_library(otbr-common
api_strings.cpp
byteswap.hpp
code_utils.cpp
code_utils.hpp
dns_utils.cpp
logging.cpp
Expand All @@ -49,4 +50,6 @@ target_link_libraries(otbr-common
PUBLIC otbr-config
openthread-ftd
openthread-posix
$<$<BOOL:${OTBR_FEATURE_FLAGS}>:otbr-proto>
$<$<BOOL:${OTBR_TELEMETRY_DATA_API}>:otbr-proto>
)
40 changes: 40 additions & 0 deletions src/common/code_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "common/code_utils.hpp"

uint64_t ConvertOpenThreadUint64(const uint8_t *aValue)
{
uint64_t val = 0;

for (size_t i = 0; i < sizeof(uint64_t); i++)
{
val = (val << 8) | aValue[i];
}
return val;
}
12 changes: 12 additions & 0 deletions src/common/code_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
reinterpret_cast<aAlignType>( \
((reinterpret_cast<unsigned long>(aMem) + sizeof(aAlignType) - 1) / sizeof(aAlignType)) * sizeof(aAlignType))

// Allocate the structure using "raw" storage.
#define OT_DEFINE_ALIGNED_VAR(name, size, align_type) \
align_type name[(((size) + (sizeof(align_type) - 1)) / sizeof(align_type))]

#ifndef CONTAINING_RECORD
#define BASE 0x1
#define myoffsetof(s, m) (((size_t) & (((s *)BASE)->m)) - BASE)
Expand Down Expand Up @@ -165,6 +169,14 @@ template <typename T, typename... Args> std::unique_ptr<T> MakeUnique(Args &&...
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

/**
* This method converts 8 uint8_t bytes into uint64_t using big-endian.
*
* @param[in] aValue The input 8 uint8_t bytes.
* @returns The converted uint64_t.
*/
uint64_t ConvertOpenThreadUint64(const uint8_t *aValue);

/**
* This class makes any class that derives from it non-copyable. It is intended to be used as a private base class.
*
Expand Down
125 changes: 3 additions & 122 deletions src/dbus/server/dbus_thread_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

#include "common/api_strings.hpp"
#include "common/byteswap.hpp"
#include "common/code_utils.hpp"
#include "dbus/common/constants.hpp"
#include "dbus/server/dbus_agent.hpp"
#include "dbus/server/dbus_thread_object.hpp"
Expand Down Expand Up @@ -85,58 +86,6 @@ static std::string GetNat64StateName(otNat64State aState)
}
#endif // OTBR_ENABLE_NAT64

static uint64_t ConvertOpenThreadUint64(const uint8_t *aValue)
{
uint64_t val = 0;

for (size_t i = 0; i < sizeof(uint64_t); i++)
{
val = (val << 8) | aValue[i];
}
return val;
}

#if OTBR_ENABLE_TELEMETRY_DATA_API
static uint32_t TelemetryNodeTypeFromRoleAndLinkMode(const otDeviceRole &aRole, const otLinkModeConfig &aLinkModeCfg)
{
uint32_t nodeType;

switch (aRole)
{
case OT_DEVICE_ROLE_DISABLED:
nodeType = threadnetwork::TelemetryData::NODE_TYPE_DISABLED;
break;
case OT_DEVICE_ROLE_DETACHED:
nodeType = threadnetwork::TelemetryData::NODE_TYPE_DETACHED;
break;
case OT_DEVICE_ROLE_ROUTER:
nodeType = threadnetwork::TelemetryData::NODE_TYPE_ROUTER;
break;
case OT_DEVICE_ROLE_LEADER:
nodeType = threadnetwork::TelemetryData::NODE_TYPE_LEADER;
break;
case OT_DEVICE_ROLE_CHILD:
if (!aLinkModeCfg.mRxOnWhenIdle)
{
nodeType = threadnetwork::TelemetryData::NODE_TYPE_SLEEPY_END;
}
else if (!aLinkModeCfg.mDeviceType)
{ // If it's not an FTD, return as minimal end device.
nodeType = threadnetwork::TelemetryData::NODE_TYPE_MINIMAL_END;
}
else
{
nodeType = threadnetwork::TelemetryData::NODE_TYPE_END;
}
break;
default:
nodeType = threadnetwork::TelemetryData::NODE_TYPE_UNSPECIFIED;
}

return nodeType;
}
#endif // OTBR_ENABLE_TELEMETRY_DATA_API

namespace otbr {
namespace DBus {

Expand Down Expand Up @@ -1443,77 +1392,9 @@ otError DBusThreadObject::GetTelemetryDataHandler(DBusMessageIter &aIter)
#if OTBR_ENABLE_TELEMETRY_DATA_API
otError error = OT_ERROR_NONE;
threadnetwork::TelemetryData telemetryData;
auto threadHelper = mNcp->GetThreadHelper();

auto threadHelper = mNcp->GetThreadHelper();

// Begin of WpanStats section.
auto wpanStats = telemetryData.mutable_wpan_stats();

{
otDeviceRole role = otThreadGetDeviceRole(threadHelper->GetInstance());
otLinkModeConfig otCfg = otThreadGetLinkMode(threadHelper->GetInstance());

wpanStats->set_node_type(TelemetryNodeTypeFromRoleAndLinkMode(role, otCfg));
}

wpanStats->set_channel(otLinkGetChannel(threadHelper->GetInstance()));

{
uint16_t ccaFailureRate = otLinkGetCcaFailureRate(threadHelper->GetInstance());

wpanStats->set_mac_cca_fail_rate(static_cast<float>(ccaFailureRate) / 0xffff);
}

{
int8_t radioTxPower;

SuccessOrExit(error = otPlatRadioGetTransmitPower(threadHelper->GetInstance(), &radioTxPower));
wpanStats->set_radio_tx_power(radioTxPower);
}

{
const otMacCounters *linkCounters = otLinkGetCounters(threadHelper->GetInstance());

wpanStats->set_phy_rx(linkCounters->mRxTotal);
wpanStats->set_phy_tx(linkCounters->mTxTotal);
wpanStats->set_mac_unicast_rx(linkCounters->mRxUnicast);
wpanStats->set_mac_unicast_tx(linkCounters->mTxUnicast);
wpanStats->set_mac_broadcast_rx(linkCounters->mRxBroadcast);
wpanStats->set_mac_broadcast_tx(linkCounters->mTxBroadcast);
wpanStats->set_mac_tx_ack_req(linkCounters->mTxAckRequested);
wpanStats->set_mac_tx_no_ack_req(linkCounters->mTxNoAckRequested);
wpanStats->set_mac_tx_acked(linkCounters->mTxAcked);
wpanStats->set_mac_tx_data(linkCounters->mTxData);
wpanStats->set_mac_tx_data_poll(linkCounters->mTxDataPoll);
wpanStats->set_mac_tx_beacon(linkCounters->mTxBeacon);
wpanStats->set_mac_tx_beacon_req(linkCounters->mTxBeaconRequest);
wpanStats->set_mac_tx_other_pkt(linkCounters->mTxOther);
wpanStats->set_mac_tx_retry(linkCounters->mTxRetry);
wpanStats->set_mac_rx_data(linkCounters->mRxData);
wpanStats->set_mac_rx_data_poll(linkCounters->mRxDataPoll);
wpanStats->set_mac_rx_beacon(linkCounters->mRxBeacon);
wpanStats->set_mac_rx_beacon_req(linkCounters->mRxBeaconRequest);
wpanStats->set_mac_rx_other_pkt(linkCounters->mRxOther);
wpanStats->set_mac_rx_filter_whitelist(linkCounters->mRxAddressFiltered);
wpanStats->set_mac_rx_filter_dest_addr(linkCounters->mRxDestAddrFiltered);
wpanStats->set_mac_tx_fail_cca(linkCounters->mTxErrCca);
wpanStats->set_mac_rx_fail_decrypt(linkCounters->mRxErrSec);
wpanStats->set_mac_rx_fail_no_frame(linkCounters->mRxErrNoFrame);
wpanStats->set_mac_rx_fail_unknown_neighbor(linkCounters->mRxErrUnknownNeighbor);
wpanStats->set_mac_rx_fail_invalid_src_addr(linkCounters->mRxErrInvalidSrcAddr);
wpanStats->set_mac_rx_fail_fcs(linkCounters->mRxErrFcs);
wpanStats->set_mac_rx_fail_other(linkCounters->mRxErrOther);
}

{
const otIpCounters *ipCounters = otThreadGetIp6Counters(threadHelper->GetInstance());

wpanStats->set_ip_tx_success(ipCounters->mTxSuccess);
wpanStats->set_ip_rx_success(ipCounters->mRxSuccess);
wpanStats->set_ip_tx_failure(ipCounters->mTxFailure);
wpanStats->set_ip_rx_failure(ipCounters->mRxFailure);
}
// End of WpanStats section.
VerifyOrExit(threadHelper->RetrieveTelemetryData(*mPublisher, telemetryData) == OT_ERROR_NONE);

{
const std::string telemetryDataBytes = telemetryData.SerializeAsString();
Expand Down
7 changes: 1 addition & 6 deletions src/ncp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,5 @@ add_library(otbr-ncp
target_link_libraries(otbr-ncp PRIVATE
otbr-common
$<$<BOOL:${OTBR_FEATURE_FLAGS}>:otbr-proto>
$<$<BOOL:${OTBR_TELEMETRY_DATA_API}>:otbr-proto>
)

if(OTBR_FEATURE_FLAGS)
target_include_directories(otbr-ncp PRIVATE
${PROJECT_SOURCE_DIR}/build/src
)
endif()
7 changes: 6 additions & 1 deletion src/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ add_library(otbr-proto STATIC
)

find_package(Protobuf REQUIRED)
target_link_libraries(otbr-proto

target_link_libraries(otbr-proto PUBLIC
protobuf::libprotobuf-lite
)

target_include_directories(otbr-proto PUBLIC
${PROJECT_SOURCE_DIR}/build/src
)
Loading