Skip to content

Commit

Permalink
fix: add error message when no BT device
Browse files Browse the repository at this point in the history
  • Loading branch information
posterzh committed Dec 22, 2021
1 parent cf305ef commit 61698d1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
13 changes: 12 additions & 1 deletion lib/cputemp/advertisement.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@

import dbus
import dbus.service

from hm_pyhelper.logger import get_logger
from lib.cputemp.bletools import BleTools

logger = get_logger(__name__)

BLUEZ_SERVICE_NAME = "org.bluez"
LE_ADVERTISING_MANAGER_IFACE = "org.bluez.LEAdvertisingManager1"
DBUS_OM_IFACE = "org.freedesktop.DBus.ObjectManager"
Expand Down Expand Up @@ -134,12 +136,21 @@ def register_ad_error_callback(self, err):
def register(self):
bus = BleTools.get_bus()
adapter = BleTools.find_adapter(bus)
if not adapter:
logger.error(
"Unable to start Advertisement: No Bluetooth adapter.")
return

ad_manager = dbus.Interface(
bus.get_object(
BLUEZ_SERVICE_NAME,
adapter),
LE_ADVERTISING_MANAGER_IFACE)
if not ad_manager:
logger.error("Unable to start Advertisement: "
"No Bluetooth Advertising Manager.")
return

ad_manager.RegisterAdvertisement(
self.get_path(),
{},
Expand Down
13 changes: 11 additions & 2 deletions lib/cputemp/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
import dbus.service
import dbus.mainloop.glib
import dbus.exceptions

import array
from hm_pyhelper.logger import get_logger
from lib.cputemp.bletools import BleTools

logger = get_logger(__name__)

try:
from gi.repository import GObject
except ImportError:
import gobject as GObject

from lib.cputemp.bletools import BleTools

BLUEZ_SERVICE_NAME = "org.bluez"
GATT_MANAGER_IFACE = "org.bluez.GattManager1"
Expand Down Expand Up @@ -93,10 +95,17 @@ def register_app_error_callback(self, error):

def register(self):
adapter = BleTools.find_adapter(self.bus)
if not adapter:
logger.error(
"Unable to start Bluetooth application: No Bluetooth Adapter")
return

service_manager = dbus.Interface(
self.bus.get_object(BLUEZ_SERVICE_NAME, adapter),
GATT_MANAGER_IFACE)
if not service_manager:
logger.error("No Bluetooth Service Manager")
return

service_manager.RegisterApplication(
self.get_path(),
Expand Down
66 changes: 64 additions & 2 deletions tests/lib/cputemp/test_ble_tools.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import patch

# Test Cases
import dbus
from gatewayconfig.processors.bluetooth_services_processor import \
BluetoothServicesProcessor
from lib.cputemp.advertisement import Advertisement
from lib.cputemp.bletools import BleTools


def return_magic_mock(*args, **kwargs):
return MagicMock()


def system_bus_no_ble_side_effect(*args, **kwargs):
def get_object_side_effect(bus_name, object_path):
# When there is no BLE adapter, object_path became None
# it should raise an exception
if not object_path:
raise TypeError()
return MagicMock()

mock_dbus = MagicMock()
mock_dbus.get_object = MagicMock()
mock_dbus.get_object.side_effect = get_object_side_effect
return mock_dbus


class TestBleTools(TestCase):

def test_get_bus(self):
Expand All @@ -26,6 +41,53 @@ def test_find_adapter(self, mock_dbus_interface):
bus.get_object.assert_called()
self.assertEqual(result, None)

# Test BluetoothServicesProcessor
@patch('dbus.SystemBus')
@patch('dbus.Interface')
def test_service_no_adapter(self, mocked_interface, mocked_system_bus):
mocked_system_bus.side_effect = system_bus_no_ble_side_effect

eth0_mac_address = ""
wlan0_mac_address = ""
firmware_version = ""
ethernet_is_online_filepath = ""
shared_state = {}

try:
with self.assertLogs('lib.cputemp', level='DEBUG') as cm:
BluetoothServicesProcessor(
eth0_mac_address,
wlan0_mac_address,
firmware_version,
ethernet_is_online_filepath,
shared_state)
self.assertIn('ERROR:lib.cputemp.service:'
'Unable to start Bluetooth application: '
'No Bluetooth Adapter',
cm.output)
except TypeError:
self.fail("Exception raised when no BT adapter")

# Test Advertisement
@patch('dbus.SystemBus')
@patch('dbus.Interface')
def test_advertise_no_adapter(self, mocked_interface, mocked_system_bus):
mocked_system_bus.side_effect = system_bus_no_ble_side_effect

index = 0
advertising_type = "peripheral"
try:
with self.assertLogs('lib.cputemp',
level='DEBUG') as cm:
ble_advertisement = Advertisement(index, advertising_type)
ble_advertisement.register()
self.assertIn('ERROR:lib.cputemp.advertisement:'
'Unable to start Advertisement: '
'No Bluetooth adapter.',
cm.output)
except TypeError:
self.fail("Exception raised when no BT adapter")

@patch('dbus.Interface', side_effect=return_magic_mock)
def test_find_connection(self, mock_dbus_interface):
bus = MagicMock()
Expand Down

0 comments on commit 61698d1

Please sign in to comment.