Esempio n. 1
0
        public UOP_Package ReadPackage()
        {
            UOP_Package pack = new UOP_Package();

            byte[] buf = new byte[2048];
            pack.Received = false;
            int size = Read(buf);

            if (size >= UOP_HeaderSize)
            {
                pack.Marker    = (uint)(((int)buf[UOP_Marker_Pos]));
                pack.Marker   += (uint)(((int)buf[UOP_Marker_Pos + 1]) << 8);
                pack.Marker   += (uint)(((int)buf[UOP_Marker_Pos + 2]) << 16);
                pack.Marker   += (uint)(((int)buf[UOP_Marker_Pos + 3]) << 24);
                pack.Cmd       = buf[UOP_Cmd_Pos];
                pack.Flags     = buf[UOP_Flags_Pos];
                pack.DataSize  = (UInt16)((int)buf[UOP_DataSize_Pos]);
                pack.DataSize += (UInt16)((int)buf[UOP_DataSize_Pos + 1] << 8);
                pack.CheckSum  = buf[UOP_CheckSum_Pos];
                pack.Data      = buf;
                if (pack.CheckSum == GetCheckSum(buf, size))
                {
                    pack.Received = true;
                }
                return(pack);
            }
            return(pack);
        }
Esempio n. 2
0
 public UOP_StatusType MCU_Write(int Addr, byte Count, ref byte[] buf)
 {
     byte[] data = new byte[UOP_RW_DataHeader_Size + Count];
     BitConverter.GetBytes(Addr).CopyTo(data, UOP_RW_Addr);
     data[UOP_RW_Size] = Count;
     Array.Copy(buf, 0, data, UOP_RW_DataHeader_Size, Count);
     if (SendMsg(UOP_MCU_Write, data, UOP_RW_DataHeader_Size + Count))
     {
         UOP_Package pack = ReadPackage();
         if (pack.Received)
         {
             int p = UOP_HeaderSize;
             if ((UOP_StatusType)pack.Data[p++] == UOP_StatusType.OK)
             {
                 int  Addr2  = BitConverter.ToInt32(pack.Data, UOP_HeaderSize + UOP_RW_Addr_cb);
                 byte Count2 = pack.Data[UOP_HeaderSize + UOP_RW_Size_cb];
                 if (Count == Count2 && Addr == Addr2)
                 {
                     return(UOP_StatusType.OK);
                 }
             }
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 3
0
 public UOP_StatusType LB_Start()
 {
     if (SendMsg(UOP_Start, null, 0))
     {
         UOP_Package pack = ReadPackage();
         if (pack.Received)
         {
             int p = UOP_HeaderSize;
             if ((UOP_StatusType)pack.Data[p++] == UOP_StatusType.OK)
             {
                 return(UOP_StatusType.OK);
             }
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 4
0
 public UOP_StatusType MCU_WriteUnProtect()
 {
     if (SendMsg(UOP_MCU_WriteUnProtect, null, 0))
     {
         UOP_Package pack = ReadPackage();
         if (pack.Received)
         {
             int p = UOP_HeaderSize;
             if ((UOP_StatusType)pack.Data[p] == UOP_StatusType.OK)
             {
                 return(UOP_StatusType.OK);
             }
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 5
0
        public UOP_StatusType MCU_EraseAll()
        {
            ushort Count = 0xFFFF; // for global mass erase

            byte[] data = new byte[UOP_EErase_DataHeader_Size];
            BitConverter.GetBytes(Count).CopyTo(data, UOP_EErase_Count);

            if (SendMsg(UOP_MCU_ExtendedErase, data, UOP_EErase_DataHeader_Size))
            {
                UOP_Package pack = ReadPackage();
                if (pack.Received)
                {
                    return((UOP_StatusType)pack.Data[UOP_HeaderSize]);
                }
            }
            return(UOP_StatusType.Error);
        }
Esempio n. 6
0
 public UOP_StatusType MCU_GV()
 {
     if (SendMsg(UOP_MCU_GV, null, 0))
     {
         UOP_Package pack = ReadPackage();
         if (pack.Received)
         {
             int p = UOP_HeaderSize;
             if ((UOP_StatusType)pack.Data[p++] == UOP_StatusType.OK)
             {
                 BootGV.BootVersion = pack.Data[p];
                 return(UOP_StatusType.OK);
             }
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 7
0
 public UOP_StatusType UART_Send(UInt16 Count, ref byte[] buf)
 {
     if (Count > 0 && buf != null)
     {
         byte[] data = new byte[Count + 2];
         BitConverter.GetBytes(Count).CopyTo(data, 0);
         Array.Copy(buf, 0, data, 2, Count);
         if (SendMsg(UOP_UART_Send, data, Count + 2))
         {
             UOP_Package pack = ReadPackage();
             if (pack.Received)
             {
                 return((UOP_StatusType)pack.Data[UOP_HeaderSize]);
             }
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 8
0
 public UOP_StatusType MCU_WriteProtect(byte Count, byte[] sectors)
 {
     if (Count == 0)
     {
         return(UOP_StatusType.Error);
     }
     byte[] data = new byte[UOP_WriteProtect_DataHeader_Size + Count];
     data[UOP_WriteProtect_Count] = (byte)(Count - 1);
     sectors.CopyTo(data, UOP_WriteProtect_DataHeader_Size);
     if (SendMsg(UOP_MCU_WriteProtect, data, UOP_WriteProtect_DataHeader_Size + Count))
     {
         UOP_Package pack = ReadPackage();
         if (pack.Received)
         {
             return((UOP_StatusType)pack.Data[UOP_HeaderSize]);
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 9
0
 public UOP_StatusType MCU_GetID()
 {
     if (SendMsg(UOP_MCU_GetID, null, 0))
     {
         UOP_Package pack = ReadPackage();
         if (pack.Received)
         {
             int p = UOP_HeaderSize;
             if ((UOP_StatusType)pack.Data[p++] == UOP_StatusType.OK)
             {
                 BootID.ID    = new byte[2];
                 BootID.ID[0] = pack.Data[p++];
                 BootID.ID[1] = pack.Data[p++];
                 BootID.Count = 2;
                 return(UOP_StatusType.OK);
             }
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 10
0
        public string GetTime()
        {
            string time = "";

            if (SendMsg(UOP_GetTime, null, 0))
            {
                UOP_Package pack = ReadPackage();
                if (pack.Received)
                {
                    if ((UOP_StatusType)pack.Data[UOP_HeaderSize] == UOP_StatusType.OK)
                    {
                        int len = pack.Data[UOP_HeaderSize + 1];
                        int p   = 0;
                        for (; p < len; p++)
                        {
                            time += (char)pack.Data[UOP_HeaderSize + 2 + p];
                        }
                    }
                }
            }

            return(time);
        }
Esempio n. 11
0
 public UOP_StatusType MCU_Get()
 {
     if (SendMsg(UOP_MCU_Get, null, 0))
     {
         UOP_Package pack = ReadPackage();
         if (pack.Received)
         {
             int p = UOP_HeaderSize;
             if ((UOP_StatusType)pack.Data[p++] == UOP_StatusType.OK)
             {
                 BootInfo.BootVersion   = pack.Data[p++];
                 BootInfo.CommandsCount = pack.Data[p++];
                 BootInfo.Commands      = new byte[BootInfo.CommandsCount];
                 for (int n = 0; n < BootInfo.CommandsCount; n++)
                 {
                     BootInfo.Commands[n] = pack.Data[p++];
                 }
                 return(UOP_StatusType.OK);
             }
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 12
0
 public UOP_StatusType DeleteFile(string FileName)
 {
     FileName = Path.GetFileName(FileName);
     byte[] data = new byte[UOP_FS_DeleteFile_HeaderSize + FileName.Length];
     if (FileName.Length > 255)
     {
         return(UOP_StatusType.Error);
     }
     data[UOP_FS_DeleteFile_NameLength] = (byte)FileName.Length;
     Encoding.UTF8.GetBytes(FileName).CopyTo(data, UOP_FS_DeleteFile_Name);
     if (SendMsg(UOP_FS_DeleteFile, data, data.Length))
     {
         UOP_Package pack = ReadPackage();
         if (pack.Received)
         {
             int p = UOP_HeaderSize;
             if ((UOP_StatusType)pack.Data[p] == UOP_StatusType.OK || (UOP_StatusType)pack.Data[p] == UOP_StatusType.FS_FileDoesNotExist)
             {
                 return(UOP_StatusType.OK);
             }
         }
     }
     return(UOP_StatusType.Error);
 }
Esempio n. 13
0
        public UOP_StatusType MCU_ExtendedErase(UInt16 Count, UInt16[] pages)
        {
            if (Count >= 0xFFF0 || Count == 0)
            {
                return(UOP_StatusType.Error);
            }
            byte[] data = new byte[UOP_EErase_DataHeader_Size + Count * 2];
            BitConverter.GetBytes(Count - 1).CopyTo(data, UOP_EErase_Count);
            int p = UOP_EErase_DataHeader_Size;

            for (int n = 0; n < Count; n++, p += 2)
            {
                BitConverter.GetBytes(pages[n]).CopyTo(data, p);
            }
            if (SendMsg(UOP_MCU_ExtendedErase, data, UOP_EErase_DataHeader_Size + Count * 2))
            {
                UOP_Package pack = ReadPackage();
                if (pack.Received)
                {
                    return((UOP_StatusType)pack.Data[UOP_HeaderSize]);
                }
            }
            return(UOP_StatusType.Error);
        }
Esempio n. 14
0
        public UOP_StatusType WriteFile(string FileName, bool Create)
        {
            using (BinaryReader file = new BinaryReader(File.Open(FileName, FileMode.Open)))
            {
                byte[] buf;
                int    FileSize = (int)file.BaseStream.Length;
                UOP_FS_WriteFileHeader header = new UOP_FS_WriteFileHeader();
                FileInfo finfo = new FileInfo(FileName);
                FileName = finfo.Name;
                if (Create)
                {
                    header.Flags = 1;
                }
                if (FileName.Length > 255)
                {
                    return(UOP_StatusType.Error);
                }
                header.NameLength = (byte)FileName.Length;
                int BlocksCount = (FileSize / UOP_FS_WriteFile_BlockSize);
                if ((FileSize % UOP_FS_WriteFile_BlockSize) > 0)
                {
                    BlocksCount++;
                }
                uint pos = 0;
                for (int block = 0; block < BlocksCount; block++, pos += UOP_FS_WriteFile_BlockSize)
                {
                    header.DataPos = pos;
                    if ((FileSize - pos) < UOP_FS_WriteFile_BlockSize)
                    {
                        header.DataSize = (ushort)(FileSize - pos);
                    }
                    else
                    {
                        header.DataSize = UOP_FS_WriteFile_BlockSize;
                    }
                    buf = new byte[UOP_FS_WriteFile_HeaderSize + header.NameLength + header.DataSize];

                    buf[UOP_FS_WriteFile_Flags]      = header.Flags;
                    buf[UOP_FS_WriteFile_NameLength] = header.NameLength;

                    /*byte[] ValueArr = BitConverter.GetBytes(header.DataPos);
                     * Array.Reverse(ValueArr);
                     * ValueArr.CopyTo(buf, UOP_FS_WriteFile_DataPos);
                     * ValueArr = BitConverter.GetBytes(header.DataSize);
                     * Array.Reverse(ValueArr);
                     * ValueArr.CopyTo(buf, UOP_FS_WriteFile_DataPos);*/

                    BitConverter.GetBytes(header.DataPos).CopyTo(buf, UOP_FS_WriteFile_DataPos);
                    BitConverter.GetBytes(header.DataSize).CopyTo(buf, UOP_FS_WriteFile_DataSize);
                    Encoding.UTF8.GetBytes(FileName).CopyTo(buf, UOP_FS_WriteFile_HeaderSize);
                    file.ReadBytes(header.DataSize).CopyTo(buf, UOP_FS_WriteFile_HeaderSize + header.NameLength);

                    for (int try_count = 0; try_count < 2; try_count++)
                    {
                        if (SendMsg(UOP_FS_WriteFile, buf, buf.Length))
                        {
                            UOP_Package pack = ReadPackage();
                            if (pack.Received)
                            {
                                if ((UOP_StatusType)pack.Data[UOP_HeaderSize] == UOP_StatusType.OK)
                                {
                                    break;
                                }
                            }
                        }

                        if (try_count < 1)
                        {
                            Thread.Sleep(5000);
                            if (LB_Stop() != ESP8266_CommunicationProtocol.UOP_StatusType.OK)
                            {
                                return(UOP_StatusType.Error);
                            }
                        }
                        else
                        {
                            return(UOP_StatusType.Error);
                        }
                    }
                    if (WriteFileProgress != null)
                    {
                        WriteFileProgress(((block + 1) * 100) / BlocksCount);
                    }
                    Thread.Sleep(10);
                }
            }
            return(UOP_StatusType.OK);
        }