public static void GetEEPROM(this Node node, Home home)
 {
     var memoryEEPROM = GetBinaryConfiguration(node, home).Item2;
     //guardamos el bin
     File.WriteAllBytes(node.Mac.ToString() + ".bin", memoryEEPROM);
     //guardamos el hex
     Hex.SaveBin2Hex(memoryEEPROM, node.Mac.ToString());
 }
        public async void SendConfiguration(Node node, Home home)
        {
            ushort nodeAddress = (ushort)node.Address;

            bool isNewTransaction = !this.currentWriteTransactions.ContainsKey(nodeAddress);
            bool isAbortedOrCompletedTransaction = isNewTransaction ?
                false : 
                this.currentWriteTransactions[nodeAddress].FragmentWriteTransaction.IsAborted ||
                this.currentWriteTransactions[nodeAddress].FragmentWriteTransaction.IsCompleted;

            if (isNewTransaction || isAbortedOrCompletedTransaction)
            {
                // If the last transaction was aborted or completed. Remove it to allow a new one.
                if (isAbortedOrCompletedTransaction)
                    this.currentWriteTransactions.Remove(nodeAddress);

                var configuration = node.GetBinaryConfiguration(home);

                var newTransaction = new FragmentWriteTransaction(this, OperationMessage.OPCodes.ConfigWrite, typeof(ConfigWriteStatusCodes), nodeAddress, configuration.Item2);
                this.currentWriteTransactions.Add(nodeAddress, new ConfigTransaction()
                {
                    Checksum = configuration.Item1,
                    FragmentWriteTransaction = newTransaction,
                    TimeFlag = (DateTime)(node.LastChecksumUpdate ?? DateTime.MinValue),
                });

                if (!await newTransaction.StartTransaction())
                {
                    //TODO: Check the problem
                    PrintLog(true, string.Format("An error occurred on configuration update for node 0x{0:X4}", nodeAddress));
                }
            }
        }
        /// <summary>
        /// Generates the EEPROM configuration.
        /// </summary>
        /// <param name="home">The home.</param>
        /// <returns>A tuple containing the checksum (Item1) and the raw bytes (Item2)</returns>
        public Tuple<ushort, byte[]> GenerateEEPROM(Home home)
        {
            _tempMemory = new List<Byte>();

            //Generar toda la memoria (la memoria se genera con CRC16 a "00 00")

            //ConfigHeader
            _tempMemory.AddRange(ConfigHeader());

            //NetworkConfig
            _tempMemory.AddRange(NetworkConfig(home));

            ushort pointerStartDinamicIndex = (ushort)_tempMemory.Count;
            _dinamicIndex = new List<ushort>();

            //fill with 00 in memory for Dinamic Index
            for (int i = 0; i < 13; i++)
            {
                _tempMemory.Add(0x00);
                _tempMemory.Add(0x00);
            }

            //DevicesConfig
            DevicesConfig();

            //TimeOperation
            _dinamicIndex.Add((ushort)_tempMemory.Count);
            _tempMemory.AddRange(TimeOperation());

            //Operation
            _dinamicIndex.Add((ushort)_tempMemory.Count);
            _tempMemory.AddRange(Operation((ushort)_tempMemory.Count));

            //OperationTimeRestriction
            _dinamicIndex.Add((ushort)_tempMemory.Count);
            _tempMemory.AddRange(OperationTimeRestriction());

            //OperationConditionRestrictions
            _dinamicIndex.Add((ushort)_tempMemory.Count);
            //TODO

            //free region
            _dinamicIndex.Add((ushort)_tempMemory.Count);

            //change directions of DinamicIndex using the DinamicIndexList
            DinamicIndex(pointerStartDinamicIndex);
            //DINAMIC END

            byte[] memory = _tempMemory.ToArray();

            //Calculamos el tamaño en bytes
            ushort sizeMemory = (ushort)memory.Length;

            memory[3] = sizeMemory.UshortToByte(_baseConfiguration.LittleEndian)[0];
            memory[4] = sizeMemory.UshortToByte(_baseConfiguration.LittleEndian)[1];

            //Generar el CRC
            byte[] crc = new Crc16().ComputeChecksumBytes(memory, _baseConfiguration.LittleEndian);

            //sustituimos el valor de CRC que esta en la posicion de memoria 0x05 0x06, no hace falta contar con endianidad
            memory[5] = crc[0];
            memory[6] = crc[1];

            ushort crcResult = BitConverter.ToUInt16(crc, 0);

            return new Tuple<ushort,byte[]>(crcResult, memory);
        }
 public static Tuple<ushort, byte[]> GetBinaryConfiguration(this Node node, Home home)
 {
     FirmwareUno fw = new FirmwareUno(node, 0x00); //TODO: Ojo
     return fw.GenerateEEPROM(home);
 }
        //Cogemos por defecto la configuration del NetworkManager
        private byte[] NetworkConfig(Home home)
        {
            List<byte> result = new List<byte>();

            //deviceAddress
            result.AddRange(((ushort)_node.Address).UshortToByte(_baseConfiguration.LittleEndian));

            ////chanel
            result.Add((byte)home.Security.Channel);

            ////panID
            result.AddRange(((ushort)home.Security.PanId).UshortToByte(_baseConfiguration.LittleEndian));

            ////securityKey
            result.AddRange(home.Security.GetSecurityKey());

            //networkRetriesLimit
            result.Add((byte)_node.NetworkRetries);

            return result.ToArray();
        }