1#!/usr/bin/python3.4
2#
3#   Copyright 2017 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import sys
18import time
19
20from acts import asserts
21from acts.test_decorators import test_tracker_info
22from acts.test_utils.net import connectivity_const as cconsts
23from acts.test_utils.wifi.aware import aware_const as aconsts
24from acts.test_utils.wifi.aware import aware_test_utils as autils
25from acts.test_utils.wifi.aware.AwareBaseTest import AwareBaseTest
26from acts.test_utils.wifi.rtt import rtt_const as rconsts
27from acts.test_utils.wifi.rtt import rtt_test_utils as rutils
28from acts.test_utils.wifi.rtt.RttBaseTest import RttBaseTest
29
30
31class AwareDiscoveryWithRangingTest(AwareBaseTest, RttBaseTest):
32    """Set of tests for Wi-Fi Aware discovery configured with ranging (RTT)."""
33
34    SERVICE_NAME = "GoogleTestServiceRRRRR"
35
36    # Flag indicating whether the device has a limitation that does not allow it
37    # to execute Aware-based Ranging (whether direct or as part of discovery)
38    # whenever NDP is enabled.
39    RANGING_NDP_CONCURRENCY_LIMITATION = True
40
41    # Flag indicating whether the device has a limitation that does not allow it
42    # to execute Aware-based Ranging (whether direct or as part of discovery)
43    # for both Initiators and Responders. Only the first mode works.
44    RANGING_INITIATOR_RESPONDER_CONCURRENCY_LIMITATION = True
45
46    def setup_test(self):
47        """Manual setup here due to multiple inheritance: explicitly execute the
48    setup method from both parents."""
49        AwareBaseTest.setup_test(self)
50        RttBaseTest.setup_test(self)
51
52    def teardown_test(self):
53        """Manual teardown here due to multiple inheritance: explicitly execute the
54    teardown method from both parents."""
55        AwareBaseTest.teardown_test(self)
56        RttBaseTest.teardown_test(self)
57
58    #########################################################################
59
60    def run_discovery(self,
61                      p_config,
62                      s_config,
63                      expect_discovery,
64                      expect_range=False):
65        """Run discovery on the 2 input devices with the specified configurations.
66
67    Args:
68      p_config, s_config: Publisher and Subscriber discovery configuration.
69      expect_discovery: True or False indicating whether discovery is expected
70                        with the specified configurations.
71      expect_range: True if we expect distance results (i.e. ranging to happen).
72                    Only relevant if expect_discovery is True.
73    Returns:
74      p_dut, s_dut: Publisher/Subscribe DUT
75      p_disc_id, s_disc_id: Publisher/Subscribe discovery session ID
76    """
77        p_dut = self.android_devices[0]
78        p_dut.pretty_name = "Publisher"
79        s_dut = self.android_devices[1]
80        s_dut.pretty_name = "Subscriber"
81
82        # Publisher+Subscriber: attach and wait for confirmation
83        p_id = p_dut.droid.wifiAwareAttach(False)
84        autils.wait_for_event(p_dut, aconsts.EVENT_CB_ON_ATTACHED)
85        time.sleep(self.device_startup_offset)
86        s_id = s_dut.droid.wifiAwareAttach(False)
87        autils.wait_for_event(s_dut, aconsts.EVENT_CB_ON_ATTACHED)
88
89        # Publisher: start publish and wait for confirmation
90        p_disc_id = p_dut.droid.wifiAwarePublish(p_id, p_config)
91        autils.wait_for_event(p_dut, aconsts.SESSION_CB_ON_PUBLISH_STARTED)
92
93        # Subscriber: start subscribe and wait for confirmation
94        s_disc_id = s_dut.droid.wifiAwareSubscribe(s_id, s_config)
95        autils.wait_for_event(s_dut, aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED)
96
97        # Subscriber: wait or fail on service discovery
98        if expect_discovery:
99            event = autils.wait_for_event(
100                s_dut, aconsts.SESSION_CB_ON_SERVICE_DISCOVERED)
101            if expect_range:
102                asserts.assert_true(
103                    aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
104                    "Discovery with ranging expected!")
105            else:
106                asserts.assert_false(
107                    aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
108                    "Discovery with ranging NOT expected!")
109        else:
110            autils.fail_on_event(s_dut,
111                                 aconsts.SESSION_CB_ON_SERVICE_DISCOVERED)
112
113        # (single) sleep for timeout period and then verify that no further events
114        time.sleep(autils.EVENT_TIMEOUT)
115        autils.verify_no_more_events(p_dut, timeout=0)
116        autils.verify_no_more_events(s_dut, timeout=0)
117
118        return p_dut, s_dut, p_disc_id, s_disc_id
119
120    def run_discovery_update(self,
121                             p_dut,
122                             s_dut,
123                             p_disc_id,
124                             s_disc_id,
125                             p_config,
126                             s_config,
127                             expect_discovery,
128                             expect_range=False):
129        """Run discovery on the 2 input devices with the specified update
130    configurations. I.e. update the existing discovery sessions with the
131    configurations.
132
133    Args:
134      p_dut, s_dut: Publisher/Subscriber DUTs.
135      p_disc_id, s_disc_id: Publisher/Subscriber discovery session IDs.
136      p_config, s_config: Publisher and Subscriber discovery configuration.
137      expect_discovery: True or False indicating whether discovery is expected
138                        with the specified configurations.
139      expect_range: True if we expect distance results (i.e. ranging to happen).
140                    Only relevant if expect_discovery is True.
141    """
142
143        # try to perform reconfiguration at same time (and wait once for all
144        # confirmations)
145        if p_config is not None:
146            p_dut.droid.wifiAwareUpdatePublish(p_disc_id, p_config)
147        if s_config is not None:
148            s_dut.droid.wifiAwareUpdateSubscribe(s_disc_id, s_config)
149
150        if p_config is not None:
151            autils.wait_for_event(p_dut,
152                                  aconsts.SESSION_CB_ON_SESSION_CONFIG_UPDATED)
153        if s_config is not None:
154            autils.wait_for_event(s_dut,
155                                  aconsts.SESSION_CB_ON_SESSION_CONFIG_UPDATED)
156
157        # Subscriber: wait or fail on service discovery
158        if expect_discovery:
159            event = autils.wait_for_event(
160                s_dut, aconsts.SESSION_CB_ON_SERVICE_DISCOVERED)
161            if expect_range:
162                asserts.assert_true(
163                    aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
164                    "Discovery with ranging expected!")
165            else:
166                asserts.assert_false(
167                    aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
168                    "Discovery with ranging NOT expected!")
169        else:
170            autils.fail_on_event(s_dut,
171                                 aconsts.SESSION_CB_ON_SERVICE_DISCOVERED)
172
173        # (single) sleep for timeout period and then verify that no further events
174        time.sleep(autils.EVENT_TIMEOUT)
175        autils.verify_no_more_events(p_dut, timeout=0)
176        autils.verify_no_more_events(s_dut, timeout=0)
177
178    def run_discovery_prange_sminmax_outofrange(self, is_unsolicited_passive):
179        """Run discovery with ranging:
180    - Publisher enables ranging
181    - Subscriber enables ranging with min/max such that out of range (min=large,
182      max=large+1)
183
184    Expected: no discovery
185
186    This is a baseline test for the update-configuration tests.
187
188    Args:
189      is_unsolicited_passive: True for Unsolicited/Passive, False for
190                              Solicited/Active.
191    Returns: the return arguments of the run_discovery.
192    """
193        pub_type = (aconsts.PUBLISH_TYPE_UNSOLICITED if is_unsolicited_passive
194                    else aconsts.PUBLISH_TYPE_SOLICITED)
195        sub_type = (aconsts.SUBSCRIBE_TYPE_PASSIVE if is_unsolicited_passive
196                    else aconsts.SUBSCRIBE_TYPE_ACTIVE)
197        return self.run_discovery(
198            p_config=autils.add_ranging_to_pub(
199                autils.create_discovery_config(
200                    self.SERVICE_NAME, pub_type, ssi=self.getname(2)),
201                enable_ranging=True),
202            s_config=autils.add_ranging_to_sub(
203                autils.create_discovery_config(
204                    self.SERVICE_NAME, sub_type, ssi=self.getname(2)),
205                min_distance_mm=1000000,
206                max_distance_mm=1000001),
207            expect_discovery=False)
208
209    def getname(self, level=1):
210        """Python magic to return the name of the *calling* function.
211
212    Args:
213      level: How many levels up to go for the method name. Default = calling
214             method.
215    """
216        return sys._getframe(level).f_code.co_name
217
218    #########################################################################
219    # Run discovery with ranging configuration.
220    #
221    # Names: test_ranged_discovery_<ptype>_<stype>_<p_range>_<s_range>_<ref_dist>
222    #
223    # where:
224    # <ptype>_<stype>: unsolicited_passive or solicited_active
225    # <p_range>: prange or pnorange
226    # <s_range>: smin or smax or sminmax or snorange
227    # <ref_distance>: inrange or outoforange
228    #########################################################################
229
230    @test_tracker_info(uuid="3a216e9a-7a57-4741-89c0-84456975e1ac")
231    def test_ranged_discovery_unsolicited_passive_prange_snorange(self):
232        """Verify discovery with ranging:
233    - Unsolicited Publish/Passive Subscribe
234    - Publisher enables ranging
235    - Subscriber disables ranging
236
237    Expect: normal discovery (as if no ranging performed) - no distance
238    """
239        self.run_discovery(
240            p_config=autils.add_ranging_to_pub(
241                autils.create_discovery_config(
242                    self.SERVICE_NAME,
243                    aconsts.PUBLISH_TYPE_UNSOLICITED,
244                    ssi=self.getname()),
245                enable_ranging=True),
246            s_config=autils.create_discovery_config(
247                self.SERVICE_NAME,
248                aconsts.SUBSCRIBE_TYPE_PASSIVE,
249                ssi=self.getname()),
250            expect_discovery=True,
251            expect_range=False)
252
253    @test_tracker_info(uuid="859a321e-18e2-437b-aa7a-2a45a42ee737")
254    def test_ranged_discovery_solicited_active_prange_snorange(self):
255        """Verify discovery with ranging:
256    - Solicited Publish/Active Subscribe
257    - Publisher enables ranging
258    - Subscriber disables ranging
259
260    Expect: normal discovery (as if no ranging performed) - no distance
261    """
262        self.run_discovery(
263            p_config=autils.add_ranging_to_pub(
264                autils.create_discovery_config(
265                    self.SERVICE_NAME,
266                    aconsts.PUBLISH_TYPE_SOLICITED,
267                    ssi=self.getname()),
268                enable_ranging=True),
269            s_config=autils.create_discovery_config(
270                self.SERVICE_NAME,
271                aconsts.SUBSCRIBE_TYPE_ACTIVE,
272                ssi=self.getname()),
273            expect_discovery=True,
274            expect_range=False)
275
276    @test_tracker_info(uuid="12a4f899-4f70-4641-8f3c-351004669b71")
277    def test_ranged_discovery_unsolicited_passive_pnorange_smax_inrange(self):
278        """Verify discovery with ranging:
279    - Unsolicited Publish/Passive Subscribe
280    - Publisher disables ranging
281    - Subscriber enables ranging with max such that always within range (large
282      max)
283
284    Expect: normal discovery (as if no ranging performed) - no distance
285    """
286        self.run_discovery(
287            p_config=autils.add_ranging_to_pub(
288                autils.create_discovery_config(
289                    self.SERVICE_NAME,
290                    aconsts.PUBLISH_TYPE_UNSOLICITED,
291                    ssi=self.getname()),
292                enable_ranging=False),
293            s_config=autils.add_ranging_to_sub(
294                autils.create_discovery_config(
295                    self.SERVICE_NAME,
296                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
297                    ssi=self.getname()),
298                min_distance_mm=None,
299                max_distance_mm=1000000),
300            expect_discovery=True,
301            expect_range=False)
302
303    @test_tracker_info(uuid="b7f90793-113d-4355-be20-856d92ac939f")
304    def test_ranged_discovery_solicited_active_pnorange_smax_inrange(self):
305        """Verify discovery with ranging:
306    - Solicited Publish/Active Subscribe
307    - Publisher disables ranging
308    - Subscriber enables ranging with max such that always within range (large
309      max)
310
311    Expect: normal discovery (as if no ranging performed) - no distance
312    """
313        self.run_discovery(
314            p_config=autils.add_ranging_to_pub(
315                autils.create_discovery_config(
316                    self.SERVICE_NAME,
317                    aconsts.PUBLISH_TYPE_SOLICITED,
318                    ssi=self.getname()),
319                enable_ranging=False),
320            s_config=autils.add_ranging_to_sub(
321                autils.create_discovery_config(
322                    self.SERVICE_NAME,
323                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
324                    ssi=self.getname()),
325                min_distance_mm=None,
326                max_distance_mm=1000000),
327            expect_discovery=True,
328            expect_range=False)
329
330    @test_tracker_info(uuid="da3ab6df-58f9-44ae-b7be-8200d9e1bb76")
331    def test_ranged_discovery_unsolicited_passive_pnorange_smin_outofrange(
332            self):
333        """Verify discovery with ranging:
334    - Unsolicited Publish/Passive Subscribe
335    - Publisher disables ranging
336    - Subscriber enables ranging with min such that always out of range (large
337      min)
338
339    Expect: normal discovery (as if no ranging performed) - no distance
340    """
341        self.run_discovery(
342            p_config=autils.add_ranging_to_pub(
343                autils.create_discovery_config(
344                    self.SERVICE_NAME,
345                    aconsts.PUBLISH_TYPE_UNSOLICITED,
346                    ssi=self.getname()),
347                enable_ranging=False),
348            s_config=autils.add_ranging_to_sub(
349                autils.create_discovery_config(
350                    self.SERVICE_NAME,
351                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
352                    ssi=self.getname()),
353                min_distance_mm=1000000,
354                max_distance_mm=None),
355            expect_discovery=True,
356            expect_range=False)
357
358    @test_tracker_info(uuid="275e0806-f266-4fa6-9ca0-1cfd7b65a6ca")
359    def test_ranged_discovery_solicited_active_pnorange_smin_outofrange(self):
360        """Verify discovery with ranging:
361    - Solicited Publish/Active Subscribe
362    - Publisher disables ranging
363    - Subscriber enables ranging with min such that always out of range (large
364      min)
365
366    Expect: normal discovery (as if no ranging performed) - no distance
367    """
368        self.run_discovery(
369            p_config=autils.add_ranging_to_pub(
370                autils.create_discovery_config(
371                    self.SERVICE_NAME,
372                    aconsts.PUBLISH_TYPE_SOLICITED,
373                    ssi=self.getname()),
374                enable_ranging=False),
375            s_config=autils.add_ranging_to_sub(
376                autils.create_discovery_config(
377                    self.SERVICE_NAME,
378                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
379                    ssi=self.getname()),
380                min_distance_mm=1000000,
381                max_distance_mm=None),
382            expect_discovery=True,
383            expect_range=False)
384
385    @test_tracker_info(uuid="8cd0aa1e-6866-4a5d-a550-f25483eebea1")
386    def test_ranged_discovery_unsolicited_passive_prange_smin_inrange(self):
387        """Verify discovery with ranging:
388    - Unsolicited Publish/Passive Subscribe
389    - Publisher enables ranging
390    - Subscriber enables ranging with min such that in range (min=0)
391
392    Expect: discovery with distance
393    """
394        self.run_discovery(
395            p_config=autils.add_ranging_to_pub(
396                autils.create_discovery_config(
397                    self.SERVICE_NAME,
398                    aconsts.PUBLISH_TYPE_UNSOLICITED,
399                    ssi=self.getname()),
400                enable_ranging=True),
401            s_config=autils.add_ranging_to_sub(
402                autils.create_discovery_config(
403                    self.SERVICE_NAME,
404                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
405                    ssi=self.getname()),
406                min_distance_mm=0,
407                max_distance_mm=None),
408            expect_discovery=True,
409            expect_range=True)
410
411    @test_tracker_info(uuid="97c22c54-669b-4f7a-bf51-2f484e5f3e74")
412    def test_ranged_discovery_unsolicited_passive_prange_smax_inrange(self):
413        """Verify discovery with ranging:
414    - Unsolicited Publish/Passive Subscribe
415    - Publisher enables ranging
416    - Subscriber enables ranging with max such that in range (max=large)
417
418    Expect: discovery with distance
419    """
420        self.run_discovery(
421            p_config=autils.add_ranging_to_pub(
422                autils.create_discovery_config(
423                    self.SERVICE_NAME,
424                    aconsts.PUBLISH_TYPE_UNSOLICITED,
425                    ssi=self.getname()),
426                enable_ranging=True),
427            s_config=autils.add_ranging_to_sub(
428                autils.create_discovery_config(
429                    self.SERVICE_NAME,
430                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
431                    ssi=self.getname()),
432                min_distance_mm=None,
433                max_distance_mm=1000000),
434            expect_discovery=True,
435            expect_range=True)
436
437    @test_tracker_info(uuid="616673d7-9d0b-43de-a378-e5e949b51b32")
438    def test_ranged_discovery_unsolicited_passive_prange_sminmax_inrange(self):
439        """Verify discovery with ranging:
440    - Unsolicited Publish/Passive Subscribe
441    - Publisher enables ranging
442    - Subscriber enables ranging with min/max such that in range (min=0,
443      max=large)
444
445    Expect: discovery with distance
446    """
447        self.run_discovery(
448            p_config=autils.add_ranging_to_pub(
449                autils.create_discovery_config(
450                    self.SERVICE_NAME,
451                    aconsts.PUBLISH_TYPE_UNSOLICITED,
452                    ssi=self.getname()),
453                enable_ranging=True),
454            s_config=autils.add_ranging_to_sub(
455                autils.create_discovery_config(
456                    self.SERVICE_NAME,
457                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
458                    ssi=self.getname()),
459                min_distance_mm=0,
460                max_distance_mm=1000000),
461            expect_discovery=True,
462            expect_range=True)
463
464    @test_tracker_info(uuid="2bf84912-dcad-4a8f-971f-e445a07f05ce")
465    def test_ranged_discovery_solicited_active_prange_smin_inrange(self):
466        """Verify discovery with ranging:
467    - Solicited Publish/Active Subscribe
468    - Publisher enables ranging
469    - Subscriber enables ranging with min such that in range (min=0)
470
471    Expect: discovery with distance
472    """
473        self.run_discovery(
474            p_config=autils.add_ranging_to_pub(
475                autils.create_discovery_config(
476                    self.SERVICE_NAME,
477                    aconsts.PUBLISH_TYPE_SOLICITED,
478                    ssi=self.getname()),
479                enable_ranging=True),
480            s_config=autils.add_ranging_to_sub(
481                autils.create_discovery_config(
482                    self.SERVICE_NAME,
483                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
484                    ssi=self.getname()),
485                min_distance_mm=0,
486                max_distance_mm=None),
487            expect_discovery=True,
488            expect_range=True)
489
490    @test_tracker_info(uuid="5cfd7961-9665-4742-a1b5-2d1fc97f9795")
491    def test_ranged_discovery_solicited_active_prange_smax_inrange(self):
492        """Verify discovery with ranging:
493    - Solicited Publish/Active Subscribe
494    - Publisher enables ranging
495    - Subscriber enables ranging with max such that in range (max=large)
496
497    Expect: discovery with distance
498    """
499        self.run_discovery(
500            p_config=autils.add_ranging_to_pub(
501                autils.create_discovery_config(
502                    self.SERVICE_NAME,
503                    aconsts.PUBLISH_TYPE_SOLICITED,
504                    ssi=self.getname()),
505                enable_ranging=True),
506            s_config=autils.add_ranging_to_sub(
507                autils.create_discovery_config(
508                    self.SERVICE_NAME,
509                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
510                    ssi=self.getname()),
511                min_distance_mm=None,
512                max_distance_mm=1000000),
513            expect_discovery=True,
514            expect_range=True)
515
516    @test_tracker_info(uuid="5cf650ad-0b42-4b7d-9e05-d5f45fe0554d")
517    def test_ranged_discovery_solicited_active_prange_sminmax_inrange(self):
518        """Verify discovery with ranging:
519    - Solicited Publish/Active Subscribe
520    - Publisher enables ranging
521    - Subscriber enables ranging with min/max such that in range (min=0,
522      max=large)
523
524    Expect: discovery with distance
525    """
526        self.run_discovery(
527            p_config=autils.add_ranging_to_pub(
528                autils.create_discovery_config(
529                    self.SERVICE_NAME,
530                    aconsts.PUBLISH_TYPE_SOLICITED,
531                    ssi=self.getname()),
532                enable_ranging=True),
533            s_config=autils.add_ranging_to_sub(
534                autils.create_discovery_config(
535                    self.SERVICE_NAME,
536                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
537                    ssi=self.getname()),
538                min_distance_mm=0,
539                max_distance_mm=1000000),
540            expect_discovery=True,
541            expect_range=True)
542
543    @test_tracker_info(uuid="5277f418-ac35-43ce-9b30-3c895272898e")
544    def test_ranged_discovery_unsolicited_passive_prange_smin_outofrange(self):
545        """Verify discovery with ranging:
546    - Unsolicited Publish/Passive Subscribe
547    - Publisher enables ranging
548    - Subscriber enables ranging with min such that out of range (min=large)
549
550    Expect: no discovery
551    """
552        self.run_discovery(
553            p_config=autils.add_ranging_to_pub(
554                autils.create_discovery_config(
555                    self.SERVICE_NAME,
556                    aconsts.PUBLISH_TYPE_UNSOLICITED,
557                    ssi=self.getname()),
558                enable_ranging=True),
559            s_config=autils.add_ranging_to_sub(
560                autils.create_discovery_config(
561                    self.SERVICE_NAME,
562                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
563                    ssi=self.getname()),
564                min_distance_mm=1000000,
565                max_distance_mm=None),
566            expect_discovery=False)
567
568    @test_tracker_info(uuid="8a7e6ab1-acf4-41a7-a5fb-8c164d593b5f")
569    def test_ranged_discovery_unsolicited_passive_prange_smax_outofrange(self):
570        """Verify discovery with ranging:
571    - Unsolicited Publish/Passive Subscribe
572    - Publisher enables ranging
573    - Subscriber enables ranging with max such that in range (max=0)
574
575    Expect: no discovery
576    """
577        self.run_discovery(
578            p_config=autils.add_ranging_to_pub(
579                autils.create_discovery_config(
580                    self.SERVICE_NAME,
581                    aconsts.PUBLISH_TYPE_UNSOLICITED,
582                    ssi=self.getname()),
583                enable_ranging=True),
584            s_config=autils.add_ranging_to_sub(
585                autils.create_discovery_config(
586                    self.SERVICE_NAME,
587                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
588                    ssi=self.getname()),
589                min_distance_mm=None,
590                max_distance_mm=0),
591            expect_discovery=False)
592
593    @test_tracker_info(uuid="b744f5f9-2641-4373-bf86-3752e2f9aace")
594    def test_ranged_discovery_unsolicited_passive_prange_sminmax_outofrange(
595            self):
596        """Verify discovery with ranging:
597    - Unsolicited Publish/Passive Subscribe
598    - Publisher enables ranging
599    - Subscriber enables ranging with min/max such that out of range (min=large,
600      max=large+1)
601
602    Expect: no discovery
603    """
604        self.run_discovery(
605            p_config=autils.add_ranging_to_pub(
606                autils.create_discovery_config(
607                    self.SERVICE_NAME,
608                    aconsts.PUBLISH_TYPE_UNSOLICITED,
609                    ssi=self.getname()),
610                enable_ranging=True),
611            s_config=autils.add_ranging_to_sub(
612                autils.create_discovery_config(
613                    self.SERVICE_NAME,
614                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
615                    ssi=self.getname()),
616                min_distance_mm=1000000,
617                max_distance_mm=1000001),
618            expect_discovery=False)
619
620    @test_tracker_info(uuid="d2e94199-b2e6-4fa5-a347-24594883c801")
621    def test_ranged_discovery_solicited_active_prange_smin_outofrange(self):
622        """Verify discovery with ranging:
623    - Solicited Publish/Active Subscribe
624    - Publisher enables ranging
625    - Subscriber enables ranging with min such that out of range (min=large)
626
627    Expect: no discovery
628    """
629        self.run_discovery(
630            p_config=autils.add_ranging_to_pub(
631                autils.create_discovery_config(
632                    self.SERVICE_NAME,
633                    aconsts.PUBLISH_TYPE_SOLICITED,
634                    ssi=self.getname()),
635                enable_ranging=True),
636            s_config=autils.add_ranging_to_sub(
637                autils.create_discovery_config(
638                    self.SERVICE_NAME,
639                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
640                    ssi=self.getname()),
641                min_distance_mm=1000000,
642                max_distance_mm=None),
643            expect_discovery=False)
644
645    @test_tracker_info(uuid="a5619835-496a-4244-a428-f85cba3d4115")
646    def test_ranged_discovery_solicited_active_prange_smax_outofrange(self):
647        """Verify discovery with ranging:
648    - Solicited Publish/Active Subscribe
649    - Publisher enables ranging
650    - Subscriber enables ranging with max such that out of range (max=0)
651
652    Expect: no discovery
653    """
654        self.run_discovery(
655            p_config=autils.add_ranging_to_pub(
656                autils.create_discovery_config(
657                    self.SERVICE_NAME,
658                    aconsts.PUBLISH_TYPE_SOLICITED,
659                    ssi=self.getname()),
660                enable_ranging=True),
661            s_config=autils.add_ranging_to_sub(
662                autils.create_discovery_config(
663                    self.SERVICE_NAME,
664                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
665                    ssi=self.getname()),
666                min_distance_mm=None,
667                max_distance_mm=0),
668            expect_discovery=False)
669
670    @test_tracker_info(uuid="12ebd91f-a973-410b-8ee1-0bd86024b921")
671    def test_ranged_discovery_solicited_active_prange_sminmax_outofrange(self):
672        """Verify discovery with ranging:
673    - Solicited Publish/Active Subscribe
674    - Publisher enables ranging
675    - Subscriber enables ranging with min/max such that out of range (min=large,
676      max=large+1)
677
678    Expect: no discovery
679    """
680        self.run_discovery(
681            p_config=autils.add_ranging_to_pub(
682                autils.create_discovery_config(
683                    self.SERVICE_NAME,
684                    aconsts.PUBLISH_TYPE_SOLICITED,
685                    ssi=self.getname()),
686                enable_ranging=True),
687            s_config=autils.add_ranging_to_sub(
688                autils.create_discovery_config(
689                    self.SERVICE_NAME,
690                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
691                    ssi=self.getname()),
692                min_distance_mm=1000000,
693                max_distance_mm=1000001),
694            expect_discovery=False)
695
696    #########################################################################
697    # Run discovery with ranging configuration & update configurations after
698    # first run.
699    #
700    # Names: test_ranged_updated_discovery_<ptype>_<stype>_<scenario>
701    #
702    # where:
703    # <ptype>_<stype>: unsolicited_passive or solicited_active
704    # <scenario>: test scenario (details in name)
705    #########################################################################
706
707    @test_tracker_info(uuid="59442180-4a6c-428f-b926-86000e8339b4")
708    def test_ranged_updated_discovery_unsolicited_passive_oor_to_ir(self):
709        """Verify discovery with ranging operation with updated configuration:
710    - Unsolicited Publish/Passive Subscribe
711    - Publisher enables ranging
712    - Subscriber:
713      - Starts: Ranging enabled, min/max such that out of range (min=large,
714                max=large+1)
715      - Reconfigured to: Ranging enabled, min/max such that in range (min=0,
716                        max=large)
717
718    Expect: discovery + ranging after update
719    """
720        (p_dut, s_dut, p_disc_id,
721         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(True)
722        self.run_discovery_update(
723            p_dut,
724            s_dut,
725            p_disc_id,
726            s_disc_id,
727            p_config=None,  # no updates
728            s_config=autils.add_ranging_to_sub(
729                autils.create_discovery_config(
730                    self.SERVICE_NAME,
731                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
732                    ssi=self.getname()),
733                min_distance_mm=0,
734                max_distance_mm=1000000),
735            expect_discovery=True,
736            expect_range=True)
737
738    @test_tracker_info(uuid="60188508-104d-42d5-ac3a-3605093c45d7")
739    def test_ranged_updated_discovery_unsolicited_passive_pub_unrange(self):
740        """Verify discovery with ranging operation with updated configuration:
741    - Unsolicited Publish/Passive Subscribe
742    - Publisher enables ranging
743    - Subscriber: Ranging enabled, min/max such that out of range (min=large,
744                  max=large+1)
745    - Reconfigured to: Publisher disables ranging
746
747    Expect: discovery w/o ranging after update
748    """
749        (p_dut, s_dut, p_disc_id,
750         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(True)
751        self.run_discovery_update(
752            p_dut,
753            s_dut,
754            p_disc_id,
755            s_disc_id,
756            p_config=autils.create_discovery_config(
757                self.SERVICE_NAME,
758                aconsts.PUBLISH_TYPE_UNSOLICITED,
759                ssi=self.getname()),
760            s_config=None,  # no updates
761            expect_discovery=True,
762            expect_range=False)
763
764    @test_tracker_info(uuid="f96b434e-751d-4eb5-ae01-0c5c3a6fb4a2")
765    def test_ranged_updated_discovery_unsolicited_passive_sub_unrange(self):
766        """Verify discovery with ranging operation with updated configuration:
767    - Unsolicited Publish/Passive Subscribe
768    - Publisher enables ranging
769    - Subscriber:
770      - Starts: Ranging enabled, min/max such that out of range (min=large,
771                max=large+1)
772      - Reconfigured to: Ranging disabled
773
774    Expect: discovery w/o ranging after update
775    """
776        (p_dut, s_dut, p_disc_id,
777         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(True)
778        self.run_discovery_update(
779            p_dut,
780            s_dut,
781            p_disc_id,
782            s_disc_id,
783            p_config=None,  # no updates
784            s_config=autils.create_discovery_config(
785                self.SERVICE_NAME,
786                aconsts.SUBSCRIBE_TYPE_PASSIVE,
787                ssi=self.getname()),
788            expect_discovery=True,
789            expect_range=False)
790
791    @test_tracker_info(uuid="78970de8-9362-4647-931a-3513bcf58e80")
792    def test_ranged_updated_discovery_unsolicited_passive_sub_oor(self):
793        """Verify discovery with ranging operation with updated configuration:
794    - Unsolicited Publish/Passive Subscribe
795    - Publisher enables ranging
796    - Subscriber:
797      - Starts: Ranging enabled, min/max such that out of range (min=large,
798                max=large+1)
799      - Reconfigured to: different out-of-range setting
800
801    Expect: no discovery after update
802    """
803        (p_dut, s_dut, p_disc_id,
804         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(True)
805        self.run_discovery_update(
806            p_dut,
807            s_dut,
808            p_disc_id,
809            s_disc_id,
810            p_config=None,  # no updates
811            s_config=autils.add_ranging_to_sub(
812                autils.create_discovery_config(
813                    self.SERVICE_NAME,
814                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
815                    ssi=self.getname()),
816                min_distance_mm=100000,
817                max_distance_mm=100001),
818            expect_discovery=False)
819
820    @test_tracker_info(uuid="0841ad05-4899-4521-bd24-04a8e2e345ac")
821    def test_ranged_updated_discovery_unsolicited_passive_pub_same(self):
822        """Verify discovery with ranging operation with updated configuration:
823    - Unsolicited Publish/Passive Subscribe
824    - Publisher enables ranging
825    - Subscriber: Ranging enabled, min/max such that out of range (min=large,
826                  max=large+1)
827    - Reconfigured to: Publisher with same settings (ranging enabled)
828
829    Expect: no discovery after update
830    """
831        (p_dut, s_dut, p_disc_id,
832         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(True)
833        self.run_discovery_update(
834            p_dut,
835            s_dut,
836            p_disc_id,
837            s_disc_id,
838            p_config=autils.add_ranging_to_pub(
839                autils.create_discovery_config(
840                    self.SERVICE_NAME,
841                    aconsts.PUBLISH_TYPE_UNSOLICITED,
842                    ssi=self.getname()),
843                enable_ranging=True),
844            s_config=None,  # no updates
845            expect_discovery=False)
846
847    @test_tracker_info(uuid="ec6ca57b-f115-4516-813a-4572b930c8d3")
848    def test_ranged_updated_discovery_unsolicited_passive_multi_step(self):
849        """Verify discovery with ranging operation with updated configuration:
850    - Unsolicited Publish/Passive Subscribe
851    - Publisher enables ranging
852    - Subscriber: Ranging enabled, min/max such that out of range (min=large,
853                  max=large+1)
854      - Expect: no discovery
855    - Reconfigured to: Ranging enabled, min/max such that in-range (min=0)
856      - Expect: discovery with ranging
857    - Reconfigured to: Ranging enabled, min/max such that out-of-range
858                       (min=large)
859      - Expect: no discovery
860    - Reconfigured to: Ranging disabled
861      - Expect: discovery without ranging
862    """
863        (p_dut, s_dut, p_disc_id,
864         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(True)
865        self.run_discovery_update(
866            p_dut,
867            s_dut,
868            p_disc_id,
869            s_disc_id,
870            p_config=None,  # no updates
871            s_config=autils.add_ranging_to_sub(
872                autils.create_discovery_config(
873                    self.SERVICE_NAME,
874                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
875                    ssi=self.getname()),
876                min_distance_mm=0,
877                max_distance_mm=None),
878            expect_discovery=True,
879            expect_range=True)
880        self.run_discovery_update(
881            p_dut,
882            s_dut,
883            p_disc_id,
884            s_disc_id,
885            p_config=None,  # no updates
886            s_config=autils.add_ranging_to_sub(
887                autils.create_discovery_config(
888                    self.SERVICE_NAME,
889                    aconsts.SUBSCRIBE_TYPE_PASSIVE,
890                    ssi=self.getname()),
891                min_distance_mm=1000000,
892                max_distance_mm=None),
893            expect_discovery=False)
894        self.run_discovery_update(
895            p_dut,
896            s_dut,
897            p_disc_id,
898            s_disc_id,
899            p_config=None,  # no updates
900            s_config=autils.create_discovery_config(
901                self.SERVICE_NAME,
902                aconsts.SUBSCRIBE_TYPE_PASSIVE,
903                ssi=self.getname()),
904            expect_discovery=True,
905            expect_range=False)
906
907    @test_tracker_info(uuid="bbaac63b-000c-415f-bf19-0906f04031cd")
908    def test_ranged_updated_discovery_solicited_active_oor_to_ir(self):
909        """Verify discovery with ranging operation with updated configuration:
910    - Solicited Publish/Active Subscribe
911    - Publisher enables ranging
912    - Subscriber:
913      - Starts: Ranging enabled, min/max such that out of range (min=large,
914                max=large+1)
915      - Reconfigured to: Ranging enabled, min/max such that in range (min=0,
916                        max=large)
917
918    Expect: discovery + ranging after update
919    """
920        (p_dut, s_dut, p_disc_id,
921         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(False)
922        self.run_discovery_update(
923            p_dut,
924            s_dut,
925            p_disc_id,
926            s_disc_id,
927            p_config=None,  # no updates
928            s_config=autils.add_ranging_to_sub(
929                autils.create_discovery_config(
930                    self.SERVICE_NAME,
931                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
932                    ssi=self.getname()),
933                min_distance_mm=0,
934                max_distance_mm=1000000),
935            expect_discovery=True,
936            expect_range=True)
937
938    @test_tracker_info(uuid="c385b361-7955-4f34-9109-8d8ca81cb4cc")
939    def test_ranged_updated_discovery_solicited_active_pub_unrange(self):
940        """Verify discovery with ranging operation with updated configuration:
941    - Solicited Publish/Active Subscribe
942    - Publisher enables ranging
943    - Subscriber: Ranging enabled, min/max such that out of range (min=large,
944                  max=large+1)
945    - Reconfigured to: Publisher disables ranging
946
947    Expect: discovery w/o ranging after update
948    """
949        (p_dut, s_dut, p_disc_id,
950         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(False)
951        self.run_discovery_update(
952            p_dut,
953            s_dut,
954            p_disc_id,
955            s_disc_id,
956            p_config=autils.create_discovery_config(
957                self.SERVICE_NAME,
958                aconsts.PUBLISH_TYPE_SOLICITED,
959                ssi=self.getname()),
960            s_config=None,  # no updates
961            expect_discovery=True,
962            expect_range=False)
963
964    @test_tracker_info(uuid="ec5120ea-77ec-48c6-8820-48b82ad3dfd4")
965    def test_ranged_updated_discovery_solicited_active_sub_unrange(self):
966        """Verify discovery with ranging operation with updated configuration:
967    - Solicited Publish/Active Subscribe
968    - Publisher enables ranging
969    - Subscriber:
970      - Starts: Ranging enabled, min/max such that out of range (min=large,
971                max=large+1)
972      - Reconfigured to: Ranging disabled
973
974    Expect: discovery w/o ranging after update
975    """
976        (p_dut, s_dut, p_disc_id,
977         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(False)
978        self.run_discovery_update(
979            p_dut,
980            s_dut,
981            p_disc_id,
982            s_disc_id,
983            p_config=None,  # no updates
984            s_config=autils.create_discovery_config(
985                self.SERVICE_NAME,
986                aconsts.SUBSCRIBE_TYPE_ACTIVE,
987                ssi=self.getname()),
988            expect_discovery=True,
989            expect_range=False)
990
991    @test_tracker_info(uuid="6231cb42-91e4-48d3-b9db-b37efbe8537c")
992    def test_ranged_updated_discovery_solicited_active_sub_oor(self):
993        """Verify discovery with ranging operation with updated configuration:
994    - Solicited Publish/Active Subscribe
995    - Publisher enables ranging
996    - Subscriber:
997      - Starts: Ranging enabled, min/max such that out of range (min=large,
998                max=large+1)
999      - Reconfigured to: different out-of-range setting
1000
1001    Expect: no discovery after update
1002    """
1003        (p_dut, s_dut, p_disc_id,
1004         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(False)
1005        self.run_discovery_update(
1006            p_dut,
1007            s_dut,
1008            p_disc_id,
1009            s_disc_id,
1010            p_config=None,  # no updates
1011            s_config=autils.add_ranging_to_sub(
1012                autils.create_discovery_config(
1013                    self.SERVICE_NAME,
1014                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
1015                    ssi=self.getname()),
1016                min_distance_mm=100000,
1017                max_distance_mm=100001),
1018            expect_discovery=False)
1019
1020    @test_tracker_info(uuid="ec999420-6a50-455e-b624-f4c9b4cb7ea5")
1021    def test_ranged_updated_discovery_solicited_active_pub_same(self):
1022        """Verify discovery with ranging operation with updated configuration:
1023    - Solicited Publish/Active Subscribe
1024    - Publisher enables ranging
1025    - Subscriber: Ranging enabled, min/max such that out of range (min=large,
1026                  max=large+1)
1027    - Reconfigured to: Publisher with same settings (ranging enabled)
1028
1029    Expect: no discovery after update
1030    """
1031        (p_dut, s_dut, p_disc_id,
1032         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(False)
1033        self.run_discovery_update(
1034            p_dut,
1035            s_dut,
1036            p_disc_id,
1037            s_disc_id,
1038            p_config=autils.add_ranging_to_pub(
1039                autils.create_discovery_config(
1040                    self.SERVICE_NAME,
1041                    aconsts.PUBLISH_TYPE_SOLICITED,
1042                    ssi=self.getname()),
1043                enable_ranging=True),
1044            s_config=None,  # no updates
1045            expect_discovery=False)
1046
1047    @test_tracker_info(uuid="ec6ca57b-f115-4516-813a-4572b930c8d3")
1048    def test_ranged_updated_discovery_solicited_active_multi_step(self):
1049        """Verify discovery with ranging operation with updated configuration:
1050    - Unsolicited Publish/Passive Subscribe
1051    - Publisher enables ranging
1052    - Subscriber: Ranging enabled, min/max such that out of range (min=large,
1053                  max=large+1)
1054      - Expect: no discovery
1055    - Reconfigured to: Ranging enabled, min/max such that in-range (min=0)
1056      - Expect: discovery with ranging
1057    - Reconfigured to: Ranging enabled, min/max such that out-of-range
1058                       (min=large)
1059      - Expect: no discovery
1060    - Reconfigured to: Ranging disabled
1061      - Expect: discovery without ranging
1062    """
1063        (p_dut, s_dut, p_disc_id,
1064         s_disc_id) = self.run_discovery_prange_sminmax_outofrange(True)
1065        self.run_discovery_update(
1066            p_dut,
1067            s_dut,
1068            p_disc_id,
1069            s_disc_id,
1070            p_config=None,  # no updates
1071            s_config=autils.add_ranging_to_sub(
1072                autils.create_discovery_config(
1073                    self.SERVICE_NAME,
1074                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
1075                    ssi=self.getname()),
1076                min_distance_mm=0,
1077                max_distance_mm=None),
1078            expect_discovery=True,
1079            expect_range=True)
1080        self.run_discovery_update(
1081            p_dut,
1082            s_dut,
1083            p_disc_id,
1084            s_disc_id,
1085            p_config=None,  # no updates
1086            s_config=autils.add_ranging_to_sub(
1087                autils.create_discovery_config(
1088                    self.SERVICE_NAME,
1089                    aconsts.SUBSCRIBE_TYPE_ACTIVE,
1090                    ssi=self.getname()),
1091                min_distance_mm=1000000,
1092                max_distance_mm=None),
1093            expect_discovery=False)
1094        self.run_discovery_update(
1095            p_dut,
1096            s_dut,
1097            p_disc_id,
1098            s_disc_id,
1099            p_config=None,  # no updates
1100            s_config=autils.create_discovery_config(
1101                self.SERVICE_NAME,
1102                aconsts.SUBSCRIBE_TYPE_ACTIVE,
1103                ssi=self.getname()),
1104            expect_discovery=True,
1105            expect_range=False)
1106
1107    #########################################################################
1108
1109    @test_tracker_info(uuid="6edc47ab-7300-4bff-b7dd-5de83f58928a")
1110    def test_ranged_discovery_multi_session(self):
1111        """Verify behavior with multiple concurrent discovery session with different
1112    configurations:
1113
1114    Device A (Publisher):
1115      Publisher AA: ranging enabled
1116      Publisher BB: ranging enabled
1117      Publisher CC: ranging enabled
1118      Publisher DD: ranging disabled
1119    Device B (Subscriber):
1120      Subscriber AA: ranging out-of-range -> no match
1121      Subscriber BB: ranging in-range -> match w/range
1122      Subscriber CC: ranging disabled -> match w/o range
1123      Subscriber DD: ranging out-of-range -> match w/o range
1124    """
1125        p_dut = self.android_devices[0]
1126        p_dut.pretty_name = "Publisher"
1127        s_dut = self.android_devices[1]
1128        s_dut.pretty_name = "Subscriber"
1129
1130        # Publisher+Subscriber: attach and wait for confirmation
1131        p_id = p_dut.droid.wifiAwareAttach(False)
1132        autils.wait_for_event(p_dut, aconsts.EVENT_CB_ON_ATTACHED)
1133        time.sleep(self.device_startup_offset)
1134        s_id = s_dut.droid.wifiAwareAttach(False)
1135        autils.wait_for_event(s_dut, aconsts.EVENT_CB_ON_ATTACHED)
1136
1137        # Subscriber: start sessions
1138        aa_s_disc_id = s_dut.droid.wifiAwareSubscribe(
1139            s_id,
1140            autils.add_ranging_to_sub(
1141                autils.create_discovery_config("AA",
1142                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1143                min_distance_mm=1000000,
1144                max_distance_mm=1000001), True)
1145        bb_s_disc_id = s_dut.droid.wifiAwareSubscribe(
1146            s_id,
1147            autils.add_ranging_to_sub(
1148                autils.create_discovery_config("BB",
1149                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1150                min_distance_mm=0,
1151                max_distance_mm=1000000), True)
1152        cc_s_disc_id = s_dut.droid.wifiAwareSubscribe(
1153            s_id,
1154            autils.create_discovery_config(
1155                "CC", aconsts.SUBSCRIBE_TYPE_PASSIVE), True)
1156        dd_s_disc_id = s_dut.droid.wifiAwareSubscribe(
1157            s_id,
1158            autils.add_ranging_to_sub(
1159                autils.create_discovery_config("DD",
1160                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1161                min_distance_mm=1000000,
1162                max_distance_mm=1000001), True)
1163
1164        autils.wait_for_event(
1165            s_dut,
1166            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1167                                  aa_s_disc_id))
1168        autils.wait_for_event(
1169            s_dut,
1170            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1171                                  bb_s_disc_id))
1172        autils.wait_for_event(
1173            s_dut,
1174            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1175                                  cc_s_disc_id))
1176        autils.wait_for_event(
1177            s_dut,
1178            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1179                                  dd_s_disc_id))
1180
1181        # Publisher: start sessions
1182        aa_p_disc_id = p_dut.droid.wifiAwarePublish(
1183            p_id,
1184            autils.add_ranging_to_pub(
1185                autils.create_discovery_config(
1186                    "AA", aconsts.PUBLISH_TYPE_UNSOLICITED),
1187                enable_ranging=True), True)
1188        bb_p_disc_id = p_dut.droid.wifiAwarePublish(
1189            p_id,
1190            autils.add_ranging_to_pub(
1191                autils.create_discovery_config(
1192                    "BB", aconsts.PUBLISH_TYPE_UNSOLICITED),
1193                enable_ranging=True), True)
1194        cc_p_disc_id = p_dut.droid.wifiAwarePublish(
1195            p_id,
1196            autils.add_ranging_to_pub(
1197                autils.create_discovery_config(
1198                    "CC", aconsts.PUBLISH_TYPE_UNSOLICITED),
1199                enable_ranging=True), True)
1200        dd_p_disc_id = p_dut.droid.wifiAwarePublish(
1201            p_id,
1202            autils.create_discovery_config(
1203                "DD", aconsts.PUBLISH_TYPE_UNSOLICITED), True)
1204
1205        autils.wait_for_event(
1206            p_dut,
1207            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1208                                  aa_p_disc_id))
1209        autils.wait_for_event(
1210            p_dut,
1211            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1212                                  bb_p_disc_id))
1213        autils.wait_for_event(
1214            p_dut,
1215            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1216                                  cc_p_disc_id))
1217        autils.wait_for_event(
1218            p_dut,
1219            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1220                                  dd_p_disc_id))
1221
1222        # Expected and unexpected service discovery
1223        event = autils.wait_for_event(
1224            s_dut,
1225            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1226                                  bb_s_disc_id))
1227        asserts.assert_true(
1228            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1229            "Discovery with ranging for BB expected!")
1230        event = autils.wait_for_event(
1231            s_dut,
1232            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1233                                  cc_s_disc_id))
1234        asserts.assert_false(
1235            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1236            "Discovery with ranging for CC NOT expected!")
1237        event = autils.wait_for_event(
1238            s_dut,
1239            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1240                                  dd_s_disc_id))
1241        asserts.assert_false(
1242            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1243            "Discovery with ranging for DD NOT expected!")
1244        autils.fail_on_event(
1245            s_dut,
1246            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1247                                  aa_s_disc_id))
1248
1249        # (single) sleep for timeout period and then verify that no further events
1250        time.sleep(autils.EVENT_TIMEOUT)
1251        autils.verify_no_more_events(p_dut, timeout=0)
1252        autils.verify_no_more_events(s_dut, timeout=0)
1253
1254    #########################################################################
1255
1256    @test_tracker_info(uuid="deede47f-a54c-46d9-88bb-f4482fbd8470")
1257    def test_ndp_concurrency(self):
1258        """Verify the behavior of Wi-Fi Aware Ranging whenever an NDP is created -
1259    for those devices that have a concurrency limitation that does not allow
1260    Aware Ranging, whether direct or as part of discovery.
1261
1262    Publisher: start 3 services
1263      AA w/o ranging
1264      BB w/ ranging
1265      CC w/ ranging
1266      DD w/ ranging
1267    Subscriber: start 2 services
1268      AA w/o ranging
1269      BB w/ ranging out-of-range
1270      (do not start CC!)
1271      DD w/ ranging in-range
1272    Expect AA discovery, DD discovery w/range, but no BB
1273    Start NDP in context of AA
1274    IF NDP_CONCURRENCY_LIMITATION:
1275      Verify discovery on BB w/o range
1276    Start EE w/ranging out-of-range
1277    Start FF w/ranging in-range
1278    IF NDP_CONCURRENCY_LIMITATION:
1279      Verify discovery on EE w/o range
1280      Verify discovery on FF w/o range
1281    Else:
1282      Verify discovery on FF w/ range
1283    Tear down NDP
1284    Subscriber
1285      Start CC w/ ranging out-of-range
1286      Wait to verify that do not get match
1287      Update configuration to be in-range
1288      Verify that get match with ranging information
1289    """
1290        p_dut = self.android_devices[0]
1291        p_dut.pretty_name = "Publisher"
1292        s_dut = self.android_devices[1]
1293        s_dut.pretty_name = "Subscriber"
1294
1295        # Publisher+Subscriber: attach and wait for confirmation
1296        p_id = p_dut.droid.wifiAwareAttach(False)
1297        autils.wait_for_event(p_dut, aconsts.EVENT_CB_ON_ATTACHED)
1298        time.sleep(self.device_startup_offset)
1299        s_id = s_dut.droid.wifiAwareAttach(False)
1300        autils.wait_for_event(s_dut, aconsts.EVENT_CB_ON_ATTACHED)
1301
1302        # Publisher: AA w/o ranging, BB w/ ranging, CC w/ ranging, DD w/ ranging
1303        aa_p_id = p_dut.droid.wifiAwarePublish(
1304            p_id,
1305            autils.create_discovery_config(
1306                "AA", aconsts.PUBLISH_TYPE_SOLICITED), True)
1307        autils.wait_for_event(
1308            p_dut,
1309            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1310                                  aa_p_id))
1311        bb_p_id = p_dut.droid.wifiAwarePublish(
1312            p_id,
1313            autils.add_ranging_to_pub(
1314                autils.create_discovery_config(
1315                    "BB", aconsts.PUBLISH_TYPE_UNSOLICITED),
1316                enable_ranging=True), True)
1317        autils.wait_for_event(
1318            p_dut,
1319            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1320                                  bb_p_id))
1321        cc_p_id = p_dut.droid.wifiAwarePublish(
1322            p_id,
1323            autils.add_ranging_to_pub(
1324                autils.create_discovery_config(
1325                    "CC", aconsts.PUBLISH_TYPE_UNSOLICITED),
1326                enable_ranging=True), True)
1327        autils.wait_for_event(
1328            p_dut,
1329            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1330                                  cc_p_id))
1331        dd_p_id = p_dut.droid.wifiAwarePublish(
1332            p_id,
1333            autils.add_ranging_to_pub(
1334                autils.create_discovery_config(
1335                    "DD", aconsts.PUBLISH_TYPE_UNSOLICITED),
1336                enable_ranging=True), True)
1337        autils.wait_for_event(
1338            p_dut,
1339            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1340                                  dd_p_id))
1341
1342        # Subscriber: AA w/o ranging, BB w/ranging out-of-range,
1343        #             DD w /ranging in-range
1344        aa_s_id = s_dut.droid.wifiAwareSubscribe(
1345            s_id,
1346            autils.create_discovery_config(
1347                "AA", aconsts.SUBSCRIBE_TYPE_ACTIVE), True)
1348        autils.wait_for_event(
1349            s_dut,
1350            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1351                                  aa_s_id))
1352        bb_s_id = s_dut.droid.wifiAwareSubscribe(
1353            s_id,
1354            autils.add_ranging_to_sub(
1355                autils.create_discovery_config("BB",
1356                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1357                min_distance_mm=1000000,
1358                max_distance_mm=1000001), True)
1359        autils.wait_for_event(
1360            s_dut,
1361            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1362                                  bb_s_id))
1363        dd_s_id = s_dut.droid.wifiAwareSubscribe(
1364            s_id,
1365            autils.add_ranging_to_sub(
1366                autils.create_discovery_config("DD",
1367                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1368                min_distance_mm=None,
1369                max_distance_mm=1000000), True)
1370        autils.wait_for_event(
1371            s_dut,
1372            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1373                                  dd_s_id))
1374
1375        # verify: AA discovered, BB not discovered, DD discovery w/range
1376        event = autils.wait_for_event(
1377            s_dut,
1378            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1379                                  aa_s_id))
1380        asserts.assert_false(
1381            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1382            "Discovery with ranging for AA NOT expected!")
1383        aa_peer_id_on_sub = event['data'][aconsts.SESSION_CB_KEY_PEER_ID]
1384        autils.fail_on_event(
1385            s_dut,
1386            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1387                                  bb_s_id))
1388        event = autils.wait_for_event(
1389            s_dut,
1390            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1391                                  dd_s_id))
1392        asserts.assert_true(
1393            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1394            "Discovery with ranging for DD expected!")
1395
1396        # start NDP in context of AA:
1397
1398        # Publisher: request network (from ANY)
1399        p_req_key = autils.request_network(
1400            p_dut, p_dut.droid.wifiAwareCreateNetworkSpecifier(aa_p_id, None))
1401
1402        # Subscriber: request network
1403        s_req_key = autils.request_network(
1404            s_dut,
1405            s_dut.droid.wifiAwareCreateNetworkSpecifier(
1406                aa_s_id, aa_peer_id_on_sub))
1407
1408        # Publisher & Subscriber: wait for network formation
1409        p_net_event = autils.wait_for_event_with_keys(
1410            p_dut, cconsts.EVENT_NETWORK_CALLBACK, autils.EVENT_TIMEOUT,
1411            (cconsts.NETWORK_CB_KEY_EVENT,
1412             cconsts.NETWORK_CB_LINK_PROPERTIES_CHANGED),
1413            (cconsts.NETWORK_CB_KEY_ID, p_req_key))
1414        s_net_event = autils.wait_for_event_with_keys(
1415            s_dut, cconsts.EVENT_NETWORK_CALLBACK, autils.EVENT_TIMEOUT,
1416            (cconsts.NETWORK_CB_KEY_EVENT,
1417             cconsts.NETWORK_CB_LINK_PROPERTIES_CHANGED),
1418            (cconsts.NETWORK_CB_KEY_ID, s_req_key))
1419
1420        p_aware_if = p_net_event["data"][cconsts.NETWORK_CB_KEY_INTERFACE_NAME]
1421        s_aware_if = s_net_event["data"][cconsts.NETWORK_CB_KEY_INTERFACE_NAME]
1422
1423        p_ipv6 = p_dut.droid.connectivityGetLinkLocalIpv6Address(
1424            p_aware_if).split("%")[0]
1425        s_ipv6 = s_dut.droid.connectivityGetLinkLocalIpv6Address(
1426            s_aware_if).split("%")[0]
1427
1428        self.log.info("AA NDP Interface names: P=%s, S=%s", p_aware_if,
1429                      s_aware_if)
1430        self.log.info("AA NDP Interface addresses (IPv6): P=%s, S=%s", p_ipv6,
1431                      s_ipv6)
1432
1433        if self.RANGING_NDP_CONCURRENCY_LIMITATION:
1434            # Expect BB to now discover w/o ranging
1435            event = autils.wait_for_event(
1436                s_dut,
1437                autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1438                                      bb_s_id))
1439            asserts.assert_false(
1440                aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1441                "Discovery with ranging for BB NOT expected!")
1442
1443        # Publishers: EE, FF w/ ranging
1444        ee_p_id = p_dut.droid.wifiAwarePublish(
1445            p_id,
1446            autils.add_ranging_to_pub(
1447                autils.create_discovery_config("EE",
1448                                               aconsts.PUBLISH_TYPE_SOLICITED),
1449                enable_ranging=True), True)
1450        autils.wait_for_event(
1451            p_dut,
1452            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1453                                  ee_p_id))
1454        ff_p_id = p_dut.droid.wifiAwarePublish(
1455            p_id,
1456            autils.add_ranging_to_pub(
1457                autils.create_discovery_config(
1458                    "FF", aconsts.PUBLISH_TYPE_UNSOLICITED),
1459                enable_ranging=True), True)
1460        autils.wait_for_event(
1461            p_dut,
1462            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1463                                  ff_p_id))
1464
1465        # Subscribers: EE out-of-range, FF in-range
1466        ee_s_id = s_dut.droid.wifiAwareSubscribe(
1467            s_id,
1468            autils.add_ranging_to_sub(
1469                autils.create_discovery_config("EE",
1470                                               aconsts.SUBSCRIBE_TYPE_ACTIVE),
1471                min_distance_mm=1000000,
1472                max_distance_mm=1000001), True)
1473        autils.wait_for_event(
1474            s_dut,
1475            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1476                                  ee_s_id))
1477        ff_s_id = s_dut.droid.wifiAwareSubscribe(
1478            s_id,
1479            autils.add_ranging_to_sub(
1480                autils.create_discovery_config("FF",
1481                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1482                min_distance_mm=None,
1483                max_distance_mm=1000000), True)
1484        autils.wait_for_event(
1485            s_dut,
1486            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1487                                  ff_s_id))
1488
1489        if self.RANGING_NDP_CONCURRENCY_LIMITATION:
1490            # Expect EE & FF discovery w/o range
1491            event = autils.wait_for_event(
1492                s_dut,
1493                autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1494                                      ee_s_id))
1495            asserts.assert_false(
1496                aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1497                "Discovery with ranging for EE NOT expected!")
1498            event = autils.wait_for_event(
1499                s_dut,
1500                autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1501                                      ff_s_id))
1502            asserts.assert_false(
1503                aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1504                "Discovery with ranging for FF NOT expected!")
1505        else:
1506            event = autils.wait_for_event(
1507                s_dut,
1508                autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1509                                      ff_s_id))
1510            asserts.assert_true(
1511                aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1512                "Discovery with ranging for FF expected!")
1513
1514        # tear down NDP
1515        p_dut.droid.connectivityUnregisterNetworkCallback(p_req_key)
1516        s_dut.droid.connectivityUnregisterNetworkCallback(s_req_key)
1517
1518        time.sleep(5)  # give time for NDP termination to finish
1519
1520        # Subscriber: start CC out-of-range - no discovery expected!
1521        cc_s_id = s_dut.droid.wifiAwareSubscribe(
1522            s_id,
1523            autils.add_ranging_to_sub(
1524                autils.create_discovery_config("CC",
1525                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1526                min_distance_mm=1000000,
1527                max_distance_mm=1000001), True)
1528        autils.wait_for_event(
1529            s_dut,
1530            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1531                                  cc_s_id))
1532        autils.fail_on_event(
1533            s_dut,
1534            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1535                                  cc_s_id))
1536
1537        # Subscriber: modify CC to in-range - expect discovery w/ range
1538        s_dut.droid.wifiAwareUpdateSubscribe(
1539            cc_s_id,
1540            autils.add_ranging_to_sub(
1541                autils.create_discovery_config("CC",
1542                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1543                min_distance_mm=None,
1544                max_distance_mm=1000001))
1545        autils.wait_for_event(
1546            s_dut,
1547            autils.decorate_event(aconsts.SESSION_CB_ON_SESSION_CONFIG_UPDATED,
1548                                  cc_s_id))
1549        event = autils.wait_for_event(
1550            s_dut,
1551            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1552                                  cc_s_id))
1553        asserts.assert_true(
1554            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1555            "Discovery with ranging for CC expected!")
1556
1557    @test_tracker_info(uuid="d94dac91-4090-4c03-a867-6dfac6558ba3")
1558    def test_role_concurrency(self):
1559        """Verify the behavior of Wi-Fi Aware Ranging (in the context of discovery)
1560     when the device has concurrency limitations which do not permit concurrent
1561     Initiator and Responder roles on the same device. In such case it is
1562     expected that normal discovery without ranging is executed AND that ranging
1563     is restored whenever the concurrency constraints are removed.
1564
1565     Note: all Subscribers are in-range.
1566
1567     DUT1: start multiple services
1568      Publish AA w/ ranging (unsolicited)
1569      Subscribe BB w/ ranging (active)
1570      Publish CC w/ ranging (unsolicited)
1571      Publish DD w/o ranging (solicited)
1572      Subscribe EE w/ ranging (passive)
1573      Subscribe FF w/ ranging (active)
1574     DUT2: start multiple services
1575      Subscribe AA w/ ranging (passive)
1576      Publish BB w/ ranging (solicited)
1577      Subscribe DD w/o ranging (active)
1578     Expect
1579      DUT2: AA match w/ range information
1580      DUT1: BB match w/o range information (concurrency disables ranging)
1581      DUT2: DD match w/o range information
1582     DUT1: Terminate AA
1583     DUT2:
1584      Terminate AA
1585      Start Publish EE w/ ranging (unsolicited)
1586     DUT1: expect EE w/o ranging
1587     DUT1: Terminate CC
1588     DUT2: Start Publish FF w/ ranging (solicited)
1589     DUT1: expect FF w/ ranging information - should finally be back up
1590     """
1591        dut1 = self.android_devices[0]
1592        dut1.pretty_name = "DUT1"
1593        dut2 = self.android_devices[1]
1594        dut2.pretty_name = "DUT2"
1595
1596        # Publisher+Subscriber: attach and wait for confirmation
1597        dut1_id = dut1.droid.wifiAwareAttach(False)
1598        autils.wait_for_event(dut1, aconsts.EVENT_CB_ON_ATTACHED)
1599        time.sleep(self.device_startup_offset)
1600        dut2_id = dut2.droid.wifiAwareAttach(False)
1601        autils.wait_for_event(dut2, aconsts.EVENT_CB_ON_ATTACHED)
1602
1603        # DUT1: initial service bringup
1604        aa_p_id = dut1.droid.wifiAwarePublish(
1605            dut1_id,
1606            autils.add_ranging_to_pub(
1607                autils.create_discovery_config(
1608                    "AA", aconsts.PUBLISH_TYPE_UNSOLICITED),
1609                enable_ranging=True), True)
1610        autils.wait_for_event(
1611            dut1,
1612            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1613                                  aa_p_id))
1614        bb_s_id = dut1.droid.wifiAwareSubscribe(
1615            dut1_id,
1616            autils.add_ranging_to_sub(
1617                autils.create_discovery_config("BB",
1618                                               aconsts.SUBSCRIBE_TYPE_ACTIVE),
1619                min_distance_mm=None,
1620                max_distance_mm=1000000), True)
1621        autils.wait_for_event(
1622            dut1,
1623            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1624                                  bb_s_id))
1625        cc_p_id = dut1.droid.wifiAwarePublish(
1626            dut1_id,
1627            autils.add_ranging_to_pub(
1628                autils.create_discovery_config(
1629                    "CC", aconsts.PUBLISH_TYPE_UNSOLICITED),
1630                enable_ranging=True), True)
1631        autils.wait_for_event(
1632            dut1,
1633            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1634                                  cc_p_id))
1635        dd_p_id = dut1.droid.wifiAwarePublish(
1636            dut1_id,
1637            autils.create_discovery_config(
1638                "DD", aconsts.PUBLISH_TYPE_SOLICITED), True)
1639        autils.wait_for_event(
1640            dut1,
1641            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1642                                  dd_p_id))
1643        ee_s_id = dut1.droid.wifiAwareSubscribe(
1644            dut1_id,
1645            autils.add_ranging_to_sub(
1646                autils.create_discovery_config("EE",
1647                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1648                min_distance_mm=None,
1649                max_distance_mm=1000000), True)
1650        autils.wait_for_event(
1651            dut1,
1652            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1653                                  ee_s_id))
1654        ff_s_id = dut1.droid.wifiAwareSubscribe(
1655            dut1_id,
1656            autils.add_ranging_to_sub(
1657                autils.create_discovery_config("FF",
1658                                               aconsts.SUBSCRIBE_TYPE_ACTIVE),
1659                min_distance_mm=None,
1660                max_distance_mm=1000000), True)
1661        autils.wait_for_event(
1662            dut1,
1663            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1664                                  ff_s_id))
1665
1666        # DUT2: initial service bringup
1667        aa_s_id = dut2.droid.wifiAwareSubscribe(
1668            dut2_id,
1669            autils.add_ranging_to_sub(
1670                autils.create_discovery_config("AA",
1671                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1672                min_distance_mm=None,
1673                max_distance_mm=1000000), True)
1674        autils.wait_for_event(
1675            dut2,
1676            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1677                                  aa_s_id))
1678        bb_p_id = dut2.droid.wifiAwarePublish(
1679            dut2_id,
1680            autils.add_ranging_to_pub(
1681                autils.create_discovery_config("BB",
1682                                               aconsts.PUBLISH_TYPE_SOLICITED),
1683                enable_ranging=True), True)
1684        autils.wait_for_event(
1685            dut2,
1686            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1687                                  bb_p_id))
1688        dd_s_id = dut2.droid.wifiAwareSubscribe(
1689            dut2_id,
1690            autils.create_discovery_config(
1691                "AA", aconsts.SUBSCRIBE_TYPE_ACTIVE), True)
1692        autils.wait_for_event(
1693            dut2,
1694            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1695                                  dd_s_id))
1696
1697        # Initial set of discovery events for AA, BB, and DD (which are up)
1698        event = autils.wait_for_event(
1699            dut2,
1700            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1701                                  aa_s_id))
1702        asserts.assert_true(
1703            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1704            "Discovery with ranging for AA expected!")
1705        event = autils.wait_for_event(
1706            dut1,
1707            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1708                                  bb_s_id))
1709        if self.RANGING_INITIATOR_RESPONDER_CONCURRENCY_LIMITATION:
1710            asserts.assert_false(
1711                aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1712                "Discovery with ranging for BB NOT expected!")
1713        else:
1714            asserts.assert_true(
1715                aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1716                "Discovery with ranging for BB expected!")
1717        event = autils.wait_for_event(
1718            dut2,
1719            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1720                                  dd_s_id))
1721        asserts.assert_false(
1722            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1723            "Discovery with ranging for DD NOT expected!")
1724
1725        # DUT1/DUT2: terminate AA
1726        dut1.droid.wifiAwareDestroyDiscoverySession(aa_p_id)
1727        dut2.droid.wifiAwareDestroyDiscoverySession(aa_s_id)
1728
1729        time.sleep(
1730            5)  # guarantee that session terminated (and host recovered?)
1731
1732        # DUT2: try EE service - ranging still disabled
1733        ee_p_id = dut2.droid.wifiAwarePublish(
1734            dut2_id,
1735            autils.add_ranging_to_pub(
1736                autils.create_discovery_config(
1737                    "EE", aconsts.PUBLISH_TYPE_UNSOLICITED),
1738                enable_ranging=True), True)
1739        autils.wait_for_event(
1740            dut2,
1741            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1742                                  ee_p_id))
1743
1744        event = autils.wait_for_event(
1745            dut1,
1746            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1747                                  ee_s_id))
1748        if self.RANGING_INITIATOR_RESPONDER_CONCURRENCY_LIMITATION:
1749            asserts.assert_false(
1750                aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1751                "Discovery with ranging for EE NOT expected!")
1752        else:
1753            asserts.assert_true(
1754                aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1755                "Discovery with ranging for EE expected!")
1756
1757        # DUT1: terminate CC - last publish w/ ranging on DUT!
1758        dut1.droid.wifiAwareDestroyDiscoverySession(cc_p_id)
1759
1760        time.sleep(
1761            5)  # guarantee that session terminated (and host recovered?)
1762
1763        # DUT2: try FF service - ranging should now function
1764        ff_p_id = dut2.droid.wifiAwarePublish(
1765            dut2_id,
1766            autils.add_ranging_to_pub(
1767                autils.create_discovery_config("FF",
1768                                               aconsts.PUBLISH_TYPE_SOLICITED),
1769                enable_ranging=True), True)
1770        autils.wait_for_event(
1771            dut2,
1772            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1773                                  ff_p_id))
1774
1775        event = autils.wait_for_event(
1776            dut1,
1777            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1778                                  ff_s_id))
1779        asserts.assert_true(
1780            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1781            "Discovery with ranging for FF expected!")
1782
1783    @test_tracker_info(uuid="6700eab8-a172-43cd-aed3-e6577ce8fd89")
1784    def test_discovery_direct_concurrency(self):
1785        """Verify the behavior of Wi-Fi Aware Ranging used as part of discovery and
1786    as direct ranging to a peer device.
1787
1788    Process:
1789    - Start YYY service with ranging in-range
1790    - Start XXX service with ranging out-of-range
1791    - Start performing direct Ranging
1792    - While above going on update XXX to be in-range
1793    - Keep performing direct Ranging in context of YYY
1794    - Stop direct Ranging and look for XXX to discover
1795    """
1796        dut1 = self.android_devices[0]
1797        dut1.pretty_name = "DUT1"
1798        dut2 = self.android_devices[1]
1799        dut2.pretty_name = "DUT2"
1800
1801        # DUTs: attach and wait for confirmation
1802        dut1_id = dut1.droid.wifiAwareAttach(False)
1803        autils.wait_for_event(dut1, aconsts.EVENT_CB_ON_ATTACHED)
1804        time.sleep(self.device_startup_offset)
1805        dut2_id = dut2.droid.wifiAwareAttach(True)
1806        event = autils.wait_for_event(dut2,
1807                                      aconsts.EVENT_CB_ON_IDENTITY_CHANGED)
1808        dut2_mac = event['data']['mac']
1809
1810        # DUT1: publishers bring-up
1811        xxx_p_id = dut1.droid.wifiAwarePublish(
1812            dut1_id,
1813            autils.add_ranging_to_pub(
1814                autils.create_discovery_config(
1815                    "XXX", aconsts.PUBLISH_TYPE_UNSOLICITED),
1816                enable_ranging=True), True)
1817        autils.wait_for_event(
1818            dut1,
1819            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1820                                  xxx_p_id))
1821        yyy_p_id = dut1.droid.wifiAwarePublish(
1822            dut1_id,
1823            autils.add_ranging_to_pub(
1824                autils.create_discovery_config(
1825                    "YYY", aconsts.PUBLISH_TYPE_UNSOLICITED),
1826                enable_ranging=True), True)
1827        autils.wait_for_event(
1828            dut1,
1829            autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED,
1830                                  yyy_p_id))
1831
1832        # DUT2: subscribers bring-up
1833        xxx_s_id = dut2.droid.wifiAwareSubscribe(
1834            dut2_id,
1835            autils.add_ranging_to_sub(
1836                autils.create_discovery_config("XXX",
1837                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1838                min_distance_mm=1000000,
1839                max_distance_mm=1000001), True)
1840        autils.wait_for_event(
1841            dut2,
1842            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1843                                  xxx_s_id))
1844        yyy_s_id = dut2.droid.wifiAwareSubscribe(
1845            dut2_id,
1846            autils.add_ranging_to_sub(
1847                autils.create_discovery_config("YYY",
1848                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1849                min_distance_mm=None,
1850                max_distance_mm=1000000), True)
1851        autils.wait_for_event(
1852            dut2,
1853            autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED,
1854                                  yyy_s_id))
1855
1856        # Service discovery: YYY (with range info), but no XXX
1857        event = autils.wait_for_event(
1858            dut2,
1859            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1860                                  yyy_s_id))
1861        asserts.assert_true(
1862            aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"],
1863            "Discovery with ranging for YYY expected!")
1864        yyy_peer_id_on_sub = event['data'][aconsts.SESSION_CB_KEY_PEER_ID]
1865
1866        autils.fail_on_event(
1867            dut2,
1868            autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1869                                  xxx_s_id))
1870
1871        # Direct ranging
1872        results21 = []
1873        for iter in range(10):
1874            id = dut2.droid.wifiRttStartRangingToAwarePeerId(
1875                yyy_peer_id_on_sub)
1876            event = autils.wait_for_event(
1877                dut2,
1878                rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT, id))
1879            results21.append(
1880                event["data"][rconsts.EVENT_CB_RANGING_KEY_RESULTS][0])
1881
1882        time.sleep(5)  # while switching roles
1883
1884        results12 = []
1885        for iter in range(10):
1886            id = dut1.droid.wifiRttStartRangingToAwarePeerMac(dut2_mac)
1887            event = autils.wait_for_event(
1888                dut1,
1889                rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT, id))
1890            results12.append(
1891                event["data"][rconsts.EVENT_CB_RANGING_KEY_RESULTS][0])
1892
1893        stats = [
1894            rutils.extract_stats(results12, 0, 0, 0),
1895            rutils.extract_stats(results21, 0, 0, 0)
1896        ]
1897
1898        # Update XXX to be within range
1899        dut2.droid.wifiAwareUpdateSubscribe(
1900            xxx_s_id,
1901            autils.add_ranging_to_sub(
1902                autils.create_discovery_config("XXX",
1903                                               aconsts.SUBSCRIBE_TYPE_PASSIVE),
1904                min_distance_mm=None,
1905                max_distance_mm=1000000))
1906        autils.wait_for_event(
1907            dut2,
1908            autils.decorate_event(aconsts.SESSION_CB_ON_SESSION_CONFIG_UPDATED,
1909                                  xxx_s_id))
1910
1911        # Expect discovery on XXX - wait until discovery with ranging:
1912        # - 0 or more: without ranging info (due to concurrency limitations)
1913        # - 1 or more: with ranging (once concurrency limitation relieved)
1914        num_events = 0
1915        while True:
1916            event = autils.wait_for_event(
1917                dut2,
1918                autils.decorate_event(aconsts.SESSION_CB_ON_SERVICE_DISCOVERED,
1919                                      xxx_s_id))
1920            if aconsts.SESSION_CB_KEY_DISTANCE_MM in event["data"]:
1921                break
1922            num_events = num_events + 1
1923            asserts.assert_true(
1924                num_events < 10,  # arbitrary safety valve
1925                "Way too many discovery events without ranging!")
1926
1927        asserts.explicit_pass(
1928            "Discovery/Direct RTT Concurrency Pass", extras={"data": stats})
1929