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