예제 #1
0
        public static ushort TestCrc(string data, int test)
        {
            var combinedBitArray = NRF24Helpers.GetBitArrayFromHexString(data);

            ushort testCrc = 0xFFFF;

            for (var offsetFront = 0; offsetFront <= combinedBitArray.Length; offsetFront++)
            {
                for (var i = combinedBitArray.Length - offsetFront; i >= 0; i--)
                {
                    //backwards stripping
                    var subBitArray = NRF24Helpers.GetPartOfBitArray(combinedBitArray, offsetFront, i);
                    testCrc = NRF24Helpers.GetCrc16(subBitArray);

                    if (testCrc == 0xB4CC && test == 1)
                    {
                        var b = "a";
                    }
                    if (testCrc == 0x52F7 && test == 2)
                    {
                        var b = "a";
                    }
                    if (testCrc == 0x354E && test == 3)
                    {
                        var b = "a";
                    }
                }
            }

            return(testCrc);
        }
예제 #2
0
        private NRF24Data CreateNRF24Data(NRF24Structure nrf24Structure)
        {
            if (nrf24Structure.NRF24Header == null || nrf24Structure.Data.Length < 5)
            {
                return(null);
            }

            // Set the NRF24 data part
            var nrf24Data = new NRF24Data
            {
                NodeAddress = $"{nrf24Structure.NRF24Header.Address}{nrf24Structure.Data.Substring(0, 2)}"
            };

            var controlFieldString   = nrf24Structure.Data.Substring(2, 4);
            var controlFieldBitArray = NRF24Helpers.GetBitArrayFromHexString(controlFieldString);

            nrf24Data.PayloadLength = NRF24Helpers.GetPartOfBitArray(controlFieldBitArray, 0, 6);
            nrf24Data.Pid           = NRF24Helpers.GetPartOfBitArray(controlFieldBitArray, 6, 2);
            nrf24Data.NoAck         = controlFieldBitArray[9];

            nrf24Data.Payload = nrf24Structure.Data.Substring(4, nrf24Structure.Data.Length - 4);
            var payloadFieldBitArray = NRF24Helpers.GetBitArrayFromHexString(nrf24Data.Payload);

            // For last byte only the MSbit has value; rest will be cleared
            payloadFieldBitArray = NRF24Helpers.GetPartOfBitArray(payloadFieldBitArray, 0, payloadFieldBitArray.Length - 7);

            // Get the packet crc (the crc is located in last two bytes of the packet)
            var crcOffset = payloadFieldBitArray.Length - 16;

            nrf24Data.PacketCrc = NRF24Helpers.GetPartOfBitArray(payloadFieldBitArray, crcOffset, 16);

            // Remove the crc bits from the bitarray before it is processed further
            nrf24Data.PayloadBitArray = NRF24Helpers.GetPartOfBitArray(payloadFieldBitArray, 0, payloadFieldBitArray.Length - 16);

            //TODO what about the string payload??

            // ****
            // Calculate the crc ourselfs

            // Get the payload in bits, as the bitArray is already stripped of all unneeded stuff, the length is the amount of bits
            //var payloadLengthBits = payloadFieldBitArray.Length;
            //var packetLengthBits = 40 + 9 + payloadLengthBits;
            // As the crc is calculated over the address, packet control field and payload, attach the two fields
            //var combinedString = $"{this.NRF24Header.Address}{this.Data}";
            //var combinedString = $"{this.Data}";

            // Create the NRF24Mysensor part
            nrf24Data.NRF24Mysensor = CreateNRF24Mysensor(nrf24Data);

            return(nrf24Data);
        }
예제 #3
0
        private NRF24Mysensor CreateNRF24Mysensor(NRF24Data nrf24Data)
        {
            // Check if the bitArray contains enough information to be processed
            if (nrf24Data.PayloadBitArray.Length < 57)
            {
                return(null);
            }

            // We should start at 1 as the string is not byte aligned
            var bitOffset = 1;

            // Set the MySensors data part
            var nrf24Mysensor = new NRF24Mysensor();

            nrf24Mysensor.Last = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset);
            bitOffset         += 8;

            nrf24Mysensor.Sender = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset);
            bitOffset           += 8;

            nrf24Mysensor.Destination = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray,
                                                                         bitOffset);
            bitOffset += 8;

            nrf24Mysensor.Length = NRF24Helpers.GetPartOfBitArray(nrf24Data.PayloadBitArray, bitOffset, 5);
            bitOffset           += 5;

            nrf24Mysensor.Version = NRF24Helpers.GetPartOfBitArray(nrf24Data.PayloadBitArray, bitOffset, 3);
            bitOffset            += 3;

            nrf24Mysensor.DataType = NRF24Helpers.GetPartOfBitArray(nrf24Data.PayloadBitArray, bitOffset, 3);
            bitOffset += 3;

            nrf24Mysensor.IsAck = nrf24Data.PayloadBitArray[bitOffset];
            bitOffset++;

            nrf24Mysensor.ReqAck = nrf24Data.PayloadBitArray[bitOffset];
            bitOffset++;

            nrf24Mysensor.CommandType = NRF24Helpers.GetPartOfBitArray(nrf24Data.PayloadBitArray, bitOffset, 3);
            bitOffset += 3;

            nrf24Mysensor.Type = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset);
            bitOffset         += 8;

            nrf24Mysensor.Sensor = NRF24Helpers.GetByteFromBitArray(nrf24Data.PayloadBitArray, bitOffset);
            bitOffset           += 8;

            nrf24Mysensor.PayloadBitArray = NRF24Helpers.GetPartOfBitArray(
                nrf24Data.PayloadBitArray,
                bitOffset,
                nrf24Data.PayloadBitArray.Length - bitOffset);

            // Find, is enabled, the VeraDevices corresponding to the package
            if (_generalSettings.LookupMysensorsNodeViaVera)
            {
                // Check if it is the gateway that sends the message
                if (nrf24Mysensor.Sender == 0)
                {
                    nrf24Mysensor.SenderVeraDevice = _veraSettings.VeraDevices
                                                     .SingleOrDefault(a_item => a_item.IsGateway);
                }
                else
                {
                    nrf24Mysensor.SenderVeraDevice = _veraSettings.VeraDevices
                                                     .SingleOrDefault(
                        a_item => a_item.VeraDeviceAltID != null &&
                        a_item.VeraDeviceAltID.NodeID == nrf24Mysensor.Sender &&
                        a_item.VeraDeviceAltID.ChildID == 255);
                }

                // Check if it is the gateway that receives the message
                if (nrf24Mysensor.Destination == 0)
                {
                    nrf24Mysensor.DestinationVeraDevice = _veraSettings.VeraDevices
                                                          .SingleOrDefault(a_item => a_item.IsGateway);
                }
                else
                {
                    nrf24Mysensor.DestinationVeraDevice = _veraSettings.VeraDevices
                                                          .Where(a_item => a_item.VeraDeviceAltID != null)
                                                          .SingleOrDefault(a_item =>
                                                                           a_item.VeraDeviceAltID.NodeID == nrf24Mysensor.Destination &&
                                                                           a_item.VeraDeviceAltID.ChildID == 255);
                }

                nrf24Mysensor.SensorVeraDevice = _veraSettings.VeraDevices
                                                 .Where(a_item => a_item.VeraDeviceAltID != null)
                                                 .SingleOrDefault(
                    a_item => a_item.VeraDeviceAltID.NodeID == nrf24Mysensor.Sender &&
                    a_item.VeraDeviceAltID.ChildID == nrf24Mysensor.Sensor);
            }

            return(nrf24Mysensor);
        }