mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
Diff: platform/mbed_retarget.h
- Revision:
- 184:08ed48f1de7f
- Parent:
- 180:96ed750bd169
- Child:
- 186:707f6e361f3e
--- a/platform/mbed_retarget.h Tue Mar 20 17:01:51 2018 +0000 +++ b/platform/mbed_retarget.h Thu Apr 19 17:12:19 2018 +0100 @@ -21,6 +21,8 @@ #if __cplusplus #include <cstdio> +#else +#include <stdio.h> #endif //__cplusplus #include <stdint.h> #include <stddef.h> @@ -30,6 +32,8 @@ * target embedded systems */ typedef signed int ssize_t; ///< Signed size type, usually encodes negative errors typedef signed long off_t; ///< Offset in a data stream +typedef unsigned int nfds_t; ///< Number of file descriptors +typedef unsigned long long fsblkcnt_t; ///< Count of file system blocks #if defined(__ARMCC_VERSION) || !defined(__GNUC__) typedef unsigned int mode_t; ///< Mode for opening files typedef unsigned int dev_t; ///< Device ID type @@ -50,6 +54,10 @@ #define NAME_MAX 255 ///< Maximum size of a name in a file path +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + #include <time.h> /** \addtogroup platform */ @@ -62,32 +70,57 @@ /* DIR declarations must also be here */ #if __cplusplus namespace mbed { - + class FileHandle; class DirHandle; -std::FILE *mbed_fdopen(FileHandle *fh, const char *mode); + +/** Targets may implement this to change stdin, stdout, stderr. + * + * If the application hasn't provided mbed_override_console, this is called + * to give the target a chance to specify a FileHandle for the console. + * + * If this is not provided or returns NULL, the console will be: + * - UARTSerial if configuration option "platform.stdio-buffered-serial" is + * true and the target has DEVICE_SERIAL; + * - Raw HAL serial via serial_getc and serial_putc if + * "platform.stdio-buffered-serial" is false and the target has DEVICE_SERIAL; + * - stdout/stderr will be a sink and stdin will input a stream of 0s if the + * target does not have DEVICE_SERIAL. + * + * @param fd file descriptor - STDIN_FILENO, STDOUT_FILENO or STDERR_FILENO + * @return pointer to FileHandle to override normal stream otherwise NULL + */ +FileHandle* mbed_target_override_console(int fd); + +/** Applications may implement this to change stdin, stdout, stderr. + * + * This hook gives the application a chance to specify a custom FileHandle + * for the console. + * + * If this is not provided or returns NULL, the console will be specified + * by mbed_target_override_console, else will default to serial - see + * mbed_target_override_console for more details. + * + * Example: + * @code + * FileHandle* mbed::mbed_override_console(int) { + * static UARTSerial my_serial(D0, D1); + * return &my_serial; + * } + * @endcode + + * @param fd file descriptor - STDIN_FILENO, STDOUT_FILENO or STDERR_FILENO + * @return pointer to FileHandle to override normal stream otherwise NULL + */ +FileHandle* mbed_override_console(int fd); } + typedef mbed::DirHandle DIR; #else typedef struct Dir DIR; #endif -#if __cplusplus -extern "C" { -#endif - DIR *opendir(const char*); - struct dirent *readdir(DIR *); - int closedir(DIR*); - void rewinddir(DIR*); - long telldir(DIR*); - void seekdir(DIR*, long); - int mkdir(const char *name, mode_t n); -#if __cplusplus -}; -#endif - - /* The intent of this section is to unify the errno error values to match * the POSIX definitions for the GCC_ARM, ARMCC and IAR compilers. This is * necessary because the ARMCC/IAR errno.h, or sys/stat.h are missing some @@ -389,7 +422,7 @@ #define S_IXUSR 0000100 ///< execute/search permission, owner #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP) #define S_IRGRP 0000040 ///< read permission, group -#define S_IWGRP 0000020 ///< write permission, grougroup +#define S_IWGRP 0000020 ///< write permission, group #define S_IXGRP 0000010 ///< execute/search permission, group #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH) #define S_IROTH 0000004 ///< read permission, other @@ -415,16 +448,20 @@ time_t st_ctime; ///< Time of last status change }; -#if __cplusplus -extern "C" { -#endif - int stat(const char *path, struct stat *st); -#if __cplusplus +struct statvfs { + unsigned long f_bsize; ///< Filesystem block size + unsigned long f_frsize; ///< Fragment size (block size) + + fsblkcnt_t f_blocks; ///< Number of blocks + fsblkcnt_t f_bfree; ///< Number of free blocks + fsblkcnt_t f_bavail; ///< Number of free blocks for unprivileged users + + unsigned long f_fsid; ///< Filesystem ID + + unsigned long f_namemax; ///< Maximum filename length }; -#endif - -/* The following are dirent.h definitions are declared here to garuntee +/* The following are dirent.h definitions are declared here to guarantee * consistency where structure may be different with different toolchains */ struct dirent { @@ -443,6 +480,78 @@ DT_SOCK, ///< This is a UNIX domain socket. }; +struct pollfd { + int fd; + short events; + short revents; +}; + +/* POSIX-compatible I/O functions */ +#if __cplusplus +extern "C" { +#endif + int open(const char *path, int oflag, ...); +#ifndef __IAR_SYSTEMS_ICC__ /* IAR provides fdopen itself */ +#if __cplusplus + std::FILE* fdopen(int fildes, const char *mode); +#else + FILE* fdopen(int fildes, const char *mode); +#endif +#endif + ssize_t write(int fildes, const void *buf, size_t nbyte); + ssize_t read(int fildes, void *buf, size_t nbyte); + off_t lseek(int fildes, off_t offset, int whence); + int isatty(int fildes); + int fsync(int fildes); + int fstat(int fh, struct stat *st); + int poll(struct pollfd fds[], nfds_t nfds, int timeout); + int close(int fildes); + int stat(const char *path, struct stat *st); + int statvfs(const char *path, struct statvfs *buf); + DIR *opendir(const char*); + struct dirent *readdir(DIR *); + int closedir(DIR*); + void rewinddir(DIR*); + long telldir(DIR*); + void seekdir(DIR*, long); + int mkdir(const char *name, mode_t n); +#if __cplusplus +}; // extern "C" + +namespace mbed { + +/** This call is an analogue to POSIX fdopen(). + * + * It associates a C stream to an already-opened FileHandle, to allow you to + * use C printf/scanf/fwrite etc. The provided FileHandle must remain open - + * it will be closed by the C library when fclose(FILE) is called. + * + * The net effect is fdopen(bind_to_fd(fh), mode), with error handling. + * + * @param fh a pointer to an opened FileHandle + * @param mode operation upon the file descriptor, e.g., "w+" + * + * @returns a pointer to FILE + */ +std::FILE* fdopen(mbed::FileHandle *fh, const char *mode); + +/** Bind an mbed FileHandle to a POSIX file descriptor + * + * This is similar to fdopen, but only operating at the POSIX layer - it + * associates a POSIX integer file descriptor with a FileHandle, to allow you + * to use POSIX read/write calls etc. The provided FileHandle must remain open - + * it will be closed when close(int) is called. + * + * @param fh a pointer to an opened FileHandle + * + * @return an integer file descriptor, or negative if no descriptors available + */ +int bind_to_fd(mbed::FileHandle *fh); + +} // namespace mbed + +#endif // __cplusplus + /**@}*/ /**@}*/