init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

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