-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
104841d
commit 1033744
Showing
13 changed files
with
374 additions
and
28 deletions.
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
2 changes: 2 additions & 0 deletions
2
examples/bridge-app/bridge-common/protos/bridge_service.options
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,2 @@ | ||
chip.rpc.bridge.AddDevice.device_types max_count:32 | ||
chip.rpc.bridge.AddDevice.clusters max_count:64 |
33 changes: 33 additions & 0 deletions
33
examples/bridge-app/bridge-common/protos/bridge_service.proto
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,33 @@ | ||
syntax = "proto3"; | ||
|
||
package chip.rpc.bridge; | ||
|
||
message Empty {} | ||
|
||
message RemoveDevice { | ||
uint32 id = 1; | ||
} | ||
|
||
message Cluster { | ||
uint32 cluster_id = 1; | ||
} | ||
|
||
message DeviceType { | ||
uint32 id = 1; | ||
uint32 version = 2; | ||
} | ||
|
||
message AddDevice { | ||
repeated Cluster clusters = 1; | ||
repeated DeviceType device_types = 2; | ||
uint32 parent_endpoint = 3; | ||
} | ||
|
||
message AddDeviceResponse { | ||
uint32 id = 1; | ||
} | ||
|
||
service Bridge { | ||
rpc Add(AddDevice) returns (AddDeviceResponse){}; | ||
rpc Remove(RemoveDevice) returns (Empty){}; | ||
} |
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,62 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022 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 "Backend.h" | ||
|
||
#include "main.h" | ||
|
||
std::vector<std::unique_ptr<Device>> g_devices; | ||
std::vector<std::unique_ptr<DynamicDeviceImpl>> g_device_impls; | ||
|
||
bool RemoveDeviceAt(uint32_t index) | ||
{ | ||
if (index >= g_devices.size() || !g_devices[index]) | ||
{ | ||
return false; | ||
} | ||
|
||
RemoveDeviceEndpoint(g_devices[index].get()); | ||
|
||
for (auto & room : gRooms) | ||
room.RemoveEndpoint(g_devices[index]->GetEndpointId()); | ||
|
||
g_devices[index] = nullptr; | ||
g_device_impls[index] = nullptr; | ||
|
||
return true; | ||
} | ||
|
||
int AddDevice(std::unique_ptr<DynamicDeviceImpl> device) | ||
{ | ||
auto dev = std::make_unique<Device>(device->CreateDevice()); | ||
int ep = AddDeviceEndpoint(dev.get()); | ||
if (ep < 0) | ||
{ | ||
return -1; | ||
} | ||
|
||
size_t index = (size_t) ep; | ||
if (g_devices.size() <= index) | ||
{ | ||
g_devices.resize(index + 1); | ||
g_device_impls.resize(index + 1); | ||
} | ||
g_devices[index] = std::move(dev); | ||
g_device_impls[index] = std::move(device); | ||
return ep; | ||
} |
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
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,90 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022 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 "bridge_service.h" | ||
|
||
#include "Backend.h" | ||
#include "main.h" | ||
|
||
namespace chip { | ||
namespace rpc { | ||
|
||
namespace { | ||
const clusters::ClusterInfo * FindCluster(uint32_t id) | ||
{ | ||
for (const auto & cluster : clusters::kKnownClusters) | ||
{ | ||
if (id == cluster.id) | ||
return &cluster; | ||
} | ||
return nullptr; | ||
} | ||
} // namespace | ||
|
||
::pw::Status Bridge::Add(const ::chip_rpc_bridge_AddDevice & request, ::chip_rpc_bridge_AddDeviceResponse & response) | ||
{ | ||
std::unique_ptr<DynamicDeviceImpl> pending; | ||
|
||
pending = std::make_unique<DynamicDeviceImpl>(); | ||
pending->SetParentEndpointId(request.parent_endpoint); | ||
|
||
for (pb_size_t i = 0; i < request.clusters_count; i++) | ||
{ | ||
const chip_rpc_bridge_Cluster & c = request.clusters[i]; | ||
|
||
auto cluster = FindCluster(c.cluster_id); | ||
if (!cluster) | ||
{ | ||
return pw::Status::InvalidArgument(); | ||
} | ||
|
||
std::unique_ptr<CommonCluster> obj(cluster->ctor(::operator new(cluster->size))); | ||
DynamicAttributeList dynamic_attrs; | ||
auto attrs = obj->GetAllAttributes(); | ||
for (auto & attr : attrs) | ||
dynamic_attrs.Add(attr); | ||
pending->AddCluster(std::move(obj), dynamic_attrs, nullptr, nullptr); | ||
} | ||
|
||
for (pb_size_t i = 0; i < request.device_types_count; i++) | ||
{ | ||
EmberAfDeviceType devType = { (uint16_t) request.device_types[i].id, (uint8_t) request.device_types[i].version }; | ||
pending->AddDeviceType(devType); | ||
} | ||
|
||
int ret = AddDevice(std::move(pending)); | ||
if (ret < 0) | ||
{ | ||
return pw::Status::Aborted(); | ||
} | ||
response.id = ret; | ||
return pw::OkStatus(); | ||
} | ||
|
||
::pw::Status Bridge::Remove(const ::chip_rpc_bridge_RemoveDevice & request, ::chip_rpc_bridge_Empty & response) | ||
{ | ||
if (!RemoveDeviceAt(request.id)) | ||
{ | ||
return pw::Status::NotFound(); | ||
} | ||
|
||
return pw::OkStatus(); | ||
} | ||
|
||
} // namespace rpc | ||
} // namespace chip |
Oops, something went wrong.