Initial commit
mbed-dev-master/platform/FileBase.cpp@0:bb348c97df44, 2020-09-16 (annotated)
- Committer:
- lypinator
- Date:
- Wed Sep 16 01:11:49 2020 +0000
- Revision:
- 0:bb348c97df44
Added PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lypinator | 0:bb348c97df44 | 1 | /* mbed Microcontroller Library |
lypinator | 0:bb348c97df44 | 2 | * Copyright (c) 2006-2013 ARM Limited |
lypinator | 0:bb348c97df44 | 3 | * |
lypinator | 0:bb348c97df44 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lypinator | 0:bb348c97df44 | 5 | * you may not use this file except in compliance with the License. |
lypinator | 0:bb348c97df44 | 6 | * You may obtain a copy of the License at |
lypinator | 0:bb348c97df44 | 7 | * |
lypinator | 0:bb348c97df44 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lypinator | 0:bb348c97df44 | 9 | * |
lypinator | 0:bb348c97df44 | 10 | * Unless required by applicable law or agreed to in writing, software |
lypinator | 0:bb348c97df44 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lypinator | 0:bb348c97df44 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lypinator | 0:bb348c97df44 | 13 | * See the License for the specific language governing permissions and |
lypinator | 0:bb348c97df44 | 14 | * limitations under the License. |
lypinator | 0:bb348c97df44 | 15 | */ |
lypinator | 0:bb348c97df44 | 16 | #include "platform/FileBase.h" |
lypinator | 0:bb348c97df44 | 17 | #include "platform/FileLike.h" |
lypinator | 0:bb348c97df44 | 18 | #include "platform/FileHandle.h" |
lypinator | 0:bb348c97df44 | 19 | |
lypinator | 0:bb348c97df44 | 20 | namespace mbed { |
lypinator | 0:bb348c97df44 | 21 | |
lypinator | 0:bb348c97df44 | 22 | FileBase *FileBase::_head = NULL; |
lypinator | 0:bb348c97df44 | 23 | SingletonPtr<PlatformMutex> FileBase::_mutex; |
lypinator | 0:bb348c97df44 | 24 | |
lypinator | 0:bb348c97df44 | 25 | FileBase::FileBase(const char *name, PathType t) : _next(NULL), |
lypinator | 0:bb348c97df44 | 26 | _name(name), |
lypinator | 0:bb348c97df44 | 27 | _path_type(t) |
lypinator | 0:bb348c97df44 | 28 | { |
lypinator | 0:bb348c97df44 | 29 | _mutex->lock(); |
lypinator | 0:bb348c97df44 | 30 | if (name != NULL) { |
lypinator | 0:bb348c97df44 | 31 | // put this object at head of the list |
lypinator | 0:bb348c97df44 | 32 | _next = _head; |
lypinator | 0:bb348c97df44 | 33 | _head = this; |
lypinator | 0:bb348c97df44 | 34 | } else { |
lypinator | 0:bb348c97df44 | 35 | _next = NULL; |
lypinator | 0:bb348c97df44 | 36 | } |
lypinator | 0:bb348c97df44 | 37 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 38 | } |
lypinator | 0:bb348c97df44 | 39 | |
lypinator | 0:bb348c97df44 | 40 | FileBase::~FileBase() |
lypinator | 0:bb348c97df44 | 41 | { |
lypinator | 0:bb348c97df44 | 42 | _mutex->lock(); |
lypinator | 0:bb348c97df44 | 43 | if (_name != NULL) { |
lypinator | 0:bb348c97df44 | 44 | // remove this object from the list |
lypinator | 0:bb348c97df44 | 45 | if (_head == this) { // first in the list, so just drop me |
lypinator | 0:bb348c97df44 | 46 | _head = _next; |
lypinator | 0:bb348c97df44 | 47 | } else { // find the object before me, then drop me |
lypinator | 0:bb348c97df44 | 48 | FileBase *p = _head; |
lypinator | 0:bb348c97df44 | 49 | while (p->_next != this) { |
lypinator | 0:bb348c97df44 | 50 | p = p->_next; |
lypinator | 0:bb348c97df44 | 51 | } |
lypinator | 0:bb348c97df44 | 52 | p->_next = _next; |
lypinator | 0:bb348c97df44 | 53 | } |
lypinator | 0:bb348c97df44 | 54 | } |
lypinator | 0:bb348c97df44 | 55 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 56 | |
lypinator | 0:bb348c97df44 | 57 | if (getPathType() == FilePathType) { |
lypinator | 0:bb348c97df44 | 58 | extern void remove_filehandle(FileHandle * file); |
lypinator | 0:bb348c97df44 | 59 | remove_filehandle(static_cast<FileHandle *>(static_cast<FileLike *>(this))); |
lypinator | 0:bb348c97df44 | 60 | } |
lypinator | 0:bb348c97df44 | 61 | } |
lypinator | 0:bb348c97df44 | 62 | |
lypinator | 0:bb348c97df44 | 63 | FileBase *FileBase::lookup(const char *name, unsigned int len) |
lypinator | 0:bb348c97df44 | 64 | { |
lypinator | 0:bb348c97df44 | 65 | _mutex->lock(); |
lypinator | 0:bb348c97df44 | 66 | FileBase *p = _head; |
lypinator | 0:bb348c97df44 | 67 | while (p != NULL) { |
lypinator | 0:bb348c97df44 | 68 | /* Check that p->_name matches name and is the correct length */ |
lypinator | 0:bb348c97df44 | 69 | if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) { |
lypinator | 0:bb348c97df44 | 70 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 71 | return p; |
lypinator | 0:bb348c97df44 | 72 | } |
lypinator | 0:bb348c97df44 | 73 | p = p->_next; |
lypinator | 0:bb348c97df44 | 74 | } |
lypinator | 0:bb348c97df44 | 75 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 76 | return NULL; |
lypinator | 0:bb348c97df44 | 77 | } |
lypinator | 0:bb348c97df44 | 78 | |
lypinator | 0:bb348c97df44 | 79 | FileBase *FileBase::get(int n) |
lypinator | 0:bb348c97df44 | 80 | { |
lypinator | 0:bb348c97df44 | 81 | _mutex->lock(); |
lypinator | 0:bb348c97df44 | 82 | FileBase *p = _head; |
lypinator | 0:bb348c97df44 | 83 | int m = 0; |
lypinator | 0:bb348c97df44 | 84 | while (p != NULL) { |
lypinator | 0:bb348c97df44 | 85 | if (m == n) { |
lypinator | 0:bb348c97df44 | 86 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 87 | return p; |
lypinator | 0:bb348c97df44 | 88 | } |
lypinator | 0:bb348c97df44 | 89 | |
lypinator | 0:bb348c97df44 | 90 | m++; |
lypinator | 0:bb348c97df44 | 91 | p = p->_next; |
lypinator | 0:bb348c97df44 | 92 | } |
lypinator | 0:bb348c97df44 | 93 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 94 | return NULL; |
lypinator | 0:bb348c97df44 | 95 | } |
lypinator | 0:bb348c97df44 | 96 | |
lypinator | 0:bb348c97df44 | 97 | const char *FileBase::getName(void) |
lypinator | 0:bb348c97df44 | 98 | { |
lypinator | 0:bb348c97df44 | 99 | // Constant read so no lock needed |
lypinator | 0:bb348c97df44 | 100 | return _name; |
lypinator | 0:bb348c97df44 | 101 | } |
lypinator | 0:bb348c97df44 | 102 | |
lypinator | 0:bb348c97df44 | 103 | PathType FileBase::getPathType(void) |
lypinator | 0:bb348c97df44 | 104 | { |
lypinator | 0:bb348c97df44 | 105 | // Constant read so no lock needed |
lypinator | 0:bb348c97df44 | 106 | return _path_type; |
lypinator | 0:bb348c97df44 | 107 | } |
lypinator | 0:bb348c97df44 | 108 | |
lypinator | 0:bb348c97df44 | 109 | } // namespace mbed |
lypinator | 0:bb348c97df44 | 110 |