Skip to content

Commit

Permalink
Stream class should use mbed::fdopen() to attach a stream
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hasnain Virk committed May 31, 2017
1 parent b2408d8 commit 09ae609
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions drivers/RawSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
#include "drivers/RawSerial.h"
#include "platform/mbed_wait_api.h"
#include <stdio.h>
#include <cstdarg>


#if DEVICE_SERIAL

#define STRING_STACK_LIMIT 120
Expand Down
1 change: 0 additions & 1 deletion drivers/SerialBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

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

#include "Stream.h"
#include "Callback.h"
#include "serial_api.h"
#include "mbed_toolchain.h"
Expand Down
7 changes: 4 additions & 3 deletions platform/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ namespace mbed {
Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
// No lock needed in constructor
/* open ourselves */
char buf[12]; /* :0x12345678 + null byte */
std::sprintf(buf, ":%p", this);
_file = std::fopen(buf, "w+");
_file = fdopen(this, "w+");
// fdopen() will make us buffered because Stream::isatty()
// wrongly returns zero which is not being changed for
// backward compatibility
if (_file) {
mbed_set_unbuffered_stream(_file);
} else {
Expand Down
7 changes: 4 additions & 3 deletions platform/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
#include "platform/platform.h"
#include "platform/FileLike.h"
#include "platform/FileHandle.h"
#include <cstdio>
#include <cstdarg>

namespace mbed {
/** \addtogroup platform */
/** @{*/

extern void mbed_set_unbuffered_stream(FILE *_file);
extern int mbed_getc(FILE *_file);
extern char* mbed_gets(char *s, int size, FILE *_file);
extern void mbed_set_unbuffered_stream(std::FILE *_file);
extern int mbed_getc(std::FILE *_file);
extern char* mbed_gets(char *s, int size, std::FILE *_file);
/** @}*/

/** File stream
Expand Down

0 comments on commit 09ae609

Please sign in to comment.