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