|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 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 | + |
| 18 | +#include <app/clusters/thread-border-router-management-server/thread-border-router-management-server.h> |
| 19 | +#include <app/server/Server.h> |
| 20 | +#include <lib/core/CHIPEncoding.h> |
| 21 | +#include <lib/support/CodeUtils.h> |
| 22 | +#include <lib/support/Span.h> |
| 23 | +#include <lib/support/ThreadOperationalDataset.h> |
| 24 | +#include <platform/CHIPDeviceLayer.h> |
| 25 | + |
| 26 | +#include <optional> |
| 27 | + |
| 28 | +using namespace chip; |
| 29 | +using namespace chip::literals; |
| 30 | +using namespace chip::app; |
| 31 | +using namespace chip::app::Clusters; |
| 32 | + |
| 33 | +namespace { |
| 34 | +class FakeBorderRouterDelegate final : public ThreadBorderRouterManagement::Delegate |
| 35 | +{ |
| 36 | + CHIP_ERROR Init(AttributeChangeCallback * attributeChangeCallback) override |
| 37 | + { |
| 38 | + mAttributeChangeCallback = attributeChangeCallback; |
| 39 | + return CHIP_NO_ERROR; |
| 40 | + } |
| 41 | + |
| 42 | + bool GetPanChangeSupported() override { return true; } |
| 43 | + |
| 44 | + void GetBorderRouterName(MutableCharSpan & borderRouterName) override |
| 45 | + { |
| 46 | + CopyCharSpanToMutableCharSpan("netman-br"_span, borderRouterName); |
| 47 | + } |
| 48 | + |
| 49 | + CHIP_ERROR GetBorderAgentId(MutableByteSpan & borderAgentId) override |
| 50 | + { |
| 51 | + static constexpr uint8_t kBorderAgentId[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 52 | + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; |
| 53 | + VerifyOrReturnError(borderAgentId.size() == 16, CHIP_ERROR_INVALID_ARGUMENT); |
| 54 | + return CopySpanToMutableSpan(ByteSpan(kBorderAgentId), borderAgentId); |
| 55 | + } |
| 56 | + |
| 57 | + uint16_t GetThreadVersion() override { return /* Thread 1.3.1 */ 5; } |
| 58 | + |
| 59 | + bool GetInterfaceEnabled() override { return !mActiveDataset.IsEmpty(); } |
| 60 | + |
| 61 | + CHIP_ERROR GetDataset(Thread::OperationalDataset & dataset, DatasetType type) override |
| 62 | + { |
| 63 | + Thread::OperationalDataset * source; |
| 64 | + switch (type) |
| 65 | + { |
| 66 | + case DatasetType::kActive: |
| 67 | + source = &mActiveDataset; |
| 68 | + break; |
| 69 | + case DatasetType::kPending: |
| 70 | + source = &mPendingDataset; |
| 71 | + break; |
| 72 | + default: |
| 73 | + return CHIP_ERROR_INVALID_ARGUMENT; |
| 74 | + } |
| 75 | + VerifyOrReturnError(!source->IsEmpty(), CHIP_ERROR_NOT_FOUND); |
| 76 | + return dataset.Init(source->AsByteSpan()); |
| 77 | + } |
| 78 | + |
| 79 | + void SetActiveDataset(const Thread::OperationalDataset & activeDataset, uint32_t sequenceNum, |
| 80 | + ActivateDatasetCallback * callback) override |
| 81 | + { |
| 82 | + if (mActivateDatasetCallback != nullptr) |
| 83 | + { |
| 84 | + callback->OnActivateDatasetComplete(sequenceNum, CHIP_ERROR_INCORRECT_STATE); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + CHIP_ERROR err = mActiveDataset.Init(activeDataset.AsByteSpan()); |
| 89 | + if (err != CHIP_NO_ERROR) |
| 90 | + { |
| 91 | + callback->OnActivateDatasetComplete(sequenceNum, err); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + mActivateDatasetCallback = callback; |
| 96 | + mActivateDatasetSequence = sequenceNum; |
| 97 | + DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(3000), CompleteDatasetActivation, this); |
| 98 | + } |
| 99 | + |
| 100 | + CHIP_ERROR CommitActiveDataset() override { return CHIP_NO_ERROR; } |
| 101 | + CHIP_ERROR RevertActiveDataset() override { return CHIP_ERROR_NOT_IMPLEMENTED; } |
| 102 | + CHIP_ERROR SetPendingDataset(const Thread::OperationalDataset & pendingDataset) override { return CHIP_ERROR_NOT_IMPLEMENTED; } |
| 103 | + |
| 104 | +private: |
| 105 | + static void CompleteDatasetActivation(System::Layer *, void * context) |
| 106 | + { |
| 107 | + auto * self = static_cast<FakeBorderRouterDelegate *>(context); |
| 108 | + auto * callback = self->mActivateDatasetCallback; |
| 109 | + auto sequenceNum = self->mActivateDatasetSequence; |
| 110 | + self->mActivateDatasetCallback = nullptr; |
| 111 | + callback->OnActivateDatasetComplete(sequenceNum, CHIP_NO_ERROR); |
| 112 | + } |
| 113 | + |
| 114 | + AttributeChangeCallback * mAttributeChangeCallback; |
| 115 | + Thread::OperationalDataset mActiveDataset; |
| 116 | + Thread::OperationalDataset mPendingDataset; |
| 117 | + |
| 118 | + ActivateDatasetCallback * mActivateDatasetCallback = nullptr; |
| 119 | + uint32_t mActivateDatasetSequence; |
| 120 | +}; |
| 121 | + |
| 122 | +FakeBorderRouterDelegate gBorderRouterDelegate{}; |
| 123 | +} // namespace |
| 124 | + |
| 125 | +std::optional<ThreadBorderRouterManagement::ServerInstance> gThreadBorderRouterManagementServer; |
| 126 | +void emberAfThreadBorderRouterManagementClusterInitCallback(EndpointId endpoint) |
| 127 | +{ |
| 128 | + VerifyOrDie(!gThreadBorderRouterManagementServer); |
| 129 | + gThreadBorderRouterManagementServer.emplace(endpoint, &gBorderRouterDelegate, Server::GetInstance().GetFailSafeContext()) |
| 130 | + .Init(); |
| 131 | +} |
0 commit comments