ISLogFileFatFs.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include "drivers/d_time.h"
14 
15 #include "ISLogFileFatFs.h"
16 #include "ISConstants.h"
17 
19 {
20 }
21 
22 cISLogFileFatFs::cISLogFileFatFs(const std::string& filePath, const char* mode) : cISLogFileFatFs(filePath.c_str(), mode)
23 {
24 
25 }
26 
27 cISLogFileFatFs::cISLogFileFatFs(const char* filePath, const char* mode) : cISLogFileFatFs()
28 {
29  open(filePath, mode);
30 }
31 
33 {
34  close();
35 }
36 
37 bool cISLogFileFatFs::open(const char* filePath, const char* mode)
38 {
39  CONST_EXPRESSION std::size_t MAX_MODE_LEN = 3;
40  CONST_EXPRESSION char DRIVE[] = "0:";
41  BYTE flags = 0x00;
42  bool append = false;
43  char fullFilePath[256];
44  strncpy(fullFilePath, DRIVE, sizeof(fullFilePath));
45  strncat(fullFilePath, filePath, _MIN(strlen(fullFilePath), sizeof(fullFilePath)));
46 
47  if (strncmp(mode, "r", MAX_MODE_LEN) == 0)
48  {
49  flags = FA_READ;
50  }
51  else if (strncmp(mode, "r+", MAX_MODE_LEN) == 0)
52  {
53  flags = FA_READ | FA_WRITE;
54  }
55  else if (strncmp(mode, "w", MAX_MODE_LEN) == 0 ||
56  strncmp(mode, "wb", MAX_MODE_LEN) == 0)
57  {
58  flags = FA_CREATE_ALWAYS | FA_WRITE;
59  }
60  else if (strncmp(mode, "w+", MAX_MODE_LEN) == 0 ||
61  (strncmp(mode, "wb+", MAX_MODE_LEN) == 0))
62  {
63  flags = FA_CREATE_ALWAYS | FA_WRITE | FA_READ;
64  }
65  else if (strncmp(mode, "a", MAX_MODE_LEN) == 0)
66  {
67  // FatFs 0.09 does not support append mode.
68  // If we upgrade, we can use instead of setting n append flag and seeking.
69  //flags = FA_OPEN_APPEND | FA_WRITE;
70 
71  flags = FA_WRITE;
72  append = true;
73  }
74  else if (strncmp(mode, "a+", MAX_MODE_LEN) == 0)
75  {
76  // FatFs 0.09 does not support append mode.
77  // If we upgrade, we can use instead of setting n append flag and seeking.
78  //flags = FA_OPEN_APPEND | FA_WRITE | FA_READ;
79 
80  flags = FA_WRITE | FA_READ;
81  append = true;
82  }
83  else if (strncmp(mode, "wx", MAX_MODE_LEN) == 0)
84  {
85  flags = FA_CREATE_NEW | FA_WRITE;
86  }
87  else if (strncmp(mode, "w+x", MAX_MODE_LEN) == 0)
88  {
89  flags = FA_CREATE_NEW | FA_WRITE | FA_READ;
90  }
91  else
92  {
93  while (1) {} // not supported, fix this
94  }
95 
96  FRESULT result = f_open(&m_file, filePath, flags);
97 
98  if (result != FR_OK)
99  {
100  return false;
101  }
102 
103  if (append)
104  {
105  /* Move to end of the file to append data */
106  result = f_lseek(&m_file, f_size(&m_file));
107  if (result != FR_OK)
108  {
109  close();
110  return false;
111  }
112  }
113 
114  m_opened = true;
115  m_lastSync = time_msec();
116  return true;
117 }
118 
120 {
121  if (m_opened)
122  {
123  FRESULT result = f_close(&m_file);
124  if (result != FR_OK)
125  {
126  return false;
127  }
128  m_opened = false;
129  }
130  return true;
131 }
132 
134 {
135  bool result = (f_sync(&m_file) == FR_OK);
136  if (result)
137  {
138  m_lastSync = time_msec();
139  }
140  return result;
141 }
142 
144 {
145  return (f_error(&m_file) == FR_OK);
146 }
147 
149 {
150  return m_opened;
151 }
152 
154 {
155  if (f_putc(ch, &m_file) == 1)
156  {
157  return static_cast<int>(ch);
158  }
159  return EOF;
160 }
161 
162 int cISLogFileFatFs::puts(const char* str)
163 {
164  return f_puts(str, &m_file);
165 }
166 
167 std::size_t cISLogFileFatFs::write(const void* bytes, std::size_t len)
168 {
169  std::size_t bytes_written = 0;
170  f_write(&m_file, bytes, len, &bytes_written);
171 
172  // Indicate write to disk
173  extern int g_indicateFsWriteMs;
174  g_indicateFsWriteMs = 50;
175 
177  {
178  flush();
179  }
180 
181 
182  return bytes_written;
183 }
184 
185 int cISLogFileFatFs::lprintf(const char* format, ...)
186 {
187  int result;
188  va_list args;
189  va_start(args, format);
190  result = vprintf(format, args);
191  va_end(args);
192  return result;
193 }
194 
195 int cISLogFileFatFs::vprintf(const char* format, va_list args)
196 {
197  return f_vprintf(&m_file, format, args);
198 }
199 
201 {
202  char buffer[1];
203  CONST_EXPRESSION UINT bytesToRead = sizeof(buffer);
204  UINT bytesRead;
205 
206  if (f_read(&m_file, buffer, bytesToRead, &bytesRead) == FR_OK && bytesRead == bytesToRead)
207  {
208  return static_cast<int>(buffer[0]);
209  }
210  return EOF;
211 }
212 
213 std::size_t cISLogFileFatFs::read(void* bytes, std::size_t len)
214 {
215  std::size_t bytes_read = 0;
216  f_read(&m_file, bytes, len, &bytes_read);
217  return bytes_read;
218 }
#define _MIN(a, b)
Definition: ISConstants.h:298
uint32_t time_msec(void)
Definition: d_time.c:95
FRESULT f_write(FIL *fp, const void *buff, UINT btw, UINT *bw)
Definition: ff.c:2482
unsigned char BYTE
FRESULT f_sync(FIL *fp)
Definition: ff.c:2601
#define FA_CREATE_NEW
Definition: ff.h:288
#define f_error(fp)
Definition: ff.h:236
bool good() OVERRIDE
bool open(const char *filePath, const char *mode) OVERRIDE
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
Definition: ff.c:2381
int g_indicateFsWriteMs
#define FA_WRITE
Definition: ff.h:287
#define FA_READ
Definition: ff.h:282
int f_putc(TCHAR, FIL *)
std::uint32_t m_lastSync
#define FA_CREATE_ALWAYS
Definition: ff.h:289
#define CONST_EXPRESSION
Definition: ISConstants.h:411
int f_vprintf(FIL *, const TCHAR *, va_list arp)
int putch(char ch) OVERRIDE
std::size_t read(void *bytes, std::size_t len) OVERRIDE
FRESULT f_lseek(FIL *fp, DWORD ofs)
Definition: ff.c:2812
bool isOpened() OVERRIDE
std::size_t write(const void *bytes, std::size_t len) OVERRIDE
FRESULT
Definition: ff.h:178
int f_puts(const TCHAR *, FIL *)
CONST_EXPRESSION std::uint32_t SYNC_FREQ_MS
unsigned int UINT
Definition: integer.h:17
#define EOF
Definition: ff.h:241
#define f_size(fp)
Definition: ff.h:238
int puts(const char *str) OVERRIDE
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Definition: ff.c:2251
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
bool flush() OVERRIDE
int vprintf(const char *format, va_list args) OVERRIDE
int lprintf(const char *format,...) OVERRIDE
bool close() OVERRIDE
Definition: ff.h:179
int getch() OVERRIDE
FRESULT f_close(FIL *fp)
Definition: ff.c:2648


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04