/// <summary> /// Attempts to start a new session. /// </summary> /// <param name="pairingRecord"> /// The pairing record used to authenticate the host with the device. /// </param> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation. /// </param> /// <returns> /// The response to the request to start a new session. /// </returns> public async Task <StartSessionResponse> TryStartSessionAsync(PairingRecord pairingRecord, CancellationToken cancellationToken) { if (pairingRecord == null) { throw new ArgumentNullException(nameof(pairingRecord)); } await this.protocol.WriteMessageAsync( new StartSessionRequest() { Label = this.Label, HostID = pairingRecord.HostId, Request = "StartSession", SystemBUID = pairingRecord.SystemBUID, }, cancellationToken).ConfigureAwait(false); var message = await this.protocol.ReadMessageAsync <StartSessionResponse>(cancellationToken).ConfigureAwait(false); if (message.EnableSessionSSL) { await this.protocol.EnableSslAsync(pairingRecord, cancellationToken).ConfigureAwait(false); } return(message); }
/// <summary> /// Starts a new exception, and throws an exception when a new session could not be created. /// </summary> /// <param name="pairingRecord"> /// The pairing record used to authenticate the host with the device. /// </param> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation. /// </param> /// <returns> /// The response to the request to start a new session. /// </returns> public virtual async Task <StartSessionResponse> StartSessionAsync(PairingRecord pairingRecord, CancellationToken cancellationToken) { var message = await this.TryStartSessionAsync(pairingRecord, cancellationToken).ConfigureAwait(false); this.EnsureSuccess(message); return(message); }
/// <summary> /// Asynchronously sends a request to pair a host with the device. /// </summary> /// <param name="pairingRecord"> /// A pairing record which represents the pairing request. /// </param> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous /// operation. /// </param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public virtual Task <PairingResult> PairAsync(PairingRecord pairingRecord, CancellationToken cancellationToken) { return(this.PairAsync( "Pair", pairingRecord, new PairingOptions() { ExtendedPairingErrors = true, }, cancellationToken)); }
/// <summary> /// Asynchronously sends a request to validate a pairing record. /// </summary> /// <param name="pairingRecord"> /// A pairing record which represents the pairing record to validate. /// </param> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous /// operation. /// </param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public virtual async Task <bool> ValidatePairAsync(PairingRecord pairingRecord, CancellationToken cancellationToken) { var result = await this.TryStartSessionAsync(pairingRecord, cancellationToken); if (result.Error != null) { return(false); } else { await this.StopSessionAsync(result.SessionID, cancellationToken).ConfigureAwait(false); return(true); } }
private async Task <PairingResult> PairAsync(string request, PairingRecord pairingRecord, PairingOptions options, CancellationToken cancellationToken) { if (pairingRecord == null) { throw new ArgumentNullException(nameof(pairingRecord)); } var pairRequest = new PairRequest() { Request = request, PairRecord = pairingRecord, PairingOptions = options, Label = this.Label, }; await this.protocol.WriteMessageAsync( pairRequest, cancellationToken).ConfigureAwait(false); var message = await this.protocol.ReadMessageAsync <PairResponse>( cancellationToken); if (message == null) { return(null); } if (message.Error == null) { return(new PairingResult() { Status = PairingStatus.Success, EscrowBag = message.EscrowBag, }); } else if (Enum.IsDefined((PairingStatus)message.Error.Value)) { return(new PairingResult() { Status = (PairingStatus)message.Error.Value, }); } else { throw new LockdownException(message.Error.Value); } }
/// <summary> /// Asynchronously sends a request to unpair a host with the device. /// </summary> /// <param name="pairingRecord"> /// A pairing record which represents the paired host. /// </param> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> which can be used to cancel teh asynchronous /// operation. /// </param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public virtual Task <PairingResult> UnpairAsync(PairingRecord pairingRecord, CancellationToken cancellationToken) { return(this.PairAsync("Unpair", pairingRecord, null, cancellationToken)); }
/// <summary> /// Asynchronously saves the pairing record for a device. /// </summary> /// <param name="udid"> /// The UDID of the device for which to store the pairing record. /// </param> /// <param name="pairingRecord"> /// The pairing record to store. /// </param> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation. /// </param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public abstract Task WriteAsync(string udid, PairingRecord pairingRecord, CancellationToken cancellationToken);