Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/platform/Stream.cpp@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* mbed Microcontroller Library |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2006-2013 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 | #include "platform/Stream.h" |
marcozecchini | 0:9fca2b23d0ba | 17 | #include "platform/mbed_error.h" |
marcozecchini | 0:9fca2b23d0ba | 18 | #include <errno.h> |
marcozecchini | 0:9fca2b23d0ba | 19 | |
marcozecchini | 0:9fca2b23d0ba | 20 | namespace mbed { |
marcozecchini | 0:9fca2b23d0ba | 21 | |
marcozecchini | 0:9fca2b23d0ba | 22 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
marcozecchini | 0:9fca2b23d0ba | 23 | // No lock needed in constructor |
marcozecchini | 0:9fca2b23d0ba | 24 | /* open ourselves */ |
marcozecchini | 0:9fca2b23d0ba | 25 | _file = fdopen(this, "w+"); |
marcozecchini | 0:9fca2b23d0ba | 26 | // fdopen() will make us buffered because Stream::isatty() |
marcozecchini | 0:9fca2b23d0ba | 27 | // wrongly returns zero which is not being changed for |
marcozecchini | 0:9fca2b23d0ba | 28 | // backward compatibility |
marcozecchini | 0:9fca2b23d0ba | 29 | if (_file) { |
marcozecchini | 0:9fca2b23d0ba | 30 | mbed_set_unbuffered_stream(_file); |
marcozecchini | 0:9fca2b23d0ba | 31 | } else { |
marcozecchini | 0:9fca2b23d0ba | 32 | error("Stream obj failure, errno=%d\r\n", errno); |
marcozecchini | 0:9fca2b23d0ba | 33 | } |
marcozecchini | 0:9fca2b23d0ba | 34 | } |
marcozecchini | 0:9fca2b23d0ba | 35 | |
marcozecchini | 0:9fca2b23d0ba | 36 | Stream::~Stream() { |
marcozecchini | 0:9fca2b23d0ba | 37 | // No lock can be used in destructor |
marcozecchini | 0:9fca2b23d0ba | 38 | fclose(_file); |
marcozecchini | 0:9fca2b23d0ba | 39 | } |
marcozecchini | 0:9fca2b23d0ba | 40 | |
marcozecchini | 0:9fca2b23d0ba | 41 | int Stream::putc(int c) { |
marcozecchini | 0:9fca2b23d0ba | 42 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 43 | fflush(_file); |
marcozecchini | 0:9fca2b23d0ba | 44 | int ret = std::fputc(c, _file); |
marcozecchini | 0:9fca2b23d0ba | 45 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 46 | return ret; |
marcozecchini | 0:9fca2b23d0ba | 47 | } |
marcozecchini | 0:9fca2b23d0ba | 48 | int Stream::puts(const char *s) { |
marcozecchini | 0:9fca2b23d0ba | 49 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 50 | fflush(_file); |
marcozecchini | 0:9fca2b23d0ba | 51 | int ret = std::fputs(s, _file); |
marcozecchini | 0:9fca2b23d0ba | 52 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 53 | return ret; |
marcozecchini | 0:9fca2b23d0ba | 54 | } |
marcozecchini | 0:9fca2b23d0ba | 55 | int Stream::getc() { |
marcozecchini | 0:9fca2b23d0ba | 56 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 57 | fflush(_file); |
marcozecchini | 0:9fca2b23d0ba | 58 | int ret = mbed_getc(_file); |
marcozecchini | 0:9fca2b23d0ba | 59 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 60 | return ret; |
marcozecchini | 0:9fca2b23d0ba | 61 | } |
marcozecchini | 0:9fca2b23d0ba | 62 | char* Stream::gets(char *s, int size) { |
marcozecchini | 0:9fca2b23d0ba | 63 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 64 | fflush(_file); |
marcozecchini | 0:9fca2b23d0ba | 65 | char *ret = mbed_gets(s,size,_file); |
marcozecchini | 0:9fca2b23d0ba | 66 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 67 | return ret; |
marcozecchini | 0:9fca2b23d0ba | 68 | } |
marcozecchini | 0:9fca2b23d0ba | 69 | |
marcozecchini | 0:9fca2b23d0ba | 70 | int Stream::close() { |
marcozecchini | 0:9fca2b23d0ba | 71 | return 0; |
marcozecchini | 0:9fca2b23d0ba | 72 | } |
marcozecchini | 0:9fca2b23d0ba | 73 | |
marcozecchini | 0:9fca2b23d0ba | 74 | ssize_t Stream::write(const void* buffer, size_t length) { |
marcozecchini | 0:9fca2b23d0ba | 75 | const char* ptr = (const char*)buffer; |
marcozecchini | 0:9fca2b23d0ba | 76 | const char* end = ptr + length; |
marcozecchini | 0:9fca2b23d0ba | 77 | |
marcozecchini | 0:9fca2b23d0ba | 78 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 79 | while (ptr != end) { |
marcozecchini | 0:9fca2b23d0ba | 80 | if (_putc(*ptr++) == EOF) { |
marcozecchini | 0:9fca2b23d0ba | 81 | break; |
marcozecchini | 0:9fca2b23d0ba | 82 | } |
marcozecchini | 0:9fca2b23d0ba | 83 | } |
marcozecchini | 0:9fca2b23d0ba | 84 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 85 | |
marcozecchini | 0:9fca2b23d0ba | 86 | return ptr - (const char*)buffer; |
marcozecchini | 0:9fca2b23d0ba | 87 | } |
marcozecchini | 0:9fca2b23d0ba | 88 | |
marcozecchini | 0:9fca2b23d0ba | 89 | ssize_t Stream::read(void* buffer, size_t length) { |
marcozecchini | 0:9fca2b23d0ba | 90 | char* ptr = (char*)buffer; |
marcozecchini | 0:9fca2b23d0ba | 91 | char* end = ptr + length; |
marcozecchini | 0:9fca2b23d0ba | 92 | |
marcozecchini | 0:9fca2b23d0ba | 93 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 94 | while (ptr != end) { |
marcozecchini | 0:9fca2b23d0ba | 95 | int c = _getc(); |
marcozecchini | 0:9fca2b23d0ba | 96 | if (c==EOF) break; |
marcozecchini | 0:9fca2b23d0ba | 97 | *ptr++ = c; |
marcozecchini | 0:9fca2b23d0ba | 98 | } |
marcozecchini | 0:9fca2b23d0ba | 99 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 100 | |
marcozecchini | 0:9fca2b23d0ba | 101 | return ptr - (const char*)buffer; |
marcozecchini | 0:9fca2b23d0ba | 102 | } |
marcozecchini | 0:9fca2b23d0ba | 103 | |
marcozecchini | 0:9fca2b23d0ba | 104 | off_t Stream::seek(off_t offset, int whence) { |
marcozecchini | 0:9fca2b23d0ba | 105 | return 0; |
marcozecchini | 0:9fca2b23d0ba | 106 | } |
marcozecchini | 0:9fca2b23d0ba | 107 | |
marcozecchini | 0:9fca2b23d0ba | 108 | off_t Stream::tell() { |
marcozecchini | 0:9fca2b23d0ba | 109 | return 0; |
marcozecchini | 0:9fca2b23d0ba | 110 | } |
marcozecchini | 0:9fca2b23d0ba | 111 | |
marcozecchini | 0:9fca2b23d0ba | 112 | void Stream::rewind() { |
marcozecchini | 0:9fca2b23d0ba | 113 | } |
marcozecchini | 0:9fca2b23d0ba | 114 | |
marcozecchini | 0:9fca2b23d0ba | 115 | int Stream::isatty() { |
marcozecchini | 0:9fca2b23d0ba | 116 | return 0; |
marcozecchini | 0:9fca2b23d0ba | 117 | } |
marcozecchini | 0:9fca2b23d0ba | 118 | |
marcozecchini | 0:9fca2b23d0ba | 119 | int Stream::sync() { |
marcozecchini | 0:9fca2b23d0ba | 120 | return 0; |
marcozecchini | 0:9fca2b23d0ba | 121 | } |
marcozecchini | 0:9fca2b23d0ba | 122 | |
marcozecchini | 0:9fca2b23d0ba | 123 | off_t Stream::size() { |
marcozecchini | 0:9fca2b23d0ba | 124 | return 0; |
marcozecchini | 0:9fca2b23d0ba | 125 | } |
marcozecchini | 0:9fca2b23d0ba | 126 | |
marcozecchini | 0:9fca2b23d0ba | 127 | int Stream::printf(const char* format, ...) { |
marcozecchini | 0:9fca2b23d0ba | 128 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 129 | std::va_list arg; |
marcozecchini | 0:9fca2b23d0ba | 130 | va_start(arg, format); |
marcozecchini | 0:9fca2b23d0ba | 131 | fflush(_file); |
marcozecchini | 0:9fca2b23d0ba | 132 | int r = vfprintf(_file, format, arg); |
marcozecchini | 0:9fca2b23d0ba | 133 | va_end(arg); |
marcozecchini | 0:9fca2b23d0ba | 134 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 135 | return r; |
marcozecchini | 0:9fca2b23d0ba | 136 | } |
marcozecchini | 0:9fca2b23d0ba | 137 | |
marcozecchini | 0:9fca2b23d0ba | 138 | int Stream::scanf(const char* format, ...) { |
marcozecchini | 0:9fca2b23d0ba | 139 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 140 | std::va_list arg; |
marcozecchini | 0:9fca2b23d0ba | 141 | va_start(arg, format); |
marcozecchini | 0:9fca2b23d0ba | 142 | fflush(_file); |
marcozecchini | 0:9fca2b23d0ba | 143 | int r = vfscanf(_file, format, arg); |
marcozecchini | 0:9fca2b23d0ba | 144 | va_end(arg); |
marcozecchini | 0:9fca2b23d0ba | 145 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 146 | return r; |
marcozecchini | 0:9fca2b23d0ba | 147 | } |
marcozecchini | 0:9fca2b23d0ba | 148 | |
marcozecchini | 0:9fca2b23d0ba | 149 | int Stream::vprintf(const char* format, std::va_list args) { |
marcozecchini | 0:9fca2b23d0ba | 150 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 151 | fflush(_file); |
marcozecchini | 0:9fca2b23d0ba | 152 | int r = vfprintf(_file, format, args); |
marcozecchini | 0:9fca2b23d0ba | 153 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 154 | return r; |
marcozecchini | 0:9fca2b23d0ba | 155 | } |
marcozecchini | 0:9fca2b23d0ba | 156 | |
marcozecchini | 0:9fca2b23d0ba | 157 | int Stream::vscanf(const char* format, std::va_list args) { |
marcozecchini | 0:9fca2b23d0ba | 158 | lock(); |
marcozecchini | 0:9fca2b23d0ba | 159 | fflush(_file); |
marcozecchini | 0:9fca2b23d0ba | 160 | int r = vfscanf(_file, format, args); |
marcozecchini | 0:9fca2b23d0ba | 161 | unlock(); |
marcozecchini | 0:9fca2b23d0ba | 162 | return r; |
marcozecchini | 0:9fca2b23d0ba | 163 | } |
marcozecchini | 0:9fca2b23d0ba | 164 | |
marcozecchini | 0:9fca2b23d0ba | 165 | } // namespace mbed |