Operating System
fsapi.h
Go to the documentation of this file.
1 
6 #ifndef __FILE_SYSTEM_API_H_
7 #define __FILE_SYSTEM_API_H_
8 
9 #include <stdint.h>
10 
11 #include "../FATMacro.h"
12 #include "../filesystem.h"
13 #include "../fsutilities.h"
14 #include "../filesystem.h"
15 #include "../../include/mystring.h"
16 #include "fsErrorCode.h"
17 #include "../../include/utilities.h"
18 #include "../../include/stdio.h"
24 #define TYPE_FLDR 0
26 #define TYPE_FILE 1
27 #define TYPE_DOC 2
28 
30 
35 static inline FAT_ITEM* __get_root_dir() {
36  return (FAT_ITEM*)(ROOT_AREA_ADDRESS);
37 }
42 static inline int16_t __has_next_item(const FAT_ITEM* p) {
43  while ((p+1)->filename[0] == S_DEL) {
44  p++;
45  }
46  return (p+1)->filename[0] != S_UNUSED;
47 }
51 static inline int16_t __has_prev_item(const FAT_ITEM* p) {
52  while ((p-1)->filename[0] == S_DEL) {
53  p--;
54  }
55  return (p-1)->filename[0] != S_UNUSED;
56  //TODO: how to deal with it? return false forever
57 }
62 static inline FAT_ITEM* __next_item(FAT_ITEM* p) {
63  while ((p+1)->filename[0] == S_DEL) {
64  p++;
65  }
66  return p+1;
67 }
71 static inline FAT_ITEM* __prev_item(FAT_ITEM* p) {
72  while ((p-1)->filename[0] == S_DEL) {
73  p--;
74  }
75  return p-1;
76 }
77 
83 static inline int16_t __FAT_item_type(const FAT_ITEM* p) {
84  if (p->mod & FAT_fldr) {
85  return TYPE_FLDR;
86  } else if (p->mod & FAT_doc) {
87  return TYPE_DOC;
88  }
89  return TYPE_FILE;
90 }
95 static inline int16_t __FAT_showable_item(const FAT_ITEM* p) {
96  return __FAT_item_type(p) != TYPE_DOC;
97 }
102 static inline FAT_ITEM* __jmp_into_dir(const FAT_ITEM* p) {
103  uint16_t cluster = p->blow_cluster;
104  uint16_t numOfSector = filesize2sectors(p->filesize);
105  uint16_t sectorNth = HIDDEN_SECTOR + RESERVED_SECTOR +
107  cluster * SECTOR_PER_CLUSTER + 1;
108  loadLogicSector(sectorNth, DATA_BLOCK_ADDRESS, numOfSector);
109  return (FAT_ITEM*)(DATA_BLOCK_ADDRESS);
110 }
115 static inline int16_t __rm_this_file(FAT_ITEM* p) {
116  uint16_t mod = p->mod;
117  if (mod & FAT_fldr) {
118  return ERR_TYPE_FLDR;
119  }
120  if (mod & FAT_sys) {
121  return ERR_SYS_PROTC;
122  }
123  if (mod & FAT_doc) {
124  return ERR_TYPE_DOC;
125  }
126  const uint8_t* const fn = p->filename;
127  if (fn[0] == S_UNUSED) {
128  return ERR_NOT_FOUND;
129  }
130  if (fn[0] == S_DEL) {
131  return ERR_NOT_FOUND;
132  }
133  (p->filename[0]) = S_DEL;
134  return NO_ERR;
135 }
142 static inline int16_t __run_this_file(FAT_ITEM* p) {
143  uint16_t mod = p->mod;
144  if (mod & FAT_sys) {
145  return ERR_SYS_PROTC;
146  }
147  if (mod & FAT_fldr) {
148  return ERR_TYPE_FLDR;
149  }
150  if (mod & FAT_doc) {
151  return ERR_TYPE_DOC;
152  }
153  uint16_t cluster = p->blow_cluster;
154  uint16_t sectorNth = HIDDEN_SECTOR + RESERVED_SECTOR +
156  cluster * SECTOR_PER_CLUSTER + 1;
157  uint16_t numOfSector = filesize2sectors(p->filesize);
158  loadLogicSector(sectorNth, USER_PROGRAM_ADDRESS, numOfSector);
159  return 0;
160 
161 }
171 static inline int16_t __load_program(const char* targetFilename) {
172  FAT_ITEM* pfat = __get_root_dir();
173  if (strcmp(pfat->filename, targetFilename) == 0) {
174  return __run_this_file(pfat);
175  }
176  while (__has_next_item(pfat)) {
177  pfat = __next_item(pfat);
178  if (strcmp(pfat->filename, targetFilename) == 0) {
179  return __run_this_file(pfat);
180  }
181  }
182  return ERR_NOT_FOUND;
183 
184 }
187 static inline void __print_file_cluster_list(const FAT_ITEM* pfat) {
188  FAT_ITEM_T* FAT_table = (FAT_ITEM_T*)(FAT_TABLE_ADDRESS);
189  int16_t cluster = pfat->blow_cluster;
190  FAT_ITEM_T* pFAT_ITEM = FAT_table + cluster;
191  puti(cluster);
192  puts(" -> ");
193  while (*pFAT_ITEM != 0xFFFF) {
194  puti(*pFAT_ITEM);
195  puts(" -> ");
196  pFAT_ITEM = FAT_table + *pFAT_ITEM;
197  }
198  puts("NULL");
199 
200 }
201 #endif
#define TYPE_FLDR
type file directory
Definition: fsapi.h:25
#define ERR_TYPE_DOC
target type is partition doc, unable
Definition: fsErrorCode.h:11
#define FAT_sys
system file
Definition: FATMacro.h:48
static FAT_ITEM * __prev_item(FAT_ITEM *p)
Definition: fsapi.h:71
#define SECTOR_PER_FAT
Definition: filesystem.h:10
static FAT_ITEM * __next_item(FAT_ITEM *p)
Definition: fsapi.h:62
#define NUMBER_OF_FAT
Definition: filesystem.h:8
uint8_t mod
Definition: FATMacro.h:12
#define SECTOR_PER_CLUSTER
Definition: filesystem.h:6
uint8_t mod
Definition: FATMacro.h:24
#define USER_PROGRAM_ADDRESS
Definition: filesystem.h:15
#define DATA_BLOCK_ADDRESS
Definition: filesystem.h:19
#define ERR_NOT_FOUND
file not found
Definition: fsErrorCode.h:13
#define ERR_TYPE_FLDR
target type is folder, unable to execute the operation
Definition: fsErrorCode.h:9
static int16_t __rm_this_file(FAT_ITEM *p)
Definition: fsapi.h:115
uint8_t filename[8]
Definition: FATMacro.h:10
static void loadLogicSector(uint16_t lgsector, uint16_t addr, uint16_t num)
Definition: fsutilities.h:8
#define TYPE_FILE
type file
Definition: fsapi.h:26
static void __print_file_cluster_list(const FAT_ITEM *pfat)
Definition: fsapi.h:187
#define RESERVED_SECTOR
Definition: filesystem.h:7
uint16_t FAT_ITEM_T
Definition: FATMacro.h:9
#define HIDDEN_SECTOR
Definition: filesystem.h:13
#define S_DEL
has been deleted, can be reused
Definition: FATMacro.h:19
static int16_t __load_program(const char *targetFilename)
Definition: fsapi.h:171
#define NO_ERR
Definition: fsErrorCode.h:5
uint32_t filesize
in bytes
Definition: FATMacro.h:36
#define ROOT_AREA_ADDRESS
Definition: filesystem.h:18
static int16_t puti(int32_t num)
Definition: stdio.h:159
static int16_t __FAT_item_type(const FAT_ITEM *p)
Definition: fsapi.h:83
#define FAT_fldr
folder
Definition: FATMacro.h:49
int strcmp(const char *lhs, const char *rhs)
Definition: mystring.c:2
#define TYPE_DOC
type (reserved) document for file system
Definition: fsapi.h:27
static int16_t __run_this_file(FAT_ITEM *p)
Definition: fsapi.h:142
uint8_t filename[8]
Definition: FATMacro.h:22
static FAT_ITEM * __jmp_into_dir(const FAT_ITEM *p)
Definition: fsapi.h:102
#define ERR_SYS_PROTC
system protected file, not allow executing the operation
Definition: fsErrorCode.h:7
#define S_UNUSED
item unused
Definition: FATMacro.h:17
#define FAT_TABLE_ADDRESS
Definition: filesystem.h:17
static int16_t __has_prev_item(const FAT_ITEM *p)
Definition: fsapi.h:51
static FAT_ITEM * __get_root_dir()
Definition: fsapi.h:35
uint16_t blow_cluster
Definition: FATMacro.h:35
#define FAT_doc
partition reserved doc
Definition: FATMacro.h:50
define error code that is used in fsapi.h
#define filesize2sectors(X)
Definition: fsutilities.h:15
static int16_t __has_next_item(const FAT_ITEM *p)
Definition: fsapi.h:42
static int16_t puts(char const *str)
Definition: stdio.h:146
static int16_t __FAT_showable_item(const FAT_ITEM *p)
Definition: fsapi.h:95