]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-FAT-SL/fat_sl/common/f_lock.c
23ebbf20bffe1d33d52ae2b140e8b3b7e0406b73
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-FAT-SL / fat_sl / common / f_lock.c
1 /*\r
2  * FreeRTOS+FAT FS V1.0.0 (C) 2013 HCC Embedded\r
3  *\r
4  * The FreeRTOS+FAT SL license terms are different to the FreeRTOS license \r
5  * terms.\r
6  * \r
7  * FreeRTOS+FAT SL uses a dual license model that allows the software to be used\r
8  * under a pure GPL open source license (as opposed to the modified GPL licence\r
9  * under which FreeRTOS is distributed) or a commercial license.  Details of \r
10  * both license options follow:\r
11  * \r
12  * - Open source licensing -\r
13  * FreeRTOS+FAT SL is a free download and may be used, modified, evaluated and\r
14  * distributed without charge provided the user adheres to version two of the \r
15  * GNU General Public License (GPL) and does not remove the copyright notice or \r
16  * this text.  The GPL V2 text is available on the gnu.org web site, and on the\r
17  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.\r
18  * \r
19  * - Commercial licensing -\r
20  * Businesses and individuals who for commercial or other reasons cannot comply\r
21  * with the terms of the GPL V2 license must obtain a commercial license before \r
22  * incorporating FreeRTOS+FAT SL into proprietary software for distribution in \r
23  * any form.  Commercial licenses can be purchased from \r
24  * http://shop.freertos.org/fat_sl and do not require any source files to be \r
25  * changed.\r
26  *\r
27  * FreeRTOS+FAT SL is distributed in the hope that it will be useful.  You\r
28  * cannot use FreeRTOS+FAT SL unless you agree that you use the software 'as\r
29  * is'.  FreeRTOS+FAT SL is provided WITHOUT ANY WARRANTY; without even the\r
30  * implied warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A\r
31  * PARTICULAR PURPOSE. Real Time Engineers Ltd. and HCC Embedded disclaims all\r
32  * conditions and terms, be they implied, expressed, or statutory.\r
33  *\r
34  * http://www.FreeRTOS.org\r
35  * http://www.FreeRTOS.org/FreeRTOS-Plus\r
36  *\r
37  */\r
38 \r
39 #define FS_MUTEX_DEFINED\r
40 \r
41 #include "../../api/fat_sl.h"\r
42 #include "../../version/ver_fat_sl.h"\r
43 #if VER_FAT_SL_MAJOR != 3 || VER_FAT_SL_MINOR != 2\r
44  #error Incompatible FAT_SL version number!\r
45 #endif\r
46 \r
47 #if F_FS_THREAD_AWARE == 1\r
48 \r
49 xSemaphoreHandle fs_lock_semaphore;\r
50 \r
51 \r
52 /*\r
53 ** fr_findfirst\r
54 **\r
55 ** find first time a file using wildcards\r
56 **\r
57 ** INPUT : filename - name of the file\r
58 **         *find - pointer to a pre-define F_FIND structure\r
59 ** RETURN: F_NOERR - on success\r
60 **         F_ERR_NOTFOUND - if not found\r
61 */\r
62 unsigned char fr_findfirst ( const char * filename, F_FIND * find )\r
63 {\r
64   unsigned char  rc;\r
65 \r
66   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
67   {\r
68     rc = fn_findfirst( filename, find );\r
69     xSemaphoreGive( fs_lock_semaphore );\r
70   }\r
71   else\r
72   {\r
73     rc = F_ERR_OS;\r
74   }\r
75 \r
76   return rc;\r
77 }\r
78 \r
79 \r
80 /*\r
81 ** fr_findnext\r
82 **\r
83 ** find next time a file using wildcards\r
84 **\r
85 ** INPUT : *find - pointer to a pre-define F_FIND structure\r
86 ** RETURN: F_NOERR - on success\r
87 **         F_ERR_NOTFOUND - if not found\r
88 */\r
89 unsigned char fr_findnext ( F_FIND * find )\r
90 {\r
91   unsigned char  rc;\r
92 \r
93   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
94   {\r
95     rc = fn_findnext( find );\r
96     xSemaphoreGive( fs_lock_semaphore );\r
97   }\r
98   else\r
99   {\r
100     rc = F_ERR_OS;\r
101   }\r
102 \r
103   return rc;\r
104 }\r
105 \r
106 \r
107 /*\r
108 ** fr_filelength\r
109 **\r
110 ** Get the length of a file\r
111 **\r
112 ** INPUT : filename - name of the file\r
113 ** RETURN: size of the file or F_ERR_INVALID if not exists or volume not working\r
114 */\r
115 long fr_filelength ( const char * filename )\r
116 {\r
117   unsigned long  rc;\r
118 \r
119   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
120   {\r
121     rc = fn_filelength( filename );\r
122     xSemaphoreGive( fs_lock_semaphore );\r
123   }\r
124   else\r
125   {\r
126     rc = 0;\r
127   }\r
128 \r
129   return rc;\r
130 }\r
131 \r
132 \r
133 /*\r
134 ** fr_open\r
135 **\r
136 ** open a file\r
137 **\r
138 ** INPUT : filename - file to be opened\r
139 **         mode - open method (r,w,a,r+,w+,a+)\r
140 ** RETURN: pointer to a file descriptor or 0 on error\r
141 */\r
142 F_FILE * fr_open ( const char * filename, const char * mode )\r
143 {\r
144   F_FILE * rc;\r
145 \r
146   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
147   {\r
148     rc = fn_open( filename, mode );\r
149     xSemaphoreGive( fs_lock_semaphore );\r
150   }\r
151   else\r
152   {\r
153     rc = NULL;\r
154   }\r
155 \r
156   return rc;\r
157 }\r
158 \r
159 \r
160 /*\r
161 ** fr_close\r
162 **\r
163 ** Close a previously opened file.\r
164 **\r
165 ** INPUT : *filehandle - pointer to the file descriptor\r
166 ** RETURN: F_NOERR on success, other if error\r
167 */\r
168 unsigned char fr_close ( F_FILE * filehandle )\r
169 {\r
170   unsigned char  rc;\r
171 \r
172   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
173   {\r
174     rc = fn_close( filehandle );\r
175     xSemaphoreGive( fs_lock_semaphore );\r
176   }\r
177   else\r
178   {\r
179     rc = F_ERR_OS;\r
180   }\r
181 \r
182   return rc;\r
183 }\r
184 \r
185 \r
186 /*\r
187 ** fr_read\r
188 **\r
189 ** Read from a file.\r
190 **\r
191 ** INPUT : buf - buffer to read data\r
192 **         size - number of unique\r
193 **         size_st - size of unique\r
194 **         *filehandle - pointer to file descriptor\r
195 ** OUTPUT: number of read bytes\r
196 */\r
197 long fr_read ( void * bbuf, long size, long size_st, F_FILE * filehandle )\r
198 {\r
199   long  rc;\r
200 \r
201   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
202   {\r
203     rc = fn_read( bbuf, size, size_st, filehandle );\r
204     xSemaphoreGive( fs_lock_semaphore );\r
205   }\r
206   else\r
207   {\r
208     rc = 0;\r
209   }\r
210 \r
211   return rc;\r
212 }\r
213 \r
214 \r
215 /*\r
216 ** fr_write\r
217 **\r
218 ** INPUT : bbuf - buffer to write from\r
219 **         size - number of unique\r
220 **         size_st - size of unique\r
221 **         *filehandle - pointer to the file descriptor\r
222 ** RETURN: number of written bytes\r
223 */\r
224 long fr_write ( const void * bbuf, long size, long size_st, F_FILE * filehandle )\r
225 {\r
226   long  rc;\r
227 \r
228   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
229   {\r
230     rc = fn_write( bbuf, size, size_st, filehandle );\r
231     xSemaphoreGive( fs_lock_semaphore );\r
232   }\r
233   else\r
234   {\r
235     rc = 0;\r
236   }\r
237 \r
238   return rc;\r
239 }\r
240 \r
241 /*\r
242 ** fr_seek\r
243 **\r
244 ** Seek in a file.\r
245 **\r
246 ** INPUT : *filehandle - pointer to a file descriptor\r
247 **         offset - offset\r
248 **         whence - F_SEEK_SET: position = offset\r
249 **                  F_SEEK_CUR: position = position + offset\r
250 **                  F_SEEK_END: position = end of file (offset=0)\r
251 ** RETURN: F_NOERR on succes, other if error.\r
252 */\r
253 unsigned char fr_seek ( F_FILE * filehandle, long offset, unsigned char whence )\r
254 {\r
255   unsigned char  rc;\r
256 \r
257   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
258   {\r
259     rc = fn_seek( filehandle, offset, whence );\r
260     xSemaphoreGive( fs_lock_semaphore );\r
261   }\r
262   else\r
263   {\r
264     rc = F_ERR_OS;\r
265   }\r
266 \r
267   return rc;\r
268 }\r
269 \r
270 /*\r
271 ** fr_tell\r
272 **\r
273 ** get current position in the file\r
274 **\r
275 ** INPUT : *filehandle - pointer to a file descriptor\r
276 ** RETURN: -1 on error or current position.\r
277 */\r
278 long fr_tell ( F_FILE * filehandle )\r
279 {\r
280   long  rc;\r
281 \r
282   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
283   {\r
284     rc = fn_tell( filehandle );\r
285     xSemaphoreGive( fs_lock_semaphore );\r
286   }\r
287   else\r
288   {\r
289     rc = -1;\r
290   }\r
291 \r
292   return rc;\r
293 }\r
294 \r
295 /*\r
296 ** fr_getc\r
297 **\r
298 ** read one byte from a file\r
299 **\r
300 ** INPUT : *filehandle - pointer to a file descriptor\r
301 ** RETURN: -1 if error, otherwise the read character.\r
302 */\r
303 int fr_getc ( F_FILE * filehandle )\r
304 {\r
305   int  rc;\r
306 \r
307   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
308   {\r
309     rc = fn_getc( filehandle );\r
310     xSemaphoreGive( fs_lock_semaphore );\r
311   }\r
312   else\r
313   {\r
314     rc = -1;\r
315   }\r
316 \r
317   return rc;\r
318 }\r
319 \r
320 /*\r
321 ** fr_putc\r
322 **\r
323 ** write one byte to a file\r
324 **\r
325 ** INPUT : ch - character to write\r
326 **         *filehandle - pointer to a file handler\r
327 ** RETURN: ch on success, -1 on error\r
328 */\r
329 int fr_putc ( int ch, F_FILE * filehandle )\r
330 {\r
331   int  rc;\r
332 \r
333   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
334   {\r
335     rc = fn_putc( ch, filehandle );\r
336     xSemaphoreGive( fs_lock_semaphore );\r
337   }\r
338   else\r
339   {\r
340     rc = -1;\r
341   }\r
342 \r
343   return rc;\r
344 }\r
345 \r
346 /*\r
347 ** fr_rewind\r
348 **\r
349 ** set current position in the file to the beginning\r
350 **\r
351 ** INPUT : *filehandle - pointer to a file descriptor\r
352 ** RETURN: F_NOERR on succes, other if error.\r
353 */\r
354 unsigned char fr_rewind ( F_FILE * filehandle )\r
355 {\r
356   unsigned char rc;\r
357 \r
358   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
359   {\r
360     rc = fn_rewind( filehandle );\r
361     xSemaphoreGive( fs_lock_semaphore );\r
362   }\r
363   else\r
364   {\r
365     rc = F_ERR_OS;\r
366   }\r
367 \r
368   return rc;\r
369 }\r
370 \r
371 /*\r
372 ** fr_eof\r
373 **\r
374 ** check if current position is at the end of the file.\r
375 **\r
376 ** INPUT : *filehandle - pointer to a file descriptor\r
377 ** RETURN: F_ERR_EOF - at the end of the file\r
378 **         F_NOERR - no error, end of the file not reached\r
379 **         other - on error\r
380 */\r
381 unsigned char fr_eof ( F_FILE * filehandle )\r
382 {\r
383   unsigned char  rc;\r
384 \r
385   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
386   {\r
387     rc = fn_eof( filehandle );\r
388     xSemaphoreGive( fs_lock_semaphore );\r
389   }\r
390   else\r
391   {\r
392     rc = F_ERR_OS;\r
393   }\r
394 \r
395   return rc;\r
396 }\r
397 \r
398 /*\r
399 ** Format the device\r
400 */\r
401 unsigned char fr_hardformat ( unsigned char fattype )\r
402 {\r
403   unsigned char  rc;\r
404 \r
405   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
406   {\r
407     rc = fn_hardformat( fattype );\r
408     xSemaphoreGive( fs_lock_semaphore );\r
409   }\r
410   else\r
411   {\r
412     rc = F_ERR_OS;\r
413   }\r
414 \r
415   return rc;\r
416 }\r
417 \r
418 /*\r
419 ** fr_get_serial\r
420 **\r
421 ** Get serial number\r
422 **\r
423 ** OUTPUT: serial - where to write the serial number\r
424 ** RETURN: error code\r
425 */\r
426 unsigned char fr_getserial ( unsigned long * serial )\r
427 {\r
428   unsigned char  rc;\r
429 \r
430   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
431   {\r
432     rc = fn_getserial( serial );\r
433     xSemaphoreGive( fs_lock_semaphore );\r
434   }\r
435   else\r
436   {\r
437     rc = F_ERR_OS;\r
438   }\r
439 \r
440   return rc;\r
441 }\r
442 \r
443 \r
444 /*\r
445 ** fr_delete\r
446 **\r
447 ** Delete a file. Removes the chain that belongs to the file and inserts a new descriptor\r
448 ** to the directory with first_cluster set to 0.\r
449 **\r
450 ** INPUT : filename - name of the file to delete\r
451 ** RETURN: F_NOERR on success, other if error.\r
452 */\r
453 unsigned char fr_delete ( const char * filename )\r
454 {\r
455   unsigned char  rc;\r
456 \r
457   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
458   {\r
459     rc = fn_delete( filename );\r
460     xSemaphoreGive( fs_lock_semaphore );\r
461   }\r
462   else\r
463   {\r
464     rc = F_ERR_OS;\r
465   }\r
466 \r
467   return rc;\r
468 }\r
469 \r
470 /*\r
471 ** fr_truncate\r
472 **\r
473 ** Open a file and set end of file\r
474 **\r
475 ** INPUT:       filename - name of the file\r
476 **              filesize - required new size\r
477 ** RETURN:      NULL on error, otherwise file pointer\r
478 */\r
479 F_FILE * fr_truncate ( const char * filename, long filesize )\r
480 {\r
481   F_FILE * f;\r
482 \r
483   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
484   {\r
485     f = fn_truncate( filename, filesize );\r
486     xSemaphoreGive( fs_lock_semaphore );\r
487   }\r
488   else\r
489   {\r
490     f = NULL;\r
491   }\r
492 \r
493   return f;\r
494 }\r
495 \r
496 \r
497 /*\r
498 ** fr_getfreespace\r
499 **\r
500 ** Get free space on the volume\r
501 **\r
502 ** OUTPUT: *sp - pre-defined F_SPACE structure, where information will be stored\r
503 ** RETURN: F_NOERR - on success\r
504 **         F_ERR_NOTFORMATTED - if volume is not formatted\r
505 */\r
506 unsigned char fr_getfreespace ( F_SPACE * sp )\r
507 {\r
508   unsigned char  rc;\r
509 \r
510   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
511   {\r
512     rc = fn_getfreespace( sp );\r
513     xSemaphoreGive( fs_lock_semaphore );\r
514   }\r
515   else\r
516   {\r
517     rc = F_ERR_OS;\r
518   }\r
519 \r
520   return rc;\r
521 }\r
522 \r
523 \r
524 /*\r
525 ** fr_chdir\r
526 **\r
527 ** Change to a directory\r
528 **\r
529 ** INPUT:  path - path to the dircetory\r
530 ** RETURN: 0 - on success, other if error\r
531 */\r
532 unsigned char fr_chdir ( const char * path )\r
533 {\r
534   unsigned char  rc;\r
535 \r
536   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
537   {\r
538     rc = fn_chdir( path );\r
539     xSemaphoreGive( fs_lock_semaphore );\r
540   }\r
541   else\r
542   {\r
543     rc = F_ERR_OS;\r
544   }\r
545 \r
546   return rc;\r
547 }\r
548 \r
549 \r
550 /*\r
551 ** fr_mkdir\r
552 **\r
553 ** Create a directory\r
554 **\r
555 ** INPUT:  path - new directory path\r
556 ** RETURN: 0 - on success, other if error\r
557 */\r
558 unsigned char fr_mkdir ( const char * path )\r
559 {\r
560   unsigned char  rc;\r
561 \r
562   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
563   {\r
564     rc = fn_mkdir( path );\r
565     xSemaphoreGive( fs_lock_semaphore );\r
566   }\r
567   else\r
568   {\r
569     rc = F_ERR_OS;\r
570   }\r
571 \r
572   return rc;\r
573 }\r
574 \r
575 \r
576 /*\r
577 ** fr_rmdir\r
578 **\r
579 ** Removes a directory\r
580 **\r
581 ** INPUT:  path - path to remove\r
582 ** RETURN: 0 - on success, other if error\r
583 */\r
584 unsigned char fr_rmdir ( const char * path )\r
585 {\r
586   unsigned char  rc;\r
587 \r
588   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
589   {\r
590     rc = fn_rmdir( path );\r
591     xSemaphoreGive( fs_lock_semaphore );\r
592   }\r
593   else\r
594   {\r
595     rc = F_ERR_OS;\r
596   }\r
597 \r
598   return rc;\r
599 }\r
600 \r
601 \r
602 /*\r
603 ** fr_getcwd\r
604 **\r
605 ** Get current working directory\r
606 **\r
607 ** INPUT:  maxlen - maximum length allowed\r
608 ** OUTPUT: path - current working directory\r
609 ** RETURN: 0 - on success, other if error\r
610 */\r
611 unsigned char fr_getcwd ( char * path, unsigned char maxlen, char root )\r
612 {\r
613   unsigned char  rc;\r
614 \r
615   if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )\r
616   {\r
617     rc = fn_getcwd( path, maxlen, root );\r
618     xSemaphoreGive( fs_lock_semaphore );\r
619   }\r
620   else\r
621   {\r
622     rc = F_ERR_OS;\r
623   }\r
624 \r
625   return rc;\r
626 }\r
627 \r
628 \r
629 /*\r
630 ** fr_init\r
631 **\r
632 ** Initialize FAT_SL OS module\r
633 **\r
634 ** RETURN: F_NO_ERROR or F_ERR_OS\r
635 */\r
636 unsigned char fr_init ( void )\r
637 {\r
638   return F_NO_ERROR;\r
639 }\r
640 \r
641 #endif /* F_FS_THREAD_AWARE */\r
642 \r