Skip to content

Commit 72ed829

Browse files
Upate example network-manager-app
Add ubus integration Fix device type id Prefix binary name with "matter-"
1 parent 7f94440 commit 72ed829

File tree

10 files changed

+425
-15
lines changed

10 files changed

+425
-15
lines changed

examples/network-manager-app/linux/BUILD.gn

+25-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import("//build_overrides/build.gni")
1616
import("//build_overrides/chip.gni")
1717

18-
executable("network-manager-app") {
18+
declare_args() {
19+
matter_enable_ubus = false
20+
}
21+
22+
executable("matter-network-manager-app") {
1923
sources = [
2024
"include/CHIPProjectAppConfig.h",
2125
"main.cpp",
@@ -27,12 +31,31 @@ executable("network-manager-app") {
2731
"${chip_root}/src/lib",
2832
]
2933

34+
libs = []
35+
defines = []
3036
include_dirs = [ "include" ]
37+
38+
if (matter_enable_ubus) {
39+
defines += [ "MATTER_ENABLE_UBUS=1" ]
40+
sources += [
41+
"UbusManager.cpp",
42+
"UbusManager.h",
43+
"UbusSchema.c",
44+
"UbusSchema.h",
45+
"UloopHandler.cpp",
46+
"UloopHandler.h",
47+
]
48+
libs += [
49+
"ubox",
50+
"ubus",
51+
]
52+
}
53+
3154
output_dir = root_out_dir
3255
}
3356

3457
group("linux") {
35-
deps = [ ":network-manager-app" ]
58+
deps = [ ":matter-network-manager-app" ]
3659
}
3760

3861
group("default") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "UbusManager.h"
19+
20+
#include "UbusSchema.h"
21+
#include "UloopHandler.h"
22+
#include <lib/support/CodeUtils.h>
23+
24+
#include <libubus.h>
25+
26+
using namespace chip;
27+
28+
CHIP_ERROR UbusManager::Init()
29+
{
30+
VerifyOrReturnError(!mInitialized, CHIP_ERROR_INCORRECT_STATE);
31+
32+
UloopHandler::Register();
33+
34+
int status;
35+
if ((status = ubus_connect_ctx(&mContext, nullptr)))
36+
{
37+
ChipLogError(AppServer, "Failed to connect to ubus: %s", ubus_strerror(status));
38+
UloopHandler::Unregister();
39+
return CHIP_ERROR_INTERNAL;
40+
}
41+
42+
mObjectType = kMatterUbusObjectType;
43+
mObject.type = &mObjectType; // can't use kMatterUbusObjectType directly, must be mutable
44+
mObject.name = mObjectType.name;
45+
mObject.methods = mObjectType.methods;
46+
mObject.n_methods = mObjectType.n_methods;
47+
if ((status = ubus_add_object(&mContext, &mObject)))
48+
{
49+
ChipLogError(AppServer, "Failed to register ubus service: %s", ubus_strerror(status));
50+
ubus_shutdown(&mContext);
51+
UloopHandler::Unregister();
52+
return CHIP_ERROR_INTERNAL;
53+
}
54+
55+
ubus_add_uloop(&mContext);
56+
mInitialized = true;
57+
58+
return CHIP_NO_ERROR;
59+
}
60+
61+
void UbusManager::Shutdown()
62+
{
63+
VerifyOrReturn(mInitialized);
64+
mInitialized = false;
65+
ubus_shutdown(&mContext);
66+
UloopHandler::Unregister();
67+
}
68+
69+
int MatterUbusHandleStatus(struct ubus_context * ctx, struct ubus_object * obj, struct ubus_request_data * req, const char * method,
70+
struct blob_attr * msg)
71+
{
72+
FabricTable const & fabricTable = Server::GetInstance().GetFabricTable();
73+
74+
blob_buf blob{};
75+
blob_buf_init(&blob, 0);
76+
blobmsg_add_u8(&blob, "commissioned", fabricTable.FabricCount() > 0);
77+
ubus_send_reply(ctx, req, blob.head);
78+
blob_buf_free(&blob);
79+
return 0;
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include <app/server/Server.h>
21+
#include <lib/core/CHIPError.h>
22+
23+
#include <libubus.h>
24+
25+
class UbusManager
26+
{
27+
public:
28+
UbusManager() = default;
29+
~UbusManager() { Shutdown(); }
30+
31+
CHIP_ERROR Init();
32+
void Shutdown();
33+
34+
private:
35+
bool mInitialized = false;
36+
ubus_context mContext;
37+
ubus_object_type mObjectType;
38+
ubus_object mObject;
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "UbusSchema.h"
19+
20+
const struct ubus_method kMatterMethods[] = {
21+
UBUS_METHOD_NOARG("status", &MatterUbusHandleStatus),
22+
};
23+
24+
const struct ubus_object_type kMatterUbusObjectType = UBUS_OBJECT_TYPE("matter", kMatterMethods);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <libubus.h>
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
// Note these constants are defined in a C file because the macros
25+
// used to generate these structures don't compile correctly in C++.
26+
27+
extern const struct ubus_object_type kMatterUbusObjectType;
28+
29+
int MatterUbusHandleStatus(struct ubus_context * ctx, struct ubus_object * obj, struct ubus_request_data * req, const char * method,
30+
struct blob_attr * msg);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "UloopHandler.h"
19+
20+
#include <lib/support/CodeUtils.h>
21+
#include <platform/CHIPDeviceLayer.h>
22+
23+
#include <chrono>
24+
25+
extern "C" {
26+
#include <libubox/uloop.h>
27+
}
28+
29+
namespace chip {
30+
31+
namespace {
32+
System::LayerSocketsLoop & SystemLayer()
33+
{
34+
return static_cast<System::LayerSocketsLoop &>(DeviceLayer::SystemLayer());
35+
}
36+
} // namespace
37+
38+
UloopHandler UloopHandler::gInstance;
39+
int UloopHandler::gRegistered = 0;
40+
41+
CHIP_ERROR UloopHandler::Register()
42+
{
43+
VerifyOrReturnError(gRegistered++ == 0, CHIP_NO_ERROR);
44+
45+
if (uloop_fd_set_cb != nullptr)
46+
{
47+
gRegistered = 0;
48+
return CHIP_ERROR_INCORRECT_STATE;
49+
}
50+
51+
ChipLogDetail(chipSystemLayer, "Registering uloop handler");
52+
uloop_fd_set_cb = &UloopFdSet;
53+
uloop_init();
54+
SystemLayer().AddLoopHandler(gInstance);
55+
56+
return CHIP_NO_ERROR;
57+
}
58+
59+
void UloopHandler::Unregister()
60+
{
61+
auto count = gRegistered--;
62+
VerifyOrDie(count > 0);
63+
VerifyOrReturn(count == 1);
64+
65+
ChipLogDetail(chipSystemLayer, "Unregistering uloop handler");
66+
SystemLayer().RemoveLoopHandler(gInstance);
67+
uloop_done();
68+
uloop_fd_set_cb = nullptr;
69+
}
70+
71+
System::Clock::Timestamp UloopHandler::PrepareEvents(System::Clock::Timestamp now)
72+
{
73+
std::chrono::duration<int, std::milli> timeout(uloop_get_next_timeout());
74+
VerifyOrReturnValue(timeout.count() >= 0, System::Clock::Timestamp::max());
75+
ChipLogDetail(chipSystemLayer, "Next uloop timeout: %d", timeout.count());
76+
return now + std::chrono::duration_cast<System::Clock::Timestamp>(timeout);
77+
}
78+
79+
void UloopHandler::HandleEvents()
80+
{
81+
uloop_run_timeout(0);
82+
}
83+
84+
void UloopHandler::UloopFdSet(struct uloop_fd * fd, unsigned int events)
85+
{
86+
ChipLogDetail(chipSystemLayer, "Uloop fd_set(%d, %d)", fd->fd, events);
87+
88+
auto & system = SystemLayer();
89+
System::SocketWatchToken watch;
90+
CHIP_ERROR err = system.StartWatchingSocket(fd->fd, &watch); // or find existing watch
91+
if (events != 0)
92+
{
93+
int changed = fd->flags ^ events;
94+
if (changed & ULOOP_READ)
95+
{
96+
if (events & ULOOP_READ)
97+
{
98+
system.RequestCallbackOnPendingRead(watch);
99+
}
100+
else
101+
{
102+
system.ClearCallbackOnPendingRead(watch);
103+
}
104+
}
105+
if (changed & ULOOP_WRITE)
106+
{
107+
if (events & ULOOP_WRITE)
108+
{
109+
system.RequestCallbackOnPendingWrite(watch);
110+
}
111+
else
112+
{
113+
system.ClearCallbackOnPendingWrite(watch);
114+
}
115+
}
116+
}
117+
else
118+
{
119+
VerifyOrReturn(err == CHIP_NO_ERROR); // shouldn't happen, but ignore
120+
SuccessOrExit(err = system.StopWatchingSocket(&watch));
121+
}
122+
123+
return;
124+
exit:
125+
ChipLogError(chipSystemLayer, "Failed to handle uloop fd_set(%d, %d): %" CHIP_ERROR_FORMAT, fd->fd, events, err.Format());
126+
}
127+
128+
} // namespace chip

0 commit comments

Comments
 (0)