Example #1
0
def hdl_wid_22(desc):
    MMI.reset()
    MMI.parse_description(desc)

    parsed_args = []

    for arg in MMI.args:
        parsed_args.append(filter(lambda char: char != "-", arg))

    handles = []
    uuids = []

    # Extract UUID's from parsed arguments
    uuids_from_parse = parsed_args[::3]

    # Delete unwanted UUID values
    del parsed_args[0::3]
    parsed_handles = parsed_args

    # Convert remaining arguments to integers
    parsed_handles = [int(arg, 16) for arg in parsed_handles]

    # Increment every 2th handle
    parsed_handles[1::2] = [arg + 1 for arg in parsed_handles[1::2]]

    # Get all primary services
    attrs = btp.gatts_get_attrs(type_uuid='2800')
    for attr in attrs:
        start_handle, perm, type_uuid = attr

        val = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                     btp.pts_addr_get(), start_handle)
        if not val:
            continue

        (_, uuid_len, uuid) = val

        uuids.append(str(btp.btp2uuid(uuid_len, uuid)))
        handles.append(start_handle)

    for uuid in uuids_from_parse:
        if uuid in uuids_from_parse:
            logging.debug("UUUID %r present", uuid)
            continue
        else:
            logging.debug("UUID %r not present", uuid)
            return False
    for handle in parsed_handles:
        if handle in parsed_handles:
            logging.debug("Handle %r present", handle)
            continue
        else:
            logging.debug("Handle $r not present", handle)
            return False

    return True
Example #2
0
def gatt_server_fetch_db():
    db = GattDB()
    bd_addr = btp.pts_addr_get()
    bd_addr_type = btp.pts_addr_type_get()

    attrs = btp.gatts_get_attrs()
    for attr in attrs:
        handle, perm, type_uuid = attr

        attr_val = btp.gatts_get_attr_val(bd_addr_type, bd_addr, handle)
        if not attr_val:
            logging.debug("cannot read value %r", handle)
            continue

        att_rsp, val_len, val = attr_val

        if type_uuid in ('2800', '2801'):
            uuid = btp.btp2uuid(val_len, val)

            if type_uuid == '2800':
                db.attr_add(handle, GattPrimary(handle, perm, uuid, att_rsp))
            else:
                db.attr_add(handle, GattSecondary(handle, perm, uuid, att_rsp))
        elif type_uuid == '2803':

            hdr = '<BH'
            hdr_len = struct.calcsize(hdr)
            uuid_len = val_len - hdr_len

            prop, value_handle, uuid = struct.unpack("<BH%ds" % uuid_len, val)
            uuid = btp.btp2uuid(uuid_len, uuid)

            db.attr_add(
                handle,
                GattCharacteristic(handle, perm, uuid, att_rsp, prop,
                                   value_handle))
        elif type_uuid == '2802':
            hdr = "<HH"
            hdr_len = struct.calcsize(hdr)
            uuid_len = val_len - hdr_len
            incl_svc_hdl, end_grp_hdl, uuid = struct.unpack(
                hdr + "%ds" % uuid_len, val)
            uuid = btp.btp2uuid(uuid_len, uuid)

            db.attr_add(
                handle,
                GattServiceIncluded(handle, perm, uuid, att_rsp, incl_svc_hdl,
                                    end_grp_hdl))
        else:
            uuid = type_uuid.replace("0x", "").replace("-", "").upper()

            db.attr_add(
                handle,
                GattCharacteristicDescriptor(handle, perm, uuid, att_rsp, val))

    return db
def hdl_wid_51(desc):
    MMI.reset()
    MMI.parse_description(desc)

    btp.gattc_read_uuid(btp.pts_addr_type_get(), btp.pts_addr_get(),
                        MMI.args[1], MMI.args[2], MMI.args[0])

    btp.gattc_read_uuid_rsp(True)

    return True
Example #4
0
def hdl_wid_104(params: WIDParams):
    stack = get_stack()
    if stack.gap.io_cap == IOCap.keyboard_only:
        bd_addr = btp.pts_addr_get()
        bd_addr_type = btp.pts_addr_type_get()
        if stack.gap.get_passkey() is None:
            return False
        btp.gap_passkey_entry_rsp(bd_addr, bd_addr_type,
                                  stack.gap.passkey.data)
    return btp.var_store_get_passkey(params.description)
Example #5
0
def hdl_wid_304(params: WIDParams):
    MMI.reset()
    MMI.parse_description(params.description)

    hdl = MMI.args[0]
    val = MMI.args[1]
    _, _, data = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                        btp.pts_addr_get(), hdl)
    data = hexlify(data).decode().upper()
    return bool(data in val)
