yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Kevin Schoedel | a868187 | 2021-01-28 15:53:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2020-2021 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 | |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 43 | #include "ChipDeviceController-ScriptDeviceAddressUpdateDelegate.h" |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 44 | #include "ChipDeviceController-ScriptDevicePairingDelegate.h" |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 45 | #include "ChipDeviceController-StorageDelegate.h" |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 46 | |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 47 | #include "chip/interaction_model/Delegate.h" |
| 48 | |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 49 | #include <app/CommandSender.h> |
| 50 | #include <app/InteractionModelEngine.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 51 | #include <app/server/Dnssd.h> |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 52 | #include <controller/CHIPDevice.h> |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 53 | #include <controller/CHIPDeviceController.h> |
Sagar Dhawan | ae69dd7 | 2021-09-29 15:13:09 -0700 | [diff] [blame] | 54 | #include <controller/CHIPDeviceControllerFactory.h> |
Pankaj Garg | 0238a6c | 2021-05-10 07:33:19 -0700 | [diff] [blame] | 55 | #include <controller/ExampleOperationalCredentialsIssuer.h> |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 56 | #include <credentials/DeviceAttestationVerifier.h> |
| 57 | #include <credentials/examples/DeviceAttestationVerifierExample.h> |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 58 | #include <inet/IPAddress.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 59 | #include <lib/dnssd/Resolver.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 60 | #include <lib/support/BytesToHex.h> |
| 61 | #include <lib/support/CHIPMem.h> |
| 62 | #include <lib/support/CodeUtils.h> |
| 63 | #include <lib/support/DLLUtil.h> |
| 64 | #include <lib/support/ScopedBuffer.h> |
| 65 | #include <lib/support/logging/CHIPLogging.h> |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 66 | #include <platform/CHIPDeviceLayer.h> |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 67 | #include <setup_payload/QRCodeSetupPayloadParser.h> |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 68 | |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 69 | using namespace chip; |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 70 | using namespace chip::Ble; |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 71 | using namespace chip::Controller; |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 72 | using namespace chip::Credentials; |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 73 | using namespace chip::DeviceLayer; |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 74 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 75 | static_assert(std::is_same<uint32_t, ChipError::StorageType>::value, "python assumes CHIP_ERROR maps to c_uint32"); |
| 76 | |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 77 | extern "C" { |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 78 | typedef void (*ConstructBytesArrayFunct)(const uint8_t * dataBuf, uint32_t dataLen); |
| 79 | typedef void (*LogMessageFunct)(uint64_t time, uint64_t timeUS, const char * moduleName, uint8_t category, const char * msg); |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 80 | typedef void (*DeviceAvailableFunc)(Device * device, ChipError::StorageType err); |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 81 | typedef void (*ChipThreadTaskRunnerFunct)(intptr_t context); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 84 | namespace { |
| 85 | chip::Controller::PythonPersistentStorageDelegate sStorageDelegate; |
| 86 | chip::Controller::ScriptDevicePairingDelegate sPairingDelegate; |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 87 | chip::Controller::ScriptDeviceAddressUpdateDelegate sDeviceAddressUpdateDelegate; |
Pankaj Garg | 0238a6c | 2021-05-10 07:33:19 -0700 | [diff] [blame] | 88 | chip::Controller::ExampleOperationalCredentialsIssuer sOperationalCredentialsIssuer; |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 89 | } // namespace |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 90 | |
| 91 | // NOTE: Remote device ID is in sync with the echo server device id |
| 92 | // At some point, we may want to add an option to connect to a device without |
| 93 | // 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] | 94 | chip::NodeId kDefaultLocalDeviceId = chip::kTestControllerNodeId; |
| 95 | chip::NodeId kRemoteDeviceId = chip::kTestDeviceNodeId; |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 96 | |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 97 | extern "C" { |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 98 | ChipError::StorageType pychip_DeviceController_NewDeviceController(chip::Controller::DeviceCommissioner ** outDevCtrl, |
| 99 | chip::NodeId localDeviceId); |
| 100 | ChipError::StorageType pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl); |
| 101 | ChipError::StorageType pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, |
| 102 | chip::NodeId nodeId, char * outAddress, uint64_t maxAddressLen, |
| 103 | uint16_t * outPort); |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 104 | ChipError::StorageType pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl, |
| 105 | uint64_t * outFabricId); |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 106 | ChipError::StorageType pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 107 | |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 108 | // Rendezvous |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 109 | ChipError::StorageType pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, |
| 110 | uint32_t setupPINCode, chip::NodeId nodeid); |
| 111 | ChipError::StorageType pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, |
| 112 | uint32_t setupPINCode, chip::NodeId nodeid); |
| 113 | ChipError::StorageType pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid); |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 114 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 115 | ChipError::StorageType |
| 116 | pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, |
| 117 | uint16_t long_discriminator); |
| 118 | ChipError::StorageType pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 119 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 120 | ChipError::StorageType |
| 121 | pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, |
| 122 | uint16_t short_discriminator); |
| 123 | ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl, |
| 124 | uint16_t vendor); |
| 125 | ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl, |
| 126 | uint16_t device_type); |
| 127 | ChipError::StorageType |
chrisdecenzo | 97d9f7e | 2021-09-14 10:37:29 -0700 | [diff] [blame] | 128 | pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl); |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 129 | ChipError::StorageType pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext); |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 130 | |
Yufeng Wang | 275368c | 2021-08-31 17:42:23 -0700 | [diff] [blame] | 131 | ChipError::StorageType pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl, |
| 132 | chip::NodeId nodeid, uint16_t timeout, uint16_t iteration, |
| 133 | uint16_t discriminator, uint8_t option); |
| 134 | |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 135 | void pychip_DeviceController_PrintDiscoveredDevices(chip::Controller::DeviceCommissioner * devCtrl); |
| 136 | bool pychip_DeviceController_GetIPForDiscoveredDevice(chip::Controller::DeviceCommissioner * devCtrl, int idx, char * addrStr, |
| 137 | uint32_t len); |
| 138 | |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 139 | // Pairing Delegate |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 140 | ChipError::StorageType |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 141 | pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl, |
| 142 | chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback); |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 143 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 144 | ChipError::StorageType pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback( |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 145 | chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningCompleteFunct callback); |
| 146 | |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 147 | void pychip_ScriptDeviceAddressUpdateDelegate_SetOnAddressUpdateComplete( |
| 148 | chip::Controller::DeviceAddressUpdateDelegate_OnUpdateComplete callback); |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 149 | ChipError::StorageType pychip_Resolver_ResolveNode(uint64_t fabricid, chip::NodeId nodeid); |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 150 | |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 151 | // BLE |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 152 | ChipError::StorageType pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl); |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 153 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 154 | uint8_t pychip_DeviceController_GetLogFilter(); |
| 155 | void pychip_DeviceController_SetLogFilter(uint8_t category); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 156 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 157 | ChipError::StorageType pychip_Stack_Init(); |
| 158 | ChipError::StorageType pychip_Stack_Shutdown(); |
| 159 | const char * pychip_Stack_ErrorToString(ChipError::StorageType err); |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 160 | const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t statusCode); |
| 161 | void pychip_Stack_SetLogFunct(LogMessageFunct logFunct); |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 162 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 163 | ChipError::StorageType pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, |
| 164 | DeviceAvailableFunc callback); |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 165 | uint64_t pychip_GetCommandSenderHandle(chip::Controller::Device * device); |
Song Guo | bbcc376 | 2021-03-10 07:08:52 +0800 | [diff] [blame] | 166 | // CHIP Stack objects |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 167 | ChipError::StorageType pychip_BLEMgrImpl_ConfigureBle(uint32_t bluetoothAdapterId); |
Song GUO | 75f3895 | 2021-10-13 04:13:10 +0800 | [diff] [blame] | 168 | |
| 169 | chip::ChipError::StorageType pychip_InteractionModel_ShutdownSubscription(uint64_t subscriptionId); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 172 | ChipError::StorageType pychip_DeviceController_NewDeviceController(chip::Controller::DeviceCommissioner ** outDevCtrl, |
| 173 | chip::NodeId localDeviceId) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 174 | { |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 175 | *outDevCtrl = new chip::Controller::DeviceCommissioner(); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 176 | VerifyOrReturnError(*outDevCtrl != NULL, CHIP_ERROR_NO_MEMORY.AsInteger()); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 177 | |
Kevin Schoedel | 5f19e99 | 2021-03-02 14:36:20 -0500 | [diff] [blame] | 178 | if (localDeviceId == chip::kUndefinedNodeId) |
| 179 | { |
| 180 | localDeviceId = kDefaultLocalDeviceId; |
| 181 | } |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 182 | |
Marty Leisner | 0e52970 | 2021-09-27 10:55:08 -0400 | [diff] [blame] | 183 | // Initialize device attestation verifier |
| 184 | SetDeviceAttestationVerifier(Examples::GetExampleDACVerifier()); |
| 185 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 186 | CHIP_ERROR err = sOperationalCredentialsIssuer.Initialize(sStorageDelegate); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 187 | VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); |
Pankaj Garg | 0238a6c | 2021-05-10 07:33:19 -0700 | [diff] [blame] | 188 | |
Pankaj Garg | 7b00057 | 2021-08-13 20:14:29 -0700 | [diff] [blame] | 189 | chip::Crypto::P256Keypair ephemeralKey; |
| 190 | err = ephemeralKey.Initialize(); |
| 191 | VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); |
| 192 | |
| 193 | chip::Platform::ScopedMemoryBuffer<uint8_t> noc; |
| 194 | ReturnErrorCodeIf(!noc.Alloc(kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY.AsInteger()); |
| 195 | MutableByteSpan nocSpan(noc.Get(), kMaxCHIPDERCertLength); |
| 196 | |
| 197 | chip::Platform::ScopedMemoryBuffer<uint8_t> icac; |
| 198 | ReturnErrorCodeIf(!icac.Alloc(kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY.AsInteger()); |
| 199 | MutableByteSpan icacSpan(icac.Get(), kMaxCHIPDERCertLength); |
| 200 | |
| 201 | chip::Platform::ScopedMemoryBuffer<uint8_t> rcac; |
| 202 | ReturnErrorCodeIf(!rcac.Alloc(kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY.AsInteger()); |
| 203 | MutableByteSpan rcacSpan(rcac.Get(), kMaxCHIPDERCertLength); |
| 204 | |
| 205 | err = sOperationalCredentialsIssuer.GenerateNOCChainAfterValidation(localDeviceId, 0, ephemeralKey.Pubkey(), rcacSpan, icacSpan, |
| 206 | nocSpan); |
| 207 | VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); |
| 208 | |
Sagar Dhawan | ae69dd7 | 2021-09-29 15:13:09 -0700 | [diff] [blame] | 209 | FactoryInitParams factoryParams; |
| 210 | factoryParams.storageDelegate = &sStorageDelegate; |
| 211 | factoryParams.imDelegate = &PythonInteractionModelDelegate::Instance(); |
| 212 | |
| 213 | SetupParams initParams; |
| 214 | initParams.deviceAddressUpdateDelegate = &sDeviceAddressUpdateDelegate; |
Song Guo | 80120a2 | 2021-05-11 23:04:52 +0800 | [diff] [blame] | 215 | initParams.pairingDelegate = &sPairingDelegate; |
Pankaj Garg | 0238a6c | 2021-05-10 07:33:19 -0700 | [diff] [blame] | 216 | initParams.operationalCredentialsDelegate = &sOperationalCredentialsIssuer; |
Pankaj Garg | 7b00057 | 2021-08-13 20:14:29 -0700 | [diff] [blame] | 217 | initParams.ephemeralKeypair = &ephemeralKey; |
| 218 | initParams.controllerRCAC = rcacSpan; |
| 219 | initParams.controllerICAC = icacSpan; |
| 220 | initParams.controllerNOC = nocSpan; |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 221 | |
Sagar Dhawan | ae69dd7 | 2021-09-29 15:13:09 -0700 | [diff] [blame] | 222 | ReturnErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryParams).AsInteger()); |
| 223 | err = DeviceControllerFactory::GetInstance().SetupCommissioner(initParams, **outDevCtrl); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 224 | VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); |
Sharad Binjola | 535feb0 | 2021-09-15 17:37:57 -0700 | [diff] [blame] | 225 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 226 | chip::app::DnssdServer::Instance().StartServer(chip::Dnssd::CommissioningMode::kDisabled); |
Sharad Binjola | 535feb0 | 2021-09-15 17:37:57 -0700 | [diff] [blame] | 227 | #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 228 | |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 229 | return CHIP_NO_ERROR.AsInteger(); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 232 | ChipError::StorageType pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 233 | { |
| 234 | if (devCtrl != NULL) |
| 235 | { |
| 236 | devCtrl->Shutdown(); |
| 237 | delete devCtrl; |
| 238 | } |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 239 | |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 240 | return CHIP_NO_ERROR.AsInteger(); |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 243 | ChipError::StorageType pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, |
| 244 | chip::NodeId nodeId, char * outAddress, uint64_t maxAddressLen, |
| 245 | uint16_t * outPort) |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 246 | { |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 247 | Inet::IPAddress address; |
Pankaj Garg | fee3d26 | 2021-10-17 19:31:32 -0700 | [diff] [blame^] | 248 | ReturnErrorOnFailure( |
| 249 | devCtrl |
| 250 | ->GetPeerAddressAndPort(PeerId().SetCompressedFabricId(devCtrl->GetCompressedFabricId()).SetNodeId(nodeId), address, |
| 251 | *outPort) |
| 252 | .AsInteger()); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 253 | VerifyOrReturnError(address.ToString(outAddress, maxAddressLen), CHIP_ERROR_BUFFER_TOO_SMALL.AsInteger()); |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 254 | |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 255 | return CHIP_NO_ERROR.AsInteger(); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 258 | ChipError::StorageType pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl, |
| 259 | uint64_t * outFabricId) |
| 260 | { |
| 261 | *outFabricId = devCtrl->GetCompressedFabricId(); |
| 262 | return CHIP_NO_ERROR.AsInteger(); |
| 263 | } |
| 264 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 265 | ChipError::StorageType pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId) |
Yufeng Wang | b728b74 | 2021-06-16 08:04:17 -0700 | [diff] [blame] | 266 | { |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 267 | *outFabricId = devCtrl->GetFabricId(); |
| 268 | return CHIP_NO_ERROR.AsInteger(); |
Yufeng Wang | b728b74 | 2021-06-16 08:04:17 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 271 | const char * pychip_DeviceController_ErrorToString(ChipError::StorageType err) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 272 | { |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 273 | return chip::ErrorStr(CHIP_ERROR(err)); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 276 | const char * pychip_DeviceController_StatusReportToString(uint32_t profileId, uint16_t statusCode) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 277 | { |
| 278 | // return chip::StatusReportStr(profileId, statusCode); |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 282 | uint8_t pychip_DeviceController_GetLogFilter() |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 283 | { |
| 284 | #if _CHIP_USE_LOGGING |
| 285 | return chip::Logging::GetLogFilter(); |
| 286 | #else |
| 287 | return chip::Logging::kLogCategory_None; |
| 288 | #endif |
| 289 | } |
| 290 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 291 | void pychip_DeviceController_SetLogFilter(uint8_t category) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 292 | { |
| 293 | #if _CHIP_USE_LOGGING |
| 294 | chip::Logging::SetLogFilter(category); |
| 295 | #endif |
| 296 | } |
| 297 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 298 | ChipError::StorageType pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, |
| 299 | uint32_t setupPINCode, chip::NodeId nodeid) |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 300 | { |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 301 | return devCtrl |
| 302 | ->PairDevice(nodeid, |
| 303 | chip::RendezvousParameters() |
| 304 | .SetPeerAddress(Transport::PeerAddress(Transport::Type::kBle)) |
| 305 | .SetSetupPINCode(setupPINCode) |
| 306 | .SetDiscriminator(discriminator)) |
| 307 | .AsInteger(); |
Song Guo | b94c980 | 2021-01-29 13:58:48 +0800 | [diff] [blame] | 308 | } |
| 309 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 310 | ChipError::StorageType pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, |
| 311 | uint32_t setupPINCode, chip::NodeId nodeid) |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 312 | { |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 313 | chip::Inet::IPAddress peerAddr; |
| 314 | chip::Transport::PeerAddress addr; |
| 315 | chip::RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode); |
| 316 | |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 317 | VerifyOrReturnError(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); |
Song Guo | 830d1f8 | 2020-12-10 01:59:07 +0800 | [diff] [blame] | 318 | // TODO: IP rendezvous should use TCP connection. |
| 319 | addr.SetTransportType(chip::Transport::Type::kUdp).SetIPAddress(peerAddr); |
| 320 | params.SetPeerAddress(addr).SetDiscriminator(0); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 321 | return devCtrl->PairDevice(nodeid, params).AsInteger(); |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 322 | } |
| 323 | |
Pankaj Garg | fee3d26 | 2021-10-17 19:31:32 -0700 | [diff] [blame^] | 324 | void CloseSessionCallback(Device * device, ChipError::StorageType err) |
| 325 | { |
| 326 | if (device != nullptr) |
| 327 | { |
| 328 | device->CloseSession(); |
| 329 | } |
| 330 | if (!ChipError::IsSuccess(err)) |
| 331 | { |
| 332 | ChipLogError(Controller, "Close session callback was called with an error: %d", err); |
| 333 | } |
| 334 | } |
| 335 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 336 | ChipError::StorageType pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid) |
Damian Królik | 88cd766 | 2021-07-17 01:00:54 +0200 | [diff] [blame] | 337 | { |
Pankaj Garg | fee3d26 | 2021-10-17 19:31:32 -0700 | [diff] [blame^] | 338 | return pychip_GetConnectedDeviceByNodeId(devCtrl, nodeid, CloseSessionCallback); |
Damian Królik | 88cd766 | 2021-07-17 01:00:54 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 341 | ChipError::StorageType pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl) |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 342 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 343 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, static_cast<uint64_t>(0)); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 344 | return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 345 | } |
| 346 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 347 | ChipError::StorageType |
| 348 | pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, |
| 349 | uint16_t long_discriminator) |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 350 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 351 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kLong, long_discriminator); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 352 | return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 353 | } |
| 354 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 355 | ChipError::StorageType |
| 356 | pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, |
| 357 | uint16_t short_discriminator) |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 358 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 359 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kShort, short_discriminator); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 360 | return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 361 | } |
| 362 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 363 | ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl, |
| 364 | uint16_t vendor) |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 365 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 366 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kVendor, vendor); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 367 | return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 368 | } |
| 369 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 370 | ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl, |
| 371 | uint16_t device_type) |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 372 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 373 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kDeviceType, device_type); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 374 | return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 375 | } |
| 376 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 377 | ChipError::StorageType |
chrisdecenzo | 97d9f7e | 2021-09-14 10:37:29 -0700 | [diff] [blame] | 378 | pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl) |
C Freeman | 8a53528 | 2021-06-22 22:54:19 -0400 | [diff] [blame] | 379 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 380 | Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kCommissioningMode); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 381 | return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Yufeng Wang | 275368c | 2021-08-31 17:42:23 -0700 | [diff] [blame] | 384 | ChipError::StorageType pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl, |
| 385 | chip::NodeId nodeid, uint16_t timeout, uint16_t iteration, |
| 386 | uint16_t discriminator, uint8_t option) |
| 387 | { |
| 388 | return devCtrl->OpenCommissioningWindow(nodeid, timeout, iteration, discriminator, option).AsInteger(); |
| 389 | } |
| 390 | |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 391 | void pychip_DeviceController_PrintDiscoveredDevices(chip::Controller::DeviceCommissioner * devCtrl) |
| 392 | { |
| 393 | for (int i = 0; i < devCtrl->GetMaxCommissionableNodesSupported(); ++i) |
| 394 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 395 | const chip::Dnssd::DiscoveredNodeData * dnsSdInfo = devCtrl->GetDiscoveredDevice(i); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 396 | if (dnsSdInfo == nullptr) |
| 397 | { |
| 398 | continue; |
| 399 | } |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 400 | char rotatingId[chip::Dnssd::kMaxRotatingIdLen * 2 + 1] = ""; |
C Freeman | b5eade0 | 2021-06-02 16:36:56 -0400 | [diff] [blame] | 401 | Encoding::BytesToUppercaseHexString(dnsSdInfo->rotatingId, dnsSdInfo->rotatingIdLen, rotatingId, sizeof(rotatingId)); |
| 402 | |
Sharad Binjola | b2f8af0 | 2021-06-29 09:13:13 -0700 | [diff] [blame] | 403 | ChipLogProgress(Discovery, "Commissionable Node %d", i); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 404 | ChipLogProgress(Discovery, "\tHost name:\t\t%s", dnsSdInfo->hostName); |
chrisdecenzo | 97d9f7e | 2021-09-14 10:37:29 -0700 | [diff] [blame] | 405 | ChipLogProgress(Discovery, "\tPort:\t\t\t%u", dnsSdInfo->port); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 406 | ChipLogProgress(Discovery, "\tLong discriminator:\t%u", dnsSdInfo->longDiscriminator); |
| 407 | ChipLogProgress(Discovery, "\tVendor ID:\t\t%u", dnsSdInfo->vendorId); |
| 408 | ChipLogProgress(Discovery, "\tProduct ID:\t\t%u", dnsSdInfo->productId); |
C Freeman | b5eade0 | 2021-06-02 16:36:56 -0400 | [diff] [blame] | 409 | ChipLogProgress(Discovery, "\tCommissioning Mode\t%u", dnsSdInfo->commissioningMode); |
| 410 | ChipLogProgress(Discovery, "\tDevice Type\t\t%u", dnsSdInfo->deviceType); |
| 411 | ChipLogProgress(Discovery, "\tDevice Name\t\t%s", dnsSdInfo->deviceName); |
| 412 | ChipLogProgress(Discovery, "\tRotating Id\t\t%s", rotatingId); |
| 413 | ChipLogProgress(Discovery, "\tPairing Instruction\t%s", dnsSdInfo->pairingInstruction); |
| 414 | ChipLogProgress(Discovery, "\tPairing Hint\t\t0x%x", dnsSdInfo->pairingHint); |
C Freeman | cb511ba | 2021-09-17 10:44:32 -0400 | [diff] [blame] | 415 | if (dnsSdInfo->GetMrpRetryIntervalIdle().HasValue()) |
| 416 | { |
| 417 | ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", dnsSdInfo->GetMrpRetryIntervalIdle().Value()); |
| 418 | } |
| 419 | else |
| 420 | { |
| 421 | ChipLogProgress(Discovery, "\tMrp Interval idle\tNot present"); |
| 422 | } |
| 423 | if (dnsSdInfo->GetMrpRetryIntervalActive().HasValue()) |
| 424 | { |
| 425 | ChipLogProgress(Discovery, "\tMrp Interval active\t%u", dnsSdInfo->GetMrpRetryIntervalActive().Value()); |
| 426 | } |
| 427 | else |
| 428 | { |
| 429 | ChipLogProgress(Discovery, "\tMrp Interval active\tNot present"); |
| 430 | } |
| 431 | ChipLogProgress(Discovery, "\tSupports TCP\t\t%d", dnsSdInfo->supportsTcp); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 432 | for (int j = 0; j < dnsSdInfo->numIPs; ++j) |
| 433 | { |
| 434 | char buf[chip::Inet::kMaxIPAddressStringLength]; |
| 435 | dnsSdInfo->ipAddress[j].ToString(buf); |
| 436 | ChipLogProgress(Discovery, "\tAddress %d:\t\t%s", j, buf); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | bool pychip_DeviceController_GetIPForDiscoveredDevice(chip::Controller::DeviceCommissioner * devCtrl, int idx, char * addrStr, |
| 442 | uint32_t len) |
| 443 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 444 | const chip::Dnssd::DiscoveredNodeData * dnsSdInfo = devCtrl->GetDiscoveredDevice(idx); |
cecille | 8185019 | 2021-05-11 21:43:14 -0400 | [diff] [blame] | 445 | if (dnsSdInfo == nullptr) |
| 446 | { |
| 447 | return false; |
| 448 | } |
| 449 | // TODO(cecille): Select which one we actually want. |
| 450 | if (dnsSdInfo->ipAddress[0].ToString(addrStr, len) == addrStr) |
| 451 | { |
| 452 | return true; |
| 453 | } |
| 454 | return false; |
| 455 | } |
| 456 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 457 | ChipError::StorageType |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 458 | pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl, |
| 459 | chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback) |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 460 | { |
Song Guo | e2590b8 | 2021-02-11 03:30:13 +0800 | [diff] [blame] | 461 | sPairingDelegate.SetKeyExchangeCallback(callback); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 462 | return CHIP_NO_ERROR.AsInteger(); |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 463 | } |
| 464 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 465 | ChipError::StorageType pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback( |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 466 | chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningCompleteFunct callback) |
| 467 | { |
| 468 | sPairingDelegate.SetCommissioningCompleteCallback(callback); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 469 | return CHIP_NO_ERROR.AsInteger(); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 470 | } |
| 471 | |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 472 | void pychip_ScriptDeviceAddressUpdateDelegate_SetOnAddressUpdateComplete( |
| 473 | chip::Controller::DeviceAddressUpdateDelegate_OnUpdateComplete callback) |
| 474 | { |
| 475 | sDeviceAddressUpdateDelegate.SetOnAddressUpdateComplete(callback); |
| 476 | } |
| 477 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 478 | ChipError::StorageType pychip_Resolver_ResolveNode(uint64_t fabricid, chip::NodeId nodeid) |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 479 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 480 | return Dnssd::Resolver::Instance() |
Pankaj Garg | 6efec20 | 2021-08-25 10:47:29 -0700 | [diff] [blame] | 481 | .ResolveNodeId(PeerId().SetNodeId(nodeid).SetCompressedFabricId(fabricid), Inet::kIPAddressType_Any) |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 482 | .AsInteger(); |
Damian Królik | a578416 | 2021-03-05 16:56:42 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 485 | ChipError::StorageType pychip_Stack_Init() |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 486 | { |
| 487 | CHIP_ERROR err = CHIP_NO_ERROR; |
| 488 | |
Song Guo | fe1eeb4 | 2020-10-30 04:17:04 +0800 | [diff] [blame] | 489 | err = chip::Platform::MemoryInit(); |
| 490 | SuccessOrExit(err); |
| 491 | |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 492 | #if !CHIP_SYSTEM_CONFIG_USE_SOCKETS |
| 493 | |
| 494 | ExitNow(err = CHIP_ERROR_NOT_IMPLEMENTED); |
| 495 | |
| 496 | #else /* CHIP_SYSTEM_CONFIG_USE_SOCKETS */ |
| 497 | |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 498 | #endif /* CHIP_SYSTEM_CONFIG_USE_SOCKETS */ |
| 499 | |
| 500 | exit: |
| 501 | if (err != CHIP_NO_ERROR) |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 502 | pychip_Stack_Shutdown(); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 503 | |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 504 | return err.AsInteger(); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 507 | ChipError::StorageType pychip_Stack_Shutdown() |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 508 | { |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 509 | return CHIP_NO_ERROR.AsInteger(); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 512 | const char * pychip_Stack_ErrorToString(ChipError::StorageType err) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 513 | { |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 514 | return chip::ErrorStr(CHIP_ERROR(err)); |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 517 | const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t statusCode) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 518 | { |
| 519 | // return chip::StatusReportStr(profileId, statusCode); |
| 520 | return NULL; |
| 521 | } |
| 522 | |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 523 | namespace { |
| 524 | struct GetDeviceCallbacks |
| 525 | { |
| 526 | GetDeviceCallbacks(DeviceAvailableFunc callback) : |
| 527 | mOnSuccess(OnDeviceConnectedFn, this), mOnFailure(OnConnectionFailureFn, this), mCallback(callback) |
| 528 | {} |
| 529 | |
| 530 | static void OnDeviceConnectedFn(void * context, Device * device) |
| 531 | { |
| 532 | auto * self = static_cast<GetDeviceCallbacks *>(context); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 533 | self->mCallback(device, CHIP_NO_ERROR.AsInteger()); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 534 | delete self; |
| 535 | } |
| 536 | |
| 537 | static void OnConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error) |
| 538 | { |
| 539 | auto * self = static_cast<GetDeviceCallbacks *>(context); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 540 | self->mCallback(nullptr, error.AsInteger()); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 541 | delete self; |
| 542 | } |
| 543 | |
| 544 | Callback::Callback<OnDeviceConnected> mOnSuccess; |
| 545 | Callback::Callback<OnDeviceConnectionFailure> mOnFailure; |
| 546 | DeviceAvailableFunc mCallback; |
| 547 | }; |
| 548 | } // anonymous namespace |
| 549 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 550 | ChipError::StorageType pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, |
| 551 | DeviceAvailableFunc callback) |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 552 | { |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 553 | VerifyOrReturnError(devCtrl != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 554 | auto * callbacks = new GetDeviceCallbacks(callback); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 555 | return devCtrl->GetConnectedDevice(nodeId, &callbacks->mOnSuccess, &callbacks->mOnFailure).AsInteger(); |
Boris Zbarsky | 39d1e92 | 2021-07-02 15:23:53 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 558 | ChipError::StorageType pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl) |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 559 | { |
| 560 | #if CONFIG_NETWORK_LAYER_BLE |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 561 | return devCtrl->CloseBleConnection().AsInteger(); |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 562 | #else |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 563 | return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE.AsInteger(); |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 564 | #endif |
| 565 | } |
| 566 | |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 567 | uint64_t pychip_GetCommandSenderHandle(chip::Controller::Device * device) |
| 568 | { |
Song Guo | 8d7486f | 2021-05-17 23:11:39 +0800 | [diff] [blame] | 569 | return 0; |
Song Guo | 9ab3c90 | 2021-04-09 06:08:35 +0800 | [diff] [blame] | 570 | } |
| 571 | |
Andrei Litvin | b8287fe | 2021-02-04 15:23:05 -0500 | [diff] [blame] | 572 | void pychip_Stack_SetLogFunct(LogMessageFunct logFunct) |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 573 | { |
Andrei Litvin | 0a91b1b | 2021-02-08 09:31:24 -0500 | [diff] [blame] | 574 | // TODO: determine if log redirection is supposed to be functioning in CHIP |
| 575 | // |
| 576 | // Background: original log baseline supported 'redirect logs to this |
| 577 | // function' however CHIP does not currently provide this. |
| 578 | // |
| 579 | // Ideally log redirection should work so that python code can do things |
| 580 | // like using the log module. |
yunhanw-google | 90ea314 | 2020-10-01 13:53:26 -0700 | [diff] [blame] | 581 | } |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 582 | |
Kevin Schoedel | 57874d4 | 2021-07-20 21:10:00 -0400 | [diff] [blame] | 583 | ChipError::StorageType pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext) |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 584 | { |
| 585 | if (callback == nullptr || pythonContext == nullptr) |
| 586 | { |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 587 | return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 588 | } |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 589 | PlatformMgr().ScheduleWork(callback, reinterpret_cast<intptr_t>(pythonContext)); |
Kevin Schoedel | 0fe17ec | 2021-08-05 08:52:07 -0400 | [diff] [blame] | 590 | return CHIP_NO_ERROR.AsInteger(); |
Song GUO | ed3b233 | 2021-07-21 02:12:40 +0800 | [diff] [blame] | 591 | } |
Song GUO | 75f3895 | 2021-10-13 04:13:10 +0800 | [diff] [blame] | 592 | |
| 593 | chip::ChipError::StorageType pychip_InteractionModel_ShutdownSubscription(uint64_t subscriptionId) |
| 594 | { |
| 595 | return chip::app::InteractionModelEngine::GetInstance()->ShutdownSubscription(subscriptionId).AsInteger(); |
| 596 | } |