Yihui Xiong / BLE_NODE_TEST

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 9:05f0b5a3a70a 1 /* mbed Microcontroller Library
yihui 9:05f0b5a3a70a 2 * Copyright (c) 2006-2013 ARM Limited
yihui 9:05f0b5a3a70a 3 *
yihui 9:05f0b5a3a70a 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 9:05f0b5a3a70a 5 * you may not use this file except in compliance with the License.
yihui 9:05f0b5a3a70a 6 * You may obtain a copy of the License at
yihui 9:05f0b5a3a70a 7 *
yihui 9:05f0b5a3a70a 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 9:05f0b5a3a70a 9 *
yihui 9:05f0b5a3a70a 10 * Unless required by applicable law or agreed to in writing, software
yihui 9:05f0b5a3a70a 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 9:05f0b5a3a70a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 9:05f0b5a3a70a 13 * See the License for the specific language governing permissions and
yihui 9:05f0b5a3a70a 14 * limitations under the License.
yihui 9:05f0b5a3a70a 15 */
yihui 9:05f0b5a3a70a 16 #include "Stream.h"
yihui 9:05f0b5a3a70a 17
yihui 9:05f0b5a3a70a 18 #include <cstdarg>
yihui 9:05f0b5a3a70a 19
yihui 9:05f0b5a3a70a 20 namespace mbed {
yihui 9:05f0b5a3a70a 21
yihui 9:05f0b5a3a70a 22 Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
yihui 9:05f0b5a3a70a 23 /* open ourselves */
yihui 9:05f0b5a3a70a 24 char buf[12]; /* :0x12345678 + null byte */
yihui 9:05f0b5a3a70a 25 std::sprintf(buf, ":%p", this);
yihui 9:05f0b5a3a70a 26 _file = std::fopen(buf, "w+");
yihui 9:05f0b5a3a70a 27 setbuf(_file, NULL);
yihui 9:05f0b5a3a70a 28 }
yihui 9:05f0b5a3a70a 29
yihui 9:05f0b5a3a70a 30 Stream::~Stream() {
yihui 9:05f0b5a3a70a 31 fclose(_file);
yihui 9:05f0b5a3a70a 32 }
yihui 9:05f0b5a3a70a 33
yihui 9:05f0b5a3a70a 34 int Stream::putc(int c) {
yihui 9:05f0b5a3a70a 35 fflush(_file);
yihui 9:05f0b5a3a70a 36 return std::fputc(c, _file);
yihui 9:05f0b5a3a70a 37 }
yihui 9:05f0b5a3a70a 38 int Stream::puts(const char *s) {
yihui 9:05f0b5a3a70a 39 fflush(_file);
yihui 9:05f0b5a3a70a 40 return std::fputs(s, _file);
yihui 9:05f0b5a3a70a 41 }
yihui 9:05f0b5a3a70a 42 int Stream::getc() {
yihui 9:05f0b5a3a70a 43 fflush(_file);
yihui 9:05f0b5a3a70a 44 return std::fgetc(_file);
yihui 9:05f0b5a3a70a 45 }
yihui 9:05f0b5a3a70a 46 char* Stream::gets(char *s, int size) {
yihui 9:05f0b5a3a70a 47 fflush(_file);
yihui 9:05f0b5a3a70a 48 return std::fgets(s,size,_file);
yihui 9:05f0b5a3a70a 49 }
yihui 9:05f0b5a3a70a 50
yihui 9:05f0b5a3a70a 51 int Stream::close() {
yihui 9:05f0b5a3a70a 52 return 0;
yihui 9:05f0b5a3a70a 53 }
yihui 9:05f0b5a3a70a 54
yihui 9:05f0b5a3a70a 55 ssize_t Stream::write(const void* buffer, size_t length) {
yihui 9:05f0b5a3a70a 56 const char* ptr = (const char*)buffer;
yihui 9:05f0b5a3a70a 57 const char* end = ptr + length;
yihui 9:05f0b5a3a70a 58 while (ptr != end) {
yihui 9:05f0b5a3a70a 59 if (_putc(*ptr++) == EOF) {
yihui 9:05f0b5a3a70a 60 break;
yihui 9:05f0b5a3a70a 61 }
yihui 9:05f0b5a3a70a 62 }
yihui 9:05f0b5a3a70a 63 return ptr - (const char*)buffer;
yihui 9:05f0b5a3a70a 64 }
yihui 9:05f0b5a3a70a 65
yihui 9:05f0b5a3a70a 66 ssize_t Stream::read(void* buffer, size_t length) {
yihui 9:05f0b5a3a70a 67 char* ptr = (char*)buffer;
yihui 9:05f0b5a3a70a 68 char* end = ptr + length;
yihui 9:05f0b5a3a70a 69 while (ptr != end) {
yihui 9:05f0b5a3a70a 70 int c = _getc();
yihui 9:05f0b5a3a70a 71 if (c==EOF) break;
yihui 9:05f0b5a3a70a 72 *ptr++ = c;
yihui 9:05f0b5a3a70a 73 }
yihui 9:05f0b5a3a70a 74 return ptr - (const char*)buffer;
yihui 9:05f0b5a3a70a 75 }
yihui 9:05f0b5a3a70a 76
yihui 9:05f0b5a3a70a 77 off_t Stream::lseek(off_t offset, int whence) {
yihui 9:05f0b5a3a70a 78 return 0;
yihui 9:05f0b5a3a70a 79 }
yihui 9:05f0b5a3a70a 80
yihui 9:05f0b5a3a70a 81 int Stream::isatty() {
yihui 9:05f0b5a3a70a 82 return 0;
yihui 9:05f0b5a3a70a 83 }
yihui 9:05f0b5a3a70a 84
yihui 9:05f0b5a3a70a 85 int Stream::fsync() {
yihui 9:05f0b5a3a70a 86 return 0;
yihui 9:05f0b5a3a70a 87 }
yihui 9:05f0b5a3a70a 88
yihui 9:05f0b5a3a70a 89 off_t Stream::flen() {
yihui 9:05f0b5a3a70a 90 return 0;
yihui 9:05f0b5a3a70a 91 }
yihui 9:05f0b5a3a70a 92
yihui 9:05f0b5a3a70a 93 int Stream::printf(const char* format, ...) {
yihui 9:05f0b5a3a70a 94 std::va_list arg;
yihui 9:05f0b5a3a70a 95 va_start(arg, format);
yihui 9:05f0b5a3a70a 96 fflush(_file);
yihui 9:05f0b5a3a70a 97 int r = vfprintf(_file, format, arg);
yihui 9:05f0b5a3a70a 98 va_end(arg);
yihui 9:05f0b5a3a70a 99 return r;
yihui 9:05f0b5a3a70a 100 }
yihui 9:05f0b5a3a70a 101
yihui 9:05f0b5a3a70a 102 int Stream::scanf(const char* format, ...) {
yihui 9:05f0b5a3a70a 103 std::va_list arg;
yihui 9:05f0b5a3a70a 104 va_start(arg, format);
yihui 9:05f0b5a3a70a 105 fflush(_file);
yihui 9:05f0b5a3a70a 106 int r = vfscanf(_file, format, arg);
yihui 9:05f0b5a3a70a 107 va_end(arg);
yihui 9:05f0b5a3a70a 108 return r;
yihui 9:05f0b5a3a70a 109 }
yihui 9:05f0b5a3a70a 110
yihui 9:05f0b5a3a70a 111 } // namespace mbed