Advertisement
marcushund

sfs-demo.h

Jun 20th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. /*
  2.  * File:   sfs.h
  3.  * Author: Marcus Hund (@marcus at community.spark.io)
  4.  * Created on May 30, 2014, 2:21 PM
  5.  * SFS: Simple|Silly File System
  6.  * This class creates a files system that can hold 16 files, each with a max size of 64kB. It's silly, in the sense that it
  7.  * uses 1 4k block for the directory entry and 16 4K blocks per file. These parameters can be tailored to your need off course.
  8.  * But keep in mind that the flash memory is divided in sectors of 4kB.
  9.  * It's working flawlessly in a webserver I created.
  10.  *
  11.  */
  12. #include "stdint.h"
  13. #include "string.h"
  14. #include "spark_wiring_string.h"
  15. #include "stm32_it.h"
  16. #include "spark_wiring_usartserial.h"
  17. #include "spark_wiring_tcpclient.h"
  18. #ifndef SFS_H
  19. #define SFS_H
  20.  
  21. #define MAX_FILES 16
  22. #define FLASH_USER_SPACE 0x80000
  23. #define FILE_NONE 0
  24. #define FILE_WRITE 1
  25. #define FILE_READ 2
  26.  
  27. #define DIR_ENTRY 0x1000
  28.  
  29. #define MAX_NAME_LEN 32
  30. #define FILE_LEN_OFFSET MAX_NAME_LEN + 2
  31. #define MAX_FILESIZE_LEN 8
  32. #define MAX_FILE_BLOCKS 16
  33.  
  34. #define FILE_NOT_FOUND -1;
  35. class sfs {
  36. public:
  37.     sfs();
  38.     int dir(char * entry);
  39.     int dir(char * entry, int * ndx);
  40.     bool open(char* name);
  41.     bool create(char* name);
  42.     void append(char * str);
  43.     void append(char* array, int len);
  44.     int read(char *buffer, int len);
  45.     void close();
  46.     int get_handle(char *name);
  47.     int rename(char*oldname, char* newname);
  48.     void dump();
  49.     void dump(int handle, int len);
  50.     void dump_loc(uint32_t p, int len);
  51.     void erase_file(char* name);
  52.     void erase_all_files();
  53.     int size();
  54.    
  55. private:
  56.  
  57.     unsigned long filepointer;
  58.     uint8_t bb;
  59.     int filesize;
  60.     int read();
  61.  
  62.     int read_user_flash(unsigned long loc, uint8_t* array, unsigned long length);
  63.     unsigned long calc_file_offset(int where);
  64.     bool handle_is_ok();
  65.     void write_filename(char*name);
  66.     void write_file_size();
  67.     void read_flash(uint8_t* array, uint32_t start, uint32_t len);
  68.     void write_flash(uint8_t* array, uint32_t start, uint32_t len);
  69.     void pad(uint8_t * buf, char* str);
  70.     unsigned long calc_dir_entry();
  71.     void get_free_handle(char *name);
  72.     void erase_handle();
  73.     void erase_file_data();
  74.     void erase_file(int h);
  75.     int erase_sector(uint32_t start);
  76.     int write_to_user_flash(uint8_t *buf, uint32_t start, uint32_t len);
  77.     void pad(uint8_t* buf, char* name, int len);
  78.     int file_mode;
  79.     bool pad_flag;
  80.     char pad_char;
  81.     int len;
  82.     int wlen;
  83.     char wbuf[256];
  84.     uint8_t wndx;
  85.     void flush();
  86.     int handle;
  87.     void add_to_buf(char str);
  88.     void add_to_buf(char* str);
  89.     void add_to_buf_minus_one(char* str);
  90.     int get_dir_entry(char * entry, int * ndx);
  91.     int dir_index;
  92. };
  93.  
  94. #endif  /* SFS_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement