Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /* mbed Microcontroller Library
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2015 ARM Limited
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 5 * you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 6 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 7 *
marcozecchini 0:9fca2b23d0ba 8 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 9 *
marcozecchini 0:9fca2b23d0ba 10 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 13 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 14 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 15 */
marcozecchini 0:9fca2b23d0ba 16
marcozecchini 0:9fca2b23d0ba 17 #include "Dir.h"
marcozecchini 0:9fca2b23d0ba 18 #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 19 #include <errno.h>
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21
marcozecchini 0:9fca2b23d0ba 22 Dir::Dir()
marcozecchini 0:9fca2b23d0ba 23 : _fs(0), _dir(0)
marcozecchini 0:9fca2b23d0ba 24 {
marcozecchini 0:9fca2b23d0ba 25 }
marcozecchini 0:9fca2b23d0ba 26
marcozecchini 0:9fca2b23d0ba 27 Dir::Dir(FileSystem *fs, const char *path)
marcozecchini 0:9fca2b23d0ba 28 : _fs(0), _dir(0)
marcozecchini 0:9fca2b23d0ba 29 {
marcozecchini 0:9fca2b23d0ba 30 open(fs, path);
marcozecchini 0:9fca2b23d0ba 31 }
marcozecchini 0:9fca2b23d0ba 32
marcozecchini 0:9fca2b23d0ba 33 Dir::~Dir()
marcozecchini 0:9fca2b23d0ba 34 {
marcozecchini 0:9fca2b23d0ba 35 if (_fs) {
marcozecchini 0:9fca2b23d0ba 36 close();
marcozecchini 0:9fca2b23d0ba 37 }
marcozecchini 0:9fca2b23d0ba 38 }
marcozecchini 0:9fca2b23d0ba 39
marcozecchini 0:9fca2b23d0ba 40 int Dir::open(FileSystem *fs, const char *path)
marcozecchini 0:9fca2b23d0ba 41 {
marcozecchini 0:9fca2b23d0ba 42 if (_fs) {
marcozecchini 0:9fca2b23d0ba 43 return -EINVAL;
marcozecchini 0:9fca2b23d0ba 44 }
marcozecchini 0:9fca2b23d0ba 45
marcozecchini 0:9fca2b23d0ba 46 int err = fs->dir_open(&_dir, path);
marcozecchini 0:9fca2b23d0ba 47 if (!err) {
marcozecchini 0:9fca2b23d0ba 48 _fs = fs;
marcozecchini 0:9fca2b23d0ba 49 }
marcozecchini 0:9fca2b23d0ba 50
marcozecchini 0:9fca2b23d0ba 51 return err;
marcozecchini 0:9fca2b23d0ba 52 }
marcozecchini 0:9fca2b23d0ba 53
marcozecchini 0:9fca2b23d0ba 54 int Dir::close()
marcozecchini 0:9fca2b23d0ba 55 {
marcozecchini 0:9fca2b23d0ba 56 if (!_fs) {
marcozecchini 0:9fca2b23d0ba 57 return -EINVAL;
marcozecchini 0:9fca2b23d0ba 58 }
marcozecchini 0:9fca2b23d0ba 59
marcozecchini 0:9fca2b23d0ba 60 int err = _fs->dir_close(_dir);
marcozecchini 0:9fca2b23d0ba 61 _fs = 0;
marcozecchini 0:9fca2b23d0ba 62 return err;
marcozecchini 0:9fca2b23d0ba 63 }
marcozecchini 0:9fca2b23d0ba 64
marcozecchini 0:9fca2b23d0ba 65 ssize_t Dir::read(struct dirent *ent)
marcozecchini 0:9fca2b23d0ba 66 {
marcozecchini 0:9fca2b23d0ba 67 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 68 memset(ent, 0, sizeof(struct dirent));
marcozecchini 0:9fca2b23d0ba 69 return _fs->dir_read(_dir, ent);
marcozecchini 0:9fca2b23d0ba 70 }
marcozecchini 0:9fca2b23d0ba 71
marcozecchini 0:9fca2b23d0ba 72 void Dir::seek(off_t offset)
marcozecchini 0:9fca2b23d0ba 73 {
marcozecchini 0:9fca2b23d0ba 74 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 75 return _fs->dir_seek(_dir, offset);
marcozecchini 0:9fca2b23d0ba 76 }
marcozecchini 0:9fca2b23d0ba 77
marcozecchini 0:9fca2b23d0ba 78 off_t Dir::tell()
marcozecchini 0:9fca2b23d0ba 79 {
marcozecchini 0:9fca2b23d0ba 80 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 81 return _fs->dir_tell(_dir);
marcozecchini 0:9fca2b23d0ba 82 }
marcozecchini 0:9fca2b23d0ba 83
marcozecchini 0:9fca2b23d0ba 84 void Dir::rewind()
marcozecchini 0:9fca2b23d0ba 85 {
marcozecchini 0:9fca2b23d0ba 86 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 87 return _fs->dir_rewind(_dir);
marcozecchini 0:9fca2b23d0ba 88 }
marcozecchini 0:9fca2b23d0ba 89
marcozecchini 0:9fca2b23d0ba 90 size_t Dir::size()
marcozecchini 0:9fca2b23d0ba 91 {
marcozecchini 0:9fca2b23d0ba 92 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 93 return _fs->dir_size(_dir);
marcozecchini 0:9fca2b23d0ba 94 }