Skip to content

Commit

Permalink
[rest] support ereasing all persistent info
Browse files Browse the repository at this point in the history
Add REST API to support erasing all persistent information, effectively
factory resetting the OTBR. The implementation follows the semantic of
the D-Bus API and automatically disables the Thread network.

After erasing all persistent information the dataset is cleared too. So
this allows to build a new Thread network with subsequent use of the
PUT method to the /node/dataset/active endpoint.
  • Loading branch information
agners committed Jun 15, 2023
1 parent 852fc69 commit 6d547cb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/rest/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ paths:
application/json:
schema:
type: object
delete:
tags:
- node
summary: Erase all persistent information, essentially factory reset the Border Router.
responses:
"200":
description: Successful operation
"409":
description: Thread interface is in wrong state.
/node/ba-id:
get:
tags:
Expand Down
38 changes: 34 additions & 4 deletions src/rest/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,46 @@ void Resource::GetNodeInfo(Response &aResponse) const
}
}

void Resource::NodeInfo(const Request &aRequest, Response &aResponse) const
void Resource::DeleteNodeInfo(Response &aResponse) const
{
otbrError error = OTBR_ERROR_NONE;
std::string errorCode;
if (aRequest.GetMethod() == HttpMethod::kGet)

VerifyOrExit(mNcp->GetThreadHelper()->Detach() == OT_ERROR_NONE, error = OTBR_ERROR_INVALID_STATE);
VerifyOrExit(otInstanceErasePersistentInfo(mInstance) == OT_ERROR_NONE, error = OTBR_ERROR_REST);
mNcp->Reset();

exit:
if (error == OTBR_ERROR_NONE)
{
GetNodeInfo(aResponse);
errorCode = GetHttpStatus(HttpStatusCode::kStatusOk);
aResponse.SetResponsCode(errorCode);
}
else
else if (error == OTBR_ERROR_INVALID_STATE)
{
ErrorHandler(aResponse, HttpStatusCode::kStatusConflict);
}
else if (error != OTBR_ERROR_NONE)
{
ErrorHandler(aResponse, HttpStatusCode::kStatusInternalServerError);
}
}

void Resource::NodeInfo(const Request &aRequest, Response &aResponse) const
{
std::string errorCode;

switch (aRequest.GetMethod())
{
case HttpMethod::kGet:
GetNodeInfo(aResponse);
break;
case HttpMethod::kDelete:
DeleteNodeInfo(aResponse);
break;
default:
ErrorHandler(aResponse, HttpStatusCode::kStatusMethodNotAllowed);
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/rest/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Resource
void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse);

void GetNodeInfo(Response &aResponse) const;
void DeleteNodeInfo(Response &aResponse) const;
void GetDataBaId(Response &aResponse) const;
void GetDataExtendedAddr(Response &aResponse) const;
void GetDataState(Response &aResponse) const;
Expand Down
2 changes: 1 addition & 1 deletion src/rest/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_HEADERS \
"Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, " \
"Access-Control-Request-Headers"
#define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_METHOD "GET, OPTIONS, PUT"
#define OT_REST_RESPONSE_ACCESS_CONTROL_ALLOW_METHOD "DELETE, GET, OPTIONS, PUT"
#define OT_REST_RESPONSE_CONNECTION "close"

namespace otbr {
Expand Down

0 comments on commit 6d547cb

Please sign in to comment.