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