Skip to content

Commit

Permalink
[dbus] implement D-BUS API GetSrpServerInfo (#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
superwhd authored May 19, 2022
1 parent faf97b2 commit dd645be
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ jobs:
BUILD_TARGET: check
OTBR_BUILD_TYPE: ${{ matrix.build_type }}
OTBR_MDNS: ${{ matrix.mdns }}
OTBR_OPTIONS: "-DOTBR_SRP_ADVERTISING_PROXY=ON"
OTBR_COVERAGE: 1
steps:
- uses: actions/checkout@v2
Expand All @@ -97,6 +98,8 @@ jobs:
env:
BUILD_TARGET: check
OTBR_REST: ${{ matrix.rest }}
OTBR_MDNS: mDNSResponder
OTBR_OPTIONS: "-DOTBR_SRP_ADVERTISING_PROXY=ON"
OTBR_COVERAGE: 1
steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 5 additions & 0 deletions src/dbus/client/thread_api_dbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ ClientError ThreadApiDBus::GetRadioRegion(std::string &aRadioRegion)
return GetProperty(OTBR_DBUS_PROPERTY_RADIO_REGION, aRadioRegion);
}

ClientError ThreadApiDBus::GetSrpServerInfo(SrpServerInfo &aSrpServerInfo)
{
return GetProperty(OTBR_DBUS_PROPERTY_SRP_SERVER_INFO, aSrpServerInfo);
}

