Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rest] add commissioner api #2515

Merged
merged 19 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
25b31a0
added commissioner openapi specification
OmegaRelay Aug 19, 2024
2616866
implemented commissioner state in the rest api
OmegaRelay Aug 19, 2024
89285d7
implemented commissioner joiner methods in the rest api
OmegaRelay Aug 19, 2024
e6e74ab
added support for joiner discerner
theisHafele Oct 3, 2024
4a6a555
added 204 response for setting commissioner to the state it's already in
theisHafele Oct 3, 2024
8753727
ran make-pretty
theisHafele Oct 3, 2024
73c2853
added a http 507 response code. added a distinction between an invali…
theisHafele Oct 3, 2024
d469d67
removed dataset logging
theisHafele Oct 5, 2024
0831231
updated openapi docs with discerner description
theisHafele Oct 8, 2024
265499a
updated resource error handling with a catchall for internal server e…
theisHafele Oct 8, 2024
cb3ba98
renamed variable otError to errorOt to avoid confusion with typedef
theisHafele Oct 8, 2024
92db6b9
replaced Uint2Hex with Long2Hex and updated Long2Hex implementation t…
theisHafele Oct 8, 2024
d9dd509
Update src/rest/openapi.yaml
theisHafele Oct 24, 2024
d877a15
removed extra error handling for idempotent delete requests
theisHafele Oct 24, 2024
1ef9b5f
fix: ignore return value
theisHafele Oct 26, 2024
f8468af
Apply suggestions from code review
OmegaRelay Oct 29, 2024
6d33dc7
removed magic numbers, reversed ret to be initialised as false in `Js…
theisHafele Oct 29, 2024
8d7051a
replaced long if-else blocks with switch case statements
theisHafele Oct 29, 2024
3756476
fixed 32 char pskds not having null terminator
theisHafele Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/common/api_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,23 @@ std::string GetDhcp6PdStateName(otBorderRoutingDhcp6PdState aState)
return stateName;
}
#endif // OTBR_ENABLE_DHCP6_PD

std::string GetCommissionerStateName(otCommissionerState aState)
{
std::string stateName;

switch (aState)
{
case OT_COMMISSIONER_STATE_DISABLED:
stateName = OTBR_COMMISSIONER_STATE_NAME_DISABLED;
break;
case OT_COMMISSIONER_STATE_PETITION:
stateName = OTBR_COMMISSIONER_STATE_NAME_PETITION;
break;
case OT_COMMISSIONER_STATE_ACTIVE:
stateName = OTBR_COMMISSIONER_STATE_NAME_ACTIVE;
break;
}

return stateName;
}
6 changes: 6 additions & 0 deletions src/common/api_strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@
#define OTBR_DHCP6_PD_STATE_NAME_IDLE "idle"
#endif

#define OTBR_COMMISSIONER_STATE_NAME_DISABLED "disabled"
#define OTBR_COMMISSIONER_STATE_NAME_PETITION "petitioning"
#define OTBR_COMMISSIONER_STATE_NAME_ACTIVE "active"

std::string GetDeviceRoleName(otDeviceRole aRole);

#if OTBR_ENABLE_DHCP6_PD
std::string GetDhcp6PdStateName(otBorderRoutingDhcp6PdState aDhcp6PdState);
#endif // OTBR_ENABLE_DHCP6_PD

std::string GetCommissionerStateName(otCommissionerState aState);

#endif // OTBR_COMMON_API_STRINGS_HPP_
192 changes: 191 additions & 1 deletion src/rest/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
#include <sstream>

#include "common/code_utils.hpp"
#include "common/types.hpp"

extern "C" {
#include <cJSON.h>
}

#ifndef BITS_PER_BYTE
#define BITS_PER_BYTE 8
#endif

namespace otbr {
namespace rest {
namespace Json {
Expand Down Expand Up @@ -926,6 +929,193 @@ bool JsonPendingDatasetString2Dataset(const std::string &aJsonPendingDataset, ot
return ret;
}

cJSON *JoinerInfo2Json(const otJoinerInfo &aJoinerInfo)
{
cJSON *node = cJSON_CreateObject();

cJSON_AddItemToObject(node, "Pskd", cJSON_CreateString(aJoinerInfo.mPskd.m8));
if (aJoinerInfo.mType == OT_JOINER_INFO_TYPE_EUI64)
{
cJSON_AddItemToObject(node, "Eui64", Bytes2HexJson(aJoinerInfo.mSharedId.mEui64.m8, OT_EXT_ADDRESS_SIZE));
}
else if (aJoinerInfo.mType == OT_JOINER_INFO_TYPE_DISCERNER)
{
char hexValue[((OT_JOINER_MAX_DISCERNER_LENGTH / 8) * 2) + 1] = {0};
char string[sizeof("0x") + ((OT_JOINER_MAX_DISCERNER_LENGTH / 8) * 2) + sizeof("/xx") + 1] = {0};

otbr::Utils::Long2Hex(aJoinerInfo.mSharedId.mDiscerner.mValue, hexValue);
snprintf(string, sizeof(string), "0x%s/%d", hexValue, aJoinerInfo.mSharedId.mDiscerner.mLength);
cJSON_AddItemToObject(node, "Discerner", cJSON_CreateString(string));
}
else
{
cJSON_AddItemToObject(node, "JoinerId", cJSON_CreateString("*"));
}
cJSON_AddItemToObject(node, "Timeout", cJSON_CreateNumber(aJoinerInfo.mExpirationTime));

return node;
}

std::string JoinerInfo2JsonString(const otJoinerInfo &aJoinerInfo)
{
cJSON *node;
std::string ret;

node = JoinerInfo2Json(aJoinerInfo);
ret = Json2String(node);
cJSON_Delete(node);

return ret;
}

otbrError StringDiscerner2Discerner(char *aString, otJoinerDiscerner &aDiscerner)
{
otbrError error = OTBR_ERROR_NONE;
char *separator;
uint8_t byteLength;
uint8_t byteSwapBuffer[OT_JOINER_MAX_DISCERNER_LENGTH / BITS_PER_BYTE] = {0};
uint8_t *buffer = (uint8_t *)&aDiscerner.mValue;

separator = strstr(aString, "/");
VerifyOrExit(separator != nullptr, error = OTBR_ERROR_NOT_FOUND);
VerifyOrExit(sscanf(separator + 1, "%hhu", &aDiscerner.mLength) == 1, error = OTBR_ERROR_INVALID_ARGS);
VerifyOrExit(aDiscerner.mLength > 0 && aDiscerner.mLength <= OT_JOINER_MAX_DISCERNER_LENGTH,
error = OTBR_ERROR_INVALID_ARGS);

if (memcmp(aString, "0x", 2) == 0)
{
aString += 2;
}

*separator = '\0';
byteLength = Hex2BytesJsonString(std::string(aString), byteSwapBuffer, OT_JOINER_MAX_DISCERNER_LENGTH);
VerifyOrExit(byteLength <= (1 + ((aDiscerner.mLength - 1) / BITS_PER_BYTE)), error = OTBR_ERROR_INVALID_ARGS);

// The discerner is expected to be big endian
for (uint8_t i = 0; i < byteLength; i++)
{
buffer[i] = byteSwapBuffer[byteLength - i - 1];
}

exit:
return error;
}

bool JsonJoinerInfo2JoinerInfo(const cJSON *jsonJoinerInfo, otJoinerInfo &aJoinerInfo)
{
cJSON *value;
bool ret = false;

aJoinerInfo.mType = OT_JOINER_INFO_TYPE_ANY;
memset(&aJoinerInfo.mSharedId.mEui64, 0, sizeof(aJoinerInfo.mSharedId.mEui64));
memset(&aJoinerInfo.mPskd.m8, 0, sizeof(aJoinerInfo.mPskd.m8));

value = cJSON_GetObjectItemCaseSensitive(jsonJoinerInfo, "Pskd");
if (cJSON_IsString(value))
{
VerifyOrExit(value->valuestring != nullptr);
VerifyOrExit(strlen(value->valuestring) <= OT_JOINER_MAX_PSKD_LENGTH);
strncpy(aJoinerInfo.mPskd.m8, value->valuestring, OT_JOINER_MAX_PSKD_LENGTH);
}
else
{
ExitNow();
}

value = cJSON_GetObjectItemCaseSensitive(jsonJoinerInfo, "JoinerId");
if (cJSON_IsString(value))
{
VerifyOrExit(aJoinerInfo.mType == OT_JOINER_INFO_TYPE_ANY);
VerifyOrExit(value->valuestring != nullptr);
if (strncmp(value->valuestring, "*", 1) != 0)
{
otbrError err = StringDiscerner2Discerner(value->valuestring, aJoinerInfo.mSharedId.mDiscerner);
if (err == OTBR_ERROR_NOT_FOUND)
{
VerifyOrExit(Hex2BytesJsonString(std::string(value->valuestring), aJoinerInfo.mSharedId.mEui64.m8,
OT_EXT_ADDRESS_SIZE) == OT_EXT_ADDRESS_SIZE);
aJoinerInfo.mType = OT_JOINER_INFO_TYPE_EUI64;
}
else
{
VerifyOrExit(err == OTBR_ERROR_NONE);
aJoinerInfo.mType = OT_JOINER_INFO_TYPE_DISCERNER;
}
}
}

value = cJSON_GetObjectItemCaseSensitive(jsonJoinerInfo, "Discerner");
if (cJSON_IsString(value))
{
VerifyOrExit(aJoinerInfo.mType == OT_JOINER_INFO_TYPE_ANY);
VerifyOrExit(value->valuestring != nullptr);
if (strncmp(value->valuestring, "*", 1) != 0)
{
VerifyOrExit(StringDiscerner2Discerner(value->valuestring, aJoinerInfo.mSharedId.mDiscerner) ==
OTBR_ERROR_NONE);
aJoinerInfo.mType = OT_JOINER_INFO_TYPE_DISCERNER;
}
}

value = cJSON_GetObjectItemCaseSensitive(jsonJoinerInfo, "Eui64");
if (cJSON_IsString(value))
{
VerifyOrExit(aJoinerInfo.mType == OT_JOINER_INFO_TYPE_ANY);
VerifyOrExit(value->valuestring != nullptr);
if (strncmp(value->valuestring, "*", 1) != 0)
{
VerifyOrExit(Hex2BytesJsonString(std::string(value->valuestring), aJoinerInfo.mSharedId.mEui64.m8,
OT_EXT_ADDRESS_SIZE) == OT_EXT_ADDRESS_SIZE);
aJoinerInfo.mType = OT_JOINER_INFO_TYPE_EUI64;
}
}

aJoinerInfo.mExpirationTime = 60;
value = cJSON_GetObjectItemCaseSensitive(jsonJoinerInfo, "Timeout");
if (cJSON_IsNumber(value))
{
aJoinerInfo.mExpirationTime = value->valueint;
}

ret = true;
exit:
return ret;
}

bool JsonJoinerInfoString2JoinerInfo(const std::string &aJsonJoinerInfo, otJoinerInfo &aJoinerInfo)
{
cJSON *jsonJoinerInfo;
bool ret = true;

VerifyOrExit((jsonJoinerInfo = cJSON_Parse(aJsonJoinerInfo.c_str())) != nullptr, ret = false);
VerifyOrExit(cJSON_IsObject(jsonJoinerInfo), ret = false);

ret = JsonJoinerInfo2JoinerInfo(jsonJoinerInfo, aJoinerInfo);

exit:
cJSON_Delete(jsonJoinerInfo);

return ret;
}

cJSON *JoinerTable2Json(const std::vector<otJoinerInfo> &aJoinerTable)
{
cJSON *table = cJSON_CreateArray();

for (const otJoinerInfo joiner : aJoinerTable)
{
cJSON *joinerJson = JoinerInfo2Json(joiner);
cJSON_AddItemToArray(table, joinerJson);
}

return table;
}

std::string JoinerTable2JsonString(const std::vector<otJoinerInfo> &aJoinerTable)
{
return Json2String(JoinerTable2Json(aJoinerTable));
}

} // namespace Json
} // namespace rest
} // namespace otbr
9 changes: 9 additions & 0 deletions src/rest/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "openthread/link.h"
#include "openthread/thread_ftd.h"

#include "common/types.hpp"
#include "rest/types.hpp"
#include "utils/hex.hpp"

Expand Down Expand Up @@ -251,6 +252,14 @@ bool JsonActiveDatasetString2Dataset(const std::string &aJsonActiveDataset, otOp
*/
bool JsonPendingDatasetString2Dataset(const std::string &aJsonPendingDataset, otOperationalDataset &aDataset);

std::string JoinerInfo2JsonString(const otJoinerInfo &aJoinerInfo);

otbrError StringDiscerner2Discerner(char *aString, otJoinerDiscerner &aDiscerner);

bool JsonJoinerInfoString2JoinerInfo(const std::string &aJsonJoinerInfo, otJoinerInfo &aJoinerInfo);

std::string JoinerTable2JsonString(const std::vector<otJoinerInfo> &aJoinerTable);

}; // namespace Json

} // namespace rest
Expand Down
Loading
Loading