Simple RAM file system.

Dependents:   modem_ref_helper

Committer:
Jeej
Date:
Thu Dec 14 18:24:53 2017 +0000
Revision:
5:d2b8d685f86f
Parent:
4:58ee8b15988b
Added data pointer fetching function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jeej 0:befc17558de9 1 #include "ram_fs.h"
Jeej 0:befc17558de9 2 #include "WizziDebug.h"
Jeej 0:befc17558de9 3
Jeej 2:91473a2fac1c 4 #if 0
Jeej 2:91473a2fac1c 5 #define RAM_FS_PRINT(...) PRINT(__VA_ARGS__)
Jeej 2:91473a2fac1c 6 #else
Jeej 2:91473a2fac1c 7 #define RAM_FS_PRINT(...);
Jeej 2:91473a2fac1c 8 #endif
Jeej 2:91473a2fac1c 9
Jeej 0:befc17558de9 10 static ram_fs_file_t* g_fs = NULL;
Jeej 0:befc17558de9 11
Jeej 3:1bc9159847ab 12 static void ram_fs_update(ram_fs_file_t** file, ram_fs_file_t* file_new)
Jeej 0:befc17558de9 13 {
Jeej 0:befc17558de9 14 if (*file == NULL)
Jeej 0:befc17558de9 15 {
Jeej 3:1bc9159847ab 16 // Append new file
Jeej 0:befc17558de9 17 *file = file_new;
Jeej 0:befc17558de9 18 return;
Jeej 0:befc17558de9 19 }
Jeej 0:befc17558de9 20
Jeej 3:1bc9159847ab 21 if ((*file)->file_id == file_new->file_id)
Jeej 3:1bc9159847ab 22 {
Jeej 3:1bc9159847ab 23 // Update existing file
Jeej 3:1bc9159847ab 24 (*file)->header = file_new->header;
Jeej 3:1bc9159847ab 25 (*file)->data = file_new->data;
Jeej 3:1bc9159847ab 26 FREE(file_new);
Jeej 3:1bc9159847ab 27 return;
Jeej 3:1bc9159847ab 28 }
Jeej 3:1bc9159847ab 29
Jeej 3:1bc9159847ab 30 ram_fs_update((ram_fs_file_t**)&((*file)->next), file_new);
Jeej 0:befc17558de9 31 }
Jeej 0:befc17558de9 32
Jeej 0:befc17558de9 33 static ram_fs_file_t* ram_fs_get(ram_fs_file_t* file, uint8_t fid)
Jeej 0:befc17558de9 34 {
Jeej 0:befc17558de9 35 if (file == NULL)
Jeej 0:befc17558de9 36 {
Jeej 0:befc17558de9 37 return NULL;
Jeej 0:befc17558de9 38 }
Jeej 0:befc17558de9 39
Jeej 0:befc17558de9 40 if (file->file_id == fid)
Jeej 0:befc17558de9 41 {
Jeej 0:befc17558de9 42 return file;
Jeej 0:befc17558de9 43 }
Jeej 0:befc17558de9 44
Jeej 0:befc17558de9 45 return ram_fs_get((ram_fs_file_t*)file->next, fid);
Jeej 0:befc17558de9 46 }
Jeej 0:befc17558de9 47
Jeej 0:befc17558de9 48 static bool ram_fs_remove(ram_fs_file_t** file, uint8_t fid)
Jeej 0:befc17558de9 49 {
Jeej 0:befc17558de9 50 if (*file == NULL)
Jeej 0:befc17558de9 51 {
Jeej 0:befc17558de9 52 // File does nor exist
Jeej 0:befc17558de9 53 return true;
Jeej 0:befc17558de9 54 }
Jeej 0:befc17558de9 55
Jeej 0:befc17558de9 56 if ((*file)->file_id == fid)
Jeej 0:befc17558de9 57 {
Jeej 0:befc17558de9 58 ram_fs_file_t* file_old = *file;
Jeej 0:befc17558de9 59 *file = (ram_fs_file_t*)file_old->next;
Jeej 0:befc17558de9 60 FREE(file_old);
Jeej 0:befc17558de9 61 return false;
Jeej 0:befc17558de9 62 }
Jeej 0:befc17558de9 63
Jeej 0:befc17558de9 64 return ram_fs_remove((ram_fs_file_t**)&((*file)->next), fid);
Jeej 0:befc17558de9 65 }
Jeej 0:befc17558de9 66
Jeej 0:befc17558de9 67 void ram_fs_new(uint8_t file_id, uint8_t* header, uint8_t* data)
Jeej 0:befc17558de9 68 {
Jeej 2:91473a2fac1c 69 RAM_FS_PRINT("NF(%d)\r\n", file_id);
Jeej 1:73a2c1814a34 70
Jeej 0:befc17558de9 71 ram_fs_file_t* file_new = (ram_fs_file_t*)MALLOC(sizeof(ram_fs_file_t));
Jeej 0:befc17558de9 72
Jeej 0:befc17558de9 73 file_new->file_id = file_id;
Jeej 0:befc17558de9 74 file_new->header = header;
Jeej 0:befc17558de9 75 file_new->data = data;
Jeej 0:befc17558de9 76 file_new->next = NULL;
Jeej 0:befc17558de9 77
Jeej 3:1bc9159847ab 78 ram_fs_update(&g_fs, file_new);
Jeej 0:befc17558de9 79 }
Jeej 0:befc17558de9 80
Jeej 0:befc17558de9 81 bool ram_fs_delete(uint8_t file_id)
Jeej 0:befc17558de9 82 {
Jeej 2:91473a2fac1c 83 RAM_FS_PRINT("DF(%d)\r\n", file_id);
Jeej 0:befc17558de9 84 return ram_fs_remove(&g_fs, file_id);
Jeej 0:befc17558de9 85 }
Jeej 0:befc17558de9 86
Jeej 0:befc17558de9 87 bool ram_fs_write(uint8_t file_id, uint16_t offset, uint16_t size, uint8_t* content)
Jeej 0:befc17558de9 88 {
Jeej 0:befc17558de9 89 ram_fs_file_t* file;
Jeej 0:befc17558de9 90
Jeej 2:91473a2fac1c 91 RAM_FS_PRINT("WF(%d)\r\n", file_id);
Jeej 0:befc17558de9 92
Jeej 0:befc17558de9 93 // Retrieve pointer to file
Jeej 0:befc17558de9 94 file = ram_fs_get(g_fs, file_id);
Jeej 0:befc17558de9 95
Jeej 0:befc17558de9 96 if (file == NULL)
Jeej 0:befc17558de9 97 {
Jeej 0:befc17558de9 98 return true;
Jeej 0:befc17558de9 99 }
Jeej 0:befc17558de9 100
Jeej 0:befc17558de9 101 // Write the new data
Jeej 0:befc17558de9 102 memcpy((void*)(file->data+offset), (void*)content, size);
Jeej 0:befc17558de9 103
Jeej 0:befc17558de9 104 return false;
Jeej 0:befc17558de9 105 }
Jeej 0:befc17558de9 106
Jeej 0:befc17558de9 107 bool ram_fs_read(uint8_t file_id, uint16_t offset, uint16_t size, uint8_t* buf)
Jeej 0:befc17558de9 108 {
Jeej 0:befc17558de9 109 ram_fs_file_t* file;
Jeej 0:befc17558de9 110
Jeej 2:91473a2fac1c 111 RAM_FS_PRINT("RF(%d)\r\n", file_id);
Jeej 0:befc17558de9 112
Jeej 0:befc17558de9 113 // Retrieve pointer to file
Jeej 0:befc17558de9 114 file = ram_fs_get(g_fs, file_id);
Jeej 0:befc17558de9 115
Jeej 0:befc17558de9 116 if (file == NULL)
Jeej 0:befc17558de9 117 {
Jeej 0:befc17558de9 118 return true;
Jeej 0:befc17558de9 119 }
Jeej 0:befc17558de9 120
Jeej 0:befc17558de9 121 // Read data
Jeej 0:befc17558de9 122 memcpy((void*)buf, (void*)(file->data+offset), size);
Jeej 0:befc17558de9 123
Jeej 0:befc17558de9 124 return false;
Jeej 0:befc17558de9 125 }
Jeej 0:befc17558de9 126
Jeej 0:befc17558de9 127 void* ram_fs_get_header(uint8_t file_id)
Jeej 0:befc17558de9 128 {
Jeej 0:befc17558de9 129 ram_fs_file_t* file;
Jeej 0:befc17558de9 130
Jeej 2:91473a2fac1c 131 RAM_FS_PRINT("RH(%d)\r\n", file_id);
Jeej 0:befc17558de9 132
Jeej 0:befc17558de9 133 // Retrieve pointer to file
Jeej 0:befc17558de9 134 file = ram_fs_get(g_fs, file_id);
Jeej 0:befc17558de9 135
Jeej 1:73a2c1814a34 136 //ASSERT(file != NULL, "File %d does not exist!\r\n", file_id);
Jeej 0:befc17558de9 137
Jeej 1:73a2c1814a34 138 if (file == NULL)
Jeej 1:73a2c1814a34 139 {
Jeej 1:73a2c1814a34 140 return NULL;
Jeej 1:73a2c1814a34 141 }
Jeej 1:73a2c1814a34 142 else
Jeej 1:73a2c1814a34 143 {
Jeej 1:73a2c1814a34 144 // Return header pointer
Jeej 1:73a2c1814a34 145 return file->header;
Jeej 1:73a2c1814a34 146 }
Jeej 0:befc17558de9 147 }
Jeej 5:d2b8d685f86f 148
Jeej 5:d2b8d685f86f 149 void* ram_fs_get_data(uint8_t file_id)
Jeej 5:d2b8d685f86f 150 {
Jeej 5:d2b8d685f86f 151 ram_fs_file_t* file;
Jeej 5:d2b8d685f86f 152
Jeej 5:d2b8d685f86f 153 RAM_FS_PRINT("RH(%d)\r\n", file_id);
Jeej 5:d2b8d685f86f 154
Jeej 5:d2b8d685f86f 155 // Retrieve pointer to file
Jeej 5:d2b8d685f86f 156 file = ram_fs_get(g_fs, file_id);
Jeej 5:d2b8d685f86f 157
Jeej 5:d2b8d685f86f 158 //ASSERT(file != NULL, "File %d does not exist!\r\n", file_id);
Jeej 5:d2b8d685f86f 159
Jeej 5:d2b8d685f86f 160 if (file == NULL)
Jeej 5:d2b8d685f86f 161 {
Jeej 5:d2b8d685f86f 162 return NULL;
Jeej 5:d2b8d685f86f 163 }
Jeej 5:d2b8d685f86f 164 else
Jeej 5:d2b8d685f86f 165 {
Jeej 5:d2b8d685f86f 166 // Return data pointer
Jeej 5:d2b8d685f86f 167 return file->data;
Jeej 5:d2b8d685f86f 168 }
Jeej 5:d2b8d685f86f 169 }