std::string ThreadApiDBus::GetInterfaceName(void)
{
return mInterfaceName;
Expand Down
12 changes: 12 additions & 0 deletions src/dbus/client/thread_api_dbus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,18 @@ class ThreadApiDBus
*/
ClientError GetRadioRegion(std::string &aRadioRegion);

/**
* This method gets the SRP server information.
*
* @param[out] aSrpServerInfo The SRP server information.
*
* @retval ERROR_NONE Successfully performed the dbus function call
* @retval ERROR_DBUS dbus encode/decode error
* @retval ... OpenThread defined error value otherwise
*
*/
ClientError GetSrpServerInfo(SrpServerInfo &aSrpServerInfo);

/**
* This method returns the network interface name the client is bound to.
*
Expand Down
1 change: 1 addition & 0 deletions src/dbus/common/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
#define OTBR_DBUS_PROPERTY_ON_MESH_PREFIXES "OnMeshPrefixes"
#define OTBR_DBUS_PROPERTY_ACTIVE_DATASET_TLVS "ActiveDatasetTlvs"
#define OTBR_DBUS_PROPERTY_RADIO_REGION "RadioRegion"
#define OTBR_DBUS_PROPERTY_SRP_SERVER_INFO "SrpServerInfo"

#define OTBR_ROLE_NAME_DISABLED "disabled"
#define OTBR_ROLE_NAME_DETACHED "detached"
Expand Down
27 changes: 27 additions & 0 deletions src/dbus/common/dbus_message_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ otbrError DBusMessageEncode(DBusMessageIter *aIter, const ChannelQuality &aQuali
otbrError DBusMessageExtract(DBusMessageIter *aIter, ChannelQuality &aQuality);
otbrError DBusMessageEncode(DBusMessageIter *aIter, const TxtEntry &aTxtEntry);
otbrError DBusMessageExtract(DBusMessageIter *aIter, TxtEntry &aTxtEntry);
otbrError DBusMessageEncode(DBusMessageIter *aIter, const SrpServerInfo::Registration &aRegistration);
otbrError DBusMessageExtract(DBusMessageIter *aIter, SrpServerInfo::Registration &aRegistration);
otbrError DBusMessageEncode(DBusMessageIter *aIter, const SrpServerInfo::ResponseCounters &aResponseCounters);
otbrError DBusMessageExtract(DBusMessageIter *aIter, SrpServerInfo::ResponseCounters &aResponseCounters);
otbrError DBusMessageEncode(DBusMessageIter *aIter, const SrpServerInfo &aSrpServerInfo);
otbrError DBusMessageExtract(DBusMessageIter *aIter, SrpServerInfo &aSrpServerInfo);

template <typename T> struct DBusTypeTrait;

Expand Down Expand Up @@ -211,6 +217,27 @@ template <> struct DBusTypeTrait<std::vector<TxtEntry>>
static constexpr const char *TYPE_AS_STRING = "a(say)";
};

template <> struct DBusTypeTrait<SrpServerState>
{
static constexpr int TYPE = DBUS_TYPE_BYTE;
static constexpr const char *TYPE_AS_STRING = DBUS_TYPE_BYTE_AS_STRING;
};

template <> struct DBusTypeTrait<SrpServerAddressMode>
{
static constexpr int TYPE = DBUS_TYPE_BYTE;
static constexpr const char *TYPE_AS_STRING = DBUS_TYPE_BYTE_AS_STRING;
};

template <> struct DBusTypeTrait<SrpServerInfo>
{
// struct of { uint8, uint16, uint8,
// struct of { uint32, uint32, uint64, uint64, uint64, uint64 },
// struct of { uint32, uint32, uint64, uint64, uint64, uint64 },
// struct of { uint32, uint32, uint32, uint32, uint32, uint32} }
static constexpr const char *TYPE_AS_STRING = "(yqy(uutttt)(uutttt)(uuuuuu))";
};

template <> struct DBusTypeTrait<int8_t>
{
static constexpr int TYPE = DBUS_TYPE_BYTE;
Expand Down
97 changes: 97 additions & 0 deletions src/dbus/common/dbus_message_helper_openthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,5 +512,102 @@ otbrError DBusMessageExtract(DBusMessageIter *aIter, TxtEntry &aTxtEntry)
return error;
}

otbrError DBusMessageEncode(DBusMessageIter *aIter, const SrpServerInfo::Registration &aRegistration)
{
DBusMessageIter sub;
otbrError error = OTBR_ERROR_NONE;
auto args = std::tie(aRegistration.mFreshCount, aRegistration.mDeletedCount, aRegistration.mLeaseTimeTotal,
aRegistration.mKeyLeaseTimeTotal, aRegistration.mRemainingLeaseTimeTotal,
aRegistration.mRemainingKeyLeaseTimeTotal);

VerifyOrExit(dbus_message_iter_open_container(aIter, DBUS_TYPE_STRUCT, nullptr, &sub), error = OTBR_ERROR_DBUS);
SuccessOrExit(error = ConvertToDBusMessage(&sub, args));
VerifyOrExit(dbus_message_iter_close_container(aIter, &sub) == true, error = OTBR_ERROR_DBUS);
exit:
return error;
}

otbrError DBusMessageExtract(DBusMessageIter *aIter, SrpServerInfo::Registration &aRegistration)
{
DBusMessageIter sub;
otbrError error = OTBR_ERROR_NONE;
auto args = std::tie(aRegistration.mFreshCount, aRegistration.mDeletedCount, aRegistration.mLeaseTimeTotal,
aRegistration.mKeyLeaseTimeTotal, aRegistration.mRemainingLeaseTimeTotal,
aRegistration.mRemainingKeyLeaseTimeTotal);

VerifyOrExit(dbus_message_iter_get_arg_type(aIter) == DBUS_TYPE_STRUCT, error = OTBR_ERROR_DBUS);
dbus_message_iter_recurse(aIter, &sub);
SuccessOrExit(error = ConvertToTuple(&sub, args));
dbus_message_iter_next(aIter);
exit:
return error;
}

otbrError DBusMessageEncode(DBusMessageIter *aIter, const SrpServerInfo::ResponseCounters &aResponseCounters)
{
DBusMessageIter sub;
otbrError error = OTBR_ERROR_NONE;
auto args = std::tie(aResponseCounters.mSuccess, aResponseCounters.mServerFailure, aResponseCounters.mFormatError,
aResponseCounters.mNameExists, aResponseCounters.mRefused, aResponseCounters.mOther);

VerifyOrExit(dbus_message_iter_open_container(aIter, DBUS_TYPE_STRUCT, nullptr, &sub), error = OTBR_ERROR_DBUS);
SuccessOrExit(error = ConvertToDBusMessage(&sub, args));
VerifyOrExit(dbus_message_iter_close_container(aIter, &sub) == true, error = OTBR_ERROR_DBUS);
exit:
return error;
}

otbrError DBusMessageExtract(DBusMessageIter *aIter, SrpServerInfo::ResponseCounters &aResponseCounters)
{
DBusMessageIter sub;
otbrError error = OTBR_ERROR_NONE;
auto args = std::tie(aResponseCounters.mSuccess, aResponseCounters.mServerFailure, aResponseCounters.mFormatError,
aResponseCounters.mNameExists, aResponseCounters.mRefused, aResponseCounters.mOther);

VerifyOrExit(dbus_message_iter_get_arg_type(aIter) == DBUS_TYPE_STRUCT, error = OTBR_ERROR_DBUS);
dbus_message_iter_recurse(aIter, &sub);
SuccessOrExit(error = ConvertToTuple(&sub, args));
dbus_message_iter_next(aIter);
exit:
return error;
}

otbrError DBusMessageEncode(DBusMessageIter *aIter, const SrpServerInfo &aSrpServerInfo)
{
DBusMessageIter sub;
otbrError error = OTBR_ERROR_NONE;

VerifyOrExit(dbus_message_iter_open_container(aIter, DBUS_TYPE_STRUCT, nullptr, &sub), error = OTBR_ERROR_DBUS);

SuccessOrExit(error = DBusMessageEncode(&sub, aSrpServerInfo.mState));
SuccessOrExit(error = DBusMessageEncode(&sub, aSrpServerInfo.mPort));
SuccessOrExit(error = DBusMessageEncode(&sub, aSrpServerInfo.mAddressMode));
SuccessOrExit(error = DBusMessageEncode(&sub, aSrpServerInfo.mHosts));
SuccessOrExit(error = DBusMessageEncode(&sub, aSrpServerInfo.mServices));
SuccessOrExit(error = DBusMessageEncode(&sub, aSrpServerInfo.mResponseCounters));

VerifyOrExit(dbus_message_iter_close_container(aIter, &sub), error = OTBR_ERROR_DBUS);
exit:
return error;
}

otbrError DBusMessageExtract(DBusMessageIter *aIter, SrpServerInfo &aSrpServerInfo)
{
DBusMessageIter sub;
otbrError error = OTBR_ERROR_NONE;

dbus_message_iter_recurse(aIter, &sub);
SuccessOrExit(error = DBusMessageExtract(&sub, aSrpServerInfo.mState));
SuccessOrExit(error = DBusMessageExtract(&sub, aSrpServerInfo.mPort));
SuccessOrExit(error = DBusMessageExtract(&sub, aSrpServerInfo.mAddressMode));
SuccessOrExit(error = DBusMessageExtract(&sub, aSrpServerInfo.mHosts));
SuccessOrExit(error = DBusMessageExtract(&sub, aSrpServerInfo.mServices));
SuccessOrExit(error = DBusMessageExtract(&sub, aSrpServerInfo.mResponseCounters));

dbus_message_iter_next(aIter);
exit:
return error;
}

} // namespace DBus
} // namespace otbr
57 changes: 57 additions & 0 deletions src/dbus/common/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include <string>
#include <vector>

#include <openthread/srp_server.h>

namespace otbr {
namespace DBus {

Expand Down Expand Up @@ -517,6 +519,61 @@ struct TxtEntry
std::vector<uint8_t> mValue;
};

enum SrpServerState : uint8_t
{
OTBR_SRP_SERVER_STATE_DISABLED = 0, ///< The SRP server is disabled.
OTBR_SRP_SERVER_STATE_RUNNING = 1, ///< The SRP server is running.
OTBR_SRP_SERVER_STATE_STOPPED = 2, ///< The SRP server is stopped.
};

static_assert(OTBR_SRP_SERVER_STATE_DISABLED == static_cast<uint8_t>(OT_SRP_SERVER_STATE_DISABLED),
"OTBR_SRP_SERVER_STATE_DISABLED value is incorrect");
static_assert(OTBR_SRP_SERVER_STATE_RUNNING == static_cast<uint8_t>(OT_SRP_SERVER_STATE_RUNNING),
"OTBR_SRP_SERVER_STATE_RUNNING value is incorrect");
static_assert(OTBR_SRP_SERVER_STATE_STOPPED == static_cast<uint8_t>(OT_SRP_SERVER_STATE_STOPPED),
"OTBR_SRP_SERVER_STATE_STOPPED value is incorrect");

enum SrpServerAddressMode : uint8_t
{
OTBR_SRP_SERVER_ADDRESS_MODE_UNICAST = 0, ///< Unicast address mode.
OTBR_SRP_SERVER_ADDRESS_MODE_ANYCAST = 1, ///< Anycast address mode.
};

static_assert(OTBR_SRP_SERVER_ADDRESS_MODE_UNICAST == static_cast<uint8_t>(OT_SRP_SERVER_ADDRESS_MODE_UNICAST),
"OTBR_SRP_SERVER_ADDRESS_MODE_UNICAST value is incorrect");
static_assert(OTBR_SRP_SERVER_ADDRESS_MODE_ANYCAST == static_cast<uint8_t>(OT_SRP_SERVER_ADDRESS_MODE_ANYCAST),
"OTBR_SRP_SERVER_ADDRESS_MODE_ANYCAST value is incorrect");

struct SrpServerInfo
{
struct Registration
{
uint32_t mFreshCount;
uint32_t mDeletedCount;
uint64_t mLeaseTimeTotal;
uint64_t mKeyLeaseTimeTotal;
uint64_t mRemainingLeaseTimeTotal;
uint64_t mRemainingKeyLeaseTimeTotal;
};

struct ResponseCounters
{
uint32_t mSuccess;
uint32_t mServerFailure;
uint32_t mFormatError;
uint32_t mNameExists;
uint32_t mRefused;
uint32_t mOther;
};

SrpServerState mState;
uint16_t mPort;
SrpServerAddressMode mAddressMode;
Registration mHosts;
Registration mServices;
ResponseCounters mResponseCounters;
};

} // namespace DBus
} // namespace otbr

Expand Down
72 changes: 72 additions & 0 deletions src/dbus/server/dbus_thread_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <openthread/link_raw.h>
#include <openthread/ncp.h>
#include <openthread/netdata.h>
#include <openthread/srp_server.h>
#include <openthread/thread_ftd.h>
#include <openthread/platform/radio.h>

Expand Down Expand Up @@ -213,6 +214,8 @@ otbrError DBusThreadObject::Init(void)
std::bind(&DBusThreadObject::GetActiveDatasetTlvsHandler, this, _1));
RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_RADIO_REGION,
std::bind(&DBusThreadObject::GetRadioRegionHandler, this, _1));
RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_SRP_SERVER_INFO,
std::bind(&DBusThreadObject::GetSrpServerInfoHandler, this, _1));

return error;
}
Expand Down Expand Up @@ -1176,5 +1179,74 @@ otError DBusThreadObject::GetRadioRegionHandler(DBusMessageIter &aIter)
return error;
}

otError DBusThreadObject::GetSrpServerInfoHandler(DBusMessageIter &aIter)
{
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
auto threadHelper = mNcp->GetThreadHelper();
auto instance = threadHelper->GetInstance();
otError error = OT_ERROR_NONE;
SrpServerInfo srpServerInfo{};
otSrpServerLeaseInfo leaseInfo;
const otSrpServerHost * host = nullptr;
const otSrpServerResponseCounters *responseCounters = otSrpServerGetResponseCounters(instance);

srpServerInfo.mState = SrpServerState(static_cast<uint8_t>(otSrpServerGetState(instance)));
srpServerInfo.mPort = otSrpServerGetPort(instance);
srpServerInfo.mAddressMode = SrpServerAddressMode(static_cast<uint8_t>(otSrpServerGetAddressMode(instance)));

while ((host = otSrpServerGetNextHost(instance, host)))
{
const otSrpServerService *service = nullptr;

if (otSrpServerHostIsDeleted(host))
{
++srpServerInfo.mHosts.mDeletedCount;
}
else
{
++srpServerInfo.mHosts.mFreshCount;
otSrpServerHostGetLeaseInfo(host, &leaseInfo);
srpServerInfo.mHosts.mLeaseTimeTotal += leaseInfo.mLease;
srpServerInfo.mHosts.mKeyLeaseTimeTotal += leaseInfo.mKeyLease;
srpServerInfo.mHosts.mRemainingLeaseTimeTotal += leaseInfo.mRemainingLease;
srpServerInfo.mHosts.mRemainingKeyLeaseTimeTotal += leaseInfo.mRemainingKeyLease;
}

while ((service = otSrpServerHostGetNextService(host, service)))
{
if (otSrpServerServiceIsDeleted(service))
{
++srpServerInfo.mServices.mDeletedCount;
}
else
{
++srpServerInfo.mServices.mFreshCount;
otSrpServerServiceGetLeaseInfo(service, &leaseInfo);
srpServerInfo.mServices.mLeaseTimeTotal += leaseInfo.mLease;
srpServerInfo.mServices.mKeyLeaseTimeTotal += leaseInfo.mKeyLease;
srpServerInfo.mServices.mRemainingLeaseTimeTotal += leaseInfo.mRemainingLease;
srpServerInfo.mServices.mRemainingKeyLeaseTimeTotal += leaseInfo.mRemainingKeyLease;
}
}
}

srpServerInfo.mResponseCounters.mSuccess = responseCounters->mSuccess;
srpServerInfo.mResponseCounters.mServerFailure = responseCounters->mServerFailure;
srpServerInfo.mResponseCounters.mFormatError = responseCounters->mFormatError;
srpServerInfo.mResponseCounters.mNameExists = responseCounters->mNameExists;
srpServerInfo.mResponseCounters.mRefused = responseCounters->mRefused;
srpServerInfo.mResponseCounters.mOther = responseCounters->mOther;

VerifyOrExit(DBusMessageEncodeToVariant(&aIter, srpServerInfo) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);

exit:
return error;
#else // OTBR_ENABLE_SRP_ADVERTISING_PROXY
OTBR_UNUSED_VARIABLE(aIter);

return OT_ERROR_NOT_IMPLEMENTED;
#endif // OTBR_ENABLE_SRP_ADVERTISING_PROXY
}

} // namespace DBus
} // namespace otbr
1 change: 1 addition & 0 deletions src/dbus/server/dbus_thread_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class DBusThreadObject : public DBusObject
otError GetOnMeshPrefixesHandler(DBusMessageIter &aIter);
otError GetActiveDatasetTlvsHandler(DBusMessageIter &aIter);
otError GetRadioRegionHandler(DBusMessageIter &aIter);
otError GetSrpServerInfoHandler(DBusMessageIter &aIter);

void ReplyScanResult(DBusRequest &aRequest, otError aError, const std::vector<otActiveScanResult> &aResult);
void ReplyEnergyScanResult(DBusRequest &aRequest, otError aError, const std::vector<otEnergyScanResult> &aResult);
Expand Down
Loading

0 comments on commit dd645be

Please sign in to comment.