public FileNode (MonoBrick.NXT.BrickFile file) { this.file = file; switch(file.FileType){ case FileType.Datalog: type = "Datalog"; break; case FileType.Firmware: type = "Firmware"; break; case FileType.Graphics: type = "Graphics"; break; case FileType.OnBrickProgram: type = "On-Brick"; break; case FileType.Program: type = "Program"; break; case FileType.Sound: type = "Sound"; break; case FileType.TryMeProgram: type = "Try-Me"; break; case FileType.Unknown: type = "Unknown"; break; default: type = "Unknown"; break; } }
/// <summary> /// Downloads a file from the brick /// </summary> /// <param name='destinationFileName'> /// Destination file name /// </param> /// <param name='brickFile'> /// Brick file to download /// </param> public void DownloadFile(string destinationFileName, BrickFile brickFile){ DownloadFile(destinationFileName, brickFile.Name); }
/// <summary> /// Deletes file. /// </summary> /// <param name='file'> /// File to delete /// </param> public void DeleteFile(BrickFile file){ DeleteFile(file.Name); }
/// <summary> /// Closes the file. /// </summary> /// <param name='file'> /// File to close /// </param> public void CloseFile(BrickFile file){ var command = new Command(CommandType.SystemCommand, CommandByte.Close, true); command.Append(file.Handle); Connection.Send(command); var reply = connection.Receive(); if(reply.HasError && reply.ErrorCode != (byte)BrickError.HandleAlreadyClosed){ Error.ThrowException(reply); } //compare the handle numbers }
/// <summary> /// Read a specefic number of bytes from a file /// </summary> /// <param name='file'> /// The file to read from /// </param> /// <param name='bytesToRead'> /// Bytes to read /// </param> public byte[] Read(BrickFile file, UInt16 bytesToRead){ var command = new Command(CommandType.SystemCommand, CommandByte.Read, true); command.Append(file.Handle); command.Append(bytesToRead); connection.Send(command); var reply = connection.Receive(); if(reply.HasError){ CloseFile(file); Error.ThrowException(reply); } //UInt16 bytesRead = reply.GetUInt16(4); //use this for some error checking byte[] result = reply.GetData(6); if(OnBytesRead != null) OnBytesRead(result.Length); return result; }
/// <summary> /// Read all bytes from a file /// </summary> /// <param name='file'> /// The file to read from /// </param> public byte[] Read(BrickFile file){ byte[] data = new byte[file.Size]; byte[] newData = null; int bytesRead = 0; while(bytesRead < file.Size){ if((file.Size - bytesRead) >= MaxBytesRead){ newData = Read(file,MaxBytesRead); Array.Copy(newData,0,data,bytesRead,MaxBytesRead); if(newData.Length != MaxBytesRead){ CloseFile(file); throw new BrickException(BrickError.UndefinedFileError); } } else{ UInt32 bytesToRead = ((UInt32)file.Size - (UInt32)bytesRead); newData = Read(file,(ushort)bytesToRead); Array.Copy(newData,0,data,bytesRead,bytesToRead); if(newData.Length != bytesToRead){ CloseFile(file); throw new BrickException(BrickError.UndefinedFileError); } } bytesRead = bytesRead + newData.Length; } return data; }
private int Write(BrickFile file, byte[] data, int offset, int length){ var command = new Command(CommandType.SystemCommand, CommandByte.Write,true); command.Append(file.Handle); command.Append(data, offset, length); connection.Send(command); //Console.WriteLine(file.Size); //command.Print(); var reply = connection.Receive(); Error.CheckForError(reply,6, delegate(){CloseFile(file);}); int result = (int) reply.GetUInt16(4);//The number of bytes written if(OnBytesWritten != null) OnBytesWritten(result); return result; }
/// <summary> /// Write a byte array to a file /// </summary> /// <param name='file'> /// The file /// </param> /// <param name='data'> /// Data to write /// </param> public int Write(BrickFile file, byte[] data){ int bytesWritten = 0; while(bytesWritten < data.Length){ if((data.Length - bytesWritten) >= MaxBytesWrite){ if(Write(file,data,bytesWritten,MaxBytesWrite) != MaxBytesWrite){ CloseFile(file); throw new BrickException(BrickError.UndefinedFileError); } bytesWritten += MaxBytesRead; } else{ int bytesToWrite = (data.Length - bytesWritten); if(Write(file,data,bytesWritten,bytesToWrite) != bytesToWrite){ CloseFile(file); throw new BrickException(BrickError.UndefinedFileError); } bytesWritten += bytesToWrite; } } return bytesWritten; }
private BrickFile FindNext(BrickFile file){ var command = new Command(CommandType.SystemCommand,CommandByte.FindNext,true); command.Append(file.Handle); connection.Send(command); var reply = connection.Receive(); if(reply.HasError && reply.ErrorCode != (byte)BrickError.FileNotFound){ Error.ThrowException(reply); } if(reply.ErrorCode == (byte)BrickError.FileNotFound){ return new BrickFile();//empty file } if(reply.Length != 28){ throw new BrickException(BrickError.WrongNumberOfBytes); } BrickFile newFile = new BrickFile(reply.GetString(4),reply[3],reply.GetUInt32(24)); try{ CloseFile(newFile); } catch(MonoBrickException e){ if(e.ErrorCode != (byte)BrickError.HandleAlreadyClosed) Error.ThrowException(e.ErrorCode); } return newFile; }