• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5from autotest_lib.client.common_lib import error
6
7class 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, instance=0):
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        @param instance: int hostapd instance on router to send frames from.
26        """
27        if router.board == "panther":
28            raise error.TestNAError('Panther router does not support manual '
29                                    'beacon frame generation')
30        self._router = router
31        self._channel = channel
32        self._frame_type = frame_type
33        self._ssid_prefix = ssid_prefix
34        self._num_bss = num_bss
35        self._frame_count = frame_count
36        self._delay = delay
37        self._dest_addr = dest_addr
38        self._probe_resp_footer = probe_resp_footer
39        self._ap_interface = router.hostapd_instances[instance].interface
40        self._injection_interface = None
41        self._pid = None
42
43
44    def __enter__(self):
45        self._injection_interface = self._router.get_configured_interface(
46            'monitor', same_phy_as=self._ap_interface)
47        self._pid = self._router.send_management_frame(
48            self._injection_interface,
49            self._frame_type, self._channel, ssid_prefix=self._ssid_prefix,
50            num_bss=self._num_bss, frame_count=self._frame_count,
51            delay=self._delay, dest_addr=self._dest_addr,
52            probe_resp_footer=self._probe_resp_footer)
53        return self
54
55
56    def __exit__(self, exception, value, traceback):
57        if self._injection_interface:
58            self._router.release_interface(self._injection_interface)
59        if self._pid:
60            self._router.host.run('kill %d' % self._pid, ignore_status=True)
61