From 63798be712ed1dbcce59d7bbf76f6d9fbd77c749 Mon Sep 17 00:00:00 2001 From: Jerry Johns Date: Thu, 7 Jul 2022 09:14:20 -0700 Subject: [PATCH] Brought back the auto-stack init in native, albeit, a minimal version only. Updated ChipStack to now use native to implicitly achieve this auto init (which sets up MemoryInit) before continuining onwards with controller-style initialization. --- src/controller/python/BUILD.gn | 2 +- .../ChipDeviceController-ScriptBinding.cpp | 5 -- src/controller/python/CommonStackInit.h | 47 ------------------- .../{ => chip/native}/CommonStackInit.cpp | 19 +++----- src/controller/python/chip/native/__init__.py | 2 + 5 files changed, 9 insertions(+), 66 deletions(-) delete mode 100644 src/controller/python/CommonStackInit.h rename src/controller/python/{ => chip/native}/CommonStackInit.cpp (85%) diff --git a/src/controller/python/BUILD.gn b/src/controller/python/BUILD.gn index d9cc0dd4fd3d6a..63332c5fe59071 100644 --- a/src/controller/python/BUILD.gn +++ b/src/controller/python/BUILD.gn @@ -44,7 +44,7 @@ shared_library("ChipDeviceCtrl") { "chip/setup_payload/Parser.cpp", ] - sources += [ "CommonStackInit.cpp" ] + sources += [ "chip/native/CommonStackInit.cpp" ] if (chip_controller) { sources += [ diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp index 2d54e6fcf7cd09..8ed19a674aeb85 100644 --- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp +++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp @@ -52,7 +52,6 @@ #include #include -#include #include #include @@ -223,8 +222,6 @@ chip::Controller::Python::StorageAdapter * pychip_Storage_GetStorageAdapter() ChipError::StorageType pychip_DeviceController_StackInit(uint32_t bluetoothAdapterId) { - ReturnErrorOnFailure(chip::Controller::Python::CommonStackInit().AsInteger()); - VerifyOrDie(sStorageAdapter != nullptr); #if CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE @@ -291,8 +288,6 @@ ChipError::StorageType pychip_DeviceController_StackShutdown() DeviceControllerFactory::GetInstance().Shutdown(); - chip::Controller::Python::CommonStackShutdown(); - return CHIP_NO_ERROR.AsInteger(); } diff --git a/src/controller/python/CommonStackInit.h b/src/controller/python/CommonStackInit.h deleted file mode 100644 index a28f4163ee5e0f..00000000000000 --- a/src/controller/python/CommonStackInit.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * - * Copyright (c) 2020-2022 Project CHIP Authors - * Copyright (c) 2019-2020 Google LLC. - * Copyright (c) 2013-2018 Nest Labs, Inc. - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * Implementation of the native methods expected by the Python - * version of Chip Device Manager. - * - */ -#include - -namespace chip { -namespace Controller { -namespace Python { - -// -// This encapsulates stack initialization logic that is common to both -// controller and server initialization sequences. -// -CHIP_ERROR CommonStackInit(); - -// -// This encapsulates stack shutdown logic that is common to both -// controller and server shutdown sequences. -// -void CommonStackShutdown(); - -} // namespace Python -} // namespace Controller -} // namespace chip diff --git a/src/controller/python/CommonStackInit.cpp b/src/controller/python/chip/native/CommonStackInit.cpp similarity index 85% rename from src/controller/python/CommonStackInit.cpp rename to src/controller/python/chip/native/CommonStackInit.cpp index 7bfdb860e04531..a6a9b3e455899e 100644 --- a/src/controller/python/CommonStackInit.cpp +++ b/src/controller/python/chip/native/CommonStackInit.cpp @@ -25,8 +25,6 @@ * */ -#include - #include #include @@ -39,21 +37,19 @@ #include #include -namespace chip { -namespace Controller { -namespace Python { +static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); + +extern "C" { -CHIP_ERROR CommonStackInit() +CHIP_ERROR pychip_CommonStackInit() { ReturnErrorOnFailure(chip::Platform::MemoryInit()); ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgr().InitChipStack()); return CHIP_NO_ERROR; } -void CommonStackShutdown() +void pychip_CommonStackShutdown() { - chip::DeviceLayer::PlatformMgr().Shutdown(); - #if 0 // // We cannot actually call this because the destructor for the MdnsContexts singleton on Darwin only gets called // on termination of the program, and that unfortunately makes a bunch of Platform::MemoryFree calls. @@ -61,7 +57,4 @@ void CommonStackShutdown() chip::Platform::MemoryShutdown(); #endif } - -} // namespace Python -} // namespace Controller -} // namespace chip +}; diff --git a/src/controller/python/chip/native/__init__.py b/src/controller/python/chip/native/__init__.py index b34bb0adb1f4a0..b3770ac2881235 100644 --- a/src/controller/python/chip/native/__init__.py +++ b/src/controller/python/chip/native/__init__.py @@ -76,5 +76,7 @@ def GetLibraryHandle() -> ctypes.CDLL: if _nativeLibraryHandle is None: _nativeLibraryHandle = ctypes.CDLL(FindNativeLibraryPath()) setter = NativeLibraryHandleMethodArguments(_nativeLibraryHandle) + setter.Set("pychip_CommonStackInit", None, []) + _nativeLibraryHandle.pychip_CommonStackInit() return _nativeLibraryHandle