You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.1KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // FatFS wrappers.
  19. #include "avrlibx/filesystem/file.h"
  20. namespace avrlibx {
  21. File::File()
  22. : opened_(0) {
  23. }
  24. File::~File() {
  25. if (opened_) {
  26. Close();
  27. }
  28. }
  29. FilesystemStatus File::Open(
  30. const char* file_name,
  31. const char* mode,
  32. uint16_t retry_timeout) {
  33. if (mode[0] == 'r') {
  34. return Open(file_name, FA_READ | FA_OPEN_EXISTING, retry_timeout);
  35. } else if (mode[0] == 'w') {
  36. return Open(file_name, FA_WRITE | FA_CREATE_ALWAYS, retry_timeout);
  37. } else {
  38. return FS_INVALID_PARAMETER;
  39. }
  40. }
  41. FilesystemStatus File::Open(
  42. const char* file_name,
  43. uint8_t attributes,
  44. uint16_t retry_timeout) {
  45. if (opened_) {
  46. Close();
  47. }
  48. FilesystemStatus s;
  49. s = static_cast<FilesystemStatus>(f_open(&f_, file_name, attributes));
  50. if (s == FS_DISK_ERROR && retry_timeout) {
  51. // If an open fails because of a disk access error, try to reinitialize the
  52. // disk access layer. This might happen because a process in the background
  53. // has temporarily disabled the disk access layer (for example to access
  54. // another device). It is OK to have the disk access layer disabled between
  55. // file access "sessions" -- but it is not OK to have it disabled during a
  56. // session.
  57. Filesystem::Init(retry_timeout);
  58. s = static_cast<FilesystemStatus>(f_open(&f_, file_name, attributes));
  59. }
  60. opened_ = s == FS_OK;
  61. return s;
  62. }
  63. FilesystemStatus File::Seek(uint32_t position) {
  64. if (!opened_) {
  65. return FS_NOT_OPENED;
  66. }
  67. return static_cast<FilesystemStatus>(f_lseek(&f_, position));
  68. }
  69. FilesystemStatus File::Close() {
  70. return static_cast<FilesystemStatus>(f_close(&f_));
  71. }
  72. FilesystemStatus File::Truncate() {
  73. if (!opened_) {
  74. return FS_NOT_OPENED;
  75. }
  76. return static_cast<FilesystemStatus>(f_truncate(&f_));
  77. }
  78. FilesystemStatus File::Sync() {
  79. if (!opened_) {
  80. return FS_NOT_OPENED;
  81. }
  82. return static_cast<FilesystemStatus>(f_sync(&f_));
  83. }
  84. FilesystemStatus File::Read(uint8_t* data, uint16_t size, uint16_t* read) {
  85. if (!opened_) {
  86. return FS_NOT_OPENED;
  87. }
  88. return static_cast<FilesystemStatus>(f_read(&f_, data, size, read));
  89. }
  90. FilesystemStatus File::Write(
  91. const uint8_t* data,
  92. uint16_t size,
  93. uint16_t* written) {
  94. if (!opened_) {
  95. return FS_NOT_OPENED;
  96. }
  97. return static_cast<FilesystemStatus>(f_write(&f_, data, size, written));
  98. }
  99. } // namespace avrlibx