コード例 #1
0
ファイル: l2cap.py プロジェクト: AyturkDuzen/auto-pts
def l2cap_conn(bd_addr, bd_addr_type, psm, mtu=0, num=1, ecfc=0, hold_credit=0):
    logging.debug("%s %r %r %r", l2cap_conn.__name__, bd_addr, bd_addr_type,
                  psm)
    iutctl = get_iut()
    gap_wait_for_connection()

    if isinstance(psm, str):
        psm = int(psm, 16)

    bd_addr = pts_addr_get(bd_addr)
    bd_addr_type = pts_addr_type_get(bd_addr_type)

    bd_addr_ba = addr2btp_ba(bd_addr)
    data_ba = bytearray(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(struct.pack('H', psm))
    data_ba.extend(struct.pack('H', mtu))
    data_ba.extend(struct.pack('B', num))

    opts = 0
    if ecfc:
        opts |= defs.L2CAP_CONNECT_OPT_ECFC

    if hold_credit:
        opts |= defs.L2CAP_CONNECT_OPT_HOLD_CREDIT

    data_ba.extend(struct.pack('B', opts))

    iutctl.btp_socket.send(*L2CAP['connect'], data=data_ba)

    chan_ids = l2cap_conn_rsp()
    logging.debug("id %r", chan_ids)
コード例 #2
0
def gattc_write(bd_addr_type, bd_addr, hdl, val, val_mtp=None):
    logging.debug("%s %r %r %r %r %r", gattc_write.__name__, bd_addr_type,
                  bd_addr, hdl, val, val_mtp)
    iutctl = get_iut()

    gap_wait_for_connection()

    if isinstance(hdl, str):
        hdl = int(hdl, 16)

    if val_mtp:
        val *= int(val_mtp)

    data_ba = bytearray()

    bd_addr_ba = addr2btp_ba(bd_addr)
    hdl_ba = struct.pack('H', hdl)
    val_ba = binascii.unhexlify(val)
    val_len_ba = struct.pack('H', len(val_ba))

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(hdl_ba)
    data_ba.extend(val_len_ba)
    data_ba.extend(val_ba)

    iutctl.btp_socket.send(*GATTC['write'], data=data_ba)
コード例 #3
0
def gattc_write_long(bd_addr_type, bd_addr, hdl, off, val, length=None):
    logging.debug("%s %r %r %r %r %r", gattc_write_long.__name__, bd_addr_type,
                  hdl, off, val, length)
    gap_wait_for_connection()

    if isinstance(hdl, str):
        hdl = int(hdl, 16)  # convert string in hex format to int

    if isinstance(off, str):
        off = int(off, 16)

    if length:
        val *= int(length)

    iutctl = get_iut()

    bd_addr_ba = addr2btp_ba(bd_addr)
    hdl_ba = struct.pack('H', hdl)
    off_ba = struct.pack('H', off)
    val_ba = bytes.fromhex(val)
    val_len_ba = struct.pack('H', len(val_ba))

    data_ba = bytearray()
    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(hdl_ba)
    data_ba.extend(off_ba)
    data_ba.extend(val_len_ba)
    data_ba.extend(val_ba)

    iutctl.btp_socket.send(*GATTC['write_long'], data=data_ba)
コード例 #4
0
def gattc_read_long(bd_addr_type, bd_addr, hdl, off, modif_off=None):
    logging.debug("%s %r %r %r %r %r", gattc_read_long.__name__, bd_addr_type,
                  bd_addr, hdl, off, modif_off)
    iutctl = get_iut()

    gap_wait_for_connection()

    data_ba = bytearray()

    if isinstance(off, str):
        off = int(off, 16)
    if modif_off:
        off += modif_off
    if isinstance(hdl, str):
        hdl = int(hdl, 16)

    bd_addr_ba = addr2btp_ba(bd_addr)
    hdl_ba = struct.pack('H', hdl)
    off_ba = struct.pack('H', off)

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(hdl_ba)
    data_ba.extend(off_ba)

    iutctl.btp_socket.send(*GATTC['read_long'], data=data_ba)
コード例 #5
0
def gattc_cfg_indicate(bd_addr_type, bd_addr, enable, ccc_hdl):
    logging.debug("%s %r %r, %r, %r", gattc_cfg_indicate.__name__,
                  bd_addr_type, bd_addr, enable, ccc_hdl)
    gap_wait_for_connection()

    if isinstance(ccc_hdl, str):
        ccc_hdl = int(ccc_hdl, 16)

    iutctl = get_iut()

    bd_addr_ba = addr2btp_ba(bd_addr)
    ccc_hdl_ba = struct.pack('H', ccc_hdl)

    data_ba = bytearray()
    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(chr(enable).encode('utf-8'))
    data_ba.extend(ccc_hdl_ba)

    iutctl.btp_socket.send(*GATTC['cfg_indicate'], data=data_ba)

    tuple_hdr, tuple_data = iutctl.btp_socket.read()
    logging.debug("%s received %r %r", gattc_cfg_indicate.__name__, tuple_hdr,
                  tuple_data)

    btp_hdr_check(tuple_hdr, defs.BTP_SERVICE_ID_GATT, defs.GATT_CFG_INDICATE)
コード例 #6
0
def gattc_read_uuid(bd_addr_type, bd_addr, start_hdl, end_hdl, uuid):
    logging.debug("%s %r %r %r %r %r", gattc_read_uuid.__name__, bd_addr_type,
                  bd_addr, start_hdl, end_hdl, uuid)
    iutctl = get_iut()

    gap_wait_for_connection()

    if isinstance(start_hdl, str):
        start_hdl = int(start_hdl, 16)

    if isinstance(end_hdl, str):
        end_hdl = int(end_hdl, 16)

    data_ba = bytearray()

    bd_addr_ba = addr2btp_ba(bd_addr)
    start_hdl_ba = struct.pack('H', start_hdl)
    end_hdl_ba = struct.pack('H', end_hdl)

    if "-" in uuid:
        uuid = uuid.replace("-", "")
    if uuid.startswith("0x"):
        uuid = uuid.replace("0x", "")
    uuid_ba = binascii.unhexlify(uuid)[::-1]

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(start_hdl_ba)
    data_ba.extend(end_hdl_ba)
    data_ba.extend(chr(len(uuid_ba)).encode('utf-8'))
    data_ba.extend(uuid_ba)

    iutctl.btp_socket.send(*GATTC['read_uuid'], data=data_ba)
コード例 #7
0
def gattc_disc_all_desc(bd_addr_type, bd_addr, start_hdl, stop_hdl):
    logging.debug("%s %r %r %r %r", gattc_disc_all_desc.__name__, bd_addr_type,
                  bd_addr, start_hdl, stop_hdl)
    iutctl = get_iut()

    gap_wait_for_connection()

    if isinstance(start_hdl, str):
        start_hdl = int(start_hdl, 16)

    if isinstance(stop_hdl, str):
        stop_hdl = int(stop_hdl, 16)

    data_ba = bytearray()

    bd_addr_ba = addr2btp_ba(bd_addr)
    start_hdl_ba = struct.pack('H', start_hdl)
    stop_hdl_ba = struct.pack('H', stop_hdl)

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(start_hdl_ba)
    data_ba.extend(stop_hdl_ba)

    iutctl.btp_socket.send(*GATTC['disc_all_desc'], data=data_ba)
コード例 #8
0
def gattc_disc_all_prim(bd_addr_type, bd_addr):
    logging.debug("%s %r %r", gattc_disc_all_prim.__name__, bd_addr_type,
                  bd_addr)
    iutctl = get_iut()

    gap_wait_for_connection()

    data_ba = bytearray()

    bd_addr_ba = addr2btp_ba(bd_addr)

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)

    iutctl.btp_socket.send(*GATTC['disc_all_prim'], data=data_ba)
