public byte[] GetUnconfirmedDataUpMessage()
        {
            byte[] mhbr    = new byte[] { 0x40 };
            byte[] devAddr = this.LoRaDevice.GetDevAddr();
            Array.Reverse(devAddr);
            byte[] fCtrl = new byte[] { 0x80 };
            // byte[] _FCnt = new byte[] { 0x00, 0x00 };
            this._fCnt[0]++;
            List <MacCommand> fopts = null;

            byte[] fPort = new byte[] { 0x01 };
            // Creating a random number
            // Random random = new Random();
            // int temp = random.Next(-50, 70);
            this.IncrementalData++;
            Logger.LogAlways(this.LoRaDevice.DevEUI, $"Simulated data: {this.IncrementalData.ToString()}");
            byte[] payload = Encoding.ASCII.GetBytes(this.IncrementalData.ToString());
            Array.Reverse(payload);
            // 0 = uplink, 1 = downlink
            int             direction    = 0;
            LoRaPayloadData standardData = new LoRaPayloadData((LoRaMessageType)mhbr[0], devAddr, fCtrl, this._fCnt, fopts, fPort, payload, direction);

            // Need to create Fops. If not, then MIC won't be correct
            standardData.Fopts = new byte[0];
            // First encrypt the data
            standardData.PerformEncryption(this.LoRaDevice.AppSKey);
            // "0A501524F8EA5FCBF9BDB5AD7D126F75");
            // Now we have the full package, create the MIC
            standardData.SetMic(this.LoRaDevice.NwkSKey);
            // "99D58493D1205B43EFF938F0F66C339E");

            return(standardData.GetByteMessage());
        }
Beispiel #2
0
        public byte[] GetUnconfirmedDataUpMessage()
        {
            byte[] _mhbr    = new byte[] { 0x40 };
            byte[] _devAddr = LoRaDevice.GetDevAddr();
            Array.Reverse(_devAddr);
            byte[] _FCtrl = new byte[] { 0x80 };
            // byte[] _FCnt = new byte[] { 0x00, 0x00 };
            _FCnt[0]++;
            byte[] _Fopts = null;
            byte[] _FPort = new byte[] { 0x01 };
            // Creating a random number
            Random random = new Random();
            int    temp   = random.Next(-50, 70);

            Logger.Log(LoRaDevice.DevAddr, $"Simulated data: {temp.ToString()}", Logger.LoggingLevel.Always);
            byte[] _payload = Encoding.ASCII.GetBytes(temp.ToString());
            Array.Reverse(_payload);
            // 0 = uplink, 1 = downlink
            int             direction    = 0;
            LoRaPayloadData standardData = new LoRaPayloadData(_mhbr, _devAddr, _FCtrl, _FCnt, _Fopts, _FPort, _payload, direction);

            // Need to create Fops. If not, then MIC won't be correct
            standardData.Fopts = new byte[0];
            // First encrypt the data
            standardData.PerformEncryption(LoRaDevice.AppSKey); //"0A501524F8EA5FCBF9BDB5AD7D126F75");
            // Now we have the full package, create the MIC
            standardData.SetMic(LoRaDevice.NwkSKey);            //"99D58493D1205B43EFF938F0F66C339E");

            return(standardData.GetByteMessage());
        }
Beispiel #3
0
        byte[] CreateUnconfirmedDataUpMessage(string data, byte fport = 1)
        {
            byte[] mhbr    = new byte[] { 0x40 };
            byte[] devAddr = ConversionHelper.StringToByteArray(LoRaDevice.DevAddr);
            Array.Reverse(devAddr);
            byte[] fCtrl = new byte[] { 0x80 };
            // byte[] _FCnt = new byte[] { 0x00, 0x00 };
            fCnt[0]++;
            byte[] fopts = null;
            byte[] fPort = new byte[] { fport };
            TestLogger.Log($"{LoRaDevice.DeviceID}: Simulated data: {data}");
            byte[] payload = Encoding.UTF8.GetBytes(data);
            Array.Reverse(payload);
            // 0 = uplink, 1 = downlink
            int             direction    = 0;
            LoRaPayloadData standardData = new LoRaPayloadData((LoRaPayloadData.MType)mhbr[0], devAddr, fCtrl, fCnt, fopts, fPort, payload, direction);

            // Need to create Fops. If not, then MIC won't be correct
            standardData.Fopts = new byte[0];
            // First encrypt the data
            standardData.PerformEncryption(LoRaDevice.AppSKey); //"0A501524F8EA5FCBF9BDB5AD7D126F75");
            // Now we have the full package, create the MIC
            standardData.SetMic(LoRaDevice.NwkSKey);            //"99D58493D1205B43EFF938F0F66C339E");

            return(standardData.GetByteMessage());
        }
        public void TestConfirmedDataUp()
        {
            byte[] mhdr = new byte[1];
            mhdr[0] = 128;
            byte[] devAddr = new byte[4]
            {
                4, 3, 2, 1
            };

            byte[] fctrl = new byte[1] {
                0
            };
            byte[] fcnt = new byte[2]
            {
                0, 0
            };
            byte[] fport = new byte[1]
            {
                10
            };
            byte[] frmPayload = new byte[4]
            {
                4, 3, 2, 1
            };

            var nwkkey = new byte[16] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
            };
            var appkey = new byte[16] {
                16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
            };


            LoRaPayloadData lora = new LoRaPayloadData(MType.ConfirmedDataUp, devAddr, fctrl, fcnt, null, fport, frmPayload, 0);

            lora.PerformEncryption(ConversionHelper.ByteArrayToString(appkey));
            byte[] testEncrypt = new byte[4]
            {
                226, 100, 212, 247
            };
            Assert.Equal(testEncrypt, lora.Frmpayload.ToArray());
            lora.SetMic(ConversionHelper.ByteArrayToString(nwkkey));
            byte[] testMic = new byte[4]
            {
                181, 106, 14, 117
            };
            Assert.Equal(testMic, lora.Mic.ToArray());
            var mess = lora.GetByteMessage();

            lora.ChangeEndianess();
            Assert.Equal(mess, lora.RawMessage);
            //TODO
        }