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