class TestMockBLEBasic(unittest.TestCase): def setUp(self): self.dev = SimpleVirtualDevice(1, 'TestCN') self.ble = MockBLEDevice("AA:BB:CC:DD:EE:FF", self.dev) logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) def tearDown(self): pass def test_basic_functionality(self): """Make sure that things are setup right """ assert self.ble.notifications_enabled(self.ble.TBReceiveHeaderChar) is False assert self.ble.notifications_enabled(self.ble.TBReceivePayloadChar) is False assert self.ble.notifications_enabled(self.ble.TBStreamingChar) is False def test_calling_invalid_rpc(self): """Make sure that basically calling an RPC works """ header = struct.pack("<BBBBB", 0, 0, 0xFF, 0xFF, 127) success, notif = self.ble._handle_write(self.ble.TBSendPayloadChar, b"") assert success is True assert len(notif) == 0 success, notif = self.ble._handle_write(self.ble.TBSendHeaderChar, header) assert success is True assert len(notif) == 1 char_id, resp_header = notif[0] assert self.ble.find_uuid(char_id)[0] == self.ble.TBReceiveHeaderChar status, _, resp_len, _ = struct.unpack("<BBBB", resp_header) assert status == 0xFF assert resp_len == 0 def test_calling_valid_rpc(self): """Make sure that calling a valid RPC with a payload works """ header = struct.pack("<BBBBB", 0, 0, 0x04, 0x00, 8) success, notif = self.ble._handle_write(self.ble.TBSendPayloadChar, b"") assert success is True assert len(notif) == 0 success, notif = self.ble._handle_write(self.ble.TBSendHeaderChar, header) assert success is True assert len(notif) == 2 char_id, resp_header = notif[0] assert self.ble.find_uuid(char_id)[0] == self.ble.TBReceiveHeaderChar char_id, resp_payload = notif[1] assert self.ble.find_uuid(char_id)[0] == self.ble.TBReceivePayloadChar
def setUp(self): self.old_serial = serial.Serial serial.Serial = util.dummy_serial.Serial self.adapter = MockBLED112(3) self.dev1 = MockIOTileDevice(100, 'TestCN') self.dev1_ble = MockBLEDevice("00:11:22:33:44:55", self.dev1) self.adapter.add_device(self.dev1_ble) self.dev1.reports = [ IndividualReadingReport.FromReadings(100, [IOTileReading(0, 1, 2)]) ] self._reports_received = threading.Event() logging.basicConfig(level=logging.INFO, stream=sys.stdout) util.dummy_serial.RESPONSE_GENERATOR = self.adapter.generate_response self.scanned_devices = [] self.bled = BLED112Adapter('test', self._on_scan_callback, self._on_disconnect_callback, passive=False, stop_check_interval=0.01) self.bled.add_callback('on_report', self._on_report_callback) self.reports = []
def setUp(self): self.old_serial = serial.Serial serial.Serial = util.dummy_serial.Serial self.adapter = MockBLED112(3) self.dev1 = SimpleVirtualDevice(100, 'TestCN') self.dev1_ble = MockBLEDevice("00:11:22:33:44:55", self.dev1) self.adapter.add_device(self.dev1_ble) util.dummy_serial.RESPONSE_GENERATOR = self.adapter.generate_response self._connected = threading.Event() self._rpcs_enabled = threading.Event() self.scanned_devices = [] self.bled = BLED112Adapter('test', self._on_scan_callback, self._on_disconnect_callback, stop_check_interval=0.01)
def setUp(self): self.old_serial = serial.Serial serial.Serial = util.dummy_serial.Serial self.adapter = MockBLED112(3) self.dev1 = MockIOTileDevice(100, 'TestCN') self.dev1_ble = MockBLEDevice("00:11:22:33:44:55", self.dev1) self.adapter.add_device(self.dev1_ble) util.dummy_serial.RESPONSE_GENERATOR = self.adapter.generate_response self.dev1.reports = [ IndividualReadingReport.FromReadings(100, [IOTileReading(0, 1, 2)]) ] self._reports_received = threading.Event() logging.basicConfig(level=logging.INFO, stream=sys.stdout) self.scanned_devices = [] self.hw = HardwareManager(port='bled112:test')
class TestBLED112Connections(unittest.TestCase): """ Test to make sure that the BLED112Adapter is properly handling disconnections """ def setUp(self): self.old_serial = serial.Serial serial.Serial = util.dummy_serial.Serial self.adapter = MockBLED112(3) self.dev1 = SimpleVirtualDevice(100, 'TestCN') self.dev1_ble = MockBLEDevice("00:11:22:33:44:55", self.dev1) self.adapter.add_device(self.dev1_ble) util.dummy_serial.RESPONSE_GENERATOR = self.adapter.generate_response self._connected = threading.Event() self._rpcs_enabled = threading.Event() self.scanned_devices = [] self.bled = BLED112Adapter('test', self._on_scan_callback, self._on_disconnect_callback, stop_check_interval=0.01) def tearDown(self): self.bled.stop_sync() serial.Serial = self.old_serial def test_connect_but_nodevice(self): start = time.time() self.bled.connect_async(0, "00:11:22:33:44:56", self._on_connection) self._connected.wait() end = time.time() assert self._conn_id == 0 assert self._result is False def test_connection_succeeds(self): start = time.time() self.bled.connect_async(1, "00:11:22:33:44:55", self._on_connection) self._connected.wait() end = time.time() print(self.bled._connections[0]['services']) assert self._conn_id == 1 assert self._result is True def test_disconnect_nodevice(self): result = self.bled.disconnect_sync(1) assert result['success'] is False def test_disconnect_device(self): result = self.bled.connect_sync(1, "00:11:22:33:44:55") assert result['success'] is True result = self.bled.disconnect_sync(1) assert result['success'] is True def test_enable_rpcs(self): result = self.bled.connect_sync(1, "00:11:22:33:44:55") assert result['success'] is True assert not self.dev1_ble.notifications_enabled( self.dev1_ble.TBReceiveHeaderChar) assert not self.dev1_ble.notifications_enabled( self.dev1_ble.TBReceivePayloadChar) result = self.bled.open_interface_sync(1, 'rpc') assert result['success'] is True assert self.dev1_ble.notifications_enabled( self.dev1_ble.TBReceiveHeaderChar) assert self.dev1_ble.notifications_enabled( self.dev1_ble.TBReceivePayloadChar) def _on_connection(self, conn_id, adapter_id, result, value): self._result = result self._conn_id = conn_id self._value = value self._connected.set() def _on_rpcs_enabled(self, conn_id, result, value): self._result = result self._conn_id = conn_id self._value = value self._rpcs_enabled.set() def _on_scan_callback(self, ad_id, info, expiry): pass def _on_disconnect_callback(self, *args, **kwargs): pass
def setUp(self): self.dev = MockIOTileDevice('TestCN') self.ble = MockBLEDevice("AA:BB:CC:DD:EE:FF", self.dev) logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)