Ejemplo n.º 1
0
        public UsbKeyPowerSettings ReadUsbSettings()
        {
            // create the packet
            byte[] packet = new byte[Constants.PacketsSize];
            packet[0] = CommandCodes.ReadChipParameters;
            packet[1] = SubCommandCodes.UsbPowerUpKeyParameters;
            byte[] reply = _hidHandler.WriteData(packet);

            // check if package format is faulty
            if (reply[0] != CommandCodes.ReadChipParameters ||
                reply[2] != SubCommandCodes.UsbPowerUpKeyParameters)
            {
                throw new PacketReplyFormatException();
            }

            // create the settings
            UsbKeyPowerSettings settings = new UsbKeyPowerSettings();

            settings.VID = Convert.ToInt32(BitConverter.ToUInt16(reply, 12));
            settings.PID = Convert.ToInt32(BitConverter.ToUInt16(reply, 14));
            settings.RequestedCurrent    = Convert.ToInt32(reply[30]) * 2; // 2mA Quanta
            settings.RemoteWakeUpCapable = (reply[29] & 32) == 32;
            settings.SelfPowered         = (reply[29] & 64) == 64;
            settings.HostPowered         = (reply[29] & 128) == 128;

            return(settings);
        }
Ejemplo n.º 2
0
        public void WriteUsbSettings(UsbKeyPowerSettings settings)
        {
            // get VID & pid bytes
            byte[] vidBytes = BitConverter.GetBytes(Convert.ToUInt16(settings.VID));
            byte[] pidBytes = BitConverter.GetBytes(Convert.ToUInt16(settings.PID));

            // get chip power options
            byte cpOptionsByte = 0x00;

            cpOptionsByte |= (byte)(settings.RemoteWakeUpCapable ? 32 : 0); // 0010 0000
            cpOptionsByte |= (byte)(settings.SelfPowered ? 64 : 0);         // 0100 0000
            cpOptionsByte |= (byte)(settings.HostPowered ? 128 : 0);        // 1000 0000

            // get current bytes
            byte currentByte = Convert.ToByte(settings.RequestedCurrent / 2);

            // create the package
            byte[] packet = new byte[Constants.PacketsSize];
            packet[0] = CommandCodes.WriteChipParameters;
            packet[1] = SubCommandCodes.UsbPowerUpKeyParameters;
            packet[4] = vidBytes[0];
            packet[5] = vidBytes[1];
            packet[6] = pidBytes[0];
            packet[7] = pidBytes[1];
            packet[8] = cpOptionsByte;
            packet[9] = currentByte;

            // write the packet
            byte[] reply = _hidHandler.WriteData(packet);

            // check if package format is faulty
            if (reply[0] != CommandCodes.WriteChipParameters ||
                reply[2] != SubCommandCodes.UsbPowerUpKeyParameters)
            {
                throw new PacketReplyFormatException();
            }

            switch (reply[1])
            {
            case ReplyStatusCodes.CompletedSuccessfully:
                break;

            case ReplyStatusCodes.BlockedAccess:
                throw new AccessBlockedException();

            default:
                throw new NotImplementedException();
            }
        }