Skip to content

Commit

Permalink
Brought back the auto-stack init in native, albeit, a minimal version
Browse files Browse the repository at this point in the history
only.

Updated ChipStack to now use native to implicitly achieve this auto init
(which sets up MemoryInit) before continuining onwards with
controller-style initialization.
  • Loading branch information
mrjerryjohns committed Jul 7, 2022
1 parent c9b6e0c commit fa7ac2f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/controller/python/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ shared_library("ChipDeviceCtrl") {
"chip/setup_payload/Parser.cpp",
]

sources += [ "CommonStackInit.cpp" ]
sources += [ "chip/native/CommonStackInit.cpp" ]

if (chip_controller) {
sources += [
Expand Down
7 changes: 1 addition & 6 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@

#include <controller/python/ChipDeviceController-ScriptDevicePairingDelegate.h>
#include <controller/python/ChipDeviceController-StorageDelegate.h>
#include <controller/python/CommonStackInit.h>
#include <controller/python/chip/interaction_model/Delegate.h>

#include <credentials/GroupDataProviderImpl.h>
Expand Down Expand Up @@ -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
Expand All @@ -247,7 +244,7 @@ ChipError::StorageType pychip_DeviceController_StackInit(uint32_t bluetoothAdapt
factoryParams.enableServerInteractions = true;

// Hack needed due to the fact that DnsSd server uses the CommissionableDataProvider even
// when never starting operational advertising. This will not be used but prevents
// when never starting commissionable advertising. This will not be used but prevents
// null pointer dereferences.
static chip::DeviceLayer::TestOnlyCommissionableDataProvider TestOnlyCommissionableDataProvider;
chip::DeviceLayer::SetCommissionableDataProvider(&TestOnlyCommissionableDataProvider);
Expand Down Expand Up @@ -291,8 +288,6 @@ ChipError::StorageType pychip_DeviceController_StackShutdown()

DeviceControllerFactory::GetInstance().Shutdown();

chip::Controller::Python::CommonStackShutdown();

return CHIP_NO_ERROR.AsInteger();
}

Expand Down
47 changes: 0 additions & 47 deletions src/controller/python/CommonStackInit.h

This file was deleted.

53 changes: 16 additions & 37 deletions src/controller/python/chip/ChipStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
from .clusters import Objects as GeneratedObjects
from .clusters.CHIPClusters import *

import chip.native

__all__ = [
"DeviceStatusStruct",
"ChipStackException",
Expand Down Expand Up @@ -182,7 +184,10 @@ def __init__(self, persistentStoragePath: str, installDefaultLogHandler=True, bl
self._activeLogFunct = None
self.addModulePrefixToLogMessage = True

#
# Locate and load the chip shared library.
# This also implictly does a minimal stack initialization (i.e call MemoryInit).
#
self._loadLib()

# Arrange to log output from the chip library to a python logger object with the
Expand Down Expand Up @@ -317,6 +322,12 @@ def Shutdown(self):
# to avoid accessing builtins.chipStack after destruction.
self._persistentStorage = None
self.Call(lambda: self._ChipStackLib.pychip_DeviceController_StackShutdown())

#
# Stack init happens in native, but shutdown happens here unfortunately.
# #20437 tracks consolidating these.
#
self._ChipStackLib.pychip_CommonStackShutdown()
self.networkLock = None
self.completeEvent = None
self._ChipStackLib = None
Expand Down Expand Up @@ -404,42 +415,8 @@ def ErrorToException(self, err, devStatusPtr=None):
)

def LocateChipDLL(self):
if self._chipDLLPath:
return self._chipDLLPath

scriptDir = os.path.dirname(os.path.abspath(__file__))

# When properly installed in the chip package, the Chip Device Manager DLL will
# be located in the package root directory, along side the package's
# modules.
dmDLLPath = os.path.join(scriptDir, ChipStackDLLBaseName)
if os.path.exists(dmDLLPath):
self._chipDLLPath = dmDLLPath
return self._chipDLLPath

# For the convenience of developers, search the list of parent paths relative to the
# running script looking for an CHIP build directory containing the Chip Device
# Manager DLL. This makes it possible to import and use the ChipDeviceMgr module
# directly from a built copy of the CHIP source tree.
buildMachineGlob = "%s-*-%s*" % (platform.machine(),
platform.system().lower())
relDMDLLPathGlob = os.path.join(
"build",
buildMachineGlob,
"src/controller/python/.libs",
ChipStackDLLBaseName,
)
for dir in self._AllDirsToRoot(scriptDir):
dmDLLPathGlob = os.path.join(dir, relDMDLLPathGlob)
for dmDLLPath in glob.glob(dmDLLPathGlob):
if os.path.exists(dmDLLPath):
self._chipDLLPath = dmDLLPath
return self._chipDLLPath

raise Exception(
"Unable to locate Chip Device Manager DLL (%s); expected location: %s"
% (ChipStackDLLBaseName, scriptDir)
)
self._loadLib()
return self._chipDLLPath

# ----- Private Members -----
def _AllDirsToRoot(self, dir):
Expand All @@ -453,7 +430,9 @@ def _AllDirsToRoot(self, dir):

def _loadLib(self):
if self._ChipStackLib is None:
self._ChipStackLib = CDLL(self.LocateChipDLL())
self._ChipStackLib = chip.native.GetLibraryHandle()
self._chipDLLPath = chip.native.FindNativeLibraryPath()

self._ChipStackLib.pychip_DeviceController_StackInit.argtypes = [c_uint32]
self._ChipStackLib.pychip_DeviceController_StackInit.restype = c_uint32
self._ChipStackLib.pychip_DeviceController_StackShutdown.argtypes = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*
*/

#include <controller/python/CommonStackInit.h>

#include <stdio.h>
#include <stdlib.h>

Expand All @@ -39,29 +37,24 @@
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>

namespace chip {
namespace Controller {
namespace Python {
static_assert(std::is_same<uint32_t, chip::ChipError::StorageType>::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.
//
chip::Platform::MemoryShutdown();
#endif
}

} // namespace Python
} // namespace Controller
} // namespace chip
};
7 changes: 7 additions & 0 deletions src/controller/python/chip/native/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,12 @@ def GetLibraryHandle() -> ctypes.CDLL:
if _nativeLibraryHandle is None:
_nativeLibraryHandle = ctypes.CDLL(FindNativeLibraryPath())
setter = NativeLibraryHandleMethodArguments(_nativeLibraryHandle)
setter.Set("pychip_CommonStackInit", None, [])

#
# We've a split initialization model with some init happening here and some other
# bits of init happening in ChipStack. #20437 tracks consolidating those.
#
_nativeLibraryHandle.pychip_CommonStackInit()

return _nativeLibraryHandle

0 comments on commit fa7ac2f

Please sign in to comment.