Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "sfs.h"
- //VERBOSE defined gives a lot of information on serial port 1
- //#define VERBOSE
- sfs::sfs() {
- file_mode = FILE_NONE;
- wndx = 0;
- }
- bool sfs::open(char* name) {
- filepointer = 0;
- int rc;
- if ((rc = get_handle(name) < 0)) {
- #ifdef VERBOSE
- Serial1.print("get handle error: ");
- Serial1.println(rc, DEC);
- #endif
- };
- #ifdef VERBOSE
- Serial1.print("open: ");
- Serial1.print(name);
- Serial1.print("\t");
- Serial1.print(handle, DEC);
- Serial1.print("\t");
- Serial1.print(filesize, DEC);
- Serial1.println();
- #endif
- if (handle_is_ok() == false) return false;
- file_mode = FILE_READ;
- return true;
- }
- int sfs::size(){
- return filesize;
- }
- void sfs::close() {
- #ifdef VERBOSE
- Serial1.println("close");
- #endif
- if (handle_is_ok() == false) return;
- flush();
- if (file_mode == FILE_WRITE) {
- write_file_size();
- }
- file_mode = FILE_NONE;
- handle = -1;
- }
- void sfs::flush() {
- char buf[3];
- #ifdef VERBOSE
- Serial1.println("flush");
- #endif
- if (pad_flag == true) {
- buf[0] = pad_char;
- buf[1] = 0;
- buf[2] = 0;
- pad_flag = false;
- unsigned long start = calc_file_offset(2);
- write_flash((uint8_t*) buf, start, 2);
- filepointer++;
- #ifdef VERBOSE
- Serial1.print("flushing: ");
- Serial1.println(buf);
- #endif
- }
- }
- void sfs::erase_file(char* name){
- handle=get_handle(name);
- erase_file(handle);
- }
- int sfs::get_handle(char *name) {
- uint8_t buf[65];
- filesize = 0;
- for (handle = 0; handle < MAX_FILES; handle++) {
- read_flash((uint8_t*) buf, ((unsigned long) handle * DIR_ENTRY), (unsigned long) 64);
- #ifdef VERBOSE
- Serial1.print("get handle ");
- Serial1.print(" ");
- Serial1.println(name);
- #endif
- if (strcmp((char*) buf, name) == 0) { //found!
- filesize = atoi((char*) buf + FILE_LEN_OFFSET);
- #ifdef VERBOSE
- Serial1.print("get handle:");
- Serial1.print(name);
- Serial1.print("\tsize:");
- Serial1.print(filesize, DEC);
- Serial1.print("\thandle:");
- Serial1.println(handle, DEC);
- #endif
- return handle;
- } else
- filesize = 0;
- }
- return -1;
- }
- int sfs::rename(char*oldname, char* newname){
- Serial1.print("rename");
- if (get_handle(oldname)==-1)
- return FILE_NOT_FOUND;
- erase_handle();
- write_filename(newname);
- write_file_size();
- return 0;
- }
- int sfs::dir(char * entry){
- if (entry==NULL){
- dir_index=0;
- return 0;
- }else
- return dir(entry,&dir_index);
- }
- int sfs::dir(char * entry, int * ndx) {
- #ifdef VERBOSE
- Serial1.print(" dir: ");
- #endif
- if (*ndx < 0 || *ndx > MAX_FILES)
- return -1;
- for (; *ndx < MAX_FILES; (*ndx)++) {
- int rc = get_dir_entry(entry, ndx);
- if (rc > 0) {
- (*ndx)++;
- return rc;
- }
- }
- return -1;
- }
- int sfs::get_dir_entry(char * entry, int * ndx) {
- uint8_t buf[68];
- memset(buf, 0, sizeof (buf));
- read_flash(buf, (unsigned long) (*ndx * 0x1000), 64);
- buf[16] = 0;
- if (buf[0] != 0xFF) {
- len = strlen((char*) buf);
- memcpy(entry, (char*) buf, len);
- // len++;
- //entry[len] = ',';
- //len++;
- //memcpy(entry + len, buf + FILE_LEN_OFFSET, 8);
- int flen = atoi((char*)(buf + (FILE_LEN_OFFSET)));
- #ifdef VERBOSE
- Serial1.println((char*) (entry));
- #endif
- return flen;
- } else {
- return -1;
- }
- }
- boolean sfs::create(char* name) {
- #ifdef VERBOSE
- Serial1.println("create: ");
- #endif
- get_free_handle(name);
- if (handle_is_ok() == false) return false;
- file_mode = FILE_WRITE;
- #ifdef VERBOSE
- Serial1.print("file create: ");
- Serial1.print(name);
- Serial1.print(" handle: ");
- Serial1.println(handle, DEC);
- #endif
- erase_handle();
- write_filename(name);
- filepointer = 0;
- #ifdef VERBOSE
- Serial1.print("0handle: ");
- Serial1.println(handle, DEC);
- erase_file_data();
- Serial1.print("1handle: ");
- Serial1.println(handle, DEC);
- #endif
- return true;
- }
- void sfs::append(char * str) {
- append(str, strlen(str));
- }
- void sfs::add_to_buf(char* str) {
- int len = strlen(str);
- strncpy((wbuf + wndx), str, len);
- wndx = 0;
- }
- void sfs::add_to_buf(char chr) {
- wbuf[0] = chr;
- wndx = 1;
- }
- void sfs::add_to_buf_minus_one(char* str) {
- int len = strlen(str) - 1;
- strncpy((wbuf + wndx), str, len);
- wndx = 0;
- }
- void sfs::append(char* array, int len) {
- #ifdef VERBOSE
- Serial1.print("append: ");
- Serial1.println(array);
- int stat = 0;
- #endif
- memset(wbuf, 0, sizeof (wbuf));
- if ((len & 1)) {
- if (pad_flag == true) {
- add_to_buf(pad_char);
- add_to_buf(array);
- pad_flag = false;
- #ifdef VERBOSE
- stat = 1;
- #endif
- } else {
- pad_char = array[len - 1];
- pad_flag = true;
- if (len == 1)
- return;
- add_to_buf_minus_one(array);
- #ifdef VERBOSE
- stat = 2;
- #endif
- }
- } else {
- if (pad_flag == true) {
- add_to_buf(pad_char);
- add_to_buf_minus_one(array);
- pad_char = array[len - 1];
- pad_flag = true;
- #ifdef VERBOSE
- stat = 3;
- #endif
- } else {
- add_to_buf(array);
- pad_flag = false;
- #ifdef VERBOSE
- stat = 4;
- #endif
- }
- }
- unsigned long offset = calc_file_offset(3);
- wlen = strlen(wbuf);
- write_flash((uint8_t*) wbuf, offset, wlen);
- #ifdef VERBOSE
- Serial1.print("stat: ");
- #endif
- filepointer += (unsigned long) wlen;
- }
- void sfs::get_free_handle(char *name) {
- uint8_t buf[64];
- memset(buf, '@', 64);
- for (handle = 0; handle < MAX_FILES; handle++) {
- unsigned long loc = ((unsigned long) handle * DIR_ENTRY);
- read_flash(buf, loc, (unsigned long) 16);
- if (strcmp((char*) buf, name) == 0 || buf[0] == 0xFF) { //empty location
- #ifdef VERBOSE
- Serial1.print("free handle: ");
- Serial1.println(handle, DEC);
- #endif
- return;
- }
- }
- #ifdef VERBOSE
- Serial1.println("no handle available!");
- #endif
- handle = -1;
- }
- void sfs::erase_handle() {
- #ifdef VERBOSE
- Serial1.print("erase handle: ");
- Serial1.println(handle, DEC);
- #endif
- if (handle_is_ok() == false) return;
- unsigned long start = FLASH_USER_SPACE + ((unsigned long) handle * 0x1000);
- erase_sector(start);
- }
- void sfs::erase_all_files() {
- for (handle = 0; handle < MAX_FILES; handle++) {
- erase_handle();
- erase_file_data();
- }
- handle = 0;
- }
- void sfs::erase_file(int handle) {
- this->handle = handle;
- erase_handle();
- erase_file_data();
- }
- void sfs::erase_file_data() {
- #ifdef VERBOSE
- Serial1.print("erase file data: ");
- #endif
- if (handle_is_ok() == false) return;
- unsigned long start = FLASH_USER_SPACE + 0x10000 + ((unsigned long) handle * 0x10000);
- for (int ii = 0; ii < MAX_FILE_BLOCKS; ii++) {
- unsigned long offset = start + (ii * 0x1000);
- erase_sector(offset);
- }
- }
- int sfs::erase_sector(uint32_t start) {
- #ifdef VERBOSE
- Serial1.print("erase sector: ");
- #endif
- if (start < FLASH_USER_SPACE) {
- #ifdef VERBOSE
- Serial1.println("Attempt to erase sector outside user space!");
- #endif
- return -1;
- }
- #ifdef VERBOSE
- Serial1.println(start, HEX);
- #endif
- sFLASH_EraseSector(start);
- return 0;
- }
- int sfs::read(char *buffer, int len) {
- unsigned long offset = calc_file_offset(1);
- unsigned long br = ((unsigned long) filesize - filepointer);
- int L = 0;
- if (br < (unsigned int) len)
- L = br;
- else
- L = len;
- read_flash((uint8_t*) buffer, offset, L);
- buffer[L] = 0;
- #ifdef VERBOSE
- Serial1.print("Offset: ");
- Serial1.print(offset, HEX);
- Serial1.print(" L: ");
- Serial1.print(L, DEC);
- Serial1.print(" ");
- #endif
- filepointer += (unsigned long) L;
- return L;
- }
- unsigned long sfs::calc_file_offset(int where) {
- #ifdef VERBOSE
- Serial1.print("calc file offset: ");
- Serial1.println(where, DEC);
- Serial1.println((unsigned long) filepointer, HEX);
- #endif
- unsigned long offset;
- offset = filepointer + 0x10000 + (0x10000 * (unsigned long) handle);
- #ifdef VERBOSE
- Serial1.print(where, DEC); //who's calling?
- Serial1.print(" handle:");
- Serial1.println(handle, DEC);
- Serial1.print(" offset:");
- Serial1.println((unsigned long) offset, HEX);
- Serial1.print("filepointer:");
- Serial1.print((unsigned long) filepointer, HEX);
- Serial1.print(" ");
- #endif
- return (unsigned long) offset;
- }
- bool sfs::handle_is_ok() {
- if (handle < 0 || handle >= MAX_FILES) {
- #ifdef VERBOSE
- Serial1.print("Bad handle: ");
- Serial1.println(handle, DEC);
- #endif
- return false;
- } else
- return true;
- }
- void sfs::write_filename(char* name) {
- #ifdef VERBOSE
- Serial1.print("write filename ");
- #endif
- if (handle_is_ok() == false) return;
- unsigned long start = calc_dir_entry();
- #ifdef VERBOSE
- Serial1.print("start: ");
- Serial1.print(start, HEX);
- Serial1.print(" handle:");
- Serial1.print(handle, DEC);
- Serial1.print(" ");
- Serial1.println(name);
- #endif
- uint8_t buf[MAX_NAME_LEN + 1];
- pad(buf, name, MAX_NAME_LEN);
- write_flash(buf, start, MAX_NAME_LEN);
- }
- void sfs::pad(uint8_t * dest, char* str, int ln) {
- memset((char*) dest, 0, ln);
- strncpy((char*) dest, str, ln);
- }
- void sfs::write_file_size() {
- uint8_t buf[MAX_FILESIZE_LEN + 1];
- unsigned char buf2[MAX_FILESIZE_LEN + 1];
- if (handle_is_ok() == false) return;
- unsigned long start = calc_dir_entry();
- String file_size = String(filepointer);
- file_size.getBytes(buf2, MAX_FILESIZE_LEN, 0);
- pad(buf, (char*) buf2, MAX_FILESIZE_LEN);
- write_flash(buf, start + FILE_LEN_OFFSET, MAX_FILESIZE_LEN);
- #ifdef VERBOSE
- Serial1.print("write size: ");
- Serial1.println(file_size);
- #endif
- }
- int sfs::write_to_user_flash(uint8_t *buf, uint32_t start, uint32_t len) {
- #ifdef VERBOSE
- Serial1.print("write to user flash: ");
- #endif
- if (start < FLASH_USER_SPACE) {
- #ifdef VERBOSE
- Serial1.println("Error: attempt to write outside user space!");
- #endif
- return -1;
- }
- #ifdef VERBOSE
- Serial1.print(start, HEX);
- Serial1.print(" to ");
- Serial1.print(start + len - 1, HEX);
- Serial1.print(" len:");
- Serial1.print(len, DEC);
- Serial1.print("\tdata: ");
- Serial1.println((char*) buf);
- #endif
- write_flash(buf, FLASH_USER_SPACE + start, len);
- return 0;
- }
- void pad(uint8_t* buf, char* name, int len) {
- #ifdef VERBOSE
- Serial1.print("pad: ");
- #endif
- uint8_t nlen = strlen(name);
- if (nlen >= len) {
- nlen = len;
- }
- memset((char*) buf, 0, len + 1);
- memcpy((char*) buf, name, nlen);
- }
- unsigned long sfs::calc_dir_entry() {
- #ifdef VERBOSE
- Serial1.println("calc dir entry: ");
- #endif
- if (handle_is_ok() == false)
- return -1;
- uint32_t loc = ((unsigned long) handle * 0x1000);
- return loc;
- }
- void sfs::dump() {
- uint8_t array[256];
- uint32_t loc = 0x10000;
- int length = 45;
- read_flash(array, loc, length);
- Serial1.println((char*) array);
- }
- void sfs::read_flash(uint8_t* array, uint32_t start, uint32_t len) {
- sFLASH_ReadBuffer(array, FLASH_USER_SPACE + start, len);
- }
- void sfs::write_flash(uint8_t* array, uint32_t start, uint32_t len) {
- start += FLASH_USER_SPACE;
- sFLASH_WriteBuffer(array, start, len);
- #ifdef VERBOSE
- Serial1.print("writing to:");
- Serial1.print(start, HEX);
- Serial1.print(" len:");
- Serial1.print(len, DEC);
- Serial1.print(" data:");
- Serial1.println((char*) array);
- #endif
- }
- void sfs::dump_loc(uint32_t p, int len) {
- Serial1.println("Dump(loc):");
- if (len < 64)
- len = 64;
- uint8_t array[len + 1];
- // uint32_t loc;
- for (uint32_t kk = 0; kk < (uint32_t) len; kk += 64) {
- p += kk;
- read_flash(array, p, 64);
- Serial1.print(p, HEX);
- Serial1.print("\t");
- for (int jj = 0; jj < 64; jj++) {
- uint8_t c = array[jj];
- switch (c) {
- case 0: c = '.';
- break;
- case 10: c = '~';
- break;
- case 0xFF: c = '`';
- break;
- default:
- if (c < 32 || c > 127)
- c = '.';
- }
- Serial1.print((char) c);
- }
- Serial1.println();
- }
- }
- void sfs::dump(int h, int len) {
- handle = h;
- Serial1.println("Dump(handle):");
- if (len < 64)
- len = 64;
- uint8_t array[len + 1];
- uint32_t loc;
- for (filepointer = 0; filepointer < (unsigned long) len; filepointer += (unsigned long) 64) {
- loc = calc_file_offset(4);
- read_flash(array, loc, 64);
- Serial1.print(FLASH_USER_SPACE + loc, HEX);
- Serial1.print("\t");
- for (int jj = 0; jj < 64; jj++) {
- uint8_t c = array[jj];
- switch (c) {
- case 0: c = '.';
- break;
- case 10: c = '~';
- break;
- case 0xFF: c = '`';
- break;
- default:
- if (c < 32 || c > 127)
- c = '.';
- }
- Serial1.print((char) c);
- }
- Serial1.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement