,,
Fork of Application by
mbed-dev/drivers/Stream.cpp@10:41552d038a69, 2017-01-10 (annotated)
- Committer:
- Zaitsev
- Date:
- Tue Jan 10 20:42:26 2017 +0000
- Revision:
- 10:41552d038a69
USB Serial bi-directional bridge
Who changed what in which revision?
User | Revision | Line number | New 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/Stream.h" |
Zaitsev | 10:41552d038a69 | 17 | |
Zaitsev | 10:41552d038a69 | 18 | namespace mbed { |
Zaitsev | 10:41552d038a69 | 19 | |
Zaitsev | 10:41552d038a69 | 20 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
Zaitsev | 10:41552d038a69 | 21 | // No lock needed in constructor |
Zaitsev | 10:41552d038a69 | 22 | /* open ourselves */ |
Zaitsev | 10:41552d038a69 | 23 | char buf[12]; /* :0x12345678 + null byte */ |
Zaitsev | 10:41552d038a69 | 24 | std::sprintf(buf, ":%p", this); |
Zaitsev | 10:41552d038a69 | 25 | _file = std::fopen(buf, "w+"); |
Zaitsev | 10:41552d038a69 | 26 | mbed_set_unbuffered_stream(_file); |
Zaitsev | 10:41552d038a69 | 27 | } |
Zaitsev | 10:41552d038a69 | 28 | |
Zaitsev | 10:41552d038a69 | 29 | Stream::~Stream() { |
Zaitsev | 10:41552d038a69 | 30 | // No lock can be used in destructor |
Zaitsev | 10:41552d038a69 | 31 | fclose(_file); |
Zaitsev | 10:41552d038a69 | 32 | } |
Zaitsev | 10:41552d038a69 | 33 | |
Zaitsev | 10:41552d038a69 | 34 | int Stream::putc(int c) { |
Zaitsev | 10:41552d038a69 | 35 | lock(); |
Zaitsev | 10:41552d038a69 | 36 | fflush(_file); |
Zaitsev | 10:41552d038a69 | 37 | int ret = std::fputc(c, _file); |
Zaitsev | 10:41552d038a69 | 38 | unlock(); |
Zaitsev | 10:41552d038a69 | 39 | return ret; |
Zaitsev | 10:41552d038a69 | 40 | } |
Zaitsev | 10:41552d038a69 | 41 | int Stream::puts(const char *s) { |
Zaitsev | 10:41552d038a69 | 42 | lock(); |
Zaitsev | 10:41552d038a69 | 43 | fflush(_file); |
Zaitsev | 10:41552d038a69 | 44 | int ret = std::fputs(s, _file); |
Zaitsev | 10:41552d038a69 | 45 | unlock(); |
Zaitsev | 10:41552d038a69 | 46 | return ret; |
Zaitsev | 10:41552d038a69 | 47 | } |
Zaitsev | 10:41552d038a69 | 48 | int Stream::getc() { |
Zaitsev | 10:41552d038a69 | 49 | lock(); |
Zaitsev | 10:41552d038a69 | 50 | fflush(_file); |
Zaitsev | 10:41552d038a69 | 51 | int ret = mbed_getc(_file); |
Zaitsev | 10:41552d038a69 | 52 | unlock(); |
Zaitsev | 10:41552d038a69 | 53 | return ret; |
Zaitsev | 10:41552d038a69 | 54 | } |
Zaitsev | 10:41552d038a69 | 55 | char* Stream::gets(char *s, int size) { |
Zaitsev | 10:41552d038a69 | 56 | lock(); |
Zaitsev | 10:41552d038a69 | 57 | fflush(_file); |
Zaitsev | 10:41552d038a69 | 58 | char *ret = mbed_gets(s,size,_file); |
Zaitsev | 10:41552d038a69 | 59 | unlock(); |
Zaitsev | 10:41552d038a69 | 60 | return ret; |
Zaitsev | 10:41552d038a69 | 61 | } |
Zaitsev | 10:41552d038a69 | 62 | |
Zaitsev | 10:41552d038a69 | 63 | int Stream::close() { |
Zaitsev | 10:41552d038a69 | 64 | return 0; |
Zaitsev | 10:41552d038a69 | 65 | } |
Zaitsev | 10:41552d038a69 | 66 | |
Zaitsev | 10:41552d038a69 | 67 | ssize_t Stream::write(const void* buffer, size_t length) { |
Zaitsev | 10:41552d038a69 | 68 | const char* ptr = (const char*)buffer; |
Zaitsev | 10:41552d038a69 | 69 | const char* end = ptr + length; |
Zaitsev | 10:41552d038a69 | 70 | |
Zaitsev | 10:41552d038a69 | 71 | lock(); |
Zaitsev | 10:41552d038a69 | 72 | while (ptr != end) { |
Zaitsev | 10:41552d038a69 | 73 | if (_putc(*ptr++) == EOF) { |
Zaitsev | 10:41552d038a69 | 74 | break; |
Zaitsev | 10:41552d038a69 | 75 | } |
Zaitsev | 10:41552d038a69 | 76 | } |
Zaitsev | 10:41552d038a69 | 77 | unlock(); |
Zaitsev | 10:41552d038a69 | 78 | |
Zaitsev | 10:41552d038a69 | 79 | return ptr - (const char*)buffer; |
Zaitsev | 10:41552d038a69 | 80 | } |
Zaitsev | 10:41552d038a69 | 81 | |
Zaitsev | 10:41552d038a69 | 82 | ssize_t Stream::read(void* buffer, size_t length) { |
Zaitsev | 10:41552d038a69 | 83 | char* ptr = (char*)buffer; |
Zaitsev | 10:41552d038a69 | 84 | char* end = ptr + length; |
Zaitsev | 10:41552d038a69 | 85 | |
Zaitsev | 10:41552d038a69 | 86 | lock(); |
Zaitsev | 10:41552d038a69 | 87 | while (ptr != end) { |
Zaitsev | 10:41552d038a69 | 88 | int c = _getc(); |
Zaitsev | 10:41552d038a69 | 89 | if (c==EOF) break; |
Zaitsev | 10:41552d038a69 | 90 | *ptr++ = c; |
Zaitsev | 10:41552d038a69 | 91 | } |
Zaitsev | 10:41552d038a69 | 92 | unlock(); |
Zaitsev | 10:41552d038a69 | 93 | |
Zaitsev | 10:41552d038a69 | 94 | return ptr - (const char*)buffer; |
Zaitsev | 10:41552d038a69 | 95 | } |
Zaitsev | 10:41552d038a69 | 96 | |
Zaitsev | 10:41552d038a69 | 97 | off_t Stream::lseek(off_t offset, int whence) { |
Zaitsev | 10:41552d038a69 | 98 | return 0; |
Zaitsev | 10:41552d038a69 | 99 | } |
Zaitsev | 10:41552d038a69 | 100 | |
Zaitsev | 10:41552d038a69 | 101 | int Stream::isatty() { |
Zaitsev | 10:41552d038a69 | 102 | return 0; |
Zaitsev | 10:41552d038a69 | 103 | } |
Zaitsev | 10:41552d038a69 | 104 | |
Zaitsev | 10:41552d038a69 | 105 | int Stream::fsync() { |
Zaitsev | 10:41552d038a69 | 106 | return 0; |
Zaitsev | 10:41552d038a69 | 107 | } |
Zaitsev | 10:41552d038a69 | 108 | |
Zaitsev | 10:41552d038a69 | 109 | off_t Stream::flen() { |
Zaitsev | 10:41552d038a69 | 110 | return 0; |
Zaitsev | 10:41552d038a69 | 111 | } |
Zaitsev | 10:41552d038a69 | 112 | |
Zaitsev | 10:41552d038a69 | 113 | int Stream::printf(const char* format, ...) { |
Zaitsev | 10:41552d038a69 | 114 | lock(); |
Zaitsev | 10:41552d038a69 | 115 | std::va_list arg; |
Zaitsev | 10:41552d038a69 | 116 | va_start(arg, format); |
Zaitsev | 10:41552d038a69 | 117 | fflush(_file); |
Zaitsev | 10:41552d038a69 | 118 | int r = vfprintf(_file, format, arg); |
Zaitsev | 10:41552d038a69 | 119 | va_end(arg); |
Zaitsev | 10:41552d038a69 | 120 | unlock(); |
Zaitsev | 10:41552d038a69 | 121 | return r; |
Zaitsev | 10:41552d038a69 | 122 | } |
Zaitsev | 10:41552d038a69 | 123 | |
Zaitsev | 10:41552d038a69 | 124 | int Stream::scanf(const char* format, ...) { |
Zaitsev | 10:41552d038a69 | 125 | lock(); |
Zaitsev | 10:41552d038a69 | 126 | std::va_list arg; |
Zaitsev | 10:41552d038a69 | 127 | va_start(arg, format); |
Zaitsev | 10:41552d038a69 | 128 | fflush(_file); |
Zaitsev | 10:41552d038a69 | 129 | int r = vfscanf(_file, format, arg); |
Zaitsev | 10:41552d038a69 | 130 | va_end(arg); |
Zaitsev | 10:41552d038a69 | 131 | unlock(); |
Zaitsev | 10:41552d038a69 | 132 | return r; |
Zaitsev | 10:41552d038a69 | 133 | } |
Zaitsev | 10:41552d038a69 | 134 | |
Zaitsev | 10:41552d038a69 | 135 | int Stream::vprintf(const char* format, std::va_list args) { |
Zaitsev | 10:41552d038a69 | 136 | lock(); |
Zaitsev | 10:41552d038a69 | 137 | fflush(_file); |
Zaitsev | 10:41552d038a69 | 138 | int r = vfprintf(_file, format, args); |
Zaitsev | 10:41552d038a69 | 139 | unlock(); |
Zaitsev | 10:41552d038a69 | 140 | return r; |
Zaitsev | 10:41552d038a69 | 141 | } |
Zaitsev | 10:41552d038a69 | 142 | |
Zaitsev | 10:41552d038a69 | 143 | int Stream::vscanf(const char* format, std::va_list args) { |
Zaitsev | 10:41552d038a69 | 144 | lock(); |
Zaitsev | 10:41552d038a69 | 145 | fflush(_file); |
Zaitsev | 10:41552d038a69 | 146 | int r = vfscanf(_file, format, args); |
Zaitsev | 10:41552d038a69 | 147 | unlock(); |
Zaitsev | 10:41552d038a69 | 148 | return r; |
Zaitsev | 10:41552d038a69 | 149 | } |
Zaitsev | 10:41552d038a69 | 150 | |
Zaitsev | 10:41552d038a69 | 151 | } // namespace mbed |