コード例 #9
0
def gattc_exchange_mtu(bd_addr_type, bd_addr):
    logging.debug("%s %r %r", gattc_exchange_mtu.__name__, bd_addr_type,
                  bd_addr)
    iutctl = get_iut()

    gap_wait_for_connection()

    data_ba = bytearray()
    bd_addr_ba = addr2btp_ba(bd_addr)

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)

    iutctl.btp_socket.send(*GATTC['exchange_mtu'], data=data_ba)

    gatt_command_rsp_succ()
コード例 #10
0
def gattc_find_included(bd_addr_type, bd_addr, start_hdl=None, end_hdl=None):
    logging.debug("%s %r %r %r %r", gattc_find_included.__name__, bd_addr_type,
                  bd_addr, start_hdl, end_hdl)
    gap_wait_for_connection()

    if start_hdl and end_hdl:
        _gattc_find_included_req(bd_addr_type, bd_addr, start_hdl, end_hdl)
        return

    gattc_disc_all_prim(bd_addr_type, bd_addr)
    svcs_tuple = gattc_disc_all_prim_rsp()

    global VERIFY_VALUES
    VERIFY_VALUES = []

    for start, end, _ in svcs_tuple:
        _gattc_find_included_req(bd_addr_type, bd_addr, start, end)
        _gattc_find_included_rsp()
コード例 #11
0
def gattc_disc_prim_uuid(bd_addr_type, bd_addr, uuid):
    logging.debug("%s %r %r %r", gattc_disc_prim_uuid.__name__, bd_addr_type,
                  bd_addr, uuid)
    iutctl = get_iut()

    gap_wait_for_connection()

    data_ba = bytearray()

    bd_addr_ba = addr2btp_ba(bd_addr)
    uuid_ba = bytes.fromhex(uuid.replace("-", ""))

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(chr(len(uuid_ba)).encode('utf-8'))
    data_ba.extend(uuid_ba)

    iutctl.btp_socket.send(*GATTC['disc_prim_uuid'], data=data_ba)
コード例 #12
0
def gattc_disc_all_chrc(bd_addr_type, bd_addr, start_hdl, stop_hdl, svc=None):
    logging.debug("%s %r %r %r %r %r", gattc_disc_all_chrc.__name__,
                  bd_addr_type, bd_addr, start_hdl, stop_hdl, svc)
    iutctl = get_iut()

    gap_wait_for_connection()

    if svc:
        svc_nb = svc[1]
        for s in GATT_SVCS:
            if not ((svc[0][0] and svc[0][0] != s[0]) and
                    (svc[0][1] and svc[0][1] != s[1]) and
                    (svc[0][2] and svc[0][2] != s[2])):

                # To take n-th service
                svc_nb -= 1
                if svc_nb != 0:
                    continue

                start_hdl = s[0]
                stop_hdl = s[1]

                logging.debug("Got requested service!")

                break

    if isinstance(start_hdl, str):
        start_hdl = int(start_hdl, 16)

    if isinstance(stop_hdl, str):
        stop_hdl = int(stop_hdl, 16)

    data_ba = bytearray()

    bd_addr_ba = addr2btp_ba(bd_addr)
    start_hdl_ba = struct.pack('H', start_hdl)
    stop_hdl_ba = struct.pack('H', stop_hdl)

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(start_hdl_ba)
    data_ba.extend(stop_hdl_ba)

    iutctl.btp_socket.send(*GATTC['disc_all_chrc'], data=data_ba)
コード例 #13
0
def gattc_read(bd_addr_type, bd_addr, hdl):
    logging.debug("%s %r %r %r", gattc_read.__name__, bd_addr_type, bd_addr,
                  hdl)
    iutctl = get_iut()

    gap_wait_for_connection()

    data_ba = bytearray()

    bd_addr_ba = addr2btp_ba(bd_addr)
    if isinstance(hdl, str):
        hdl = int(hdl, 16)
    hdl_ba = struct.pack('H', hdl)

    data_ba.extend(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(hdl_ba)

    iutctl.btp_socket.send(*GATTC['read'], data=data_ba)
コード例 #14
0
ファイル: l2cap.py プロジェクト: agata-ponitka/auto-pts
def l2cap_conn(bd_addr, bd_addr_type, psm, mtu=0, num=1):
    logging.debug("%s %r %r %r", l2cap_conn.__name__, bd_addr, bd_addr_type,
                  psm)
    iutctl = get_iut()
    gap_wait_for_connection()

    if isinstance(psm, str):
        psm = int(psm, 16)

    bd_addr = pts_addr_get(bd_addr)
    bd_addr_type = pts_addr_type_get(bd_addr_type)

    bd_addr_ba = addr2btp_ba(bd_addr)
    data_ba = bytearray(chr(bd_addr_type).encode('utf-8'))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(struct.pack('H', psm))
    data_ba.extend(struct.pack('H', mtu))
    data_ba.extend(struct.pack('B', num))

    iutctl.btp_socket.send(*L2CAP['connect'], data=data_ba)

    chan_ids = l2cap_conn_rsp()
    logging.debug("id %r", chan_ids)
コード例 #15
0
def gattc_read_multiple_var(bd_addr_type, bd_addr, *hdls):
    logging.debug("%s %r %r %r", gattc_read_multiple_var.__name__,
                  bd_addr_type, bd_addr, hdls)
    iutctl = get_iut()

    gap_wait_for_connection()

    data_ba = bytearray()

    bd_addr_ba = addr2btp_ba(bd_addr)
    hdls_j = ''.join(hdl for hdl in hdls)
    hdls_byte_table = [hdls_j[i:i + 2] for i in range(0, len(hdls_j), 2)]
    hdls_swp = ''.join([
        c[1] + c[0] for c in zip(hdls_byte_table[::2], hdls_byte_table[1::2])
    ])
    hdls_ba = binascii.unhexlify(bytearray(hdls_swp))

    data_ba.extend(chr(bd_addr_type))
    data_ba.extend(bd_addr_ba)
    data_ba.extend(chr(len(hdls)))
    data_ba.extend(hdls_ba)

    iutctl.btp_socket.send(*GATTC['read_multiple_var'], data=data_ba)