diff --git a/drivers/DirHandle.h b/drivers/DirHandle.h deleted file mode 100644 index 6be86f969ad..00000000000 --- a/drivers/DirHandle.h +++ /dev/null @@ -1,115 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef MBED_DIRHANDLE_H -#define MBED_DIRHANDLE_H - -#include -#include "platform/platform.h" - -#include "FileHandle.h" - -namespace mbed { -/** \addtogroup drivers */ -/** @{*/ - -/** Represents a directory stream. Objects of this type are returned - * by a FileSystemLike's opendir method. Implementations must define - * at least closedir, readdir and rewinddir. - * - * If a FileSystemLike class defines the opendir method, then the - * directories of an object of that type can be accessed by - * DIR *d = opendir("/example/directory") (or opendir("/example") - * to open the root of the filesystem), and then using readdir(d) etc. - * - * The root directory is considered to contain all FileLike and - * FileSystemLike objects, so the DIR* returned by opendir("/") will - * reflect this. - * - * @Note Synchronization level: Set by subclass - */ -class DirHandle { -public: - MBED_DEPRECATED_SINCE("mbed-os-5.4", - "The mbed 2 filesystem classes have been superseeded by the FileSystem api, " - "Replaced by File") - DirHandle() {} - - /** Closes the directory. - * - * @returns - * 0 on success, - * -1 on error. - */ - virtual int closedir()=0; - - /** Return the directory entry at the current position, and - * advances the position to the next entry. - * - * @returns - * A pointer to a dirent structure representing the - * directory entry at the current position, or NULL on reaching - * end of directory or error. - */ - virtual struct dirent *readdir()=0; - - /** Resets the position to the beginning of the directory. - */ - virtual void rewinddir()=0; - - /** Returns the current position of the DirHandle. - * - * @returns - * the current position, - * -1 on error. - */ - virtual off_t telldir() { return -1; } - - /** Sets the position of the DirHandle. - * - * @param location The location to seek to. Must be a value returned by telldir. - */ - virtual void seekdir(off_t location) { (void)location;} - - virtual ~DirHandle() {} - -protected: - - /** Acquire exclusive access to this object. - */ - virtual void lock() { - // Stub - } - - /** Release exclusive access to this object. - */ - virtual void unlock() { - // Stub - } - -protected: - /** Internal-only constructor to work around deprecated notices when not used - *. due to nested deprecations and difficulty of compilers finding their way around - * the class hierarchy - */ - friend class FileSystemLike; - DirHandle(int) {} -}; - -} // namespace mbed - -#endif /* MBED_DIRHANDLE_H */ - -/** @}*/ diff --git a/drivers/FileHandle.h b/drivers/FileHandle.h deleted file mode 100644 index 696c8365ed1..00000000000 --- a/drivers/FileHandle.h +++ /dev/null @@ -1,142 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef MBED_FILEHANDLE_H -#define MBED_FILEHANDLE_H - -typedef int FILEHANDLE; - -#include -#include "platform/platform.h" - -namespace mbed { -/** \addtogroup drivers */ -/** @{*/ - -/** An OO equivalent of the internal FILEHANDLE variable - * and associated _sys_* functions. - * - * FileHandle is an abstract class, needing at least sys_write and - * sys_read to be implmented for a simple interactive device. - * - * No one ever directly tals to/instanciates a FileHandle - it gets - * created by FileSystem, and wrapped up by stdio. - * - * @Note Synchronization level: Set by subclass - */ -class FileHandle { - -public: - MBED_DEPRECATED_SINCE("mbed-os-5.4", - "The mbed 2 filesystem classes have been superseeded by the FileSystem api, " - "Replaced by File") - FileHandle() {} - - /** Write the contents of a buffer to the file - * - * @param buffer the buffer to write from - * @param length the number of characters to write - * - * @returns - * The number of characters written (possibly 0) on success, -1 on error. - */ - virtual ssize_t write(const void* buffer, size_t length) = 0; - - /** Close the file - * - * @returns - * Zero on success, -1 on error. - */ - virtual int close() = 0; - - /** Function read - * Reads the contents of the file into a buffer - * - * @param buffer the buffer to read in to - * @param length the number of characters to read - * - * @returns - * The number of characters read (zero at end of file) on success, -1 on error. - */ - virtual ssize_t read(void* buffer, size_t length) = 0; - - /** Check if the handle is for a interactive terminal device. - * If so, line buffered behaviour is used by default - * - * @returns - * 1 if it is a terminal, - * 0 otherwise - */ - virtual int isatty() = 0; - - /** Move the file position to a given offset from a given location. - * - * @param offset The offset from whence to move to - * @param whence SEEK_SET for the start of the file, SEEK_CUR for the - * current file position, or SEEK_END for the end of the file. - * - * @returns - * new file position on success, - * -1 on failure or unsupported - */ - virtual off_t lseek(off_t offset, int whence) = 0; - - /** Flush any buffers associated with the FileHandle, ensuring it - * is up to date on disk - * - * @returns - * 0 on success or un-needed, - * -1 on error - */ - virtual int fsync() = 0; - - virtual off_t flen() { - lock(); - /* remember our current position */ - off_t pos = lseek(0, SEEK_CUR); - if(pos == -1) { - unlock(); - return -1; - } - /* seek to the end to get the file length */ - off_t res = lseek(0, SEEK_END); - /* return to our old position */ - lseek(pos, SEEK_SET); - unlock(); - return res; - } - - virtual ~FileHandle() {}; - -protected: - - /** Acquire exclusive access to this object. - */ - virtual void lock() { - // Stub - } - - /** Release exclusive access to this object. - */ - virtual void unlock() { - // Stub - } -}; - -} // namespace mbed - -#endif - -/** @}*/ diff --git a/features/filesystem/Dir.h b/features/filesystem/Dir.h index b4dc4133d18..84b9194584e 100644 --- a/features/filesystem/Dir.h +++ b/features/filesystem/Dir.h @@ -18,6 +18,7 @@ #define DIR_H #include "filesystem/FileSystem.h" +#include "platform/DirHandle.h" namespace mbed { /** \addtogroup filesystem */ @@ -26,7 +27,7 @@ namespace mbed { /** Dir class */ -class Dir { +class Dir : public DirHandle { public: /** Create an uninitialized directory * diff --git a/features/filesystem/File.cpp b/features/filesystem/File.cpp index 78cf92addf2..af8ffc85ff4 100644 --- a/features/filesystem/File.cpp +++ b/features/filesystem/File.cpp @@ -25,7 +25,7 @@ File::File() } File::File(FileSystem *fs, const char *path, int flags) - : FileLike(path), _fs(0), _file(0) + : _fs(0), _file(0) { open(fs, path, flags); } diff --git a/features/filesystem/File.h b/features/filesystem/File.h index 4a3da361f90..700c241634d 100644 --- a/features/filesystem/File.h +++ b/features/filesystem/File.h @@ -18,7 +18,7 @@ #define FILE_H #include "filesystem/FileSystem.h" -#include "drivers/FileLike.h" +#include "platform/FileHandle.h" namespace mbed { /** \addtogroup filesystem */ @@ -27,7 +27,7 @@ namespace mbed { /** File class */ -class File : public FileLike { +class File : public FileHandle { public: /** Create an uninitialized file * @@ -75,7 +75,7 @@ class File : public FileLike { * @return The number of bytes read, 0 at end of file, negative error on failure */ - virtual ssize_t read(void *buffer, size_t len); + virtual ssize_t read(void *buffer, size_t size); /** Write the contents of a buffer to a file * @@ -83,7 +83,7 @@ class File : public FileLike { * @param size The number of bytes to write * @return The number of bytes written, negative error on failure */ - virtual ssize_t write(const void *buffer, size_t len); + virtual ssize_t write(const void *buffer, size_t size); /** Flush any buffers associated with the file * diff --git a/features/filesystem/FileSystem.h b/features/filesystem/FileSystem.h index cfc84225d17..ca7597282d1 100644 --- a/features/filesystem/FileSystem.h +++ b/features/filesystem/FileSystem.h @@ -19,7 +19,7 @@ #include "platform/platform.h" -#include "drivers/FileBase.h" +#include "platform/FileBase.h" #include "BlockDevice.h" namespace mbed { @@ -118,7 +118,7 @@ class FileSystem : public FileBase { * @param size 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(fs_file_t file, void *buffer, size_t len) = 0; + virtual ssize_t file_read(fs_file_t file, void *buffer, size_t size) = 0; /** Write the contents of a buffer to a file * @@ -127,7 +127,7 @@ class FileSystem : public FileBase { * @param size The number of bytes to write * @return The number of bytes written, negative error on failure */ - virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len) = 0; + virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t size) = 0; /** Flush any buffers associated with the file * diff --git a/mbed.h b/mbed.h index 4ec380812a0..9b1b01e8d6a 100644 --- a/mbed.h +++ b/mbed.h @@ -94,7 +94,7 @@ #include "drivers/LowPowerTimeout.h" #include "drivers/LowPowerTicker.h" #include "drivers/LowPowerTimer.h" -#include "drivers/LocalFileSystem.h" +#include "platform/LocalFileSystem.h" #include "drivers/InterruptIn.h" #include "platform/mbed_wait_api.h" #include "hal/sleep_api.h" diff --git a/platform/DirHandle.h b/platform/DirHandle.h new file mode 100644 index 00000000000..7478b62b80e --- /dev/null +++ b/platform/DirHandle.h @@ -0,0 +1,150 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MBED_DIRHANDLE_H +#define MBED_DIRHANDLE_H + +#include +#include "platform/platform.h" +#include "platform/FileHandle.h" + +namespace mbed { +/** \addtogroup drivers */ +/** @{*/ + + +/** Represents a directory stream. Objects of this type are returned + * by an opendir function. The core functions are read and seek, + * but only a subset needs to be provided. + * + * If a FileSystemLike class defines the opendir method, then the + * directories of an object of that type can be accessed by + * DIR *d = opendir("/example/directory") (or opendir("/example") + * to open the root of the filesystem), and then using readdir(d) etc. + * + * The root directory is considered to contain all FileHandle and + * FileSystem objects, so the DIR* returned by opendir("/") will + * reflect this. + * + * @note to create a directory, @see Dir + * @Note Synchronization level: Set by subclass + */ +class DirHandle { +public: + virtual ~DirHandle() {} + + /** Read the next directory entry + * + * @param path The buffer to read the null terminated path name in to + * @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 read(struct dirent *ent) = 0; + + /** Close a directory + * + * return 0 on success, negative error code on failure + */ + virtual int close() = 0; + + /** Set the current position of the directory + * + * @param offset Offset of the location to seek to, + * must be a value returned from tell + */ + virtual void seek(off_t offset) = 0; + + /** Get the current position of the directory + * + * @return Position of the directory that can be passed to rewind + */ + virtual off_t tell() = 0; + + /** Rewind the current position to the beginning of the directory + */ + virtual void rewind() = 0; + + /** Get the sizeof the directory + * + * @return Number of files in the directory + */ + virtual size_t size() + { + off_t off = tell(); + size_t size = 0; + struct dirent *ent = new struct dirent; + + rewind(); + while (read(ent) > 0) { + size += 1; + } + seek(off); + + delete ent; + return size; + } + + /** Closes the directory. + * + * @returns + * 0 on success, + * -1 on error. + */ + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close") + virtual int closedir() { return close(); }; + + /** Return the directory entry at the current position, and + * advances the position to the next entry. + * + * @returns + * A pointer to a dirent structure representing the + * directory entry at the current position, or NULL on reaching + * end of directory or error. + */ + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read") + virtual struct dirent *readdir() + { + static struct dirent ent; + return (read(&ent) > 0) ? &ent : NULL; + } + + /** Resets the position to the beginning of the directory. + */ + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind") + virtual void rewinddir() { rewind(); } + + /** Returns the current position of the DirHandle. + * + * @returns + * the current position, + * -1 on error. + */ + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell") + virtual off_t telldir() { return tell(); } + + /** Sets the position of the DirHandle. + * + * @param location The location to seek to. Must be a value returned by telldir. + */ + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek") + virtual void seekdir(off_t location) { seek(location); } +}; + + +} // namespace mbed + +#endif /* MBED_DIRHANDLE_H */ + +/** @}*/ diff --git a/drivers/FileBase.cpp b/platform/FileBase.cpp similarity index 91% rename from drivers/FileBase.cpp rename to platform/FileBase.cpp index 862dc74b0d2..4458f584b48 100644 --- a/drivers/FileBase.cpp +++ b/platform/FileBase.cpp @@ -13,8 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/FileBase.h" -#include "drivers/FileLike.h" +#include "platform/FileBase.h" +#include "platform/FileLike.h" +#include "platform/FileHandle.h" namespace mbed { @@ -52,8 +53,8 @@ FileBase::~FileBase() { _mutex->unlock(); if (getPathType() == FilePathType) { - extern void remove_filehandle(FileLike *file); - remove_filehandle(static_cast(this)); + extern void remove_filehandle(FileHandle *file); + remove_filehandle(static_cast(static_cast(this))); } } diff --git a/drivers/FileBase.h b/platform/FileBase.h similarity index 100% rename from drivers/FileBase.h rename to platform/FileBase.h diff --git a/drivers/FileLike.h b/platform/FileHandle.h similarity index 71% rename from drivers/FileLike.h rename to platform/FileHandle.h index f213272021c..e131599600a 100644 --- a/drivers/FileLike.h +++ b/platform/FileHandle.h @@ -13,31 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MBED_FILELIKE_H -#define MBED_FILELIKE_H +#ifndef MBED_FILEHANDLE_H +#define MBED_FILEHANDLE_H -#include "platform/mbed_toolchain.h" -#include "drivers/FileBase.h" +typedef int FILEHANDLE; + +#include +#include "platform/platform.h" namespace mbed { /** \addtogroup drivers */ /** @{*/ -/* Class FileLike - * A file-like object is one that can be opened with fopen by - * fopen("/name", mode). +/** Class FileHandle + * + * An abstract interface that represents operations on a file-like + * object. The core functions are read, write, and seek, but only + * a subset of these operations can be provided. * - * @Note Synchronization level: Set by subclass + * @note to create a file, @see File + * @note Synchronization level: Set by subclass */ -class FileLike : public FileBase { +class FileHandle { public: - /** Constructor FileLike - * - * @param name The name to use to open the file. - */ - FileLike(const char *name = NULL) : FileBase(name, FilePathType) {} - virtual ~FileLike() {} + virtual ~FileHandle() {} /** Read the contents of a file into a buffer * @@ -45,7 +45,7 @@ class FileLike : public FileBase { * @param size The number of bytes to read * @return The number of bytes read, 0 at end of file, negative error on failure */ - virtual ssize_t read(void *buffer, size_t len) = 0; + virtual ssize_t read(void *buffer, size_t size) = 0; /** Write the contents of a buffer to a file * @@ -53,7 +53,18 @@ class FileLike : public FileBase { * @param size The number of bytes to write * @return The number of bytes written, negative error on failure */ - virtual ssize_t write(const void *buffer, size_t len) = 0; + virtual ssize_t write(const void *buffer, size_t size) = 0; + + /** Move the file position to a given offset from from a given location + * + * @param offset The offset from whence to move to + * @param whence The start of where to seek + * SEEK_SET to start from beginning of file, + * SEEK_CUR to start from current position in file, + * SEEK_END to start from end of file + * @return The new offset of the file + */ + virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0; /** Close a file * @@ -65,42 +76,51 @@ class FileLike : public FileBase { * * @return 0 on success, negative error code on failure */ - virtual int sync() = 0; + virtual int sync() + { + return 0; + } /** Check if the file in an interactive terminal device * * @return True if the file is a terminal */ - virtual int isatty() = 0; - - /** Move the file position to a given offset from from a given location - * - * @param offset The offset from whence to move to - * @param whence The start of where to seek - * SEEK_SET to start from beginning of file, - * SEEK_CUR to start from current position in file, - * SEEK_END to start from end of file - * @return The new offset of the file - */ - virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0; + virtual int isatty() + { + return false; + } /** Get the file position of the file + * + * @note This is equivalent to seek(0, SEEK_CUR) * * @return The current offset in the file */ - virtual off_t tell() = 0; + virtual off_t tell() + { + return seek(0, SEEK_CUR); + } /** Rewind the file position to the beginning of the file * - * @note This is equivalent to file_seek(file, 0, FS_SEEK_SET) + * @note This is equivalent to seek(0, SEEK_SET) */ - virtual void rewind() = 0; + virtual void rewind() + { + seek(0, SEEK_SET); + } /** Get the size of the file * * @return Size of the file in bytes */ - virtual size_t size() = 0; + virtual size_t size() + { + off_t off = tell(); + size_t size = seek(0, SEEK_END); + seek(off, SEEK_SET); + return size; + } /** Move the file position to a given offset from a given location. * @@ -112,7 +132,7 @@ class FileLike : public FileBase { * new file position on success, * -1 on failure or unsupported */ - MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::seek") + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek") virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); } /** Flush any buffers associated with the FileHandle, ensuring it @@ -122,7 +142,7 @@ class FileLike : public FileBase { * 0 on success or un-needed, * -1 on error */ - MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::sync") + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync") virtual int fsync() { return sync(); } /** Find the length of the file @@ -130,21 +150,8 @@ class FileLike : public FileBase { * @returns * Length of the file */ - MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::size") + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size") virtual off_t flen() { return size(); } - -protected: - /** Acquire exclusive access to this object. - */ - virtual void lock() { - // Stub - } - - /** Release exclusive access to this object. - */ - virtual void unlock() { - // Stub - } }; diff --git a/platform/FileLike.h b/platform/FileLike.h new file mode 100644 index 00000000000..a249b68c905 --- /dev/null +++ b/platform/FileLike.h @@ -0,0 +1,48 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MBED_FILELIKE_H +#define MBED_FILELIKE_H + +#include "platform/mbed_toolchain.h" +#include "platform/FileBase.h" +#include "platform/FileHandle.h" + +namespace mbed { +/** \addtogroup drivers */ +/** @{*/ + + +/* Class FileLike + * A file-like object is one that can be opened with fopen by + * fopen("/name", mode). + * + * @Note Synchronization level: Set by subclass + */ +class FileLike : public FileHandle, public FileBase { +public: + /** Constructor FileLike + * + * @param name The name to use to open the file. + */ + FileLike(const char *name = NULL) : FileBase(name, FilePathType) {} + virtual ~FileLike() {} +}; + + +/** @}*/ +} // namespace mbed + +#endif diff --git a/drivers/FilePath.cpp b/platform/FilePath.cpp similarity index 98% rename from drivers/FilePath.cpp rename to platform/FilePath.cpp index cdd64f5d9c6..e8733a33b4f 100644 --- a/drivers/FilePath.cpp +++ b/platform/FilePath.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/FilePath.h" +#include "platform/FilePath.h" namespace mbed { diff --git a/drivers/FilePath.h b/platform/FilePath.h similarity index 94% rename from drivers/FilePath.h rename to platform/FilePath.h index 7e1d1a58794..9daa1597c69 100644 --- a/drivers/FilePath.h +++ b/platform/FilePath.h @@ -18,8 +18,8 @@ #include "platform/platform.h" -#include "drivers/FileSystemLike.h" -#include "drivers/FileLike.h" +#include "platform/FileSystemLike.h" +#include "platform/FileLike.h" namespace mbed { /** \addtogroup drivers */ diff --git a/drivers/FileSystemLike.cpp b/platform/FileSystemLike.cpp similarity index 83% rename from drivers/FileSystemLike.cpp rename to platform/FileSystemLike.cpp index 167936af325..3831c22b46e 100644 --- a/drivers/FileSystemLike.cpp +++ b/platform/FileSystemLike.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/FileSystemLike.h" +#include "platform/FileSystemLike.h" namespace mbed { @@ -27,48 +27,47 @@ class BaseDirHandle : public DirHandle { give unusual results from readdir. */ off_t n; - struct dirent cur_entry; - BaseDirHandle() : DirHandle(0), n(0), cur_entry() { + BaseDirHandle() : DirHandle(), n(0) { } - virtual int closedir() { + virtual int close() { // No lock can be used in destructor delete this; return 0; } - virtual struct dirent *readdir() { + virtual int read(struct dirent *ent) { lock(); FileBase *ptr = FileBase::get(n); if (ptr == NULL) { unlock(); - return NULL; + return -1; } /* Increment n, so next readdir gets the next item */ n++; /* Setup cur entry and return a pointer to it */ - std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX); + std::strncpy(ent->d_name, ptr->getName(), NAME_MAX); unlock(); - return &cur_entry; + return 0; } - virtual off_t telldir() { + virtual off_t tell() { lock(); off_t offset = n; unlock(); return offset; } - virtual void seekdir(off_t offset) { + virtual void seek(off_t offset) { lock(); n = offset; unlock(); } - virtual void rewinddir() { + virtual void rewind() { lock(); n = 0; unlock(); diff --git a/drivers/FileSystemLike.h b/platform/FileSystemLike.h similarity index 97% rename from drivers/FileSystemLike.h rename to platform/FileSystemLike.h index 47ece539d9a..b9949c2a878 100644 --- a/drivers/FileSystemLike.h +++ b/platform/FileSystemLike.h @@ -18,9 +18,9 @@ #include "platform/platform.h" -#include "drivers/FileBase.h" -#include "drivers/FileHandle.h" -#include "drivers/DirHandle.h" +#include "platform/FileBase.h" +#include "platform/FileHandle.h" +#include "platform/DirHandle.h" namespace mbed { /** \addtogroup drivers */ diff --git a/drivers/LocalFileSystem.cpp b/platform/LocalFileSystem.cpp similarity index 92% rename from drivers/LocalFileSystem.cpp rename to platform/LocalFileSystem.cpp index 0e11be4cd4b..6215fc432b0 100644 --- a/drivers/LocalFileSystem.cpp +++ b/platform/LocalFileSystem.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/LocalFileSystem.h" +#include "platform/LocalFileSystem.h" #if DEVICE_LOCALFILESYSTEM @@ -142,7 +142,7 @@ int LocalFileHandle::isatty() { return ret; } -off_t LocalFileHandle::lseek(off_t position, int whence) { +off_t LocalFileHandle::seek(off_t position, int whence) { lock(); if (whence == SEEK_CUR) { position += pos; @@ -157,14 +157,14 @@ off_t LocalFileHandle::lseek(off_t position, int whence) { return position; } -int LocalFileHandle::fsync() { +int LocalFileHandle::sync() { lock(); int ret = semihost_ensure(_fh); unlock(); return ret; } -off_t LocalFileHandle::flen() { +size_t LocalFileHandle::size() { lock(); off_t off = semihost_flen(_fh); unlock(); @@ -182,43 +182,42 @@ void LocalFileHandle::unlock() { class LocalDirHandle : public DirHandle { public: - struct dirent cur_entry; XFINFO info; - LocalDirHandle() : cur_entry(), info() { + LocalDirHandle() : info() { } - virtual int closedir() { + virtual int close() { // No lock can be used in destructor delete this; return 0; } - virtual struct dirent *readdir() { + virtual int read(struct dirent *ent) { lock(); if (xffind("*", &info)!=0) { unlock(); - return NULL; + return 0; } - memcpy(cur_entry.d_name, info.name, sizeof(info.name)); + memcpy(ent->d_name, info.name, sizeof(info.name)); unlock(); - return &cur_entry; + return 1; } - virtual void rewinddir() { + virtual void rewind() { lock(); info.fileID = 0; unlock(); } - virtual off_t telldir() { + virtual off_t tell() { lock(); int fileId = info.fileID; unlock(); return fileId; } - virtual void seekdir(off_t offset) { + virtual void seek(off_t offset) { lock(); info.fileID = offset; unlock(); diff --git a/drivers/LocalFileSystem.h b/platform/LocalFileSystem.h similarity index 95% rename from drivers/LocalFileSystem.h rename to platform/LocalFileSystem.h index c48c0f75b2f..526c2d1f223 100644 --- a/drivers/LocalFileSystem.h +++ b/platform/LocalFileSystem.h @@ -20,7 +20,7 @@ #if DEVICE_LOCALFILESYSTEM -#include "drivers/FileSystemLike.h" +#include "platform/FileSystemLike.h" #include "platform/PlatformMutex.h" namespace mbed { @@ -42,11 +42,11 @@ class LocalFileHandle : public FileHandle { virtual int isatty(); - virtual off_t lseek(off_t position, int whence); + virtual off_t seek(off_t position, int whence); - virtual int fsync(); + virtual int sync(); - virtual off_t flen(); + virtual size_t size(); protected: virtual void lock(); diff --git a/drivers/Stream.cpp b/platform/Stream.cpp similarity index 99% rename from drivers/Stream.cpp rename to platform/Stream.cpp index 9837c812971..76568712e78 100644 --- a/drivers/Stream.cpp +++ b/platform/Stream.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/Stream.h" +#include "platform/Stream.h" namespace mbed { diff --git a/drivers/Stream.h b/platform/Stream.h similarity index 87% rename from drivers/Stream.h rename to platform/Stream.h index 6f8292118a3..cbb25a9c615 100644 --- a/drivers/Stream.h +++ b/platform/Stream.h @@ -17,8 +17,8 @@ #define MBED_STREAM_H #include "platform/platform.h" -#include "drivers/FileLike.h" -#include "drivers/FileHandle.h" +#include "platform/FileLike.h" +#include "platform/FileHandle.h" #include namespace mbed { @@ -66,6 +66,18 @@ class Stream : public FileLike { std::FILE *_file; + /** Acquire exclusive access to this object. + */ + virtual void lock() { + // Stub + } + + /** Release exclusive access to this object. + */ + virtual void unlock() { + // Stub + } + /* disallow copy constructor and assignment operators */ private: Stream(const Stream&); diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index bff1d59a699..3297fe1882b 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ #include "platform/platform.h" -#include "drivers/FilePath.h" +#include "platform/FilePath.h" #include "hal/serial_api.h" #include "platform/mbed_toolchain.h" #include "platform/mbed_semihost_api.h" @@ -85,11 +85,11 @@ uint32_t mbed_heap_size = 0; * put it in a filehandles array and return the index into that array * (or rather index+3, as filehandles 0-2 are stdin/out/err). */ -static FileLike *filehandles[OPEN_MAX]; +static FileHandle *filehandles[OPEN_MAX]; static SingletonPtr filehandle_mutex; namespace mbed { -void remove_filehandle(FileLike *file) { +void remove_filehandle(FileHandle *file) { filehandle_mutex->lock(); /* Remove all open filehandles for this */ for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) { @@ -233,16 +233,16 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) { filehandle_mutex->unlock(); return -1; } - filehandles[fh_i] = (FileLike*)FILE_HANDLE_RESERVED; + filehandles[fh_i] = (FileHandle*)FILE_HANDLE_RESERVED; filehandle_mutex->unlock(); - FileLike *res = NULL; + FileHandle *res = NULL; - /* FILENAME: ":0x12345678" describes a FileLike* */ + /* FILENAME: ":0x12345678" describes a FileHandle* */ if (name[0] == ':') { void *p; sscanf(name, ":%p", &p); - res = (FileLike*)p; + res = (FileHandle*)p; /* FILENAME: "/file_system/file_name" */ } else { @@ -295,7 +295,7 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh) { if (fh < 3) return 0; errno = EBADF; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; filehandles[fh-3] = NULL; if (fhc == NULL) return -1; @@ -335,7 +335,7 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign #endif n = length; } else { - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; n = fhc->write(buffer, length); @@ -387,7 +387,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int #endif n = 1; } else { - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; n = fhc->read(buffer, length); @@ -412,7 +412,7 @@ extern "C" int _isatty(FILEHANDLE fh) /* stdin, stdout and stderr should be tty */ if (fh < 3) return 1; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; int err = fhc->isatty(); @@ -436,7 +436,7 @@ int _lseek(FILEHANDLE fh, int offset, int whence) errno = EBADF; if (fh < 3) return 0; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; #if defined(__ARMCC_VERSION) @@ -451,7 +451,7 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh) { errno = EBADF; if (fh < 3) return 0; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; int err = fhc->sync(); @@ -467,7 +467,7 @@ extern "C" long PREFIX(_flen)(FILEHANDLE fh) { errno = EBADF; if (fh < 3) return 0; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; return fhc->size();