def hdl_wid_108(desc):
    MMI.reset()
    MMI.parse_description(desc)

    btp.gattc_read_uuid(btp.pts_addr_type_get(), btp.pts_addr_get(), 0x0001,
                        0xffff, MMI.args[0])

    btp.gattc_read_uuid_rsp(True, True)

    return True
Example #7
0
def hdl_wid_60(desc):
    btp.gap_set_conn()
    btp.gap_set_gendiscov()

    bd_addr = btp.pts_addr_get()
    bd_addr_type = btp.pts_addr_type_get()

    btp.gap_direct_adv_on(bd_addr, bd_addr_type)

    return True
def hdl_wid_76(desc):
    MMI.reset()
    MMI.parse_description(desc)

    btp.gattc_write_reliable(btp.pts_addr_type_get(), btp.pts_addr_get(),
                             MMI.args[0], 0, '12', MMI.args[1])

    btp.gattc_write_reliable_rsp(True)

    return True
Example #9
0
def hdl_wid_112(desc):
    bd_addr = btp.pts_addr_get()
    bd_addr_type = btp.pts_addr_type_get()

    handle = btp.parse_handle_description(desc)
    if not handle:
        return False

    btp.gattc_read(bd_addr_type, bd_addr, handle)
    btp.gattc_read_rsp()
    return True
Example #10
0
def hdl_wid_90(_: WIDParams):
    stack = get_stack()
    gatt = stack.gatt

    gatt.wait_notification_ev(timeout=5)

    assert gatt.notification_events
    addr_type, addr, notif_type, _, _ = gatt.notification_events[0]

    return (addr_type, addr, notif_type) == (btp.pts_addr_type_get(),
                                             btp.pts_addr_get(), 1)
Example #11
0
def hdl_wid_52(desc):
    MMI.reset()
    MMI.parse_description(desc)

    handle = int(MMI.args[0], 16)

    _, _, value = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                         btp.pts_addr_get(), handle)

    value_read = hexlify(value).decode('utf-8')

    return bool(value_read == MMI.args[1])
Example #12
0
def hdl_wid_112(desc):
    bd_addr = btp.pts_addr_get()
    bd_addr_type = btp.pts_addr_type_get()

    handle = btp.parse_handle_description(desc)
    if not handle:
        return False

    btp.gattc_read(bd_addr_type, bd_addr, handle)
    # PTS doesn't respond to read req if we do not respond to this WID
    # btp.gattc_read_rsp()
    return True
Example #13
0
def hdl_wid_25(desc):
    MMI.reset()
    MMI.parse_description(desc)

    pts_chrc_uuid = MMI.args[0]
    pts_chrc_handles = [
        int(MMI.args[1], 16),
        int(MMI.args[2], 16),
        int(MMI.args[3], 16),
        int(MMI.args[4], 16)
    ]

    iut_start_handle = None
    iut_end_handle = None

    bd_addr = btp.pts_addr_get()
    bd_addr_type = btp.pts_addr_type_get()

    # Find pts_chrc_uuid service and it's handle range
    svcs = btp.gatts_get_attrs(type_uuid='2800')
    for svc in svcs:
        handle, perm, type_uuid = svc

        if iut_start_handle:
            iut_end_handle = handle - 1
            break

        svc_val = btp.gatts_get_attr_val(bd_addr_type, bd_addr, handle)
        if not svc_val:
            continue

        att_rsp, uuid_len, uuid = svc_val
        if btp.btp2uuid(uuid_len, uuid) == pts_chrc_uuid:
            iut_start_handle = handle

    if iut_start_handle is None or iut_end_handle is None:
        logging.error("service %s not found", pts_chrc_uuid)
        return False

    iut_chrc_handles = []

    # Lookup all services within service range
    chrcs = btp.gatts_get_attrs(type_uuid='2803',
                                start_handle=iut_start_handle,
                                end_handle=iut_end_handle)
    for chrc in chrcs:
        handle, perm, type_uuid = chrc
        iut_chrc_handles.append(handle)

    if iut_chrc_handles != pts_chrc_handles:
        return False

    return True
Example #14
0
def hdl_wid_139(desc):
    MMI.reset()
    MMI.parse_description(desc)

    hdl = MMI.args[0]

    if not hdl:
        logging.error("parsing error")
        return False

    read_val = btp.gatts_get_attr_val(btp.pts_addr_type_get(), btp.pts_addr_get(), hdl)
    return bool(read_val == COMPARED_VALUE[0])
Example #15
0
def hdl_wid_142(desc):
    MMI.reset()
    MMI.parse_description(desc)

    hdl = MMI.args[0]

    if not hdl:
        logging.error("parsing error")
        return False

    btp.gattc_write(btp.pts_addr_type_get(None), btp.pts_addr_get(None), hdl,
                    '02', 1)
    return True
