-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch ble adapter listing to use the native bluez interface. (#4839)
* Add an adapter iterator class to bluez * code review updates * Fix typo in method * Fix compilation and off-by-one error * Ensure we unref the adapter during listing * Remove unused variable * Fix typos in comments - I clobbered the fixes that Justin pushed * Fix typos in comments - I clobbered the fixes that Justin pushed * Add support for native adapter listing * Restyle fixes * Update the init logic * Do not auto-import GetAdapters. ble is a stand alone package for now * Update typing * Move iterator values to std::string and fix typo in linux impl * Switch reinterpret cast to static cast
- Loading branch information
Showing
9 changed files
with
218 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
#include <platform/CHIPDeviceLayer.h> | ||
#include <platform/Linux/CHIPBluezHelper.h> | ||
#include <platform/internal/BLEManager.h> | ||
#include <support/CHIPMem.h> | ||
#include <support/ReturnMacros.h> | ||
|
||
using namespace chip::DeviceLayer::Internal; | ||
|
||
/////////// Listing adapters implementation ////////// | ||
|
||
extern "C" void * pychip_ble_adapter_list_new() | ||
{ | ||
return static_cast<void *>(new chip::DeviceLayer::Internal::AdapterIterator()); | ||
} | ||
|
||
extern "C" void pychip_ble_adapter_list_delete(void * adapter) | ||
{ | ||
delete static_cast<chip::DeviceLayer::Internal::AdapterIterator *>(adapter); | ||
} | ||
|
||
extern "C" bool pychip_ble_adapter_list_next(void * adapter) | ||
{ | ||
return static_cast<chip::DeviceLayer::Internal::AdapterIterator *>(adapter)->Next(); | ||
} | ||
|
||
extern "C" unsigned pychip_ble_adapter_list_get_index(void * adapter) | ||
{ | ||
/// NOTE: returning unsigned because python native has no sized values | ||
return static_cast<chip::DeviceLayer::Internal::AdapterIterator *>(adapter)->GetIndex(); | ||
} | ||
|
||
extern "C" const char * pychip_ble_adapter_list_get_address(void * adapter) | ||
{ | ||
return static_cast<chip::DeviceLayer::Internal::AdapterIterator *>(adapter)->GetAddress(); | ||
} | ||
|
||
extern "C" const char * pychip_ble_adapter_list_get_alias(void * adapter) | ||
{ | ||
return static_cast<chip::DeviceLayer::Internal::AdapterIterator *>(adapter)->GetAlias(); | ||
} | ||
|
||
extern "C" const char * pychip_ble_adapter_list_get_name(void * adapter) | ||
{ | ||
return static_cast<chip::DeviceLayer::Internal::AdapterIterator *>(adapter)->GetName(); | ||
} | ||
|
||
extern "C" bool pychip_ble_adapter_list_is_powered(void * adapter) | ||
{ | ||
return static_cast<chip::DeviceLayer::Internal::AdapterIterator *>(adapter)->IsPowered(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import ctypes | ||
import glob | ||
import os | ||
import platform | ||
|
||
NATIVE_LIBRARY_BASE_NAME = "_ChipDeviceCtrl.so" | ||
|
||
|
||
def _AllDirsToRoot(dir): | ||
"""Return all parent paths of a directory.""" | ||
dir = os.path.abspath(dir) | ||
while True: | ||
yield dir | ||
parent = os.path.dirname(dir) | ||
if parent == "" or parent == dir: | ||
break | ||
dir = parent | ||
|
||
|
||
def FindNativeLibraryPath() -> str: | ||
"""Find the native CHIP dll/so path.""" | ||
|
||
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( | ||
os.path.dirname(scriptDir), # file should be inside 'chip' | ||
NATIVE_LIBRARY_BASE_NAME) | ||
if os.path.exists(dmDLLPath): | ||
return dmDLLPath | ||
|
||
# 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", | ||
NATIVE_LIBRARY_BASE_NAME, | ||
) | ||
for dir in _AllDirsToRoot(scriptDir): | ||
dmDLLPathGlob = os.path.join(dir, relDMDLLPathGlob) | ||
for dmDLLPath in glob.glob(dmDLLPathGlob): | ||
if os.path.exists(dmDLLPath): | ||
return dmDLLPath | ||
|
||
raise Exception( | ||
"Unable to locate Chip Device Manager DLL (%s); expected location: %s" % | ||
(NATIVE_LIBRARY_BASE_NAME, scriptDir)) | ||
|
||
|
||
class NativeLibraryHandleMethodArguments: | ||
"""Convenience wrapper to set native method argtype and restype for methods.""" | ||
|
||
def __init__(self, handle): | ||
self.handle = handle | ||
|
||
def Set(self, methodName: str, resultType, argumentTypes: list): | ||
method = getattr(self.handle, methodName) | ||
method.restype = resultType | ||
method.argtype = argumentTypes | ||
|
||
|
||
_nativeLibraryHandle: ctypes.CDLL = None | ||
|
||
|
||
def GetLibraryHandle() -> ctypes.CDLL: | ||
"""Get a memoized handle to the chip native code dll.""" | ||
|
||
global _nativeLibraryHandle | ||
if _nativeLibraryHandle is None: | ||
_nativeLibraryHandle = ctypes.CDLL(FindNativeLibraryPath()) | ||
|
||
return _nativeLibraryHandle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters