Ejemplo n.º 1
0
        public async Task SendBytes(byte[] message)
        {
            HidOutputReport outReport = _device.CreateOutputReport(0);

            DataWriter dataWriter = new DataWriter();

            dataWriter.WriteBytes(message);

            if (message.Length < outReport.Data.Capacity)
            {
                int repeat = ((int)outReport.Data.Capacity) - message.Length;

                IEnumerable <byte> bytes = Enumerable.Repeat <byte>(0, repeat);
                dataWriter.WriteBytes(bytes.ToArray());
            }
            outReport.Data = dataWriter.DetachBuffer();
            try
            {
                await _device.SendOutputReportAsync(outReport);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
                throw;
            }
        }
Ejemplo n.º 2
0
        public static void imm_FormatMetaUTC(HidOutputReport report, bool padWithEmpty)
        {
            byte[] raw = new byte[USB_PKT_SIZE + 1];
            raw[0] = 0;
            raw[1] = (YGenericHub.YSTREAM_META << 3);
            raw[2] = (byte)(YPKT_STREAM + (YGenericHub.USB_META_UTCTIME_SIZE << 2));
            raw[3] = YGenericHub.USB_META_UTCTIME;
            double totalSeconds = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            UInt32 currUtcTime  = (UInt32)totalSeconds;

            raw[4] = (byte)(currUtcTime & 0xff);
            raw[5] = (byte)((currUtcTime >> 8) & 0xff);
            raw[6] = (byte)((currUtcTime >> 16) & 0xff);
            raw[7] = (byte)((currUtcTime >> 24) & 0xff);
            // 1/250 seconds
            double val = (totalSeconds * 250) % 250;

            raw[8] = (byte)((uint)val & 0xff);
            if (padWithEmpty)
            {
                raw[9]  = 0 + (YGenericHub.YSTREAM_EMPTY << 3);
                raw[10] = (byte)(YPKT_STREAM + ((55) << 2));
            }

            report.Data = raw.AsBuffer();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Broken. Due to an unknown glitch in the output report's data cannot operate at all.
        /// </summary>
        /// <param name="reportID">One of the Wiimotes known reportID identifiers</param>
        /// <param name="commands">A list of 1 or more command parameters</param>
        /// <returns>Succesful or not</returns>
        public async Task <bool> WriteWiiMoteAsync(WiiMoteReportID reportID, WiiMoteCommand[] commands)
        {
            //Create report
            HidOutputReport outReport = device.CreateOutputReport();
            //Create data
            var writer = new DataWriter();

            writer.WriteByte((byte)prefix);
            writer.WriteByte((byte)reportID);
            for (int i = 0; i < 20 - commands.Length; i++)
            {
                writer.WriteByte(0);
            }
            foreach (WiiMoteCommand c in commands)
            {
                writer.WriteByte((byte)c);
            }
            outReport.Data = writer.DetachBuffer();
            //Send data
            try {
                uint result = await device.SendOutputReportAsync(outReport);
            }
            catch (Exception)
            {
                System.Diagnostics.Debug.WriteLine("Failed to send report");
                return(false);
            }
            return(true);
        }
Ejemplo n.º 4
0
        internal async Task Setup(uint pktVersion)
        {
            _currentTask = new TaskCompletionSource <bool>();

            // construct a HID output report to send to the device
            HidOutputReport outReport = Hid.CreateOutputReport();

            YUSBPkt.imm_FormatConfReset(outReport, pktVersion);
            // Send the output report asynchronously
            _devState = DevState.ResetSend;
            var u = await Hid.SendOutputReportAsync(outReport);

            if (u != 65)
            {
                _devState = DevState.IOError;
                throw new YAPI_Exception(YAPI.IO_ERROR, "Unable to send Reset PKT");
            }

            Task <bool> task     = _currentTask.Task;
            Task        taskDone = await Task.WhenAny(task, Task.Delay(1000));

            if (taskDone != task)
            {
                throw new YAPI_Exception(YAPI.IO_ERROR, "Device does not respond to reset");
            }
        }
Ejemplo n.º 5
0
        private async Task SendOutputMessage(byte[] message)
        {
            if (_hidDevice != null)
            {
                HidOutputReport report = _hidDevice.CreateOutputReport();
                report.Data = message.AsBuffer();

                await _hidDevice.SendOutputReportAsync(report);
            }
        }
Ejemplo n.º 6
0
 public static void imm_FormatConfStart(HidOutputReport report, byte nbinface, byte pktAckDelay)
 {
     byte[] raw = new byte[USB_PKT_SIZE + 1];
     raw[0]      = 0;
     raw[1]      = 0 + (USB_CONF_START << 3);
     raw[2]      = YPKT_CONF + (USB_MAX_PKT_CONTENT_SIZE << 2);
     raw[3]      = nbinface;    // nbifac
     raw[4]      = pktAckDelay; // ifaceno
     report.Data = raw.AsBuffer();
 }
        private async Task WriteAsyncInternal(byte[] data)
        {
            if (_hidDevice == null)
            {
                return;
            }

            HidOutputReport report = _hidDevice.CreateOutputReport();

            data.CopyTo(0, report.Data, 1, data.Length);
            await _hidDevice.SendOutputReportAsync(report);
        }
Ejemplo n.º 8
0
        public static void imm_FormatConfReset(HidOutputReport report, uint api_version)
        {
            byte[] raw = new byte[USB_PKT_SIZE + 1];
            raw[0] = 0;
            raw[1] = 0 + (USB_CONF_RESET << 3);
            raw[2] = YPKT_CONF + (USB_MAX_PKT_CONTENT_SIZE << 2);
            raw[3] = (byte)(api_version & 0xff);
            raw[4] = (byte)(api_version >> 8);
            raw[5] = 1; // nbifac
            raw[6] = 0; // ifaceno
            raw[7] = 1; // nbifac
            IBuffer buf = raw.AsBuffer();

            report.Data = buf;
        }
Ejemplo n.º 9
0
        internal async void SendReport(IBuffer buffer)
        {
            byte[] parameters = buffer.ToArray();

            HidOutputReport report = device.CreateOutputReport();

            Array.Resize(ref parameters, (int)report.Data.Length);
            report.Data = parameters.AsBuffer();

            now = DateTime.Now;
            await device.SendOutputReportAsync(report);

            int ms = DateTime.Now.Subtract(now).Milliseconds;

            Debug.WriteLine("TX - {0} bytes sent in {1}ms.", report.Data.Length.ToString(), ms);
        }
Ejemplo n.º 10
0
        public async void SendReport()
        {
            byte[] parameters = { 0, 85, 85, 9, (int)RobotCommand.BusServoInfoRead, 6, 1, 2, 3, 4, 5, 6 };

            HidOutputReport report = device.CreateOutputReport();

            Array.Resize(ref parameters, (int)report.Data.Length);
            report.Data = parameters.AsBuffer();

            now = DateTime.Now;
            await device.SendOutputReportAsync(report);

            int ms = DateTime.Now.Subtract(now).Milliseconds;

            Debug.WriteLine("TX - {0} bytes sent in {1}ms.", report.Data.Length.ToString(), ms);
        }
Ejemplo n.º 11
0
        internal async Task Start(byte pktAckDelay)
        {
            // construct a HID output report to send to the device
            HidOutputReport outReport = Hid.CreateOutputReport();

            //("Activate USB pkt ack (%dms)\n", dev->pktAckDelay);
            YUSBPkt.imm_FormatConfStart(outReport, 1, pktAckDelay);
            // Send the output report asynchronously
            _devState = DevState.StartSend;
            var u = await Hid.SendOutputReportAsync(outReport);

            if (u != 65)
            {
                _devState = DevState.IOError;
                throw new YAPI_Exception(YAPI.IO_ERROR, "Unable to send Start PKT");
            }
        }
Ejemplo n.º 12
0
        private async Task checkMetaUTC()
        {
            if (_lastMetaUTC + META_UTC_DELAY < YAPI.GetTickCount())
            {
                HidOutputReport outReport = Hid.CreateOutputReport();
                YUSBPkt.imm_FormatMetaUTC(outReport, true);
                var u = await Hid.SendOutputReportAsync(outReport);

                if (u != 65)
                {
                    _devState = DevState.IOError;
                    throw new YAPI_Exception(YAPI.IO_ERROR, "Unable to send Start PKT");
                }

                _lastMetaUTC = YAPI.GetTickCount();
            }
        }
Ejemplo n.º 13
0
        public static int imm_FormatTCP(HidOutputReport outReport, byte[] request, int pos, bool padWithEmpty)
        {
            int size, streamType, remaining;

            if (request != null)
            {
                int toWrite = request.Length - pos;
                streamType = YGenericHub.YSTREAM_TCP;
                if (toWrite < USB_MAX_PKT_CONTENT_SIZE)
                {
                    size      = toWrite;
                    remaining = USB_PKT_SIZE - size - 2;
                }
                else
                {
                    size      = USB_MAX_PKT_CONTENT_SIZE;
                    remaining = 0;
                }
            }
            else
            {
                streamType = YGenericHub.YSTREAM_TCP_CLOSE;
                size       = 0;
                remaining  = USB_PKT_SIZE - 2;
            }

            byte[] raw = new byte[USB_PKT_SIZE + 1];
            raw[0] = 0;
            raw[1] = (byte)(streamType << 3);
            raw[2] = (byte)(YPKT_STREAM + (size << 2));
            if (size > 0)
            {
                Buffer.BlockCopy(request, pos, raw, 3, size);
            }

            if (remaining > 2 && padWithEmpty)
            {
                raw[3 + size] = 0 + (YGenericHub.YSTREAM_EMPTY << 3);
                raw[4 + size] = (byte)(YPKT_STREAM + ((remaining - 2) << 2));
            }

            outReport.Data = raw.AsBuffer();
            return(size);
        }
Ejemplo n.º 14
0
        private async Task sendRequest(byte[] request, YGenericHub.RequestAsyncResult asyncResult, object asyncContext)
        {
            int pos = 0;

            try {
                if (_currentRequest != null)
                {
                    await _currentRequest.GetResponse();
                }

                //Debug.WriteLine(string.Format("{0}:Check last request is sent", Environment.CurrentManagedThreadId));
                _currentRequest = new YRequest(request, asyncResult, asyncContext, 10000);
                while (pos < request.Length)
                {
                    // construct a HID output report to send to the device
                    HidOutputReport outReport = Hid.CreateOutputReport();
                    int             size      = YUSBPkt.imm_FormatTCP(outReport, request, pos, true);
                    // Send the output report asynchronously
                    var u = await Hid.SendOutputReportAsync(outReport);

                    if (u != 65)
                    {
                        _devState = DevState.IOError;
                        _watcher.imm_removeUsableDevice(this);
                        return;
                    }

                    pos += size;
                }

                //Debug.WriteLine(string.Format("{0}:sent", Environment.CurrentManagedThreadId));
            } catch (Exception ex) {
                _devState       = DevState.IOError;
                _currentRequest = null;
                throw new YAPI_Exception(YAPI.IO_ERROR, ex.Message);
            }
        }
Ejemplo n.º 15
0
        public void Write(byte[] buffer, uint cbToWrite)
        {
            if (buffer[0] != 0)
            {
                buffer = AddToBegining(buffer, 0);
            }
            if (buffer.Length < cbToWrite)
            {
                byte[] temp = new byte[cbToWrite];
                for (int x = 0; x < buffer.Length; x++)
                {
                    temp[x] = buffer[x];
                }
                for (int x = buffer.Length; x < cbToWrite; x++)
                {
                    temp[x] = 0;
                }
                buffer = temp;
            }
            HidOutputReport y = NativeDevice.CreateOutputReport();

            y.Data = buffer.AsBuffer();
            _      = NativeDevice.SendOutputReportAsync(y);
        }
Ejemplo n.º 16
0
        internal async Task streamHandler(List <YPktStreamHead> streams)
        {
            foreach (YPktStreamHead s in streams)
            {
                uint streamType = s.StreamType;
                switch (streamType)
                {
                case YGenericHub.YSTREAM_NOTICE:
                case YGenericHub.YSTREAM_NOTICE_V2:
                    imm_handleNotifcation(s);
                    break;

                case YGenericHub.YSTREAM_TCP_CLOSE:
                case YGenericHub.YSTREAM_TCP:
                    if (_devState != DevState.StreamReadyReceived || _currentRequest == null)
                    {
                        continue;
                    }

                    _currentRequest.imm_AddIncommingData(s);
                    if (streamType == YGenericHub.YSTREAM_TCP_CLOSE)
                    {
                        // construct a HID output report to send to the device
                        HidOutputReport outReport = Hid.CreateOutputReport();
                        YUSBPkt.imm_FormatTCP(outReport, null, 0, true);
                        // Send the output report asynchronously
                        var u = await Hid.SendOutputReportAsync(outReport);

                        if (u != 65)
                        {
                            _devState = DevState.IOError;
                            _watcher.imm_removeUsableDevice(this);
                            return;
                        }

                        _currentRequest.imm_Close();
                    }

                    break;

                case YGenericHub.YSTREAM_EMPTY:
                    break;

                case YGenericHub.YSTREAM_REPORT:
                    if (_devState == DevState.StreamReadyReceived)
                    {
                        imm_handleTimedNotification(s);
                    }

                    break;

                case YGenericHub.YSTREAM_REPORT_V2:
                    if (_devState == DevState.StreamReadyReceived)
                    {
                        handleTimedNotificationV2(s);
                    }

                    break;

                default:
                    _yctx._Log("drop unknown ystream:" + s);
                    break;
                }
            }
        }
Ejemplo n.º 17
0
        public async Task <int> SendOutputReportAsync(HidOutputReport report, CancellationToken cancellationToken = new CancellationToken())
        {
            var result = await device.SendOutputReportAsync(report).AsTask(cancellationToken);

            return((int)result);
        }
Ejemplo n.º 18
0
 public UwpOutputReport(HidOutputReport report)
 {
     this.report = report;
     Data = new ArraySegment<byte>(new byte[report.Data.Capacity-1]);
 }
Ejemplo n.º 19
0
 public async Task<int> SendOutputReportAsync(HidOutputReport report, CancellationToken cancellationToken = new CancellationToken())
 {
     var result = await device.SendOutputReportAsync(report).AsTask(cancellationToken);
     return (int)result;
 }
Ejemplo n.º 20
0
 public UwpOutputReport(HidOutputReport report)
 {
     this.report = report;
     Data        = new ArraySegment <byte>(new byte[report.Data.Capacity - 1]);
 }