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