Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream: add necessary flushes, removing unneeded IAR workaround #8331

Merged
merged 1 commit into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions TESTS/mbed_platform/Stream/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "greentea-client/test_env.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "mbed.h"

using utest::v1::Case;

class Loopback : public Stream {
public:
Loopback(const char *name = NULL) : Stream(name) {}

protected:
virtual int _getc()
{
return _c;
}
virtual int _putc(int c)
{
_c = c;
return c;
}
private:
char _c;
};

Loopback loop("loopback");

void test_putc_getc()
{
int ret;
char char_buf[2] = {'a', 'b'};

ret = loop.putc(char_buf[0]);
TEST_ASSERT_EQUAL_INT(char_buf[0], ret);
ret = loop.getc();
TEST_ASSERT_EQUAL_INT(char_buf[0], ret);
ret = loop.putc(char_buf[1]);
TEST_ASSERT_EQUAL_INT(char_buf[1], ret);
ret = loop.getc();
TEST_ASSERT_EQUAL_INT(char_buf[1], ret);
return;
}

utest::v1::status_t test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(10, "default_auto");
return utest::v1::verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Test putc/getc", test_putc_getc)
};

utest::v1::Specification specification(test_setup, cases);

int main()
{
return !utest::v1::Harness::run(specification);
}
8 changes: 4 additions & 4 deletions platform/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ Stream::~Stream()
int Stream::putc(int c)
{
lock();
fflush(_file);
std::fseek(_file, 0, SEEK_CUR);
int ret = std::fputc(c, _file);
unlock();
return ret;
}
int Stream::puts(const char *s)
{
lock();
fflush(_file);
std::fseek(_file, 0, SEEK_CUR);
int ret = std::fputs(s, _file);
unlock();
return ret;
Expand All @@ -60,15 +60,15 @@ int Stream::getc()
{
lock();
fflush(_file);
int ret = mbed_getc(_file);
int ret = std::fgetc(_file);
unlock();
return ret;
}
char *Stream::gets(char *s, int size)
{
lock();
fflush(_file);
char *ret = mbed_gets(s, size, _file);
char *ret = std::fgets(s, size, _file);
unlock();
return ret;
}
Expand Down
2 changes: 0 additions & 2 deletions platform/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace mbed {
*/

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
32 changes: 0 additions & 32 deletions platform/mbed_retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,38 +1345,6 @@ void mbed_set_unbuffered_stream(std::FILE *_file)
#endif
}

int mbed_getc(std::FILE *_file)
{
#if defined(__IAR_SYSTEMS_ICC__ ) && (__VER__ < 8000000)
/*This is only valid for unbuffered streams*/
int res = std::fgetc(_file);
if (res >= 0) {
_file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
_file->_Rend = _file->_Wend;
_file->_Next = _file->_Wend;
}
return res;
#else
return std::fgetc(_file);
#endif
}

char *mbed_gets(char *s, int size, std::FILE *_file)
{
#if defined(__IAR_SYSTEMS_ICC__ ) && (__VER__ < 8000000)
/*This is only valid for unbuffered streams*/
char *str = fgets(s, size, _file);
if (str != NULL) {
_file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
_file->_Rend = _file->_Wend;
_file->_Next = _file->_Wend;
}
return str;
#else
return std::fgets(s, size, _file);
#endif
}

} // namespace mbed

#if defined (__ICCARM__)
Expand Down