|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2022 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include "descriptor_service/descriptor_service.rpc.pb.h" |
| 22 | + |
| 23 | +#include <app-common/zap-generated/af-structs.h> |
| 24 | +#include <app-common/zap-generated/cluster-objects.h> |
| 25 | +#include <app-common/zap-generated/ids/Attributes.h> |
| 26 | +#include <app-common/zap-generated/ids/Clusters.h> |
| 27 | + |
| 28 | +namespace chip { |
| 29 | +namespace rpc { |
| 30 | + |
| 31 | +class Descriptor : public pw_rpc::nanopb::Descriptor::Service<Descriptor> |
| 32 | +{ |
| 33 | +public: |
| 34 | + virtual ~Descriptor() = default; |
| 35 | + |
| 36 | + virtual void DeviceTypeList(const ::chip_rpc_Endpoint & request, ServerWriter<::chip_rpc_DeviceType> & writer) |
| 37 | + { |
| 38 | + constexpr uint16_t kInvalidEndpointIndex = 0xFFFF; |
| 39 | + uint16_t index = emberAfIndexFromEndpoint(request.endpoint); |
| 40 | + if (index == kInvalidEndpointIndex) |
| 41 | + { |
| 42 | + writer.Finish(pw::Status::InvalidArgument()); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + chip_rpc_DeviceType out{ .device_type = emberAfDeviceIdFromIndex(index) }; |
| 47 | + writer.Write(out); |
| 48 | + writer.Finish(); |
| 49 | + } |
| 50 | + |
| 51 | + void ServerList(const ::chip_rpc_Endpoint & request, ServerWriter<::chip_rpc_Cluster> & writer) |
| 52 | + { |
| 53 | + ClusterList(request.endpoint, true /*server*/, writer); |
| 54 | + } |
| 55 | + |
| 56 | + void ClientList(const ::chip_rpc_Endpoint & request, ServerWriter<::chip_rpc_Cluster> & writer) |
| 57 | + { |
| 58 | + ClusterList(request.endpoint, false /*server*/, writer); |
| 59 | + } |
| 60 | + |
| 61 | + void PartsList(const ::chip_rpc_Endpoint & request, ServerWriter<::chip_rpc_Endpoint> & writer) |
| 62 | + { |
| 63 | + if (request.endpoint == 0x00) |
| 64 | + { |
| 65 | + for (uint16_t index = 0; index < emberAfEndpointCount(); index++) |
| 66 | + { |
| 67 | + if (emberAfEndpointIndexIsEnabled(index)) |
| 68 | + { |
| 69 | + EndpointId endpoint_id = emberAfEndpointFromIndex(index); |
| 70 | + if (endpoint_id == 0) |
| 71 | + continue; |
| 72 | + chip_rpc_Endpoint out{ endpoint : endpoint_id }; |
| 73 | + writer.Write(out); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + writer.Finish(); |
| 78 | + } |
| 79 | + |
| 80 | +private: |
| 81 | + void ClusterList(EndpointId endpoint, bool server, ServerWriter<::chip_rpc_Cluster> & writer) |
| 82 | + { |
| 83 | + uint16_t cluster_count = emberAfClusterCount(endpoint, server); |
| 84 | + |
| 85 | + for (uint8_t cluster_index = 0; cluster_index < cluster_count; cluster_index++) |
| 86 | + { |
| 87 | + const EmberAfCluster * cluster = emberAfGetNthCluster(endpoint, cluster_index, server); |
| 88 | + chip_rpc_Cluster out{ cluster_id : cluster->clusterId }; |
| 89 | + writer.Write(out); |
| 90 | + } |
| 91 | + writer.Finish(); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +} // namespace rpc |
| 96 | +} // namespace chip |
0 commit comments