Esempio n. 1
0
        private void Worker_upload_DoWork(object sender, DoWorkEventArgs e)
        {
            WorkerData workerData = e.Argument as WorkerData; // We gather the info from our class
            uint       offset     = 0;
            uint       total      = workerData.total;
            int        uiTransmitted;
            string     file = workerData.filename;
            int        progressnb;

            byte[]    writeBuffer = new byte[2048 * 8];
            byte[]    info        = workerData.info;
            byte[]    csum        = workerData.csum;
            bool      small       = false;
            ErrorCode eReturn;

            // Create a FileStream object to read a stream from a file
            FileStream   fileStream = System.IO.File.OpenRead(file);
            BinaryReader binReader  = new BinaryReader(fileStream);

            if (total > writeBuffer.Length) //if file is smaller then buffer, we'll skip this part.
            {
                // Filling the buffer with informative bytes...
                for (int i = 0; i < (int)info.Length; i++)
                {
                    writeBuffer[i] = info[i];
                }

                binReader.Read(writeBuffer, (int)info.Length, (int)(writeBuffer.Length - info.Length));
                bool infosent = false;

                while ((offset <= (total - writeBuffer.Length)) && !e.Cancel)
                {
                    if (Worker_upload.CancellationPending)
                    {
                        e.Cancel = true;
                    }

                    if (infosent) //to avoid overwriting the infos
                    {
                        writeBuffer = binReader.ReadBytes(writeBuffer.Length);
                    }

                    eReturn = mEpWriter.Write(writeBuffer, 1000, out uiTransmitted);
                    if (eReturn == ErrorCode.None)
                    {
                        offset    += (uint)writeBuffer.Length;
                        progressnb = (int)((float)((float)offset / (float)total) * 100.0F);
                        Worker_upload.ReportProgress(progressnb);
                        infosent = true;
                    }

                    else
                    {
                        binReader.Close();
                        fileStream.Close();
                        mEpWriter.Flush();
                        return;
                    }
                }
            }

            else //if file is smaller then buffer do this
            {
                small = true;
                t_log.AppendText("## Small file detected" + "\r\n");
                writeBuffer = new byte[total]; //buffer size = file size

                // Filling the buffer with informative bytes...
                for (int i = 0; i < (int)info.Length; i++)
                {
                    writeBuffer[i] = info[i];
                }

                binReader.Read(writeBuffer, (int)info.Length, (int)(writeBuffer.Length - info.Length));
                mEpWriter.Write(writeBuffer, 1000, out uiTransmitted);
                mEpWriter.Write(csum, 1000, out uiTransmitted);
                Worker_upload.ReportProgress(100);
                return;
            }

            if ((offset >= (total - writeBuffer.Length)) && !e.Cancel && !small)
            {
                //we create a buffer sized to the remaining bytes. This is to avoid writing a file too big.
                byte[] endBuffer = new byte[total - offset];
                endBuffer = binReader.ReadBytes(endBuffer.Length);
                mEpWriter.Write(endBuffer, 1000, out uiTransmitted);
                mEpWriter.Write(csum, 1000, out uiTransmitted);
                return;
            }
            mEpWriter.Flush();
            binReader.Close();
            fileStream.Flush();
            fileStream.Unlock(0, fileStream.Length);
            fileStream.Close();
        }
Esempio n. 2
0
        private void b_upload_Click(object sender, EventArgs e)
        {
            if (b_upload.Text == "Cancel")
            {
                Worker_upload.CancelAsync();
                if (Worker_upload.CancellationPending)
                {
                    t_log.AppendText("## Cancelling..." + "\r\n");
                }
                return;
            }

            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                filename = openFileDialog1.FileName;
                openFileDialog1.Dispose();
                t_log.AppendText("## Opening " + filename + "\r\n");
                byte[] infoBuffer = new byte[8];
                byte[] csumBuffer = new byte[2];
                byte[] bstart     = new byte[4];
                byte[] bsize      = new byte[4];

                // Create a FileStream object to write a stream to a file
                FileInfo file         = new FileInfo(filename);
                uint     fileSize     = (uint)file.Length;
                uint     fullFileSize = (uint)file.Length + (uint)infoBuffer.Length + (uint)csumBuffer.Length;
                //uint start = 0x30000000; // Kernel ram base
                uint       start      = dlAddr; // We start where we transfered the program
                FileStream fileStream = file.OpenRead();
                uint       end        = (start + fileSize);

                t_log.AppendText("## Calculating checksum... This might take some time on large files." + "\r\n");
                UInt16 csum = csum_upload(fileStream, fileSize);

                t_log.AppendText("## File Information: " + "\r\n"
                                 + "## File Size        : " + fileSize.ToString() + " (" + (((fileSize) / 1024) / 1024).ToString() + "MB"
                                 + " - " + ((fileSize) / 1024).ToString() + "KB)" + "\r\n"
                                 + "## Start Addr       : " + "0x" + start.ToString("x") + "\r\n"
                                 + "## End Addr         : " + "0x" + end.ToString("x") + "\r\n");


                /* 4 bytes address, 4 bytes length, data, 2 bytes csum,
                 * we have to spread the ints across 4 bytes (an int32 is 4 bytes - 32 bits / 8) */

                bstart     = BitConverter.GetBytes(start);        // 1
                bsize      = BitConverter.GetBytes(fullFileSize); // 2
                csumBuffer = BitConverter.GetBytes(csum);         // 3

                infoBuffer[0] = bstart[0];
                infoBuffer[1] = bstart[1];
                infoBuffer[2] = bstart[2];
                infoBuffer[3] = bstart[3];

                infoBuffer[4] = bsize[0];
                infoBuffer[5] = bsize[1];
                infoBuffer[6] = bsize[2];
                infoBuffer[7] = bsize[3];

                t_log.AppendText("## Checksum        : 0x" + csum.ToString("x04") + "\r\n");
                t_log.AppendText("## Got the info we need, now uploading data..." + "\r\n");
                b_upload.Enabled   = false;
                b_download.Enabled = false;


                // We load the data in our class.
                WorkerData workerData = new WorkerData
                {
                    total    = fullFileSize,
                    start    = start,
                    end      = end,
                    info     = infoBuffer,
                    csum     = csumBuffer,
                    filename = filename
                };

//                fileStream.Unlock(0, fileStream.Length);
                fileStream.Flush();
                fileStream.Close();
                b_upload.Enabled = true;
                b_upload.Text    = "Cancel";

                Worker_upload.RunWorkerAsync(workerData); // We spawn the thread.
            }
            else
            {
                openFileDialog1.Dispose(); // If no file is selected, free ressources.
            }
        }