1#!/usr/bin/env python3 2# 3# Copyright 2018 Google, Inc. 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 logging 18import os 19import time 20 21from queue import Empty 22 23from acts import asserts 24from acts import utils 25from acts_contrib.test_utils.wifi.p2p import wifi_p2p_const as p2pconsts 26import acts.utils 27 28 29def is_discovered(event, ad): 30 """Check an Android device exist in WifiP2pOnPeersAvailable event or not. 31 32 Args: 33 event: WifiP2pOnPeersAvailable which include all of p2p devices. 34 ad: The android device 35 Returns: 36 True: if an Android device exist in p2p list 37 False: if not exist 38 """ 39 for device in event['data']['Peers']: 40 if device['Name'] == ad.name: 41 ad.deviceAddress = device['Address'] 42 return True 43 return False 44 45 46def check_disconnect(ad, timeout=p2pconsts.DEFAULT_TIMEOUT): 47 """Check an Android device disconnect or not 48 49 Args: 50 ad: The android device 51 """ 52 ad.droid.wifiP2pRequestConnectionInfo() 53 # wait disconnect event 54 ad.ed.pop_event(p2pconsts.DISCONNECTED_EVENT, timeout) 55 56 57def p2p_disconnect(ad): 58 """Invoke an Android device removeGroup to trigger p2p disconnect 59 60 Args: 61 ad: The android device 62 """ 63 ad.log.debug("Disconnect") 64 ad.droid.wifiP2pRemoveGroup() 65 check_disconnect(ad) 66 67 68def p2p_connection_ping_test(ad, target_ip_address): 69 """Let an Android device to start ping target_ip_address 70 71 Args: 72 ad: The android device 73 target_ip_address: ip address which would like to ping 74 """ 75 ad.log.debug("Run Ping Test, %s ping %s " % (ad.serial, target_ip_address)) 76 asserts.assert_true( 77 acts.utils.adb_shell_ping(ad, 78 count=6, 79 dest_ip=target_ip_address, 80 timeout=20), "%s ping failed" % (ad.serial)) 81 82 83def is_go(ad): 84 """Check an Android p2p role is Go or not 85 86 Args: 87 ad: The android device 88 Return: 89 True: An Android device is p2p go 90 False: An Android device is p2p gc 91 """ 92 ad.log.debug("is go check") 93 ad.droid.wifiP2pRequestConnectionInfo() 94 ad_connect_info_event = ad.ed.pop_event( 95 p2pconsts.CONNECTION_INFO_AVAILABLE_EVENT, p2pconsts.DEFAULT_TIMEOUT) 96 if ad_connect_info_event['data']['isGroupOwner']: 97 return True 98 return False 99 100 101def p2p_go_ip(ad): 102 """Get GO IP address 103 104 Args: 105 ad: The android device 106 Return: 107 GO IP address 108 """ 109 ad.log.debug("p2p go ip") 110 ad.droid.wifiP2pRequestConnectionInfo() 111 ad_connect_info_event = ad.ed.pop_event( 112 p2pconsts.CONNECTION_INFO_AVAILABLE_EVENT, p2pconsts.DEFAULT_TIMEOUT) 113 ad.log.debug("p2p go ip: %s" % 114 ad_connect_info_event['data']['groupOwnerHostAddress']) 115 return ad_connect_info_event['data']['groupOwnerHostAddress'] 116 117 118def p2p_get_current_group(ad): 119 """Get current group information 120 121 Args: 122 ad: The android device 123 Return: 124 p2p group information 125 """ 126 ad.log.debug("get current group") 127 ad.droid.wifiP2pRequestGroupInfo() 128 ad_group_info_event = ad.ed.pop_event(p2pconsts.GROUP_INFO_AVAILABLE_EVENT, 129 p2pconsts.DEFAULT_TIMEOUT) 130 ad.log.debug( 131 "p2p group: SSID:%s, password:%s, owner address: %s, interface: %s" % 132 (ad_group_info_event['data']['NetworkName'], 133 ad_group_info_event['data']['Passphrase'], 134 ad_group_info_event['data']['OwnerAddress'], 135 ad_group_info_event['data']['Interface'])) 136 return ad_group_info_event['data'] 137 138def is_ongoing_peer_ready(peerConfig, waitForPin): 139 """Check whether the peer config is ready 140 141 Args: 142 peerConfig: the ongoing config 143 waitForPin: this config needs key or not 144 Return: 145 true for ready; false otherwise. 146 """ 147 if peerConfig is None: 148 return False 149 if not peerConfig['data'][WifiP2PEnums.WifiP2pConfig.DEVICEADDRESS_KEY]: 150 return False 151 if not waitForPin: 152 return True 153 if WifiP2PEnums.WpsInfo.WPS_PIN_KEY in peerConfig['data'][ 154 WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY]: 155 return True 156 return False 157 158def wait_for_ongoing_peer_ready(ad, waitForPin, maxPollingCount): 159 """wait for the ongoing peer data ready 160 161 Args: 162 ad: The android device 163 waitForPin: this config needs key or not 164 maxPollingCount: the max polling count 165 Return: 166 the ongoing peer config 167 """ 168 ad_peerConfig = None 169 ad.log.debug("%s is waiting for the ongoing peer, max polling count %s" 170 % (ad.name, maxPollingCount)) 171 while maxPollingCount > 0: 172 ad.droid.requestP2pPeerConfigure() 173 ad_peerConfig = ad.ed.pop_event( 174 p2pconsts.ONGOING_PEER_INFO_AVAILABLE_EVENT, 175 p2pconsts.DEFAULT_TIMEOUT) 176 maxPollingCount -= 1 177 if is_ongoing_peer_ready(ad_peerConfig, waitForPin): 178 break 179 ad.log.debug("%s is not ready for next step" % (ad.name)) 180 time.sleep(p2pconsts.DEFAULT_POLLING_SLEEPTIME) 181 asserts.assert_true( 182 ad_peerConfig['data'][WifiP2PEnums.WifiP2pConfig.DEVICEADDRESS_KEY], 183 "DUT %s does not receive the request." % (ad.name)) 184 ad.log.debug(ad_peerConfig['data']) 185 return ad_peerConfig 186 187#trigger p2p connect to ad2 from ad1 188def p2p_connect(ad1, 189 ad2, 190 isReconnect, 191 wpsSetup, 192 p2p_connect_type=p2pconsts.P2P_CONNECT_NEGOTIATION, 193 go_ad=None): 194 """trigger p2p connect to ad2 from ad1 195 196 Args: 197 ad1: The android device 198 ad2: The android device 199 isReconnect: boolean, if persist group is exist, 200 isReconnect is true, otherswise is false. 201 wpsSetup: which wps connection would like to use 202 p2p_connect_type: enumeration, which type this p2p connection is 203 go_ad: The group owner android device which is used for the invitation connection 204 """ 205 ad1.log.info("Create p2p connection from %s to %s via wps: %s type %d" % 206 (ad1.name, ad2.name, wpsSetup, p2p_connect_type)) 207 if p2p_connect_type == p2pconsts.P2P_CONNECT_INVITATION: 208 if go_ad is None: 209 go_ad = ad1 210 find_p2p_device(ad1, ad2) 211 # GO might be another peer, so ad2 needs to find it first. 212 find_p2p_group_owner(ad2, go_ad) 213 elif p2p_connect_type == p2pconsts.P2P_CONNECT_JOIN: 214 find_p2p_group_owner(ad1, ad2) 215 else: 216 find_p2p_device(ad1, ad2) 217 time.sleep(p2pconsts.DEFAULT_SLEEPTIME) 218 wifi_p2p_config = { 219 WifiP2PEnums.WifiP2pConfig.DEVICEADDRESS_KEY: ad2.deviceAddress, 220 WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY: { 221 WifiP2PEnums.WpsInfo.WPS_SETUP_KEY: wpsSetup 222 } 223 } 224 ad1.droid.wifiP2pConnect(wifi_p2p_config) 225 ad1.ed.pop_event(p2pconsts.CONNECT_SUCCESS_EVENT, 226 p2pconsts.DEFAULT_TIMEOUT) 227 if not isReconnect: 228 # ad1 is the initiator, it should be ready soon. 229 ad1_peerConfig = wait_for_ongoing_peer_ready(ad1, 230 wpsSetup == WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_DISPLAY, 6) 231 # auto-join tries 10 times to find groups, and 232 # one round takes 2 - 3 seconds. 233 ad2_peerConfig = wait_for_ongoing_peer_ready(ad2, 234 wpsSetup == WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_KEYPAD, 31) 235 if wpsSetup == WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_DISPLAY: 236 asserts.assert_true( 237 WifiP2PEnums.WpsInfo.WPS_PIN_KEY in ad1_peerConfig['data'][ 238 WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY], 239 "Can't get display pin value") 240 ad2_peerConfig['data'][WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY][ 241 WifiP2PEnums.WpsInfo.WPS_PIN_KEY] = ad1_peerConfig['data'][ 242 WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY][ 243 WifiP2PEnums.WpsInfo.WPS_PIN_KEY] 244 ad2.droid.setP2pPeerConfigure(ad2_peerConfig['data']) 245 ad2.ed.pop_event(p2pconsts.ONGOING_PEER_SET_SUCCESS_EVENT, 246 p2pconsts.DEFAULT_TIMEOUT) 247 ad2.droid.wifiP2pAcceptConnection() 248 elif wpsSetup == WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_KEYPAD: 249 asserts.assert_true( 250 WifiP2PEnums.WpsInfo.WPS_PIN_KEY in ad2_peerConfig['data'][ 251 WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY], 252 "Can't get keypad pin value") 253 ad1_peerConfig['data'][WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY][ 254 WifiP2PEnums.WpsInfo.WPS_PIN_KEY] = ad2_peerConfig['data'][ 255 WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY][ 256 WifiP2PEnums.WpsInfo.WPS_PIN_KEY] 257 ad1.droid.setP2pPeerConfigure(ad1_peerConfig['data']) 258 ad1.ed.pop_event(p2pconsts.ONGOING_PEER_SET_SUCCESS_EVENT, 259 p2pconsts.DEFAULT_TIMEOUT) 260 ad1.droid.wifiP2pAcceptConnection() 261 time.sleep(p2pconsts.DEFAULT_SLEEPTIME) 262 ad2.droid.wifiP2pConfirmConnection() 263 elif wpsSetup == WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_PBC: 264 ad2.droid.wifiP2pAcceptConnection() 265 if p2p_connect_type == p2pconsts.P2P_CONNECT_INVITATION: 266 time.sleep(p2pconsts.DEFAULT_SLEEPTIME) 267 go_ad.droid.wifiP2pAcceptConnection() 268 269 #wait connected event 270 if p2p_connect_type == p2pconsts.P2P_CONNECT_INVITATION: 271 go_ad.ed.pop_event(p2pconsts.CONNECTED_EVENT, 272 p2pconsts.DEFAULT_TIMEOUT) 273 else: 274 ad1.ed.pop_event(p2pconsts.CONNECTED_EVENT, p2pconsts.DEFAULT_TIMEOUT) 275 ad2.ed.pop_event(p2pconsts.CONNECTED_EVENT, p2pconsts.DEFAULT_TIMEOUT) 276 277 278def p2p_connect_with_config(ad1, ad2, network_name, passphrase, band): 279 """trigger p2p connect to ad2 from ad1 with config 280 281 Args: 282 ad1: The android device 283 ad2: The android device 284 network_name: the network name of the desired group. 285 passphrase: the passphrase of the desired group. 286 band: the operating band of the desired group. 287 """ 288 ad1.log.info("Create p2p connection from %s to %s" % (ad1.name, ad2.name)) 289 find_p2p_device(ad1, ad2) 290 time.sleep(p2pconsts.DEFAULT_SLEEPTIME) 291 wifi_p2p_config = { 292 WifiP2PEnums.WifiP2pConfig.NETWORK_NAME: network_name, 293 WifiP2PEnums.WifiP2pConfig.PASSPHRASE: passphrase, 294 WifiP2PEnums.WifiP2pConfig.GROUP_BAND: band, 295 WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY: { 296 WifiP2PEnums.WpsInfo.WPS_SETUP_KEY: 297 WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_PBC 298 } 299 } 300 ad1.droid.wifiP2pConnect(wifi_p2p_config) 301 ad1.ed.pop_event(p2pconsts.CONNECT_SUCCESS_EVENT, 302 p2pconsts.DEFAULT_TIMEOUT) 303 time.sleep(p2pconsts.DEFAULT_SLEEPTIME) 304 305 #wait connected event 306 ad1.ed.pop_event(p2pconsts.CONNECTED_EVENT, p2pconsts.DEFAULT_TIMEOUT) 307 ad2.ed.pop_event(p2pconsts.CONNECTED_EVENT, p2pconsts.DEFAULT_TIMEOUT) 308 309 310def find_p2p_device(ad1, ad2): 311 """Check an Android device ad1 can discover an Android device ad2 312 313 Args: 314 ad1: The android device 315 ad2: The android device 316 """ 317 ad1.droid.wifiP2pDiscoverPeers() 318 ad2.droid.wifiP2pDiscoverPeers() 319 p2p_find_result = False 320 ad1.ed.clear_events(p2pconsts.PEER_AVAILABLE_EVENT) 321 while not p2p_find_result: 322 ad1_event = ad1.ed.pop_event(p2pconsts.PEER_AVAILABLE_EVENT, 323 p2pconsts.P2P_FIND_TIMEOUT) 324 ad1.log.debug(ad1_event['data']) 325 p2p_find_result = is_discovered(ad1_event, ad2) 326 asserts.assert_true(p2p_find_result, 327 "DUT didn't discovered peer:%s device" % (ad2.name)) 328 329 330def find_p2p_group_owner(ad1, ad2): 331 """Check an Android device ad1 can discover an Android device ad2 which 332 is a group owner 333 334 Args: 335 ad1: The android device 336 ad2: The android device which is a group owner 337 """ 338 p2p_find_result = False 339 ad1.ed.clear_events(p2pconsts.PEER_AVAILABLE_EVENT) 340 while not p2p_find_result: 341 ad2.droid.wifiP2pStopPeerDiscovery() 342 ad1.droid.wifiP2pStopPeerDiscovery() 343 ad2.droid.wifiP2pDiscoverPeers() 344 ad1.droid.wifiP2pDiscoverPeers() 345 ad1_event = ad1.ed.pop_event(p2pconsts.PEER_AVAILABLE_EVENT, 346 p2pconsts.P2P_FIND_TIMEOUT) 347 ad1.log.debug(ad1_event['data']) 348 for device in ad1_event['data']['Peers']: 349 if (device['Name'] == ad2.name and int(device['GroupCapability']) 350 & p2pconsts.P2P_GROUP_CAPAB_GROUP_OWNER): 351 ad2.deviceAddress = device['Address'] 352 p2p_find_result = True 353 asserts.assert_true( 354 p2p_find_result, 355 "DUT didn't discovered group owner peer:%s device" % (ad2.name)) 356 357 358def createP2pLocalService(ad, serviceCategory): 359 """Based on serviceCategory to create p2p local service 360 on an Android device ad 361 362 Args: 363 ad: The android device 364 serviceCategory: p2p local service type, UPNP / IPP / AFP, 365 """ 366 testData = genTestData(serviceCategory) 367 if serviceCategory == p2pconsts.P2P_LOCAL_SERVICE_UPNP: 368 ad.droid.wifiP2pCreateUpnpServiceInfo(testData[0], testData[1], 369 testData[2]) 370 elif (serviceCategory == p2pconsts.P2P_LOCAL_SERVICE_IPP 371 or serviceCategory == p2pconsts.P2P_LOCAL_SERVICE_AFP): 372 ad.droid.wifiP2pCreateBonjourServiceInfo(testData[0], testData[1], 373 testData[2]) 374 ad.droid.wifiP2pAddLocalService() 375 376 377def requestServiceAndCheckResult(ad_serviceProvider, ad_serviceReceiver, 378 serviceType, queryString1, queryString2): 379 """Based on serviceType and query info, check service request result 380 same as expect or not on an Android device ad_serviceReceiver. 381 And remove p2p service request after result check. 382 383 Args: 384 ad_serviceProvider: The android device which provide p2p local service 385 ad_serviceReceiver: The android device which query p2p local service 386 serviceType: P2p local service type, Upnp or Bonjour 387 queryString1: Query String, NonNull 388 queryString2: Query String, used for Bonjour, Nullable 389 """ 390 expectData = genExpectTestData(serviceType, queryString1, queryString2) 391 find_p2p_device(ad_serviceReceiver, ad_serviceProvider) 392 ad_serviceReceiver.droid.wifiP2pStopPeerDiscovery() 393 ad_serviceReceiver.droid.wifiP2pClearServiceRequests() 394 time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME) 395 396 ad_serviceReceiver.droid.wifiP2pDiscoverServices() 397 serviceData = {} 398 service_id = 0 399 if (serviceType == 400 WifiP2PEnums.WifiP2pServiceInfo.WIFI_P2P_SERVICE_TYPE_BONJOUR): 401 ad_serviceReceiver.log.info( 402 "Request bonjour service in \ 403 %s with Query String %s and %s " % 404 (ad_serviceReceiver.name, queryString1, queryString2)) 405 ad_serviceReceiver.log.info("expectData %s" % expectData) 406 if queryString1 != None: 407 service_id = ad_serviceReceiver.droid.wifiP2pAddDnssdServiceRequest( 408 queryString1, queryString2) 409 else: 410 service_id = ad_serviceReceiver.droid.wifiP2pAddServiceRequest( 411 serviceType) 412 ad_serviceReceiver.log.info("request bonjour service id %s" % 413 service_id) 414 ad_serviceReceiver.droid.wifiP2pSetDnsSdResponseListeners() 415 ad_serviceReceiver.droid.wifiP2pDiscoverServices() 416 ad_serviceReceiver.log.info("Check Service Listener") 417 time.sleep(p2pconsts.DEFAULT_SERVICE_WAITING_TIME) 418 try: 419 dnssd_events = ad_serviceReceiver.ed.pop_all(p2pconsts.DNSSD_EVENT) 420 dnssd_txrecord_events = ad_serviceReceiver.ed.pop_all( 421 p2pconsts.DNSSD_TXRECORD_EVENT) 422 dns_service = WifiP2PEnums.WifiP2pDnsSdServiceResponse() 423 for dnssd_event in dnssd_events: 424 if dnssd_event['data'][ 425 'SourceDeviceAddress'] == ad_serviceProvider.deviceAddress: 426 dns_service.InstanceName = dnssd_event['data'][ 427 p2pconsts.DNSSD_EVENT_INSTANCENAME_KEY] 428 dns_service.RegistrationType = dnssd_event['data'][ 429 p2pconsts.DNSSD_EVENT_REGISTRATIONTYPE_KEY] 430 dns_service.FullDomainName = "" 431 dns_service.TxtRecordMap = "" 432 serviceData[dns_service.toString()] = 1 433 for dnssd_txrecord_event in dnssd_txrecord_events: 434 if dnssd_txrecord_event['data'][ 435 'SourceDeviceAddress'] == ad_serviceProvider.deviceAddress: 436 dns_service.InstanceName = "" 437 dns_service.RegistrationType = "" 438 dns_service.FullDomainName = dnssd_txrecord_event['data'][ 439 p2pconsts.DNSSD_TXRECORD_EVENT_FULLDOMAINNAME_KEY] 440 dns_service.TxtRecordMap = dnssd_txrecord_event['data'][ 441 p2pconsts.DNSSD_TXRECORD_EVENT_TXRECORDMAP_KEY] 442 serviceData[dns_service.toString()] = 1 443 ad_serviceReceiver.log.info("serviceData %s" % serviceData) 444 if len(serviceData) == 0: 445 ad_serviceReceiver.droid.wifiP2pRemoveServiceRequest( 446 service_id) 447 return -1 448 except queue.Empty as error: 449 ad_serviceReceiver.log.info("dnssd event is empty", ) 450 elif (serviceType == 451 WifiP2PEnums.WifiP2pServiceInfo.WIFI_P2P_SERVICE_TYPE_UPNP): 452 ad_serviceReceiver.log.info( 453 "Request upnp service in %s with Query String %s " % 454 (ad_serviceReceiver.name, queryString1)) 455 ad_serviceReceiver.log.info("expectData %s" % expectData) 456 if queryString1 != None: 457 service_id = ad_serviceReceiver.droid.wifiP2pAddUpnpServiceRequest( 458 queryString1) 459 else: 460 service_id = ad_serviceReceiver.droid.wifiP2pAddServiceRequest( 461 WifiP2PEnums.WifiP2pServiceInfo.WIFI_P2P_SERVICE_TYPE_UPNP) 462 ad_serviceReceiver.droid.wifiP2pSetUpnpResponseListeners() 463 ad_serviceReceiver.droid.wifiP2pDiscoverServices() 464 ad_serviceReceiver.log.info("Check Service Listener") 465 time.sleep(p2pconsts.DEFAULT_SERVICE_WAITING_TIME) 466 try: 467 upnp_events = ad_serviceReceiver.ed.pop_all(p2pconsts.UPNP_EVENT) 468 for upnp_event in upnp_events: 469 if upnp_event['data']['Device'][ 470 'Address'] == ad_serviceProvider.deviceAddress: 471 for service in upnp_event['data'][ 472 p2pconsts.UPNP_EVENT_SERVICELIST_KEY]: 473 serviceData[service] = 1 474 ad_serviceReceiver.log.info("serviceData %s" % serviceData) 475 if len(serviceData) == 0: 476 ad_serviceReceiver.droid.wifiP2pRemoveServiceRequest( 477 service_id) 478 return -1 479 except queue.Empty as error: 480 ad_serviceReceiver.log.info("p2p upnp event is empty", ) 481 482 ad_serviceReceiver.log.info("Check ServiceList") 483 asserts.assert_true(checkServiceQueryResult(serviceData, expectData), 484 "ServiceList not same as Expect") 485 # After service checked, remove the service_id 486 ad_serviceReceiver.droid.wifiP2pRemoveServiceRequest(service_id) 487 return 0 488 489 490def requestServiceAndCheckResultWithRetry(ad_serviceProvider, 491 ad_serviceReceiver, 492 serviceType, 493 queryString1, 494 queryString2, 495 retryCount=3): 496 """ allow failures for requestServiceAndCheckResult. Service 497 discovery might fail unexpectedly because the request packet might not be 498 recevied by the service responder due to p2p state switch. 499 500 Args: 501 ad_serviceProvider: The android device which provide p2p local service 502 ad_serviceReceiver: The android device which query p2p local service 503 serviceType: P2p local service type, Upnp or Bonjour 504 queryString1: Query String, NonNull 505 queryString2: Query String, used for Bonjour, Nullable 506 retryCount: maximum retry count, default is 3 507 """ 508 ret = 0 509 while retryCount > 0: 510 ret = requestServiceAndCheckResult(ad_serviceProvider, 511 ad_serviceReceiver, serviceType, 512 queryString1, queryString2) 513 if (ret == 0): 514 break 515 retryCount -= 1 516 517 asserts.assert_equal(0, ret, "cannot find any services with retries.") 518 519 520def checkServiceQueryResult(serviceList, expectServiceList): 521 """Check serviceList same as expectServiceList or not 522 523 Args: 524 serviceList: ServiceList which get from query result 525 expectServiceList: ServiceList which hardcode in genExpectTestData 526 Return: 527 True: serviceList same as expectServiceList 528 False:Exist discrepancy between serviceList and expectServiceList 529 """ 530 tempServiceList = serviceList.copy() 531 tempExpectServiceList = expectServiceList.copy() 532 for service in serviceList.keys(): 533 if service in expectServiceList: 534 del tempServiceList[service] 535 del tempExpectServiceList[service] 536 return len(tempExpectServiceList) == 0 and len(tempServiceList) == 0 537 538 539def genTestData(serviceCategory): 540 """Based on serviceCategory to generator Test Data 541 542 Args: 543 serviceCategory: P2p local service type, Upnp or Bonjour 544 Return: 545 TestData 546 """ 547 testData = [] 548 if serviceCategory == p2pconsts.P2P_LOCAL_SERVICE_UPNP: 549 testData.append(p2pconsts.UpnpTestData.uuid) 550 testData.append(p2pconsts.UpnpTestData.serviceType) 551 testData.append([ 552 p2pconsts.UpnpTestData.AVTransport, 553 p2pconsts.UpnpTestData.ConnectionManager 554 ]) 555 elif serviceCategory == p2pconsts.P2P_LOCAL_SERVICE_IPP: 556 testData.append(p2pconsts.IppTestData.ippInstanceName) 557 testData.append(p2pconsts.IppTestData.ippRegistrationType) 558 testData.append(p2pconsts.IppTestData.ipp_txtRecord) 559 elif serviceCategory == p2pconsts.P2P_LOCAL_SERVICE_AFP: 560 testData.append(p2pconsts.AfpTestData.afpInstanceName) 561 testData.append(p2pconsts.AfpTestData.afpRegistrationType) 562 testData.append(p2pconsts.AfpTestData.afp_txtRecord) 563 564 return testData 565 566 567def genExpectTestData(serviceType, queryString1, queryString2): 568 """Based on serviceCategory to generator expect serviceList 569 570 Args: 571 serviceType: P2p local service type, Upnp or Bonjour 572 queryString1: Query String, NonNull 573 queryString2: Query String, used for Bonjour, Nullable 574 Return: 575 expectServiceList 576 """ 577 expectServiceList = {} 578 if (serviceType == 579 WifiP2PEnums.WifiP2pServiceInfo.WIFI_P2P_SERVICE_TYPE_BONJOUR): 580 ipp_service = WifiP2PEnums.WifiP2pDnsSdServiceResponse() 581 afp_service = WifiP2PEnums.WifiP2pDnsSdServiceResponse() 582 if queryString1 == p2pconsts.IppTestData.ippRegistrationType: 583 if queryString2 == p2pconsts.IppTestData.ippInstanceName: 584 ipp_service.InstanceName = "" 585 ipp_service.RegistrationType = "" 586 ipp_service.FullDomainName = p2pconsts.IppTestData.ippDomainName 587 ipp_service.TxtRecordMap = p2pconsts.IppTestData.ipp_txtRecord 588 expectServiceList[ipp_service.toString()] = 1 589 return expectServiceList 590 ipp_service.InstanceName = p2pconsts.IppTestData.ippInstanceName 591 ipp_service.RegistrationType = ( 592 p2pconsts.IppTestData.ippRegistrationType + ".local.") 593 ipp_service.FullDomainName = "" 594 ipp_service.TxtRecordMap = "" 595 expectServiceList[ipp_service.toString()] = 1 596 return expectServiceList 597 elif queryString1 == p2pconsts.AfpTestData.afpRegistrationType: 598 if queryString2 == p2pconsts.AfpTestData.afpInstanceName: 599 afp_service.InstanceName = "" 600 afp_service.RegistrationType = "" 601 afp_service.FullDomainName = p2pconsts.AfpTestData.afpDomainName 602 afp_service.TxtRecordMap = p2pconsts.AfpTestData.afp_txtRecord 603 expectServiceList[afp_service.toString()] = 1 604 return expectServiceList 605 ipp_service.InstanceName = p2pconsts.IppTestData.ippInstanceName 606 ipp_service.RegistrationType = ( 607 p2pconsts.IppTestData.ippRegistrationType + ".local.") 608 ipp_service.FullDomainName = "" 609 ipp_service.TxtRecordMap = "" 610 expectServiceList[ipp_service.toString()] = 1 611 612 ipp_service.InstanceName = "" 613 ipp_service.RegistrationType = "" 614 ipp_service.FullDomainName = p2pconsts.IppTestData.ippDomainName 615 ipp_service.TxtRecordMap = p2pconsts.IppTestData.ipp_txtRecord 616 expectServiceList[ipp_service.toString()] = 1 617 618 afp_service.InstanceName = p2pconsts.AfpTestData.afpInstanceName 619 afp_service.RegistrationType = ( 620 p2pconsts.AfpTestData.afpRegistrationType + ".local.") 621 afp_service.FullDomainName = "" 622 afp_service.TxtRecordMap = "" 623 expectServiceList[afp_service.toString()] = 1 624 625 afp_service.InstanceName = "" 626 afp_service.RegistrationType = "" 627 afp_service.FullDomainName = p2pconsts.AfpTestData.afpDomainName 628 afp_service.TxtRecordMap = p2pconsts.AfpTestData.afp_txtRecord 629 expectServiceList[afp_service.toString()] = 1 630 631 return expectServiceList 632 elif serviceType == WifiP2PEnums.WifiP2pServiceInfo.WIFI_P2P_SERVICE_TYPE_UPNP: 633 upnp_service = "uuid:" + p2pconsts.UpnpTestData.uuid + "::" + ( 634 p2pconsts.UpnpTestData.rootdevice) 635 expectServiceList[upnp_service] = 1 636 if queryString1 != "upnp:rootdevice": 637 upnp_service = "uuid:" + p2pconsts.UpnpTestData.uuid + ( 638 "::" + p2pconsts.UpnpTestData.AVTransport) 639 expectServiceList[upnp_service] = 1 640 upnp_service = "uuid:" + p2pconsts.UpnpTestData.uuid + ( 641 "::" + p2pconsts.UpnpTestData.ConnectionManager) 642 expectServiceList[upnp_service] = 1 643 upnp_service = "uuid:" + p2pconsts.UpnpTestData.uuid + ( 644 "::" + p2pconsts.UpnpTestData.serviceType) 645 expectServiceList[upnp_service] = 1 646 upnp_service = "uuid:" + p2pconsts.UpnpTestData.uuid 647 expectServiceList[upnp_service] = 1 648 649 return expectServiceList 650 651 652def p2p_create_group(ad): 653 """Create a group as Group Owner 654 655 Args: 656 ad: The android device 657 """ 658 ad.droid.wifiP2pCreateGroup() 659 ad.ed.pop_event(p2pconsts.CREATE_GROUP_SUCCESS_EVENT, 660 p2pconsts.DEFAULT_TIMEOUT) 661 time.sleep(p2pconsts.DEFAULT_SLEEPTIME) 662 663 664def p2p_create_group_with_config(ad, network_name, passphrase, band): 665 """Create a group as Group Owner 666 667 Args: 668 ad: The android device 669 """ 670 wifi_p2p_config = { 671 WifiP2PEnums.WifiP2pConfig.NETWORK_NAME: network_name, 672 WifiP2PEnums.WifiP2pConfig.PASSPHRASE: passphrase, 673 WifiP2PEnums.WifiP2pConfig.GROUP_BAND: band, 674 WifiP2PEnums.WifiP2pConfig.WPSINFO_KEY: { 675 WifiP2PEnums.WpsInfo.WPS_SETUP_KEY: 676 WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_PBC 677 } 678 } 679 ad.droid.wifiP2pCreateGroupWithConfig(wifi_p2p_config) 680 ad.ed.pop_event(p2pconsts.CREATE_GROUP_SUCCESS_EVENT, 681 p2pconsts.DEFAULT_TIMEOUT) 682 time.sleep(p2pconsts.DEFAULT_SLEEPTIME) 683 684 685def wifi_p2p_set_channels_for_current_group(ad, listening_chan, 686 operating_chan): 687 """Sets the listening channel and operating channel of the current group 688 created with initialize. 689 690 Args: 691 ad: The android device 692 listening_chan: Integer, the listening channel 693 operating_chan: Integer, the operating channel 694 """ 695 ad.droid.wifiP2pSetChannelsForCurrentGroup(listening_chan, operating_chan) 696 ad.ed.pop_event(p2pconsts.SET_CHANNEL_SUCCESS_EVENT, 697 p2pconsts.DEFAULT_TIMEOUT) 698 699 700class WifiP2PEnums(): 701 class WifiP2pConfig(): 702 DEVICEADDRESS_KEY = "deviceAddress" 703 WPSINFO_KEY = "wpsInfo" 704 GO_INTENT_KEY = "groupOwnerIntent" 705 NETID_KEY = "netId" 706 NETWORK_NAME = "networkName" 707 PASSPHRASE = "passphrase" 708 GROUP_BAND = "groupOwnerBand" 709 710 class WpsInfo(): 711 WPS_SETUP_KEY = "setup" 712 BSSID_KEY = "BSSID" 713 WPS_PIN_KEY = "pin" 714 #TODO: remove it from wifi_test_utils.py 715 WIFI_WPS_INFO_PBC = 0 716 WIFI_WPS_INFO_DISPLAY = 1 717 WIFI_WPS_INFO_KEYPAD = 2 718 WIFI_WPS_INFO_LABEL = 3 719 WIFI_WPS_INFO_INVALID = 4 720 721 class WifiP2pServiceInfo(): 722 #TODO: remove it from wifi_test_utils.py 723 # Macros for wifi p2p. 724 WIFI_P2P_SERVICE_TYPE_ALL = 0 725 WIFI_P2P_SERVICE_TYPE_BONJOUR = 1 726 WIFI_P2P_SERVICE_TYPE_UPNP = 2 727 WIFI_P2P_SERVICE_TYPE_VENDOR_SPECIFIC = 255 728 729 class WifiP2pDnsSdServiceResponse(): 730 def __init__(self): 731 pass 732 733 InstanceName = "" 734 RegistrationType = "" 735 FullDomainName = "" 736 TxtRecordMap = {} 737 738 def toString(self): 739 return self.InstanceName + self.RegistrationType + ( 740 self.FullDomainName + str(self.TxtRecordMap)) 741