Skip to content

Commit

Permalink
[rest] add api/actions 'addThreadDeviceTask' in openapi.yaml specific…
Browse files Browse the repository at this point in the history
…ation

also includes bug fixes.
  • Loading branch information
martinzi committed Nov 4, 2024
1 parent 594a975 commit 08f6c06
Show file tree
Hide file tree
Showing 19 changed files with 345 additions and 105 deletions.
64 changes: 32 additions & 32 deletions src/rest/extensions/commissioner_allow_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ extern "C" {
#define COMMISSIONER_START_WAIT_TIME_MS 100
#define COMMISSIONER_START_MAX_ATTEMPTS 5

static otInstance *mInstance;
static allow_list::LinkedList<AllowListEntry> AllowListEntryList;
static void consoleEntryPrint(AllowListEntry *aEntry);

Expand Down Expand Up @@ -104,11 +105,11 @@ AllowListEntry *entryEui64Find(const otExtAddress *aEui64)
return entry;
}

otError allowListCommissionerJoinerAdd(otExtAddress aEui64,
uint32_t aTimeout,
char *aPskd,
otInstance *aInstance,
uuid_t uuid)
otError allowListCommissionerJoinerAdd(otExtAddress aEui64,
uint32_t aTimeout,
char *aPskd,
otInstance *aInstance,
otbr::rest::uuid_t uuid)
{
otError error;
AllowListEntry *entry = nullptr;
Expand Down Expand Up @@ -197,8 +198,8 @@ AllowListEntry *parse_buf_as_json(char *aBuf)
otExtAddress eui64;
uint32_t timeout = 0;
AllowListEntry::AllowListEntryState state = AllowListEntry::kAllowListEntryNew;
UUID uuid_obj;
uuid_t uuid;
otbr::rest::UUID uuid_obj;
otbr::rest::uuid_t uuid;
char *uuid_str = nullptr;
char *eui64_str = nullptr;
char *pskdValue = nullptr;
Expand Down Expand Up @@ -274,10 +275,10 @@ AllowListEntry *parse_buf_as_json(char *aBuf)
return pEntry;
}

