Skip to content

Commit

Permalink
backends:bgapi: support environment variable detection
Browse files Browse the repository at this point in the history
This allows examples to be switched to the alternate backend by simply
specifiying an environment variable.

Signed-off-by: Karl Palsson <[email protected]>
  • Loading branch information
karlp committed Jan 9, 2023
1 parent ea1826d commit be2d621
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
20 changes: 15 additions & 5 deletions bleak/backends/bgapi/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import asyncio
import logging
import struct
Expand Down Expand Up @@ -56,14 +58,22 @@ def __init__(self, address_or_ble_device: Union[BLEDevice, str], **kwargs):
self._mtu_size: Optional[int] = None
self._services_resolved = False

self._adapter: Optional[str] = kwargs.get("adapter", kwargs.get("ncp"))
self._bgapi = kwargs.get(
"bgapi",
"/home/karlp/SimplicityStudio/SDKs/gecko_sdk_2/protocol/bluetooth/api/sl_bt.xapi",
# Env vars have priority
self._bgapi = os.environ.get("BLEAK_BGAPI_XAPI", kwargs.get("bgapi", None))
if not self._bgapi:
raise BleakError(
"BGAPI file for your target (sl_bt.xapi) is required, normally this is in your SDK tree"
)
self._adapter = os.environ.get(
"BLEAK_BGAPI_ADAPTER", kwargs.get("adapter", "/dev/ttyACM0")
)
baudrate = os.environ.get(
"BLEAK_BGAPI_BAUDRATE", kwargs.get("bgapi_baudrate", 115200)
)

### XXX are we in trouble here making a new serial connection? the scanner does too!
self._lib = bgapi.BGLib(
bgapi.SerialConnector(self._adapter),
bgapi.SerialConnector(self._adapter, baudrate=baudrate),
self._bgapi,
event_handler=self._bgapi_evt_handler,
)
Expand Down
18 changes: 14 additions & 4 deletions bleak/backends/bgapi/scanner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
import os
import struct
import sys
from typing import List, Optional
Expand All @@ -12,6 +13,7 @@
else:
from typing import Literal

from ...exc import BleakError
from ..scanner import AdvertisementData, AdvertisementDataCallback, BaseBleakScanner

logger = logging.getLogger(__name__)
Expand All @@ -33,14 +35,22 @@ def __init__(
super(BleakScannerBGAPI, self).__init__(detection_callback, service_uuids)
self._adapter: Optional[str] = kwargs.get("adapter", kwargs.get("ncp"))

self._bgapi = kwargs.get(
"bgapi",
"/home/karlp/SimplicityStudio/SDKs/gecko_sdk_2/protocol/bluetooth/api/sl_bt.xapi",
# Env vars have priority
self._bgapi = os.environ.get("BLEAK_BGAPI_XAPI", kwargs.get("bgapi", None))
if not self._bgapi:
raise BleakError(
"BGAPI file for your target (sl_bt.xapi) is required, normally this is in your SDK tree"
)
self._adapter = os.environ.get(
"BLEAK_BGAPI_ADAPTER", kwargs.get("adapter", "/dev/ttyACM0")
)
baudrate = os.environ.get(
"BLEAK_BGAPI_BAUDRATE", kwargs.get("bgapi_baudrate", 115200)
)

self._loop = asyncio.get_running_loop()
self._lib = bgapi.BGLib(
bgapi.SerialConnector(self._adapter),
bgapi.SerialConnector(self._adapter, baudrate=baudrate),
self._bgapi,
event_handler=self._bgapi_evt_handler,
)
Expand Down
5 changes: 5 additions & 0 deletions bleak/backends/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def get_platform_client_backend_type() -> Type[BaseBleakClient]:
"""
Gets the platform-specific :class:`BaseBleakClient` type.
"""
if os.environ.get("BLEAK_BGAPI_XAPI") is not None:
from bleak.backends.bgapi.client import BleakClientBGAPI

return BleakClientBGAPI

if os.environ.get("P4A_BOOTSTRAP") is not None:
from bleak.backends.p4android.client import BleakClientP4Android

Expand Down
5 changes: 5 additions & 0 deletions bleak/backends/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ def get_platform_scanner_backend_type() -> Type[BaseBleakScanner]:
"""
Gets the platform-specific :class:`BaseBleakScanner` type.
"""
if os.environ.get("BLEAK_BGAPI_XAPI") is not None:
from bleak.backends.bgapi.scanner import BleakScannerBGAPI

return BleakScannerBGAPI

if os.environ.get("P4A_BOOTSTRAP") is not None:
from bleak.backends.p4android.scanner import BleakScannerP4Android

Expand Down

0 comments on commit be2d621

Please sign in to comment.