def ble_scan(self, duration, result_format=BleInterface.SCAN_RESULT_FORMAT_DEFAULT): """ Function defined in GapInterface. """ if not self._enable_bluetooth(): return None command = '+SRBLESCAN' args = [duration, int(result_format)] if result_format == BleInterface.SCAN_RESULT_FORMAT_DEFAULT: if self._write(command, args, duration * 2) is EUL_RESULT_SUCCESS: regex = r"\"([\w|:]{17})\",([0|1]),-(\d+),(\d+)(,\"(.+)\")?" responses = self._serial.serial_search_regex_all(regex) return [ BleInterface.ScanResult( Euler._convert_euler_address_to_standard(resp[0]), int(resp[1]), int(resp[2]), int(resp[3]), resp[4]) for resp in responses ] elif result_format == BleInterface.SCAN_RESULT_FORMAT_RAW_DATA: if self._write(command, args, duration * 2) is EUL_RESULT_SUCCESS: regex = r"\"([\w|:]{17})\",([0|1]),-(\d+),\"((\\[0-9A-F]{2})+)\"" responses = self._serial.serial_search_regex_all(regex) return [BleInterface.ScanRawResult( \ Euler._convert_euler_address_to_standard(resp[0]), int(resp[1]), int(resp[2]), Euler._convert_escaped_string_to_data(resp[3])) for resp in responses] else: raise ValueError('invalid result_format') return None
def ble_create_session(self, bdaddr): """Function defined in BleInterface. """ command = '+SRBLECFG' args = ['"' + bdaddr.addr + '"'] if self._write(command, args) is EDD_RESULT_SUCCESS: regex = r"\+SRBLECFG: (\d+),([0|1]),\"([\w|:]{17})\"(,(\d),(\d+))?" response = self._serial.serial_search_regex(regex) return BleInterface.BleSession( int(response[0]), BleInterface.Bdaddr(response[2], bdaddr.addr_type)) return None
def ble_get_all_sessions(self): """Function defined in BleInterface. """ command = '+SRBLECFG' if self._query(command) is EDD_RESULT_SUCCESS: regex = r"\+SRBLECFG: (\d+),([0|1]),\"([\w|:]{17})\"(,(\d),(\d+))?" responses = self._serial.serial_search_regex_all(regex) sessions = [] for resp in responses: sessions.append( BleInterface.BleSession( int(resp[0]), BleInterface.Bdaddr( resp[2], BleInterface.LE_BDADDR_TYPE_UNKNOWN))) return sessions return None
def ble_get_local_address(self): """Function defined in BleInterface.""" addr = Melody._convert_melody_address_to_standard( self._get_config('LOCAL_ADDR')[1]) addr_type = True if self._get_config( 'BLE_CONFIG')[3] == 'ON' else False return BleInterface.Bdaddr(addr, addr_type)
def ble_create_session(self, bdaddr): """Function defined in BleInterface. """ if not self._enable_bluetooth(): return None command = '+SRBLECFG' args = ['"' + bdaddr.addr + '"'] if self._write(command, args) is EUL_RESULT_SUCCESS: regex = r"\+SRBLECFG: (\d+),([0|1]),\"(%s)\",(\d+)" % ( Euler._convert_standard_address_to_euler(bdaddr.addr)) response = self._serial.serial_search_regex(regex) return BleInterface.BleSession( int(response[0]), BleInterface.Bdaddr( Euler._convert_euler_address_to_standard(response[2]), bdaddr.addr_type)) return None
def ble_get_all_sessions(self): """Function defined in BleInterface.""" # use virtual sessions since Melody does not have sessions. return [ BleInterface.BleSession(s.session_id, s.bdaddr) for s in self._ble_sessions ]
def ble_get_local_address(self): """Function defined in BleInterface. """ command = '+SRBLEADDR' if self._query(command) is EDD_RESULT_SUCCESS: regex = r"\+SRBLEADDR: \"([\w|:]{17})\",(\d)" response = self._serial.serial_search_regex(regex) return BleInterface.Bdaddr(response[0], int(response[1])) return None
def ble_get_all_sessions(self): """Function defined in BleInterface. """ if not self._enable_bluetooth(): return None command = '+SRBLECFG' if self._query(command) is EUL_RESULT_SUCCESS: regex = r"\+SRBLECFG: (\d+),([0|1]),\"([\w|:]{17})\",(\d+)" sessions = [] responses = self._serial.serial_search_regex_all(regex) for resp in responses: sessions.append( BleInterface.BleSession( int(resp[0]), BleInterface.Bdaddr( Euler._convert_euler_address_to_standard(resp[2]), BleInterface.LE_BDADDR_TYPE_UNKNOWN))) return sessions return None
def ble_get_local_address(self): """Function defined in BleInterface. """ if not self._enable_bluetooth(): return None command = '+SRBTADDR' if self._query(command) is EUL_RESULT_SUCCESS: regex = r"\+SRBTADDR: \"([\w|:]{17})\"" response = self._serial.serial_search_regex(regex) return BleInterface.Bdaddr( Euler._convert_euler_address_to_standard(response[0]), BleInterface.LE_BDADDR_TYPE_PUBLIC) return None
def ble_gatt_wait_for_write_request(self, session_id, handle, timeout=5): """Function defined in GattInterface. """ regex = r"\+SRBLEWRITE_(REQ|IND): %d,%d,(\d+),(\d+),\"(.+)\"" % ( session_id, handle) response = self._serial.serial_search_regex(regex, timeout) if response: offset = int(response[1]) value = Eddington._convert_escaped_string_to_data(response[3]) need_rsp = True if response[0] == 'REQ' else False return BleInterface.GattWriteReq(session_id, handle, offset, value, need_rsp) return None
def test_ble_session_02(dut): """Verify DUT can list all the BLE sessions. """ # GIVEN a = dut.ble_create_session( BleInterface.Bdaddr('20:FA:BB:00:00:AA', BleInterface.LE_BDADDR_TYPE_PUBLIC)) assert a is not None b = dut.ble_create_session( BleInterface.Bdaddr('11:22:33:44:55:66', BleInterface.LE_BDADDR_TYPE_PRIVATE)) assert b is not None c = dut.ble_create_session( BleInterface.Bdaddr('77:88:99:AA:BB:CC', BleInterface.LE_BDADDR_TYPE_PRIVATE)) assert c is not None # WHEN list = dut.ble_get_all_sessions() # THEN assert a in list assert b in list assert c in list
def ble_gatt_wait_for_write_request(self, session_id, handle, timeout=5): """Function defined in GattInterface. """ regex = r"\+SRBLEWRITE: %d,%d,\"(.+)\"" % (session_id, handle) response = self._serial.serial_search_regex(regex, timeout) if response: value = Euler._convert_escaped_string_to_data(response[0]) warnings.warn('offset not supported - ignored.') warnings.warn( 'need_rsp automatically handled (accepted) by Euler. \ Write requests cannot be rejected.') return BleInterface.GattWriteReq(session_id, handle, 0, value, False) return None
def test_ble_session_01(dut): """Verify DUT can create up to 16 BLE sessions. """ # GIVEN 'N/A' # WHEN for i in range(0, 15): addr = '20:FA:BB:00:00:{:02X}'.format(i) session = dut.ble_create_session( BleInterface.Bdaddr(addr, BleInterface.LE_BDADDR_TYPE_PUBLIC)) assert session.session_id == (i + 1) assert session.bdaddr.addr == addr # THEN 'N/A'
def ble_gatt_wait_for_write_request(self, session_id, handle, timeout=5): """Function defined in GattInterface.""" link_id = self._ble_sessions_get_link_id_from_session_id(session_id) if link_id is not None: regex = r"BLE_WRITE {:X} {:04X} (\d+) (\d+)".format( link_id, handle) + BC127_EOL response = self._serial.serial_search_regex(regex, timeout) if response: data_iter = iter(response[1]) value = [int(a + b, 16) for a, b in zip(data_iter, data_iter)] return BleInterface.GattWriteReq(session_id, handle, 0, value, False) return None
def ble_scan(self, duration, result_format=BleInterface.SCAN_RESULT_FORMAT_DEFAULT): """Function defined in GapInterface.""" command = 'SCAN' args = [duration] if result_format == BleInterface.SCAN_RESULT_FORMAT_DEFAULT: if self._execute( command, args, success_string='SCAN_OK', timeout=duration + 1) is BC127_RESULT_SUCCESS: regex = r"SCAN (\w{12}) (0|1) <(.+)> ([0-9A-F]{2}) -(\d+)dBm" + BC127_EOL responses = self._serial.serial_search_regex_all(regex) return [BleInterface.ScanResult( \ Melody._convert_melody_address_to_standard(resp[0]), int(resp[1]), int(resp[4]), int(resp[3], 16), resp[2]) for resp in responses] elif result_format == BleInterface.SCAN_RESULT_FORMAT_RAW_DATA: args.append('ON') if self._execute( command, args, success_string='SCAN_OK', timeout=duration + 1) is BC127_RESULT_SUCCESS: scan_raw_regex = r"SCAN_RAW (\w{12}) (\d) -(\d+)dBm (\d+) ([0-9A-F| ]+)" + BC127_EOL responses = self._serial.serial_search_regex_all( scan_raw_regex) return [BleInterface.ScanRawResult( \ Melody._convert_melody_address_to_standard(resp[0]), int(resp[1]), int(resp[2]), [int(x, 16) for x in resp[4].split()]) for resp in responses] else: raise ValueError('invalid result_format') return None
def ble_gatt_discover_all_characteristics(self, session_id): """Function defined in GattInterface.""" link_id = self._ble_sessions_get_link_id_from_session_id(session_id) if link_id is not None: command = 'BLE_GET_CHAR' args = ['{:X}'.format(link_id)] if self._execute(command, args) is BC127_RESULT_SUCCESS: regex = r"BLE_CHAR (\d4) (\w+) ([0-9A-F|\-]+) ([0-9A-F]{4}) ([0-9A-F]{2})" \ + BC127_EOL responses = self._serial.serial_search_regex_all(regex) return [ BleInterface.GattCharacteristic(resp[2], int(resp[3], 16), int(resp[4], 16)) for resp in responses ] return None
def ble_gatt_discover_all_characteristics(self, session_id): """Function defined in GattInterface. """ if not self._enable_bluetooth(): return None command = '+SRBLEDISCCHAR' args = [session_id] if self._write(command, args) is EUL_RESULT_SUCCESS: regex = r"\+SRBLEDISCCHAR: (%d),\"([0-9a-f|\-]+)\",(\d+),(\d+)" % ( session_id) responses = self._serial.serial_search_regex_all(regex) res = [] for resp in responses: res.append( BleInterface.GattCharacteristic(resp[1].upper(), int(resp[3]), int(resp[2]))) return res return None
def ble_gatt_discover_all_primary_services(self, session_id): """Function defined in GattInterface. """ if not self._enable_bluetooth(): return None command = '+SRBLEDISCSERV' args = [session_id] res = None if self._write(command, args) is EUL_RESULT_SUCCESS: regex = r"\+SRBLEDISCSERV: (%d),\"([0-9a-f|\-]+)\",(1),(\d+),(\d+)" % ( session_id) responses = self._serial.serial_search_regex_all(regex) res = [] for resp in responses: res.append( BleInterface.GattService(resp[1].upper(), True, int(resp[3]), int(resp[4]))) return res return None
def ble_wait_for_connection(self, timeout=10): """Function defined in GapInterface.""" regex = r"OPEN_OK (\d4) BLE (\w{12})" + BC127_EOL response = self._serial.serial_search_regex(regex, timeout) if response: link_id = int(response[0], 16) bdaddr = BleInterface.Bdaddr( self._convert_melody_address_to_standard(response[1]), BleInterface.LE_BDADDR_TYPE_UNKNOWN) session_id = self._ble_sessions_get_session_from_bdaddr(bdaddr) if session_id: # virtual BLE session already exists self._ble_sessions_set_session_link_id(session_id, link_id) else: # need to create a new virtual BLE session new_session = self.ble_create_session(bdaddr) self._ble_sessions_set_session_link_id(new_session.session_id, link_id) return True return False