Skip to content

Commit

Permalink
[rest] return border agent ID (#1864)
Browse files Browse the repository at this point in the history
* [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.
  • Loading branch information
agners committed May 12, 2023
1 parent 7af2ca4 commit b7eacd2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/rest/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
14 changes: 14 additions & 0 deletions src/rest/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 45 additions & 2 deletions src/rest/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<const uint8_t *>(otLinkGetExtendedAddress(mInstance));
Expand Down
3 changes: 3 additions & 0 deletions src/rest/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <unordered_map>

#include <openthread/border_agent.h>
#include <openthread/border_router.h>

#include "ncp/ncp_openthread.hpp"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/rest/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include <string>
#include <vector>

#include <openthread/border_agent.h>

#include "openthread/netdiag.h"

#define OT_REST_ACCEPT_HEADER "Accept"
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b7eacd2

Please sign in to comment.