mbed library sources

Committer:
ebrus
Date:
Wed Jul 27 18:35:32 2016 +0000
Revision:
0:0a673c671a56
4

Who changed what in which revision?

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