mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:f782d9c66c49 1 /* mbed Microcontroller Library
dkato 0:f782d9c66c49 2 * Copyright (c) 2006-2013 ARM Limited
dkato 0:f782d9c66c49 3 *
dkato 0:f782d9c66c49 4 * Licensed under the Apache License, Version 2.0 (the "License");
dkato 0:f782d9c66c49 5 * you may not use this file except in compliance with the License.
dkato 0:f782d9c66c49 6 * You may obtain a copy of the License at
dkato 0:f782d9c66c49 7 *
dkato 0:f782d9c66c49 8 * http://www.apache.org/licenses/LICENSE-2.0
dkato 0:f782d9c66c49 9 *
dkato 0:f782d9c66c49 10 * Unless required by applicable law or agreed to in writing, software
dkato 0:f782d9c66c49 11 * distributed under the License is distributed on an "AS IS" BASIS,
dkato 0:f782d9c66c49 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dkato 0:f782d9c66c49 13 * See the License for the specific language governing permissions and
dkato 0:f782d9c66c49 14 * limitations under the License.
dkato 0:f782d9c66c49 15 */
dkato 0:f782d9c66c49 16 #include "drivers/FileSystemLike.h"
dkato 0:f782d9c66c49 17
dkato 0:f782d9c66c49 18 namespace mbed {
dkato 0:f782d9c66c49 19
dkato 0:f782d9c66c49 20 class BaseDirHandle : public DirHandle {
dkato 0:f782d9c66c49 21 public:
dkato 0:f782d9c66c49 22 /*
dkato 0:f782d9c66c49 23 We keep track of our current location as the n'th object in the
dkato 0:f782d9c66c49 24 FileSystemLike list. Using a Base* instead would cause problems if that
dkato 0:f782d9c66c49 25 object were to be destroyed between readdirs.
dkato 0:f782d9c66c49 26 Using this method does mean though that destroying/creating objects can
dkato 0:f782d9c66c49 27 give unusual results from readdir.
dkato 0:f782d9c66c49 28 */
dkato 0:f782d9c66c49 29 off_t n;
dkato 0:f782d9c66c49 30 struct dirent cur_entry;
dkato 0:f782d9c66c49 31
dkato 0:f782d9c66c49 32 BaseDirHandle() : DirHandle(0), n(0), cur_entry() {
dkato 0:f782d9c66c49 33 }
dkato 0:f782d9c66c49 34
dkato 0:f782d9c66c49 35 virtual int closedir() {
dkato 0:f782d9c66c49 36 // No lock can be used in destructor
dkato 0:f782d9c66c49 37 delete this;
dkato 0:f782d9c66c49 38 return 0;
dkato 0:f782d9c66c49 39 }
dkato 0:f782d9c66c49 40
dkato 0:f782d9c66c49 41 virtual struct dirent *readdir() {
dkato 0:f782d9c66c49 42 lock();
dkato 0:f782d9c66c49 43 FileBase *ptr = FileBase::get(n);
dkato 0:f782d9c66c49 44 if (ptr == NULL) {
dkato 0:f782d9c66c49 45 unlock();
dkato 0:f782d9c66c49 46 return NULL;
dkato 0:f782d9c66c49 47 }
dkato 0:f782d9c66c49 48
dkato 0:f782d9c66c49 49 /* Increment n, so next readdir gets the next item */
dkato 0:f782d9c66c49 50 n++;
dkato 0:f782d9c66c49 51
dkato 0:f782d9c66c49 52 /* Setup cur entry and return a pointer to it */
dkato 0:f782d9c66c49 53 std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX);
dkato 0:f782d9c66c49 54 unlock();
dkato 0:f782d9c66c49 55 return &cur_entry;
dkato 0:f782d9c66c49 56 }
dkato 0:f782d9c66c49 57
dkato 0:f782d9c66c49 58 virtual off_t telldir() {
dkato 0:f782d9c66c49 59 lock();
dkato 0:f782d9c66c49 60 off_t offset = n;
dkato 0:f782d9c66c49 61 unlock();
dkato 0:f782d9c66c49 62 return offset;
dkato 0:f782d9c66c49 63 }
dkato 0:f782d9c66c49 64
dkato 0:f782d9c66c49 65 virtual void seekdir(off_t offset) {
dkato 0:f782d9c66c49 66 lock();
dkato 0:f782d9c66c49 67 n = offset;
dkato 0:f782d9c66c49 68 unlock();
dkato 0:f782d9c66c49 69 }
dkato 0:f782d9c66c49 70
dkato 0:f782d9c66c49 71 virtual void rewinddir() {
dkato 0:f782d9c66c49 72 lock();
dkato 0:f782d9c66c49 73 n = 0;
dkato 0:f782d9c66c49 74 unlock();
dkato 0:f782d9c66c49 75 }
dkato 0:f782d9c66c49 76
dkato 0:f782d9c66c49 77 protected:
dkato 0:f782d9c66c49 78 PlatformMutex _mutex;
dkato 0:f782d9c66c49 79
dkato 0:f782d9c66c49 80 virtual void lock() {
dkato 0:f782d9c66c49 81 _mutex.lock();
dkato 0:f782d9c66c49 82 }
dkato 0:f782d9c66c49 83
dkato 0:f782d9c66c49 84 virtual void unlock() {
dkato 0:f782d9c66c49 85 _mutex.unlock();
dkato 0:f782d9c66c49 86 }
dkato 0:f782d9c66c49 87 };
dkato 0:f782d9c66c49 88
dkato 0:f782d9c66c49 89 FileSystemLike::FileSystemLike(const char *name) : FileBase(name, FileSystemPathType) {
dkato 0:f782d9c66c49 90
dkato 0:f782d9c66c49 91 }
dkato 0:f782d9c66c49 92
dkato 0:f782d9c66c49 93 FileSystemLike::~FileSystemLike() {
dkato 0:f782d9c66c49 94
dkato 0:f782d9c66c49 95 }
dkato 0:f782d9c66c49 96
dkato 0:f782d9c66c49 97 DirHandle *FileSystemLike::opendir() {
dkato 0:f782d9c66c49 98 return new BaseDirHandle();
dkato 0:f782d9c66c49 99 }
dkato 0:f782d9c66c49 100
dkato 0:f782d9c66c49 101 } // namespace mbed