yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Evgeny Margolis | c69b6f1 | 2022-02-07 09:34:01 -0800 | [diff] [blame] | 3 | * Copyright (c) 2020-2022 Project CHIP Authors |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 4 | * Copyright (c) 2019-2020 Google LLC. |
| 5 | * Copyright (c) 2013-2018 Nest Labs, Inc. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * @file |
| 23 | * Implementation of the native methods expected by the Python |
| 24 | * version of Chip Device Manager. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 30 | #include <memory> |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <sys/time.h> |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 34 | #include <type_traits> |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | |
| 37 | #include <system/SystemError.h> |
| 38 | #include <system/SystemLayer.h> |
| 39 | |
| 40 | #include <inttypes.h> |
| 41 | #include <net/if.h> |
| 42 | |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 43 | #include <app/DeviceProxy.h> |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 44 | #include <app/InteractionModelEngine.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 45 | #include <app/server/Dnssd.h> |
C Freeman | 8ffe422 | 2022-02-02 14:21:16 -0500 | [diff] [blame] | 46 | #include <controller/AutoCommissioner.h> |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 47 | #include <controller/CHIPDeviceController.h> |
Sagar Dhawan | ae69dd7 | 2021-09-29 15:13:09 -0700 | [diff] [blame] | 48 | #include <controller/CHIPDeviceControllerFactory.h> |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 49 | #include <controller/CommissioningDelegate.h> |
Boris Zbarsky | 710b9d8 | 2022-03-24 16:08:47 -0400 | [diff] [blame] | 50 | #include <controller/CommissioningWindowOpener.h> |
Hamid Abubakr | 94ccbdd | 2023-04-14 03:20:35 +0400 | [diff] [blame] | 51 | #include <controller/CurrentFabricRemover.h> |
Pankaj Garg | 0238a6c | 2021-05-10 07:33:19 -0700 | [diff] [blame] | 52 | #include <controller/ExampleOperationalCredentialsIssuer.h> |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 53 | |
| 54 | #include <controller/python/ChipDeviceController-ScriptDevicePairingDelegate.h> |
Tennessee Carmel-Veilleux | 88bc0b2 | 2022-08-03 01:17:35 -0400 | [diff] [blame] | 55 | #include <controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.h> |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 56 | #include <controller/python/ChipDeviceController-StorageDelegate.h> |
| 57 | #include <controller/python/chip/interaction_model/Delegate.h> |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 58 | #include <controller/python/chip/native/PyChipError.h> |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 59 | |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 60 | #include <credentials/GroupDataProviderImpl.h> |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 61 | #include <credentials/PersistentStorageOpCertStore.h> |
Tennessee Carmel-Veilleux | 22fb6c3 | 2022-02-22 16:49:16 -0500 | [diff] [blame] | 62 | #include <credentials/attestation_verifier/DefaultDeviceAttestationVerifier.h> |
| 63 | #include <credentials/attestation_verifier/DeviceAttestationVerifier.h> |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 64 | #include <crypto/RawKeySessionKeystore.h> |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 65 | #include <inet/IPAddress.h> |
Martin Turon | 82bfcd5 | 2023-01-09 13:30:38 -0800 | [diff] [blame] | 66 | #include <lib/core/TLV.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 67 | #include <lib/dnssd/Resolver.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 68 | #include <lib/support/BytesToHex.h> |
| 69 | #include <lib/support/CHIPMem.h> |
| 70 | #include <lib/support/CodeUtils.h> |
| 71 | #include <lib/support/DLLUtil.h> |
| 72 | #include <lib/support/ScopedBuffer.h> |
| 73 | #include <lib/support/logging/CHIPLogging.h> |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 74 | #include <platform/CHIPDeviceLayer.h> |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 75 | #include <setup_payload/QRCodeSetupPayloadParser.h> |
Boris Zbarsky | 710b9d8 | 2022-03-24 16:08:47 -0400 | [diff] [blame] | 76 | #include <system/SystemClock.h> |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 77 | |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 78 | #include <platform/CommissionableDataProvider.h> |
| 79 | #include <platform/PlatformManager.h> |
| 80 | #include <platform/TestOnlyCommissionableDataProvider.h> |
| 81 | |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 82 | using namespace chip; |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 83 | using namespace chip::Ble; |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 84 | using namespace chip::Controller; |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 85 | using namespace chip::Credentials; |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 86 | using namespace chip::DeviceLayer; |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 87 | |
| 88 | extern "C" { |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 89 | typedef void (*ConstructBytesArrayFunct)(const uint8_t * dataBuf, uint32_t dataLen); |
| 90 | typedef void (*LogMessageFunct)(uint64_t time, uint64_t timeUS, const char * moduleName, uint8_t category, const char * msg); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 91 | typedef void (*DeviceAvailableFunc)(DeviceProxy * device, PyChipError err); |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 92 | typedef void (*ChipThreadTaskRunnerFunct)(intptr_t context); |
Hamid Abubakr | 94ccbdd | 2023-04-14 03:20:35 +0400 | [diff] [blame] | 93 | typedef void (*DeviceUnpairingCompleteFunct)(uint64_t nodeId, PyChipError error); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 96 | namespace { |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 97 | chip::Platform::ScopedMemoryBuffer<uint8_t> sSsidBuf; |
| 98 | chip::Platform::ScopedMemoryBuffer<uint8_t> sCredsBuf; |
| 99 | chip::Platform::ScopedMemoryBuffer<uint8_t> sThreadBuf; |
C Freeman | ac3535b | 2023-07-18 13:32:59 -0400 | [diff] [blame] | 100 | chip::Platform::ScopedMemoryBuffer<char> sDefaultNTPBuf; |
| 101 | app::Clusters::TimeSynchronization::Structs::DSTOffsetStruct::Type sDSTBuf; |
| 102 | app::Clusters::TimeSynchronization::Structs::TimeZoneStruct::Type sTimeZoneBuf; |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 103 | chip::Controller::CommissioningParameters sCommissioningParameters; |
C Freeman | 8ffe422 | 2022-02-02 14:21:16 -0500 | [diff] [blame] | 104 | |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 105 | } // namespace |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 106 | |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 107 | chip::Controller::ScriptDevicePairingDelegate sPairingDelegate; |
Tennessee Carmel-Veilleux | 88bc0b2 | 2022-08-03 01:17:35 -0400 | [diff] [blame] | 108 | chip::Controller::ScriptPairingDeviceDiscoveryDelegate sPairingDeviceDiscoveryDelegate; |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 109 | chip::Credentials::GroupDataProviderImpl sGroupDataProvider; |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 110 | chip::Credentials::PersistentStorageOpCertStore sPersistentStorageOpCertStore; |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 111 | chip::Crypto::RawKeySessionKeystore sSessionKeystore; |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 112 | |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 113 | // NOTE: Remote device ID is in sync with the echo server device id |
| 114 | // At some point, we may want to add an option to connect to a device without |
| 115 | // knowing its id, because the ID can be learned on the first response that is received. |
Kevin Schoedel | 5f19e99 | 2021-03-02 14:36:20 -0500 | [diff] [blame] | 116 | chip::NodeId kDefaultLocalDeviceId = chip::kTestControllerNodeId; |
| 117 | chip::NodeId kRemoteDeviceId = chip::kTestDeviceNodeId; |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 118 | |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 119 | extern "C" { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 120 | PyChipError pychip_DeviceController_StackInit(Controller::Python::StorageAdapter * storageAdapter, bool enableServerInteractions); |
| 121 | PyChipError pychip_DeviceController_StackShutdown(); |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 122 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 123 | PyChipError pychip_DeviceController_NewDeviceController(chip::Controller::DeviceCommissioner ** outDevCtrl, |
| 124 | chip::NodeId localDeviceId, bool useTestCommissioner); |
| 125 | PyChipError pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl); |
| 126 | PyChipError pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, |
| 127 | char * outAddress, uint64_t maxAddressLen, uint16_t * outPort); |
| 128 | PyChipError pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId); |
| 129 | PyChipError pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId); |
| 130 | PyChipError pychip_DeviceController_GetNodeId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outNodeId); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 131 | |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 132 | // Rendezvous |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 133 | PyChipError pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, |
| 134 | uint32_t setupPINCode, chip::NodeId nodeid); |
| 135 | PyChipError pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, |
| 136 | uint32_t setupPINCode, chip::NodeId nodeid); |
| 137 | PyChipError pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, const char * onboardingPayload, |
| 138 | chip::NodeId nodeid); |
Hamid Abubakr | 94ccbdd | 2023-04-14 03:20:35 +0400 | [diff] [blame] | 139 | PyChipError pychip_DeviceController_UnpairDevice(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId remoteDeviceId, |
| 140 | DeviceUnpairingCompleteFunct callback); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 141 | PyChipError pychip_DeviceController_SetThreadOperationalDataset(const char * threadOperationalDataset, uint32_t size); |
| 142 | PyChipError pychip_DeviceController_SetWiFiCredentials(const char * ssid, const char * credentials); |
C Freeman | ac3535b | 2023-07-18 13:32:59 -0400 | [diff] [blame] | 143 | PyChipError pychip_DeviceController_SetTimeZone(int32_t offset, uint64_t validAt); |
| 144 | PyChipError pychip_DeviceController_SetDSTOffset(int32_t offset, uint64_t validStarting, uint64_t validUntil); |
| 145 | PyChipError pychip_DeviceController_SetDefaultNtp(const char * defaultNTP); |
| 146 | PyChipError pychip_DeviceController_SetTrustedTimeSource(chip::NodeId nodeId, chip::EndpointId endpoint); |
| 147 | PyChipError pychip_DeviceController_ResetCommissioningParameters(); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 148 | PyChipError pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid); |
| 149 | PyChipError pychip_DeviceController_EstablishPASESessionIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, |
C Freeman | 5d2beb7 | 2023-07-12 18:23:50 -0400 | [diff] [blame] | 150 | uint32_t setupPINCode, chip::NodeId nodeid, uint16_t port); |
Jerry Johns | 71da650 | 2022-12-11 19:32:16 -0800 | [diff] [blame] | 151 | PyChipError pychip_DeviceController_EstablishPASESessionBLE(chip::Controller::DeviceCommissioner * devCtrl, uint32_t setupPINCode, |
| 152 | uint16_t discriminator, chip::NodeId nodeid); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 153 | PyChipError pychip_DeviceController_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid); |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 154 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 155 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, |
| 156 | uint16_t long_discriminator); |
| 157 | PyChipError pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 158 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 159 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, |
| 160 | uint16_t short_discriminator); |
| 161 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl, |
| 162 | uint16_t vendor); |
| 163 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl, |
| 164 | uint16_t device_type); |
| 165 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl); |
Tennessee Carmel-Veilleux | 88bc0b2 | 2022-08-03 01:17:35 -0400 | [diff] [blame] | 166 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 167 | PyChipError pychip_DeviceController_OnNetworkCommission(chip::Controller::DeviceCommissioner * devCtrl, uint64_t nodeId, |
tianfeng-yang | 8cb1cb7 | 2023-07-19 10:16:33 +0800 | [diff] [blame] | 168 | uint32_t setupPasscode, const uint8_t filterType, const char * filterParam, |
| 169 | uint32_t discoveryTimeoutMsec); |
Tennessee Carmel-Veilleux | 88bc0b2 | 2022-08-03 01:17:35 -0400 | [diff] [blame] | 170 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 171 | PyChipError pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext); |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 172 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 173 | PyChipError pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid, |
| 174 | uint16_t timeout, uint32_t iteration, uint16_t discriminator, |
| 175 | uint8_t optionInt); |
Yufeng Wang | 275368c | 2021-08-31 17:42:23 -0700 | [diff] [blame] | 176 | |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 177 | void pychip_DeviceController_PrintDiscoveredDevices(chip::Controller::DeviceCommissioner * devCtrl); |
| 178 | bool pychip_DeviceController_GetIPForDiscoveredDevice(chip::Controller::DeviceCommissioner * devCtrl, int idx, char * addrStr, |
| 179 | uint32_t len); |
| 180 | |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 181 | // Pairing Delegate |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 182 | PyChipError |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 183 | pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl, |
| 184 | chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback); |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 185 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 186 | PyChipError pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback( |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 187 | chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningCompleteFunct callback); |
| 188 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 189 | PyChipError pychip_ScriptDevicePairingDelegate_SetCommissioningStatusUpdateCallback( |
C Freeman | c3b1810 | 2022-04-04 16:17:09 -0400 | [diff] [blame] | 190 | chip::Controller::DeviceCommissioner * devCtrl, |
| 191 | chip::Controller::DevicePairingDelegate_OnCommissioningStatusUpdateFunct callback); |
C Freeman | 89d538c | 2023-01-26 12:29:48 -0500 | [diff] [blame] | 192 | PyChipError pychip_ScriptDevicePairingDelegate_SetOpenWindowCompleteCallback( |
| 193 | chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnWindowOpenCompleteFunct callback); |
C Freeman | c3b1810 | 2022-04-04 16:17:09 -0400 | [diff] [blame] | 194 | |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 195 | // BLE |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 196 | PyChipError pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl); |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 197 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 198 | uint8_t pychip_DeviceController_GetLogFilter(); |
| 199 | void pychip_DeviceController_SetLogFilter(uint8_t category); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 200 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 201 | const char * pychip_Stack_ErrorToString(ChipError::StorageType err); |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 202 | const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t statusCode); |
| 203 | void pychip_Stack_SetLogFunct(LogMessageFunct logFunct); |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 204 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 205 | PyChipError pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, |
| 206 | DeviceAvailableFunc callback); |
| 207 | PyChipError pychip_FreeOperationalDeviceProxy(chip::OperationalDeviceProxy * deviceProxy); |
| 208 | PyChipError pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId); |
| 209 | PyChipError pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions); |
C Freeman | 83ecf9c | 2023-06-02 18:32:53 -0400 | [diff] [blame] | 210 | PyChipError pychip_GetAttestationChallenge(chip::OperationalDeviceProxy * deviceProxy, uint8_t * buf, size_t * size); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 211 | PyChipError pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, |
| 212 | CommissioneeDeviceProxy ** proxy); |
| 213 | PyChipError pychip_ExpireSessions(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId); |
Tennessee Carmel-Veilleux | c3cd004 | 2022-07-08 13:49:30 -0400 | [diff] [blame] | 214 | |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 215 | uint64_t pychip_GetCommandSenderHandle(chip::DeviceProxy * device); |
Song GUO | 75f3895 | 2021-10-13 04:13:10 +0800 | [diff] [blame] | 216 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 217 | PyChipError pychip_InteractionModel_ShutdownSubscription(SubscriptionId subscriptionId); |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 218 | |
| 219 | // |
| 220 | // Storage |
| 221 | // |
Jerry Johns | c517ba2 | 2022-08-18 09:48:09 -0700 | [diff] [blame] | 222 | void * pychip_Storage_InitializeStorageAdapter(chip::Controller::Python::PyObject * context, |
| 223 | chip::Controller::Python::SyncSetKeyValueCb setCb, |
| 224 | chip::Controller::Python::SetGetKeyValueCb getCb, |
| 225 | chip::Controller::Python::SyncDeleteKeyValueCb deleteCb); |
| 226 | void pychip_Storage_ShutdownAdapter(chip::Controller::Python::StorageAdapter * storageAdapter); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Jerry Johns | c517ba2 | 2022-08-18 09:48:09 -0700 | [diff] [blame] | 229 | void * pychip_Storage_InitializeStorageAdapter(chip::Controller::Python::PyObject * context, |
| 230 | chip::Controller::Python::SyncSetKeyValueCb setCb, |
| 231 | chip::Controller::Python::SetGetKeyValueCb getCb, |
| 232 | chip::Controller::Python::SyncDeleteKeyValueCb deleteCb) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 233 | { |
Jerry Johns | c517ba2 | 2022-08-18 09:48:09 -0700 | [diff] [blame] | 234 | auto ptr = new chip::Controller::Python::StorageAdapter(context, setCb, getCb, deleteCb); |
| 235 | return ptr; |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 236 | } |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 237 | |
Jerry Johns | c517ba2 | 2022-08-18 09:48:09 -0700 | [diff] [blame] | 238 | void pychip_Storage_ShutdownAdapter(chip::Controller::Python::StorageAdapter * storageAdapter) |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 239 | { |
Jerry Johns | c517ba2 | 2022-08-18 09:48:09 -0700 | [diff] [blame] | 240 | delete storageAdapter; |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 241 | } |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 242 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 243 | PyChipError pychip_DeviceController_StackInit(Controller::Python::StorageAdapter * storageAdapter, bool enableServerInteractions) |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 244 | { |
Jerry Johns | c517ba2 | 2022-08-18 09:48:09 -0700 | [diff] [blame] | 245 | VerifyOrDie(storageAdapter != nullptr); |
Sagar Dhawan | f0bbca7 | 2021-10-27 17:23:34 -0500 | [diff] [blame] | 246 | |
Sagar Dhawan | ae69dd7 | 2021-09-29 15:13:09 -0700 | [diff] [blame] | 247 | FactoryInitParams factoryParams; |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 248 | |
Jerry Johns | c517ba2 | 2022-08-18 09:48:09 -0700 | [diff] [blame] | 249 | factoryParams.fabricIndependentStorage = storageAdapter; |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 250 | factoryParams.sessionKeystore = &sSessionKeystore; |
Jerry Johns | c517ba2 | 2022-08-18 09:48:09 -0700 | [diff] [blame] | 251 | |
| 252 | sGroupDataProvider.SetStorageDelegate(storageAdapter); |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 253 | sGroupDataProvider.SetSessionKeystore(factoryParams.sessionKeystore); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 254 | PyReturnErrorOnFailure(ToPyChipError(sGroupDataProvider.Init())); |
Terence Hampson | ec2b5e3 | 2023-02-22 13:22:02 -0500 | [diff] [blame] | 255 | Credentials::SetGroupDataProvider(&sGroupDataProvider); |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 256 | factoryParams.groupDataProvider = &sGroupDataProvider; |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 257 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 258 | PyReturnErrorOnFailure(ToPyChipError(sPersistentStorageOpCertStore.Init(storageAdapter))); |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 259 | factoryParams.opCertStore = &sPersistentStorageOpCertStore; |
| 260 | |
Song GUO | 59fbe3a | 2022-09-08 00:28:36 +0800 | [diff] [blame] | 261 | factoryParams.enableServerInteractions = enableServerInteractions; |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 262 | |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 263 | // Hack needed due to the fact that DnsSd server uses the CommissionableDataProvider even |
| 264 | // when never starting commissionable advertising. This will not be used but prevents |
| 265 | // null pointer dereferences. |
| 266 | static chip::DeviceLayer::TestOnlyCommissionableDataProvider TestOnlyCommissionableDataProvider; |
| 267 | chip::DeviceLayer::SetCommissionableDataProvider(&TestOnlyCommissionableDataProvider); |
| 268 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 269 | PyReturnErrorOnFailure(ToPyChipError(DeviceControllerFactory::GetInstance().Init(factoryParams))); |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 270 | |
| 271 | // |
| 272 | // In situations where all the controller instances get shutdown, the entire stack is then also |
| 273 | // implicitly shutdown. In the REPL, users can create such a situation by manually shutting down |
| 274 | // controllers (for example, when they call ChipReplStartup::LoadFabricAdmins multiple times). In |
| 275 | // that situation, momentarily, the stack gets de-initialized. This results in further interactions with |
| 276 | // the stack being dangerous (and in fact, causes crashes). |
| 277 | // |
| 278 | // This retain call ensures the stack doesn't get de-initialized in the REPL. |
| 279 | // |
| 280 | DeviceControllerFactory::GetInstance().RetainSystemState(); |
| 281 | |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 282 | // |
| 283 | // Finally, start up the main Matter thread. Any further interactions with the stack |
| 284 | // will now need to happen on the Matter thread, OR protected with the stack lock. |
| 285 | // |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 286 | PyReturnErrorOnFailure(ToPyChipError(chip::DeviceLayer::PlatformMgr().StartEventLoopTask())); |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 287 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 288 | return ToPyChipError(CHIP_NO_ERROR); |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 291 | PyChipError pychip_DeviceController_StackShutdown() |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 292 | { |
| 293 | ChipLogError(Controller, "Shutting down the stack..."); |
| 294 | |
| 295 | // |
| 296 | // Let's stop the Matter thread, and wait till the event loop has stopped. |
| 297 | // |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 298 | PyReturnErrorOnFailure(ToPyChipError(chip::DeviceLayer::PlatformMgr().StopEventLoopTask())); |
Jerry Johns | a7a70ae | 2022-07-07 14:48:18 -0700 | [diff] [blame] | 299 | |
| 300 | // |
| 301 | // There is the symmetric call to match the Retain called at stack initialization |
| 302 | // time. This will release all resources (if there are no other controllers active). |
| 303 | // |
| 304 | DeviceControllerFactory::GetInstance().ReleaseSystemState(); |
| 305 | |
| 306 | DeviceControllerFactory::GetInstance().Shutdown(); |
| 307 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 308 | return ToPyChipError(CHIP_NO_ERROR); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 311 | PyChipError pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, |
| 312 | char * outAddress, uint64_t maxAddressLen, uint16_t * outPort) |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 313 | { |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 314 | Inet::IPAddress address; |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 315 | PyReturnErrorOnFailure(ToPyChipError(devCtrl->GetPeerAddressAndPort(nodeId, address, *outPort))); |
Jerry Johns | d94cbdc | 2022-07-26 08:07:06 -0700 | [diff] [blame] | 316 | VerifyOrReturnError(address.ToString(outAddress, static_cast<uint32_t>(maxAddressLen)), |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 317 | ToPyChipError(CHIP_ERROR_BUFFER_TOO_SMALL)); |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 318 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 319 | return ToPyChipError(CHIP_NO_ERROR); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 322 | PyChipError pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId) |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 323 | { |
| 324 | *outFabricId = devCtrl->GetCompressedFabricId(); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 325 | return ToPyChipError(CHIP_NO_ERROR); |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 328 | PyChipError pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId) |
Yufeng Wang | b728b74 | 2021-06-16 08:04:17 -0700 | [diff] [blame] | 329 | { |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 330 | *outFabricId = devCtrl->GetFabricId(); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 331 | return ToPyChipError(CHIP_NO_ERROR); |
Yufeng Wang | b728b74 | 2021-06-16 08:04:17 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 334 | PyChipError pychip_DeviceController_GetNodeId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outNodeId) |
tehampson | f3be2ee | 2022-06-23 13:10:52 -0400 | [diff] [blame] | 335 | { |
| 336 | *outNodeId = devCtrl->GetNodeId(); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 337 | return ToPyChipError(CHIP_NO_ERROR); |
tehampson | f3be2ee | 2022-06-23 13:10:52 -0400 | [diff] [blame] | 338 | } |
| 339 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 340 | const char * pychip_DeviceController_ErrorToString(ChipError::StorageType err) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 341 | { |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 342 | return chip::ErrorStr(CHIP_ERROR(err)); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 345 | const char * pychip_DeviceController_StatusReportToString(uint32_t profileId, uint16_t statusCode) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 346 | { |
| 347 | // return chip::StatusReportStr(profileId, statusCode); |
Andrei Litvin | 697b155 | 2022-04-06 10:45:26 -1000 | [diff] [blame] | 348 | return nullptr; |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 351 | uint8_t pychip_DeviceController_GetLogFilter() |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 352 | { |
| 353 | #if _CHIP_USE_LOGGING |
| 354 | return chip::Logging::GetLogFilter(); |
| 355 | #else |
| 356 | return chip::Logging::kLogCategory_None; |
| 357 | #endif |
| 358 | } |
| 359 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 360 | void pychip_DeviceController_SetLogFilter(uint8_t category) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 361 | { |
| 362 | #if _CHIP_USE_LOGGING |
| 363 | chip::Logging::SetLogFilter(category); |
| 364 | #endif |
| 365 | } |
| 366 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 367 | PyChipError pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, |
| 368 | uint32_t setupPINCode, chip::NodeId nodeid) |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 369 | { |
Terence Hampson | 11c0cdf | 2023-02-09 12:40:19 -0500 | [diff] [blame] | 370 | sPairingDelegate.SetExpectingPairingComplete(true); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 371 | return ToPyChipError(devCtrl->PairDevice(nodeid, |
| 372 | chip::RendezvousParameters() |
| 373 | .SetPeerAddress(Transport::PeerAddress(Transport::Type::kBle)) |
| 374 | .SetSetupPINCode(setupPINCode) |
| 375 | .SetDiscriminator(discriminator), |
| 376 | sCommissioningParameters)); |
Song Guo | b94c980 | 2021-01-29 13:58:48 +0800 | [diff] [blame] | 377 | } |
| 378 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 379 | PyChipError pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, |
| 380 | uint32_t setupPINCode, chip::NodeId nodeid) |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 381 | { |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 382 | chip::Inet::IPAddress peerAddr; |
| 383 | chip::Transport::PeerAddress addr; |
| 384 | chip::RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode); |
| 385 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 386 | VerifyOrReturnError(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 387 | |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 388 | // TODO: IP rendezvous should use TCP connection. |
Andrei Litvin | e68bd6c | 2022-02-11 11:29:49 -0500 | [diff] [blame] | 389 | addr.SetTransportType(chip::Transport::Type::kUdp).SetIPAddress(peerAddr); |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 390 | params.SetPeerAddress(addr).SetDiscriminator(0); |
Jerry Johns | f68cd64 | 2022-01-28 16:39:08 -0800 | [diff] [blame] | 391 | |
Terence Hampson | 11c0cdf | 2023-02-09 12:40:19 -0500 | [diff] [blame] | 392 | sPairingDelegate.SetExpectingPairingComplete(true); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 393 | return ToPyChipError(devCtrl->PairDevice(nodeid, params, sCommissioningParameters)); |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 394 | } |
| 395 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 396 | PyChipError pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, const char * onboardingPayload, |
| 397 | chip::NodeId nodeid) |
Song GUO | aaad739 | 2022-05-19 23:30:37 +0800 | [diff] [blame] | 398 | { |
Terence Hampson | 11c0cdf | 2023-02-09 12:40:19 -0500 | [diff] [blame] | 399 | sPairingDelegate.SetExpectingPairingComplete(true); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 400 | return ToPyChipError(devCtrl->PairDevice(nodeid, onboardingPayload, sCommissioningParameters)); |
Song GUO | aaad739 | 2022-05-19 23:30:37 +0800 | [diff] [blame] | 401 | } |
| 402 | |
Hamid Abubakr | 94ccbdd | 2023-04-14 03:20:35 +0400 | [diff] [blame] | 403 | namespace { |
| 404 | struct UnpairDeviceCallback |
| 405 | { |
| 406 | UnpairDeviceCallback(DeviceUnpairingCompleteFunct callback, chip::Controller::CurrentFabricRemover * remover) : |
| 407 | mOnCurrentFabricRemove(OnCurrentFabricRemoveFn, this), mCallback(callback), mRemover(remover) |
| 408 | {} |
| 409 | |
| 410 | static void OnCurrentFabricRemoveFn(void * context, chip::NodeId nodeId, CHIP_ERROR error) |
| 411 | { |
| 412 | auto * self = static_cast<UnpairDeviceCallback *>(context); |
| 413 | self->mCallback(nodeId, ToPyChipError(error)); |
| 414 | delete self->mRemover; |
| 415 | delete self; |
| 416 | } |
| 417 | |
| 418 | Callback::Callback<OnCurrentFabricRemove> mOnCurrentFabricRemove; |
| 419 | DeviceUnpairingCompleteFunct mCallback; |
| 420 | chip::Controller::CurrentFabricRemover * mRemover; |
| 421 | }; |
| 422 | } // anonymous namespace |
| 423 | |
| 424 | PyChipError pychip_DeviceController_UnpairDevice(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid, |
| 425 | DeviceUnpairingCompleteFunct callback) |
| 426 | { |
| 427 | // Create a new CurrentFabricRemover instance |
| 428 | auto * fabricRemover = new chip::Controller::CurrentFabricRemover(devCtrl); |
| 429 | |
| 430 | auto * callbacks = new UnpairDeviceCallback(callback, fabricRemover); |
| 431 | |
| 432 | // Pass the callback and nodeid to the RemoveCurrentFabric function |
| 433 | CHIP_ERROR err = fabricRemover->RemoveCurrentFabric(nodeid, &callbacks->mOnCurrentFabricRemove); |
| 434 | if (err != CHIP_NO_ERROR) |
| 435 | { |
| 436 | delete fabricRemover; |
| 437 | delete callbacks; |
| 438 | } |
| 439 | // Else will clean up when the callback is called. |
| 440 | return ToPyChipError(err); |
| 441 | } |
| 442 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 443 | PyChipError pychip_DeviceController_OnNetworkCommission(chip::Controller::DeviceCommissioner * devCtrl, uint64_t nodeId, |
tianfeng-yang | 8cb1cb7 | 2023-07-19 10:16:33 +0800 | [diff] [blame] | 444 | uint32_t setupPasscode, const uint8_t filterType, const char * filterParam, |
| 445 | uint32_t discoveryTimeoutMsec) |
Tennessee Carmel-Veilleux | 88bc0b2 | 2022-08-03 01:17:35 -0400 | [diff] [blame] | 446 | { |
| 447 | Dnssd::DiscoveryFilter filter(static_cast<Dnssd::DiscoveryFilterType>(filterType)); |
| 448 | switch (static_cast<Dnssd::DiscoveryFilterType>(filterType)) |
| 449 | { |
| 450 | case chip::Dnssd::DiscoveryFilterType::kNone: |
| 451 | break; |
| 452 | case chip::Dnssd::DiscoveryFilterType::kShortDiscriminator: |
| 453 | case chip::Dnssd::DiscoveryFilterType::kLongDiscriminator: |
| 454 | case chip::Dnssd::DiscoveryFilterType::kCompressedFabricId: |
| 455 | case chip::Dnssd::DiscoveryFilterType::kVendorId: |
| 456 | case chip::Dnssd::DiscoveryFilterType::kDeviceType: { |
| 457 | // For any numerical filter, convert the string to a filter value |
| 458 | errno = 0; |
| 459 | unsigned long long int numericalArg = strtoull(filterParam, nullptr, 0); |
| 460 | if ((numericalArg == ULLONG_MAX) && (errno == ERANGE)) |
| 461 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 462 | return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); |
Tennessee Carmel-Veilleux | 88bc0b2 | 2022-08-03 01:17:35 -0400 | [diff] [blame] | 463 | } |
| 464 | filter.code = static_cast<uint64_t>(numericalArg); |
| 465 | break; |
| 466 | } |
| 467 | case chip::Dnssd::DiscoveryFilterType::kCommissioningMode: |
| 468 | break; |
| 469 | case chip::Dnssd::DiscoveryFilterType::kCommissioner: |
| 470 | filter.code = 1; |
| 471 | break; |
| 472 | case chip::Dnssd::DiscoveryFilterType::kInstanceName: |
| 473 | filter.code = 0; |
| 474 | filter.instanceName = filterParam; |
| 475 | break; |
| 476 | default: |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 477 | return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); |
Tennessee Carmel-Veilleux | 88bc0b2 | 2022-08-03 01:17:35 -0400 | [diff] [blame] | 478 | } |
| 479 | |
tianfeng-yang | 8cb1cb7 | 2023-07-19 10:16:33 +0800 | [diff] [blame] | 480 | sPairingDelegate.SetExpectingPairingComplete(true); |
| 481 | CHIP_ERROR err = sPairingDeviceDiscoveryDelegate.Init(nodeId, setupPasscode, sCommissioningParameters, &sPairingDelegate, |
| 482 | devCtrl, discoveryTimeoutMsec); |
| 483 | VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 484 | return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); |
Tennessee Carmel-Veilleux | 88bc0b2 | 2022-08-03 01:17:35 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 487 | PyChipError pychip_DeviceController_SetThreadOperationalDataset(const char * threadOperationalDataset, uint32_t size) |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 488 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 489 | ReturnErrorCodeIf(!sThreadBuf.Alloc(size), ToPyChipError(CHIP_ERROR_NO_MEMORY)); |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 490 | memcpy(sThreadBuf.Get(), threadOperationalDataset, size); |
| 491 | sCommissioningParameters.SetThreadOperationalDataset(ByteSpan(sThreadBuf.Get(), size)); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 492 | return ToPyChipError(CHIP_NO_ERROR); |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 493 | } |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 494 | PyChipError pychip_DeviceController_SetWiFiCredentials(const char * ssid, const char * credentials) |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 495 | { |
| 496 | size_t ssidSize = strlen(ssid); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 497 | ReturnErrorCodeIf(!sSsidBuf.Alloc(ssidSize), ToPyChipError(CHIP_ERROR_NO_MEMORY)); |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 498 | memcpy(sSsidBuf.Get(), ssid, ssidSize); |
| 499 | |
| 500 | size_t credsSize = strlen(credentials); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 501 | ReturnErrorCodeIf(!sCredsBuf.Alloc(credsSize), ToPyChipError(CHIP_ERROR_NO_MEMORY)); |
C Freeman | 8bedafa | 2021-12-15 09:27:51 -0500 | [diff] [blame] | 502 | memcpy(sCredsBuf.Get(), credentials, credsSize); |
| 503 | |
Vivien Nicolas | 4972181 | 2022-01-06 20:34:14 +0100 | [diff] [blame] | 504 | sCommissioningParameters.SetWiFiCredentials( |
| 505 | chip::Controller::WiFiCredentials(ByteSpan(sSsidBuf.Get(), ssidSize), ByteSpan(sCredsBuf.Get(), credsSize))); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 506 | return ToPyChipError(CHIP_NO_ERROR); |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 507 | } |
| 508 | |
C Freeman | ac3535b | 2023-07-18 13:32:59 -0400 | [diff] [blame] | 509 | PyChipError pychip_DeviceController_SetTimeZone(int32_t offset, uint64_t validAt) |
| 510 | { |
| 511 | sTimeZoneBuf.offset = offset; |
| 512 | sTimeZoneBuf.validAt = validAt; |
| 513 | app::DataModel::List<app::Clusters::TimeSynchronization::Structs::TimeZoneStruct::Type> list(&sTimeZoneBuf, 1); |
| 514 | sCommissioningParameters.SetTimeZone(list); |
| 515 | return ToPyChipError(CHIP_NO_ERROR); |
| 516 | } |
| 517 | PyChipError pychip_DeviceController_SetDSTOffset(int32_t offset, uint64_t validStarting, uint64_t validUntil) |
| 518 | { |
| 519 | sDSTBuf.offset = offset; |
| 520 | sDSTBuf.validStarting = validStarting; |
| 521 | sDSTBuf.validUntil = chip::app::DataModel::MakeNullable(validUntil); |
| 522 | app::DataModel::List<app::Clusters::TimeSynchronization::Structs::DSTOffsetStruct::Type> list(&sDSTBuf, 1); |
| 523 | sCommissioningParameters.SetDSTOffsets(list); |
| 524 | return ToPyChipError(CHIP_NO_ERROR); |
| 525 | } |
| 526 | PyChipError pychip_DeviceController_SetDefaultNtp(const char * defaultNTP) |
| 527 | { |
| 528 | size_t len = strlen(defaultNTP); |
| 529 | ReturnErrorCodeIf(!sDefaultNTPBuf.Alloc(len), ToPyChipError(CHIP_ERROR_NO_MEMORY)); |
| 530 | memcpy(sDefaultNTPBuf.Get(), defaultNTP, len); |
| 531 | sCommissioningParameters.SetDefaultNTP(chip::app::DataModel::MakeNullable(CharSpan(sDefaultNTPBuf.Get(), len))); |
| 532 | return ToPyChipError(CHIP_NO_ERROR); |
| 533 | } |
| 534 | |
| 535 | PyChipError pychip_DeviceController_SetTrustedTimeSource(chip::NodeId nodeId, chip::EndpointId endpoint) |
| 536 | { |
| 537 | chip::app::Clusters::TimeSynchronization::Structs::FabricScopedTrustedTimeSourceStruct::Type timeSource = { .nodeID = nodeId, |
| 538 | .endpoint = |
| 539 | endpoint }; |
| 540 | sCommissioningParameters.SetTrustedTimeSource(chip::app::DataModel::MakeNullable(timeSource)); |
| 541 | return ToPyChipError(CHIP_NO_ERROR); |
| 542 | } |
| 543 | |
| 544 | PyChipError pychip_DeviceController_ResetCommissioningParameters() |
| 545 | { |
| 546 | sCommissioningParameters = CommissioningParameters(); |
| 547 | return ToPyChipError(CHIP_NO_ERROR); |
| 548 | } |
| 549 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 550 | PyChipError pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid) |
Damian Królik | 88cd766 | 2021-07-17 01:00:54 +0200 | [diff] [blame] | 551 | { |
Jerry Johns | 0eed15d | 2022-08-01 17:56:38 -0700 | [diff] [blame] | 552 | // |
| 553 | // Since we permit multiple controllers per fabric and each is associated with a unique fabric index, closing a session |
| 554 | // requires us to do so across all controllers on the same logical fabric. |
| 555 | // |
Terence Hampson | 9dbfa91 | 2022-08-15 19:23:32 -0400 | [diff] [blame] | 556 | devCtrl->SessionMgr()->ForEachMatchingSessionOnLogicalFabric(ScopedNodeId(nodeid, devCtrl->GetFabricIndex()), |
| 557 | [](auto * session) { |
| 558 | if (session->IsActiveSession()) |
| 559 | { |
| 560 | session->MarkAsDefunct(); |
| 561 | } |
| 562 | }); |
Jerry Johns | 0eed15d | 2022-08-01 17:56:38 -0700 | [diff] [blame] | 563 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 564 | return ToPyChipError(CHIP_NO_ERROR); |
Damian Królik | 88cd766 | 2021-07-17 01:00:54 +0200 | [diff] [blame] | 565 | } |
Andrei Litvin | 5a28865 | 2022-03-11 16:23:36 -0500 | [diff] [blame] | 566 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 567 | PyChipError pychip_DeviceController_EstablishPASESessionIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, |
C Freeman | 5d2beb7 | 2023-07-12 18:23:50 -0400 | [diff] [blame] | 568 | uint32_t setupPINCode, chip::NodeId nodeid, uint16_t port) |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 569 | { |
| 570 | chip::Inet::IPAddress peerAddr; |
| 571 | chip::Transport::PeerAddress addr; |
| 572 | RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 573 | VerifyOrReturnError(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); |
Andrei Litvin | e68bd6c | 2022-02-11 11:29:49 -0500 | [diff] [blame] | 574 | addr.SetTransportType(chip::Transport::Type::kUdp).SetIPAddress(peerAddr); |
C Freeman | 5d2beb7 | 2023-07-12 18:23:50 -0400 | [diff] [blame] | 575 | if (port != 0) |
| 576 | { |
| 577 | addr.SetPort(port); |
| 578 | } |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 579 | params.SetPeerAddress(addr).SetDiscriminator(0); |
Terence Hampson | 11c0cdf | 2023-02-09 12:40:19 -0500 | [diff] [blame] | 580 | sPairingDelegate.SetExpectingPairingComplete(true); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 581 | return ToPyChipError(devCtrl->EstablishPASEConnection(nodeid, params)); |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 582 | } |
Jerry Johns | 71da650 | 2022-12-11 19:32:16 -0800 | [diff] [blame] | 583 | |
| 584 | PyChipError pychip_DeviceController_EstablishPASESessionBLE(chip::Controller::DeviceCommissioner * devCtrl, uint32_t setupPINCode, |
| 585 | uint16_t discriminator, chip::NodeId nodeid) |
| 586 | { |
| 587 | chip::Transport::PeerAddress addr; |
| 588 | RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode); |
| 589 | addr.SetTransportType(chip::Transport::Type::kBle); |
| 590 | params.SetPeerAddress(addr).SetDiscriminator(discriminator); |
Terence Hampson | 11c0cdf | 2023-02-09 12:40:19 -0500 | [diff] [blame] | 591 | sPairingDelegate.SetExpectingPairingComplete(true); |
Jerry Johns | 71da650 | 2022-12-11 19:32:16 -0800 | [diff] [blame] | 592 | return ToPyChipError(devCtrl->EstablishPASEConnection(nodeid, params)); |
| 593 | } |
| 594 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 595 | PyChipError pychip_DeviceController_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid) |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 596 | { |
| 597 | CommissioningParameters params; |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 598 | return ToPyChipError(devCtrl->Commission(nodeid, params)); |
C Freeman | aa71969 | 2021-12-03 11:45:30 -0500 | [diff] [blame] | 599 | } |
Damian Królik | 88cd766 | 2021-07-17 01:00:54 +0200 | [diff] [blame] | 600 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 601 | PyChipError pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl) |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 602 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 603 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, static_cast<uint64_t>(0)); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 604 | return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 607 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, |
| 608 | uint16_t long_discriminator) |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 609 | { |
Vivien Nicolas | 8e40abc | 2021-12-16 17:12:37 +0100 | [diff] [blame] | 610 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kLongDiscriminator, long_discriminator); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 611 | return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 612 | } |
| 613 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 614 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, |
| 615 | uint16_t short_discriminator) |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 616 | { |
Vivien Nicolas | 8e40abc | 2021-12-16 17:12:37 +0100 | [diff] [blame] | 617 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kShortDiscriminator, short_discriminator); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 618 | return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 619 | } |
| 620 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 621 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl, |
| 622 | uint16_t vendor) |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 623 | { |
Vivien Nicolas | 8e40abc | 2021-12-16 17:12:37 +0100 | [diff] [blame] | 624 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kVendorId, vendor); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 625 | return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 628 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl, |
| 629 | uint16_t device_type) |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 630 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 631 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kDeviceType, device_type); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 632 | return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 633 | } |
| 634 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 635 | PyChipError pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl) |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 636 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 637 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kCommissioningMode); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 638 | return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 639 | } |
| 640 | |
C Freeman | 89d538c | 2023-01-26 12:29:48 -0500 | [diff] [blame] | 641 | PyChipError pychip_ScriptDevicePairingDelegate_SetOpenWindowCompleteCallback( |
| 642 | chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnWindowOpenCompleteFunct callback) |
| 643 | { |
| 644 | sPairingDelegate.SetCommissioningWindowOpenCallback(callback); |
| 645 | return ToPyChipError(CHIP_NO_ERROR); |
| 646 | } |
| 647 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 648 | PyChipError pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid, |
| 649 | uint16_t timeout, uint32_t iteration, uint16_t discriminator, |
| 650 | uint8_t optionInt) |
Yufeng Wang | 275368c | 2021-08-31 17:42:23 -0700 | [diff] [blame] | 651 | { |
Boris Zbarsky | 710b9d8 | 2022-03-24 16:08:47 -0400 | [diff] [blame] | 652 | const auto option = static_cast<Controller::CommissioningWindowOpener::CommissioningWindowOption>(optionInt); |
| 653 | if (option == Controller::CommissioningWindowOpener::CommissioningWindowOption::kOriginalSetupCode) |
| 654 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 655 | return ToPyChipError(Controller::AutoCommissioningWindowOpener::OpenBasicCommissioningWindow( |
| 656 | devCtrl, nodeid, System::Clock::Seconds16(timeout))); |
Boris Zbarsky | 710b9d8 | 2022-03-24 16:08:47 -0400 | [diff] [blame] | 657 | } |
Damian Królik | cb432c8 | 2022-02-12 07:38:49 +0100 | [diff] [blame] | 658 | |
Boris Zbarsky | 710b9d8 | 2022-03-24 16:08:47 -0400 | [diff] [blame] | 659 | if (option == Controller::CommissioningWindowOpener::CommissioningWindowOption::kTokenWithRandomPIN) |
| 660 | { |
| 661 | SetupPayload payload; |
C Freeman | 89d538c | 2023-01-26 12:29:48 -0500 | [diff] [blame] | 662 | auto opener = |
| 663 | Platform::New<Controller::CommissioningWindowOpener>(static_cast<chip::Controller::DeviceController *>(devCtrl)); |
| 664 | PyChipError err = ToPyChipError(opener->OpenCommissioningWindow(nodeid, System::Clock::Seconds16(timeout), iteration, |
| 665 | discriminator, NullOptional, NullOptional, |
| 666 | sPairingDelegate.GetOpenWindowCallback(opener), payload)); |
| 667 | return err; |
Boris Zbarsky | 710b9d8 | 2022-03-24 16:08:47 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 670 | return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); |
Yufeng Wang | 275368c | 2021-08-31 17:42:23 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 673 | PyChipError |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 674 | pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl, |
| 675 | chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback) |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 676 | { |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 677 | sPairingDelegate.SetKeyExchangeCallback(callback); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 678 | return ToPyChipError(CHIP_NO_ERROR); |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 679 | } |
| 680 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 681 | PyChipError pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback( |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 682 | chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningCompleteFunct callback) |
| 683 | { |
| 684 | sPairingDelegate.SetCommissioningCompleteCallback(callback); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 685 | return ToPyChipError(CHIP_NO_ERROR); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 686 | } |
| 687 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 688 | PyChipError pychip_ScriptDevicePairingDelegate_SetCommissioningStatusUpdateCallback( |
C Freeman | c3b1810 | 2022-04-04 16:17:09 -0400 | [diff] [blame] | 689 | chip::Controller::DeviceCommissioner * devCtrl, |
| 690 | chip::Controller::DevicePairingDelegate_OnCommissioningStatusUpdateFunct callback) |
| 691 | { |
| 692 | sPairingDelegate.SetCommissioningStatusUpdateCallback(callback); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 693 | return ToPyChipError(CHIP_NO_ERROR); |
C Freeman | c3b1810 | 2022-04-04 16:17:09 -0400 | [diff] [blame] | 694 | } |
| 695 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 696 | const char * pychip_Stack_ErrorToString(ChipError::StorageType err) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 697 | { |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 698 | return chip::ErrorStr(CHIP_ERROR(err)); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 701 | const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t statusCode) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 702 | { |
| 703 | // return chip::StatusReportStr(profileId, statusCode); |
Andrei Litvin | 697b155 | 2022-04-06 10:45:26 -1000 | [diff] [blame] | 704 | return nullptr; |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 707 | namespace { |
Terence Hampson | b6859d7 | 2022-08-05 13:48:11 -0400 | [diff] [blame] | 708 | |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 709 | struct GetDeviceCallbacks |
| 710 | { |
| 711 | GetDeviceCallbacks(DeviceAvailableFunc callback) : |
| 712 | mOnSuccess(OnDeviceConnectedFn, this), mOnFailure(OnConnectionFailureFn, this), mCallback(callback) |
| 713 | {} |
| 714 | |
Marcos B | cae6319 | 2023-01-13 14:53:14 -0700 | [diff] [blame] | 715 | static void OnDeviceConnectedFn(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle) |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 716 | { |
Terence Hampson | b6859d7 | 2022-08-05 13:48:11 -0400 | [diff] [blame] | 717 | auto * self = static_cast<GetDeviceCallbacks *>(context); |
| 718 | auto * operationalDeviceProxy = new OperationalDeviceProxy(&exchangeMgr, sessionHandle); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 719 | self->mCallback(operationalDeviceProxy, ToPyChipError(CHIP_NO_ERROR)); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 720 | delete self; |
| 721 | } |
| 722 | |
Jerry Johns | d94cbdc | 2022-07-26 08:07:06 -0700 | [diff] [blame] | 723 | static void OnConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR error) |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 724 | { |
| 725 | auto * self = static_cast<GetDeviceCallbacks *>(context); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 726 | self->mCallback(nullptr, ToPyChipError(error)); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 727 | delete self; |
| 728 | } |
| 729 | |
| 730 | Callback::Callback<OnDeviceConnected> mOnSuccess; |
| 731 | Callback::Callback<OnDeviceConnectionFailure> mOnFailure; |
| 732 | DeviceAvailableFunc mCallback; |
| 733 | }; |
| 734 | } // anonymous namespace |
| 735 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 736 | PyChipError pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, |
| 737 | DeviceAvailableFunc callback) |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 738 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 739 | VerifyOrReturnError(devCtrl != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 740 | auto * callbacks = new GetDeviceCallbacks(callback); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 741 | return ToPyChipError(devCtrl->GetConnectedDevice(nodeId, &callbacks->mOnSuccess, &callbacks->mOnFailure)); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 742 | } |
| 743 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 744 | PyChipError pychip_FreeOperationalDeviceProxy(chip::OperationalDeviceProxy * deviceProxy) |
Terence Hampson | b6859d7 | 2022-08-05 13:48:11 -0400 | [diff] [blame] | 745 | { |
| 746 | if (deviceProxy != nullptr) |
| 747 | { |
| 748 | delete deviceProxy; |
| 749 | } |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 750 | return ToPyChipError(CHIP_NO_ERROR); |
Terence Hampson | b6859d7 | 2022-08-05 13:48:11 -0400 | [diff] [blame] | 751 | } |
| 752 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 753 | PyChipError pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId) |
Jerry Johns | 6aa835e | 2022-08-28 00:11:35 -0400 | [diff] [blame] | 754 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 755 | VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), ToPyChipError(CHIP_ERROR_MISSING_SECURE_SESSION)); |
| 756 | VerifyOrReturnError(localSessionId != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); |
Jerry Johns | 6aa835e | 2022-08-28 00:11:35 -0400 | [diff] [blame] | 757 | |
| 758 | *localSessionId = deviceProxy->GetSecureSession().Value()->AsSecureSession()->GetLocalSessionId(); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 759 | return ToPyChipError(CHIP_NO_ERROR); |
Jerry Johns | 6aa835e | 2022-08-28 00:11:35 -0400 | [diff] [blame] | 760 | } |
| 761 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 762 | PyChipError pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions) |
Jerry Johns | 6aa835e | 2022-08-28 00:11:35 -0400 | [diff] [blame] | 763 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 764 | VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), ToPyChipError(CHIP_ERROR_MISSING_SECURE_SESSION)); |
| 765 | VerifyOrReturnError(numSessions != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); |
Jerry Johns | 6aa835e | 2022-08-28 00:11:35 -0400 | [diff] [blame] | 766 | |
| 767 | *numSessions = 0; |
| 768 | deviceProxy->GetExchangeManager()->GetSessionManager()->ForEachMatchingSession( |
| 769 | deviceProxy->GetPeerScopedNodeId(), [numSessions](auto * session) { (*numSessions)++; }); |
| 770 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 771 | return ToPyChipError(CHIP_NO_ERROR); |
Jerry Johns | 6aa835e | 2022-08-28 00:11:35 -0400 | [diff] [blame] | 772 | } |
| 773 | |
C Freeman | 83ecf9c | 2023-06-02 18:32:53 -0400 | [diff] [blame] | 774 | PyChipError pychip_GetAttestationChallenge(chip::OperationalDeviceProxy * deviceProxy, uint8_t * buf, size_t * size) |
| 775 | { |
| 776 | VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), ToPyChipError(CHIP_ERROR_MISSING_SECURE_SESSION)); |
| 777 | VerifyOrReturnError(buf != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); |
| 778 | |
| 779 | ByteSpan challenge = deviceProxy->GetSecureSession().Value()->AsSecureSession()->GetCryptoContext().GetAttestationChallenge(); |
| 780 | VerifyOrReturnError(challenge.size() <= *size, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); |
| 781 | memcpy(buf, challenge.data(), challenge.size()); |
| 782 | *size = challenge.size(); |
| 783 | |
| 784 | return ToPyChipError(CHIP_NO_ERROR); |
| 785 | } |
| 786 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 787 | PyChipError pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, |
| 788 | CommissioneeDeviceProxy ** proxy) |
C Freeman | 2bd6bd6 | 2021-11-29 12:36:33 -0500 | [diff] [blame] | 789 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 790 | return ToPyChipError(devCtrl->GetDeviceBeingCommissioned(nodeId, proxy)); |
C Freeman | 2bd6bd6 | 2021-11-29 12:36:33 -0500 | [diff] [blame] | 791 | } |
| 792 | |
Tennessee Carmel-Veilleux | c3cd004 | 2022-07-08 13:49:30 -0400 | [diff] [blame] | 793 | // This is a method called VERY seldom, just for RemoveFabric/UpdateNOC |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 794 | PyChipError pychip_ExpireSessions(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId) |
Tennessee Carmel-Veilleux | c3cd004 | 2022-07-08 13:49:30 -0400 | [diff] [blame] | 795 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 796 | VerifyOrReturnError((devCtrl != nullptr) && (devCtrl->SessionMgr() != nullptr), ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); |
Jerry Johns | 0eed15d | 2022-08-01 17:56:38 -0700 | [diff] [blame] | 797 | |
| 798 | // |
Jerry Johns | 71da650 | 2022-12-11 19:32:16 -0800 | [diff] [blame] | 799 | // Stop any active pairing sessions to this node. |
| 800 | // |
| 801 | devCtrl->StopPairing(nodeId); |
| 802 | |
| 803 | // |
Jerry Johns | 0eed15d | 2022-08-01 17:56:38 -0700 | [diff] [blame] | 804 | // Since we permit multiple controllers on the same fabric each associated with a different fabric index, expiring a session |
| 805 | // needs to correctly expire sessions on other controllers on matching fabrics as well. |
| 806 | // |
| 807 | devCtrl->SessionMgr()->ExpireAllSessionsOnLogicalFabric(ScopedNodeId(nodeId, devCtrl->GetFabricIndex())); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 808 | return ToPyChipError(CHIP_NO_ERROR); |
Tennessee Carmel-Veilleux | c3cd004 | 2022-07-08 13:49:30 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 811 | PyChipError pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl) |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 812 | { |
| 813 | #if CONFIG_NETWORK_LAYER_BLE |
Michael Spang | 6387049 | 2022-06-28 08:41:08 -0400 | [diff] [blame] | 814 | devCtrl->CloseBleConnection(); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 815 | return ToPyChipError(CHIP_NO_ERROR); |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 816 | #else |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 817 | return ToPyChipError(CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE); |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 818 | #endif |
| 819 | } |
| 820 | |
Pankaj Garg | a04576d | 2021-11-08 15:04:13 -0800 | [diff] [blame] | 821 | uint64_t pychip_GetCommandSenderHandle(chip::DeviceProxy * device) |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 822 | { |
Song Guo | 8d7486f | 2021-05-17 23:11:39 +0800 | [diff] [blame] | 823 | return 0; |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 824 | } |
| 825 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 826 | void pychip_Stack_SetLogFunct(LogMessageFunct logFunct) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 827 | { |
Andrei Litvin | 0a91b1b | 2021-02-08 09:31:24 -0500 | [diff] [blame] | 828 | // TODO: determine if log redirection is supposed to be functioning in CHIP |
| 829 | // |
| 830 | // Background: original log baseline supported 'redirect logs to this |
| 831 | // function' however CHIP does not currently provide this. |
| 832 | // |
| 833 | // Ideally log redirection should work so that python code can do things |
| 834 | // like using the log module. |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 835 | } |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 836 | |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 837 | PyChipError pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext) |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 838 | { |
| 839 | if (callback == nullptr || pythonContext == nullptr) |
| 840 | { |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 841 | return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 842 | } |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 843 | PlatformMgr().ScheduleWork(callback, reinterpret_cast<intptr_t>(pythonContext)); |
Song GUO | b9d32ec | 2022-10-24 14:40:11 +0800 | [diff] [blame] | 844 | return ToPyChipError(CHIP_NO_ERROR); |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 845 | } |