Example #16
0
def hdl_wid_91(desc):
    pattern = re.compile("'([0-9a-fA-F]+)'")
    params = pattern.findall(desc)
    if not params:
        logging.error("parsing error")
        return False

    handle = params[0]

    btp.gattc_cfg_notify(btp.pts_addr_type_get(), btp.pts_addr_get(), 1,
                         handle)

    return True
Example #17
0
def hdl_wid_304(desc):
    MMI.reset()
    MMI.parse_description(desc)

    hdl = MMI.args[0]
    val = MMI.args[1]
    _, _, data = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                        btp.pts_addr_get(), hdl)
    data = hexlify(data).decode().upper()
    if data in val:
        return True
    else:
        return False
Example #18
0
def hdl_wid_99(params: WIDParams):
    pattern = re.compile("'([0-9a-fA-F]+)'")
    params = pattern.findall(params.description)
    if not params:
        logging.error("parsing error")
        return False

    handle = params[0]

    btp.gattc_cfg_indicate(btp.pts_addr_type_get(), btp.pts_addr_get(), 1,
                           handle)

    return True
Example #19
0
def hdl_wid_135(params: WIDParams):
    MMI.reset()
    MMI.parse_description(params.description)

    hdl = MMI.args[0]

    if not hdl:
        logging.error("parsing error")
        return False

    btp.gattc_write(btp.pts_addr_type_get(None), btp.pts_addr_get(None), hdl,
                    '01', 1)
    return True
Example #20
0
def hdl_wid_122(desc):
    # Lookup characteristic UUID that returns Insufficient Encryption Key Size
    chrcs = btp.gatts_get_attrs(type_uuid='2803')
    for chrc in chrcs:
        handle, perm, type_uuid = chrc

        chrc_data = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                           btp.pts_addr_get(), handle)
        if not chrc_data:
            continue

        att_rsp, val_len, val = chrc_data

        hdr = '<BH'
        hdr_len = struct.calcsize(hdr)
        uuid_len = val_len - hdr_len

        prop, handle, chrc_uuid = struct.unpack("<BH%ds" % uuid_len, val)
        chrc_value_attr = btp.gatts_get_attrs(start_handle=handle,
                                              end_handle=handle)
        if not chrc_value_attr:
            continue

        handle, perm, type_uuid = chrc_value_attr[0]
        chrc_value_data = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                                 btp.pts_addr_get(), handle)
        if not chrc_value_data:
            continue

        att_rsp, val_len, val = chrc_value_data

        # Check if returned ATT Insufficient Authorization error
        if att_rsp != 0x0c:
            continue

        return btp.btp2uuid(uuid_len, chrc_uuid)

    return '0000'
Example #21
0
def hdl_wid_112_timeout(desc):
    bd_addr = btp.pts_addr_get()
    bd_addr_type = btp.pts_addr_type_get()

    handle = btp.parse_handle_description(desc)
    if not handle:
        return False

    try:
        btp.gattc_read(bd_addr_type, bd_addr, handle)
        btp.gattc_read_rsp()
    except socket.timeout:
        pass
    return True
Example #22
0
def hdl_wid_76_no_rsp_check(desc):
    pattern = re.compile("'([0-9a-fA-F]+)'")
    params = pattern.findall(desc)
    if not params:
        logging.error("parsing error")
        return False

    handle = params[0]
    off = int(params[1])

    btp.gattc_write_long(btp.pts_addr_type_get(), btp.pts_addr_get(), handle,
                         off, '12', None)

    return True
Example #23
0
def hdl_wid_70(desc):
    pattern = re.compile("'([0-9a-fA-F]+)'")
    params = pattern.findall(desc)
    if not params:
        logging.error("parsing error")
        return False

    handle = params[0]
    size = int(params[1])

    btp.gattc_write_without_rsp(btp.pts_addr_type_get(), btp.pts_addr_get(),
                                handle, '12', size)

    return True
Example #24
0
def hdl_wid_112(desc):
    bd_addr = btp.pts_addr_get()
    bd_addr_type = btp.pts_addr_type_get()

    btp.gattc_disc_all_chrc(bd_addr_type, bd_addr, 0x0001, 0xffff)
    attrs = btp.gattc_disc_all_chrc_rsp()

    for attr in attrs:
        if attr.prop & Prop.read:
            btp.gattc_read(bd_addr_type, bd_addr, attr.value_handle)
            btp.gattc_read_rsp()
            return True

    return False
