Exemplo n.º 1
0
        public static byte[] CreateResponseMessage(CategoryIdType categoryId, ushort requestType, uint sequenceId, byte errorCode, ResponseBase response = null)
        {
            // Handle response object if provided...
            byte[] payload = new byte[0];
            if (response != null)
            {
                payload = response.GetPayload();
            }

            /* Response format:
             *
             *  | Offset Bytes |    0     |    1     |    2     |    3     |
             *  |       0      |                  Preamble                 |
             *  |       4      |        Length       | Msg Type | Reserved |
             *  |       8      |      Category ID    |      Request ID     |
             *  |      12      |   Sequence Number   | Result   | Reserved |
             *  |      16      |               <Response Data>             |
             *  |     ...      |                    ...                    |
             *
             * Length           : UINT16 (LSB) - the message length excluding the first 6 bytes.
             *
             * Sequence number  : UINT16 (LSB) - must be the same as the sequence number of the
             *                    request this is answering.
             *
             * Result           : 0x00 if successful, > 0x00 as an error code if failed
             */

            byte[] message = new byte[16 + payload.Length];

            ByteArrayHelper.WriteBytes(Preamble, message);
            ByteArrayHelper.WriteLsbUInt16((ushort)(10 + payload.Length), message, 4);
            message[6] = (byte)MessageType.Response;
            ByteArrayHelper.WriteLsbUInt16((ushort)categoryId, message, 8);
            ByteArrayHelper.WriteLsbUInt16(requestType, message, 10);
            ByteArrayHelper.WriteLsbUInt16((ushort)sequenceId, message, 12);
            message[14] = errorCode;
            ByteArrayHelper.WriteBytes(payload, message, 16);

            return(message);
        }
Exemplo n.º 2
0
        public static byte[] CreateEventMessage(CategoryIdType categoryId, ushort wifiEventType)
        {
            /* Event format:
             *
             *  | Offset Bytes |    0     |    1     |    2     |    3     |
             *  |       0      |                  Preamble                 |
             *  |       4      |        Length       | Msg Type | Reserved |
             *  |       8      |      Category ID    |      Event ID       |
             *
             * Length           : UINT16 (LSB) - the message length excluding the first 6 bytes.
             */

            byte[] message = new byte[12];

            ByteArrayHelper.WriteBytes(Preamble, message);
            ByteArrayHelper.WriteLsbUInt16(6, message, 4); // Length
            message[6] = (byte)MessageType.Event;
            ByteArrayHelper.WriteLsbUInt16((ushort)categoryId, message, 8);
            ByteArrayHelper.WriteLsbUInt16((ushort)wifiEventType, message, 10);

            return(message);
        }