/// <summary> /// Calls the control remote procedure call to adjust the value of the /// given device option /// </summary> /// <typeparam name="T">The type of option</typeparam> /// <param name="handle">The handle to the device</param> /// <param name="option">The option index</param> /// <param name="action">The action to perform</param> /// <param name="type">The type of the option</param> /// <param name="size">The size of the option</param> /// <param name="sender">An action to deal with serializing the /// sending data</param> /// <param name="userName">The username</param> /// <param name="password">The password</param> /// <param name="reloadFunction">The reload function</param> /// <returns>The result of the set/get operation</returns> internal T ControlOption <T>(int handle, int option, SaneOptionAction action, SaneType type, int size, Func <NetworkMethods, int, int> sender, string userName, string password, Action reloadFunction) { return(DoControlOption <T>(handle, option, action, type, size, sender, true, userName, password, reloadFunction)); }
/// <summary> /// Just a little helper method to save the number of arguments we are /// passing around /// </summary> /// <typeparam name="T">The otpion type</typeparam> /// <param name="sender">The sender action (i.e. the thing that ///converts the object to its network specific binary data)</param> /// <param name="action">The action to perform</param> /// <returns>The result of the control call</returns> private T Control <T>(Func <NetworkMethods, int, int> sender, SaneOptionAction action) { if (action == SaneOptionAction.Automatic && !IsAutomaticAllowed) { throw new InvalidOperationException( "You cannot set this option to automatic"); } if (action == SaneOptionAction.Set) { if (!IsActive) { throw new InvalidOperationException( "You cannot set this option as it is inactive"); } if (!IsSettable) { throw new InvalidOperationException( "You cannot set this option as it is read-only"); } } var ret = _caller.ControlOption <T>(_handle, Number, action, Type, Size, sender, _userName, _password, ReloadFunction); return(ret); }
/// <summary> /// Actually performs the control option remote procedure call /// </summary> /// <typeparam name="T">The type of option</typeparam> /// <param name="handle">The handle to the device</param> /// <param name="option">The option index</param> /// <param name="action">The action to perform</param> /// <param name="type">The type of the option</param> /// <param name="size">The size of the option</param> /// <param name="sender">An action to deal with serializing the /// sending data</param> /// <param name="firstTime"><c>true</c> if this is the first attempt /// (in which case it will retry with the credentials)</param> /// <param name="userName">The username</param> /// <param name="password">The password</param> /// <param name="reloadFunction">This function is called if /// the options require reloading</param> /// <returns>The result of the operation</returns> private T DoControlOption <T>(int handle, int option, SaneOptionAction action, SaneType type, int size, Func <NetworkMethods, int, int> sender, bool firstTime, string userName, string password, Action reloadFunction) { _wire.SendCommand(NetworkCommand.ControlOption); _wire.SendWord(handle); _wire.SendWord(option); _wire.SendWord((int)action); _wire.SendWord((int)type); int sze = sender(_wire, size); int status = _wire.ReadWord(); var info = (SaneOptionInformation)_wire.ReadWord(); var valueType = (SaneType)_wire.ReadWord(); int valueSize = _wire.ReadWord(); object ret; switch (type) { case SaneType.Boolean: _wire.ReadWord(); ret = _wire.ReadWord() == 1; break; case SaneType.Integer: _wire.ReadWord(); ret = _wire.ReadWord(); break; case SaneType.String: ret = _wire.ReadString(); break; case SaneType.Fixed: _wire.ReadWord(); ret = _wire.ReadWord(); break; case SaneType.Button: ret = _wire.ReadWord() == 1; break; default: throw new NotSupportedException( "Option type not supported"); } string resource = _wire.ReadString(); if (valueType != type) { throw new InvalidOperationException( "Something has gone horribly wrong here - the returned " + "type is different to the passed type!"); } if (valueSize != sze) { throw new InvalidOperationException( "Something has gone horribly wrong here - the returned " + "size is different to the passed type!"); } if (!string.IsNullOrEmpty(resource) && firstTime) { Authorize(userName, password, resource); return(DoControlOption <T>(handle, option, action, type, size, sender, false, userName, password, reloadFunction)); } if (status != (int)SaneStatus.Success) { throw NSaneException.CreateFromStatus(status); } // OK, we need to reload the options now... if ((info & SaneOptionInformation.ReloadOptions) == SaneOptionInformation.ReloadOptions) { reloadFunction(); } return((T)ret); }
public extern static SaneStatus SaneControlOption2(IntPtr handle, int n, SaneOptionAction a, ref IntPtr v, ref int i);
public extern static SaneStatus SaneControlOptionBoolean(IntPtr handle, int n, SaneOptionAction a, [MarshalAs(UnmanagedType.Bool)] ref bool v, ref int i);
public extern static SaneStatus SaneControlOption(IntPtr handle, int n, SaneOptionAction a, StringBuilder v, ref int i);
public extern static SaneStatus SaneControlOptionString(IntPtr handle, int n, SaneOptionAction a, ref string v, ref int i);
public static extern SaneStatus SaneControlOption(IntPtr handle, int n, SaneOptionAction a, StringBuilder v, ref int i);
public static extern SaneStatus SaneControlOption(IntPtr handle, int n, SaneOptionAction a, ref float v, ref int i);
public static extern SaneStatus SaneControlOptionString(IntPtr handle, int n, SaneOptionAction a, ref string v, ref int i);
public static extern SaneStatus SaneControlOptionBoolean(IntPtr handle, int n, SaneOptionAction a, [MarshalAs(UnmanagedType.Bool)] ref bool v, ref int i);