blob: c16ea817b4c0922e860eea100cacc52cb3b410fe [file] [log] [blame]
yunhanw-google90ea3142020-10-01 13:53:26 -07001/*
2 *
Kevin Schoedela8681872021-01-28 15:53:13 -05003 * Copyright (c) 2020-2021 Project CHIP Authors
yunhanw-google90ea3142020-10-01 13:53:26 -07004 * 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ólika5784162021-03-05 16:56:42 +010030#include <memory>
yunhanw-google90ea3142020-10-01 13:53:26 -070031#include <stdio.h>
32#include <stdlib.h>
33#include <sys/time.h>
Kevin Schoedel57874d42021-07-20 21:10:00 -040034#include <type_traits>
yunhanw-google90ea3142020-10-01 13:53:26 -070035#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ólika5784162021-03-05 16:56:42 +010043#include "ChipDeviceController-ScriptDeviceAddressUpdateDelegate.h"
Song Guofe1eeb42020-10-30 04:17:04 +080044#include "ChipDeviceController-ScriptDevicePairingDelegate.h"
Song Guo830d1f82020-12-10 01:59:07 +080045#include "ChipDeviceController-StorageDelegate.h"
Song Guofe1eeb42020-10-30 04:17:04 +080046
Song Guo9ab3c902021-04-09 06:08:35 +080047#include "chip/interaction_model/Delegate.h"
48
Song Guoa77a6622021-01-29 13:13:02 +080049#include <app/CommandSender.h>
Pankaj Garga04576d2021-11-08 15:04:13 -080050#include <app/DeviceProxy.h>
Song Guoa77a6622021-01-29 13:13:02 +080051#include <app/InteractionModelEngine.h>
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +020052#include <app/server/Dnssd.h>
Song Guoe2590b82021-02-11 03:30:13 +080053#include <controller/CHIPDeviceController.h>
Sagar Dhawanae69dd72021-09-29 15:13:09 -070054#include <controller/CHIPDeviceControllerFactory.h>
Pankaj Garg0238a6c2021-05-10 07:33:19 -070055#include <controller/ExampleOperationalCredentialsIssuer.h>
Marty Leisner0e529702021-09-27 10:55:08 -040056#include <credentials/DeviceAttestationVerifier.h>
Tennessee Carmel-Veilleux7ed532b2021-11-24 10:37:42 -050057#include <credentials/examples/DefaultDeviceAttestationVerifier.h>
cecille81850192021-05-11 21:43:14 -040058#include <inet/IPAddress.h>
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +020059#include <lib/dnssd/Resolver.h>
Zang MingJie53dd5832021-09-03 03:05:16 +080060#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 GUOed3b2332021-07-21 02:12:40 +080066#include <platform/CHIPDeviceLayer.h>
cecille81850192021-05-11 21:43:14 -040067#include <setup_payload/QRCodeSetupPayloadParser.h>
yunhanw-google90ea3142020-10-01 13:53:26 -070068
Song Guo830d1f82020-12-10 01:59:07 +080069using namespace chip;
yunhanw-google90ea3142020-10-01 13:53:26 -070070using namespace chip::Ble;
Song Guoe2590b82021-02-11 03:30:13 +080071using namespace chip::Controller;
Marty Leisner0e529702021-09-27 10:55:08 -040072using namespace chip::Credentials;
Song GUOed3b2332021-07-21 02:12:40 +080073using namespace chip::DeviceLayer;
yunhanw-google90ea3142020-10-01 13:53:26 -070074
Kevin Schoedel57874d42021-07-20 21:10:00 -040075static_assert(std::is_same<uint32_t, ChipError::StorageType>::value, "python assumes CHIP_ERROR maps to c_uint32");
76
yunhanw-google90ea3142020-10-01 13:53:26 -070077extern "C" {
yunhanw-google90ea3142020-10-01 13:53:26 -070078typedef void (*ConstructBytesArrayFunct)(const uint8_t * dataBuf, uint32_t dataLen);
79typedef void (*LogMessageFunct)(uint64_t time, uint64_t timeUS, const char * moduleName, uint8_t category, const char * msg);
Pankaj Garga04576d2021-11-08 15:04:13 -080080typedef void (*DeviceAvailableFunc)(DeviceProxy * device, ChipError::StorageType err);
Song GUOed3b2332021-07-21 02:12:40 +080081typedef void (*ChipThreadTaskRunnerFunct)(intptr_t context);
yunhanw-google90ea3142020-10-01 13:53:26 -070082}
83
Song Guoe2590b82021-02-11 03:30:13 +080084namespace {
85chip::Controller::PythonPersistentStorageDelegate sStorageDelegate;
86chip::Controller::ScriptDevicePairingDelegate sPairingDelegate;
Damian Królika5784162021-03-05 16:56:42 +010087chip::Controller::ScriptDeviceAddressUpdateDelegate sDeviceAddressUpdateDelegate;
Pankaj Garg0238a6c2021-05-10 07:33:19 -070088chip::Controller::ExampleOperationalCredentialsIssuer sOperationalCredentialsIssuer;
Sagar Dhawanf0bbca72021-10-27 17:23:34 -050089chip::SimpleFabricStorage sFabricStorage;
Song Guoe2590b82021-02-11 03:30:13 +080090} // namespace
yunhanw-google90ea3142020-10-01 13:53:26 -070091
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 Schoedel5f19e992021-03-02 14:36:20 -050095chip::NodeId kDefaultLocalDeviceId = chip::kTestControllerNodeId;
96chip::NodeId kRemoteDeviceId = chip::kTestDeviceNodeId;
yunhanw-google90ea3142020-10-01 13:53:26 -070097
yunhanw-google90ea3142020-10-01 13:53:26 -070098extern "C" {
Kevin Schoedel57874d42021-07-20 21:10:00 -040099ChipError::StorageType pychip_DeviceController_NewDeviceController(chip::Controller::DeviceCommissioner ** outDevCtrl,
100 chip::NodeId localDeviceId);
101ChipError::StorageType pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl);
102ChipError::StorageType pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl,
103 chip::NodeId nodeId, char * outAddress, uint64_t maxAddressLen,
104 uint16_t * outPort);
Pankaj Garg6efec202021-08-25 10:47:29 -0700105ChipError::StorageType pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl,
106 uint64_t * outFabricId);
Kevin Schoedel57874d42021-07-20 21:10:00 -0400107ChipError::StorageType pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId);
yunhanw-google90ea3142020-10-01 13:53:26 -0700108
Song Guofe1eeb42020-10-30 04:17:04 +0800109// Rendezvous
Kevin Schoedel57874d42021-07-20 21:10:00 -0400110ChipError::StorageType pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator,
111 uint32_t setupPINCode, chip::NodeId nodeid);
112ChipError::StorageType pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr,
113 uint32_t setupPINCode, chip::NodeId nodeid);
114ChipError::StorageType pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid);
C Freemanaa719692021-12-03 11:45:30 -0500115ChipError::StorageType pychip_DeviceController_EstablishPASESessionIP(chip::Controller::DeviceCommissioner * devCtrl,
116 const char * peerAddrStr, uint32_t setupPINCode,
117 chip::NodeId nodeid);
118ChipError::StorageType pychip_DeviceController_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid);
Song Guofe1eeb42020-10-30 04:17:04 +0800119
Kevin Schoedel57874d42021-07-20 21:10:00 -0400120ChipError::StorageType
121pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl,
122 uint16_t long_discriminator);
123ChipError::StorageType pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl);
C Freeman8a535282021-06-22 22:54:19 -0400124
Kevin Schoedel57874d42021-07-20 21:10:00 -0400125ChipError::StorageType
126pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl,
127 uint16_t short_discriminator);
128ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl,
129 uint16_t vendor);
130ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl,
131 uint16_t device_type);
132ChipError::StorageType
chrisdecenzo97d9f7e2021-09-14 10:37:29 -0700133pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl);
Kevin Schoedel57874d42021-07-20 21:10:00 -0400134ChipError::StorageType pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext);
Song GUOed3b2332021-07-21 02:12:40 +0800135
Yufeng Wang275368c2021-08-31 17:42:23 -0700136ChipError::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
cecille81850192021-05-11 21:43:14 -0400140void pychip_DeviceController_PrintDiscoveredDevices(chip::Controller::DeviceCommissioner * devCtrl);
141bool pychip_DeviceController_GetIPForDiscoveredDevice(chip::Controller::DeviceCommissioner * devCtrl, int idx, char * addrStr,
142 uint32_t len);
Damian Królikb51852b2021-10-20 23:46:26 +0200143ChipError::StorageType pychip_DeviceController_UpdateDevice(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid);
cecille81850192021-05-11 21:43:14 -0400144
Song Guoe2590b82021-02-11 03:30:13 +0800145// Pairing Delegate
Kevin Schoedel57874d42021-07-20 21:10:00 -0400146ChipError::StorageType
Song Guoe2590b82021-02-11 03:30:13 +0800147pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl,
148 chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback);
Song Guofe1eeb42020-10-30 04:17:04 +0800149
Kevin Schoedel57874d42021-07-20 21:10:00 -0400150ChipError::StorageType pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback(
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400151 chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningCompleteFunct callback);
152
Damian Królika5784162021-03-05 16:56:42 +0100153void pychip_ScriptDeviceAddressUpdateDelegate_SetOnAddressUpdateComplete(
154 chip::Controller::DeviceAddressUpdateDelegate_OnUpdateComplete callback);
Damian Królika5784162021-03-05 16:56:42 +0100155
Song Guo87142e02021-04-21 09:00:00 +0800156// BLE
Kevin Schoedel57874d42021-07-20 21:10:00 -0400157ChipError::StorageType pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl);
Song Guo87142e02021-04-21 09:00:00 +0800158
Andrei Litvinb8287fe2021-02-04 15:23:05 -0500159uint8_t pychip_DeviceController_GetLogFilter();
160void pychip_DeviceController_SetLogFilter(uint8_t category);
yunhanw-google90ea3142020-10-01 13:53:26 -0700161
Kevin Schoedel57874d42021-07-20 21:10:00 -0400162ChipError::StorageType pychip_Stack_Init();
163ChipError::StorageType pychip_Stack_Shutdown();
164const char * pychip_Stack_ErrorToString(ChipError::StorageType err);
Andrei Litvinb8287fe2021-02-04 15:23:05 -0500165const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t statusCode);
166void pychip_Stack_SetLogFunct(LogMessageFunct logFunct);
Song Guoa77a6622021-01-29 13:13:02 +0800167
Kevin Schoedel57874d42021-07-20 21:10:00 -0400168ChipError::StorageType pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
169 DeviceAvailableFunc callback);
C Freeman2bd6bd62021-11-29 12:36:33 -0500170ChipError::StorageType pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
171 CommissioneeDeviceProxy ** proxy);
Pankaj Garga04576d2021-11-08 15:04:13 -0800172uint64_t pychip_GetCommandSenderHandle(chip::DeviceProxy * device);
Song Guobbcc3762021-03-10 07:08:52 +0800173// CHIP Stack objects
Kevin Schoedel57874d42021-07-20 21:10:00 -0400174ChipError::StorageType pychip_BLEMgrImpl_ConfigureBle(uint32_t bluetoothAdapterId);
Song GUO75f38952021-10-13 04:13:10 +0800175
176chip::ChipError::StorageType pychip_InteractionModel_ShutdownSubscription(uint64_t subscriptionId);
yunhanw-google90ea3142020-10-01 13:53:26 -0700177}
178
Kevin Schoedel57874d42021-07-20 21:10:00 -0400179ChipError::StorageType pychip_DeviceController_NewDeviceController(chip::Controller::DeviceCommissioner ** outDevCtrl,
180 chip::NodeId localDeviceId)
yunhanw-google90ea3142020-10-01 13:53:26 -0700181{
Song Guoe2590b82021-02-11 03:30:13 +0800182 *outDevCtrl = new chip::Controller::DeviceCommissioner();
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400183 VerifyOrReturnError(*outDevCtrl != NULL, CHIP_ERROR_NO_MEMORY.AsInteger());
yunhanw-google90ea3142020-10-01 13:53:26 -0700184
Kevin Schoedel5f19e992021-03-02 14:36:20 -0500185 if (localDeviceId == chip::kUndefinedNodeId)
186 {
187 localDeviceId = kDefaultLocalDeviceId;
188 }
Song Guo9ab3c902021-04-09 06:08:35 +0800189
Marty Leisner0e529702021-09-27 10:55:08 -0400190 // Initialize device attestation verifier
Tennessee Carmel-Veilleuxab5734c2021-11-27 12:08:31 -0500191 // 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 Leisner0e529702021-09-27 10:55:08 -0400194
Kevin Schoedel57874d42021-07-20 21:10:00 -0400195 CHIP_ERROR err = sOperationalCredentialsIssuer.Initialize(sStorageDelegate);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400196 VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger());
Pankaj Garg0238a6c2021-05-10 07:33:19 -0700197
Sagar Dhawanf0bbca72021-10-27 17:23:34 -0500198 err = sFabricStorage.Initialize(&sStorageDelegate);
199 VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger());
200
Pankaj Garg7b000572021-08-13 20:14:29 -0700201 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 Dhawanae69dd72021-09-29 15:13:09 -0700221 FactoryInitParams factoryParams;
Sagar Dhawanf0bbca72021-10-27 17:23:34 -0500222 factoryParams.fabricStorage = &sFabricStorage;
223 factoryParams.imDelegate = &PythonInteractionModelDelegate::Instance();
Sagar Dhawanae69dd72021-09-29 15:13:09 -0700224
225 SetupParams initParams;
Sagar Dhawanf0bbca72021-10-27 17:23:34 -0500226 initParams.storageDelegate = &sStorageDelegate;
Sagar Dhawanae69dd72021-09-29 15:13:09 -0700227 initParams.deviceAddressUpdateDelegate = &sDeviceAddressUpdateDelegate;
Song Guo80120a22021-05-11 23:04:52 +0800228 initParams.pairingDelegate = &sPairingDelegate;
Pankaj Garg0238a6c2021-05-10 07:33:19 -0700229 initParams.operationalCredentialsDelegate = &sOperationalCredentialsIssuer;
Pankaj Garg7b000572021-08-13 20:14:29 -0700230 initParams.ephemeralKeypair = &ephemeralKey;
231 initParams.controllerRCAC = rcacSpan;
232 initParams.controllerICAC = icacSpan;
233 initParams.controllerNOC = nocSpan;
Song Guo9ab3c902021-04-09 06:08:35 +0800234
Sagar Dhawanae69dd72021-09-29 15:13:09 -0700235 ReturnErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryParams).AsInteger());
236 err = DeviceControllerFactory::GetInstance().SetupCommissioner(initParams, **outDevCtrl);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400237 VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger());
Sharad Binjola535feb02021-09-15 17:37:57 -0700238#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200239 chip::app::DnssdServer::Instance().StartServer(chip::Dnssd::CommissioningMode::kDisabled);
Sharad Binjola535feb02021-09-15 17:37:57 -0700240#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
yunhanw-google90ea3142020-10-01 13:53:26 -0700241
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400242 return CHIP_NO_ERROR.AsInteger();
yunhanw-google90ea3142020-10-01 13:53:26 -0700243}
244
Kevin Schoedel57874d42021-07-20 21:10:00 -0400245ChipError::StorageType pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl)
yunhanw-google90ea3142020-10-01 13:53:26 -0700246{
247 if (devCtrl != NULL)
248 {
249 devCtrl->Shutdown();
250 delete devCtrl;
251 }
Damian Królika5784162021-03-05 16:56:42 +0100252
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400253 return CHIP_NO_ERROR.AsInteger();
Damian Królika5784162021-03-05 16:56:42 +0100254}
255
Kevin Schoedel57874d42021-07-20 21:10:00 -0400256ChipError::StorageType pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl,
257 chip::NodeId nodeId, char * outAddress, uint64_t maxAddressLen,
258 uint16_t * outPort)
Damian Królika5784162021-03-05 16:56:42 +0100259{
Damian Królika5784162021-03-05 16:56:42 +0100260 Inet::IPAddress address;
Pankaj Gargfee3d262021-10-17 19:31:32 -0700261 ReturnErrorOnFailure(
262 devCtrl
263 ->GetPeerAddressAndPort(PeerId().SetCompressedFabricId(devCtrl->GetCompressedFabricId()).SetNodeId(nodeId), address,
264 *outPort)
265 .AsInteger());
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400266 VerifyOrReturnError(address.ToString(outAddress, maxAddressLen), CHIP_ERROR_BUFFER_TOO_SMALL.AsInteger());
Damian Królika5784162021-03-05 16:56:42 +0100267
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400268 return CHIP_NO_ERROR.AsInteger();
yunhanw-google90ea3142020-10-01 13:53:26 -0700269}
270
Pankaj Garg6efec202021-08-25 10:47:29 -0700271ChipError::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 Schoedel57874d42021-07-20 21:10:00 -0400278ChipError::StorageType pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId)
Yufeng Wangb728b742021-06-16 08:04:17 -0700279{
Pankaj Garg6efec202021-08-25 10:47:29 -0700280 *outFabricId = devCtrl->GetFabricId();
281 return CHIP_NO_ERROR.AsInteger();
Yufeng Wangb728b742021-06-16 08:04:17 -0700282}
283
Kevin Schoedel57874d42021-07-20 21:10:00 -0400284const char * pychip_DeviceController_ErrorToString(ChipError::StorageType err)
yunhanw-google90ea3142020-10-01 13:53:26 -0700285{
Kevin Schoedel57874d42021-07-20 21:10:00 -0400286 return chip::ErrorStr(CHIP_ERROR(err));
yunhanw-google90ea3142020-10-01 13:53:26 -0700287}
288
Andrei Litvinb8287fe2021-02-04 15:23:05 -0500289const char * pychip_DeviceController_StatusReportToString(uint32_t profileId, uint16_t statusCode)
yunhanw-google90ea3142020-10-01 13:53:26 -0700290{
291 // return chip::StatusReportStr(profileId, statusCode);
292 return NULL;
293}
294
Andrei Litvinb8287fe2021-02-04 15:23:05 -0500295uint8_t pychip_DeviceController_GetLogFilter()
yunhanw-google90ea3142020-10-01 13:53:26 -0700296{
297#if _CHIP_USE_LOGGING
298 return chip::Logging::GetLogFilter();
299#else
300 return chip::Logging::kLogCategory_None;
301#endif
302}
303
Andrei Litvinb8287fe2021-02-04 15:23:05 -0500304void pychip_DeviceController_SetLogFilter(uint8_t category)
yunhanw-google90ea3142020-10-01 13:53:26 -0700305{
306#if _CHIP_USE_LOGGING
307 chip::Logging::SetLogFilter(category);
308#endif
309}
310
Kevin Schoedel57874d42021-07-20 21:10:00 -0400311ChipError::StorageType pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator,
312 uint32_t setupPINCode, chip::NodeId nodeid)
Song Guofe1eeb42020-10-30 04:17:04 +0800313{
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400314 return devCtrl
315 ->PairDevice(nodeid,
316 chip::RendezvousParameters()
317 .SetPeerAddress(Transport::PeerAddress(Transport::Type::kBle))
318 .SetSetupPINCode(setupPINCode)
319 .SetDiscriminator(discriminator))
320 .AsInteger();
Song Guob94c9802021-01-29 13:58:48 +0800321}
322
Kevin Schoedel57874d42021-07-20 21:10:00 -0400323ChipError::StorageType pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr,
324 uint32_t setupPINCode, chip::NodeId nodeid)
Song Guo830d1f82020-12-10 01:59:07 +0800325{
Song Guo830d1f82020-12-10 01:59:07 +0800326 chip::Inet::IPAddress peerAddr;
327 chip::Transport::PeerAddress addr;
328 chip::RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode);
329
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400330 VerifyOrReturnError(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), CHIP_ERROR_INVALID_ARGUMENT.AsInteger());
Song Guo830d1f82020-12-10 01:59:07 +0800331 // TODO: IP rendezvous should use TCP connection.
332 addr.SetTransportType(chip::Transport::Type::kUdp).SetIPAddress(peerAddr);
333 params.SetPeerAddress(addr).SetDiscriminator(0);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400334 return devCtrl->PairDevice(nodeid, params).AsInteger();
Song Guofe1eeb42020-10-30 04:17:04 +0800335}
336
Pankaj Garga04576d2021-11-08 15:04:13 -0800337void CloseSessionCallback(DeviceProxy * device, ChipError::StorageType err)
Pankaj Gargfee3d262021-10-17 19:31:32 -0700338{
339 if (device != nullptr)
340 {
Pankaj Garga04576d2021-11-08 15:04:13 -0800341 device->Disconnect();
Pankaj Gargfee3d262021-10-17 19:31:32 -0700342 }
343 if (!ChipError::IsSuccess(err))
344 {
345 ChipLogError(Controller, "Close session callback was called with an error: %d", err);
346 }
347}
348
Kevin Schoedel57874d42021-07-20 21:10:00 -0400349ChipError::StorageType pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid)
Damian Królik88cd7662021-07-17 01:00:54 +0200350{
Pankaj Gargfee3d262021-10-17 19:31:32 -0700351 return pychip_GetConnectedDeviceByNodeId(devCtrl, nodeid, CloseSessionCallback);
Damian Królik88cd7662021-07-17 01:00:54 +0200352}
C Freemanaa719692021-12-03 11:45:30 -0500353ChipError::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}
365ChipError::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ólik88cd7662021-07-17 01:00:54 +0200370
Kevin Schoedel57874d42021-07-20 21:10:00 -0400371ChipError::StorageType pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl)
cecille81850192021-05-11 21:43:14 -0400372{
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200373 Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, static_cast<uint64_t>(0));
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400374 return devCtrl->DiscoverCommissionableNodes(filter).AsInteger();
cecille81850192021-05-11 21:43:14 -0400375}
376
Kevin Schoedel57874d42021-07-20 21:10:00 -0400377ChipError::StorageType
378pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl,
379 uint16_t long_discriminator)
cecille81850192021-05-11 21:43:14 -0400380{
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200381 Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kLong, long_discriminator);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400382 return devCtrl->DiscoverCommissionableNodes(filter).AsInteger();
C Freeman8a535282021-06-22 22:54:19 -0400383}
384
Kevin Schoedel57874d42021-07-20 21:10:00 -0400385ChipError::StorageType
386pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl,
387 uint16_t short_discriminator)
C Freeman8a535282021-06-22 22:54:19 -0400388{
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200389 Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kShort, short_discriminator);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400390 return devCtrl->DiscoverCommissionableNodes(filter).AsInteger();
C Freeman8a535282021-06-22 22:54:19 -0400391}
392
Kevin Schoedel57874d42021-07-20 21:10:00 -0400393ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl,
394 uint16_t vendor)
C Freeman8a535282021-06-22 22:54:19 -0400395{
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200396 Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kVendor, vendor);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400397 return devCtrl->DiscoverCommissionableNodes(filter).AsInteger();
C Freeman8a535282021-06-22 22:54:19 -0400398}
399
Kevin Schoedel57874d42021-07-20 21:10:00 -0400400ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl,
401 uint16_t device_type)
C Freeman8a535282021-06-22 22:54:19 -0400402{
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200403 Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kDeviceType, device_type);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400404 return devCtrl->DiscoverCommissionableNodes(filter).AsInteger();
C Freeman8a535282021-06-22 22:54:19 -0400405}
406
Kevin Schoedel57874d42021-07-20 21:10:00 -0400407ChipError::StorageType
chrisdecenzo97d9f7e2021-09-14 10:37:29 -0700408pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl)
C Freeman8a535282021-06-22 22:54:19 -0400409{
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200410 Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kCommissioningMode);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400411 return devCtrl->DiscoverCommissionableNodes(filter).AsInteger();
cecille81850192021-05-11 21:43:14 -0400412}
413
Yufeng Wang275368c2021-08-31 17:42:23 -0700414ChipError::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 Garga04576d2021-11-08 15:04:13 -0800418 SetupPayload payload;
419 return devCtrl->OpenCommissioningWindow(nodeid, timeout, iteration, discriminator, option, payload).AsInteger();
Yufeng Wang275368c2021-08-31 17:42:23 -0700420}
421
cecille81850192021-05-11 21:43:14 -0400422void pychip_DeviceController_PrintDiscoveredDevices(chip::Controller::DeviceCommissioner * devCtrl)
423{
424 for (int i = 0; i < devCtrl->GetMaxCommissionableNodesSupported(); ++i)
425 {
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200426 const chip::Dnssd::DiscoveredNodeData * dnsSdInfo = devCtrl->GetDiscoveredDevice(i);
cecille81850192021-05-11 21:43:14 -0400427 if (dnsSdInfo == nullptr)
428 {
429 continue;
430 }
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200431 char rotatingId[chip::Dnssd::kMaxRotatingIdLen * 2 + 1] = "";
C Freemanb5eade02021-06-02 16:36:56 -0400432 Encoding::BytesToUppercaseHexString(dnsSdInfo->rotatingId, dnsSdInfo->rotatingIdLen, rotatingId, sizeof(rotatingId));
433
Sharad Binjolab2f8af02021-06-29 09:13:13 -0700434 ChipLogProgress(Discovery, "Commissionable Node %d", i);
Michał Szablowski30e1b972021-11-10 07:20:23 +0100435 ChipLogProgress(Discovery, "\tInstance name:\t\t%s", dnsSdInfo->instanceName);
cecille81850192021-05-11 21:43:14 -0400436 ChipLogProgress(Discovery, "\tHost name:\t\t%s", dnsSdInfo->hostName);
chrisdecenzo97d9f7e2021-09-14 10:37:29 -0700437 ChipLogProgress(Discovery, "\tPort:\t\t\t%u", dnsSdInfo->port);
cecille81850192021-05-11 21:43:14 -0400438 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 Freemanb5eade02021-06-02 16:36:56 -0400441 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ł Szablowski30e1b972021-11-10 07:20:23 +0100446 ChipLogProgress(Discovery, "\tPairing Hint\t\t%u", dnsSdInfo->pairingHint);
C Freemancb511ba2021-09-17 10:44:32 -0400447 if (dnsSdInfo->GetMrpRetryIntervalIdle().HasValue())
448 {
Zang MingJie4ec0e4e2021-12-02 15:20:06 +0800449 ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", dnsSdInfo->GetMrpRetryIntervalIdle().Value().count());
C Freemancb511ba2021-09-17 10:44:32 -0400450 }
451 else
452 {
453 ChipLogProgress(Discovery, "\tMrp Interval idle\tNot present");
454 }
455 if (dnsSdInfo->GetMrpRetryIntervalActive().HasValue())
456 {
Zang MingJie4ec0e4e2021-12-02 15:20:06 +0800457 ChipLogProgress(Discovery, "\tMrp Interval active\t%u", dnsSdInfo->GetMrpRetryIntervalActive().Value().count());
C Freemancb511ba2021-09-17 10:44:32 -0400458 }
459 else
460 {
461 ChipLogProgress(Discovery, "\tMrp Interval active\tNot present");
462 }
463 ChipLogProgress(Discovery, "\tSupports TCP\t\t%d", dnsSdInfo->supportsTcp);
cecille81850192021-05-11 21:43:14 -0400464 for (int j = 0; j < dnsSdInfo->numIPs; ++j)
465 {
Kevin Schoedel77b06032021-10-21 09:15:11 -0400466 char buf[chip::Inet::IPAddress::kMaxStringLength];
cecille81850192021-05-11 21:43:14 -0400467 dnsSdInfo->ipAddress[j].ToString(buf);
468 ChipLogProgress(Discovery, "\tAddress %d:\t\t%s", j, buf);
469 }
470 }
471}
472
473bool pychip_DeviceController_GetIPForDiscoveredDevice(chip::Controller::DeviceCommissioner * devCtrl, int idx, char * addrStr,
474 uint32_t len)
475{
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200476 const chip::Dnssd::DiscoveredNodeData * dnsSdInfo = devCtrl->GetDiscoveredDevice(idx);
cecille81850192021-05-11 21:43:14 -0400477 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 Schoedel57874d42021-07-20 21:10:00 -0400489ChipError::StorageType
Song Guoe2590b82021-02-11 03:30:13 +0800490pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl,
491 chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback)
Song Guofe1eeb42020-10-30 04:17:04 +0800492{
Song Guoe2590b82021-02-11 03:30:13 +0800493 sPairingDelegate.SetKeyExchangeCallback(callback);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400494 return CHIP_NO_ERROR.AsInteger();
Song Guofe1eeb42020-10-30 04:17:04 +0800495}
496
Kevin Schoedel57874d42021-07-20 21:10:00 -0400497ChipError::StorageType pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback(
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400498 chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningCompleteFunct callback)
499{
500 sPairingDelegate.SetCommissioningCompleteCallback(callback);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400501 return CHIP_NO_ERROR.AsInteger();
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400502}
503
Damian Królika5784162021-03-05 16:56:42 +0100504void pychip_ScriptDeviceAddressUpdateDelegate_SetOnAddressUpdateComplete(
505 chip::Controller::DeviceAddressUpdateDelegate_OnUpdateComplete callback)
506{
507 sDeviceAddressUpdateDelegate.SetOnAddressUpdateComplete(callback);
508}
509
Damian Królikb51852b2021-10-20 23:46:26 +0200510ChipError::StorageType pychip_DeviceController_UpdateDevice(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid)
Damian Królika5784162021-03-05 16:56:42 +0100511{
Damian Królikb51852b2021-10-20 23:46:26 +0200512 return devCtrl->UpdateDevice(nodeid).AsInteger();
Damian Królika5784162021-03-05 16:56:42 +0100513}
514
Kevin Schoedel57874d42021-07-20 21:10:00 -0400515ChipError::StorageType pychip_Stack_Init()
yunhanw-google90ea3142020-10-01 13:53:26 -0700516{
517 CHIP_ERROR err = CHIP_NO_ERROR;
518
Song Guofe1eeb42020-10-30 04:17:04 +0800519 err = chip::Platform::MemoryInit();
520 SuccessOrExit(err);
521
yunhanw-google90ea3142020-10-01 13:53:26 -0700522#if !CHIP_SYSTEM_CONFIG_USE_SOCKETS
523
524 ExitNow(err = CHIP_ERROR_NOT_IMPLEMENTED);
525
526#else /* CHIP_SYSTEM_CONFIG_USE_SOCKETS */
527
yunhanw-google90ea3142020-10-01 13:53:26 -0700528#endif /* CHIP_SYSTEM_CONFIG_USE_SOCKETS */
529
530exit:
531 if (err != CHIP_NO_ERROR)
Andrei Litvinb8287fe2021-02-04 15:23:05 -0500532 pychip_Stack_Shutdown();
yunhanw-google90ea3142020-10-01 13:53:26 -0700533
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400534 return err.AsInteger();
yunhanw-google90ea3142020-10-01 13:53:26 -0700535}
536
Kevin Schoedel57874d42021-07-20 21:10:00 -0400537ChipError::StorageType pychip_Stack_Shutdown()
yunhanw-google90ea3142020-10-01 13:53:26 -0700538{
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400539 return CHIP_NO_ERROR.AsInteger();
yunhanw-google90ea3142020-10-01 13:53:26 -0700540}
541
Kevin Schoedel57874d42021-07-20 21:10:00 -0400542const char * pychip_Stack_ErrorToString(ChipError::StorageType err)
yunhanw-google90ea3142020-10-01 13:53:26 -0700543{
Kevin Schoedel57874d42021-07-20 21:10:00 -0400544 return chip::ErrorStr(CHIP_ERROR(err));
yunhanw-google90ea3142020-10-01 13:53:26 -0700545}
546
Andrei Litvinb8287fe2021-02-04 15:23:05 -0500547const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t statusCode)
yunhanw-google90ea3142020-10-01 13:53:26 -0700548{
549 // return chip::StatusReportStr(profileId, statusCode);
550 return NULL;
551}
552
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400553namespace {
554struct GetDeviceCallbacks
555{
556 GetDeviceCallbacks(DeviceAvailableFunc callback) :
557 mOnSuccess(OnDeviceConnectedFn, this), mOnFailure(OnConnectionFailureFn, this), mCallback(callback)
558 {}
559
C Freeman2bd6bd62021-11-29 12:36:33 -0500560 static void OnDeviceConnectedFn(void * context, OperationalDeviceProxy * device)
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400561 {
562 auto * self = static_cast<GetDeviceCallbacks *>(context);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400563 self->mCallback(device, CHIP_NO_ERROR.AsInteger());
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400564 delete self;
565 }
566
567 static void OnConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error)
568 {
569 auto * self = static_cast<GetDeviceCallbacks *>(context);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400570 self->mCallback(nullptr, error.AsInteger());
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400571 delete self;
572 }
573
574 Callback::Callback<OnDeviceConnected> mOnSuccess;
575 Callback::Callback<OnDeviceConnectionFailure> mOnFailure;
576 DeviceAvailableFunc mCallback;
577};
578} // anonymous namespace
579
Kevin Schoedel57874d42021-07-20 21:10:00 -0400580ChipError::StorageType pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
581 DeviceAvailableFunc callback)
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400582{
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400583 VerifyOrReturnError(devCtrl != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger());
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400584 auto * callbacks = new GetDeviceCallbacks(callback);
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400585 return devCtrl->GetConnectedDevice(nodeId, &callbacks->mOnSuccess, &callbacks->mOnFailure).AsInteger();
Boris Zbarsky39d1e922021-07-02 15:23:53 -0400586}
587
C Freeman2bd6bd62021-11-29 12:36:33 -0500588ChipError::StorageType pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
589 CommissioneeDeviceProxy ** proxy)
590{
591 return devCtrl->GetDeviceBeingCommissioned(nodeId, proxy).AsInteger();
592}
593
Kevin Schoedel57874d42021-07-20 21:10:00 -0400594ChipError::StorageType pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl)
Song Guo87142e02021-04-21 09:00:00 +0800595{
596#if CONFIG_NETWORK_LAYER_BLE
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400597 return devCtrl->CloseBleConnection().AsInteger();
Song Guo87142e02021-04-21 09:00:00 +0800598#else
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400599 return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE.AsInteger();
Song Guo87142e02021-04-21 09:00:00 +0800600#endif
601}
602
Pankaj Garga04576d2021-11-08 15:04:13 -0800603uint64_t pychip_GetCommandSenderHandle(chip::DeviceProxy * device)
Song Guo9ab3c902021-04-09 06:08:35 +0800604{
Song Guo8d7486f2021-05-17 23:11:39 +0800605 return 0;
Song Guo9ab3c902021-04-09 06:08:35 +0800606}
607
Andrei Litvinb8287fe2021-02-04 15:23:05 -0500608void pychip_Stack_SetLogFunct(LogMessageFunct logFunct)
yunhanw-google90ea3142020-10-01 13:53:26 -0700609{
Andrei Litvin0a91b1b2021-02-08 09:31:24 -0500610 // 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-google90ea3142020-10-01 13:53:26 -0700617}
Song GUOed3b2332021-07-21 02:12:40 +0800618
Kevin Schoedel57874d42021-07-20 21:10:00 -0400619ChipError::StorageType pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext)
Song GUOed3b2332021-07-21 02:12:40 +0800620{
621 if (callback == nullptr || pythonContext == nullptr)
622 {
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400623 return CHIP_ERROR_INVALID_ARGUMENT.AsInteger();
Song GUOed3b2332021-07-21 02:12:40 +0800624 }
Song GUOed3b2332021-07-21 02:12:40 +0800625 PlatformMgr().ScheduleWork(callback, reinterpret_cast<intptr_t>(pythonContext));
Kevin Schoedel0fe17ec2021-08-05 08:52:07 -0400626 return CHIP_NO_ERROR.AsInteger();
Song GUOed3b2332021-07-21 02:12:40 +0800627}
Song GUO75f38952021-10-13 04:13:10 +0800628
629chip::ChipError::StorageType pychip_InteractionModel_ShutdownSubscription(uint64_t subscriptionId)
630{
631 return chip::app::InteractionModelEngine::GetInstance()->ShutdownSubscription(subscriptionId).AsInteger();
632}