# Tests of LLC1 data transmission on subs-bound endpoints
# Also does a XID/TEST on subs-bound endpoint

# Open and bind two LLC1 streams
open 1
attach $1 43
bind $1 40 1 0 0 0

open 2
attach $2 43
bind $2 48 1 0 0 0

subs_bind $2 0:0:0:0:0:0,50 2

# Send data from 1 to 2 on one sap
send_udata $1 0:0:0:0:0:0,48 '1 2 3 4 5'

poll $1 $2 $2

recv_udata $2 '1 2 3 4 5'

# Send data from 1 to 2 on other sap
send_udata $1 0:0:0:0:0:0,50 '1 2 3 4 5'

poll $1 $2 $2

recv_udata $2 '1 2 3 4 5'

# Send data from 1 to 2 on invalid sap
send_udata $1 0:0:0:0:0:0,52 '1 2 3 4 5'

sleep 1

poll $1 $2 0

# subs-bind to match some data
subs_bind $2 0:0:0:0:0:0,50,20,30 2

# Send data from 1 to 2 on inactive sap
send_udata $1 0:0:0:0:0:0,50 '1 2 3 4 5'

sleep 1

poll $1 $2 0

# Send data from 1 to 2 with incorrect match
send_udata $1 0:0:0:0:0:0,50 '20 29 3 4 5'

sleep 1

poll $1 $2 0

# Send data from 1 to 2 with too-short match
send_udata $1 0:0:0:0:0:0,50 '20'

sleep 1

poll $1 $2 0

# Send data from 1 to 2 on sap with matching data
send_udata $1 0:0:0:0:0:0,50 '20 30 3 4 5'

poll $1 $2 $2

recv_udata $2 '20 30 3 4 5'

# Send data from 2 to 2 on sap with matching data
send_udata $2 0:0:0:0:0:0,50 '20 30 3 4 5 6 7 8 9 10'

poll $1 $2 $2

recv_udata $2 '20 30 3 4 5 6 7 8 9 10'

# subs-bind to match some data on 1
subs_bind $1 0:0:0:0:0:0,50,20,30,3 2

# Send data from 2 to 1 on sap with matching data
send_udata $2 0:0:0:0:0:0,50 '20 30 3 4 5 6 7 8 9 10'

poll $1 $2 $1

recv_udata $1 '20 30 3 4 5 6 7 8 9 10'

# subs-unbind 
subs_unbind $1 0:0:0:0:0:0,50,20,30,3

# Send data from 1 to 2 on sap with matching data
send_udata $1 0:0:0:0:0:0,50 '20 30 3 4 5 6 7 8 9 10'

poll $1 $2 $2

recv_udata $2 '20 30 3 4 5 6 7 8 9 10'

# subs-bind to take over some of 1's area
subs_bind $2 0:0:0:0:0:0,40,01 2

# Send data from 1 to 2 on sap with matching data
send_udata $1 0:0:0:0:0:0,40 '1 2 3 4 5 6 7 8 9 10'

poll $1 $2 $2

recv_udata $2 '1 2 3 4 5 6 7 8 9 10'

# Send data from 1 to 1 on sap with matching data
send_udata $1 0:0:0:0:0:0,40 '2 2 3 4 5 6 7 8 9 10'

poll $1 $2 $1

recv_udata $1 '2 2 3 4 5 6 7 8 9 10'

# unbind 2, causing its subs-bind to be deregistered
unbind $2

# Send data from 1 to 1 on sap which was previously subs-bind for by 2
send_udata $1 0:0:0:0:0:0,40 '1 2 3 4 5 6 7 8 9 10'

poll $1 $2 $1

recv_udata $1 '1 2 3 4 5 6 7 8 9 10'

# TEST command on subs-bound sap

open 3
attach $3 43
bind $3 58 1 0 0 0
subs_bind $3 0:0:0:0:0:0,68 2

open 4
attach $4 43
bind $4 50 1 0 0 0
subs_bind $4 0:0:0:0:0:0,60 2

test_req $4 0:0:0:0:0:0,68 0 0 ''

# Should see an event on the other stream (3)
poll $3 $4 $3

test_ind $3 0

test_res $3 0:0:0:0:0:0,60 0 ''

poll $3 $4 $4

test_con $4 0

poll $3 $4 0

check_state $3 dl_idle
check_state $4 dl_idle

close $3
close $4

# XID command on subs-bound sap

open 3
attach $3 43
bind $3 58 1 0 0 0
subs_bind $3 0:0:0:0:0:0,68 2

open 4
attach $4 43
bind $4 50 1 0 0 0
subs_bind $4 0:0:0:0:0:0,60 2

xid_req $4 0:0:0:0:0:0,68 0 0 ''

# Should see an event on the other stream (3)
poll $3 $4 $3

xid_ind $3 0

xid_res $3 0:0:0:0:0:0,60 0 ''

poll $3 $4 $4

xid_con $4 0

poll $3 $4 0

check_state $3 dl_idle
check_state $4 dl_idle

close $3
close $4
