|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 Project CHIP Authors |
| 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 | +#include "simple-app-helper.h" |
| 18 | + |
| 19 | +#include "app/clusters/bindings/BindingManager.h" |
| 20 | +#include <inttypes.h> |
| 21 | +#include <lib/core/CHIPCore.h> |
| 22 | +#include <lib/shell/Commands.h> |
| 23 | +#include <lib/shell/Engine.h> |
| 24 | +#include <lib/shell/commands/Help.h> |
| 25 | +#include <lib/support/CHIPArgParser.hpp> |
| 26 | +#include <lib/support/CHIPMem.h> |
| 27 | +#include <lib/support/CodeUtils.h> |
| 28 | +#include <platform/CHIPDeviceLayer.h> |
| 29 | + |
| 30 | +DiscoveryDelegateImpl * DiscoveryDelegateImpl::_discoveryDelegateImpl = nullptr; |
| 31 | + |
| 32 | +DiscoveryDelegateImpl * DiscoveryDelegateImpl::GetInstance() |
| 33 | +{ |
| 34 | + if (_discoveryDelegateImpl == nullptr) |
| 35 | + { |
| 36 | + _discoveryDelegateImpl = new DiscoveryDelegateImpl(); |
| 37 | + } |
| 38 | + return _discoveryDelegateImpl; |
| 39 | +} |
| 40 | + |
| 41 | +void DiscoveryDelegateImpl::HandleOnAdded(matter::casting::memory::Strong<matter::casting::core::CastingPlayer> player) |
| 42 | +{ |
| 43 | + if (commissionersCount == 0) |
| 44 | + { |
| 45 | + ChipLogProgress(AppServer, "Select discovered Casting Player (start index = 0) to request commissioning"); |
| 46 | + |
| 47 | + ChipLogProgress(AppServer, "Example: cast request 0"); |
| 48 | + } |
| 49 | + ChipLogProgress(AppServer, "Discovered CastingPlayer #%d", commissionersCount); |
| 50 | + ++commissionersCount; |
| 51 | + player->LogDetail(); |
| 52 | +} |
| 53 | + |
| 54 | +void DiscoveryDelegateImpl::HandleOnUpdated(matter::casting::memory::Strong<matter::casting::core::CastingPlayer> player) |
| 55 | +{ |
| 56 | + ChipLogProgress(AppServer, "Updated CastingPlayer with ID: %s", player->GetId()); |
| 57 | +} |
| 58 | + |
| 59 | +void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer) |
| 60 | +{ |
| 61 | + ChipLogProgress(AppServer, "ConnectionHandler called with %" CHIP_ERROR_FORMAT, err.Format()); |
| 62 | +} |
| 63 | + |
| 64 | +#if defined(ENABLE_CHIP_SHELL) |
| 65 | +void RegisterCommands() |
| 66 | +{ |
| 67 | + static const chip::Shell::shell_command_t sDeviceComand = { &CommandHandler, "cast", |
| 68 | + "Casting commands. Usage: cast [command_name]" }; |
| 69 | + |
| 70 | + // Register the root `device` command with the top-level shell. |
| 71 | + chip::Shell::Engine::Root().RegisterCommands(&sDeviceComand, 1); |
| 72 | +} |
| 73 | + |
| 74 | +CHIP_ERROR CommandHandler(int argc, char ** argv) |
| 75 | +{ |
| 76 | + if (argc == 0 || strcmp(argv[0], "help") == 0) |
| 77 | + { |
| 78 | + return PrintAllCommands(); |
| 79 | + } |
| 80 | + if (strcmp(argv[0], "discover") == 0) |
| 81 | + { |
| 82 | + ChipLogProgress(AppServer, "discover"); |
| 83 | + |
| 84 | + return matter::casting::core::CastingPlayerDiscovery::GetInstance()->StartDiscovery(kTargetPlayerDeviceType); |
| 85 | + } |
| 86 | + if (strcmp(argv[0], "stop-discovery") == 0) |
| 87 | + { |
| 88 | + ChipLogProgress(AppServer, "stop-discovery"); |
| 89 | + return matter::casting::core::CastingPlayerDiscovery::GetInstance()->StopDiscovery(); |
| 90 | + } |
| 91 | + if (strcmp(argv[0], "request") == 0) |
| 92 | + { |
| 93 | + ChipLogProgress(AppServer, "request"); |
| 94 | + if (argc < 2) |
| 95 | + { |
| 96 | + return PrintAllCommands(); |
| 97 | + } |
| 98 | + char * eptr; |
| 99 | + unsigned long index = static_cast<unsigned long>(strtol(argv[1], &eptr, 10)); |
| 100 | + std::vector<matter::casting::memory::Strong<matter::casting::core::CastingPlayer>> castingPlayers = |
| 101 | + matter::casting::core::CastingPlayerDiscovery::GetInstance()->GetCastingPlayers(); |
| 102 | + VerifyOrReturnValue(0 <= index && index < castingPlayers.size(), CHIP_ERROR_INVALID_ARGUMENT, |
| 103 | + ChipLogError(AppServer, "Invalid casting player index provided: %lu", index)); |
| 104 | + std::shared_ptr<matter::casting::core::CastingPlayer> targetCastingPlayer = castingPlayers.at(index); |
| 105 | + targetCastingPlayer->VerifyOrEstablishConnection(ConnectionHandler); |
| 106 | + return CHIP_NO_ERROR; |
| 107 | + } |
| 108 | + if (strcmp(argv[0], "print-bindings") == 0) |
| 109 | + { |
| 110 | + PrintBindings(); |
| 111 | + return CHIP_NO_ERROR; |
| 112 | + } |
| 113 | + if (strcmp(argv[0], "print-fabrics") == 0) |
| 114 | + { |
| 115 | + PrintFabrics(); |
| 116 | + return CHIP_NO_ERROR; |
| 117 | + } |
| 118 | + if (strcmp(argv[0], "delete-fabric") == 0) |
| 119 | + { |
| 120 | + char * eptr; |
| 121 | + chip::FabricIndex fabricIndex = (chip::FabricIndex) strtol(argv[1], &eptr, 10); |
| 122 | + chip::Server::GetInstance().GetFabricTable().Delete(fabricIndex); |
| 123 | + return CHIP_NO_ERROR; |
| 124 | + } |
| 125 | + return CHIP_ERROR_INVALID_ARGUMENT; |
| 126 | +} |
| 127 | + |
| 128 | +CHIP_ERROR PrintAllCommands() |
| 129 | +{ |
| 130 | + chip::Shell::streamer_t * sout = chip::Shell::streamer_get(); |
| 131 | + streamer_printf(sout, " help Usage: cast <subcommand>\r\n"); |
| 132 | + streamer_printf(sout, " print-bindings Usage: cast print-bindings\r\n"); |
| 133 | + streamer_printf(sout, " print-fabrics Usage: cast print-fabrics\r\n"); |
| 134 | + streamer_printf( |
| 135 | + sout, |
| 136 | + " delete-fabric <index> Delete a fabric from the casting client's fabric store. Usage: cast delete-fabric 1\r\n"); |
| 137 | + streamer_printf(sout, " discover Discover Casting Players. Usage: cast discover\r\n"); |
| 138 | + streamer_printf(sout, " stop-discovery Stop Discovery of Casting Players. Usage: cast stop-discovery\r\n"); |
| 139 | + streamer_printf( |
| 140 | + sout, " request <index> Request connecting to discovered Casting Player with [index]. Usage: cast request 0\r\n"); |
| 141 | + streamer_printf(sout, "\r\n"); |
| 142 | + |
| 143 | + return CHIP_NO_ERROR; |
| 144 | +} |
| 145 | + |
| 146 | +void PrintBindings() |
| 147 | +{ |
| 148 | + for (const auto & binding : chip::BindingTable::GetInstance()) |
| 149 | + { |
| 150 | + ChipLogProgress(AppServer, |
| 151 | + "Binding type=%d fab=%d nodeId=0x" ChipLogFormatX64 |
| 152 | + " groupId=%d local endpoint=%d remote endpoint=%d cluster=" ChipLogFormatMEI, |
| 153 | + binding.type, binding.fabricIndex, ChipLogValueX64(binding.nodeId), binding.groupId, binding.local, |
| 154 | + binding.remote, ChipLogValueMEI(binding.clusterId.ValueOr(0))); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +void PrintFabrics() |
| 159 | +{ |
| 160 | + // set fabric to be the first in the list |
| 161 | + for (const auto & fb : chip::Server::GetInstance().GetFabricTable()) |
| 162 | + { |
| 163 | + chip::FabricIndex fabricIndex = fb.GetFabricIndex(); |
| 164 | + ChipLogError(AppServer, "Next Fabric index=%d", fabricIndex); |
| 165 | + if (!fb.IsInitialized()) |
| 166 | + { |
| 167 | + ChipLogError(AppServer, " -- Not initialized"); |
| 168 | + continue; |
| 169 | + } |
| 170 | + chip::NodeId myNodeId = fb.GetNodeId(); |
| 171 | + ChipLogProgress(AppServer, |
| 172 | + "---- Current Fabric nodeId=0x" ChipLogFormatX64 " fabricId=0x" ChipLogFormatX64 " fabricIndex=%d", |
| 173 | + ChipLogValueX64(myNodeId), ChipLogValueX64(fb.GetFabricId()), fabricIndex); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +#endif // ENABLE_CHIP_SHELL |
0 commit comments