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