diff --git a/lib/cputemp/advertisement.py b/lib/cputemp/advertisement.py index 7a961e1..0217ae3 100755 --- a/lib/cputemp/advertisement.py +++ b/lib/cputemp/advertisement.py @@ -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" @@ -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(), {}, diff --git a/lib/cputemp/service.py b/lib/cputemp/service.py index 438d6ec..d546ff9 100755 --- a/lib/cputemp/service.py +++ b/lib/cputemp/service.py @@ -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" @@ -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(), diff --git a/tests/lib/cputemp/test_ble_tools.py b/tests/lib/cputemp/test_ble_tools.py index 9e2e0a5..2d98b76 100644 --- a/tests/lib/cputemp/test_ble_tools.py +++ b/tests/lib/cputemp/test_ble_tools.py @@ -1,9 +1,10 @@ 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 @@ -11,6 +12,20 @@ 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): @@ -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()