void allowListAddDevice(otExtAddress aEui64, uint32_t aTimeout, char *aPskd, uuid_t aUuid)
void allowListAddDevice(otExtAddress aEui64, uint32_t aTimeout, char *aPskd, otbr::rest::uuid_t aUuid)
{
assert(nullptr != aPskd);

otError error;
AllowListEntry *pEntry = entryEui64Find(&aEui64);

int pskd_len = strlen(aPskd);
Expand All @@ -294,19 +295,18 @@ void allowListAddDevice(otExtAddress aEui64, uint32_t aTimeout, char *aPskd, uui
}
else
{
// TODO if (aUuid == NULL)
//{
// // this may not be needed
// uuid_generate_random(&aUuid);
// }
pEntry = new AllowListEntry(aEui64, aUuid, aTimeout, pskd_new);
if (nullptr == pEntry)
{
// otbrLogErr("%s: Err creating a new AllowListEntry", __func__);
otbrLogErr("%s: Err creating a new AllowListEntry", __func__);
free(pskd_new);
return;
}
AllowListEntryList.Add(*pEntry);
error = AllowListEntryList.Add(*pEntry);
if (error != OT_ERROR_NONE)
{
otbrLogWarning("%s: already have AllowListEntry", __func__);
}
}

consoleEntryPrint(pEntry);
Expand All @@ -322,7 +322,7 @@ static void consoleEntryPrint(AllowListEntry *aEntry)
assert(nullptr != aEntry);
// char uuidStr[UUID_STR_LEN] = {0};

UUID uuid_obj = UUID();
otbr::rest::UUID uuid_obj = otbr::rest::UUID();
uuid_obj.setUuid(aEntry->muuid);

// uuid_unparse(aEntry->muuid, uuidStr);
Expand Down Expand Up @@ -432,7 +432,7 @@ uint8_t allowListGetPendingJoinersCount(void)

while (entry)
{
if ((AllowListEntry::kAllowListEntryJoined != entry->mstate) ||
if ((AllowListEntry::kAllowListEntryJoined != entry->mstate) &&
(AllowListEntry::kAllowListEntryJoinFailed != entry->mstate))
{
pendingJoinersCount++;
Expand All @@ -454,6 +454,8 @@ void HandleJoinerEvent(otCommissionerJoinerEvent aEvent,
AllowListEntry *entry = nullptr;
uint8_t pendingDevicesCount = 0;

otError error = OT_ERROR_NONE;

// @note: Thread may call this for joiners that we are not supposed to join
// do not assume `entry` is not null in the rest of the code.
entry = entryEui64Find(&aJoinerInfo->mSharedId.mEui64);
Expand Down Expand Up @@ -510,7 +512,8 @@ void HandleJoinerEvent(otCommissionerJoinerEvent aEvent,
// If all entries have been attempted and nothing is pending, stop the commissioner
if (0 == pendingDevicesCount)
{
allowListCommissionerStopPost();
error = allowListCommissionerStopPost();
otbrLogWarning("Commissioner Stop: %s", otThreadErrorToString(error));
}
else
{
Expand All @@ -525,6 +528,7 @@ void HandleJoinerEvent(otCommissionerJoinerEvent aEvent,
otError allowListCommissionerStart(otInstance *aInstance)
{
otError error = OT_ERROR_FAILED;
mInstance = aInstance;

error = otCommissionerStart(aInstance, &HandleStateChanged, &HandleJoinerEvent, NULL);

Expand All @@ -533,33 +537,29 @@ otError allowListCommissionerStart(otInstance *aInstance)

otError allowListCommissionerStopPost(void)
{
return OT_ERROR_NONE;
otError error = OT_ERROR_FAILED;

error = otCommissionerStop(mInstance);

return error;
}

cJSON *AllowListEntry::Allow_list_entry_as_CJSON(const char *entryType)
{
assert(nullptr != entryType);

cJSON *entry_json_obj = nullptr;
// cJSON *hasActivationKey = nullptr;
cJSON *entry_json_obj = nullptr;
cJSON *attributes_json_obj = nullptr;
char eui64_str[17] = {0};
// char uuid_str[UUID_STR_LEN] = {0};

UUID uuid_obj = UUID();

// hasActivationKey = cJSON_CreateObject();
otbr::rest::UUID uuid_obj = otbr::rest::UUID();

memset(eui64_str, 0, sizeof(eui64_str));
sprintf(eui64_str, "%02x%02x%02x%02x%02x%02x%02x%02x", meui64.m8[0], meui64.m8[1], meui64.m8[2], meui64.m8[3],
meui64.m8[4], meui64.m8[5], meui64.m8[6], meui64.m8[7]);
snprintf(eui64_str, sizeof(eui64_str), "%02x%02x%02x%02x%02x%02x%02x%02x", meui64.m8[0], meui64.m8[1], meui64.m8[2],
meui64.m8[3], meui64.m8[4], meui64.m8[5], meui64.m8[6], meui64.m8[7]);

// memset(uuid_str, 0, sizeof(uuid_str));
// uuid_unparse(muuid, uuid_str);
uuid_obj.setUuid(muuid);

attributes_json_obj = cJSON_CreateObject();
// cJSON_AddItemToObject(attributes_json_obj, "hasActivationKey", hasActivationKey);
cJSON_AddItemToObject(attributes_json_obj, "eui", cJSON_CreateString(eui64_str));
cJSON_AddItemToObject(attributes_json_obj, "pskd", cJSON_CreateString(mPSKd));

Expand Down
24 changes: 14 additions & 10 deletions src/rest/extensions/commissioner_allow_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class AllowListEntry : public allow_list::LinkedListEntry<AllowListEntry>
/**
* This constructor creates an AllowListEntry.
*/
AllowListEntry(otExtAddress aEui64, uuid_t uuid, uint32_t aTimeout, char *aPskd)
AllowListEntry(otExtAddress aEui64, otbr::rest::uuid_t uuid, uint32_t aTimeout, char *aPskd)
{
meui64 = aEui64;
muuid = uuid;
Expand All @@ -101,7 +101,11 @@ class AllowListEntry : public allow_list::LinkedListEntry<AllowListEntry>
mNext = nullptr;
}

AllowListEntry(otExtAddress aEui64, uuid_t uuid, uint32_t aTimeout, AllowListEntryState state, char *aPskd)
AllowListEntry(otExtAddress aEui64,
otbr::rest::uuid_t uuid,
uint32_t aTimeout,
AllowListEntryState state,
char *aPskd)
{
meui64 = aEui64;
muuid = uuid;
Expand Down Expand Up @@ -136,7 +140,7 @@ class AllowListEntry : public allow_list::LinkedListEntry<AllowListEntry>

// Members
otExtAddress meui64;
uuid_t muuid;
otbr::rest::uuid_t muuid;
uint32_t mTimeout;
char *mPSKd;
AllowListEntryState mstate;
Expand Down Expand Up @@ -180,11 +184,11 @@ bool eui64IsNull(const otExtAddress aEui64);
* is not set. OT_ERROR_INVALID_STATE On-Mesh commissioner is not active.
*
*/
otError allowListCommissionerJoinerAdd(otExtAddress aEui64,
uint32_t aTimeout,
char *aPskd,
otInstance *aInstance,
uuid_t uuid);
otError allowListCommissionerJoinerAdd(otExtAddress aEui64,
uint32_t aTimeout,
char *aPskd,
otInstance *aInstance,
otbr::rest::uuid_t uuid);

/**
* @brief Remove a single entry from On-Mesh commissioner joiner table
Expand Down Expand Up @@ -216,7 +220,7 @@ otError allowListEntryErase(otExtAddress aEui64);
* automatically removed, in seconds.
* @param[in] aPskd Pskd to use when joinig the device
*/
void allowListAddDevice(otExtAddress aEui64, uint32_t aTimeout, char *aPskd, uuid_t uuid);
void allowListAddDevice(otExtAddress aEui64, uint32_t aTimeout, char *aPskd, otbr::rest::uuid_t uuid);

/**
* @brief Print Allow List Entries available in memory to console
Expand Down Expand Up @@ -270,4 +274,4 @@ otError allowListEntryJoinStatusGet(const otExtAddress *eui64);
}
#endif

#endif /* EXTENSIONS_COMMISSIONER_ALLOW_LIST_HPP_ */
#endif // EXTENSIONS_COMMISSIONER_ALLOW_LIST_HPP_
9 changes: 7 additions & 2 deletions src/rest/extensions/rest_server_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
*/
#include "rest_server_common.hpp"

namespace otbr {
namespace rest {

#ifdef __cplusplus
extern "C" {
#endif

#include <cJSON.h>
#include <ctype.h>
#include <openthread/logging.h>

Expand All @@ -59,7 +61,7 @@ void combineMeshLocalPrefixAndIID(const otMeshLocalPrefix *meshLocalPrefi
}

// count number of 1s in bitmask
int my_count_ones(uint32_t bitmask)
int count_ones(uint32_t bitmask)
{
int count = 0;
while (bitmask)
Expand Down Expand Up @@ -166,3 +168,6 @@ bool is_hex_string(char *str)
#ifdef __cplusplus
}
#endif

} // namespace rest
} // namespace otbr
10 changes: 8 additions & 2 deletions src/rest/extensions/rest_server_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

#include "utils/thread_helper.hpp"

namespace otbr {
namespace rest {

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -64,7 +67,7 @@ void combineMeshLocalPrefixAndIID(const otMeshLocalPrefix *meshLocalPrefi
otIp6Address *ip6Address);

// count number of 1s in bitmask
int my_count_ones(uint32_t bitmask);
int count_ones(uint32_t bitmask);

uint8_t joiner_verify_pskd(char *pskd);

Expand Down Expand Up @@ -94,4 +97,7 @@ bool is_hex_string(char *str);
} // end of extern "C"
#endif

#endif
} // namespace rest
} // namespace otbr

#endif // REST_SERVER_COMMON_HPP_
28 changes: 18 additions & 10 deletions src/rest/extensions/rest_task_add_thread_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ uint32_t getJoinerExpirationTime(otExtAddress *aEui);
cJSON *jsonify_add_thread_device_task(task_node_t *task_node)
{
otExtAddress eui64 = {0};
otError error = OT_ERROR_NONE;

cJSON *task_json = task_node_to_json(task_node);
cJSON *attributes = cJSON_GetObjectItemCaseSensitive(task_json, "attributes");
Expand All @@ -66,7 +67,7 @@ cJSON *jsonify_add_thread_device_task(task_node_t *task_node)
if ((task_node->status > ACTIONS_TASK_STATUS_PENDING) && (task_node->status != ACTIONS_TASK_STATUS_UNIMPLEMENTED))
{
// find allowListEntry and get more detailed status
str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE);
SuccessOrExit(error = otbr::rest::str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE));

if (entryEui64Find(&eui64) != nullptr)
{
Expand All @@ -81,6 +82,12 @@ cJSON *jsonify_add_thread_device_task(task_node_t *task_node)
cJSON_Print(attributes));
}
}
exit:
if (error != OT_ERROR_NONE)
{
otbrLogWarning("%s:%d - %s - missing or bad value in a field: %s", __FILE__, __LINE__, __func__,
cJSON_Print(attributes));
}
return task_json;
}

Expand All @@ -95,14 +102,15 @@ uint8_t validate_add_thread_device_task(cJSON *attributes)

VerifyOrExit((NULL != timeout && cJSON_IsNumber(timeout)), error = OT_ERROR_FAILED);

VerifyOrExit(
(NULL != eui && cJSON_IsString(eui) && 16 == strlen(eui->valuestring) && is_hex_string(eui->valuestring)),
error = OT_ERROR_FAILED);
VerifyOrExit((NULL != eui && cJSON_IsString(eui) && 16 == strlen(eui->valuestring) &&
otbr::rest::is_hex_string(eui->valuestring)),
error = OT_ERROR_FAILED);
// check eui is convertable
SuccessOrExit(error = str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE));
SuccessOrExit(error = otbr::rest::str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE));

VerifyOrExit((NULL != pskd && cJSON_IsString(pskd) && (WPANSTATUS_OK == joiner_verify_pskd(pskd->valuestring))),
error = OT_ERROR_FAILED);
VerifyOrExit(
(NULL != pskd && cJSON_IsString(pskd) && (WPANSTATUS_OK == otbr::rest::joiner_verify_pskd(pskd->valuestring))),
error = OT_ERROR_FAILED);

exit:
if (error != OT_ERROR_NONE)
Expand All @@ -128,7 +136,7 @@ otError addJoiner(task_node_t *task_node, otInstance *aInstance)
cJSON *pskd = cJSON_GetObjectItemCaseSensitive(attributes, ATTRIBUTE_PSKD);
cJSON *timeout = cJSON_GetObjectItemCaseSensitive(attributes, "timeout");

str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE);
SuccessOrExit(error = otbr::rest::str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE));

if ((entryEui64Find(&eui64) != NULL) &&
(entryEui64Find(&eui64)->mstate < AllowListEntry::kAllowListEntryJoinFailed))
Expand Down Expand Up @@ -231,7 +239,7 @@ rest_actions_task_result_t evaluate_add_thread_device_task(task_node_t *task_nod
cJSON *attributes = cJSON_GetObjectItemCaseSensitive(task, "attributes");
cJSON *eui = cJSON_GetObjectItemCaseSensitive(attributes, "eui");

str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE);
SuccessOrExit(error = otbr::rest::str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE));

addrPtr = &eui64;
SuccessOrExit(error = allowListEntryJoinStatusGet(addrPtr));
Expand Down Expand Up @@ -259,7 +267,7 @@ rest_actions_task_result_t clean_add_thread_device_task(task_node_t *task_node,
otError error = OT_ERROR_NONE;

otExtAddress eui64 = {0};
str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE);
SuccessOrExit(error = otbr::rest::str_to_m8(eui64.m8, eui->valuestring, OT_EXT_ADDRESS_SIZE));

SuccessOrExit(error = allowListCommissionerJoinerRemove(eui64, aInstance));
SuccessOrExit(error = allowListEntryErase(eui64));
Expand Down
1 change: 1 addition & 0 deletions src/rest/extensions/rest_task_add_thread_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "rest_task_handler.hpp"
#include "rest_task_queue.hpp"

// Forward declare cJSON in the global scope
struct cJSON;

#ifdef __cplusplus
Expand Down
6 changes: 4 additions & 2 deletions src/rest/extensions/rest_task_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* update task status and conversion of `task_node` to `JSON` format.
*
*/
#include <assert.h>

#include "rest_task_handler.hpp"
#include "rest_task_queue.hpp"
#include "uuid.hpp"
Expand Down Expand Up @@ -61,7 +63,7 @@ task_node_t *task_node_new(cJSON *task)
task_node_t *task_node = (task_node_t *)calloc(1, sizeof(task_node_t)); // free in rest_task_queue_handle on delete
assert(NULL != task_node);

UUID uuid = UUID();
otbr::rest::UUID uuid = otbr::rest::UUID();

// Duplicate the client data associated with this task
task_node->task = cJSON_Duplicate(task, cJSON_True);
Expand All @@ -75,7 +77,7 @@ task_node_t *task_node_new(cJSON *task)
// Populate UUID
uuid.generateRandom();
uuid.getUuid(task_node->id);
snprintf(task_node->id_str, sizeof(task_node->id_str), uuid.toString().c_str());
snprintf(task_node->id_str, sizeof(task_node->id_str), "%s", uuid.toString().c_str());
otbrLogWarning("creating new task with id %s", task_node->id_str);
cJSON_AddStringToObject(task_node->task, "id", task_node->id_str);

Expand Down
Loading

0 comments on commit 08f6c06

Please sign in to comment.