forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start a script-friendly CHIP REPL for python (project-chip#4696)
* Define a chip-repl script that is intended to provide an interactive console against the python interfaces that chip provides * Make GetAdapters work on chip repl * Restyle fixes
- Loading branch information
Showing
7 changed files
with
152 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python | ||
|
||
# | ||
# Copyright (c) 2021 Project CHIP Authors | ||
# 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 | ||
# | ||
# https://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. | ||
# | ||
|
||
from IPython import embed | ||
import chip | ||
|
||
def main(): | ||
# The chip import at the top level will be visible in the ipython REPL. | ||
embed(header = ''' | ||
Welcome to the CHIP python REPL utilty. | ||
Usage examples: | ||
######## List available BLE adapters ######### | ||
for adapter in chip.GetBleAdapters(): | ||
print(adapter) | ||
'''.strip()) | ||
|
||
if __name__ == "__main__": | ||
main() |
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 |
---|---|---|
|
@@ -22,3 +22,5 @@ | |
# | ||
|
||
"""Provides Python APIs for CHIP.""" | ||
|
||
from chip.ble import GetBleAdapters |
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,72 @@ | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
"""BLE-related functionality within CHIP""" | ||
|
||
from chip import ChipDeviceCtrl | ||
import sys | ||
import platform | ||
|
||
class Adapter: | ||
"""Generic exposed adapter information.""" | ||
|
||
def __init__(self, name, macAddress): | ||
self.name = name | ||
self.macAddress = macAddress | ||
|
||
def __str__(self): | ||
return 'chip.ble.Adapter(%s, %s)' % (self.name, self.macAddress) | ||
|
||
if platform.system() == 'Darwin': | ||
from chip.ChipCoreBluetoothMgr import CoreBluetoothManager as BleManager | ||
|
||
def ConvertNativeAdapter(nativeAdapter): | ||
raise NotImplementedError('Not implemented for Darwin') | ||
|
||
elif sys.platform.startswith('linux'): | ||
from chip.ChipBluezMgr import BluezManager as BleManager | ||
|
||
def ConvertNativeAdapter(nativeAdapter): | ||
return Adapter(name = str(nativeAdapter.path), macAddress = str(nativeAdapter.Address)) | ||
|
||
class LazyHandles(object): | ||
"""Contains a handle to an underlying BLE manager.""" | ||
|
||
def __init__(self): | ||
self._deviceController = None | ||
self._bleManager = None | ||
|
||
@property | ||
def deviceController(self): | ||
if self._deviceController is None: | ||
self._deviceController = ChipDeviceCtrl.ChipDeviceController() | ||
return self._deviceController | ||
|
||
@property | ||
def bleManager(self): | ||
if self._bleManager is None: | ||
self._bleManager = BleManager(self.deviceController) | ||
return self._bleManager | ||
|
||
def GetAdapters(self): | ||
"""Return available BLE adapters on this platform.""" | ||
return [ConvertNativeAdapter(a) for a in self.bleManager.get_adapters()] | ||
|
||
|
||
lazyHandles = LazyHandles() | ||
|
||
def GetBleAdapters(): | ||
return lazyHandles.GetAdapters() | ||
|
||
__all__ = [ | ||
"GetBleAdapters", | ||
] |