From b7eacd297cbdca742838600818d201412dcf6417 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Fri, 12 May 2023 19:38:26 +0200 Subject: [PATCH] [rest] return border agent ID (#1864) * [rest] return node information when detached Ignore error return of otThreadGetLeaderData(). It only returns an error when the Thread border router is detached, which we don't want to consider as an error condition. Instead, make sure the NodeInfo struct is cleared. This makes sure that we don't leak stack data. * [rest] return border agent ID Add BaId to the node JSON object and add a dedicated endpoint /node/ba-id to retrive the border agent ID. --- src/rest/json.cpp | 1 + src/rest/openapi.yaml | 14 +++++++++++++ src/rest/resource.cpp | 47 +++++++++++++++++++++++++++++++++++++++++-- src/rest/resource.hpp | 3 +++ src/rest/types.hpp | 3 +++ 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/rest/json.cpp b/src/rest/json.cpp index c59528bb6a4..8aeb89a414d 100644 --- a/src/rest/json.cpp +++ b/src/rest/json.cpp @@ -346,6 +346,7 @@ std::string Node2JsonString(const NodeInfo &aNode) cJSON *node = cJSON_CreateObject(); std::string ret; + cJSON_AddItemToObject(node, "BaId", Bytes2HexJson(aNode.mBaId, OT_BORDER_AGENT_ID_LENGTH)); cJSON_AddItemToObject(node, "State", cJSON_CreateNumber(aNode.mRole)); cJSON_AddItemToObject(node, "NumOfRouter", cJSON_CreateNumber(aNode.mNumOfRouter)); cJSON_AddItemToObject(node, "RlocAddress", IpAddr2Json(aNode.mRlocAddress)); diff --git a/src/rest/openapi.yaml b/src/rest/openapi.yaml index d2bb2f00261..f3a4f446d63 100644 --- a/src/rest/openapi.yaml +++ b/src/rest/openapi.yaml @@ -43,6 +43,20 @@ paths: application/json: schema: type: object + /node/ba-id: + get: + tags: + - node + summary: Get the border agent ID + responses: + "200": + description: Successful operation + content: + application/json: + schema: + type: string + description: 16 byte border agent ID as hex string. + example: "AA897CA8A67F6E6DD6166133AD1562A5" /node/rloc: get: tags: diff --git a/src/rest/resource.cpp b/src/rest/resource.cpp index dc25ad812be..4c8d2573928 100644 --- a/src/rest/resource.cpp +++ b/src/rest/resource.cpp @@ -37,6 +37,7 @@ #define OT_REST_RESOURCE_PATH_DIAGNOSTICS "/diagnostics" #define OT_REST_RESOURCE_PATH_NODE "/node" +#define OT_REST_RESOURCE_PATH_NODE_BAID "/node/ba-id" #define OT_REST_RESOURCE_PATH_NODE_RLOC "/node/rloc" #define OT_REST_RESOURCE_PATH_NODE_RLOC16 "/node/rloc16" #define OT_REST_RESOURCE_PATH_NODE_EXTADDRESS "/node/ext-address" @@ -129,6 +130,7 @@ Resource::Resource(ControllerOpenThread *aNcp) // Resource Handler mResourceMap.emplace(OT_REST_RESOURCE_PATH_DIAGNOSTICS, &Resource::Diagnostic); mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE, &Resource::NodeInfo); + mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_BAID, &Resource::BaId); mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_STATE, &Resource::State); mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_EXTADDRESS, &Resource::ExtendedAddr); mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_NETWORKNAME, &Resource::NetworkName); @@ -215,13 +217,15 @@ void Resource::ErrorHandler(Response &aResponse, HttpStatusCode aErrorCode) cons void Resource::GetNodeInfo(Response &aResponse) const { otbrError error = OTBR_ERROR_NONE; - struct NodeInfo node; + struct NodeInfo node = {}; otRouterInfo routerInfo; uint8_t maxRouterId; std::string body; std::string errorCode; + uint16_t idLength = OT_BORDER_AGENT_ID_LENGTH; - VerifyOrExit(otThreadGetLeaderData(mInstance, &node.mLeaderData) == OT_ERROR_NONE, error = OTBR_ERROR_REST); + VerifyOrExit(otBorderAgentGetId(mInstance, node.mBaId, &idLength) == OT_ERROR_NONE, error = OTBR_ERROR_REST); + (void)otThreadGetLeaderData(mInstance, &node.mLeaderData); node.mNumOfRouter = 0; maxRouterId = otThreadGetMaxRouterId(mInstance); @@ -269,6 +273,45 @@ void Resource::NodeInfo(const Request &aRequest, Response &aResponse) const } } +void Resource::GetDataBaId(Response &aResponse) const +{ + otbrError error = OTBR_ERROR_NONE; + uint8_t id[OT_BORDER_AGENT_ID_LENGTH]; + uint16_t idLength = OT_BORDER_AGENT_ID_LENGTH; + std::string body; + std::string errorCode; + + VerifyOrExit(otBorderAgentGetId(mInstance, id, &idLength) == OT_ERROR_NONE, error = OTBR_ERROR_REST); + + body = Json::Bytes2HexJsonString(id, idLength); + aResponse.SetBody(body); + +exit: + if (error == OTBR_ERROR_NONE) + { + errorCode = GetHttpStatus(HttpStatusCode::kStatusOk); + aResponse.SetResponsCode(errorCode); + } + else + { + ErrorHandler(aResponse, HttpStatusCode::kStatusInternalServerError); + } +} + +void Resource::BaId(const Request &aRequest, Response &aResponse) const +{ + std::string errorCode; + + if (aRequest.GetMethod() == HttpMethod::kGet) + { + GetDataBaId(aResponse); + } + else + { + ErrorHandler(aResponse, HttpStatusCode::kStatusMethodNotAllowed); + } +} + void Resource::GetDataExtendedAddr(Response &aResponse) const { const uint8_t *extAddress = reinterpret_cast(otLinkGetExtendedAddress(mInstance)); diff --git a/src/rest/resource.hpp b/src/rest/resource.hpp index 8dc43f22d2e..7b5e592a9d1 100644 --- a/src/rest/resource.hpp +++ b/src/rest/resource.hpp @@ -36,6 +36,7 @@ #include +#include #include #include "ncp/ncp_openthread.hpp" @@ -117,6 +118,7 @@ class Resource typedef void (Resource::*ResourceHandler)(const Request &aRequest, Response &aResponse) const; typedef void (Resource::*ResourceCallbackHandler)(const Request &aRequest, Response &aResponse); void NodeInfo(const Request &aRequest, Response &aResponse) const; + void BaId(const Request &aRequest, Response &aResponse) const; void ExtendedAddr(const Request &aRequest, Response &aResponse) const; void State(const Request &aRequest, Response &aResponse) const; void NetworkName(const Request &aRequest, Response &aResponse) const; @@ -132,6 +134,7 @@ class Resource void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse); void GetNodeInfo(Response &aResponse) const; + void GetDataBaId(Response &aResponse) const; void GetDataExtendedAddr(Response &aResponse) const; void GetDataState(Response &aResponse) const; void GetDataNetworkName(Response &aResponse) const; diff --git a/src/rest/types.hpp b/src/rest/types.hpp index 8216a978d79..0723661a6b2 100644 --- a/src/rest/types.hpp +++ b/src/rest/types.hpp @@ -38,6 +38,8 @@ #include #include +#include + #include "openthread/netdiag.h" #define OT_REST_ACCEPT_HEADER "Accept" @@ -96,6 +98,7 @@ enum class ConnectionState : std::uint8_t }; struct NodeInfo { + uint8_t mBaId[OT_BORDER_AGENT_ID_LENGTH]; uint32_t mRole; uint32_t mNumOfRouter; uint16_t mRloc16;