Skip to content

Filesystem Class added to mbed namespace #7747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions features/filesystem/fat/FATFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
#include <errno.h>
#include <stdlib.h>

////// Error handling /////
using namespace mbed;
namespace mbed {

static int fat_error_remap(FRESULT res)
{
Expand Down Expand Up @@ -136,16 +135,14 @@ static Deferred<const char*> fat_path_prefix(int id, const char *path)
return Deferred<const char*>(buffer, dodelete);
}


////// Disk operations //////

// Global access to block device from FAT driver
static BlockDevice *_ffs[FF_VOLUMES] = {0};
static mbed::BlockDevice *_ffs[FF_VOLUMES] = {0};
static SingletonPtr<PlatformMutex> _ffs_mutex;


// FAT driver functions
DWORD get_fattime(void)
extern "C" DWORD get_fattime(void)
{
time_t rawtime;
time(&rawtime);
Expand All @@ -158,12 +155,12 @@ DWORD get_fattime(void)
| (DWORD)(ptm->tm_sec/2 );
}

void *ff_memalloc(UINT size)
extern "C" void *ff_memalloc(UINT size)
{
return malloc(size);
}

void ff_memfree(void *p)
extern "C" void ff_memfree(void *p)
{
free(p);
}
Expand All @@ -189,34 +186,34 @@ static DWORD disk_get_sector_count(BYTE pdrv)
return scount;
}

DSTATUS disk_status(BYTE pdrv)
extern "C" DSTATUS disk_status(BYTE pdrv)
{
debug_if(FFS_DBG, "disk_status on pdrv [%d]\n", pdrv);
return RES_OK;
}

DSTATUS disk_initialize(BYTE pdrv)
extern "C" DSTATUS disk_initialize(BYTE pdrv)
{
debug_if(FFS_DBG, "disk_initialize on pdrv [%d]\n", pdrv);
return (DSTATUS)_ffs[pdrv]->init();
}

DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
extern "C" DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
{
Copy link
Contributor

@geky geky Aug 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the disk functions still live in the mbed namespace if you declare them as extern "C"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they are declared as extern "C" then they are part of the global namespace; the only one C knows.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though declaring them as extern "C" allows you to use anything from the mbed namespace and also notates why the functions aren't in the namespace.

It's just a minor thing to note

debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
DWORD ssize = disk_get_sector_size(pdrv);
bd_addr_t addr = (bd_addr_t)sector*ssize;
bd_size_t size = (bd_size_t)count*ssize;
mbed::bd_addr_t addr = (mbed::bd_addr_t)sector*ssize;
mbed::bd_size_t size = (mbed::bd_size_t)count*ssize;
int err = _ffs[pdrv]->read(buff, addr, size);
return err ? RES_PARERR : RES_OK;
}

DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
extern "C" DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
{
debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
DWORD ssize = disk_get_sector_size(pdrv);
bd_addr_t addr = (bd_addr_t)sector*ssize;
bd_size_t size = (bd_size_t)count*ssize;
mbed::bd_addr_t addr = (mbed::bd_addr_t)sector*ssize;
mbed::bd_size_t size = (mbed::bd_size_t)count*ssize;
int err = _ffs[pdrv]->erase(addr, size);
if (err) {
return RES_PARERR;
Expand All @@ -230,7 +227,7 @@ DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
return RES_OK;
}

DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
extern "C" DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
{
debug_if(FFS_DBG, "disk_ioctl(%d)\n", cmd);
switch (cmd) {
Expand Down Expand Up @@ -263,8 +260,8 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
} else {
DWORD *sectors = (DWORD*)buff;
DWORD ssize = disk_get_sector_size(pdrv);
bd_addr_t addr = (bd_addr_t)sectors[0]*ssize;
bd_size_t size = (bd_size_t)(sectors[1]-sectors[0]+1)*ssize;
mbed::bd_addr_t addr = (mbed::bd_addr_t)sectors[0]*ssize;
mbed::bd_size_t size = (mbed::bd_size_t)(sectors[1]-sectors[0]+1)*ssize;
int err = _ffs[pdrv]->trim(addr, size);
return err ? RES_PARERR : RES_OK;
}
Expand All @@ -273,7 +270,6 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
return RES_PARERR;
}


////// Generic filesystem operations //////

// Filesystem implementation (See FATFilySystem.h)
Expand Down Expand Up @@ -825,3 +821,4 @@ void FATFileSystem::dir_rewind(fs_dir_t dir)
unlock();
}

} // namespace mbed
46 changes: 25 additions & 21 deletions features/filesystem/fat/FATFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@
#include <stdint.h>
#include "PlatformMutex.h"

