Esempio n. 1
0
        private async Task SendABPMessages(int MESSAGES_COUNT, TestDeviceInfo device)
        {
            await this.ArduinoDevice.setDeviceModeAsync(LoRaArduinoSerial._device_mode_t.LWABP);

            await this.ArduinoDevice.setIdAsync(device.DevAddr, device.DeviceID, null);

            await this.ArduinoDevice.setKeyAsync(device.NwkSKey, device.AppSKey, null);

            await this.ArduinoDevice.SetupLora(this.TestFixture.Configuration.LoraRegion);

            // Sends 10x unconfirmed messages
            for (var i = 0; i < MESSAGES_COUNT; ++i)
            {
                var msg = PayloadGenerator.Next().ToString();
                Log($"{device.DeviceID}: Sending unconfirmed '{msg}' {i + 1}/{MESSAGES_COUNT}");
                await this.ArduinoDevice.transferPacketAsync(msg, 10);

                await Task.Delay(Constants.DELAY_BETWEEN_MESSAGES);

                // After transferPacket: Expectation from serial
                // +MSG: Done
                await AssertUtils.ContainsWithRetriesAsync("+MSG: Done", this.ArduinoDevice.SerialLogs);

                // 0000000000000005: valid frame counter, msg: 1 server: 0
                await this.TestFixture.AssertNetworkServerModuleLogStartsWithAsync($"{device.DeviceID}: valid frame counter, msg:");

                // 0000000000000005: decoding with: DecoderValueSensor port: 8
                await this.TestFixture.AssertNetworkServerModuleLogStartsWithAsync($"{device.DeviceID}: decoding with: {device.SensorDecoder} port:");

                // 0000000000000005: message '{"value": 51}' sent to hub
                await this.TestFixture.AssertNetworkServerModuleLogStartsWithAsync($"{device.DeviceID}: message '{{\"value\":{msg}}}' sent to hub");

                this.TestFixture.ClearLogs();
            }

            // Sends 10x confirmed messages
            for (var i = 0; i < MESSAGES_COUNT; ++i)
            {
                var msg = PayloadGenerator.Next().ToString();
                Log($"{device.DeviceID}: Sending confirmed '{msg}' {i + 1}/{MESSAGES_COUNT}");
                await this.ArduinoDevice.transferPacketWithConfirmedAsync(msg, 10);

                await Task.Delay(Constants.DELAY_BETWEEN_MESSAGES);

                // After transferPacketWithConfirmed: Expectation from serial
                // +CMSG: ACK Received
                await AssertUtils.ContainsWithRetriesAsync("+CMSG: ACK Received", this.ArduinoDevice.SerialLogs);

                // 0000000000000005: valid frame counter, msg: 1 server: 0
                await this.TestFixture.AssertNetworkServerModuleLogStartsWithAsync($"{device.DeviceID}: valid frame counter, msg:");

                // 0000000000000005: decoding with: DecoderValueSensor port: 8
                await this.TestFixture.AssertNetworkServerModuleLogStartsWithAsync($"{device.DeviceID}: decoding with: {device.SensorDecoder} port:");

                // 0000000000000005: message '{"value": 51}' sent to hub
                await this.TestFixture.AssertNetworkServerModuleLogStartsWithAsync($"{device.DeviceID}: message '{{\"value\":{msg}}}' sent to hub");

                this.TestFixture.ClearLogs();
            }
        }
