-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
ksperling-apple
wants to merge
3
commits into
project-chip:master
Choose a base branch
from
ksperling-apple:netman-ubus
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.