예제 #1
0
        public UsbDevice(string address)
        {
            _fd = Syscall.open( address, OpenFlags.O_NONBLOCK | OpenFlags.O_RDWR );

            Pollfd poll = new Pollfd();
            poll.fd = _fd;
            poll.events = PollEvents.POLLIN | PollEvents.POLLRDNORM;
            _poll = new [] { poll };

            _stream = new UnixStream(_fd);
            _lock = new object();
        }
예제 #2
0
 private bool IsDataAvailable(int timeout, out int pollResult)
 {
     var pollData = new Pollfd {
         fd = master,
         events = PollEvents.POLLIN
     };
     do
     {
         pollResult = Syscall.poll(new [] { pollData }, timeout);
     }
     while(!disposed && UnixMarshal.ShouldRetrySyscall(pollResult));
     return pollResult > 0;
 }
예제 #3
0
		protected override void OnOutput(BitwiseStream data)
		{
			if (Logger.IsDebugEnabled)
				Logger.Debug("\n\n" + Utilities.HexDump(data));

			long count = data.Length;
			//var buffer = new byte[MaxMTU];
			var buffer = new byte[CAN_MTU];
			int size = data.Read(buffer, 0, buffer.Length);

			Pollfd[] fds = new Pollfd[1];
			fds[0].fd = _socket.Handle;
			fds[0].events = PollEvents.POLLOUT;

			int expires = Environment.TickCount + Timeout;
			int wait = 0;

			for (;;)
			{
				try
				{
					wait = Math.Max(0, expires - Environment.TickCount);
					fds[0].revents = 0;

					int ret = Syscall.poll(fds, wait);

					if (UnixMarshal.ShouldRetrySyscall(ret))
						continue;

					UnixMarshal.ThrowExceptionForLastErrorIf(ret);

					if (ret == 0)
						throw new TimeoutException();

					if (ret != 1 || (fds[0].revents & PollEvents.POLLOUT) == 0)
						continue;

					//_socket.Write(buffer, 0, size);
					_socket.Write(buffer, 0, CAN_MTU);

					if (count != size)
						throw new Exception(string.Format("Only sent {0} of {1} byte packet.", size, count));

					return;
				}
				catch (Exception ex)
				{
					Logger.Error("Unable to send CAN packet to {0}. {1}", Interface, ex.Message);

					throw new SoftException(ex);
				}
			}
		}
예제 #4
0
		protected override void OnInput()
		{
			System.Diagnostics.Debug.Assert(_socket != null);

			if (_recvBuffer == null || _recvBuffer.Capacity < _bufferSize)
				_recvBuffer = new MemoryStream(_bufferSize);

			_recvBuffer.Seek(0, SeekOrigin.Begin);
			_recvBuffer.SetLength(_recvBuffer.Capacity);

			byte[] buf = _recvBuffer.GetBuffer();
			int offset = (int)_recvBuffer.Position;
			int size = (int)_recvBuffer.Length;

			Pollfd[] fds = new Pollfd[1];
			fds[0].fd = _socket.Handle;
			fds[0].events = PollEvents.POLLIN;

			int expires = Environment.TickCount + Timeout;
			int wait = 0;

			for (;;)
			{
				try
				{
					wait = Math.Max(0, expires - Environment.TickCount);
					fds[0].revents = 0;

					int ret = Syscall.poll(fds, wait);

					if (UnixMarshal.ShouldRetrySyscall(ret))
						continue;

					UnixMarshal.ThrowExceptionForLastErrorIf(ret);

					if (ret == 0)
						throw new TimeoutException();

					if (ret != 1 || (fds[0].revents & PollEvents.POLLIN) == 0)
						continue;

					var rxLen = _socket.Read(buf, offset, size);
					
					_recvBuffer.SetLength(rxLen);

					if (Logger.IsDebugEnabled)
						Logger.Debug("\n\n" + Utilities.HexDump(_recvBuffer));

					// Got a valid packet
					return;
				}
				catch (Exception ex)
				{
					Logger.Error("Unable to receive CAN packet on {0}. {1}", Interface, ex.Message);

					throw new SoftException(ex);
				}
			}
		}
예제 #5
0
		public static int poll (Pollfd [] fds, int timeout)
		{
			return poll (fds, (uint) fds.Length, timeout);
		}
예제 #6
0
		public static int poll (Pollfd [] fds, uint nfds, int timeout)
		{
			if (fds.Length < nfds)
				throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");

			_pollfd[] send = new _pollfd[nfds];

			for (int i = 0; i < send.Length; i++) {
				send [i].fd     = fds [i].fd;
				send [i].events = UnixConvert.FromPollEvents (fds [i].events);
			}

			int r = sys_poll (send, nfds, timeout);

			for (int i = 0; i < send.Length; i++) {
				fds [i].revents = UnixConvert.ToPollEvents (send [i].revents);
			}

			return r;
		}
예제 #7
0
파일: LibC.cs 프로젝트: rte-se/emul8
        public static byte[] ReadDataWithTimeout(int fd, int count, int timeout, Func<bool> shouldCancel)
        {
            int pollResult;
            var pollData = new Pollfd {
                fd = fd,
                events = PollEvents.POLLIN
            };

            do
            {
                pollResult = Syscall.poll(new [] { pollData }, timeout);
            }
            while(UnixMarshal.ShouldRetrySyscall(pollResult) && !shouldCancel());

            if(pollResult > 0)
            {
                return ReadData(fd, count);
            }
            else
            {
                return null;
            }
        }