This file provides a set of file access functions.
Definition in file fsaccess.h.
#include <stdio.h>
#include "file.h"
#include "navigation.h"
#include "ctrl_access.h"
Go to the source code of this file.
Defines | |
#define | O_APPEND 0x0008 |
#define | O_CREAT 0x0200 |
#define | O_RDONLY 0 |
#define | O_RDWR 0x0002 |
#define | O_WRONLY 0x0001 |
Typedefs | |
typedef signed int | ssize_t |
Functions | |
Bool | b_fsaccess_init (void) |
This function initializes mutex and navigators. | |
int | close (int fd) |
This function closes a file. | |
long | fsaccess_alloc_nav_id (void) |
This function returns a free navigator id. | |
size_t | fsaccess_file_get_size (int fd) |
This function returns the opened file size. | |
void | fsaccess_free_nav_id (int fd) |
This function frees a navigator id. | |
void | fsaccess_give_mutex (void) |
This function frees the mutex. | |
S8 | fsaccess_IsDirPresent (const char *pcStringDirName) |
Is a directory present? | |
void | fsaccess_take_mutex (void) |
This function gives the mutex to the caller. | |
int | open (const char *pathname, int flags,...) |
This function opens a file. | |
ssize_t | read (int fd, void *buf, size_t count) |
This function reads from a file. | |
ssize_t | write (int fd, const void *buf, size_t count) |
This function writes to a file. |
#define O_APPEND 0x0008 |
#define O_CREAT 0x0200 |
#define O_RDONLY 0 |
Definition at line 60 of file fsaccess.h.
Referenced by fsaccess_example_read(), and fsaccess_example_write().
#define O_RDWR 0x0002 |
#define O_WRONLY 0x0001 |
typedef signed int ssize_t |
Definition at line 67 of file fsaccess.h.
Bool b_fsaccess_init | ( | void | ) |
This function initializes mutex and navigators.
Definition at line 334 of file fsaccess.c.
References nav_reset().
Referenced by main().
00335 { 00336 nav_reset(); 00337 #ifdef FREERTOS_USED 00338 if (xFs_Access == NULL) 00339 { 00340 vSemaphoreCreateBinary( xFs_Access ); 00341 if( xFs_Access == NULL ) 00342 return( FALSE ); 00343 else 00344 return( TRUE ); 00345 } 00346 #endif 00347 return( TRUE ); 00348 }
int close | ( | int | fd | ) |
This function closes a file.
fd | file descriptor. |
Definition at line 274 of file fsaccess.c.
References file_close(), fsaccess_free_nav_id(), fsaccess_give_mutex(), fsaccess_take_mutex(), and nav_select().
Referenced by fsaccess_example_read(), and fsaccess_example_write().
00275 { 00276 if (fd < 0) 00277 { 00278 return (-1); 00279 } 00280 // take the mutex for nav access 00281 fsaccess_take_mutex(); 00282 00283 nav_select( fd ); 00284 00285 file_close(); 00286 00287 00288 fsaccess_free_nav_id(fd); 00289 00290 // give the mutex for nav access 00291 fsaccess_give_mutex(); 00292 00293 return (0); 00294 }
long fsaccess_alloc_nav_id | ( | void | ) |
This function returns a free navigator id.
Definition at line 301 of file fsaccess.c.
References FS_NB_NAVIGATOR, FS_NB_RESERVED_NAV, and pvNavUsed.
Referenced by fsaccess_IsDirPresent(), and open().
00302 { 00303 unsigned int j; 00304 // get a free nav id 00305 // NOTE: we start at FS_NB_RESERVED_NAV, because 2 is usually used as 00306 // the navigator for file copy. 0 & 1 may be used independently of the fsaccess module. 00307 for (j = FS_NB_RESERVED_NAV ; j < FS_NB_NAVIGATOR ; j++) 00308 { 00309 if (!Tst_bits(pvNavUsed, (1 << j))) 00310 { 00311 Set_bits(pvNavUsed, (1 << j)); 00312 return (j); 00313 } 00314 } 00315 // no nav free for now 00316 return(-1); 00317 }
size_t fsaccess_file_get_size | ( | int | fd | ) |
This function returns the opened file size.
fd | file descriptor. |
Definition at line 182 of file fsaccess.c.
References fsaccess_give_mutex(), fsaccess_take_mutex(), nav_file_lgt(), and nav_select().
Referenced by fsaccess_example_read(), and fsaccess_example_write().
00183 { 00184 size_t size; 00185 00186 if (fd < 0) 00187 { 00188 return (0); 00189 } 00190 // take the mutex for nav access 00191 fsaccess_take_mutex(); 00192 // select the navigator 00193 nav_select( fd ); 00194 // get the file size 00195 size = nav_file_lgt(); 00196 // give the mutex for nav access 00197 fsaccess_give_mutex(); 00198 return (size); 00199 }
void fsaccess_free_nav_id | ( | int | fd | ) |
This function frees a navigator id.
fd | file descriptor. |
Definition at line 325 of file fsaccess.c.
References pvNavUsed.
Referenced by close(), fsaccess_IsDirPresent(), and open().
00326 { 00327 // mark NavId as free 00328 Clr_bits(pvNavUsed, (1 << fd)); 00329 }
void fsaccess_give_mutex | ( | void | ) |
This function frees the mutex.
Definition at line 365 of file fsaccess.c.
Referenced by close(), fsaccess_file_get_size(), fsaccess_IsDirPresent(), open(), read(), and write().
00366 { 00367 #ifdef FREERTOS_USED 00368 // Release the mutex. 00369 xSemaphoreGive( xFs_Access ); 00370 #endif 00371 }
S8 fsaccess_IsDirPresent | ( | const char * | pcStringDirName | ) |
Is a directory present?
pcStringDirName | Input. Directory name string. |
Definition at line 381 of file fsaccess.c.
References fsaccess_alloc_nav_id(), fsaccess_free_nav_id(), fsaccess_give_mutex(), fsaccess_take_mutex(), nav_select(), and nav_setcwd().
00382 { 00383 signed short TempoNavId; 00384 S8 RetVal; 00385 00386 00387 fsaccess_take_mutex(); // Take the mutex on the file system access. 00388 00389 TempoNavId = fsaccess_alloc_nav_id(); // Get a navigator. 00390 if( -1 == TempoNavId ) // No navigator. 00391 { 00392 // give the mutex for nav access 00393 fsaccess_give_mutex(); 00394 return( -1 ); 00395 } 00396 00397 // select the navigator 00398 nav_select( TempoNavId ); 00399 00400 // Try to enter in the directory. 00401 RetVal = (S8)nav_setcwd( (FS_STRING)pcStringDirName, TRUE, FALSE ); 00402 00403 fsaccess_free_nav_id( TempoNavId ); // mark NavId as free 00404 fsaccess_give_mutex(); // give the mutex for nav access 00405 00406 return( RetVal ); 00407 }
void fsaccess_take_mutex | ( | void | ) |
This function gives the mutex to the caller.
Definition at line 354 of file fsaccess.c.
Referenced by close(), fsaccess_file_get_size(), fsaccess_IsDirPresent(), open(), read(), and write().
00355 { 00356 #ifdef FREERTOS_USED 00357 // wait for semaphore 00358 while( xSemaphoreTake( xFs_Access, portMAX_DELAY ) != pdTRUE ); 00359 #endif 00360 }
int open | ( | const char * | pathname, | |
int | flags, | |||
... | ||||
) |
This function opens a file.
pathname | path of the file to open. | |
flags | flags to give file access rights should be O_CREAT : create file if not exist O_APPEND : add data to the end of file O_RDONLY : Read Only O_WRONLY : Write Only O_RDWR : Read/Write |
This function opens a file.
pathname | path of the file to open. | |
flags | flags to give file access rights should be O_CREAT : create file if not exist O_APPEND : add data to the end of file O_RDONLY : Read Only O_WRONLY : Write Only O_RDWR : Read/Write |
Definition at line 107 of file fsaccess.c.
References file_open(), FOPEN_MODE_APPEND, FOPEN_MODE_R, FOPEN_MODE_R_PLUS, FOPEN_MODE_W, fsaccess_alloc_nav_id(), fsaccess_free_nav_id(), fsaccess_give_mutex(), fsaccess_take_mutex(), nav_select(), nav_setcwd(), O_APPEND, O_CREAT, O_RDWR, and O_WRONLY.
Referenced by fsaccess_example_read(), and fsaccess_example_write().
00108 { 00109 int CurrentNavId = -1; 00110 00111 00112 // take the mutex for nav access 00113 fsaccess_take_mutex(); 00114 00115 // if no navigator available, return an error 00116 if ((CurrentNavId = fsaccess_alloc_nav_id()) < 0) goto error_exit; 00117 00118 // select the navigator 00119 nav_select( CurrentNavId ); 00120 00121 // the filesystem is now at the root directory 00122 if ((flags & O_CREAT) == O_CREAT) 00123 { 00124 // try to create, the flag is set 00125 if(nav_setcwd((FS_STRING)pathname, FALSE, TRUE) == FALSE) 00126 { 00127 goto error_free_nav; 00128 } 00129 } 00130 else 00131 { 00132 // do not try to create, if it doesn't exist, error 00133 if(nav_setcwd((FS_STRING)pathname, FALSE, FALSE) == FALSE) 00134 { 00135 goto error_free_nav; 00136 } 00137 } 00138 00139 // if user wants to append in file 00140 if ((flags & O_APPEND) == O_APPEND) 00141 { 00142 // open the file 00143 if (file_open(FOPEN_MODE_APPEND) == FALSE) goto error_free_nav; 00144 } 00145 else if ((flags & O_RDWR) == O_RDWR) 00146 { 00147 // open as read/write 00148 if (file_open(FOPEN_MODE_R_PLUS) == FALSE) goto error_free_nav; 00149 } 00150 else if ((flags & O_WRONLY) == O_WRONLY) 00151 { 00152 // open as write 00153 if (file_open(FOPEN_MODE_W) == FALSE) goto error_free_nav; 00154 } 00155 else 00156 { 00157 // open as read 00158 if (file_open(FOPEN_MODE_R) == FALSE) goto error_free_nav; 00159 } 00160 00161 // give the mutex for nav access 00162 fsaccess_give_mutex(); 00163 00164 return (CurrentNavId); 00165 00166 error_free_nav: 00167 // mark NavId as free 00168 fsaccess_free_nav_id(CurrentNavId); 00169 error_exit: 00170 // give the mutex for nav access 00171 fsaccess_give_mutex(); 00172 return(-1); 00173 }
ssize_t read | ( | int | fd, | |
void * | buf, | |||
size_t | count | |||
) |
This function reads from a file.
fd | file descriptor. | |
buf | pointer for data that are read. | |
count | amount of bytes to read |
Definition at line 210 of file fsaccess.c.
References file_eof(), file_read_buf(), fsaccess_give_mutex(), fsaccess_take_mutex(), and nav_select().
Referenced by fsaccess_example_read(), and fsaccess_example_write().
00211 { 00212 ssize_t ReadCount = -1; 00213 00214 if (fd < 0) 00215 { 00216 return (-1); 00217 } 00218 // take the mutex for nav access 00219 fsaccess_take_mutex(); 00220 00221 nav_select( fd ); 00222 00223 if ( file_eof() ) 00224 { 00225 // give the mutex for nav access 00226 fsaccess_give_mutex(); 00227 return(-1); 00228 } 00229 00230 ReadCount = (ssize_t)file_read_buf((U8 *)buf , (U16)count); 00231 00232 // give the mutex for nav access 00233 fsaccess_give_mutex(); 00234 return(ReadCount); 00235 }
ssize_t write | ( | int | fd, | |
const void * | buf, | |||
size_t | count | |||
) |
This function writes to a file.
fd | file descriptor. | |
buf | pointer from where data are written. | |
count | amount of bytes to write |
Definition at line 247 of file fsaccess.c.
References file_write_buf(), fsaccess_give_mutex(), fsaccess_take_mutex(), and nav_select().
Referenced by fsaccess_example_write().
00248 { 00249 ssize_t WriteCount = -1; 00250 00251 if (fd < 0) 00252 { 00253 return (-1); 00254 } 00255 // take the mutex for nav access 00256 fsaccess_take_mutex(); 00257 00258 nav_select( fd ); 00259 00260 WriteCount = (ssize_t)file_write_buf((U8 *)buf , (U16)count); 00261 00262 // give the mutex for nav access 00263 fsaccess_give_mutex(); 00264 return(WriteCount); 00265 }