Esempio n. 2
0
        public async Task InitializeAsync()
        {
            if (this.Configuration.CreateDevices)
            {
                var devices = new TestDeviceInfo[]
                {
                    this.Device1_OTAA,
                    this.Device2_OTAA,
                    this.Device3_OTAA,
                    this.Device4_OTAA,
                    this.Device5_ABP,
                    this.Device6_ABP,
                    this.Device7_ABP,
                    this.Device8_ABP,
                    this.Device9_OTAA,
                    this.Device10_OTAA,
                    this.Device11_OTAA,
                    this.Device12_OTAA,
                    this.Device13_OTAA,
                };

                var registryManager = GetRegistryManager();
                foreach (var testDevice in devices.Where(x => x.RealDevice))
                {
                    var getDeviceResult = await registryManager.GetDeviceAsync(testDevice.DeviceID);

                    if (getDeviceResult == null)
                    {
                        Console.WriteLine($"Device {testDevice.DeviceID} does not exist. Creating");
                        var device = new Device(testDevice.DeviceID);
                        var twin   = new Twin(testDevice.DeviceID);
                        twin.Properties.Desired = new TwinCollection(JsonConvert.SerializeObject(testDevice.GetDesiredProperties()));

                        Console.WriteLine($"Creating device {testDevice.DeviceID}");
                        await registryManager.AddDeviceWithTwinAsync(device, twin);
                    }
                    else
                    {
                        // compare device twin and make changes if needed
                        var deviceTwin = await registryManager.GetTwinAsync(testDevice.DeviceID);

                        var desiredProperties = testDevice.GetDesiredProperties();
                        foreach (var kv in desiredProperties)
                        {
                            if (!deviceTwin.Properties.Desired.Contains(kv.Key) || (string)deviceTwin.Properties.Desired[kv.Key] != kv.Value)
                            {
                                Console.WriteLine($"Unexpected value for device {testDevice.DeviceID} twin property {kv.Key}, expecting '{kv.Value}', actual is '{(string)deviceTwin.Properties.Desired[kv.Key]}'");

                                var patch = new Twin();
                                patch.Properties.Desired = new TwinCollection(JsonConvert.SerializeObject(desiredProperties));
                                await registryManager.UpdateTwinAsync(testDevice.DeviceID, patch, deviceTwin.ETag);

                                Console.WriteLine($"Update twin for device {testDevice.DeviceID}");
                                break;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public SimulatedDevice(TestDeviceInfo testDeviceInfo, int frmCntDown = 0, int frmCntUp = 0)
        {
            this.LoRaDevice = testDeviceInfo;

            FrmCntDown = frmCntDown;
            FrmCntUp   = frmCntUp;

            //can store 32 bit but only 16 are sent
            var fcnt32 = BitConverter.GetBytes(FrmCntDown);

            fCnt[0] = fcnt32[0];
            fCnt[1] = fcnt32[1];

            if (!this.IsJoined)
            {
                this.joinFinished = new SemaphoreSlim(0);
            }
        }
Esempio n. 4
0
        private async Task Test_OTAA_Unconfirmed_Send_And_Receive_C2D_Mac_CommandsImplAsync(TestDeviceInfo device, string c2dMessageBody)
        {
            const int MaxAttempts         = 5;
            const int UnconfirmedMsgCount = 2;

            // Sends 2x unconfirmed messages
            for (var i = 1; i <= UnconfirmedMsgCount; ++i)
            {
                var msg = PayloadGenerator.Next().ToString();
                this.Log($"{device.DeviceID}: Sending unconfirmed '{msg}' {i}/{UnconfirmedMsgCount}");

                await this.ArduinoDevice.transferPacketAsync(msg, 10);

                await Task.Delay(Constants.DELAY_BETWEEN_MESSAGES);

                await AssertUtils.ContainsWithRetriesAsync("+MSG: Done", this.ArduinoDevice.SerialLogs);

                this.TestFixtureCi.ClearLogs();
            }

            var c2dMessage = new LoRaCloudToDeviceMessage()
            {
                Fport       = 1,
                Payload     = c2dMessageBody,
                MacCommands = new MacCommand[]
                {
                    new DevStatusRequest(),
                }
            };

            await this.TestFixtureCi.SendCloudToDeviceMessageAsync(device.DeviceID, c2dMessage);

            this.Log($"Message {c2dMessageBody} sent to device, need to check if it receives");

            var macCommandReceivedMsg      = $"{device.DeviceID}: cloud to device MAC command DevStatusCmd received";
            var foundMacCommandReceivedMsg = false;

            var deviceMacCommandResponseMsg      = $": DevStatusCmd mac command detected in upstream payload: Type: DevStatusCmd Answer, Battery Level:";
            var foundDeviceMacCommandResponseMsg = false;

            // Sends 5x unconfirmed messages, stopping if assertions succeeded
            for (var i = 1; i <= MaxAttempts; ++i)
            {
                var msg = PayloadGenerator.Next().ToString();
                this.Log($"{device.DeviceID}: Sending unconfirmed '{msg}' {i}/{MaxAttempts}");
                await this.ArduinoDevice.transferPacketAsync(msg, 10);

                await Task.Delay(Constants.DELAY_BETWEEN_MESSAGES);

                await AssertUtils.ContainsWithRetriesAsync("+MSG: Done", this.ArduinoDevice.SerialLogs);

                // check if c2d message was found
                if (!foundMacCommandReceivedMsg)
                {
                    var searchResults = await this.TestFixtureCi.SearchNetworkServerModuleAsync(
                        (messageBody) =>
                    {
                        return(messageBody.StartsWith(macCommandReceivedMsg));
                    },
                        new SearchLogOptions
                    {
                        Description = macCommandReceivedMsg,
                        MaxAttempts = 1
                    });

                    // We should only receive the message once
                    if (searchResults.Found)
                    {
                        foundMacCommandReceivedMsg = true;
                        this.Log($"{device.DeviceID}: Found Mac C2D message in log (after sending {i}/{MaxAttempts}) ? {foundMacCommandReceivedMsg}");
                    }
                }

                if (!foundDeviceMacCommandResponseMsg)
                {
                    var macSearchResults = await this.TestFixtureCi.SearchNetworkServerModuleAsync(
                        (messageBody) =>
                    {
                        return(messageBody.Contains(deviceMacCommandResponseMsg, StringComparison.InvariantCultureIgnoreCase));
                    },
                        new SearchLogOptions
                    {
                        Description = deviceMacCommandResponseMsg,
                        MaxAttempts = 1
                    });

                    // We should only receive the message once
                    if (macSearchResults.Found)
                    {
                        foundDeviceMacCommandResponseMsg = true;
                        this.Log($"{device.DeviceID}: Found Mac Command reply in log (after sending {i}/{MaxAttempts}) ? {foundDeviceMacCommandResponseMsg}");
                    }
                }

                this.TestFixtureCi.ClearLogs();
                await Task.Delay(Constants.DELAY_BETWEEN_MESSAGES);

                if (foundDeviceMacCommandResponseMsg && foundMacCommandReceivedMsg)
                {
                    break;
                }
            }

            Assert.True(foundMacCommandReceivedMsg, $"Did not find in network server logs: '{macCommandReceivedMsg}'");
            Assert.True(foundDeviceMacCommandResponseMsg, $"Did not find in network server logs: '{deviceMacCommandResponseMsg}'");
        }
 // Logs starts of a test method call
 protected void LogTestStart(TestDeviceInfo device, [CallerMemberName] string memberName = "")
 {
     Log($"[INFO] ** Starting {memberName} using device {device.DeviceID} **");
 }
Esempio n. 6
0
        public IntegrationTestFixture()
        {
            this.Configuration = TestConfiguration.GetConfiguration();

            if (!string.IsNullOrEmpty(Configuration.IoTHubEventHubConnectionString) && this.Configuration.NetworkServerModuleLogAssertLevel != NetworkServerModuleLogAssertLevel.Ignore)
            {
                this.NetworkServerLogEvents = new EventHubDataCollector(Configuration.IoTHubEventHubConnectionString, Configuration.IoTHubEventHubConsumerGroup);
                var startTask = this.NetworkServerLogEvents.Start();
                startTask.ConfigureAwait(false).GetAwaiter().GetResult();
            }

            var gatewayID = Environment.GetEnvironmentVariable("IOTEDGE_DEVICEID") ?? this.Configuration.LeafDeviceGatewayID;

            // Device1_OTAA: used for join test only
            this.Device1_OTAA = new TestDeviceInfo()
            {
                DeviceID   = "0000000000000001",
                AppEUI     = "BE7A0000000014E3",
                AppKey     = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID  = gatewayID,
                RealDevice = true
            };

            // Device2_OTAA: used for failed join (wrong devEUI)
            this.Device2_OTAA = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000002",
                AppEUI        = "BE7A0000000014E3",
                AppKey        = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID     = gatewayID,
                SensorDecoder = "DecoderValueSensor",
                RealDevice    = false,
            };

            // Device3_OTAA: used for failed join (wrong appKey)
            this.Device3_OTAA = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000003",
                AppEUI        = "BE7A00000000FFE3",
                AppKey        = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID     = gatewayID,
                SensorDecoder = "DecoderValueSensor",
                RealDevice    = true,
            };


            // Device4_OTAA: used for OTAA confirmed & unconfirmed messaging
            this.Device4_OTAA = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000004",
                AppEUI        = "BE7A0000000014E3",
                AppKey        = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID     = gatewayID,
                SensorDecoder = "DecoderValueSensor",
                RealDevice    = true,
            };


            // Device5_ABP: used for ABP confirmed & unconfirmed messaging
            this.Device5_ABP = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000005",
                AppEUI        = "0000000000000005",
                GatewayID     = gatewayID,
                SensorDecoder = "DecoderValueSensor",
                RealDevice    = true,
                AppSKey       = "2B7E151628AED2A6ABF7158809CF4F3C",
                NwkSKey       = "3B7E151628AED2A6ABF7158809CF4F3C",
                DevAddr       = "0028B1B0"
            };

            // Device6_ABP: used for ABP wrong devaddr
            this.Device6_ABP = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000006",
                AppEUI        = "0000000000000006",
                GatewayID     = gatewayID,
                SensorDecoder = "DecoderValueSensor",
                RealDevice    = false,
                AppSKey       = "2B7E151628AED2A6ABF7158809CF4F3C",
                NwkSKey       = "3B7E151628AED2A6ABF7158809CF4F3C",
                DevAddr       = "0028B1B1",
            };

            // Device7_ABP: used for ABP wrong nwkskey
            this.Device7_ABP = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000007",
                AppEUI        = "0000000000000007",
                GatewayID     = gatewayID,
                SensorDecoder = "DecoderValueSensor",
                RealDevice    = true,
                AppSKey       = "2B7E151628AED2A6ABF7158809CF4F3C",
                NwkSKey       = "3B7E151628AED2A6ABF7158809CF4F3C",
                DevAddr       = "0028B1B2"
            };

            // Device8_ABP: used for ABP invalid nwkskey (mic fails)
            this.Device8_ABP = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000008",
                AppEUI        = "0000000000000008",
                GatewayID     = gatewayID,
                SensorDecoder = "DecoderValueSensor",
                RealDevice    = true,
                AppSKey       = "2B7E151628AED2A6ABF7158809CF4F3C",
                NwkSKey       = "3B7E151628AED2A6ABF7158809CF4F3C",
                DevAddr       = "0028B1B3"
            };

            // Device9_OTAA: used for confirmed message & C2D
            this.Device9_OTAA = new TestDeviceInfo()
            {
                DeviceID   = "0000000000000009",
                AppEUI     = "BE7A0000000014E3",
                AppKey     = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID  = gatewayID,
                RealDevice = true
            };

            // Device10_OTAA: used for unconfirmed message & C2D
            this.Device10_OTAA = new TestDeviceInfo()
            {
                DeviceID   = "0000000000000010",
                AppEUI     = "BE7A0000000014E3",
                AppKey     = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID  = gatewayID,
                RealDevice = true
            };

            // Device11_OTAA: used for http decoder
            this.Device11_OTAA = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000011",
                AppEUI        = "BE7A0000000014E3",
                AppKey        = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID     = gatewayID,
                RealDevice    = true,
                SensorDecoder = "http://sensordecodermodule/api/DecoderValueSensor",
            };

            // Device12_OTAA: used for reflection based decoder
            this.Device12_OTAA = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000012",
                AppEUI        = "BE7A0000000014E3",
                AppKey        = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID     = gatewayID,
                RealDevice    = true,
                SensorDecoder = "DecoderValueSensor",
            };

            // Device13_OTAA: used for Join with wrong AppEUI
            this.Device13_OTAA = new TestDeviceInfo()
            {
                DeviceID      = "0000000000000013",
                AppEUI        = "BE7A00000000FEE3",
                AppKey        = "8AFE71A145B253E49C3031AD068277A3",
                GatewayID     = gatewayID,
                RealDevice    = true,
                SensorDecoder = "DecoderValueSensor",
            };
        }