mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
181:57724642e740
Parent:
167:e84263d55307
Child:
187:0387e8f68319
mbed-dev library. Release version 159.

Who changed what in which revision?

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