/// <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> /// Construct the option with what it needs to work /// </summary> /// <param name="name">The name of the option</param> /// <param name="title">The title of the option</param> /// <param name="description">A long description for the option</param> /// <param name="size">The size of the option</param> /// <param name="number">The number (i.e. index) of the option</param> /// <param name="type">The type of the option (string, bool, etc) /// </param> /// <param name="unit">The unit for the option (mm, etc, etc)</param> /// <param name="capabilities">A bit flag of the capabilities of /// the option</param> /// <param name="reloadFunction">A function to call if setting this /// option requires that all the options are reloaded</param> protected DeviceOption(string name, string title, string description, int size, int number, SaneType type, SaneUnit unit, SaneCapabilities capabilities, Action reloadFunction) { Number = number; Name = name; Title = title; Description = description; Size = size/4; Type = type; Unit = unit; Capabilities = capabilities; ReloadFunction = reloadFunction; _dy = new InnerValueDynamicObject(this); }
/// <summary> /// Construct the option with what it needs /// </summary> /// <param name="name">The name of the option</param> /// <param name="title">The title of the option</param> /// <param name="description">A long description for the option</param> /// <param name="size">The size of the option</param> /// <param name="number">The number (i.e. index) of the option</param> /// <param name="type">The type of the option (string, bool, etc) /// </param> /// <param name="unit">The unit for the option (mm, etc, etc)</param> /// <param name="capabilities">A bit flag of the capabilities of /// the option</param> /// <param name="handle">The handle to the device</param> /// <param name="reloadFunction">A function to call if setting this /// option requires that all the options are reloaded</param> internal LocalDeviceOption(string name, string title, string description, int size, int number, SaneType type, SaneUnit unit, SaneCapabilities capabilities, IntPtr handle, Action reloadFunction) : base(name, title, description, size, number, type, unit, capabilities, reloadFunction) { _handle = handle; }
/// <summary> /// Construct the option with what it needs to work /// </summary> /// <param name="name">The name of the option</param> /// <param name="title">The title of the option</param> /// <param name="description">A long description for the option</param> /// <param name="size">The size of the option</param> /// <param name="number">The number (i.e. index) of the option</param> /// <param name="type">The type of the option (string, bool, etc) /// </param> /// <param name="unit">The unit for the option (mm, etc, etc)</param> /// <param name="capabilities">A bit flag of the capabilities of /// the option</param> /// <param name="reloadFunction">A function to call if setting this /// option requires that all the options are reloaded</param> protected DeviceOption(string name, string title, string description, int size, int number, SaneType type, SaneUnit unit, SaneCapabilities capabilities, Action reloadFunction) { Number = number; Name = name; Title = title; Description = description; Size = size / 4; Type = type; Unit = unit; Capabilities = capabilities; ReloadFunction = reloadFunction; _dy = new InnerValueDynamicObject(this); }
/// <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); }