|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 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 | +#include "RpcClient.h" |
| 20 | + |
| 21 | +#include <string> |
| 22 | +#include <thread> |
| 23 | +#include <unistd.h> |
| 24 | + |
| 25 | +#include "fabric_admin_service/fabric_admin_service.rpc.pb.h" |
| 26 | +#include "pw_assert/check.h" |
| 27 | +#include "pw_hdlc/decoder.h" |
| 28 | +#include "pw_hdlc/default_addresses.h" |
| 29 | +#include "pw_hdlc/rpc_channel.h" |
| 30 | +#include "pw_rpc/client.h" |
| 31 | +#include "pw_stream/socket_stream.h" |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +constexpr size_t kMaxTransmissionUnit = 256; |
| 36 | +constexpr uint32_t kRpcTimeoutMs = 1000; |
| 37 | +const char * rpcServerAddress = "127.0.0.1"; |
| 38 | + |
| 39 | +pw::stream::SocketStream rpcSocketStream; |
| 40 | + |
| 41 | +// Set up the output channel for the pw_rpc client to use. |
| 42 | +pw::hdlc::RpcChannelOutput hdlc_channel_output(rpcSocketStream, pw::hdlc::kDefaultRpcAddress, "HDLC channel"); |
| 43 | + |
| 44 | +// An array of RPC channels (channels) is created, each associated with an HDLC channel output. |
| 45 | +// This sets up the communication channels for RPC calls. |
| 46 | +pw::rpc::Channel channels[] = { pw::rpc::Channel::Create<1>(&hdlc_channel_output) }; |
| 47 | + |
| 48 | +// Initialize the RPC client with the channels. |
| 49 | +pw::rpc::Client client(channels); |
| 50 | + |
| 51 | +// Generated clients are namespaced with their proto library. |
| 52 | +using FabricAdminClient = chip::rpc::pw_rpc::nanopb::FabricAdmin::Client; |
| 53 | + |
| 54 | +// RPC channel ID on which to make client calls. RPC calls cannot be made on |
| 55 | +// channel 0 (Channel::kUnassignedChannelId). |
| 56 | +constexpr uint32_t kDefaultChannelId = 1; |
| 57 | + |
| 58 | +// Function to process incoming packets |
| 59 | +void ProcessPackets() |
| 60 | +{ |
| 61 | + std::array<std::byte, kMaxTransmissionUnit> inputBuf; |
| 62 | + pw::hdlc::Decoder decoder(inputBuf); |
| 63 | + |
| 64 | + while (true) |
| 65 | + { |
| 66 | + std::array<std::byte, kMaxTransmissionUnit> data; |
| 67 | + auto ret = rpcSocketStream.Read(data); |
| 68 | + if (!ret.ok()) |
| 69 | + { |
| 70 | + if (ret.status() == pw::Status::OutOfRange()) |
| 71 | + { |
| 72 | + // Handle remote disconnect |
| 73 | + rpcSocketStream.Close(); |
| 74 | + return; |
| 75 | + } |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + for (std::byte byte : ret.value()) |
| 80 | + { |
| 81 | + auto result = decoder.Process(byte); |
| 82 | + if (!result.ok()) |
| 83 | + { |
| 84 | + // Wait for more bytes that form a complete packet |
| 85 | + continue; |
| 86 | + } |
| 87 | + pw::hdlc::Frame & frame = result.value(); |
| 88 | + if (frame.address() != pw::hdlc::kDefaultRpcAddress) |
| 89 | + { |
| 90 | + // Wrong address; ignore the packet |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + client.ProcessPacket(frame.data()).IgnoreError(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +template <typename CallType> |
| 100 | +CHIP_ERROR WaitForResponse(CallType & call) |
| 101 | +{ |
| 102 | + if (!call.active()) |
| 103 | + { |
| 104 | + return CHIP_ERROR_INTERNAL; |
| 105 | + } |
| 106 | + |
| 107 | + // Wait for the response or timeout |
| 108 | + uint32_t elapsedTimeMs = 0; |
| 109 | + const uint32_t sleepTimeMs = 100; |
| 110 | + |
| 111 | + while (call.active() && elapsedTimeMs < kRpcTimeoutMs) |
| 112 | + { |
| 113 | + usleep(sleepTimeMs * 1000); |
| 114 | + elapsedTimeMs += sleepTimeMs; |
| 115 | + } |
| 116 | + |
| 117 | + if (elapsedTimeMs >= kRpcTimeoutMs) |
| 118 | + { |
| 119 | + ChipLogError(NotSpecified, "RPC Response timed out!"); |
| 120 | + return CHIP_ERROR_TIMEOUT; |
| 121 | + } |
| 122 | + |
| 123 | + return CHIP_NO_ERROR; |
| 124 | +} |
| 125 | + |
| 126 | +void OperationStatusResponse(const chip_rpc_OperationStatus & response, pw::Status status) |
| 127 | +{ |
| 128 | + if (status.ok()) |
| 129 | + { |
| 130 | + ChipLogProgress(NotSpecified, "Received operation status: %d", response.success); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + ChipLogProgress(NotSpecified, "RPC call failed with status: %d\n", status.code()); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +} // namespace |
| 139 | + |
| 140 | +CHIP_ERROR InitRpcClient(uint16_t rpcServerPort) |
| 141 | +{ |
| 142 | + if (rpcSocketStream.Connect(rpcServerAddress, rpcServerPort) != PW_STATUS_OK) |
| 143 | + { |
| 144 | + ChipLogError(NotSpecified, "Failed to connect the Fabric-Admin"); |
| 145 | + return CHIP_ERROR_NOT_CONNECTED; |
| 146 | + } |
| 147 | + |
| 148 | + ChipLogProgress(NotSpecified, "Connectted to the Fabric-Admin\n"); |
| 149 | + |
| 150 | + // Start a thread to process incoming packets |
| 151 | + std::thread packet_processor(ProcessPackets); |
| 152 | + packet_processor.detach(); |
| 153 | + |
| 154 | + return CHIP_NO_ERROR; |
| 155 | +} |
| 156 | + |
| 157 | +CHIP_ERROR OpenCommissioningWindow(chip::NodeId nodeId) |
| 158 | +{ |
| 159 | + ChipLogProgress(NotSpecified, "OpenCommissioningWindow\n"); |
| 160 | + |
| 161 | + FabricAdminClient fabric_admin_client(client, kDefaultChannelId); |
| 162 | + chip_rpc_DeviceInfo device; |
| 163 | + |
| 164 | + device.node_id = nodeId; |
| 165 | + |
| 166 | + // The RPC will remain active as long as `call` is alive. |
| 167 | + auto call = fabric_admin_client.OpenCommissioningWindow(device, OperationStatusResponse); |
| 168 | + return WaitForResponse(call); |
| 169 | +} |
0 commit comments