mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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