public byte[] GetJoinRequest()
        {
            // create a join request
            var AppEUI = LoRaTools.Utils.ConversionHelper.ByteArrayToString(this.LoRaDevice.GetAppEUI());
            var DevEUI = LoRaTools.Utils.ConversionHelper.ByteArrayToString(this.LoRaDevice.GetDevEUI());

            byte[] DevNonce = new byte[2];
            if ((this.LoRaDevice.DevNonce == "") || (!this.isFirstJoinRequest))
            {
                Random random = new Random();
                // DevNonce[0] = 0xC8; DevNonce[1] = 0x86;
                random.NextBytes(DevNonce);
                this.LoRaDevice.DevNonce = BitConverter.ToString(DevNonce).Replace("-", "");
                Array.Reverse(DevNonce);
                this.isFirstJoinRequest = false;
            }
            else
            {
                DevNonce = this.LoRaDevice.GetDevNonce();
                Array.Reverse(DevNonce);
            }

            Logger.LogAlways(this.LoRaDevice.DevEUI, $"Join request sent DevNonce: {BitConverter.ToString(DevNonce).Replace("-","")}");
            var join = new LoRaPayloadJoinRequest(AppEUI, DevEUI, DevNonce);

            join.SetMic(this.LoRaDevice.AppKey);

            return(join.GetByteMessage());
        }
Example #2
0
        public byte[] GetJoinRequest()
        {
            //create a join request
            byte[] AppEUI = LoRaDevice.GetAppEUI();
            Array.Reverse(AppEUI);
            byte[] DevEUI = LoRaDevice.GetDevEUI();
            Array.Reverse(DevEUI);

            byte[] DevNonce = new byte[2];
            if ((LoRaDevice.DevNonce == "") || (!isFirstJoinRequest))
            {
                Random random = new Random();
                // DevNonce[0] = 0xC8; DevNonce[1] = 0x86;
                random.NextBytes(DevNonce);
                LoRaDevice.DevNonce = BitConverter.ToString(DevNonce).Replace("-", "");
                Array.Reverse(DevNonce);
                isFirstJoinRequest = false;
            }
            else
            {
                DevNonce = LoRaDevice.GetDevNonce();
                Array.Reverse(DevNonce);
            }

            Logger.Log(LoRaDevice.DevEUI, $"Join request sent DevNonce: {BitConverter.ToString(DevNonce).Replace("-","")}", Logger.LoggingLevel.Always);
            var join = new LoRaPayloadJoinRequest(AppEUI, DevEUI, DevNonce);

            join.SetMic(LoRaDevice.AppKey);

            return(join.GetByteMessage());
        }
Example #3
0
        byte[] CreateJoinRequest()
        {
            //create a join request
            byte[] AppEUI = ConversionHelper.StringToByteArray(LoRaDevice.AppEUI);
            Array.Reverse(AppEUI);
            byte[] DevEUI = ConversionHelper.StringToByteArray(LoRaDevice.DeviceID);
            Array.Reverse(DevEUI);

            byte[] devNonce = new byte[2];
            if ((string.IsNullOrEmpty(this.DevNonce)) || (!isFirstJoinRequest))
            {
                Random random = new Random();
                // DevNonce[0] = 0xC8; DevNonce[1] = 0x86;
                random.NextBytes(devNonce);
                this.DevNonce = BitConverter.ToString(devNonce).Replace("-", "");
                Array.Reverse(devNonce);
                isFirstJoinRequest = false;
            }
            else
            {
                devNonce = ConversionHelper.StringToByteArray(this.DevNonce);
                Array.Reverse(devNonce);
            }

            TestLogger.Log($"[{LoRaDevice.DeviceID}] Join request sent DevNonce: {BitConverter.ToString(devNonce).Replace("-","")} / {this.DevNonce}");
            var join = new LoRaPayloadJoinRequest(AppEUI, DevEUI, devNonce);

            join.SetMic(this.LoRaDevice.AppKey);

            return(join.GetByteMessage());
        }
Example #4
0
        public void JoinRequest_Should_Succeed_Mic_Check()
        {
            var appEUIText  = "0005100000000004";
            var appEUIBytes = ConversionHelper.StringToByteArray(appEUIText);

            var devEUIText  = "0005100000000004";
            var devEUIBytes = ConversionHelper.StringToByteArray(devEUIText);

            var devNonceText  = "ABCD";
            var devNonceBytes = ConversionHelper.StringToByteArray(devNonceText);

            var appKey = "00000000000000000005100000000004";

            var joinRequest = new LoRaPayloadJoinRequest(appEUIText, devEUIText, devNonceBytes);

            joinRequest.SetMic(appKey);
            Assert.True(joinRequest.CheckMic(appKey));
            Assert.True(joinRequest.CheckMic(appKey)); // ensure multiple calls work!

            var rxpk = new LoRaTools.LoRaPhysical.Rxpk()
            {
                Chan = 7,
                Rfch = 1,
                Freq = 903.700000,
                Stat = 1,
                Modu = "LORA",
                Datr = "SF10BW125",
                Codr = "4/5",
                Rssi = -17,
                Lsnr = 12.0f,
            };

            var data = joinRequest.GetByteMessage();

            rxpk.Data = Convert.ToBase64String(data);
            rxpk.Size = (uint)data.Length;

            byte[] decodedJoinRequestBytes = Convert.FromBase64String(rxpk.Data);
            var    decodedJoinRequest      = new LoRaTools.LoRaMessage.LoRaPayloadJoinRequest(decodedJoinRequestBytes);

            Assert.True(decodedJoinRequest.CheckMic(appKey));
        }