mbed-dev-f303

Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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