forked

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_retarget.h Source File

mbed_retarget.h

00001 /*
00002  * mbed Microcontroller Library
00003  * Copyright (c) 2006-2016 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  */
00018 
00019 #ifndef RETARGET_H
00020 #define RETARGET_H
00021 
00022 #if __cplusplus
00023 #include <cstdio>
00024 #endif //__cplusplus
00025 #include <stdint.h>
00026 #include <stddef.h>
00027 
00028 /* We can get the following standard types from sys/types for gcc, but we
00029  * need to define the types ourselves for the other compilers that normally
00030  * target embedded systems */
00031 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
00032 typedef int ssize_t;    ///< Signed size type, usually encodes negative errors
00033 typedef long off_t;     ///< Offset in a data stream
00034 typedef int mode_t;     ///< Mode for opening files
00035 
00036 #define O_RDONLY 0
00037 #define O_WRONLY 1
00038 #define O_RDWR   2
00039 #define O_CREAT  0x0200
00040 #define O_TRUNC  0x0400
00041 #define O_APPEND 0x0008
00042 
00043 #define NAME_MAX 255    ///< Maximum size of a name in a file path
00044 
00045 #else
00046 #include <sys/fcntl.h>
00047 #include <sys/types.h>
00048 #include <sys/syslimits.h>
00049 #endif
00050 
00051 
00052 /* DIR declarations must also be here */
00053 #if __cplusplus
00054 namespace mbed {
00055 class FileHandle;
00056 class DirHandle;
00057 std::FILE *mbed_fdopen(FileHandle *fh, const char *mode);
00058 }
00059 typedef mbed::DirHandle DIR;
00060 #else
00061 typedef struct Dir DIR;
00062 #endif
00063 
00064 #if __cplusplus
00065 extern "C" {
00066 #endif
00067     DIR *opendir(const char*);
00068     struct dirent *readdir(DIR *);
00069     int closedir(DIR*);
00070     void rewinddir(DIR*);
00071     long telldir(DIR*);
00072     void seekdir(DIR*, long);
00073     int mkdir(const char *name, mode_t n);
00074 #if __cplusplus
00075 };
00076 #endif
00077 
00078 
00079 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
00080 /* The intent of this section is to unify the errno error values to match
00081  * the POSIX definitions for the GCC_ARM, ARMCC and IAR compilers. This is
00082  * necessary because the ARMCC/IAR errno.h, or sys/stat.h are missing some
00083  * symbol definitions used by the POSIX filesystem API to return errno codes.
00084  * Note also that ARMCC errno.h defines some symbol values differently from
00085  * the GCC_ARM/IAR/standard POSIX definitions. The definitions guard against
00086  * this and future changes by changing the symbol definition as shown below. */
00087 #undef ENOENT
00088 #define ENOENT      2       /* No such file or directory. */
00089 
00090 #undef EIO
00091 #define EIO         5       /* I/O error */
00092 
00093 #undef ENXIO
00094 #define ENXIO       6       /* No such device or address */
00095 
00096 #undef ENOEXEC
00097 #define ENOEXEC     8       /* Exec format error */
00098 
00099 #undef EBADF
00100 #define EBADF       9       /* Bad file number */
00101 
00102 #undef EAGAIN
00103 #define EAGAIN      11      /* Resource unavailable, try again */
00104 
00105 #undef EWOULDBLOCK
00106 #define EWOULDBLOCK EAGAIN  /* Operation would block */
00107 
00108 #undef ENOMEM
00109 #define ENOMEM      12      /* Not enough space */
00110 
00111 #undef EACCES
00112 #define EACCES      13      /* Permission denied */
00113 
00114 #undef EFAULT
00115 #define EFAULT      14      /* Bad address */
00116 
00117 #undef EEXIST
00118 #define EEXIST      17      /* File exists */
00119 
00120 #undef EXDEV
00121 #define EXDEV       18      /* Cross-device link */
00122 
00123 #undef EINVAL
00124 #define EINVAL      22      /* Invalid argument */
00125 
00126 #undef ENFILE
00127 #define ENFILE      23      /* Too many open files in system */
00128 
00129 #undef EMFILE
00130 #define EMFILE      24      /* File descriptor value too large */
00131 
00132 #undef ESPIPE
00133 #define ESPIPE      29      /* Invalid seek */
00134 
00135 #undef ENOSYS
00136 #define ENOSYS      38      /* Function not implemented */
00137 
00138 #undef EOVERFLOW
00139 #define EOVERFLOW   75      /* Value too large to be stored in data type */
00140 
00141 /* Missing stat.h defines.
00142  * The following are sys/stat.h definitions not currently present in the ARMCC
00143  * errno.h. Note, ARMCC errno.h defines some symbol values differing from
00144  * GCC_ARM/IAR/standard POSIX definitions. Guard against this and future
00145  * changes by changing the symbol definition for filesystem use. */
00146 #define     _IFDIR  0040000 /* directory */
00147 #define     _IFREG  0100000 /* regular */
00148 
00149 #define S_IFDIR     _IFDIR
00150 #define S_IFREG     _IFREG
00151 
00152 #define S_IRWXU     (S_IRUSR | S_IWUSR | S_IXUSR)
00153 #define     S_IRUSR 0000400 /* read permission, owner */
00154 #define     S_IWUSR 0000200 /* write permission, owner */
00155 #define     S_IXUSR 0000100/* execute/search permission, owner */
00156 #define S_IRWXG     (S_IRGRP | S_IWGRP | S_IXGRP)
00157 #define     S_IRGRP 0000040 /* read permission, group */
00158 #define     S_IWGRP 0000020 /* write permission, grougroup */
00159 #define     S_IXGRP 0000010/* execute/search permission, group */
00160 #define S_IRWXO     (S_IROTH | S_IWOTH | S_IXOTH)
00161 #define     S_IROTH 0000004 /* read permission, other */
00162 #define     S_IWOTH 0000002 /* write permission, other */
00163 #define     S_IXOTH 0000001/* execute/search permission, other */
00164 
00165 #endif /* defined(__ARMCC_VERSION) || defined(__ICCARM__) */
00166 
00167 
00168 /* The following are dirent.h definitions are declared here to garuntee
00169  * consistency where structure may be different with different toolchains */
00170 struct dirent {
00171     char d_name[NAME_MAX+1];
00172     uint8_t d_type;
00173 };
00174 
00175 enum {
00176     DT_UNKNOWN, // The file type could not be determined.
00177     DT_FIFO,    // This is a named pipe (FIFO).
00178     DT_CHR,     // This is a character device.
00179     DT_DIR,     // This is a directory.
00180     DT_BLK,     // This is a block device.
00181     DT_REG,     // This is a regular file.
00182     DT_LNK,     // This is a symbolic link.
00183     DT_SOCK,    // This is a UNIX domain socket.
00184 };
00185 
00186 #endif /* RETARGET_H */