Exemplo n.º 1
0
        private void GetInterfaces()
        {
            if (mInterfaces != null)
            {
                return;
            }
            GetConfigurationBlob();
            UsbInterfaceInfo[] interfaces = new UsbInterfaceInfo[Descriptor.NumInterfaces];
            int index = 0;
            int first = -1;

            for (int i = 0; i < mDescriptors.Length; i++)
            {
                UsbDescriptorBlob descriptor = mDescriptors[i];
                if (descriptor.Type != UsbDescriptorType.Interface)
                {
                    continue;
                }
                if (first != -1)
                {
                    if (index >= interfaces.Length)
                    {
                        Array.Resize(ref interfaces, index + 1);
                    }
                    interfaces[index] = new UsbInterfaceInfo(this, ArrayUtil.Slice(mDescriptors, first, i - first));
                    index++;
                }
                first = i;
            }
            if (first != -1)
            {
                if (index >= interfaces.Length)
                {
                    Array.Resize(ref interfaces, index + 1);
                }
                interfaces[index] = new UsbInterfaceInfo(this, ArrayUtil.Slice(mDescriptors, first, mDescriptors.Length - first));
            }
            mInterfaces = interfaces;
        }
Exemplo n.º 2
0
        private void GetConfigurationBlob()
        {
            if (mConfigurationBlob != null)
            {
                return;
            }
            int length = Descriptor.TotalLength;

            if (!mHasConfigurationDescriptor)
            {
                throw new Exception("Configuration descriptor is invalid");
            }
            Byte[] blob = new Byte[length];
            length = Device.Device.GetDescriptor((Byte)UsbDescriptorType.Configuration, Index, 0, blob, 0, length);
            if (length != blob.Length)
            {
                throw new Exception("Could not read configuration descriptor");
            }
            List <UsbDescriptorBlob> descriptors = new List <UsbDescriptorBlob>();

            for (int offset = 0; offset < length;)
            {
                if (length - offset < 2)
                {
                    throw new Exception("Descriptor has been truncated");
                }
                UsbDescriptorBlob descriptor = new UsbDescriptorBlob(blob, offset);
                if (length - offset < descriptor.Length)
                {
                    throw new Exception("Descriptor has been truncated");
                }
                descriptors.Add(descriptor);
                offset += descriptor.Length;
            }
            mConfigurationBlob = blob;
            mDescriptors       = descriptors.ToArray();
        }