Ejemplo n.º 1
0
        private static Packet WriteSimplePacket(HidStream stream, ushort opcode)
        {
            using (Packet packet = new Packet())
            {
                packet.WriteByte(0);// report id
                packet.WriteUInt16(opcode);
                packet.WriteBytes(new byte[62]);
                byte[] buffer = packet.GetWrittenBuffer();
                Crc16.InsertCrc(buffer, 1, 7);// offset for the report id byte
                stream.Write(buffer);

                byte[] resultBufferWithReportId = new byte[65];
                stream.Read(resultBufferWithReportId);
                if (resultBufferWithReportId[0] != 0)
                {
                    return(null);
                }
                byte[] resultBuffer = new byte[64];
                Buffer.BlockCopy(resultBufferWithReportId, 1, resultBuffer, 0, resultBuffer.Length);
                // All handshake packets should have a result code of 1
                if (resultBuffer[2] != 1 ||
                    resultBuffer[0] != (byte)opcode || resultBuffer[1] != opcode >> 8)
                {
                    return(null);
                }
                if (!Crc16.ValidateCrc(resultBuffer))
                {
                    return(null);
                }
                Packet result = new Packet(true, resultBuffer);
                result.Index = 8;
                return(result);
            }
        }
Ejemplo n.º 2
0
        private byte[] GetResponse(byte op1, byte op2)
        {
            // This is a bit of a hack to consume any packets sent from the keyboard which we didn't request
            // such as changing the keyboard layer manually. TODO: Add a proper packet handler and remove this hack.
            const int reportHeaderLen = 1;

            while (true)
            {
                byte[] resultBufferWithReportId = new byte[65];
                try
                {
                    stream.Read(resultBufferWithReportId);
                }
                catch
                {
                    return(null);
                }
                if (resultBufferWithReportId[0] != 0)
                {
                    return(null);
                }
                if (resultBufferWithReportId[1] == op1)
                {
                    byte[] resultBuffer = new byte[64];
                    Buffer.BlockCopy(resultBufferWithReportId, 1, resultBuffer, 0, resultBuffer.Length);
                    return(resultBuffer);
                }
                else if (!Crc16.ValidateCrc(resultBufferWithReportId, reportHeaderLen, reportHeaderLen + 6))
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 3
0
        private byte[] GetResponse(byte op1, byte op2)
        {
            // This is a bit of a hack to consume any packets sent from the keyboard which we didn't request
            // such as changing the keyboard layer manually. TODO: Add a proper packet handler and remove this hack.
            const int reportHeaderLen = 1;

            while (true)
            {
                byte[] resultBufferWithReportId = new byte[65];
                try
                {
                    stream.Read(resultBufferWithReportId);
                }
                catch
                {
                    return(null);
                }
                if (resultBufferWithReportId[0] != 0)
                {
                    return(null);
                }
                if (resultBufferWithReportId[1] == op1)
                {
                    byte[] resultBuffer = new byte[64];
                    Buffer.BlockCopy(resultBufferWithReportId, 1, resultBuffer, 0, resultBuffer.Length);
                    return(resultBuffer);
                }
                else if (resultBufferWithReportId[1] == (byte)OpCodes.DriverKeyCallback)
                {
                    byte callbackId      = resultBufferWithReportId[3];
                    bool callbackKeyDown = resultBufferWithReportId[4] != 0;
                    if (callbackKeyDown)
                    {
                        KeyboardState.Key key;
                        State.KeysByLogicCode.TryGetValue(callbackId, out key);
                        Program.Log("Index:" + callbackId + " " +
                                    (key == null ? "(null)" :
                                     "Name:" + key.KeyName + " " +                   // Name of the key
                                     "Indx:" + key.LogicCode + " " +                 // 'Indx' for logic code / key index
                                     "Loc:" + key.LocationCode + " " +               // 'Loc' for location code
                                     "D:0x" + key.DriverValue.ToString("X8") + " " + // 'D' for DriverValue
                                                                                     //"E:" + (DriverValue)key.DriverValue + " " +// 'E' for the Enum variant of the DriverValue
                                     "S:" + key.DriverValueName));                   // <--- 'S' for string representation (use this for mapping keys in your UserData file)
                    }
                }
                else if (!Crc16.ValidateCrc(resultBufferWithReportId, reportHeaderLen, reportHeaderLen + 6))
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 4
0
        public Packet WritePacket(OpCodes op1, byte op2, Packet packet, byte op3 = 0)
        {
            lock (locker)
            {
                int    numPackets     = 1;
                int    offset         = 0;
                byte[] completeBuffer = null;
                if (packet != null)
                {
                    completeBuffer = packet.GetWrittenBuffer();
                    numPackets     = (completeBuffer.Length / 0x38) + 1;
                }

                bool allowsLongOffset = false;
                int  offsetOffset     = 2;
                int  lengthOffset     = 4;
                switch (op1)
                {
                case OpCodes.DriverLayerSetKeyValues:
                case OpCodes.DriverLayerUpdateRealtimeLighting:
                case OpCodes.LayerSetLightValues:
                    offsetOffset     = 2;
                    lengthOffset     = 5;
                    allowsLongOffset = true;
                    break;

                case OpCodes.LayerSetKeyPressLightingEffect:
                case OpCodes.LayerSetKeyValues:
                case OpCodes.LayerFnSetKeyValues:
                case OpCodes.LayerSetMacros:
                    offsetOffset = 2;
                    lengthOffset = 4;
                    break;
                }

                const int reportHeaderLen = 1;

                try
                {
                    for (int i = 0; i < numPackets; i++)
                    {
                        byte[] report = new byte[65];
                        report[1] = (byte)op1;
                        report[2] = op2;
                        if (op3 > 0)
                        {
                            // Not really an op, used for setting keyboard data buffers
                            report[3] = op3;
                        }
                        if (completeBuffer != null)
                        {
                            int numBytesToWrite = Math.Min(0x38, completeBuffer.Length - offset);
                            Buffer.BlockCopy(completeBuffer, offset, report, reportHeaderLen + 8, numBytesToWrite);
                            if (numPackets > 1)
                            {
                                report[reportHeaderLen + offsetOffset + 0] = (byte)offset;
                                report[reportHeaderLen + offsetOffset + 1] = (byte)(offset >> 8);
                                if (allowsLongOffset)
                                {
                                    report[reportHeaderLen + offsetOffset + 3] = (byte)(offset >> 16);
                                }
                                report[reportHeaderLen + lengthOffset] = (byte)numBytesToWrite;
                            }
                        }
                        Crc16.InsertCrc(report, reportHeaderLen, 6 + reportHeaderLen);
                        stream.Write(report);

                        byte[] resultBuffer = GetResponse((byte)op1, op2);
                        if (resultBuffer[0] != (byte)op1 /* || resultBuffer[1] != op2*/)
                        {
                            return(null);
                        }
                        if (!Crc16.ValidateCrc(resultBuffer))
                        {
                            return(null);
                        }
                        if (i == numPackets - 1)
                        {
                            Packet result = new Packet(true, resultBuffer);
                            result.Index    = 8;
                            lastSentMessage = Environment.TickCount;
                            return(result);
                        }
                        offset += 0x38;
                    }
                }
                catch
                {
                    return(null);
                }
                return(null);
            }
        }