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