1# 2# Copyright 2018 - The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16from acts import asserts 17 18def start_natt_keepalive(ad, src_ip, src_port, dst_ip, interval = 10): 19 """ Start NAT-T keep alive on dut """ 20 21 ad.log.info("Starting NATT Keepalive") 22 status = None 23 24 key = ad.droid.connectivityStartNattKeepalive( 25 interval, src_ip, src_port, dst_ip) 26 27 ad.droid.connectivityNattKeepaliveStartListeningForEvent(key, "Started") 28 try: 29 event = ad.ed.pop_event("PacketKeepaliveCallback") 30 status = event["data"]["packetKeepaliveEvent"] 31 except Empty: 32 msg = "Failed to receive confirmation of starting natt keepalive" 33 asserts.fail(msg) 34 finally: 35 ad.droid.connectivityNattKeepaliveStopListeningForEvent( 36 key, "Started") 37 38 if status != "Started": 39 ad.log.error("Received keepalive status: %s" % status) 40 ad.droid.connectivityRemovePacketKeepaliveReceiverKey(key) 41 return None 42 return key 43 44def stop_natt_keepalive(ad, key): 45 """ Stop NAT-T keep alive on dut """ 46 47 ad.log.info("Stopping NATT keepalive") 48 status = False 49 ad.droid.connectivityStopNattKeepalive(key) 50 51 ad.droid.connectivityNattKeepaliveStartListeningForEvent(key, "Stopped") 52 try: 53 event = ad.ed.pop_event("PacketKeepaliveCallback") 54 status = event["data"]["packetKeepaliveEvent"] == "Stopped" 55 except Empty: 56 msg = "Failed to receive confirmation of stopping natt keepalive" 57 asserts.fail(msg) 58 finally: 59 ad.droid.connectivityNattKeepaliveStopListeningForEvent( 60 key, "Stopped") 61 62 ad.droid.connectivityRemovePacketKeepaliveReceiverKey(key) 63 return status 64