Skip to content

Commit 6fac496

Browse files
committed
[rest] added omr and rloc endpoints. Added ip-address endpoint handler to respond with all ip addresses
1 parent 31e9155 commit 6fac496

File tree

5 files changed

+226
-1
lines changed

5 files changed

+226
-1
lines changed

src/rest/json.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,49 @@ static cJSON *LeaderData2Json(const otLeaderData &aLeaderData)
349349
return leaderData;
350350
}
351351

352+
std::string NetifAddr2JsonString(const otNetifAddress *aAddress)
353+
{
354+
std::string ret;
355+
cJSON *ipAddresses = cJSON_CreateObject();
356+
cJSON *alocAddresses = cJSON_CreateArray();
357+
cJSON *omrAddresses = cJSON_CreateArray();
358+
359+
for (const otNetifAddress *addr = aAddress; addr; addr = addr->mNext)
360+
{
361+
if (addr->mRloc)
362+
{
363+
cJSON_AddItemToObject(ipAddresses, "Rloc", IpAddr2Json(addr->mAddress));
364+
continue;
365+
}
366+
if (addr->mMeshLocal)
367+
{
368+
if (addr->mAddress.mFields.m8[OT_IP6_ADDRESS_SIZE - 2] == 0xfc)
369+
{
370+
cJSON_AddItemToArray(alocAddresses, IpAddr2Json(addr->mAddress));
371+
}
372+
else
373+
{
374+
cJSON_AddItemToObject(ipAddresses, "Mleid", IpAddr2Json(addr->mAddress));
375+
}
376+
continue;
377+
}
378+
if (addr->mAddress.mFields.m16[0] == 0x80fe)
379+
{
380+
cJSON_AddItemToObject(ipAddresses, "LinkLocal", IpAddr2Json(addr->mAddress));
381+
continue;
382+
}
383+
cJSON_AddItemToArray(omrAddresses, IpAddr2Json(addr->mAddress));
384+
}
385+
386+
cJSON_AddItemToObject(ipAddresses, "Omr", omrAddresses);
387+
cJSON_AddItemToObject(ipAddresses, "Aloc", alocAddresses);
388+
389+
ret = Json2String(ipAddresses);
390+
cJSON_Delete(ipAddresses);
391+
392+
return ret;
393+
}
394+
352395
std::string IpAddr2JsonString(const otIp6Address &aAddress)
353396
{
354397
std::string ret;

src/rest/json.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ std::string Node2JsonString(const NodeInfo &aNode);
128128
*/
129129
std::string Diag2JsonString(const std::vector<std::vector<otNetworkDiagTlv>> &aDiagSet);
130130

131+
/**
132+
* This method formats a NetifAddress to a Json object and serialize it to a string.
133+
*
134+
* @param[in] aAddress A pointer to a NetifAddress object.
135+
*
136+
* @returns A string of serialized Json object.
137+
*/
138+
std::string NetifAddr2JsonString(const otNetifAddress *aAddress);
139+
131140
/**
132141
* This method formats an Ipv6Address to a Json string and serialize it to a string.
133142
*

src/rest/openapi.yaml

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ paths:
404404
type: string
405405
description: Coprocessor version string
406406
example: "OPENTHREAD/thread-reference-20200818-1740-g33cc75ed3; NRF52840; Jun 2 2022 14:25:49"
407+
/node/ip-address:
408+
get:
409+
tags:
410+
- node
411+
summary: Get the MLEID ip address
412+
description: Retrieves the border router mesh-local EID IP address.
413+
responses:
414+
"200":
415+
description: Successful operation.
416+
content:
417+
application/json:
418+
schema:
419+
$ref: "#/components/schemas/ip-address"
407420
/node/ip-address/mleid:
408421
get:
409422
tags:
@@ -418,7 +431,37 @@ paths:
418431
schema:
419432
type: string
420433
description: mleid ip address
421-
example: "fd06:1488:f31:d0f9:f4f4:3594:e17c:4882"
434+
example: "fda7:fe3:24a2:dd92:2d48:2a40:b06:83c1"
435+
/node/ip-address/rloc:
436+
get:
437+
tags:
438+
- node
439+
summary: Get the RLOC ip address
440+
description: Retrieves the border router rloc IP address.
441+
responses:
442+
"200":
443+
description: Successful operation.
444+
content:
445+
application/json:
446+
schema:
447+
type: string
448+
description: rloc ip address
449+
example: "fda7:fe3:24a2:dd92:0:ff:fe00:5c00"
450+
/node/ip-address/omr:
451+
get:
452+
tags:
453+
- node
454+
summary: Get the OMR ip address.
455+
description: Retrieves the the preferred off-mesh-routable ip address of the border router.
456+
responses:
457+
"200":
458+
description: Successful operation.
459+
content:
460+
application/json:
461+
schema:
462+
type: string
463+
description: omr ip address
464+
example: "fd0a:901c:88d2:1:74e9:f7b8:f987:c4d5"
422465

423466
components:
424467
schemas:
@@ -618,3 +661,31 @@ components:
618661
type: integer
619662
description: Joiner expiration time in milliseconds on response and seconds on request
620663
default: 60
664+
ip-address:
665+
type: object
666+
properties:
667+
Rloc:
668+
type: string
669+
description: Current RLOC IP address of the border router
670+
example: "fde5:8dba:82e1:1::ff:fe00:1001"
671+
Mleid:
672+
type: string
673+
description: Current MLEID IP address of the border router
674+
example: "fde5:8dba:82e1:1:416:993c:8399:35ab"
675+
LinkLocal:
676+
type: string
677+
description: Current Link Local IP address of the border router
678+
example: "fe80::54db:881c:3845:57f4"
679+
Omr:
680+
type: array
681+
description: Off mesh routable IP addresses of the border router
682+
items:
683+
type: string
684+
example: "2000::54db:881c:3845:57f4"
685+
Aloc:
686+
type: array
687+
description: anycast locator IP addresses of the border router
688+
items:
689+
type: string
690+
example: "fde5:8dba:82e1:1::ff:fe00:fc01"
691+

src/rest/resource.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#define OT_REST_RESOURCE_PATH_NETWORK_CURRENT_PREFIX "/networks/current/prefix"
5858
#define OT_REST_RESOURCE_PATH_NODE_IPADDRESS "/node/ip-address"
5959
#define OT_REST_RESOURCE_PATH_NODE_IPADDRESS_MLEID "/node/ip-address/mleid"
60+
#define OT_REST_RESOURCE_PATH_NODE_IPADDRESS_RLOC "/node/ip-address/rloc"
61+
#define OT_REST_RESOURCE_PATH_NODE_IPADDRESS_OMR "/node/ip-address/omr"
6062

6163
#define OT_REST_HTTP_STATUS_200 "200 OK"
6264
#define OT_REST_HTTP_STATUS_201 "201 Created"
@@ -150,7 +152,10 @@ Resource::Resource(RcpHost *aHost)
150152
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_RLOC, &Resource::Rloc);
151153
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_DATASET_ACTIVE, &Resource::DatasetActive);
152154
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_DATASET_PENDING, &Resource::DatasetPending);
155+
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_IPADDRESS, &Resource::IpAddr);
153156
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_IPADDRESS_MLEID, &Resource::IpAddrMleid);
157+
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_IPADDRESS_RLOC, &Resource::IpAddrRloc);
158+
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_IPADDRESS_OMR, &Resource::IpAddrOmr);
154159
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_COMMISSIONER_STATE, &Resource::CommissionerState);
155160
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_COMMISSIONER_JOINER, &Resource::CommissionerJoiner);
156161
mResourceMap.emplace(OT_REST_RESOURCE_PATH_NODE_COPROCESSOR_VERSION, &Resource::CoprocessorVersion);
@@ -1089,6 +1094,32 @@ void Resource::CoprocessorVersion(const Request &aRequest, Response &aResponse)
10891094
}
10901095
}
10911096

1097+
void Resource::GetIpAddr(Response &aResponse) const
1098+
{
1099+
std::string errorCode;
1100+
std::string addrString;
1101+
const otNetifAddress *unicastAddrs = otIp6GetUnicastAddresses(mInstance);
1102+
1103+
addrString = Json::NetifAddr2JsonString(unicastAddrs);
1104+
aResponse.SetBody(addrString);
1105+
errorCode = GetHttpStatus(HttpStatusCode::kStatusOk);
1106+
aResponse.SetResponsCode(errorCode);
1107+
}
1108+
1109+
void Resource::IpAddr(const Request &aRequest, Response &aResponse) const
1110+
{
1111+
std::string errorCode;
1112+
1113+
if (aRequest.GetMethod() == HttpMethod::kGet)
1114+
{
1115+
GetIpAddr(aResponse);
1116+
}
1117+
else
1118+
{
1119+
ErrorHandler(aResponse, HttpStatusCode::kStatusMethodNotAllowed);
1120+
}
1121+
}
1122+
10921123
void Resource::GetIpAddrMleid(Response &aResponse) const
10931124
{
10941125
std::string errorCode;
@@ -1117,6 +1148,71 @@ void Resource::IpAddrMleid(const Request &aRequest, Response &aResponse) const
11171148
}
11181149
}
11191150

1151+
void Resource::GetIpAddrRloc(Response &aResponse) const
1152+
{
1153+
std::string errorCode;
1154+
std::string rlocJsonString;
1155+
const otIp6Address *rloc;
1156+
1157+
rloc = otThreadGetRloc(mInstance);
1158+
1159+
rlocJsonString = Json::IpAddr2JsonString(*rloc);
1160+
aResponse.SetBody(rlocJsonString);
1161+
errorCode = GetHttpStatus(HttpStatusCode::kStatusOk);
1162+
aResponse.SetResponsCode(errorCode);
1163+
}
1164+
1165+
void Resource::IpAddrRloc(const Request &aRequest, Response &aResponse) const
1166+
{
1167+
std::string errorCode;
1168+
1169+
if (aRequest.GetMethod() == HttpMethod::kGet)
1170+
{
1171+
GetIpAddrRloc(aResponse);
1172+
}
1173+
else
1174+
{
1175+
ErrorHandler(aResponse, HttpStatusCode::kStatusMethodNotAllowed);
1176+
}
1177+
}
1178+
1179+
void Resource::GetIpAddrOmr(Response &aResponse) const
1180+
{
1181+
std::string errorCode;
1182+
std::string omrJsonString;
1183+
const otIp6Address *omr;
1184+
const otNetifAddress *unicastAddrs = otIp6GetUnicastAddresses(mInstance);
1185+
1186+
for (const otNetifAddress *addr = unicastAddrs; addr; addr = addr->mNext)
1187+
{
1188+
if (addr->mMeshLocal || addr->mAddress.mFields.m16[0] == 0x80fe || !addr->mPreferred)
1189+
{
1190+
continue;
1191+
}
1192+
omr = &addr->mAddress;
1193+
}
1194+
1195+
1196+
omrJsonString = Json::IpAddr2JsonString(*omr);
1197+
aResponse.SetBody(omrJsonString);
1198+
errorCode = GetHttpStatus(HttpStatusCode::kStatusOk);
1199+
aResponse.SetResponsCode(errorCode);
1200+
}
1201+
1202+
void Resource::IpAddrOmr(const Request &aRequest, Response &aResponse) const
1203+
{
1204+
std::string errorCode;
1205+
1206+
if (aRequest.GetMethod() == HttpMethod::kGet)
1207+
{
1208+
GetIpAddrOmr(aResponse);
1209+
}
1210+
else
1211+
{
1212+
ErrorHandler(aResponse, HttpStatusCode::kStatusMethodNotAllowed);
1213+
}
1214+
}
1215+
11201216
void Resource::DeleteOutDatedDiagnostic(void)
11211217
{
11221218
auto eraseIt = mDiagSet.begin();

src/rest/resource.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ class Resource
130130
void Diagnostic(const Request &aRequest, Response &aResponse) const;
131131
void HandleDiagnosticCallback(const Request &aRequest, Response &aResponse);
132132
void CoprocessorVersion(const Request &aRequest, Response &aResponse) const;
133+
void IpAddr(const Request &aRequest, Response &aResponse) const;
133134
void IpAddrMleid(const Request &aRequest, Response &aResponse) const;
135+
void IpAddrRloc(const Request &aRequest, Response &aResponse) const;
136+
void IpAddrOmr(const Request &aRequest, Response &aResponse) const;
134137

135138
void GetNodeInfo(Response &aResponse) const;
136139
void DeleteNodeInfo(Response &aResponse) const;
@@ -152,7 +155,10 @@ class Resource
152155
void AddJoiner(const Request &aRequest, Response &aResponse) const;
153156
void RemoveJoiner(const Request &aRequest, Response &aResponse) const;
154157
void GetCoprocessorVersion(Response &aResponse) const;
158+
void GetIpAddr(Response &aResponse) const;
155159
void GetIpAddrMleid(Response &aResponse) const;
160+
void GetIpAddrRloc(Response &aResponse) const;
161+
void GetIpAddrOmr(Response &aResponse) const;
156162

157163
void DeleteOutDatedDiagnostic(void);
158164
void UpdateDiag(std::string aKey, std::vector<otNetworkDiagTlv> &aDiag);

0 commit comments

Comments
 (0)