SdBaseFile.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * \file
  24. * \brief SdBaseFile class
  25. */
  26. /**
  27. * Arduino SdFat Library
  28. * Copyright (C) 2009 by William Greiman
  29. *
  30. * This file is part of the Arduino Sd2Card Library
  31. */
  32. #ifndef _SDBASEFILE_H_
  33. #define _SDBASEFILE_H_
  34. #include "SdFatConfig.h"
  35. #include "SdVolume.h"
  36. #include <stdint.h>
  37. /**
  38. * \struct filepos_t
  39. * \brief internal type for istream
  40. * do not use in user apps
  41. */
  42. struct filepos_t {
  43. uint32_t position; // stream byte position
  44. uint32_t cluster; // cluster of position
  45. filepos_t() : position(0), cluster(0) {}
  46. };
  47. // use the gnu style oflag in open()
  48. uint8_t const O_READ = 0x01, // open() oflag for reading
  49. O_RDONLY = O_READ, // open() oflag - same as O_IN
  50. O_WRITE = 0x02, // open() oflag for write
  51. O_WRONLY = O_WRITE, // open() oflag - same as O_WRITE
  52. O_RDWR = (O_READ | O_WRITE), // open() oflag for reading and writing
  53. O_ACCMODE = (O_READ | O_WRITE), // open() oflag mask for access modes
  54. O_APPEND = 0x04, // The file offset shall be set to the end of the file prior to each write.
  55. O_SYNC = 0x08, // Synchronous writes - call sync() after each write
  56. O_TRUNC = 0x10, // Truncate the file to zero length
  57. O_AT_END = 0x20, // Set the initial position at the end of the file
  58. O_CREAT = 0x40, // Create the file if nonexistent
  59. O_EXCL = 0x80; // If O_CREAT and O_EXCL are set, open() shall fail if the file exists
  60. // SdBaseFile class static and const definitions
  61. // flags for ls()
  62. uint8_t const LS_DATE = 1, // ls() flag to print modify date
  63. LS_SIZE = 2, // ls() flag to print file size
  64. LS_R = 4; // ls() flag for recursive list of subdirectories
  65. // flags for timestamp
  66. uint8_t const T_ACCESS = 1, // Set the file's last access date
  67. T_CREATE = 2, // Set the file's creation date and time
  68. T_WRITE = 4; // Set the file's write date and time
  69. // values for type_
  70. uint8_t const FAT_FILE_TYPE_CLOSED = 0, // This file has not been opened.
  71. FAT_FILE_TYPE_NORMAL = 1, // A normal file
  72. FAT_FILE_TYPE_ROOT_FIXED = 2, // A FAT12 or FAT16 root directory
  73. FAT_FILE_TYPE_ROOT32 = 3, // A FAT32 root directory
  74. FAT_FILE_TYPE_SUBDIR = 4, // A subdirectory file
  75. FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED; // Test value for directory type
  76. /**
  77. * date field for FAT directory entry
  78. * \param[in] year [1980,2107]
  79. * \param[in] month [1,12]
  80. * \param[in] day [1,31]
  81. *
  82. * \return Packed date for dir_t entry.
  83. */
  84. static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) { return (year - 1980) << 9 | month << 5 | day; }
  85. /**
  86. * year part of FAT directory date field
  87. * \param[in] fatDate Date in packed dir format.
  88. *
  89. * \return Extracted year [1980,2107]
  90. */
  91. static inline uint16_t FAT_YEAR(uint16_t fatDate) { return 1980 + (fatDate >> 9); }
  92. /**
  93. * month part of FAT directory date field
  94. * \param[in] fatDate Date in packed dir format.
  95. *
  96. * \return Extracted month [1,12]
  97. */
  98. static inline uint8_t FAT_MONTH(uint16_t fatDate) { return (fatDate >> 5) & 0XF; }
  99. /**
  100. * day part of FAT directory date field
  101. * \param[in] fatDate Date in packed dir format.
  102. *
  103. * \return Extracted day [1,31]
  104. */
  105. static inline uint8_t FAT_DAY(uint16_t fatDate) { return fatDate & 0x1F; }
  106. /**
  107. * time field for FAT directory entry
  108. * \param[in] hour [0,23]
  109. * \param[in] minute [0,59]
  110. * \param[in] second [0,59]
  111. *
  112. * \return Packed time for dir_t entry.
  113. */
  114. static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) { return hour << 11 | minute << 5 | second >> 1; }
  115. /**
  116. * hour part of FAT directory time field
  117. * \param[in] fatTime Time in packed dir format.
  118. *
  119. * \return Extracted hour [0,23]
  120. */
  121. static inline uint8_t FAT_HOUR(uint16_t fatTime) { return fatTime >> 11; }
  122. /**
  123. * minute part of FAT directory time field
  124. * \param[in] fatTime Time in packed dir format.
  125. *
  126. * \return Extracted minute [0,59]
  127. */
  128. static inline uint8_t FAT_MINUTE(uint16_t fatTime) { return (fatTime >> 5) & 0x3F; }
  129. /**
  130. * second part of FAT directory time field
  131. * Note second/2 is stored in packed time.
  132. *
  133. * \param[in] fatTime Time in packed dir format.
  134. *
  135. * \return Extracted second [0,58]
  136. */
  137. static inline uint8_t FAT_SECOND(uint16_t fatTime) { return 2 * (fatTime & 0x1F); }
  138. // Default date for file timestamps is 1 Jan 2000
  139. uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
  140. // Default time for file timestamp is 1 am
  141. uint16_t const FAT_DEFAULT_TIME = (1 << 11);
  142. /**
  143. * \class SdBaseFile
  144. * \brief Base class for SdFile with Print and C++ streams.
  145. */
  146. class SdBaseFile {
  147. public:
  148. SdBaseFile() : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {}
  149. SdBaseFile(const char* path, uint8_t oflag);
  150. ~SdBaseFile() { if (isOpen()) close(); }
  151. /**
  152. * writeError is set to true if an error occurs during a write().
  153. * Set writeError to false before calling print() and/or write() and check
  154. * for true after calls to print() and/or write().
  155. */
  156. bool writeError;
  157. // helpers for stream classes
  158. /**
  159. * get position for streams
  160. * \param[out] pos struct to receive position
  161. */
  162. void getpos(filepos_t* pos);
  163. /**
  164. * set position for streams
  165. * \param[out] pos struct with value for new position
  166. */
  167. void setpos(filepos_t* pos);
  168. bool close();
  169. bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
  170. bool createContiguous(SdBaseFile* dirFile,
  171. const char* path, uint32_t size);
  172. /**
  173. * \return The current cluster number for a file or directory.
  174. */
  175. uint32_t curCluster() const { return curCluster_; }
  176. /**
  177. * \return The current position for a file or directory.
  178. */
  179. uint32_t curPosition() const { return curPosition_; }
  180. /**
  181. * \return Current working directory
  182. */
  183. static SdBaseFile* cwd() { return cwd_; }
  184. /**
  185. * Set the date/time callback function
  186. *
  187. * \param[in] dateTime The user's call back function. The callback
  188. * function is of the form:
  189. *
  190. * \code
  191. * void dateTime(uint16_t* date, uint16_t* time) {
  192. * uint16_t year;
  193. * uint8_t month, day, hour, minute, second;
  194. *
  195. * // User gets date and time from GPS or real-time clock here
  196. *
  197. * // return date using FAT_DATE macro to format fields
  198. * *date = FAT_DATE(year, month, day);
  199. *
  200. * // return time using FAT_TIME macro to format fields
  201. * *time = FAT_TIME(hour, minute, second);
  202. * }
  203. * \endcode
  204. *
  205. * Sets the function that is called when a file is created or when
  206. * a file's directory entry is modified by sync(). All timestamps,
  207. * access, creation, and modify, are set when a file is created.
  208. * sync() maintains the last access date and last modify date/time.
  209. *
  210. * See the timestamp() function.
  211. */
  212. static void dateTimeCallback(
  213. void (*dateTime)(uint16_t* date, uint16_t* time)) {
  214. dateTime_ = dateTime;
  215. }
  216. /**
  217. * Cancel the date/time callback function.
  218. */
  219. static void dateTimeCallbackCancel() { dateTime_ = 0; }
  220. bool dirEntry(dir_t* dir);
  221. static void dirName(const dir_t& dir, char* name);
  222. bool exists(const char* name);
  223. int16_t fgets(char* str, int16_t num, char* delim = 0);
  224. /**
  225. * \return The total number of bytes in a file or directory.
  226. */
  227. uint32_t fileSize() const { return fileSize_; }
  228. /**
  229. * \return The first cluster number for a file or directory.
  230. */
  231. uint32_t firstCluster() const { return firstCluster_; }
  232. /**
  233. * \return True if this is a directory else false.
  234. */
  235. bool isDir() const { return type_ >= FAT_FILE_TYPE_MIN_DIR; }
  236. /**
  237. * \return True if this is a normal file else false.
  238. */
  239. bool isFile() const { return type_ == FAT_FILE_TYPE_NORMAL; }
  240. /**
  241. * \return True if this is an open file/directory else false.
  242. */
  243. bool isOpen() const { return type_ != FAT_FILE_TYPE_CLOSED; }
  244. /**
  245. * \return True if this is a subdirectory else false.
  246. */
  247. bool isSubDir() const { return type_ == FAT_FILE_TYPE_SUBDIR; }
  248. /**
  249. * \return True if this is the root directory.
  250. */
  251. bool isRoot() const { return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32; }
  252. bool getFilename(char * const name);
  253. void ls(uint8_t flags = 0, uint8_t indent = 0);
  254. bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
  255. bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
  256. bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
  257. bool open(const char* path, uint8_t oflag = O_READ);
  258. bool openNext(SdBaseFile* dirFile, uint8_t oflag);
  259. bool openRoot(SdVolume* vol);
  260. int peek();
  261. static void printFatDate(uint16_t fatDate);
  262. static void printFatTime(uint16_t fatTime);
  263. bool printName();
  264. int16_t read();
  265. int16_t read(void* buf, uint16_t nbyte);
  266. int8_t readDir(dir_t* dir, char* longFilename);
  267. static bool remove(SdBaseFile* dirFile, const char* path);
  268. bool remove();
  269. /**
  270. * Set the file's current position to zero.
  271. */
  272. void rewind() { seekSet(0); }
  273. bool rename(SdBaseFile* dirFile, const char* newPath);
  274. bool rmdir();
  275. bool rmRfStar();
  276. /**
  277. * Set the files position to current position + \a pos. See seekSet().
  278. * \param[in] offset The new position in bytes from the current position.
  279. * \return true for success or false for failure.
  280. */
  281. bool seekCur(const int32_t offset) { return seekSet(curPosition_ + offset); }
  282. /**
  283. * Set the files position to end-of-file + \a offset. See seekSet().
  284. * \param[in] offset The new position in bytes from end-of-file.
  285. * \return true for success or false for failure.
  286. */
  287. bool seekEnd(const int32_t offset = 0) { return seekSet(fileSize_ + offset); }
  288. bool seekSet(const uint32_t pos);
  289. bool sync();
  290. bool timestamp(SdBaseFile* file);
  291. bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
  292. uint8_t hour, uint8_t minute, uint8_t second);
  293. /**
  294. * Type of file. Use isFile() or isDir() instead of type() if possible.
  295. *
  296. * \return The file or directory type.
  297. */
  298. uint8_t type() const { return type_; }
  299. bool truncate(uint32_t size);
  300. /**
  301. * \return SdVolume that contains this file.
  302. */
  303. SdVolume* volume() const { return vol_; }
  304. int16_t write(const void* buf, uint16_t nbyte);
  305. private:
  306. friend class SdFat; // allow SdFat to set cwd_
  307. static SdBaseFile* cwd_; // global pointer to cwd dir
  308. // data time callback function
  309. static void (*dateTime_)(uint16_t* date, uint16_t* time);
  310. // bits defined in flags_
  311. static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC), // should be 0x0F
  312. F_FILE_DIR_DIRTY = 0x80; // sync of directory entry required
  313. // private data
  314. uint8_t flags_; // See above for definition of flags_ bits
  315. uint8_t fstate_; // error and eof indicator
  316. uint8_t type_; // type of file see above for values
  317. uint32_t curCluster_; // cluster for current file position
  318. uint32_t curPosition_; // current file position in bytes from beginning
  319. uint32_t dirBlock_; // block for this files directory entry
  320. uint8_t dirIndex_; // index of directory entry in dirBlock
  321. uint32_t fileSize_; // file size in bytes
  322. uint32_t firstCluster_; // first cluster of file
  323. SdVolume* vol_; // volume where file is located
  324. /**
  325. * EXPERIMENTAL - Don't use!
  326. */
  327. //bool openParent(SdBaseFile* dir);
  328. // private functions
  329. bool addCluster();
  330. bool addDirCluster();
  331. dir_t* cacheDirEntry(uint8_t action);
  332. int8_t lsPrintNext(uint8_t flags, uint8_t indent);
  333. static bool make83Name(const char* str, uint8_t* name, const char** ptr);
  334. bool mkdir(SdBaseFile* parent, const uint8_t dname[11]);
  335. bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
  336. bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
  337. dir_t* readDirCache();
  338. };
  339. #endif // _SDBASEFILE_H_