Skip to content

Commit 09ae609

Browse files
author
Hasnain Virk
committed
Stream class should use mbed::fdopen() to attach a stream
mbed::fdopen() is provided in mbed_retarget.cpp which will attach a stream to the given FileHandle. Removing mbed_set_unbuffered_stream() from stream class as it is defined in mbed_retarget.cpp. Stream class should not decide whether it wants to detach buffers from c library or not. mbed::fdopen() will do that based upon isatty() call. So if a FileHandle is not a tty, i.e., is not a device type, c library buffering will not be turned off. For device type FileHandles, c library buffering is turned off.
1 parent b2408d8 commit 09ae609

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

drivers/RawSerial.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
#include "drivers/RawSerial.h"
1717
#include "platform/mbed_wait_api.h"
18+
#include <stdio.h>
1819
#include <cstdarg>
1920

21+
2022
#if DEVICE_SERIAL
2123

2224
#define STRING_STACK_LIMIT 120

drivers/SerialBase.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY)
2222

23-
#include "Stream.h"
2423
#include "Callback.h"
2524
#include "serial_api.h"
2625
#include "mbed_toolchain.h"

platform/Stream.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ namespace mbed {
2222
Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
2323
// No lock needed in constructor
2424
/* open ourselves */
25-
char buf[12]; /* :0x12345678 + null byte */
26-
std::sprintf(buf, ":%p", this);
27-
_file = std::fopen(buf, "w+");
25+
_file = fdopen(this, "w+");
26+
// fdopen() will make us buffered because Stream::isatty()
27+
// wrongly returns zero which is not being changed for
28+
// backward compatibility
2829
if (_file) {
2930
mbed_set_unbuffered_stream(_file);
3031
} else {

platform/Stream.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
#include "platform/platform.h"
2020
#include "platform/FileLike.h"
2121
#include "platform/FileHandle.h"
22+
#include <cstdio>
2223
#include <cstdarg>
2324

2425
namespace mbed {
2526
/** \addtogroup platform */
2627
/** @{*/
2728

28-
extern void mbed_set_unbuffered_stream(FILE *_file);
29-
extern int mbed_getc(FILE *_file);
30-
extern char* mbed_gets(char *s, int size, FILE *_file);
29+
extern void mbed_set_unbuffered_stream(std::FILE *_file);
30+
extern int mbed_getc(std::FILE *_file);
31+
extern char* mbed_gets(char *s, int size, std::FILE *_file);
3132
/** @}*/
3233

3334
/** File stream

0 commit comments

Comments
 (0)