Beispiel #1
0
        internal WifiSetRequest(WifiRequestId wifiRequestType, uint sequenceId, byte[] payload)
            : base(CategoryIdType.WifiControl, (ushort)wifiRequestType, sequenceId, payload, 4)
        {
            /* Data format:
             *
             * - 00 [  1 ] Operation result - 0x00 success, > 0x00 is error code
             * - 01 [  3 ] Reserved
             */

            this.ErrorCode = payload[0];
        }
        internal WifiScanSummaryRequest(WifiRequestId wifiRequestType, uint sequenceId, byte[] payload)
            : base(CategoryIdType.WifiControl, (ushort)wifiRequestType, sequenceId, payload, 8)
        {
            /* Data format:
             *
             * - 00 [  1 ] Scan result - 0x00 success, > 0x00 is error code
             * - 01 [  1 ] Total network count
             * - 02 [  2 ] Reserved
             * - 04 [  4 ] Total results size - the number of bytes needed to store scan results
             */

            ErrorCode    = payload[0];
            NetworkCount = payload[1];
        }
Beispiel #3
0
        internal RequestBase(WifiRequestId wifiRequestType, uint sequenceId, byte[] payload, int expectedPayloadLength)
        {
            if (payload == null && expectedPayloadLength != 0)
            {
                throw new ArgumentNullException(nameof(payload), "Payload should not be null.");
            }

            if (payload != null && payload.Length != expectedPayloadLength)
            {
                throw new ArgumentOutOfRangeException(nameof(payload), $"Payload should be {expectedPayloadLength} bytes. It is {payload.Length}.");
            }

            WifiRequestType = wifiRequestType;
            SequenceId      = sequenceId;
        }
        internal WifiScanResultRequest(WifiRequestId wifiRequestType, uint sequenceId, byte[] payload)
            : base(wifiRequestType, sequenceId, payload, 36)
        {
            /* Data format:
             *
             * - 00 [  1 ] Security type
             * - 01 [  1 ] WiFi signal level - RSSI range from -128 to 0
             * - 02 [  1 ] SSID length
             * - 03 [  1 ] Reserved
             * - 04 [ 32 ] SSID
             */

            SecurityType   = (SecurityType)payload[0];
            SignalStrength = ByteArrayHelper.ReadSignedByte(payload, 1);
            Ssid           = ByteArrayHelper.ReadBytes(payload, 4, payload[2]);
        }
        private static RequestBase ExtractRequestPayload(CategoryIdType categoryId, ushort requestType, uint sequenceId, byte[] payload)
        {
            switch (categoryId)
            {
            case CategoryIdType.DeviceControl:
                DeviceControlRequestId deviceControlRequestId = (DeviceControlRequestId)requestType;
                switch (deviceControlRequestId)
                {
                case DeviceControlRequestId.GetDesiredLedStatus:
                    return(new DeviceControlGetDesiredLedStatusRequest(deviceControlRequestId, sequenceId));

                case DeviceControlRequestId.ReportLedStatus:
                    return(new DeviceControlReportLedStatusRequest(deviceControlRequestId, sequenceId, payload));

                default:
                    throw new InvalidOperationException($"Unknown response payload type: {(WifiRequestId)requestType}");
                }

            case CategoryIdType.WifiControl:
                WifiRequestId wifiRequestId = (WifiRequestId)requestType;
                switch (wifiRequestId)
                {
                case WifiRequestId.SetWifiStatus:
                    return(new WifiStatusRequest(wifiRequestId, sequenceId, payload));

                case WifiRequestId.SetWifiScanResultsSummary:
                    return(new WifiScanSummaryRequest(wifiRequestId, sequenceId, payload));

                case WifiRequestId.SetNextWifiScanResult:
                    return(new WifiScanResultRequest(wifiRequestId, sequenceId, payload));

                case WifiRequestId.GetNewWifiDetails:
                    // This request doesn't have a payload
                    return(new WifiGetNewDetailsRequest(wifiRequestId, sequenceId));

                case WifiRequestId.SetWifiOperationResult:
                    return(new WifiSetRequest(wifiRequestId, sequenceId, payload));

                default:
                    throw new InvalidOperationException($"Unknown response payload type: {wifiRequestId}");
                }

            default:
                throw new InvalidOperationException($"Unknown Category ID type: {categoryId}");
            }
        }
        public static byte[] CreateResponseMessage(WifiRequestId wifiRequestType, 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.WifiControl, message, 8);
            ByteArrayHelper.WriteLsbUInt16((ushort)wifiRequestType, message, 10);
            ByteArrayHelper.WriteLsbUInt16((ushort)sequenceId, message, 12);
            message[14] = errorCode;
            ByteArrayHelper.WriteBytes(payload, message, 16);

            return(message);
        }
