Skip to content

Commit

Permalink
Add direct bluetooth commissioning support (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Jul 11, 2024
1 parent 0b85b49 commit fa4002e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions matter_server/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class ServerInfoMessage:
sdk_version: str
wifi_credentials_set: bool
thread_credentials_set: bool
bluetooth_enabled: bool


MessageType = (
Expand Down
7 changes: 7 additions & 0 deletions matter_server/server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
action="store_true",
help="Enable PAA root certificates and other device information from test-net DCL.",
)
parser.add_argument(
"--bluetooth-adapter",
type=int,
required=False,
help="Optional bluetooth adapter (id) to enable direct commisisoning support.",
)

args = parser.parse_args()

Expand Down Expand Up @@ -187,6 +193,7 @@ def main() -> None:
args.primary_interface,
args.paa_root_cert_dir,
args.enable_test_net_dcl,
args.bluetooth_adapter,
)

async def handle_stop(loop: asyncio.AbstractEventLoop) -> None:
Expand Down
4 changes: 3 additions & 1 deletion matter_server/server/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ async def commission_with_code(
:return: The NodeInfo of the commissioned device.
"""
node_id = self._get_next_node_id()
if not network_only and not self.server.bluetooth_enabled:
raise NodeCommissionFailed("Bluetooth commissioning is not available.")

node_id = self._get_next_node_id()
LOGGER.info(
"Starting Matter commissioning with code using Node ID %s.",
node_id,
Expand Down
10 changes: 5 additions & 5 deletions matter_server/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
ServerInfoMessage,
)
from ..server.client_handler import WebsocketClientHandler
from .const import (
DEFAULT_PAA_ROOT_CERTS_DIR,
MIN_SCHEMA_VERSION,
)
from .const import DEFAULT_PAA_ROOT_CERTS_DIR, MIN_SCHEMA_VERSION
from .device_controller import MatterDeviceController
from .stack import MatterStack
from .storage import StorageController
Expand Down Expand Up @@ -109,6 +106,7 @@ def __init__(
primary_interface: str | None = None,
paa_root_cert_dir: Path | None = None,
enable_test_net_dcl: bool = False,
bluetooth_adapter_id: int | None = None,
) -> None:
"""Initialize the Matter Server."""
self.storage_path = storage_path
Expand All @@ -122,11 +120,12 @@ def __init__(
else:
self.paa_root_cert_dir = Path(paa_root_cert_dir).absolute()
self.enable_test_net_dcl = enable_test_net_dcl
self.bluetooth_enabled = bluetooth_adapter_id is not None
self.logger = logging.getLogger(__name__)
self.app = web.Application()
self.loop: asyncio.AbstractEventLoop | None = None
# Instantiate the Matter Stack using the SDK using the given storage path
self.stack = MatterStack(self)
self.stack = MatterStack(self, bluetooth_adapter_id)
self.storage = StorageController(self)
self.vendor_info = VendorInfo(self)
# we dynamically register command handlers
Expand Down Expand Up @@ -243,6 +242,7 @@ def get_info(self) -> ServerInfoMessage:
sdk_version=chip_clusters_version(),
wifi_credentials_set=self._device_controller.wifi_credentials_set,
thread_credentials_set=self._device_controller.thread_credentials_set,
bluetooth_enabled=self.bluetooth_enabled,
)

@api_command(APICommand.SERVER_DIAGNOSTICS)
Expand Down
13 changes: 11 additions & 2 deletions matter_server/server/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,22 @@ class MatterStack:
def __init__(
self,
server: "MatterServer",
bluetooth_adapter_id: int | None = None,
) -> None:
"""Initialize Matter Stack."""
self.logger = logging.getLogger(__name__)
self.logger.info("Initializing CHIP/Matter Controller Stack...")
storage_file = os.path.join(server.storage_path, "chip.json")
self.logger.debug("Using storage file: %s", storage_file)
chip.native.Init()
self.logger.debug(
"Using storage file: %s - Bluetooth commissioning enabled: %s",
storage_file,
"NO"
if bluetooth_adapter_id is None
else f"YES (adapter {bluetooth_adapter_id})",
)
# give the fake adapter id of 999 to disable bluetooth
# because None means use the default adapter
chip.native.Init(999 if bluetooth_adapter_id is None else bluetooth_adapter_id)

# Initialize logging after stack init!
# See: https://github.com/project-chip/connectedhomeip/issues/20233
Expand Down

0 comments on commit fa4002e

Please sign in to comment.