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