Choco OS  V.0.16.9.0
Join to the chocolate world
oc_devfs.c
Go to the documentation of this file.
1 
27 #include <oc_devfs.h>
28 #include <oc_list.h>
29 #include <oc_object.h>
30 #include <oc_stdio.h>
31 #include <oc_driverman.h>
32 #include <string.h>
33 
39 #define _________________________________________DEFINITIONS_SECTION________________________________________________________________________
40 
41 #define MAX_PATH_LENGTH 30
42 
43 #undef _________________________________________DEFINITIONS_SECTION________________________________________________________________________
44 
45 
51 #define _________________________________________TYPES_SECTION______________________________________________________________________________
52 
53 struct File_t
54 {
55  oC_ObjectControl_t ObjectControl;
56  oC_Driver_t Driver;
57  void * DriverContext;
58 };
59 
60 struct Dir_t
61 {
62  oC_ObjectControl_t ObjectControl;
63  uint32_t CurrentDriver;
64 };
65 
66 #undef _________________________________________TYPES_SECTION______________________________________________________________________________
67 
73 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
74 
75 static bool IsFileCorrect( oC_File_t File );
76 static bool IsDirCorrect( oC_Dir_t Dir );
77 
78 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
79 
80 
86 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
87 
88 //==========================================================================================================================================
89 //==========================================================================================================================================
90 const oC_FileSystem_Registration_t DevFs = {
91  .Name = "DevFs" ,
92  .init = oC_DevFs_init ,
93  .deinit = oC_DevFs_deinit ,
94  .fopen = oC_DevFs_fopen ,
95  .fclose = oC_DevFs_fclose ,
96  .fread = oC_DevFs_fread ,
97  .fwrite = oC_DevFs_fwrite ,
98  .lseek = oC_DevFs_lseek ,
99  .ioctl = oC_DevFs_ioctl ,
100  .sync = oC_DevFs_sync ,
101  .Getc = oC_DevFs_getc ,
102  .Putc = oC_DevFs_putc ,
103  .tell = oC_DevFs_tell ,
104  .eof = oC_DevFs_eof ,
105  .size = oC_DevFs_size ,
106 
107  .opendir = oC_DevFs_opendir ,
108  .closedir = oC_DevFs_closedir ,
109  .readdir = oC_DevFs_readdir ,
110 
111  .stat = oC_DevFs_stat ,
112  .unlink = oC_DevFs_unlink ,
113  .rename = oC_DevFs_rename ,
114  .chmod = oC_DevFs_chmod ,
115  .utime = oC_DevFs_utime ,
116  .mkdir = oC_DevFs_mkdir ,
117  .DirExists= oC_DevFs_DirExists,
118 };
119 
120 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
121 
122 
128 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
129 
130 //==========================================================================================================================================
131 //==========================================================================================================================================
132 oC_ErrorCode_t oC_DevFs_init( oC_DevFs_Context_t * outContext )
133 {
134  return oC_ErrorCode_None;
135 }
136 
137 //==========================================================================================================================================
138 //==========================================================================================================================================
139 oC_ErrorCode_t oC_DevFs_deinit( oC_DevFs_Context_t Context )
140 {
141  return oC_ErrorCode_None;
142 }
143 
144 //==========================================================================================================================================
145 //==========================================================================================================================================
146 oC_ErrorCode_t oC_DevFs_fopen( oC_DevFs_Context_t Context , oC_File_t * outFile , const char * Path , oC_FileSystem_ModeFlags_t Mode , oC_FileSystem_FileAttributes_t Attributes )
147 {
148  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
149 
150  if(ErrorCondition(isaddresscorrect(Path) , oC_ErrorCode_PathNotCorrect))
151  {
152  oC_Driver_t driver = oC_DriverMan_GetDriver(&Path[1]);
153 
154  if(
155  oC_AssignErrorCodeIfFalse(&errorCode , isram(outFile) , oC_ErrorCode_OutputAddressNotInRAM) &&
156  oC_AssignErrorCodeIfFalse(&errorCode , isaddresscorrect(driver) , oC_ErrorCode_NoSuchFile)
157  )
158  {
159  oC_File_t file = smartalloc(sizeof(struct File_t),AllocationFlags_CanWait1Second);
160 
161  if(file)
162  {
163  file->ObjectControl = oC_CountObjectControl(file,oC_ObjectId_DevFsFile);
164  file->Driver = driver;
165  file->DriverContext = NULL;
166  *outFile = file;
167  errorCode = oC_ErrorCode_None;
168  }
169  else
170  {
171  errorCode = oC_ErrorCode_AllocationError;
172  }
173  }
174  }
175 
176  return errorCode;
177 }
178 
179 //==========================================================================================================================================
180 //==========================================================================================================================================
181 oC_ErrorCode_t oC_DevFs_fclose( oC_DevFs_Context_t Context , oC_File_t File )
182 {
183  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
184 
185  if(
186  oC_AssignErrorCodeIfFalse(&errorCode,IsFileCorrect(File),oC_ErrorCode_ObjectNotCorrect)
187  )
188  {
189  File->ObjectControl = 0;
190 
191  if(smartfree(File,sizeof(struct File_t),AllocationFlags_CanWaitForever))
192  {
193  errorCode = oC_ErrorCode_None;
194  }
195  else
196  {
197  errorCode = oC_ErrorCode_ReleaseError;
198  }
199  }
200 
201  return errorCode;
202 }
203 
204 //==========================================================================================================================================
205 //==========================================================================================================================================
206 oC_ErrorCode_t oC_DevFs_fread( oC_DevFs_Context_t Context , oC_File_t File , void * outBuffer , uint32_t * Size )
207 {
208  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
209 
210  if(
211  oC_AssignErrorCodeIfFalse(&errorCode,IsFileCorrect(File), oC_ErrorCode_ObjectNotCorrect) &&
212  oC_AssignErrorCodeIfFalse(&errorCode,isram(outBuffer), oC_ErrorCode_OutputAddressNotInRAM) &&
213  oC_AssignErrorCodeIfFalse(&errorCode,isram(Size), oC_ErrorCode_OutputAddressNotInRAM) &&
214  oC_AssignErrorCodeIfFalse(&errorCode,*Size > 0, oC_ErrorCode_SizeNotCorrect) &&
215  oC_AssignErrorCodeIfFalse(&errorCode,isarrayinram(outBuffer,*Size), oC_ErrorCode_ArraySizeTooBig)
216  )
217  {
218  errorCode = oC_Driver_Read(File->Driver,File->DriverContext,outBuffer,Size,oC_hour(1));
219  }
220 
221  return errorCode;
222 }
223 
224 //==========================================================================================================================================
225 //==========================================================================================================================================
226 oC_ErrorCode_t oC_DevFs_fwrite( oC_DevFs_Context_t Context , oC_File_t File , const void * Buffer , uint32_t * Size )
227 {
228  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
229 
230  if(
231  oC_AssignErrorCodeIfFalse(&errorCode,IsFileCorrect(File), oC_ErrorCode_ObjectNotCorrect) &&
232  oC_AssignErrorCodeIfFalse(&errorCode,isaddresscorrect(Buffer), oC_ErrorCode_WrongAddress) &&
233  oC_AssignErrorCodeIfFalse(&errorCode,isram(Size), oC_ErrorCode_OutputAddressNotInRAM) &&
234  oC_AssignErrorCodeIfFalse(&errorCode,*Size > 0, oC_ErrorCode_SizeNotCorrect) &&
235  oC_AssignErrorCodeIfFalse(&errorCode,isarraycorrect(Buffer,*Size), oC_ErrorCode_ArraySizeTooBig)
236  )
237  {
238  errorCode = oC_Driver_Write(File->Driver,File->DriverContext,Buffer,Size,oC_hour(1));
239  }
240 
241  return errorCode;
242 }
243 
244 //==========================================================================================================================================
245 //==========================================================================================================================================
246 oC_ErrorCode_t oC_DevFs_lseek( oC_DevFs_Context_t Context , oC_File_t File , uint32_t Offset )
247 {
248  return oC_ErrorCode_NotHandledByDriver;
249 }
250 
251 //==========================================================================================================================================
252 //==========================================================================================================================================
253 oC_ErrorCode_t oC_DevFs_ioctl( oC_DevFs_Context_t Context , oC_File_t File , oC_Ioctl_Command_t Command , void * Pointer)
254 {
255  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
256 
257  if(
258  oC_AssignErrorCodeIfFalse(&errorCode,IsFileCorrect(File), oC_ErrorCode_ObjectNotCorrect) &&
259  oC_AssignErrorCodeIfFalse(&errorCode,oC_Ioctl_IsCorrectCommand(Command), oC_ErrorCode_CommandNotCorrect)
260  )
261  {
262  if(Command == oC_IoCtl_SpecialCommand_SetDriverContext)
263  {
264  File->DriverContext = Pointer;
265  errorCode = oC_ErrorCode_None;
266  }
267  else
268  {
269  errorCode = oC_Driver_HandleIoctl(File->Driver,File->DriverContext,Command,Pointer);
270  }
271  }
272 
273  return errorCode;
274 }
275 
276 //==========================================================================================================================================
277 //==========================================================================================================================================
278 oC_ErrorCode_t oC_DevFs_sync( oC_DevFs_Context_t Context , oC_File_t File )
279 {
280  return oC_ErrorCode_NotHandledByFileSystem;
281 }
282 
283 //==========================================================================================================================================
284 //==========================================================================================================================================
285 oC_ErrorCode_t oC_DevFs_getc( oC_DevFs_Context_t Context , char * outCharacter , oC_File_t File )
286 {
287  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
288 
289  if(
290  oC_AssignErrorCodeIfFalse(&errorCode,IsFileCorrect(File), oC_ErrorCode_ObjectNotCorrect) &&
291  oC_AssignErrorCodeIfFalse(&errorCode,isram(outCharacter), oC_ErrorCode_OutputAddressNotInRAM)
292  )
293  {
294  uint32_t bytesToRead = 1;
295  errorCode = oC_Driver_Read(File->Driver,File->DriverContext,outCharacter,&bytesToRead,oC_hour(1));
296  }
297 
298  return errorCode;
299 }
300 
301 //==========================================================================================================================================
302 //==========================================================================================================================================
303 oC_ErrorCode_t oC_DevFs_putc( oC_DevFs_Context_t Context , char Character , oC_File_t File )
304 {
305  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
306 
307  if(
308  oC_AssignErrorCodeIfFalse(&errorCode,IsFileCorrect(File), oC_ErrorCode_ObjectNotCorrect)
309  )
310  {
311  uint32_t bytesToWrite = 1;
312  errorCode = oC_Driver_Write(File->Driver,File->DriverContext,&Character,&bytesToWrite,oC_hour(1));
313  }
314 
315  return errorCode;
316 }
317 
318 //==========================================================================================================================================
319 //==========================================================================================================================================
320 int32_t oC_DevFs_tell( oC_DevFs_Context_t Context , oC_File_t File )
321 {
322  return -1;
323 }
324 
325 //==========================================================================================================================================
326 //==========================================================================================================================================
327 bool oC_DevFs_eof( oC_DevFs_Context_t Context , oC_File_t File )
328 {
329  return true;
330 }
331 
332 //==========================================================================================================================================
333 //==========================================================================================================================================
334 uint32_t oC_DevFs_size( oC_DevFs_Context_t Context , oC_File_t File )
335 {
336  return sizeof(oC_Driver_Registration_t);
337 }
338 
339 //==========================================================================================================================================
340 //==========================================================================================================================================
341 oC_ErrorCode_t oC_DevFs_opendir( oC_DevFs_Context_t Context , oC_Dir_t * outDir , const char * Path )
342 {
343  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
344 
345  if(
346  oC_AssignErrorCodeIfFalse(&errorCode , isram(outDir) , oC_ErrorCode_OutputAddressNotInRAM) &&
347  oC_AssignErrorCodeIfFalse(&errorCode , isaddresscorrect(Path) , oC_ErrorCode_WrongAddress) &&
348  oC_AssignErrorCodeIfFalse(&errorCode , strcmp(Path,"/") == 0 , oC_ErrorCode_NoSuchFile)
349  )
350  {
351  oC_Dir_t dir = smartalloc(sizeof(struct Dir_t),AllocationFlags_CanWaitForever);
352 
353  if(dir)
354  {
355  dir->ObjectControl = oC_CountObjectControl(dir,oC_ObjectId_DevFsDir);
356  dir->CurrentDriver = 0;
357  *outDir = dir;
358  errorCode = oC_ErrorCode_None;
359  }
360  else
361  {
362  errorCode = oC_ErrorCode_AllocationError;
363  }
364  }
365 
366  return errorCode;
367 }
368 
369 //==========================================================================================================================================
370 //==========================================================================================================================================
371 oC_ErrorCode_t oC_DevFs_closedir( oC_DevFs_Context_t Context , oC_Dir_t Dir )
372 {
373  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
374 
375  if(oC_AssignErrorCodeIfFalse(&errorCode , IsDirCorrect(Dir) , oC_ErrorCode_ObjectNotCorrect ))
376  {
377  Dir->ObjectControl = 0;
378 
379  if(smartfree(Dir,sizeof(struct Dir_t),AllocationFlags_CanWaitForever))
380  {
381  errorCode = oC_ErrorCode_None;
382  }
383  else
384  {
385  errorCode = oC_ErrorCode_ReleaseError;
386  }
387  }
388 
389  return errorCode;
390 }
391 
392 //==========================================================================================================================================
393 //==========================================================================================================================================
394 oC_ErrorCode_t oC_DevFs_readdir( oC_DevFs_Context_t Context , oC_Dir_t Dir , oC_FileInfo_t * outFileInfo )
395 {
396  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
397 
398  if(
399  oC_AssignErrorCodeIfFalse(&errorCode , IsDirCorrect(Dir) , oC_ErrorCode_ObjectNotCorrect ) &&
400  oC_AssignErrorCodeIfFalse(&errorCode , isram(outFileInfo), oC_ErrorCode_OutputAddressNotInRAM )
401  )
402  {
403  oC_List(oC_Driver_t) drivers = oC_DriverMan_GetList();
404 
405  if(oC_List_IsCorrect(drivers))
406  {
407  oC_Driver_t driver = NULL;
408 
409  oC_List_At(drivers,Dir->CurrentDriver,driver);
410 
411  if(driver)
412  {
413  outFileInfo->Name = driver->FileName;
414  outFileInfo->Size = sizeof(oC_Driver_Registration_t);
415  outFileInfo->Timestamp = 0;
416  outFileInfo->FileType = oC_FileSystem_FileType_File;
417 
418  Dir->CurrentDriver++;
419 
420  errorCode = oC_ErrorCode_None;
421  }
422  else
423  {
424  errorCode = oC_ErrorCode_NoSuchFile;
425  }
426  }
427  else
428  {
429  errorCode = oC_ErrorCode_ListNotCorrect;
430  }
431  }
432 
433  return errorCode;
434 }
435 
436 //==========================================================================================================================================
437 //==========================================================================================================================================
438 oC_ErrorCode_t oC_DevFs_stat( oC_DevFs_Context_t Context , const char * Path , oC_FileInfo_t * outFileInfo)
439 {
440  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
441 
442  if(
443  oC_AssignErrorCodeIfFalse(&errorCode , isaddresscorrect(Path) , oC_ErrorCode_WrongAddress ) &&
444  oC_AssignErrorCodeIfFalse(&errorCode , isram(outFileInfo) , oC_ErrorCode_OutputAddressNotInRAM ) &&
445  oC_AssignErrorCodeIfFalse(&errorCode , strlen(Path) >= 2 , oC_ErrorCode_NoSuchFile ) &&
446  oC_AssignErrorCodeIfFalse(&errorCode , Path[0] == '/' , oC_ErrorCode_NoSuchFile )
447  )
448  {
449  oC_List(oC_Driver_t) drivers = oC_DriverMan_GetList();
450 
451  errorCode = oC_ErrorCode_NoSuchFile;
452 
453  oC_List_Foreach(drivers,driver)
454  {
455  if(strcmp(&Path[1],driver->FileName)==0)
456  {
457  outFileInfo->Name = driver->FileName;
458  outFileInfo->Size = sizeof(oC_Driver_Registration_t);
459  outFileInfo->Timestamp = 0;
460 
461  errorCode = oC_ErrorCode_None;
462 
463  break;
464  }
465  }
466  }
467 
468  return errorCode;
469 }
470 
471 //==========================================================================================================================================
472 //==========================================================================================================================================
473 oC_ErrorCode_t oC_DevFs_unlink( oC_DevFs_Context_t Context , const char * Path)
474 {
475  return oC_ErrorCode_NotHandledByFileSystem;
476 }
477 
478 //==========================================================================================================================================
479 //==========================================================================================================================================
480 oC_ErrorCode_t oC_DevFs_rename( oC_DevFs_Context_t Context , const char * OldName , const char * NewName)
481 {
482  return oC_ErrorCode_NotHandledByFileSystem;
483 }
484 
485 //==========================================================================================================================================
486 //==========================================================================================================================================
487 oC_ErrorCode_t oC_DevFs_chmod( oC_DevFs_Context_t Context , const char * Path, oC_FileSystem_FileAttributes_t Attributes , oC_FileSystem_FileAttributes_t Mask)
488 {
489  return oC_ErrorCode_NotHandledByFileSystem;
490 }
491 
492 //==========================================================================================================================================
493 //==========================================================================================================================================
494 oC_ErrorCode_t oC_DevFs_utime( oC_DevFs_Context_t Context , const char * Path , oC_Timestamp_t Timestamp )
495 {
496  return oC_ErrorCode_NotHandledByFileSystem;
497 }
498 
499 //==========================================================================================================================================
500 //==========================================================================================================================================
501 oC_ErrorCode_t oC_DevFs_mkdir( oC_DevFs_Context_t Context , const char * Path )
502 {
503  return oC_ErrorCode_NotHandledByFileSystem;
504 }
505 
506 //==========================================================================================================================================
507 //==========================================================================================================================================
508 bool oC_DevFs_DirExists( oC_DevFs_Context_t Context , const char * Path)
509 {
510  return strcmp(Path,"/") == 0;
511 }
512 
513 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
514 
520 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
521 
522 static bool IsFileCorrect( oC_File_t File )
523 {
524  return isram(File) && oC_CheckObjectControl(File,oC_ObjectId_DevFsFile,File->ObjectControl);
525 }
526 
527 static bool IsDirCorrect( oC_Dir_t Dir )
528 {
529  return isram(Dir) && oC_CheckObjectControl(Dir,oC_ObjectId_DevFsDir,Dir->ObjectControl);
530 }
531 
532 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
The file with helper macros for managing objects.
uint32_t oC_ObjectControl_t
stores object control value
Definition: oc_object.h:141
The file with list library.
oC_FileSystem_ModeFlags_t
Definition: oc_fs.h:54
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 drivers manager interface.
stores ETH context
Definition: oc_eth.c:97
The file with interface for device drivers file system.
Definition: oc_devfs.c:60
The file with standard input/output operations.
#define NULL
pointer to a zero
Definition: oc_null.h:37