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 "File.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 File::File()
marcozecchini 0:9fca2b23d0ba 23 : _fs(0), _file(0)
marcozecchini 0:9fca2b23d0ba 24 {
marcozecchini 0:9fca2b23d0ba 25 }
marcozecchini 0:9fca2b23d0ba 26
marcozecchini 0:9fca2b23d0ba 27 File::File(FileSystem *fs, const char *path, int flags)
marcozecchini 0:9fca2b23d0ba 28 : _fs(0), _file(0)
marcozecchini 0:9fca2b23d0ba 29 {
marcozecchini 0:9fca2b23d0ba 30 open(fs, path, flags);
marcozecchini 0:9fca2b23d0ba 31 }
marcozecchini 0:9fca2b23d0ba 32
marcozecchini 0:9fca2b23d0ba 33 File::~File()
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 File::open(FileSystem *fs, const char *path, int flags)
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->file_open(&_file, path, flags);
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 File::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->file_close(_file);
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 File::read(void *buffer, size_t len)
marcozecchini 0:9fca2b23d0ba 66 {
marcozecchini 0:9fca2b23d0ba 67 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 68 return _fs->file_read(_file, buffer, len);
marcozecchini 0:9fca2b23d0ba 69 }
marcozecchini 0:9fca2b23d0ba 70
marcozecchini 0:9fca2b23d0ba 71 ssize_t File::write(const void *buffer, size_t len)
marcozecchini 0:9fca2b23d0ba 72 {
marcozecchini 0:9fca2b23d0ba 73 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 74 return _fs->file_write(_file, buffer, len);
marcozecchini 0:9fca2b23d0ba 75 }
marcozecchini 0:9fca2b23d0ba 76
marcozecchini 0:9fca2b23d0ba 77 int File::sync()
marcozecchini 0:9fca2b23d0ba 78 {
marcozecchini 0:9fca2b23d0ba 79 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 80 return _fs->file_sync(_file);
marcozecchini 0:9fca2b23d0ba 81 }
marcozecchini 0:9fca2b23d0ba 82
marcozecchini 0:9fca2b23d0ba 83 int File::isatty()
marcozecchini 0:9fca2b23d0ba 84 {
marcozecchini 0:9fca2b23d0ba 85 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 86 return _fs->file_isatty(_file);
marcozecchini 0:9fca2b23d0ba 87 }
marcozecchini 0:9fca2b23d0ba 88
marcozecchini 0:9fca2b23d0ba 89 off_t File::seek(off_t offset, int whence)
marcozecchini 0:9fca2b23d0ba 90 {
marcozecchini 0:9fca2b23d0ba 91 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 92 return _fs->file_seek(_file, offset, whence);
marcozecchini 0:9fca2b23d0ba 93 }
marcozecchini 0:9fca2b23d0ba 94
marcozecchini 0:9fca2b23d0ba 95 off_t File::tell()
marcozecchini 0:9fca2b23d0ba 96 {
marcozecchini 0:9fca2b23d0ba 97 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 98 return _fs->file_tell(_file);
marcozecchini 0:9fca2b23d0ba 99 }
marcozecchini 0:9fca2b23d0ba 100
marcozecchini 0:9fca2b23d0ba 101 void File::rewind()
marcozecchini 0:9fca2b23d0ba 102 {
marcozecchini 0:9fca2b23d0ba 103 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 104 return _fs->file_rewind(_file);
marcozecchini 0:9fca2b23d0ba 105 }
marcozecchini 0:9fca2b23d0ba 106
marcozecchini 0:9fca2b23d0ba 107 off_t File::size()
marcozecchini 0:9fca2b23d0ba 108 {
marcozecchini 0:9fca2b23d0ba 109 MBED_ASSERT(_fs);
marcozecchini 0:9fca2b23d0ba 110 return _fs->file_size(_file);
marcozecchini 0:9fca2b23d0ba 111 }
marcozecchini 0:9fca2b23d0ba 112