mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:9db0e321a9f4 1 /* mbed Microcontroller Library
kenjiArai 1:9db0e321a9f4 2 * Copyright (c) 2006-2013 ARM Limited
kenjiArai 1:9db0e321a9f4 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 1:9db0e321a9f4 4 *
kenjiArai 1:9db0e321a9f4 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:9db0e321a9f4 6 * you may not use this file except in compliance with the License.
kenjiArai 1:9db0e321a9f4 7 * You may obtain a copy of the License at
kenjiArai 1:9db0e321a9f4 8 *
kenjiArai 1:9db0e321a9f4 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:9db0e321a9f4 10 *
kenjiArai 1:9db0e321a9f4 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:9db0e321a9f4 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:9db0e321a9f4 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:9db0e321a9f4 14 * See the License for the specific language governing permissions and
kenjiArai 1:9db0e321a9f4 15 * limitations under the License.
kenjiArai 1:9db0e321a9f4 16 */
kenjiArai 1:9db0e321a9f4 17 #include "platform/Stream.h"
kenjiArai 1:9db0e321a9f4 18 #include "platform/mbed_error.h"
kenjiArai 1:9db0e321a9f4 19 #include <errno.h>
kenjiArai 1:9db0e321a9f4 20
kenjiArai 1:9db0e321a9f4 21 namespace mbed {
kenjiArai 1:9db0e321a9f4 22
kenjiArai 1:9db0e321a9f4 23 Stream::Stream(const char *name) :
kenjiArai 1:9db0e321a9f4 24 FileLike(name)
kenjiArai 1:9db0e321a9f4 25 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 26 , _file(NULL)
kenjiArai 1:9db0e321a9f4 27 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 28 {
kenjiArai 1:9db0e321a9f4 29 // No lock needed in constructor
kenjiArai 1:9db0e321a9f4 30 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 31 /* open ourselves */
kenjiArai 1:9db0e321a9f4 32 _file = fdopen(this, "w+");
kenjiArai 1:9db0e321a9f4 33 // fdopen() will make us buffered because Stream::isatty()
kenjiArai 1:9db0e321a9f4 34 // wrongly returns zero which is not being changed for
kenjiArai 1:9db0e321a9f4 35 // backward compatibility
kenjiArai 1:9db0e321a9f4 36 if (_file) {
kenjiArai 1:9db0e321a9f4 37 mbed_set_unbuffered_stream(_file);
kenjiArai 1:9db0e321a9f4 38 } else {
kenjiArai 1:9db0e321a9f4 39 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file);
kenjiArai 1:9db0e321a9f4 40 }
kenjiArai 1:9db0e321a9f4 41 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 42 }
kenjiArai 1:9db0e321a9f4 43
kenjiArai 1:9db0e321a9f4 44 Stream::~Stream()
kenjiArai 1:9db0e321a9f4 45 {
kenjiArai 1:9db0e321a9f4 46 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 47 // No lock can be used in destructor
kenjiArai 1:9db0e321a9f4 48 fclose(_file);
kenjiArai 1:9db0e321a9f4 49 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 50 }
kenjiArai 1:9db0e321a9f4 51
kenjiArai 1:9db0e321a9f4 52 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 53 int Stream::putc(int c)
kenjiArai 1:9db0e321a9f4 54 {
kenjiArai 1:9db0e321a9f4 55 lock();
kenjiArai 1:9db0e321a9f4 56 std::fseek(_file, 0, SEEK_CUR);
kenjiArai 1:9db0e321a9f4 57 int ret = std::fputc(c, _file);
kenjiArai 1:9db0e321a9f4 58 unlock();
kenjiArai 1:9db0e321a9f4 59 return ret;
kenjiArai 1:9db0e321a9f4 60 }
kenjiArai 1:9db0e321a9f4 61 int Stream::puts(const char *s)
kenjiArai 1:9db0e321a9f4 62 {
kenjiArai 1:9db0e321a9f4 63 lock();
kenjiArai 1:9db0e321a9f4 64 std::fseek(_file, 0, SEEK_CUR);
kenjiArai 1:9db0e321a9f4 65 int ret = std::fputs(s, _file);
kenjiArai 1:9db0e321a9f4 66 unlock();
kenjiArai 1:9db0e321a9f4 67 return ret;
kenjiArai 1:9db0e321a9f4 68 }
kenjiArai 1:9db0e321a9f4 69 int Stream::getc()
kenjiArai 1:9db0e321a9f4 70 {
kenjiArai 1:9db0e321a9f4 71 lock();
kenjiArai 1:9db0e321a9f4 72 fflush(_file);
kenjiArai 1:9db0e321a9f4 73 int ret = std::fgetc(_file);
kenjiArai 1:9db0e321a9f4 74 unlock();
kenjiArai 1:9db0e321a9f4 75 return ret;
kenjiArai 1:9db0e321a9f4 76 }
kenjiArai 1:9db0e321a9f4 77 char *Stream::gets(char *s, int size)
kenjiArai 1:9db0e321a9f4 78 {
kenjiArai 1:9db0e321a9f4 79 lock();
kenjiArai 1:9db0e321a9f4 80 fflush(_file);
kenjiArai 1:9db0e321a9f4 81 char *ret = std::fgets(s, size, _file);
kenjiArai 1:9db0e321a9f4 82 unlock();
kenjiArai 1:9db0e321a9f4 83 return ret;
kenjiArai 1:9db0e321a9f4 84 }
kenjiArai 1:9db0e321a9f4 85 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 86
kenjiArai 1:9db0e321a9f4 87 int Stream::close()
kenjiArai 1:9db0e321a9f4 88 {
kenjiArai 1:9db0e321a9f4 89 return 0;
kenjiArai 1:9db0e321a9f4 90 }
kenjiArai 1:9db0e321a9f4 91
kenjiArai 1:9db0e321a9f4 92 ssize_t Stream::write(const void *buffer, size_t length)
kenjiArai 1:9db0e321a9f4 93 {
kenjiArai 1:9db0e321a9f4 94 const char *ptr = (const char *)buffer;
kenjiArai 1:9db0e321a9f4 95 const char *end = ptr + length;
kenjiArai 1:9db0e321a9f4 96
kenjiArai 1:9db0e321a9f4 97 lock();
kenjiArai 1:9db0e321a9f4 98 while (ptr != end) {
kenjiArai 1:9db0e321a9f4 99 if (_putc(*ptr++) == EOF) {
kenjiArai 1:9db0e321a9f4 100 break;
kenjiArai 1:9db0e321a9f4 101 }
kenjiArai 1:9db0e321a9f4 102 }
kenjiArai 1:9db0e321a9f4 103 unlock();
kenjiArai 1:9db0e321a9f4 104
kenjiArai 1:9db0e321a9f4 105 return ptr - (const char *)buffer;
kenjiArai 1:9db0e321a9f4 106 }
kenjiArai 1:9db0e321a9f4 107
kenjiArai 1:9db0e321a9f4 108 ssize_t Stream::read(void *buffer, size_t length)
kenjiArai 1:9db0e321a9f4 109 {
kenjiArai 1:9db0e321a9f4 110 char *ptr = (char *)buffer;
kenjiArai 1:9db0e321a9f4 111 char *end = ptr + length;
kenjiArai 1:9db0e321a9f4 112
kenjiArai 1:9db0e321a9f4 113 lock();
kenjiArai 1:9db0e321a9f4 114 while (ptr != end) {
kenjiArai 1:9db0e321a9f4 115 int c = _getc();
kenjiArai 1:9db0e321a9f4 116 if (c == EOF) {
kenjiArai 1:9db0e321a9f4 117 break;
kenjiArai 1:9db0e321a9f4 118 }
kenjiArai 1:9db0e321a9f4 119 *ptr++ = c;
kenjiArai 1:9db0e321a9f4 120 }
kenjiArai 1:9db0e321a9f4 121 unlock();
kenjiArai 1:9db0e321a9f4 122
kenjiArai 1:9db0e321a9f4 123 return ptr - (const char *)buffer;
kenjiArai 1:9db0e321a9f4 124 }
kenjiArai 1:9db0e321a9f4 125
kenjiArai 1:9db0e321a9f4 126 off_t Stream::seek(off_t offset, int whence)
kenjiArai 1:9db0e321a9f4 127 {
kenjiArai 1:9db0e321a9f4 128 return 0;
kenjiArai 1:9db0e321a9f4 129 }
kenjiArai 1:9db0e321a9f4 130
kenjiArai 1:9db0e321a9f4 131 off_t Stream::tell()
kenjiArai 1:9db0e321a9f4 132 {
kenjiArai 1:9db0e321a9f4 133 return 0;
kenjiArai 1:9db0e321a9f4 134 }
kenjiArai 1:9db0e321a9f4 135
kenjiArai 1:9db0e321a9f4 136 void Stream::rewind()
kenjiArai 1:9db0e321a9f4 137 {
kenjiArai 1:9db0e321a9f4 138 }
kenjiArai 1:9db0e321a9f4 139
kenjiArai 1:9db0e321a9f4 140 int Stream::isatty()
kenjiArai 1:9db0e321a9f4 141 {
kenjiArai 1:9db0e321a9f4 142 return 0;
kenjiArai 1:9db0e321a9f4 143 }
kenjiArai 1:9db0e321a9f4 144
kenjiArai 1:9db0e321a9f4 145 int Stream::sync()
kenjiArai 1:9db0e321a9f4 146 {
kenjiArai 1:9db0e321a9f4 147 return 0;
kenjiArai 1:9db0e321a9f4 148 }
kenjiArai 1:9db0e321a9f4 149
kenjiArai 1:9db0e321a9f4 150 off_t Stream::size()
kenjiArai 1:9db0e321a9f4 151 {
kenjiArai 1:9db0e321a9f4 152 return 0;
kenjiArai 1:9db0e321a9f4 153 }
kenjiArai 1:9db0e321a9f4 154
kenjiArai 1:9db0e321a9f4 155 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 156
kenjiArai 1:9db0e321a9f4 157 int Stream::printf(const char *format, ...)
kenjiArai 1:9db0e321a9f4 158 {
kenjiArai 1:9db0e321a9f4 159 lock();
kenjiArai 1:9db0e321a9f4 160 std::va_list arg;
kenjiArai 1:9db0e321a9f4 161 va_start(arg, format);
kenjiArai 1:9db0e321a9f4 162 std::fseek(_file, 0, SEEK_CUR);
kenjiArai 1:9db0e321a9f4 163 int r = vfprintf(_file, format, arg);
kenjiArai 1:9db0e321a9f4 164 va_end(arg);
kenjiArai 1:9db0e321a9f4 165 unlock();
kenjiArai 1:9db0e321a9f4 166 return r;
kenjiArai 1:9db0e321a9f4 167 }
kenjiArai 1:9db0e321a9f4 168
kenjiArai 1:9db0e321a9f4 169 int Stream::scanf(const char *format, ...)
kenjiArai 1:9db0e321a9f4 170 {
kenjiArai 1:9db0e321a9f4 171 lock();
kenjiArai 1:9db0e321a9f4 172 std::va_list arg;
kenjiArai 1:9db0e321a9f4 173 va_start(arg, format);
kenjiArai 1:9db0e321a9f4 174 fflush(_file);
kenjiArai 1:9db0e321a9f4 175 int r = vfscanf(_file, format, arg);
kenjiArai 1:9db0e321a9f4 176 va_end(arg);
kenjiArai 1:9db0e321a9f4 177 unlock();
kenjiArai 1:9db0e321a9f4 178 return r;
kenjiArai 1:9db0e321a9f4 179 }
kenjiArai 1:9db0e321a9f4 180
kenjiArai 1:9db0e321a9f4 181 int Stream::vprintf(const char *format, std::va_list args)
kenjiArai 1:9db0e321a9f4 182 {
kenjiArai 1:9db0e321a9f4 183 lock();
kenjiArai 1:9db0e321a9f4 184 std::fseek(_file, 0, SEEK_CUR);
kenjiArai 1:9db0e321a9f4 185 int r = vfprintf(_file, format, args);
kenjiArai 1:9db0e321a9f4 186 unlock();
kenjiArai 1:9db0e321a9f4 187 return r;
kenjiArai 1:9db0e321a9f4 188 }
kenjiArai 1:9db0e321a9f4 189
kenjiArai 1:9db0e321a9f4 190 int Stream::vscanf(const char *format, std::va_list args)
kenjiArai 1:9db0e321a9f4 191 {
kenjiArai 1:9db0e321a9f4 192 lock();
kenjiArai 1:9db0e321a9f4 193 fflush(_file);
kenjiArai 1:9db0e321a9f4 194 int r = vfscanf(_file, format, args);
kenjiArai 1:9db0e321a9f4 195 unlock();
kenjiArai 1:9db0e321a9f4 196 return r;
kenjiArai 1:9db0e321a9f4 197 }
kenjiArai 1:9db0e321a9f4 198
kenjiArai 1:9db0e321a9f4 199 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
kenjiArai 1:9db0e321a9f4 200
kenjiArai 1:9db0e321a9f4 201 } // namespace mbed