public ReconnectScenarioResult Execute()
        {
            try
            {
                bool pairingSucceeded = Pair();
                if (!pairingSucceeded)
                {
                    return(new ReconnectScenarioResult(false));
                }

                Disconnect();

                bool discoverySucceeded = Discover();
                if (!discoverySucceeded)
                {
                    return(new ReconnectScenarioResult(false));
                }

                Reconnect();

                WiFiDirectTestUtilities.RunDataPathValidation(localWFDController, remoteWFDController);
            }
            catch (Exception e)
            {
                WiFiDirectTestLogger.Error("Caught exception while executing reconnect scenario: {0}", e);
                return(new ReconnectScenarioResult(false));
            }

            return(new ReconnectScenarioResult(true));
        }
        public void ConnectASPOverExistingP2PGroup()
        {
            using (RemoteControllerLogGroup lg = new RemoteControllerLogGroup(remoteWFDController))
            {
                // Start by doing WFD pairing
                // NOTE: use invitation (not used for services) and config method push button (not used for services)
                ExecutePairingScenario(PairingScenarioType.Invitation, DOT11_WPS_CONFIG_METHOD.DOT11_WPS_CONFIG_METHOD_PUSHBUTTON);

                // Make sure we close this session on cleanup
                needToCloseSession = true;

                // Connect, auto accept
                string serviceName = remoteWFDController.GenerateUniqueServiceName();
                ServicesPublishDiscoverConnectParameters connectParams = new ServicesPublishDiscoverConnectParameters(
                    serviceName,
                    new ServicesConnectOptions(validateData: true)
                    );

                var pairResults = ExecutePublishDiscoverConnectScenario(connectParams);

                Verify.AreEqual(1, pairResults.ConnectResults.Count);

                // Disconnect
                ExecuteDisconnectScenario(
                    new ServicesDisconnectParameters(
                        pairResults.ConnectResults[0].AdvertiserSessionHandle,
                        pairResults.ConnectResults[0].SeekerSessionHandle
                        ),
                    false
                    );

                // WFD connection should stay up
                WiFiDirectTestLogger.Log("Closed ASP Session, waiting to make sure WFD session stays active...");
                Task.Delay(8000).Wait();

                WfdGlobalSessionState globalSessionStateLocal = localWFDController.QueryGlobalSessionState();

                if (globalSessionStateLocal.Sessions.Length != 1)
                {
                    WiFiDirectTestLogger.Error("Expected a single active session.  Current session count = {0}", globalSessionStateLocal.Sessions.Length);
                    throw new Exception("Local machine session is closed");
                }

                WiFiDirectTestUtilities.RunDataPathValidation(localWFDController, remoteWFDController, "6001");

                // Close WFD connection
                localWFDController.CloseSession();
                needToCloseSession = false;
            }
        }
        private void ExecuteInternal()
        {
            try
            {
                // For non-pushbutton pairing, generate a new passkey and make sure both test controllers have it.
                if (configMethod != DOT11_WPS_CONFIG_METHOD.DOT11_WPS_CONFIG_METHOD_PUSHBUTTON)
                {
                    remoteWFDController.PassKey = localWFDController.GenerateAndStoreNewPassKey();
                }

                // Prepare the remote device to receive the connection request.
                remoteWFDController.AcceptNextGroupRequest(
                    localWFDController.DeviceAddress,
                    (pairingScenarioType == PairingScenarioType.GoNegotiationDutBecomesGo) ? ((byte)0) : ((byte)14),
                    GetAcceptConfigMethod()
                    );

                // Begin by performing a targeted discovery for the remote device.
                bool discoverySucceeded = PerformTargetedDiscovery();
                if (!discoverySucceeded)
                {
                    return;
                }

                WFD_PAIR_WITH_DEVICE_PREFERENCE pairWithDevicePreference;

                switch (this.pairingScenarioType)
                {
                case PairingScenarioType.JoinExistingGo:
                    pairWithDevicePreference = WFD_PAIR_WITH_DEVICE_PREFERENCE.WFD_PAIRING_PREFER_NONE;
                    break;

                case PairingScenarioType.Invitation:
                    pairWithDevicePreference = WFD_PAIR_WITH_DEVICE_PREFERENCE.WFD_PAIRING_PREFER_INVITATION;
                    break;

                case PairingScenarioType.GoNegotiationDutBecomesGo:
                case PairingScenarioType.GoNegotiationDutBecomesClient:
                    pairWithDevicePreference = WFD_PAIR_WITH_DEVICE_PREFERENCE.WFD_PAIRING_PREFER_GO_NEGOTIATION;
                    break;

                default:
                    throw new Exception("Cannot map pairing scenario to pairing preference.");
                }

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                // Pair with the target device.
                WiFiDirectTestLogger.Log("Starting pairing with device {0} ({1})", remoteWFDController.DeviceAddress, remoteWFDController.MachineName);
                localWFDController.PairWithDevice(
                    remoteWFDController.DeviceAddress,
                    pairWithDevicePreference,
                    (pairingScenarioType == PairingScenarioType.GoNegotiationDutBecomesClient) ? ((byte)0) : ((byte)14),
                    configMethod,
                    isPersistent
                    );
                WiFiDirectTestLogger.Log("Pairing successful", remoteWFDController.DeviceAddress, remoteWFDController.MachineName);

                stopwatch.Stop();
                WiFiDirectTestLogger.Log("Pairing completed in {0} ms.", stopwatch.ElapsedMilliseconds);

                bool sessionStateValid = VerifySessionState();
                if (!sessionStateValid)
                {
                    return;
                }

                if (this.runDataPathValidation)
                {
                    WiFiDirectTestUtilities.RunDataPathValidation(localWFDController, remoteWFDController);
                }

                succeeded = true;
            }
            catch (Exception e)
            {
                WiFiDirectTestLogger.Error("Caught exception while executing pairing scenario: {0}", e);
            }
        }