version 2.8
gfile_fs_chibios.c
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.org/license.html
6  */
7 
8 /********************************************************
9  * The ChibiOS FileStream file-system
10  ********************************************************/
11 
12 #include "../../gfx.h"
13 
14 #if GFX_USE_GFILE && GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
15 
16 #include "gfile_fs.h"
17 
18 static void ChibiOSBFSClose(GFILE *f);
19 static int ChibiOSBFSRead(GFILE *f, void *buf, int size);
20 static int ChibiOSBFSWrite(GFILE *f, const void *buf, int size);
21 static bool_t ChibiOSBFSSetpos(GFILE *f, long int pos);
22 static long int ChibiOSBFSGetsize(GFILE *f);
23 static bool_t ChibiOSBFSEof(GFILE *f);
24 
25 static const GFILEVMT FsCHIBIOSVMT = {
26  GFSFLG_SEEKABLE|GFSFLG_WRITEABLE, // flags
27  0, // prefix
28  0, 0, 0, 0,
29  0, ChibiOSBFSClose, ChibiOSBFSRead, ChibiOSBFSWrite,
30  ChibiOSBFSSetpos, ChibiOSBFSGetsize, ChibiOSBFSEof,
31  0, 0, 0,
32  #if GFILE_NEED_FILELISTS
33  0, 0, 0,
34  #endif
35 };
36 
37 #if CH_KERNEL_MAJOR == 2
38  #define FileStream BaseFileStream
39  #define fileStreamClose chFileStreamClose
40  #define fileStreamRead chSequentialStreamRead
41  #define fileStreamWrite chSequentialStreamWrite
42  #define fileStreamSeek chFileStreamSeek
43  #define fileStreamGetSize chFileStreamGetSize
44 #endif
45 
46 static void ChibiOSBFSClose(GFILE *f) {
47  fileStreamClose(((FileStream *)f->obj));
48 }
49 static int ChibiOSBFSRead(GFILE *f, void *buf, int size) {
50  return fileStreamRead(((FileStream *)f->obj), (uint8_t *)buf, size);
51 }
52 static int ChibiOSBFSWrite(GFILE *f, const void *buf, int size) {
53  return fileStreamWrite(((FileStream *)f->obj), (uint8_t *)buf, size);
54 }
55 static bool_t ChibiOSBFSSetpos(GFILE *f, long int pos) {
56  fileStreamSeek(((FileStream *)f->obj), pos);
57  return TRUE;
58 }
59 static long int ChibiOSBFSGetsize(GFILE *f) { return fileStreamGetSize(((FileStream *)f->obj)); }
60 static bool_t ChibiOSBFSEof(GFILE *f) { return f->pos >= fileStreamGetSize(((FileStream *)f->obj)); }
61 
62 GFILE * gfileOpenChibiOSFileStream(void *FileStreamPtr, const char *mode) {
63  GFILE * f;
64 
65  // Get an empty file and set the flags
66  if (!(f = _gfileFindSlot(mode)))
67  return 0;
68 
69  // File is open - fill in all the details
70  f->vmt = &FsCHIBIOSVMT;
71  f->obj = FileStreamPtr;
72  f->pos = 0;
73  f->flags |= GFILEFLG_OPEN|GFILEFLG_CANSEEK;
74  return f;
75 }
76 
77 #endif //GFX_USE_GFILE && GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
GFILE * gfileOpenChibiOSFileStream(void *FileStreamPtr, const char *mode)
Open file from a ChibiOS FileStream.
struct GFILE GFILE
A file pointer.
Definition: gfile.h:34
GFILE file system header.
#define TRUE
Generic 'true' boolean constant.
Definition: gfx.h:38