public DownlinkMessage(byte[] payload,
                               ulong xtime,
                               ReceiveWindow?rx1,
                               ReceiveWindow rx2,
                               DevEui devEui,
                               RxDelay lnsRxDelay,
                               LoRaDeviceClassType deviceClassType,
                               StationEui stationEui  = default,
                               uint?antennaPreference = null)
        {
            if (payload is null)
            {
                throw new ArgumentNullException(nameof(payload));
            }
            Data = payload;

            DevEui            = devEui;
            LnsRxDelay        = lnsRxDelay;
            DeviceClassType   = deviceClassType;
            AntennaPreference = antennaPreference;
            Rx1        = rx1;
            Rx2        = rx2;
            StationEui = stationEui;
            Xtime      = xtime;
        }
Пример #2
0
        private static DownlinkMessage BuildDownstreamMessage(LoRaDevice loRaDevice,
                                                              StationEui stationEUI,
                                                              ILogger logger,
                                                              ulong xTime,
                                                              ReceiveWindow?rx1,
                                                              ReceiveWindow rx2,
                                                              RxDelay lnsRxDelay,
                                                              LoRaPayloadData loRaMessage,
                                                              LoRaDeviceClassType deviceClassType,
                                                              uint?antennaPreference = null)
        {
            var messageBytes    = loRaMessage.Serialize(loRaDevice.AppSKey.Value, loRaDevice.NwkSKey.Value);
            var downlinkMessage = new DownlinkMessage(
                messageBytes,
                xTime,
                rx1, rx2,
                loRaDevice.DevEUI,
                lnsRxDelay,
                deviceClassType,
                stationEUI,
                antennaPreference
                );

            if (logger.IsEnabled(LogLevel.Debug))
            {
                logger.LogDebug($"{loRaMessage.MessageType} {JsonConvert.SerializeObject(downlinkMessage)}");
            }
            return(downlinkMessage);
        }
 private static uint CalculateRXWindowsTime(uint windowTime, RxDelay rxDelay)
 {
     // RxDelay follows specification of RXTimingSetupReq and the delay
     // | rXDelay | Delay |
     // ===================
     // |    0    |   1   |
     // |    1    |   1   |
     // |    2    |   2   |
     // |    3    |   3   |
     return((uint)(Enum.IsDefined(rxDelay) ? windowTime + rxDelay.ToSeconds() - 1 : windowTime));
 }
Пример #4
0
        public override byte[] PerformEncryption(string appSkey)
        {
            byte[] pt;
            if (!CfList.Span.IsEmpty)
            {
                pt = AppNonce.ToArray().Concat(NetID.ToArray()).Concat(DevAddr.ToArray()).Concat(DlSettings.ToArray()).Concat(RxDelay.ToArray()).Concat(CfList.ToArray()).Concat(Mic.ToArray()).ToArray();
            }
            else
            {
                pt = AppNonce.ToArray().Concat(NetID.ToArray()).Concat(DevAddr.ToArray()).Concat(DlSettings.ToArray()).Concat(RxDelay.ToArray()).Concat(Mic.ToArray()).ToArray();
            }

            Aes aes = new AesManaged();

            aes.Key     = ConversionHelper.StringToByteArray(appSkey);
            aes.IV      = new byte[16];
            aes.Mode    = CipherMode.ECB;
            aes.Padding = PaddingMode.None;

            ICryptoTransform cipher;

            cipher = aes.CreateDecryptor();
            var encryptedPayload = cipher.TransformFinalBlock(pt, 0, pt.Length);

            RawMessage = new byte[encryptedPayload.Length];
            Array.Copy(encryptedPayload, 0, RawMessage, 0, encryptedPayload.Length);
            return(encryptedPayload);
        }
Пример #5
0
 /// <summary>
 /// Write the value to a bytes buffer.
 /// </summary>
 public static Span <byte> Write(this RxDelay delay, Span <byte> buffer)
 {
     buffer[0] = (byte)delay;
     return(buffer[sizeof(RxDelay)..]);
Пример #6
0
 /// <summary>
 /// Converts to <see cref="TimeSpan"/>, if valid.
 /// </summary>
 public static TimeSpan ToTimeSpan(this RxDelay delay) => TimeSpan.FromSeconds(ToSeconds(delay));
Пример #7
0
 /// <summary>
 /// Converts to integral seconds, if valid.
 /// </summary>
 public static int ToSeconds(this RxDelay delay) =>
 Enum.IsDefined(delay) ? (delay is RxDelay.RxDelay0 ? 1 : (int)delay) : throw new ArgumentException(null, nameof(delay));
Пример #8
0
 /// <summary>
 /// Increments delay by a second.
 /// </summary>
 public static RxDelay Inc(this RxDelay delay) => (RxDelay) checked ((byte)(delay + 1));