Ejemplo n.º 1
0
        /// <summary>
        /// Performs the get parameters remote procedure call
        /// </summary>
        /// <param name="handle">The handle to the device</param>
        /// <param name="format">The returned format</param>
        /// <param name="lastFrame">The returned last frame value</param>
        /// <param name="bytesPerLine">The returned bytes per line</param>
        /// <param name="pixelsPerLine">The returned pixels per line</param>
        /// <param name="lines">The returned lines</param>
        /// <param name="depth">The returned color depth</param>
        private void GetParameters(int handle,
                                   out FrameFormat format,
                                   out bool lastFrame,
                                   out int bytesPerLine,
                                   out int pixelsPerLine,
                                   out int lines,
                                   out int depth)
        {
            _wire.SendCommand(NetworkCommand.GetParameters);
            _wire.SendWord(handle);

            int status = _wire.ReadWord();

            format        = (FrameFormat)_wire.ReadWord();
            lastFrame     = _wire.ReadWord() == 1;
            bytesPerLine  = _wire.ReadWord();
            pixelsPerLine = _wire.ReadWord();
            lines         = _wire.ReadWord();
            depth         = _wire.ReadWord();

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus(status);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Actually performs the open procedure call
        /// </summary>
        /// <param name="deviceName">The device to open</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>
        /// <returns>The handle to the open device</returns>
        private int DoOpenDevice(string deviceName,
                                 bool firstTime,
                                 string userName,
                                 string password)
        {
            _wire.SendCommand(NetworkCommand.Open);
            _wire.SendString(deviceName);

            int    status   = _wire.ReadWord();
            int    handle   = _wire.ReadWord();
            string resource = _wire.ReadString();

            if (!string.IsNullOrEmpty(resource) && firstTime)
            {
                Authorize(userName, password, resource);
                return(DoOpenDevice(deviceName, false, null, null));
            }

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus(status);
            }

            return(handle);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Opens the device
        /// </summary>
        /// <returns></returns>
        public override IOpenedDevice Open()
        {
            IntPtr handle;
            var    status = NativeMethods.SaneOpen(Name, out handle);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            _handle = handle;
            return(this);
        }
Ejemplo n.º 4
0
        protected override bool GetBooleanValue()
        {
            int  i      = 0;
            bool val    = false;
            var  status = NativeMethods.SaneControlOptionBoolean(
                _handle, Number, SaneOptionAction.Get, ref val, ref i);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            return(val);
        }
Ejemplo n.º 5
0
        protected override string GetStringValue()
        {
            int    i      = 0;
            string val    = string.Empty;
            var    status = NativeMethods.SaneControlOptionString(
                _handle, Number, SaneOptionAction.Get, ref val, ref i);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            return(val);
        }
Ejemplo n.º 6
0
        protected override int GetInt32Value()
        {
            int i      = 0;
            int val    = 0;
            var status = NativeMethods.SaneControlOptionInteger(
                _handle, Number, SaneOptionAction.Get, ref val, ref i);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            return(val);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="LocalConnection"/> class with
        /// credentials.
        /// </summary>
        public LocalConnection()
        {
            int ver;
            var status = NativeMethods.SaneInitialize(out ver, IntPtr.Zero);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            Version = ver;

            _open = true;
        }
Ejemplo n.º 8
0
        private IEnumerable <IOpenableDevice> LoadDevices()
        {
            IntPtr devicesPointer = IntPtr.Zero;
            var    status         = NativeMethods.SaneGetDevices(ref devicesPointer, true);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            var devs = PointerToDevices(devicesPointer);

            return(devs.ToList());
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Directly open a device
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IOpenedDevice OpenDevice(string name)
        {
            IntPtr handle;
            var    status = NativeMethods.SaneOpen(name, out handle);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            var device = new LocalDevice(name, handle);

            return(device);
        }
Ejemplo n.º 10
0
        protected override string SetValue(string value)
        {
            int i      = 0;
            var status = NativeMethods.SaneControlOptionStringBuilder(
                _handle, Number, SaneOptionAction.Set, new StringBuilder(value), ref i);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            var ret = GetStringValue();

            return(ret);
        }
Ejemplo n.º 11
0
        protected override bool SetValue(bool value)
        {
            int i      = 0;
            var status = NativeMethods.SaneControlOptionBoolean(
                _handle, Number, SaneOptionAction.Set, ref value, ref i);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            var ret = GetBooleanValue();

            return(ret);
        }
Ejemplo n.º 12
0
        public override void SetToAutomatic()
        {
            int        i = 0;
            SaneStatus status;

            if (Type == SaneType.String)
            {
                status = NativeMethods.SaneControlOptionStringBuilder(
                    _handle,
                    Number,
                    SaneOptionAction.Automatic,
                    new StringBuilder(string.Empty),
                    ref i);
            }
            else if (Type == SaneType.Boolean)
            {
                bool v = true;
                status = NativeMethods.SaneControlOptionBoolean(
                    _handle, Number, SaneOptionAction.Automatic, ref v, ref i);
            }
            else if (Type == SaneType.Integer)
            {
                int v = 0;
                status = NativeMethods.SaneControlOptionInteger(
                    _handle,
                    Number,
                    SaneOptionAction.Automatic,
                    ref v,
                    ref i);
            }
            else if (Type == SaneType.Fixed)
            {
                var val = 1.0.ToFixed(SaneFixedScaleShift);

                status = NativeMethods.SaneControlOptionInteger(
                    _handle, Number, SaneOptionAction.Automatic, ref val, ref i);
            }
            else
            {
                throw new NotSupportedException(
                          "Option type not supported");
            }

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }
        }
Ejemplo n.º 13
0
        protected override double GetFixedValue()
        {
            IntPtr pointer;
            int    i      = 0;
            var    status = NativeMethods.SaneControlOption(
                _handle, Number, 0, out pointer, ref i);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            var val = pointer.ToInt32().ToFloating(SaneFixedScaleShift);

            return(val);
        }
Ejemplo n.º 14
0
        protected override double SetValue(double value)
        {
            var val = value.ToFixed(SaneFixedScaleShift);
            int i   = 0;

            var status = NativeMethods.SaneControlOptionInteger(
                _handle, Number, SaneOptionAction.Set, ref val, ref i);

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus((int)status);
            }

            var ret = GetFixedValue();

            return(ret);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Calls the Init remote procedure
        /// </summary>
        /// <param name="userName">The username</param>
        /// <param name="versionCode">The version code</param>
        /// <returns>The version of the SANE daemon</returns>
        internal int Initialise(string userName, int versionCode)
        {
            _wire.SendCommand(NetworkCommand.Initialize);
            _wire.SendWord(versionCode);

            _wire.SendString(userName);
            int status = _wire.ReadWord();

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus(status);
            }

            // TODO: Convert the version to something more human
            int version = _wire.ReadWord();

            return(version);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Requests a list of devices
        /// </summary>
        /// <param name="userName">The username</param>
        /// <param name="password">The password</param>
        /// <returns>The device list</returns>
        internal IEnumerable <IOpenableDevice> RequestDeviceList(
            string userName,
            string password)
        {
            _wire.SendCommand(NetworkCommand.GetDevices);

            var status = _wire.ReadWord();

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus(status);
            }

            var ret = _wire
                      .ReadPointerArray((w, i) =>
                                        CreateDevice(w, userName, password));

            return(ret);
        }
Ejemplo n.º 17
0
        /// <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);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Actually performs the Start remote procedure call
        /// </summary>
        /// <param name="handle">The handle to the device</param>
        /// <param name="userName">The username</param>
        /// <param name="password">The password</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="stream">The stream to add onto (in the case of
        /// multiple calls for one image)</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <param name="bytesPerLine">The returned bytes per line</param>
        /// <param name="pixelsPerLine">The returned pixels per line</param>
        /// <param name="lines">The returned lines</param>
        /// <param name="depth">The returned color depth</param>
        /// <param name="littleEndian"><c>true</c> if the data is little
        /// endian</param>
        /// <param name="color"><c>true</c> if it is color</param>
        /// <returns>The final result of the scan</returns>
        private MemoryStream DoScan(int handle,
                                    string userName,
                                    string password,
                                    bool firstTime,
                                    MemoryStream stream,
                                    CancellationToken cancellationToken,
                                    out int bytesPerLine,
                                    out int pixelsPerLine,
                                    out int lines,
                                    out int depth,
                                    out bool littleEndian,
                                    out bool color)
        {
            FrameFormat format;
            bool        lastFrame;

            GetParameters(handle,
                          out format,
                          out lastFrame,
                          out bytesPerLine,
                          out pixelsPerLine,
                          out lines,
                          out depth);

            color = format != FrameFormat.Gray;
            cancellationToken.ThrowIfCancellationRequested();

            _wire.SendCommand(NetworkCommand.Start);
            _wire.SendWord(handle);

            int    status    = _wire.ReadWord();
            int    port      = _wire.ReadWord();
            int    byteOrder = _wire.ReadWord();
            string resource  = _wire.ReadString();

            littleEndian = byteOrder == 0x1234;

            cancellationToken.ThrowIfCancellationRequested();

            if (!string.IsNullOrEmpty(resource) && firstTime)
            {
                Authorize(userName, password, resource);
                return(DoScan(handle,
                              userName,
                              password,
                              false,
                              stream,
                              cancellationToken,
                              out bytesPerLine,
                              out pixelsPerLine,
                              out lines,
                              out depth,
                              out littleEndian,
                              out color));
            }

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus(status);
            }

            MemoryStream ret;

            if (!lastFrame)
            {
                ret = AddImageData(stream, format, port);
                return(DoScan(handle,
                              userName,
                              password,
                              false,
                              ret,
                              cancellationToken,
                              out bytesPerLine,
                              out pixelsPerLine,
                              out lines,
                              out depth,
                              out littleEndian,
                              out color));
            }

            ret = AddImageData(stream, format, port);
            return(ret);
        }