/// <summary>
        /// Initializes a new instance of the <see cref="LoRaPayload"/> class.
        /// Constructor used by the simulator
        /// </summary>
        public static bool TryCreateLoRaPayloadForSimulator(Txpk txpk, string appKey, out LoRaPayload loRaPayload)
        {
            if (txpk.Data != null)
            {
                byte[] convertedInputMessage = Convert.FromBase64String(txpk.Data);
                switch ((LoRaMessageType)convertedInputMessage[0])
                {
                case LoRaMessageType.JoinRequest:
                    loRaPayload = new LoRaPayloadJoinRequest();
                    return(true);

                case LoRaMessageType.JoinAccept:
                    loRaPayload = new LoRaPayloadJoinAccept(convertedInputMessage, appKey);
                    return(true);

                case LoRaMessageType.UnconfirmedDataDown:
                case LoRaMessageType.UnconfirmedDataUp:
                case LoRaMessageType.ConfirmedDataUp:
                case LoRaMessageType.ConfirmedDataDown:
                    loRaPayload = new LoRaPayloadData();
                    return(true);
                }
            }

            loRaPayload = null;
            return(false);
        }
示例#2
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));
        }
        public static bool TryCreateLoRaPayload(Rxpk rxpk, out LoRaPayload loRaPayloadMessage)
        {
            byte[] convertedInputMessage = Convert.FromBase64String(rxpk.Data);
            var    messageType           = convertedInputMessage[0];

            switch (messageType)
            {
            case (int)LoRaMessageType.UnconfirmedDataUp:
            case (int)LoRaMessageType.ConfirmedDataUp:
                loRaPayloadMessage = new LoRaPayloadData(convertedInputMessage);
                break;

            case (int)LoRaMessageType.JoinRequest:
                loRaPayloadMessage = new LoRaPayloadJoinRequest(convertedInputMessage);
                break;

            default:
                loRaPayloadMessage = null;
                return(false);
            }

            loRaPayloadMessage.LoRaMessageType = (LoRaMessageType)messageType;
            return(true);
        }
示例#4
0
        /// <summary>
        /// This contructor is used in case of uplink message, hence we don't know the message type yet
        /// </summary>
        /// <param name="inputMessage"></param>
        public LoRaMessageWrapper(byte[] inputMessage, bool server = false, string AppKey = "")
        {
            // packet normally sent by the gateway as heartbeat. TODO find more elegant way to integrate.
            PhysicalPayload = new PhysicalPayload(inputMessage, server);
            if (PhysicalPayload.message != null)
            {
                var payload = Encoding.Default.GetString(PhysicalPayload.message);

                // todo ronnie implement a better logging by message type
                if (!payload.StartsWith("{\"stat"))
                {
                    Logger.Log($"Physical dataUp {payload}", Logger.LoggingLevel.Full);

                    // Deserialized for uplink messages
                    var payloadObject = JsonConvert.DeserializeObject <UplinkPktFwdMessage>(payload);
                    PktFwdPayload = payloadObject;
                    // set up the parts of the raw message
                    // status message
                    if (PktFwdPayload != null)
                    {
                        // if there is no packet, then it maybe a downlink message
                        if (PktFwdPayload.GetPktFwdMessage().Rxpks.Count > 0)
                        {
                            if (PktFwdPayload.GetPktFwdMessage().Rxpks[0].data != null)
                            {
                                byte[] convertedInputMessage = Convert.FromBase64String(PktFwdPayload.GetPktFwdMessage().Rxpks[0].data);
                                var    messageType           = convertedInputMessage[0] >> 5;
                                LoRaMessageType = (LoRaMessageType)messageType;
                                // Uplink Message
                                if (messageType == (int)LoRaMessageType.UnconfirmedDataUp)
                                {
                                    LoRaPayloadMessage = new LoRaPayloadData(convertedInputMessage);
                                }
                                else if (messageType == (int)LoRaMessageType.ConfirmedDataUp)
                                {
                                    LoRaPayloadMessage = new LoRaPayloadData(convertedInputMessage);
                                }
                                else if (messageType == (int)LoRaMessageType.JoinRequest)
                                {
                                    LoRaPayloadMessage = new LoRaPayloadJoinRequest(convertedInputMessage);
                                }

                                IsLoRaMessage = true;
                            }
                        }
                        else
                        {
                            // deselrialize for a downlink message
                            var payloadDownObject = JsonConvert.DeserializeObject <DownlinkPktFwdMessage>(payload);
                            if (payloadDownObject != null)
                            {
                                if (payloadDownObject.txpk != null)
                                {
                                    // if we have data, it is a downlink message
                                    if (payloadDownObject.txpk.data != null)
                                    {
                                        byte[] convertedInputMessage = Convert.FromBase64String(payloadDownObject.txpk.data);
                                        var    messageType           = convertedInputMessage[0] >> 5;
                                        LoRaMessageType = (LoRaMessageType)messageType;
                                        if (messageType == (int)LoRaMessageType.JoinAccept)
                                        {
                                            LoRaPayloadMessage = new LoRaPayloadJoinAccept(convertedInputMessage, AppKey);
                                        }
                                        IsLoRaMessage = true;
                                    }
                                }
                                else
                                {
                                    Logger.Log("Error: " + payload, Logger.LoggingLevel.Full);
                                }
                            }
                        }
                    }
                }
                else
                {
                    Logger.Log($"Statistic: {payload}", Logger.LoggingLevel.Full);
                    IsLoRaMessage = false;
                }
            }
            else
            {
                IsLoRaMessage = false;
            }
        }