00001
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #include <stdio.h>
00052 #include <string.h>
00053 #include <ctype.h>
00054
00055 #include "fsaccess.h"
00056
00057
00058 #ifdef FREERTOS_USED
00059 #include "FreeRTOS.h"
00060 #include "semphr.h"
00061 #include "task.h"
00062 #include "portmacro.h"
00063 #endif
00064
00065
00066
00067 #include "compiler.h"
00068
00069
00070
00071 #include "file.h"
00072 #include "navigation.h"
00073 #include "ctrl_access.h"
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00087 static unsigned int pvNavUsed = 0;
00088
00090 #ifdef FREERTOS_USED
00091 static xSemaphoreHandle xFs_Access;
00092 #endif
00093
00094
00107 int open(const char *pathname, int flags, ...)
00108 {
00109 int CurrentNavId = -1;
00110
00111
00112
00113 fsaccess_take_mutex();
00114
00115
00116 if ((CurrentNavId = fsaccess_alloc_nav_id()) < 0) goto error_exit;
00117
00118
00119 nav_select( CurrentNavId );
00120
00121
00122 if ((flags & O_CREAT) == O_CREAT)
00123 {
00124
00125 if(nav_setcwd((FS_STRING)pathname, FALSE, TRUE) == FALSE)
00126 {
00127 goto error_free_nav;
00128 }
00129 }
00130 else
00131 {
00132
00133 if(nav_setcwd((FS_STRING)pathname, FALSE, FALSE) == FALSE)
00134 {
00135 goto error_free_nav;
00136 }
00137 }
00138
00139
00140 if ((flags & O_APPEND) == O_APPEND)
00141 {
00142
00143 if (file_open(FOPEN_MODE_APPEND) == FALSE) goto error_free_nav;
00144 }
00145 else if ((flags & O_RDWR) == O_RDWR)
00146 {
00147
00148 if (file_open(FOPEN_MODE_R_PLUS) == FALSE) goto error_free_nav;
00149 }
00150 else if ((flags & O_WRONLY) == O_WRONLY)
00151 {
00152
00153 if (file_open(FOPEN_MODE_W) == FALSE) goto error_free_nav;
00154 }
00155 else
00156 {
00157
00158 if (file_open(FOPEN_MODE_R) == FALSE) goto error_free_nav;
00159 }
00160
00161
00162 fsaccess_give_mutex();
00163
00164 return (CurrentNavId);
00165
00166 error_free_nav:
00167
00168 fsaccess_free_nav_id(CurrentNavId);
00169 error_exit:
00170
00171 fsaccess_give_mutex();
00172 return(-1);
00173 }
00174
00182 size_t fsaccess_file_get_size(int fd)
00183 {
00184 size_t size;
00185
00186 if (fd < 0)
00187 {
00188 return (0);
00189 }
00190
00191 fsaccess_take_mutex();
00192
00193 nav_select( fd );
00194
00195 size = nav_file_lgt();
00196
00197 fsaccess_give_mutex();
00198 return (size);
00199 }
00200
00210 ssize_t read(int fd, void *buf, size_t count)
00211 {
00212 ssize_t ReadCount = -1;
00213
00214 if (fd < 0)
00215 {
00216 return (-1);
00217 }
00218
00219 fsaccess_take_mutex();
00220
00221 nav_select( fd );
00222
00223 if ( file_eof() )
00224 {
00225
00226 fsaccess_give_mutex();
00227 return(-1);
00228 }
00229
00230 ReadCount = (ssize_t)file_read_buf((U8 *)buf , (U16)count);
00231
00232
00233 fsaccess_give_mutex();
00234 return(ReadCount);
00235 }
00236
00237
00247 ssize_t write(int fd, const void *buf, size_t count)
00248 {
00249 ssize_t WriteCount = -1;
00250
00251 if (fd < 0)
00252 {
00253 return (-1);
00254 }
00255
00256 fsaccess_take_mutex();
00257
00258 nav_select( fd );
00259
00260 WriteCount = (ssize_t)file_write_buf((U8 *)buf , (U16)count);
00261
00262
00263 fsaccess_give_mutex();
00264 return(WriteCount);
00265 }
00266
00274 int close(int fd)
00275 {
00276 if (fd < 0)
00277 {
00278 return (-1);
00279 }
00280
00281 fsaccess_take_mutex();
00282
00283 nav_select( fd );
00284
00285 file_close();
00286
00287
00288 fsaccess_free_nav_id(fd);
00289
00290
00291 fsaccess_give_mutex();
00292
00293 return (0);
00294 }
00295
00301 long fsaccess_alloc_nav_id( void )
00302 {
00303 unsigned int j;
00304
00305
00306
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
00316 return(-1);
00317 }
00318
00319
00325 void fsaccess_free_nav_id(int fd)
00326 {
00327
00328 Clr_bits(pvNavUsed, (1 << fd));
00329 }
00330
00334 Bool b_fsaccess_init(void)
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 }
00349
00350
00354 void fsaccess_take_mutex(void)
00355 {
00356 #ifdef FREERTOS_USED
00357
00358 while( xSemaphoreTake( xFs_Access, portMAX_DELAY ) != pdTRUE );
00359 #endif
00360 }
00361
00365 void fsaccess_give_mutex(void)
00366 {
00367 #ifdef FREERTOS_USED
00368
00369 xSemaphoreGive( xFs_Access );
00370 #endif
00371 }
00372
00373
00381 S8 fsaccess_IsDirPresent( const char *pcStringDirName )
00382 {
00383 signed short TempoNavId;
00384 S8 RetVal;
00385
00386
00387 fsaccess_take_mutex();
00388
00389 TempoNavId = fsaccess_alloc_nav_id();
00390 if( -1 == TempoNavId )
00391 {
00392
00393 fsaccess_give_mutex();
00394 return( -1 );
00395 }
00396
00397
00398 nav_select( TempoNavId );
00399
00400
00401 RetVal = (S8)nav_setcwd( (FS_STRING)pcStringDirName, TRUE, FALSE );
00402
00403 fsaccess_free_nav_id( TempoNavId );
00404 fsaccess_give_mutex();
00405
00406 return( RetVal );
00407 }