Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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