Skip to content

Commit

Permalink
[controller] add Thread controller interface for unified APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Irving-cl committed May 29, 2024
1 parent 140247a commit c43481e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/dbus/server/dbus_thread_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,14 +660,19 @@ otError DBusThreadObject::GetLinkModeHandler(DBusMessageIter &aIter)

otError DBusThreadObject::GetDeviceRoleHandler(DBusMessageIter &aIter)
{
auto threadHelper = mHost->GetThreadHelper();
otDeviceRole role = otThreadGetDeviceRole(threadHelper->GetInstance());
std::string roleName = GetDeviceRoleName(role);
otError error = OT_ERROR_NONE;
otError error = OT_ERROR_NONE;

VerifyOrExit(DBusMessageEncodeToVariant(&aIter, roleName) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
mHost->GetDeviceRole([&aIter, &error](otError aError, otDeviceRole aRole) mutable {
OTBR_UNUSED_VARIABLE(aError);
std::string roleName = GetDeviceRoleName(aRole);
otbrError err = DBusMessageEncodeToVariant(&aIter, roleName);

if (err != OTBR_ERROR_NONE)
{
error = OT_ERROR_INVALID_ARGS;
}
});

exit:
return error;
}

Expand Down
6 changes: 6 additions & 0 deletions src/ncp/rcp_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ const char *RcpHost::GetThreadVersion(void)
return version;
}

void RcpHost::GetDeviceRole(DeviceRoleHandler aHandler)
{
otDeviceRole role = otThreadGetDeviceRole(mInstance);
aHandler(OT_ERROR_NONE, role);
}

/*
* Provide, if required an "otPlatLog()" function
*/
Expand Down
6 changes: 5 additions & 1 deletion src/ncp/rcp_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "common/mainloop.hpp"
#include "common/task_runner.hpp"
#include "common/types.hpp"
#include "ncp/thread_controller.hpp"
#include "utils/thread_helper.hpp"

namespace otbr {
Expand All @@ -63,7 +64,7 @@ namespace Ncp {
* This interface defines OpenThread Controller under RCP mode.
*
*/
class RcpHost : public MainloopProcessor
class RcpHost : public MainloopProcessor, public ThreadController
{
public:
using ThreadStateChangedCallback = std::function<void(otChangedFlags aFlags)>;
Expand Down Expand Up @@ -193,6 +194,9 @@ class RcpHost : public MainloopProcessor

~RcpHost(void) override;

// Thread Control APIs
void GetDeviceRole(DeviceRoleHandler aHandler) override;

private:
static void HandleStateChanged(otChangedFlags aFlags, void *aContext)
{
Expand Down
67 changes: 67 additions & 0 deletions src/ncp/thread_controller.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2024, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/**
* @file
* This file includes definitions of Thead Controller Interface.
*/

#ifndef OTBR_AGENT_THREAD_CONTROLLER_HPP_
#define OTBR_AGENT_THREAD_CONTROLLER_HPP_

#include <functional>
#include <vector>

#include <openthread/error.h>
#include <openthread/thread.h>

#include "lib/spinel/coprocessor_type.h"

#include "common/logging.hpp"

namespace otbr {
namespace Ncp {

class ThreadController
{
public:
/**
* Thread Control API: GetDeviceRole
*
* This method gets the device role and return the role through the handler.
*
* @param[in] aHandler A handler to return the role.
*/
using DeviceRoleHandler = std::function<void(otError, otDeviceRole)>;
virtual void GetDeviceRole(DeviceRoleHandler aHandler) = 0;
};

} // namespace Ncp
} // namespace otbr

#endif // OTBR_AGENT_THREAD_CONTROLLER_HPP_

0 comments on commit c43481e

Please sign in to comment.