USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Sat Dec 16 10:26:48 2017 +0000
Revision:
11:b3f2a8bdac4d
Parent:
10:41552d038a69
A copy for D.S;

Who changed what in which revision?

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