BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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