1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 from autotest_lib.server.cros.network import hostap_config 6 7 class FrameSender(object): 8 """Context manager for sending management frames.""" 9 10 11 def __init__(self, router, frame_type, channel, ssid_prefix=None, 12 num_bss=None, frame_count=None, delay=None, dest_addr=None, 13 probe_resp_footer=None): 14 """ 15 @param router: LinuxRouter object router to send frames from. 16 @param frame_type: int management frame type. 17 @param channel: int targeted channel. 18 @param ssid_prefix: string SSID prefix for BSSes in the frames. 19 @param num_bss: int number of BSSes configured for sending frames. 20 @param frame_count: int number of frames to send, frame_count of 0 21 implies infinite number of frames. 22 @param delay: int delay in between frames in milliseconds. 23 @param dest_addr: MAC address of the destination address (DA). 24 @param probe_resp_footer: footer bytes for probe responses. 25 """ 26 self._router = router 27 self._channel = channel 28 self._frequency = ( 29 hostap_config.HostapConfig.get_frequency_for_channel(channel)) 30 self._frame_type = frame_type 31 self._ssid_prefix = ssid_prefix 32 self._num_bss = num_bss 33 self._frame_count = frame_count 34 self._delay = delay 35 self._dest_addr = dest_addr 36 self._probe_resp_footer = probe_resp_footer 37 self._ap_interface = None 38 self._injection_interface = None 39 self._pid = None 40 41 42 def __enter__(self): 43 # Some chips don't allow beacon or probe response injection, 44 # unless an AP mode interface is present. 45 self._ap_interface = self._router.get_configured_interface( 46 '__ap', frequency=self._frequency) 47 self._injection_interface = self._router.get_configured_interface( 48 'monitor', same_phy_as=self._ap_interface) 49 self._pid = self._router.send_management_frame( 50 self._injection_interface, 51 self._frame_type, self._channel, ssid_prefix=self._ssid_prefix, 52 num_bss=self._num_bss, frame_count=self._frame_count, 53 delay=self._delay, dest_addr=self._dest_addr, 54 probe_resp_footer=self._probe_resp_footer) 55 return self 56 57 58 def __exit__(self, exception, value, traceback): 59 if self._injection_interface: 60 self._router.release_interface(self._injection_interface) 61 self._router.release_interface(self._ap_interface) 62 if self._pid: 63 self._router.host.run('kill %d' % self._pid, ignore_status=True) 64