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

Update example network-manager-app and integrate it with ubus #33968

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
27 changes: 25 additions & 2 deletions examples/network-manager-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
@@ -15,7 +15,11 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

executable("network-manager-app") {
declare_args() {
matter_enable_ubus = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document what this arg means.

}

executable("matter-network-manager-app") {
sources = [
"include/CHIPProjectAppConfig.h",
"main.cpp",
@@ -27,12 +31,31 @@ executable("network-manager-app") {
"${chip_root}/src/lib",
]

libs = []
defines = []
include_dirs = [ "include" ]

if (matter_enable_ubus) {
defines += [ "MATTER_ENABLE_UBUS=1" ]
sources += [
"UbusManager.cpp",
"UbusManager.h",
"UbusSchema.c",
"UbusSchema.h",
"UloopHandler.cpp",
"UloopHandler.h",
]
libs += [
"ubox",
"ubus",
]
}

output_dir = root_out_dir
}

group("linux") {
deps = [ ":network-manager-app" ]
deps = [ ":matter-network-manager-app" ]
}

group("default") {
80 changes: 80 additions & 0 deletions examples/network-manager-app/linux/UbusManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "UbusManager.h"

#include "UbusSchema.h"
#include "UloopHandler.h"
#include <lib/support/CodeUtils.h>

#include <libubus.h>

using namespace chip;

CHIP_ERROR UbusManager::Init()
{
VerifyOrReturnError(!mInitialized, CHIP_ERROR_INCORRECT_STATE);

UloopHandler::Register();

int status;
if ((status = ubus_connect_ctx(&mContext, nullptr)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My knowledge of ubus is nonexistent, so I am just rubber-stamping the ubus bits. If you need me to actually go read up on these APIs and do a proper review, please let me know.

{
ChipLogError(AppServer, "Failed to connect to ubus: %s", ubus_strerror(status));
UloopHandler::Unregister();
return CHIP_ERROR_INTERNAL;
}

mObjectType = kMatterUbusObjectType;
mObject.type = &mObjectType; // can't use kMatterUbusObjectType directly, must be mutable
mObject.name = mObjectType.name;
mObject.methods = mObjectType.methods;
mObject.n_methods = mObjectType.n_methods;
if ((status = ubus_add_object(&mContext, &mObject)))
{
ChipLogError(AppServer, "Failed to register ubus service: %s", ubus_strerror(status));
ubus_shutdown(&mContext);
UloopHandler::Unregister();
return CHIP_ERROR_INTERNAL;
}

ubus_add_uloop(&mContext);
mInitialized = true;

return CHIP_NO_ERROR;
}

void UbusManager::Shutdown()
{
VerifyOrReturn(mInitialized);
mInitialized = false;
ubus_shutdown(&mContext);
UloopHandler::Unregister();
}

int MatterUbusHandleStatus(struct ubus_context * ctx, struct ubus_object * obj, struct ubus_request_data * req, const char * method,
struct blob_attr * msg)
{
FabricTable const & fabricTable = Server::GetInstance().GetFabricTable();

blob_buf blob{};
blob_buf_init(&blob, 0);
blobmsg_add_u8(&blob, "commissioned", fabricTable.FabricCount() > 0);
ubus_send_reply(ctx, req, blob.head);
blob_buf_free(&blob);
return 0;
}
39 changes: 39 additions & 0 deletions examples/network-manager-app/linux/UbusManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <app/server/Server.h>
#include <lib/core/CHIPError.h>

#include <libubus.h>

class UbusManager
{
public:
UbusManager() = default;
~UbusManager() { Shutdown(); }

CHIP_ERROR Init();
void Shutdown();

private:
bool mInitialized = false;
ubus_context mContext;
ubus_object_type mObjectType;
ubus_object mObject;
};
24 changes: 24 additions & 0 deletions examples/network-manager-app/linux/UbusSchema.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "UbusSchema.h"

const struct ubus_method kMatterMethods[] = {
UBUS_METHOD_NOARG("status", &MatterUbusHandleStatus),
};

const struct ubus_object_type kMatterUbusObjectType = UBUS_OBJECT_TYPE("matter", kMatterMethods);
34 changes: 34 additions & 0 deletions examples/network-manager-app/linux/UbusSchema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <libubus.h>

#ifdef __cplusplus
extern "C" {
#endif

// Note these constants are defined in a C file because the macros
// used to generate these structures don't compile correctly in C++.

extern const struct ubus_object_type kMatterUbusObjectType;

int MatterUbusHandleStatus(struct ubus_context * ctx, struct ubus_object * obj, struct ubus_request_data * req, const char * method,
struct blob_attr * msg);

#ifdef __cplusplus
}
#endif
128 changes: 128 additions & 0 deletions examples/network-manager-app/linux/UloopHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "UloopHandler.h"

#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>

#include <chrono>

extern "C" {
#include <libubox/uloop.h>
}

namespace chip {

namespace {
System::LayerSocketsLoop & SystemLayer()
{
return static_cast<System::LayerSocketsLoop &>(DeviceLayer::SystemLayer());
}
} // namespace

UloopHandler UloopHandler::gInstance;
int UloopHandler::gRegistered = 0;

CHIP_ERROR UloopHandler::Register()
{
VerifyOrReturnError(gRegistered++ == 0, CHIP_NO_ERROR);

if (uloop_fd_set_cb != nullptr)
{
gRegistered = 0;
return CHIP_ERROR_INCORRECT_STATE;
}

ChipLogDetail(chipSystemLayer, "Registering uloop handler");
uloop_fd_set_cb = &UloopFdSet;
uloop_init();
SystemLayer().AddLoopHandler(gInstance);

return CHIP_NO_ERROR;
}

void UloopHandler::Unregister()
{
auto count = gRegistered--;
VerifyOrDie(count > 0);
VerifyOrReturn(count == 1);

ChipLogDetail(chipSystemLayer, "Unregistering uloop handler");
SystemLayer().RemoveLoopHandler(gInstance);
uloop_done();
uloop_fd_set_cb = nullptr;
}

System::Clock::Timestamp UloopHandler::PrepareEvents(System::Clock::Timestamp now)
{
std::chrono::duration<int, std::milli> timeout(uloop_get_next_timeout());
VerifyOrReturnValue(timeout.count() >= 0, System::Clock::Timestamp::max());
ChipLogDetail(chipSystemLayer, "Next uloop timeout: %d", timeout.count());
return now + std::chrono::duration_cast<System::Clock::Timestamp>(timeout);
}

void UloopHandler::HandleEvents()
{
uloop_run_timeout(0);
}

void UloopHandler::UloopFdSet(struct uloop_fd * fd, unsigned int events)
{
ChipLogDetail(chipSystemLayer, "Uloop fd_set(%d, %d)", fd->fd, events);

auto & system = SystemLayer();
System::SocketWatchToken watch;
CHIP_ERROR err = system.StartWatchingSocket(fd->fd, &watch); // or find existing watch
if (events != 0)
{
int changed = fd->flags ^ events;
if (changed & ULOOP_READ)
{
if (events & ULOOP_READ)
{
system.RequestCallbackOnPendingRead(watch);
}
else
{
system.ClearCallbackOnPendingRead(watch);
}
}
if (changed & ULOOP_WRITE)
{
if (events & ULOOP_WRITE)
{
system.RequestCallbackOnPendingWrite(watch);
}
else
{
system.ClearCallbackOnPendingWrite(watch);
}
}
}
else
{
VerifyOrReturn(err == CHIP_NO_ERROR); // shouldn't happen, but ignore
SuccessOrExit(err = system.StopWatchingSocket(&watch));
}

return;
exit:
ChipLogError(chipSystemLayer, "Failed to handle uloop fd_set(%d, %d): %" CHIP_ERROR_FORMAT, fd->fd, events, err.Format());
}

} // namespace chip
Loading
Loading