mbed library sources

Dependents:   bare

Fork of mbed-src by mbed official

Committer:
bogdanm
Date:
Mon Aug 05 14:12:34 2013 +0300
Revision:
13:0645d8841f51
Parent:
10:3bc89ef62ce7
Update mbed sources to revision 64

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 13:0645d8841f51 1 /* mbed Microcontroller Library
bogdanm 13:0645d8841f51 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 13:0645d8841f51 3 *
bogdanm 13:0645d8841f51 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 13:0645d8841f51 5 * you may not use this file except in compliance with the License.
bogdanm 13:0645d8841f51 6 * You may obtain a copy of the License at
bogdanm 13:0645d8841f51 7 *
bogdanm 13:0645d8841f51 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 13:0645d8841f51 9 *
bogdanm 13:0645d8841f51 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 13:0645d8841f51 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 13:0645d8841f51 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 13:0645d8841f51 13 * See the License for the specific language governing permissions and
bogdanm 13:0645d8841f51 14 * limitations under the License.
bogdanm 13:0645d8841f51 15 */
bogdanm 13:0645d8841f51 16 #include "LocalFileSystem.h"
bogdanm 13:0645d8841f51 17
bogdanm 13:0645d8841f51 18 #if DEVICE_LOCALFILESYSTEM
bogdanm 13:0645d8841f51 19
bogdanm 13:0645d8841f51 20 #include "semihost_api.h"
bogdanm 13:0645d8841f51 21 #include <string.h>
bogdanm 13:0645d8841f51 22 #include <stdio.h>
bogdanm 13:0645d8841f51 23
bogdanm 13:0645d8841f51 24 namespace mbed {
bogdanm 13:0645d8841f51 25
bogdanm 13:0645d8841f51 26 /* Extension to FINFO type defined in RTL.h (in Keil RL) - adds 'create time'. */
bogdanm 13:0645d8841f51 27 typedef struct {
bogdanm 13:0645d8841f51 28 unsigned char hr; /* Hours [0..23] */
bogdanm 13:0645d8841f51 29 unsigned char min; /* Minutes [0..59] */
bogdanm 13:0645d8841f51 30 unsigned char sec; /* Seconds [0..59] */
bogdanm 13:0645d8841f51 31 unsigned char day; /* Day [1..31] */
bogdanm 13:0645d8841f51 32 unsigned char mon; /* Month [1..12] */
bogdanm 13:0645d8841f51 33 unsigned short year; /* Year [1980..2107] */
bogdanm 13:0645d8841f51 34 } FTIME;
bogdanm 13:0645d8841f51 35
bogdanm 13:0645d8841f51 36 typedef struct { /* File Search info record */
bogdanm 13:0645d8841f51 37 char name[32]; /* File name */
bogdanm 13:0645d8841f51 38 long size; /* File size in bytes */
bogdanm 13:0645d8841f51 39 int fileID; /* System File Identification */
bogdanm 13:0645d8841f51 40 FTIME create_time; /* Date & time file was created */
bogdanm 13:0645d8841f51 41 FTIME write_time; /* Date & time of last write */
bogdanm 13:0645d8841f51 42 } XFINFO;
bogdanm 13:0645d8841f51 43
bogdanm 13:0645d8841f51 44 #define RESERVED_FOR_USER_APPLICATIONS (0x100) /* 0x100 - 0x1ff */
bogdanm 13:0645d8841f51 45 #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0)
bogdanm 13:0645d8841f51 46
bogdanm 13:0645d8841f51 47 static int xffind (const char *pattern, XFINFO *info) {
bogdanm 13:0645d8841f51 48 unsigned param[4];
bogdanm 13:0645d8841f51 49
bogdanm 13:0645d8841f51 50 param[0] = (unsigned long)pattern;
bogdanm 13:0645d8841f51 51 param[1] = (unsigned long)strlen(pattern);
bogdanm 13:0645d8841f51 52 param[2] = (unsigned long)info;
bogdanm 13:0645d8841f51 53 param[3] = (unsigned long)sizeof(XFINFO);
bogdanm 13:0645d8841f51 54
bogdanm 13:0645d8841f51 55 return __semihost(USR_XFFIND, param);
bogdanm 13:0645d8841f51 56 }
bogdanm 13:0645d8841f51 57
bogdanm 13:0645d8841f51 58 #define OPEN_R 0
bogdanm 13:0645d8841f51 59 #define OPEN_B 1
bogdanm 13:0645d8841f51 60 #define OPEN_PLUS 2
bogdanm 13:0645d8841f51 61 #define OPEN_W 4
bogdanm 13:0645d8841f51 62 #define OPEN_A 8
bogdanm 13:0645d8841f51 63 #define OPEN_INVALID -1
bogdanm 13:0645d8841f51 64
bogdanm 13:0645d8841f51 65 int posix_to_semihost_open_flags(int flags) {
bogdanm 13:0645d8841f51 66 /* POSIX flags -> semihosting open mode */
bogdanm 13:0645d8841f51 67 int openmode;
bogdanm 13:0645d8841f51 68 if (flags & O_RDWR) {
bogdanm 13:0645d8841f51 69 /* a plus mode */
bogdanm 13:0645d8841f51 70 openmode = OPEN_PLUS;
bogdanm 13:0645d8841f51 71 if (flags & O_APPEND) {
bogdanm 13:0645d8841f51 72 openmode |= OPEN_A;
bogdanm 13:0645d8841f51 73 } else if (flags & O_TRUNC) {
bogdanm 13:0645d8841f51 74 openmode |= OPEN_W;
bogdanm 13:0645d8841f51 75 } else {
bogdanm 13:0645d8841f51 76 openmode |= OPEN_R;
bogdanm 13:0645d8841f51 77 }
bogdanm 13:0645d8841f51 78 } else if (flags & O_WRONLY) {
bogdanm 13:0645d8841f51 79 /* write or append */
bogdanm 13:0645d8841f51 80 if (flags & O_APPEND) {
bogdanm 13:0645d8841f51 81 openmode = OPEN_A;
bogdanm 13:0645d8841f51 82 } else {
bogdanm 13:0645d8841f51 83 openmode = OPEN_W;
bogdanm 13:0645d8841f51 84 }
bogdanm 13:0645d8841f51 85 } else if (flags == O_RDONLY) {
bogdanm 13:0645d8841f51 86 /* read mode */
bogdanm 13:0645d8841f51 87 openmode = OPEN_R;
bogdanm 13:0645d8841f51 88 } else {
bogdanm 13:0645d8841f51 89 /* invalid flags */
bogdanm 13:0645d8841f51 90 openmode = OPEN_INVALID;
bogdanm 13:0645d8841f51 91 }
bogdanm 13:0645d8841f51 92
bogdanm 13:0645d8841f51 93 return openmode;
bogdanm 13:0645d8841f51 94 }
bogdanm 13:0645d8841f51 95
bogdanm 13:0645d8841f51 96 FILEHANDLE local_file_open(const char* name, int flags) {
bogdanm 13:0645d8841f51 97 int openmode = posix_to_semihost_open_flags(flags);
bogdanm 13:0645d8841f51 98 if (openmode == OPEN_INVALID) {
bogdanm 13:0645d8841f51 99 return (FILEHANDLE)NULL;
bogdanm 13:0645d8841f51 100 }
bogdanm 13:0645d8841f51 101
bogdanm 13:0645d8841f51 102 FILEHANDLE fh = semihost_open(name, openmode);
bogdanm 13:0645d8841f51 103 if (fh == -1) {
bogdanm 13:0645d8841f51 104 return (FILEHANDLE)NULL;
bogdanm 13:0645d8841f51 105 }
bogdanm 13:0645d8841f51 106
bogdanm 13:0645d8841f51 107 return fh;
bogdanm 13:0645d8841f51 108 }
bogdanm 13:0645d8841f51 109
bogdanm 13:0645d8841f51 110 LocalFileHandle::LocalFileHandle(FILEHANDLE fh) {
bogdanm 13:0645d8841f51 111 _fh = fh;
bogdanm 13:0645d8841f51 112 pos = 0;
bogdanm 13:0645d8841f51 113 }
bogdanm 13:0645d8841f51 114
bogdanm 13:0645d8841f51 115 int LocalFileHandle::close() {
bogdanm 13:0645d8841f51 116 int retval = semihost_close(_fh);
bogdanm 13:0645d8841f51 117 delete this;
bogdanm 13:0645d8841f51 118 return retval;
bogdanm 13:0645d8841f51 119 }
bogdanm 13:0645d8841f51 120
bogdanm 13:0645d8841f51 121 ssize_t LocalFileHandle::write(const void *buffer, size_t length) {
bogdanm 13:0645d8841f51 122 ssize_t n = semihost_write(_fh, (const unsigned char*)buffer, length, 0); // number of characters not written
bogdanm 13:0645d8841f51 123 n = length - n; // number of characters written
bogdanm 13:0645d8841f51 124 pos += n;
bogdanm 13:0645d8841f51 125 return n;
bogdanm 13:0645d8841f51 126 }
bogdanm 13:0645d8841f51 127
bogdanm 13:0645d8841f51 128 ssize_t LocalFileHandle::read(void *buffer, size_t length) {
bogdanm 13:0645d8841f51 129 ssize_t n = semihost_read(_fh, (unsigned char*)buffer, length, 0); // number of characters not read
bogdanm 13:0645d8841f51 130 n = length - n; // number of characters read
bogdanm 13:0645d8841f51 131 pos += n;
bogdanm 13:0645d8841f51 132 return n;
bogdanm 13:0645d8841f51 133 }
bogdanm 13:0645d8841f51 134
bogdanm 13:0645d8841f51 135 int LocalFileHandle::isatty() {
bogdanm 13:0645d8841f51 136 return semihost_istty(_fh);
bogdanm 13:0645d8841f51 137 }
bogdanm 13:0645d8841f51 138
bogdanm 13:0645d8841f51 139 off_t LocalFileHandle::lseek(off_t position, int whence) {
bogdanm 13:0645d8841f51 140 if (whence == SEEK_CUR) {
bogdanm 13:0645d8841f51 141 position += pos;
bogdanm 13:0645d8841f51 142 } else if (whence == SEEK_END) {
bogdanm 13:0645d8841f51 143 position += semihost_flen(_fh);
bogdanm 13:0645d8841f51 144 } /* otherwise SEEK_SET, so position is fine */
bogdanm 13:0645d8841f51 145
bogdanm 13:0645d8841f51 146 /* Always seems to return -1, so just ignore for now. */
bogdanm 13:0645d8841f51 147 semihost_seek(_fh, position);
bogdanm 13:0645d8841f51 148 pos = position;
bogdanm 13:0645d8841f51 149 return position;
bogdanm 13:0645d8841f51 150 }
bogdanm 13:0645d8841f51 151
bogdanm 13:0645d8841f51 152 int LocalFileHandle::fsync() {
bogdanm 13:0645d8841f51 153 return semihost_ensure(_fh);
bogdanm 13:0645d8841f51 154 }
bogdanm 13:0645d8841f51 155
bogdanm 13:0645d8841f51 156 off_t LocalFileHandle::flen() {
bogdanm 13:0645d8841f51 157 return semihost_flen(_fh);
bogdanm 13:0645d8841f51 158 }
bogdanm 13:0645d8841f51 159
bogdanm 13:0645d8841f51 160 class LocalDirHandle : public DirHandle {
bogdanm 13:0645d8841f51 161
bogdanm 13:0645d8841f51 162 public:
bogdanm 13:0645d8841f51 163 struct dirent cur_entry;
bogdanm 13:0645d8841f51 164 XFINFO info;
bogdanm 13:0645d8841f51 165
bogdanm 13:0645d8841f51 166 LocalDirHandle() {
bogdanm 13:0645d8841f51 167 info.fileID = 0;
bogdanm 13:0645d8841f51 168 }
bogdanm 13:0645d8841f51 169
bogdanm 13:0645d8841f51 170 virtual int closedir() {
bogdanm 13:0645d8841f51 171 delete this;
bogdanm 13:0645d8841f51 172 return 0;
bogdanm 13:0645d8841f51 173 }
bogdanm 13:0645d8841f51 174
bogdanm 13:0645d8841f51 175 virtual struct dirent *readdir() {
bogdanm 13:0645d8841f51 176 if (xffind("*", &info)!=0) {
bogdanm 13:0645d8841f51 177 return NULL;
bogdanm 13:0645d8841f51 178 }
bogdanm 13:0645d8841f51 179 memcpy(cur_entry.d_name, info.name, sizeof(info.name));
bogdanm 13:0645d8841f51 180 return &cur_entry;
bogdanm 13:0645d8841f51 181 }
bogdanm 13:0645d8841f51 182
bogdanm 13:0645d8841f51 183 virtual void rewinddir() {
bogdanm 13:0645d8841f51 184 info.fileID = 0;
bogdanm 13:0645d8841f51 185 }
bogdanm 13:0645d8841f51 186
bogdanm 13:0645d8841f51 187 virtual off_t telldir() {
bogdanm 13:0645d8841f51 188 return info.fileID;
bogdanm 13:0645d8841f51 189 }
bogdanm 13:0645d8841f51 190
bogdanm 13:0645d8841f51 191 virtual void seekdir(off_t offset) {
bogdanm 13:0645d8841f51 192 info.fileID = offset;
bogdanm 13:0645d8841f51 193 }
bogdanm 13:0645d8841f51 194 };
bogdanm 13:0645d8841f51 195
bogdanm 13:0645d8841f51 196 FileHandle *LocalFileSystem::open(const char* name, int flags) {
bogdanm 13:0645d8841f51 197 /* reject filenames with / in them */
bogdanm 13:0645d8841f51 198 for (const char *tmp = name; *tmp; tmp++) {
bogdanm 13:0645d8841f51 199 if (*tmp == '/') {
bogdanm 13:0645d8841f51 200 return NULL;
bogdanm 13:0645d8841f51 201 }
bogdanm 13:0645d8841f51 202 }
bogdanm 13:0645d8841f51 203
bogdanm 13:0645d8841f51 204 int openmode = posix_to_semihost_open_flags(flags);
bogdanm 13:0645d8841f51 205 if (openmode == OPEN_INVALID) {
bogdanm 13:0645d8841f51 206 return NULL;
bogdanm 13:0645d8841f51 207 }
bogdanm 13:0645d8841f51 208
bogdanm 13:0645d8841f51 209 FILEHANDLE fh = semihost_open(name, openmode);
bogdanm 13:0645d8841f51 210 if (fh == -1) {
bogdanm 13:0645d8841f51 211 return NULL;
bogdanm 13:0645d8841f51 212 }
bogdanm 13:0645d8841f51 213 return new LocalFileHandle(fh);
bogdanm 13:0645d8841f51 214 }
bogdanm 13:0645d8841f51 215
bogdanm 13:0645d8841f51 216 int LocalFileSystem::remove(const char *filename) {
bogdanm 13:0645d8841f51 217 return semihost_remove(filename);
bogdanm 13:0645d8841f51 218 }
bogdanm 13:0645d8841f51 219
bogdanm 13:0645d8841f51 220 DirHandle *LocalFileSystem::opendir(const char *name) {
bogdanm 13:0645d8841f51 221 return new LocalDirHandle();
bogdanm 13:0645d8841f51 222 }
bogdanm 13:0645d8841f51 223
bogdanm 13:0645d8841f51 224 } // namespace mbed
bogdanm 13:0645d8841f51 225
bogdanm 13:0645d8841f51 226 #endif