Choco OS  V.0.16.9.0
Join to the chocolate world
oc_flashfs.c
Go to the documentation of this file.
1 
27 #include <oc_flashfs.h>
28 #include <oc_object.h>
29 #include <oc_array.h>
30 #include <oc_string.h>
31 #include <oc_fs.h>
32 #include <oc_bits.h>
33 
39 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
40 
41 
42 
43 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
44 
50 #define _________________________________________TYPES______________________________________________________________________________________
51 
52 
53 struct File_t
54 {
55  oC_ObjectControl_t ObjectControl;
56  uint32_t Offset;
57  const oC_FlashFs_FileDefinition_t * FileDefinition;
58 };
59 
60 struct Dir_t
61 {
62  oC_ObjectControl_t ObjectControl;
63  uint32_t FileIndex;
64 };
65 
66 
67 #undef _________________________________________TYPES______________________________________________________________________________________
68 
74 #define _________________________________________VARIABLES__________________________________________________________________________________
75 
76 //==========================================================================================================================================
77 //==========================================================================================================================================
78 const oC_FileSystem_Registration_t FlashFs = {
79  .Name = "FlashFs" ,
80  .init = oC_FlashFs_init ,
81  .deinit = oC_FlashFs_deinit ,
82  .fopen = oC_FlashFs_fopen ,
83  .fclose = oC_FlashFs_fclose ,
84  .fread = oC_FlashFs_fread ,
85  .fwrite = oC_FlashFs_fwrite ,
86  .lseek = oC_FlashFs_lseek ,
87  .ioctl = oC_FlashFs_ioctl ,
88  .sync = oC_FlashFs_sync ,
89  .Getc = oC_FlashFs_getc ,
90  .Putc = oC_FlashFs_putc ,
91  .tell = oC_FlashFs_tell ,
92  .eof = oC_FlashFs_eof ,
93  .size = oC_FlashFs_size ,
94 
95  .opendir = oC_FlashFs_opendir ,
96  .closedir = oC_FlashFs_closedir ,
97  .readdir = oC_FlashFs_readdir ,
98 
99  .stat = oC_FlashFs_stat ,
100  .unlink = oC_FlashFs_unlink ,
101  .rename = oC_FlashFs_rename ,
102  .chmod = oC_FlashFs_chmod ,
103  .utime = oC_FlashFs_utime ,
104  .mkdir = oC_FlashFs_mkdir ,
105  .DirExists= oC_FlashFs_DirExists,
106 };
107 
108 
109 #undef _________________________________________VARIABLES__________________________________________________________________________________
110 
111 
117 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
118 
119 static bool IsFileCorrect ( oC_File_t File );
120 static bool IsDirCorrect ( oC_Dir_t Dir );
121 static const oC_FlashFs_FileDefinition_t * GetFileDefinition ( const char * Path );
122 
123 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
124 
125 
131 #define _________________________________________INTERFACE_FUNCTIONS________________________________________________________________________
132 
133 
134 //==========================================================================================================================================
135 //==========================================================================================================================================
136 oC_ErrorCode_t oC_FlashFs_init( oC_FlashFs_Context_t * outContext )
137 {
138  return oC_ErrorCode_None;
139 }
140 
141 //==========================================================================================================================================
142 //==========================================================================================================================================
143 oC_ErrorCode_t oC_FlashFs_deinit( oC_FlashFs_Context_t Context )
144 {
145  return oC_ErrorCode_None;
146 }
147 
148 //==========================================================================================================================================
149 //==========================================================================================================================================
150 oC_ErrorCode_t oC_FlashFs_fopen( oC_FlashFs_Context_t Context , oC_File_t * outFile , const char * Path , oC_FileSystem_ModeFlags_t Mode , oC_FileSystem_FileAttributes_t Attributes )
151 {
152  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
153 
154  if(
155  ErrorCondition( isram(outFile) , oC_ErrorCode_OutputAddressNotInRAM )
156  && ErrorCondition( isaddresscorrect(Path) , oC_ErrorCode_WrongAddress )
157  && ErrorCondition( Path[0] == '/' , oC_ErrorCode_PathNotCorrect )
158  && ErrorCondition( oC_Bits_AreBitsClearU32(Mode , oC_FileSystem_ModeFlags__Create) , oC_ErrorCode_FileIsReadOnly )
159  )
160  {
161  const oC_FlashFs_FileDefinition_t * fileDefinition = GetFileDefinition(Path);
162 
163  if(ErrorCondition(isrom(fileDefinition) , oC_ErrorCode_PathNotCorrect))
164  {
165  oC_File_t file = smartalloc(sizeof(struct File_t),AllocationFlags_CanWait1Second);
166 
167  if(file)
168  {
169  file->ObjectControl = oC_CountObjectControl(file,oC_ObjectId_FlashFsFile);
170  file->Offset = Mode & oC_FileSystem_ModeFlags_SeekToTheEnd ? file->FileDefinition->Size : 0;
171  file->FileDefinition = fileDefinition;
172  *outFile = file;
173  errorCode = oC_ErrorCode_None;
174  }
175  else
176  {
177  errorCode = oC_ErrorCode_AllocationError;
178  }
179  }
180  }
181 
182  return errorCode;
183 }
184 
185 //==========================================================================================================================================
186 //==========================================================================================================================================
187 oC_ErrorCode_t oC_FlashFs_fclose( oC_FlashFs_Context_t Context , oC_File_t File )
188 {
189  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
190 
191  if(
192  ErrorCondition(IsFileCorrect(File),oC_ErrorCode_ObjectNotCorrect)
193  )
194  {
195  File->ObjectControl = 0;
196 
197  if(smartfree(File,sizeof(struct File_t),AllocationFlags_CanWaitForever))
198  {
199  errorCode = oC_ErrorCode_None;
200  }
201  else
202  {
203  errorCode = oC_ErrorCode_ReleaseError;
204  }
205  }
206 
207  return errorCode;
208 }
209 //==========================================================================================================================================
210 //==========================================================================================================================================
211 oC_ErrorCode_t oC_FlashFs_fread( oC_FlashFs_Context_t Context , oC_File_t File , void * outBuffer , uint32_t * Size )
212 {
213  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
214 
215  if(
216  ErrorCondition( IsFileCorrect(File) , oC_ErrorCode_FileNotCorrect )
217  && ErrorCondition( isram(outBuffer) , oC_ErrorCode_OutputAddressNotInRAM )
218  && ErrorCondition( isram(Size) , oC_ErrorCode_OutputAddressNotInRAM )
219  )
220  {
221  uint32_t bytesRead = 0;
222  uint8_t* buffer = outBuffer;
223 
224  for(;File->Offset < File->FileDefinition->Size && bytesRead < (*Size); File->Offset++, bytesRead++)
225  {
226  buffer[bytesRead] = File->FileDefinition->Data[File->Offset];
227  }
228 
229  *Size = bytesRead;
230  errorCode = oC_ErrorCode_None;
231  }
232 
233  return errorCode;
234 }
235 
236 //==========================================================================================================================================
237 //==========================================================================================================================================
238 oC_ErrorCode_t oC_FlashFs_fwrite( oC_FlashFs_Context_t Context , oC_File_t File , const void * Buffer , uint32_t * Size )
239 {
240  return oC_ErrorCode_FileIsReadOnly;
241 }
242 
243 //==========================================================================================================================================
244 //==========================================================================================================================================
245 oC_ErrorCode_t oC_FlashFs_lseek( oC_FlashFs_Context_t Context , oC_File_t File , uint32_t Offset )
246 {
247  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
248 
249  if(
250  ErrorCondition( IsFileCorrect(File) , oC_ErrorCode_FileNotCorrect )
251  && ErrorCondition( Offset < File->FileDefinition->Size , oC_ErrorCode_OffsetTooBig )
252  )
253  {
254  File->Offset = Offset;
255  errorCode = oC_ErrorCode_None;
256  }
257 
258  return errorCode;
259 }
260 
261 //==========================================================================================================================================
262 //==========================================================================================================================================
263 oC_ErrorCode_t oC_FlashFs_ioctl( oC_FlashFs_Context_t Context , oC_File_t File , oC_Ioctl_Command_t Command , void * Pointer)
264 {
265  return oC_ErrorCode_NotHandledByFileSystem;
266 }
267 
268 //==========================================================================================================================================
269 //==========================================================================================================================================
270 oC_ErrorCode_t oC_FlashFs_sync( oC_FlashFs_Context_t Context , oC_File_t File )
271 {
272  return oC_ErrorCode_NotHandledByFileSystem;
273 }
274 
275 //==========================================================================================================================================
276 //==========================================================================================================================================
277 oC_ErrorCode_t oC_FlashFs_getc( oC_FlashFs_Context_t Context , char * outCharacter , oC_File_t File )
278 {
279  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
280 
281  if(
282  ErrorCondition( IsFileCorrect(File) , oC_ErrorCode_FileNotCorrect )
283  && ErrorCondition( isram(outCharacter) , oC_ErrorCode_OutputAddressNotInRAM )
284  && ErrorCondition( File->Offset < File->FileDefinition->Size , oC_ErrorCode_EndOfFile )
285  )
286  {
287  *outCharacter = File->FileDefinition->Data[File->Offset++];
288  errorCode = oC_ErrorCode_None;
289  }
290 
291  return errorCode;
292 }
293 
294 //==========================================================================================================================================
295 //==========================================================================================================================================
296 oC_ErrorCode_t oC_FlashFs_putc( oC_FlashFs_Context_t Context , char Character , oC_File_t File )
297 {
298  return oC_ErrorCode_FileIsReadOnly;
299 }
300 
301 //==========================================================================================================================================
302 //==========================================================================================================================================
303 int32_t oC_FlashFs_tell( oC_FlashFs_Context_t Context , oC_File_t File )
304 {
305  return -1;
306 }
307 
308 //==========================================================================================================================================
309 //==========================================================================================================================================
310 bool oC_FlashFs_eof( oC_FlashFs_Context_t Context , oC_File_t File )
311 {
312  bool eof = true;
313 
314  if(IsFileCorrect(File))
315  {
316  eof = File->Offset >= File->FileDefinition->Size;
317  }
318  else
319  {
320  oC_SaveError("FlashFs: EOF error - " , oC_ErrorCode_FileNotCorrect);
321  }
322 
323  return eof;
324 }
325 
326 //==========================================================================================================================================
327 //==========================================================================================================================================
328 uint32_t oC_FlashFs_size( oC_FlashFs_Context_t Context , oC_File_t File )
329 {
330  uint32_t size = 0;
331 
332  if(IsFileCorrect(File))
333  {
334  size = File->FileDefinition->Size;
335  }
336 
337  return size;
338 }
339 
340 //==========================================================================================================================================
341 //==========================================================================================================================================
342 oC_ErrorCode_t oC_FlashFs_opendir( oC_FlashFs_Context_t Context , oC_Dir_t * outDir , const char * Path )
343 {
344  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
345 
346  if(
347  ErrorCondition( isram(outDir) , oC_ErrorCode_OutputAddressNotInRAM )
348  && ErrorCondition( isaddresscorrect(Path) , oC_ErrorCode_WrongAddress )
349  && ErrorCondition( strcmp(Path,"/") == 0 , oC_ErrorCode_NoSuchFile )
350  )
351  {
352  oC_Dir_t dir = smartalloc(sizeof(struct Dir_t),AllocationFlags_CanWaitForever);
353 
354  if(dir)
355  {
356  dir->ObjectControl = oC_CountObjectControl(dir,oC_ObjectId_FlashFsDir);
357  dir->FileIndex = 0;
358  *outDir = dir;
359  errorCode = oC_ErrorCode_None;
360  }
361  else
362  {
363  errorCode = oC_ErrorCode_AllocationError;
364  }
365  }
366 
367  return errorCode;
368 }
369 
370 //==========================================================================================================================================
371 //==========================================================================================================================================
372 oC_ErrorCode_t oC_FlashFs_closedir( oC_FlashFs_Context_t Context , oC_Dir_t Dir )
373 {
374  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
375 
376  if(oC_AssignErrorCodeIfFalse(&errorCode , IsDirCorrect(Dir) , oC_ErrorCode_ObjectNotCorrect ))
377  {
378  Dir->ObjectControl = 0;
379 
380  if(smartfree(Dir,sizeof(struct Dir_t),AllocationFlags_CanWaitForever))
381  {
382  errorCode = oC_ErrorCode_None;
383  }
384  else
385  {
386  errorCode = oC_ErrorCode_ReleaseError;
387  }
388  }
389 
390  return errorCode;
391 }
392 
393 //==========================================================================================================================================
394 //==========================================================================================================================================
395 oC_ErrorCode_t oC_FlashFs_readdir( oC_FlashFs_Context_t Context , oC_Dir_t Dir , oC_FileInfo_t * outFileInfo )
396 {
397  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
398 
399  if(
400  ErrorCondition( IsDirCorrect(Dir) , oC_ErrorCode_ObjectNotCorrect )
401  && ErrorCondition( isram(outFileInfo) , oC_ErrorCode_OutputAddressNotInRAM )
402  && ErrorCondition( Dir->FileIndex < oC_FlashFs_NumberOfFiles , oC_ErrorCode_NoSuchFile )
403  )
404  {
405  outFileInfo->Name = oC_FlashFs_FileDefinitions[Dir->FileIndex].Name;
406  outFileInfo->Size = oC_FlashFs_FileDefinitions[Dir->FileIndex].Size;
407  outFileInfo->FileType = oC_FileSystem_FileType_File;
408  outFileInfo->Timestamp = 0;
409 
410  Dir->FileIndex++;
411  errorCode = oC_ErrorCode_None;
412  }
413 
414  return errorCode;
415 }
416 
417 //==========================================================================================================================================
418 //==========================================================================================================================================
419 oC_ErrorCode_t oC_FlashFs_stat( oC_FlashFs_Context_t Context , const char * Path , oC_FileInfo_t * outFileInfo)
420 {
421  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
422 
423  if(
424  ErrorCondition( isaddresscorrect(Path) , oC_ErrorCode_WrongAddress )
425  && ErrorCondition( isram(outFileInfo) , oC_ErrorCode_OutputAddressNotInRAM )
426  && ErrorCondition( Path[0] == '/' , oC_ErrorCode_PathNotCorrect )
427  )
428  {
429  const oC_FlashFs_FileDefinition_t * fileDefinition = GetFileDefinition(Path);
430 
431  if(ErrorCondition(isrom(fileDefinition) , oC_ErrorCode_NoSuchFile))
432  {
433  outFileInfo->Name = fileDefinition->Name;
434  outFileInfo->Size = fileDefinition->Size;
435  outFileInfo->FileType = oC_FileSystem_FileType_File;
436  outFileInfo->Timestamp = 0;
437  errorCode = oC_ErrorCode_None;
438  }
439  }
440 
441  return errorCode;
442 }
443 
444 //==========================================================================================================================================
445 //==========================================================================================================================================
446 oC_ErrorCode_t oC_FlashFs_unlink( oC_FlashFs_Context_t Context , const char * Path)
447 {
448  return oC_ErrorCode_NotHandledByFileSystem;
449 }
450 
451 //==========================================================================================================================================
452 //==========================================================================================================================================
453 oC_ErrorCode_t oC_FlashFs_rename( oC_FlashFs_Context_t Context , const char * OldName , const char * NewName)
454 {
455  return oC_ErrorCode_NotHandledByFileSystem;
456 }
457 
458 //==========================================================================================================================================
459 //==========================================================================================================================================
460 oC_ErrorCode_t oC_FlashFs_chmod( oC_FlashFs_Context_t Context , const char * Path, oC_FileSystem_FileAttributes_t Attributes , oC_FileSystem_FileAttributes_t Mask)
461 {
462  return oC_ErrorCode_NotHandledByFileSystem;
463 }
464 
465 //==========================================================================================================================================
466 //==========================================================================================================================================
467 oC_ErrorCode_t oC_FlashFs_utime( oC_FlashFs_Context_t Context , const char * Path , oC_Timestamp_t Timestamp )
468 {
469  return oC_ErrorCode_NotHandledByFileSystem;
470 }
471 
472 //==========================================================================================================================================
473 //==========================================================================================================================================
474 oC_ErrorCode_t oC_FlashFs_mkdir( oC_FlashFs_Context_t Context , const char * Path)
475 {
476  return oC_ErrorCode_NotHandledByFileSystem;
477 }
478 
479 //==========================================================================================================================================
480 //==========================================================================================================================================
481 bool oC_FlashFs_DirExists( oC_FlashFs_Context_t Context , const char * Path)
482 {
483  return strcmp(Path,"/") == 0;
484 }
485 
486 #undef _________________________________________INTERFACE_FUNCTIONS________________________________________________________________________
487 
488 
494 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
495 
496 static bool IsFileCorrect( oC_File_t File )
497 {
498  return isram(File) && oC_CheckObjectControl(File,oC_ObjectId_FlashFsFile,File->ObjectControl);
499 }
500 
501 static bool IsDirCorrect( oC_Dir_t Dir )
502 {
503  return isram(Dir) && oC_CheckObjectControl(Dir,oC_ObjectId_FlashFsDir,Dir->ObjectControl);
504 }
505 
506 static const oC_FlashFs_FileDefinition_t * GetFileDefinition( const char * Path )
507 {
508  const oC_FlashFs_FileDefinition_t * fileDefinition = NULL;
509 
510  oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(oC_FlashFs_FileDefinitions,oC_FlashFs_NumberOfFiles,file)
511  {
512  if(strcmp(&Path[1],file->Name) == 0)
513  {
514  fileDefinition = file;
515  break;
516  }
517  }
518 
519  return fileDefinition;
520 }
521 
522 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
523 
The file with helper macros for managing objects.
uint32_t oC_ObjectControl_t
stores object control value
Definition: oc_object.h:141
Interface for the Flash File System.
oC_FileSystem_ModeFlags_t
Definition: oc_fs.h:54
The file with interface for file systems.
static bool oC_Bits_AreBitsClearU32(uint32_t BitMask, uint32_t BitsToCheck)
checks if all bits in field are clear
Definition: oc_bits.h:952
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
Definition: oc_object.h:168
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
Definition: oc_object.h:203
The file with functions for the bits operation.
stores ETH context
Definition: oc_eth.c:97
Static array definitions.
The file with interface for string library.
moves offset pointer to the end of the file
Definition: oc_fs.h:58
Definition: oc_devfs.c:60
#define NULL
pointer to a zero
Definition: oc_null.h:37