def test_full_tether_startup(self):
        """Test full startup of wifi tethering

        1. Report current state.
        2. Switch to AP mode.
        3. verify SoftAP active.
        4. Shutdown wifi tethering.
        5. verify back to previous mode.
        """
        success = True
        initial_wifi_state = self.dut.droid.wifiCheckState()
        initial_cell_state = is_sim_ready(self.log, self.dut)
        self.log.info("current state: %s", initial_wifi_state)
        self.log.info("is sim ready? %s", initial_cell_state)

        if initial_cell_state:
            self.check_cell_data_and_enable()

        config = self.create_softap_config()

        success = wutils.start_wifi_tethering(self.dut,
                                              config[wutils.WifiEnums.SSID_KEY],
                                              config[wutils.WifiEnums.PWD_KEY])
        asserts.assert_true(success, "call to start_wifi_tethering returned false.  Check config")
        self.confirm_softap_in_scan_results(config[wutils.WifiEnums.SSID_KEY])
        wutils.stop_wifi_tethering(self.dut)
        asserts.assert_true(not self.dut.droid.wifiIsApEnabled(),
                            "SoftAp is still reported as running")
        if initial_wifi_state:
            self.verify_return_to_wifi_enabled()
        elif not self.dut.droid.wifiCheckState():
            asserts.fail("Wifi was disabled before softap and now it is enabled")
 def check_project_fi_activated(self, ad, retries=20):
     for _ in range(retries):
         if is_sim_ready(self.log,
                         ad) and (ad.droid.telephonyGetSimOperatorName()
                                  == "Fi Network"):
             ad.log.info("SIM state is READY, SIM operator is Fi")
             return True
         time.sleep(5)
    def validate_full_tether_startup(self,
                                     band=None,
                                     hidden=None,
                                     test_ping=False,
                                     test_clients=None):
        """Test full startup of wifi tethering

        1. Report current state.
        2. Switch to AP mode.
        3. verify SoftAP active.
        4. Shutdown wifi tethering.
        5. verify back to previous mode.
        """
        initial_wifi_state = self.dut.droid.wifiCheckState()
        initial_cell_state = tel_utils.is_sim_ready(self.log, self.dut)
        self.dut.log.info("current state: %s", initial_wifi_state)
        self.dut.log.info("is sim ready? %s", initial_cell_state)
        if initial_cell_state:
            self.check_cell_data_and_enable()
        config = self.create_softap_config()
        wutils.start_wifi_tethering(self.dut,
                                    config[wutils.WifiEnums.SSID_KEY],
                                    config[wutils.WifiEnums.PWD_KEY], band,
                                    hidden)
        if hidden:
            # First ensure it's not seen in scan results.
            self.confirm_softap_not_in_scan_results(
                config[wutils.WifiEnums.SSID_KEY])
            # If the network is hidden, it should be saved on the client to be
            # seen in scan results.
            config[wutils.WifiEnums.HIDDEN_KEY] = True
            ret = self.dut_client.droid.wifiAddNetwork(config)
            asserts.assert_true(ret != -1, "Add network %r failed" % config)
            self.dut_client.droid.wifiEnableNetwork(ret, 0)
        self.confirm_softap_in_scan_results(config[wutils.WifiEnums.SSID_KEY])
        if test_ping:
            self.validate_ping_between_softap_and_client(config)
        if test_clients:
            if hasattr(self, 'arduino_wifi_dongles'):
                self.validate_traffic_between_softap_clients(config)
            if len(self.android_devices) > 2:
                self.validate_ping_between_two_clients(config)
        wutils.stop_wifi_tethering(self.dut)
        asserts.assert_false(self.dut.droid.wifiIsApEnabled(),
                             "SoftAp is still reported as running")
        if initial_wifi_state:
            wutils.wait_for_wifi_state(self.dut, True)
        elif self.dut.droid.wifiCheckState():
            asserts.fail(
                "Wifi was disabled before softap and now it is enabled")
    def test_switch_to_softap_mode(self):
        """Test switch to softap mode.

         1. Report current state.
         2. Switch to AP mode.
         3. Switch back to previous state.
        """
        success = True
        initial_wifi_state = self.dut.droid.wifiCheckState()
        initial_cell_state = is_sim_ready(self.log, self.dut)
        self.log.info("current state: %s", initial_wifi_state)
        self.log.info("is sim ready? %s", initial_cell_state)
        if initial_cell_state:
            self.log.info("cell is on")
            self.check_cell_data_and_enable()

        config = self.create_softap_config()
        try:
            self.dut.droid.wifiStartTrackingTetherStateChange()
            success = self.dut.droid.wifiSetApEnabled(True, config)
            asserts.assert_true(success, "switch to softap mode failed")
            curr_state = "waiting for WifiManagerApEnabled"
            event = self.dut.ed.pop_event("WifiManagerApEnabled", 5)
            self.confirm_softap_in_scan_results(config[wutils.WifiEnums.SSID_KEY])
            success = self.dut.droid.wifiSetApEnabled(False, None)
            asserts.assert_true(success, "turn off softap mode failed")
            asserts.assert_true(not self.dut.droid.wifiIsApEnabled(),
                                "SoftAp is still reported as running")
            curr_state = "waiting for WifiManagerApDisabled"
            event = self.dut.ed.pop_event("WifiManagerApDisabled", 5)
            if initial_wifi_state:
                self.verify_return_to_wifi_enabled()
            elif not self.dut.droid.wifiCheckState():
                # really need to verify that wifi did not come back up, and will not
                # TODO(silberst): look at alternatives to this simple check
                asserts.fail("Wifi was disabled before softap and now it is enabled")
        except queue.Empty:
            self.log.exception("Failed to fully start/stop softap mode: current state = %s",
                               curr_state)
        finally:
            #self.dut.droid.wifiStopTrackingTetherStateChange()
            self.dut.droid.wifiStopTrackingTetherStateChange()
            self.dut_client.droid.wifiStopTrackingStateChange()