mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Sep 30 17:00:09 2015 +0100
Revision:
635:a11c0372f0ba
Parent:
212:34d62c0b2af6
Synchronized with git revision d29c98dae61be0946ddf3a3c641c7726056f9452

Full URL: https://github.com/mbedmicro/mbed/commit/d29c98dae61be0946ddf3a3c641c7726056f9452/

Added support for SAMW25

Who changed what in which revision?

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