|
| 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 <platform/CHIPDeviceConfig.h> |
| 22 | + |
| 23 | +#include "platform/ConnectivityManager.h" |
| 24 | +#include "thread_service/thread_service.rpc.pb.h" |
| 25 | + |
| 26 | +#if CHIP_ENABLE_OPENTHREAD |
| 27 | +#include <openthread/dataset.h> |
| 28 | +#include <platform/ThreadStackManager.h> |
| 29 | +#endif |
| 30 | + |
| 31 | +namespace chip { |
| 32 | +namespace rpc { |
| 33 | + |
| 34 | +// Implementation class for chip.rpc.Thread. |
| 35 | +class Thread : public pw_rpc::nanopb::Thread::Service<Thread> |
| 36 | +{ |
| 37 | +public: |
| 38 | + ::pw::Status GetState(const pw_protobuf_Empty & request, chip_rpc_ThreadState & response) |
| 39 | + { |
| 40 | + response.is_provisioned = DeviceLayer::ConnectivityMgr().IsThreadProvisioned(); |
| 41 | + response.is_enabled = DeviceLayer::ConnectivityMgr().IsThreadEnabled(); |
| 42 | + response.is_attached = DeviceLayer::ConnectivityMgr().IsThreadAttached(); |
| 43 | + response.device_type = static_cast<chip_rpc_ThreadDeviceType>(DeviceLayer::ConnectivityMgr().GetThreadDeviceType()); |
| 44 | + return pw::OkStatus(); |
| 45 | + } |
| 46 | + |
| 47 | + ::pw::Status GetNetworkInfo(const pw_protobuf_Empty & request, chip_rpc_ThreadNetworkInfo & response) |
| 48 | + { |
| 49 | +#if CHIP_ENABLE_OPENTHREAD |
| 50 | + otInstance * otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance(); |
| 51 | + otOperationalDataset data; |
| 52 | + if (OT_ERROR_NONE != otDatasetGetActive(otInst, &data)) |
| 53 | + { |
| 54 | + return pw::Status::NotFound(); |
| 55 | + } |
| 56 | + |
| 57 | + if (data.mComponents.mIsPanIdPresent) |
| 58 | + { |
| 59 | + response.pan_id = data.mPanId; |
| 60 | + } |
| 61 | + if (data.mComponents.mIsNetworkNamePresent) |
| 62 | + { |
| 63 | + snprintf(response.network_name, sizeof(response.network_name), "%s", data.mNetworkName.m8); |
| 64 | + } |
| 65 | + if (data.mComponents.mIsChannelPresent) |
| 66 | + { |
| 67 | + response.channel = data.mChannel; |
| 68 | + } |
| 69 | + if (data.mComponents.mIsExtendedPanIdPresent) |
| 70 | + { |
| 71 | + size_t size = std::min(sizeof(response.extended_pan_id.bytes), sizeof(data.mExtendedPanId)); |
| 72 | + memcpy(response.extended_pan_id.bytes, data.mExtendedPanId.m8, size); |
| 73 | + response.extended_pan_id.size = size; |
| 74 | + } |
| 75 | + return pw::OkStatus(); |
| 76 | +#else // CHIP_ENABLE_OPENTHREAD |
| 77 | + return ::pw::Status::Unimplemented(); |
| 78 | +#endif // CHIP_ENABLE_OPENTHREAD |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +} // namespace rpc |
| 83 | +} // namespace chip |
0 commit comments