Skip to content

Commit

Permalink
[rest] add coprocessor version string API (#2602)
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly authored Jan 14, 2025
1 parent 8bdfb6b commit b067e5a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/rest/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,21 @@ paths:
description: Invalid request body.
"409":
description: request rejected because commissioner is not active.
/node/coprocessor/version:
get:
tags:
- node
summary: Get the coprocessor firmware version
description: Retrieves the NCP or RCP coprocessor firmware version string.
responses:
"200":
description: Successful operation
content:
application/json:
schema:
type: string
description: Coprocessor version string
example: "OPENTHREAD/thread-reference-20200818-1740-g33cc75ed3; NRF52840; Jun 2 2022 14:25:49"

components:
schemas:
Expand Down
30 changes: 30 additions & 0 deletions src/rest/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
#define OT_REST_RESOURCE_PATH_NODE_DATASET_PENDING "/node/dataset/pending"
#define OT_REST_RESOURCE_PATH_NODE_COMMISSIONER_STATE "/node/commissioner/state"
#define OT_REST_RESOURCE_PATH_NODE_COMMISSIONER_JOINER "/node/commissioner/joiner"
#define OT_REST_RESOURCE_PATH_NODE_COPROCESSOR "/node/coprocessor"
#define OT_REST_RESOURCE_PATH_NODE_COPROCESSOR_VERSION "/node/coprocessor/version"
#define OT_REST_RESOURCE_PATH_NETWORK "/networks"
#define OT_REST_RESOURCE_PATH_NETWORK_CURRENT "/networks/current"
#define OT_REST_RESOURCE_PATH_NETWORK_CURRENT_COMMISSION "/networks/commission"
Expand Down Expand Up @@ -148,6 +150,7 @@ Resource::Resource(RcpHost *aHost)
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_DATASET_PENDING, &Resource::DatasetPending);
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_COMMISSIONER_STATE, &Resource::CommissionerState);
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_COMMISSIONER_JOINER, &Resource::CommissionerJoiner);
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_COPROCESSOR_VERSION, &Resource::CoprocessorVersion);

// Resource callback handler
mResourceCallbackMap.emplace(OT_REST_RESOURCE_PATH_DIAGNOSTICS, &Resource::HandleDiagnosticCallback);
Expand Down Expand Up @@ -1056,6 +1059,33 @@ void Resource::CommissionerJoiner(const Request &aRequest, Response &aResponse)
}
}

void Resource::GetCoprocessorVersion(Response &aResponse) const
{
std::string coprocessorVersion;
std::string errorCode;

coprocessorVersion = mHost->GetCoprocessorVersion();
coprocessorVersion = Json::String2JsonString(coprocessorVersion);

aResponse.SetBody(coprocessorVersion);
errorCode = GetHttpStatus(HttpStatusCode::kStatusOk);
aResponse.SetResponsCode(errorCode);
}

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

if (aRequest.GetMethod() == HttpMethod::kGet)
{
GetCoprocessorVersion(aResponse);
}
else
{
ErrorHandler(aResponse, HttpStatusCode::kStatusMethodNotAllowed);
}
}

void Resource::DeleteOutDatedDiagnostic(void)
{
auto eraseIt = mDiagSet.begin();
Expand Down
2 changes: 2 additions & 0 deletions src/rest/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class Resource
void CommissionerJoiner(const Request &aRequest, Response &aResponse) const;
void Diagnostic(const Request &aRequest, Response &aResponse) const;
void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse);
void CoprocessorVersion(const Request &aRequest, Response &aResponse) const;

void GetNodeInfo(Response &aResponse) const;
void DeleteNodeInfo(Response &aResponse) const;
Expand All @@ -149,6 +150,7 @@ class Resource
void GetJoiners(Response &aResponse) const;
void AddJoiner(const Request &aRequest, Response &aResponse) const;
void RemoveJoiner(const Request &aRequest, Response &aResponse) const;
void GetCoprocessorVersion(Response &aResponse) const;

void DeleteOutDatedDiagnostic(void);
void UpdateDiag(std::string aKey, std::vector<otNetworkDiagTlv> &aDiag);
Expand Down
21 changes: 21 additions & 0 deletions tests/rest/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ def node_ext_panid_check(data):
return True


def node_coprocessor_version_check(data):
assert data is not None

assert (type(data) == str)

return True


def node_test(thread_num):
url = rest_api_addr + "/node"

Expand Down Expand Up @@ -406,6 +414,18 @@ def node_ext_panid_test(thread_num):
print(" /node/ext-panid : all {}, valid {} ".format(thread_num, valid))


def node_coprocessor_version_test(thread_num):
url = rest_api_addr + "/node/coprocessor/version"

response_data = [None] * thread_num

create_multi_thread(get_data_from_url, url, thread_num, response_data)

valid = [node_coprocessor_version_check(data) for data in response_data].count(True)

print(" /node/coprocessor/version : all {}, valid {} ".format(thread_num, valid))


def diagnostics_test(thread_num):
url = rest_api_addr + "/diagnostics"

Expand Down Expand Up @@ -450,6 +470,7 @@ def main():
node_leader_data_test(200)
node_num_of_router_test(200)
node_ext_panid_test(200)
node_coprocessor_version_test(200)
diagnostics_test(20)
error_test(10)

Expand Down

0 comments on commit b067e5a

Please sign in to comment.