FileSystemObject.hpp
Go to the documentation of this file.
1 /*
2  * SocialLedge.com - Copyright (C) 2013
3  *
4  * This file is part of free software framework for embedded processors.
5  * You can use it and/or distribute it as long as this copyright header
6  * remains unmodified. The code is free for personal use and requires
7  * permission to use in a commercial product.
8  *
9  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
10  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
12  * I SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
13  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
14  *
15  * You can reach the author of this software at :
16  * p r e e t . w i k i @ g m a i l . c o m
17  */
18 
25 #ifndef FILE_SYSTEM_OBJ_HPP__
26 #define FILE_SYSTEM_OBJ_HPP__
27 
28 #include "fat/ff.h"
29 #include "fat/disk/diskio.h"
30 
31 
32 
39 {
40  public:
44  const char* getDrivePath() const
45  {
46  return mVolStr;
47  }
48 
54  char mount() const
55  {
56  return f_mount((FATFS*)&mFileSystem, getDrivePath(), 1);
57  }
58 
65  FRESULT getDriveInfo(unsigned int* pTotalDriveSpaceKB, unsigned int* pAvailableSpaceKB) const
66  {
67  // This object will just point back to our mFileSystem using the mVolStr
68  FATFS* pFatFs;
69 
70  *pTotalDriveSpaceKB = 0;
71  *pAvailableSpaceKB = 0;
72  unsigned long int fre_clust = 0;
73  FRESULT result;
74 
75  if (FR_OK == (result = f_getfree(mVolStr, &fre_clust, &pFatFs)))
76  {
77  /* Get total sectors and free sectors */
78  unsigned int totalSectors = (pFatFs->n_fatent - 2) * pFatFs->csize;
79  unsigned int freeSectors = fre_clust * pFatFs->csize;
80 
81  /* Print free space in unit of KB (assuming 512 bytes/sector) */
82  *pAvailableSpaceKB = freeSectors / 2;
83  *pTotalDriveSpaceKB = totalSectors / 2;
84  }
85 
86  return result;
87  }
88 
92  FRESULT format() const
93  {
94  return f_mkfs(getDrivePath(), 0, 0);
95  }
96 
97  private:
99  friend class Storage;
100 
102  FileSystemObject(DriveNumberType volumeNum) :
103  mVolNum(volumeNum)
104  {
105  mVolStr[0] = volumeNum + '0';
106  mVolStr[1] = ':';
107  mVolStr[2] = '\0';
108  }
109 
110  FATFS mFileSystem;
111  const DriveNumberType mVolNum;
112  char mVolStr[3];
113 };
114 
115 #endif /* FILE_SYSTEM_OBJ_HPP__ */
FRESULT f_mkfs(const TCHAR *path, BYTE sfd, UINT au)
Definition: ff.c:3976
DWORD n_fatent
Definition: ff.h:99
FRESULT format() const
Definition: FileSystemObject.hpp:92
char mount() const
Definition: FileSystemObject.hpp:54
Definition: ff.h:77
Definition: storage.hpp:44
BYTE csize
Definition: ff.h:80
FRESULT f_getfree(const TCHAR *path, DWORD *nclst, FATFS **fatfs)
Definition: ff.c:3317
FRESULT
Definition: ff.h:180
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Definition: ff.c:2368
Definition: FileSystemObject.hpp:38
DriveNumberType
Enumeration of the Drive numbers :
Definition: diskio.h:12
const char * getDrivePath() const
Definition: FileSystemObject.hpp:44
Definition: ff.h:181
FRESULT getDriveInfo(unsigned int *pTotalDriveSpaceKB, unsigned int *pAvailableSpaceKB) const
Definition: FileSystemObject.hpp:65