Beispiel #7
0
        internal WifiStatusRequest(WifiRequestId wifiRequestType, uint sequenceId, byte[] payload)
            : base(CategoryIdType.WifiControl, (ushort)wifiRequestType, sequenceId, payload, 48)
        {
            /* Data format:
             *
             * - 00 [  1 ] WiFi connection status - 0x00 : WiFi connected, 0x01 : Internet connected
             * - 01 [  1 ] WiFi signal level - RSSI range from -128 to 0
             * - 02 [  1 ] Security type
             * - 03 [  1 ] SSID length
             * - 04 [ 32 ] SSID
             * - 36 [  4 ] WiFi frequency - UInt32 in MHz
             * - 40 [  6 ] BSSID
             * - 46 [  2 ] Reserved
             */

            IsWifiConnected     = (payload[0] & 0x01) == 0x01;
            IsInternetConnected = (payload[0] & 0x02) == 0x02;
            SignalStrength      = ByteArrayHelper.ReadSignedByte(payload, 1);
            SecurityType        = (SecurityType)payload[2];
            Ssid         = ByteArrayHelper.ReadBytes(payload, 4, payload[3]);
            FrequencyMhz = ByteArrayHelper.ReadLsbUInt32(payload, 36);
            Bssid        = ByteArrayHelper.ReadDelimitedHex(payload, 40, 6, ':');
        }
        public static RequestBase ReadRequestMessagePayload(byte[] message)
        {
            if (message.Length < 16)
            {
                throw new ArgumentOutOfRangeException(nameof(message), $"Message should be at least 16 bytes, not {message.Length}.");
            }

            /* Request format:
             *
             *  | Offset Bytes |    0     |    1     |    2     |    3     |
             *  |       0      |                  Preamble                 |
             *  |       4      |        Length       | Msg Type | Reserved |
             *  |       8      |      Category ID    |      Request ID     |
             *  |      12      |   Sequence Number   |       Reserved      |
             *  |      16      |               <Request Data>              |
             *  |     ...      |                    ...                    |
             *
             * Length           : UINT16 (LSB) - the message length excluding the first 6 bytes.
             *
             * Sequence number  : UINT16 (LSB) - must be used for the response to this request.
             */

            uint          length          = ByteArrayHelper.ReadLsbUInt16(message, 4);
            WifiRequestId wifiRequestType = (WifiRequestId)ByteArrayHelper.ReadLsbUInt16(message, 10);
            uint          sequenceId      = ByteArrayHelper.ReadLsbUInt16(message, 12);

            byte[] payload       = null;
            uint   payloadLength = length - 10;

            if (payloadLength > 0)
            {
                payload = ByteArrayHelper.ReadBytes(message, 16, payloadLength);
            }

            return(ExtractRequestPayload(wifiRequestType, sequenceId, payload));
        }
        private static RequestBase ExtractRequestPayload(WifiRequestId wifiRequestType, uint sequenceId, byte[] payload)
        {
            switch (wifiRequestType)
            {
            case WifiRequestId.SetWifiStatus:
                return(new WifiStatusRequest(wifiRequestType, sequenceId, payload));

            case WifiRequestId.SetWifiScanResultsSummary:
                return(new WifiScanSummaryRequest(wifiRequestType, sequenceId, payload));

            case WifiRequestId.SetNextWifiScanResult:
                return(new WifiScanResultRequest(wifiRequestType, sequenceId, payload));

            case WifiRequestId.GetNewWifiDetails:
                // This request doesn't have a payload
                return(new WifiGetNewDetailsRequest(wifiRequestType, sequenceId));

            case WifiRequestId.SetWifiOperationResult:
                return(new WifiSetRequest(wifiRequestType, sequenceId, payload));

            default:
                throw new InvalidOperationException($"Unknown response payload type: {wifiRequestType}");
            }
        }
 internal WifiGetNewDetailsRequest(WifiRequestId wifiRequestType, uint sequenceId)
     : base(wifiRequestType, sequenceId, null, 0)
 {
     // This request type doesn't have a payload.
 }
 internal WifiGetNewDetailsRequest(WifiRequestId wifiRequestType, uint sequenceId)
     : base(CategoryIdType.WifiControl, (ushort)wifiRequestType, sequenceId, null, 0)
 {
     // This request type doesn't have a payload.
 }