Example #25
0
def hdl_wid_112(desc):
    # Lookup characteristic handle that requires read authorization
    chrcs = btp.gatts_get_attrs(type_uuid='2803')
    for chrc in chrcs:
        handle, perm, type_uuid = chrc

        chrc_val = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                          btp.pts_addr_get(), handle)
        if not chrc_val:
            continue

        (att_rsp, val_len, val) = chrc_val

        hdr = '<BH'
        hdr_len = struct.calcsize(hdr)
        uuid_len = val_len - hdr_len

        prop, handle, chrc_uuid = struct.unpack("<BH%ds" % uuid_len, val)
        chrc_value_attr = btp.gatts_get_attrs(start_handle=handle,
                                              end_handle=handle)
        if not chrc_value_attr:
            continue

        handle, perm, type_uuid = chrc_value_attr[0]
        chrc_value_data = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                                 btp.pts_addr_get(), handle)
        if not chrc_value_data:
            continue

        # Check if returned ATT Insufficient Authorization error
        att_rsp, val_len, val = chrc_value_data
        if att_rsp != 8:
            continue

        return '{0:04x}'.format(handle, 'x')

    return '0000'
Example #26
0
def hdl_wid_48(params: WIDParams):
    MMI.reset()
    MMI.parse_description(params.description)

    hdl = MMI.args[0]

    if not hdl:
        logging.debug("parsing error")
        return False

    no_read_long_tests = [
        "GATT/CL/GAR/BI-01-C",
        "GATT/CL/GAR/BI-02-C",
        "GATT/CL/GAR/BI-03-C",
        "GATT/CL/GAR/BI-04-C",
        "GATT/CL/GAR/BI-05-C",
        "GATT/CL/GAR/BI-11-C",
        "GATT/CL/GAR/BI-35-C",
        "GATT/CL/GAR/BV-01-C",
    ]

    no_btp_reply_tests = [
        "GATT/CL/GAT/BV-01-C",
    ]

    if params.test_case_name in no_read_long_tests:
        btp.gattc_read(btp.pts_addr_type_get(), btp.pts_addr_get(), hdl)
        btp.gattc_read_rsp(False, True)
        return True

    if params.test_case_name in no_btp_reply_tests:
        btp.gattc_read(btp.pts_addr_type_get(), btp.pts_addr_get(), hdl)
        return True

    btp.gattc_read_long(btp.pts_addr_type_get(), btp.pts_addr_get(), hdl, 0, 1)
    btp.gattc_read_long_rsp(False, True)
    return True
Example #27
0
def hdl_wid_24(desc):
    MMI.reset()
    MMI.parse_description(desc)

    # Include service in description should have 3 parameters:
    # Attribute Handle, Included Service Attribute Handle and End Group Handle
    num_includes = len(MMI.args) / 3

    pts_services = []

    for i in range(num_includes):
        pts_services.append([
            int(MMI.args[i + 0], 16),
            int(MMI.args[i + 1], 16),
            int(MMI.args[i + 2], 16)
        ])

    iut_services = []

    # Get all Included services
    attrs = btp.gatts_get_attrs(type_uuid='2802')
    for attr in attrs:
        handle, perm, type_uuid = attr

        val = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                     btp.pts_addr_get(), handle)
        if not val:
            continue

        (_, val_len, attr_value) = val

        hdr = '<HH'
        hdr_len = struct.calcsize(hdr)
        data_len = val_len - hdr_len
        incl_hdl, end_hdl, _ = struct.unpack(hdr + '%ds' % data_len,
                                             attr_value)
        iut_services.append([handle, incl_hdl, end_hdl])

    # Verification
    for service in pts_services:
        if service in iut_services:
            iut_services.remove(service)
            logging.debug("Service %r found", service)
            continue
        else:
            logging.error("Service %r not found", service)
            return False

    return True
Example #28
0
def hdl_wid_58(desc):
    MMI.reset()
    MMI.parse_description(desc)

    hdl = MMI.args[0]

    if not hdl:
        logging.error("parsing error")
        return False

    btp.gattc_read(btp.pts_addr_type_get(), btp.pts_addr_get(), hdl)

    btp.gattc_read_rsp(True, True)

    return True
def hdl_wid_52(desc):
    MMI.reset()
    MMI.parse_description(desc)

    handle = int(MMI.args[0], 16)

    _, _, value = btp.gatts_get_attr_val(btp.pts_addr_type_get(),
                                         btp.pts_addr_get(), handle)

    value_read = hexlify(value)

    if value_read == MMI.args[1]:
        return True
    else:
        return False
Example #30
0
def hdl_wid_20(desc):
    MMI.reset()
    MMI.parse_description(desc)

    uuid = MMI.args[0]

    if not uuid:
        logging.error("%s parsing error", hdl_wid_20.__name__)
        return False

    btp.gattc_disc_prim_uuid(btp.pts_addr_type_get(), btp.pts_addr_get(), uuid)

    btp.gattc_disc_prim_uuid_rsp(True)

    return True