COSA
An Object-Oriented Platform for Arduino Programming
CFFS.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_CFFS_HH
22 #define COSA_CFFS_HH
23 
24 #include "Cosa/FS.hh"
25 #include "Cosa/Flash.hh"
26 #include "Cosa/IOStream.hh"
27 
35 class CFFS {
36 public:
38  static const size_t FILENAME_MAX = 22;
39 
40 protected:
44  struct descr_t {
45  uint16_t type;
46  uint32_t size;
47  uint32_t ref;
49  };
50 
74  enum {
75  CFFS_TYPE = 0xf5cf,
76  FILE_ENTRY_TYPE = 0x8001,
77  FILE_BLOCK_TYPE = 0x8002,
78  DIR_ENTRY_TYPE = 0x8003,
79  DIR_BLOCK_TYPE = 0x8004,
80  FREE_TYPE = 0xffff,
81  ALLOC_MASK = 0x8000,
82  TYPE_MASK = 0x7fff
83  };
84 
88  static const uint32_t NULL_REF = 0xffffffffL;
89 
90 public:
101  class File : public IOStream::Device {
102  public:
107  File() : IOStream::Device(), m_flags(0) {}
108 
117  int open(const char* filename, uint8_t oflag = O_READ);
118 
124  bool is_open(void) const
125  {
126  return ((m_flags & O_RDWR) != 0);
127  }
128 
135  int remove();
136 
142  int close();
143 
152  int seek(uint32_t pos, uint8_t whence = SEEK_SET);
153 
158  uint32_t tell()
159  {
160  return (m_current_pos);
161  }
162 
168  int rewind()
169  {
170  return (seek(0L));
171  }
172 
177  uint32_t size()
178  {
179  return (m_file_size);
180  }
181 
184 
194  virtual int write(const void *buf, size_t size);
195 
205  virtual int write_P(const void* buf, size_t size);
206 
213  virtual int getchar();
214 
217 
227  virtual int read(void* buf, size_t size);
228 
229  protected:
230  uint8_t m_flags;
231  uint32_t m_entry_addr;
234  uint32_t m_file_size;
235  uint32_t m_current_addr;
236  uint32_t m_current_pos;
237 
248  int write(const void* buf, size_t size, bool progmem);
249  };
250 
257  static bool begin(Flash::Device* flash);
258 
265  static int ls(IOStream& outs);
266 
274  static int rm(const char* filename);
275 
283  static int cd(const char* filename);
284 
292  static int mkdir(const char* filename);
293 
301  static int rmdir(const char* filename);
302 
311  static int format(Flash::Device* flash, const char* name);
312 
313  friend class File;
314 
315 protected:
317  static const size_t DIR_MAX = 16;
318 
321 
323  static uint32_t current_dir_addr;
324 
334  static int read(void* dest, uint32_t src, size_t size);
335 
345  static int write(uint32_t dest, const void* src, size_t size);
346 
356  static int write_P(uint32_t dest, const void* src, size_t size);
357 
366  static int lookup(const char* filename, descr_t &entry, uint32_t& addr);
367 
380  static int create(const char* filename, uint16_t type, uint8_t flags,
381  descr_t &entry, uint32_t &addr);
382 
390  static int remove(uint32_t addr, uint16_t type);
391 
396  static uint32_t next_free_sector();
397 
402  static uint32_t next_free_directory();
403 
413  static int find_end_of_file(uint32_t sector, uint32_t &pos, uint32_t &size);
414 };
415 
416 #endif
static int write(uint32_t dest, const void *src, size_t size)
Definition: CFFS.cpp:484
uint16_t type
Type of file and entry state.
Definition: CFFS.hh:45
Absolute position.
Definition: FS.hh:47
virtual int write(const void *buf, size_t size)
virtual int read(void *buf, size_t size)
static const size_t FILENAME_MAX
Definition: CFFS.hh:38
static int find_end_of_file(uint32_t sector, uint32_t &pos, uint32_t &size)
Definition: CFFS.cpp:570
static uint32_t current_dir_addr
Definition: CFFS.hh:323
Directory reference entry.
Definition: CFFS.hh:78
Type mask.
Definition: CFFS.hh:82
CFFS::descr_t m_entry
Cached directory entry.
Definition: CFFS.hh:232
Open for reading.
Definition: FS.hh:28
File System Master header.
Definition: CFFS.hh:75
uint32_t m_entry_addr
Entry address.
Definition: CFFS.hh:231
static int rm(const char *filename)
Definition: CFFS.cpp:285
static int cd(const char *filename)
Definition: CFFS.cpp:293
Directory block.
Definition: CFFS.hh:79
static Flash::Device * device
Definition: CFFS.hh:320
static int mkdir(const char *filename)
Definition: CFFS.cpp:307
static const size_t DIR_MAX
Definition: CFFS.hh:317
uint8_t m_flags
File open flags.
Definition: CFFS.hh:230
File data block.
Definition: CFFS.hh:77
static int lookup(const char *filename, descr_t &entry, uint32_t &addr)
Definition: CFFS.cpp:366
Definition: CFFS.hh:35
static int create(const char *filename, uint16_t type, uint8_t flags, descr_t &entry, uint32_t &addr)
Definition: CFFS.cpp:391
File descriptor entry.
Definition: CFFS.hh:76
char name[FILENAME_MAX]
Printable name of object(zero terminated).
Definition: CFFS.hh:48
static uint32_t next_free_sector()
Definition: CFFS.cpp:502
static const uint32_t NULL_REF
Definition: CFFS.hh:88
Open for reading and writing.
Definition: FS.hh:32
uint32_t m_file_size
File size.
Definition: CFFS.hh:234
int rewind()
Definition: CFFS.hh:168
Allocated mask.
Definition: CFFS.hh:81
File()
Definition: CFFS.hh:107
static bool begin(Flash::Device *flash)
Definition: CFFS.cpp:228
uint32_t ref
Reference value (pointer).
Definition: CFFS.hh:47
uint32_t tell()
Definition: CFFS.hh:158
static int ls(IOStream &outs)
Definition: CFFS.cpp:253
bool is_open(void) const
Definition: CFFS.hh:124
uint32_t m_current_addr
Current flash address.
Definition: CFFS.hh:235
static int write_P(uint32_t dest, const void *src, size_t size)
Definition: CFFS.cpp:493
uint32_t size()
Definition: CFFS.hh:177
static int format(Flash::Device *flash, const char *name)
Definition: CFFS.cpp:322
static int read(void *dest, uint32_t src, size_t size)
Definition: CFFS.cpp:475
static uint32_t next_free_directory()
Definition: CFFS.cpp:531
CFFS::descr_t m_sector
Cached sector header.
Definition: CFFS.hh:233
uint32_t m_current_pos
Current logical position.
Definition: CFFS.hh:236
Free descriptor.
Definition: CFFS.hh:80
static int rmdir(const char *filename)
Definition: CFFS.cpp:315
uint32_t size
Number of bytes (including header).
Definition: CFFS.hh:46