namespace mbed {

/**
* FATFileSystem based on ChaN's Fat Filesystem library v0.8
*/
class FATFileSystem : public mbed::FileSystem {
class FATFileSystem : public FileSystem {
public:
/** Lifetime of the FATFileSystem
*
* @param name Name to add filesystem to tree as
* @param bd BlockDevice to mount, may be passed instead to mount call
*/
FATFileSystem(const char *name = NULL, mbed::BlockDevice *bd = NULL);
FATFileSystem(const char *name = NULL, BlockDevice *bd = NULL);
virtual ~FATFileSystem();

/** Formats a logical drive, FDISK partitioning rule.
Expand All @@ -58,14 +60,14 @@ class FATFileSystem : public mbed::FileSystem {
*
* @return 0 on success, negative error code on failure
*/
static int format(mbed::BlockDevice *bd, mbed::bd_size_t cluster_size = 0);
static int format(BlockDevice *bd, bd_size_t cluster_size = 0);

/** Mounts a filesystem to a block device
*
* @param bd BlockDevice to mount to
* @return 0 on success, negative error code on failure
*/
virtual int mount(mbed::BlockDevice *bd);
virtual int mount(BlockDevice *bd);

/** Unmounts a filesystem from the underlying block device
*
Expand All @@ -90,7 +92,7 @@ class FATFileSystem : public mbed::FileSystem {
*
* @return 0 on success, negative error code on failure
*/
virtual int reformat(mbed::BlockDevice *bd, int allocation_unit);
virtual int reformat(BlockDevice *bd, int allocation_unit);

/** Reformats a filesystem, results in an empty and mounted filesystem
*
Expand All @@ -100,7 +102,7 @@ class FATFileSystem : public mbed::FileSystem {
* Default: NULL
* @return 0 on success, negative error code on failure
*/
virtual int reformat(mbed::BlockDevice *bd = NULL)
virtual int reformat(BlockDevice *bd = NULL)
{
// required for virtual inheritance shenanigans
return reformat(bd, 0);
Expand Down Expand Up @@ -154,14 +156,14 @@ class FATFileSystem : public mbed::FileSystem {
* bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
* @return 0 on success, negative error code on failure
*/
virtual int file_open(mbed::fs_file_t *file, const char *path, int flags);
virtual int file_open(fs_file_t *file, const char *path, int flags);

/** Close a file
*
* @param file File handle
* @return 0 on success, negative error code on failure
*/
virtual int file_close(mbed::fs_file_t file);
virtual int file_close(fs_file_t file);

/** Read the contents of a file into a buffer
*
Expand All @@ -170,7 +172,7 @@ class FATFileSystem : public mbed::FileSystem {
* @param len The number of bytes to read
* @return The number of bytes read, 0 at end of file, negative error on failure
*/
virtual ssize_t file_read(mbed::fs_file_t file, void *buffer, size_t len);
virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len);

/** Write the contents of a buffer to a file
*
Expand All @@ -179,14 +181,14 @@ class FATFileSystem : public mbed::FileSystem {
* @param len The number of bytes to write
* @return The number of bytes written, negative error on failure
*/
virtual ssize_t file_write(mbed::fs_file_t file, const void *buffer, size_t len);
virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len);

/** Flush any buffers associated with the file
*
* @param file File handle
* @return 0 on success, negative error code on failure
*/
virtual int file_sync(mbed::fs_file_t file);
virtual int file_sync(fs_file_t file);

/** Move the file position to a given offset from from a given location
*
Expand All @@ -198,65 +200,65 @@ class FATFileSystem : public mbed::FileSystem {
* SEEK_END to start from end of file
* @return The new offset of the file
*/
virtual off_t file_seek(mbed::fs_file_t file, off_t offset, int whence);
virtual off_t file_seek(fs_file_t file, off_t offset, int whence);

/** Get the file position of the file
*
* @param file File handle
* @return The current offset in the file
*/
virtual off_t file_tell(mbed::fs_file_t file);
virtual off_t file_tell(fs_file_t file);

/** Get the size of the file
*
* @param file File handle
* @return Size of the file in bytes
*/
virtual off_t file_size(mbed::fs_file_t file);
virtual off_t file_size(fs_file_t file);

/** Open a directory on the filesystem
*
* @param dir Destination for the handle to the directory
* @param path Name of the directory to open
* @return 0 on success, negative error code on failure
*/
virtual int dir_open(mbed::fs_dir_t *dir, const char *path);
virtual int dir_open(fs_dir_t *dir, const char *path);

/** Close a directory
*
* @param dir Dir handle
* @return 0 on success, negative error code on failure
*/
virtual int dir_close(mbed::fs_dir_t dir);
virtual int dir_close(fs_dir_t dir);

/** Read the next directory entry
*
* @param dir Dir handle
* @param ent The directory entry to fill out
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
*/
virtual ssize_t dir_read(mbed::fs_dir_t dir, struct dirent *ent);
virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);

/** Set the current position of the directory
*
* @param dir Dir handle
* @param offset Offset of the location to seek to,
* must be a value returned from dir_tell
*/
virtual void dir_seek(mbed::fs_dir_t dir, off_t offset);
virtual void dir_seek(fs_dir_t dir, off_t offset);

/** Get the current position of the directory
*
* @param dir Dir handle
* @return Position of the directory that can be passed to dir_rewind
*/
virtual off_t dir_tell(mbed::fs_dir_t dir);
virtual off_t dir_tell(fs_dir_t dir);

/** Rewind the current position to the beginning of the directory
*
* @param dir Dir handle
*/
virtual void dir_rewind(mbed::fs_dir_t dir);
virtual void dir_rewind(fs_dir_t dir);

private:
FATFS _fs; // Work area (file system object) for logical drive
Expand All @@ -266,7 +268,9 @@ class FATFileSystem : public mbed::FileSystem {
protected:
virtual void lock();
virtual void unlock();
virtual int mount(mbed::BlockDevice *bd, bool mount);
virtual int mount(BlockDevice *bd, bool mount);
};

} // namespace mbed

#endif
3 changes: 2 additions & 1 deletion features/filesystem/littlefs/LittleFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C" {
#include "lfs_util.h"
}

using namespace mbed;
namespace mbed {

////// Conversion functions //////
static int lfs_toerror(int err)
Expand Down Expand Up @@ -540,3 +540,4 @@ void LittleFileSystem::dir_rewind(fs_dir_t dir)
_mutex.unlock();
}

} // namespace mbed
2 changes: 2 additions & 0 deletions features/filesystem/littlefs/LittleFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern "C" {
#include "lfs.h"
}

namespace mbed {

/**
* LittleFileSystem, a little filesystem
Expand Down Expand Up @@ -279,5 +280,6 @@ class LittleFileSystem : public mbed::FileSystem {
PlatformMutex _mutex;
};

} // namespace mbed

#endif