PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
5:ea7377f3d1af
added fix for directrectangle()

Who changed what in which revision?

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