Skip to content

Commit 6d7b655

Browse files
author
Cruz Monrreal
authored
Merge pull request #8331 from deepikabhavnani/getc_fix
Stream: add necessary flushes, removing unneeded IAR workaround
2 parents 7ea50f1 + f79a354 commit 6d7b655

File tree

4 files changed

+79
-38
lines changed

4 files changed

+79
-38
lines changed

TESTS/mbed_platform/Stream/main.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "greentea-client/test_env.h"
18+
#include "utest/utest.h"
19+
#include "unity/unity.h"
20+
#include "mbed.h"
21+
22+
using utest::v1::Case;
23+
24+
class Loopback : public Stream {
25+
public:
26+
Loopback(const char *name = NULL) : Stream(name) {}
27+
28+
protected:
29+
virtual int _getc()
30+
{
31+
return _c;
32+
}
33+
virtual int _putc(int c)
34+
{
35+
_c = c;
36+
return c;
37+
}
38+
private:
39+
char _c;
40+
};
41+
42+
Loopback loop("loopback");
43+
44+
void test_putc_getc()
45+
{
46+
int ret;
47+
char char_buf[2] = {'a', 'b'};
48+
49+
ret = loop.putc(char_buf[0]);
50+
TEST_ASSERT_EQUAL_INT(char_buf[0], ret);
51+
ret = loop.getc();
52+
TEST_ASSERT_EQUAL_INT(char_buf[0], ret);
53+
ret = loop.putc(char_buf[1]);
54+
TEST_ASSERT_EQUAL_INT(char_buf[1], ret);
55+
ret = loop.getc();
56+
TEST_ASSERT_EQUAL_INT(char_buf[1], ret);
57+
return;
58+
}
59+
60+
utest::v1::status_t test_setup(const size_t number_of_cases)
61+
{
62+
GREENTEA_SETUP(10, "default_auto");
63+
return utest::v1::verbose_test_setup_handler(number_of_cases);
64+
}
65+
66+
Case cases[] = {
67+
Case("Test putc/getc", test_putc_getc)
68+
};
69+
70+
utest::v1::Specification specification(test_setup, cases);
71+
72+
int main()
73+
{
74+
return !utest::v1::Harness::run(specification);
75+
}

platform/Stream.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ Stream::~Stream()
4343
int Stream::putc(int c)
4444
{
4545
lock();
46-
fflush(_file);
46+
std::fseek(_file, 0, SEEK_CUR);
4747
int ret = std::fputc(c, _file);
4848
unlock();
4949
return ret;
5050
}
5151
int Stream::puts(const char *s)
5252
{
5353
lock();
54-
fflush(_file);
54+
std::fseek(_file, 0, SEEK_CUR);
5555
int ret = std::fputs(s, _file);
5656
unlock();
5757
return ret;
@@ -60,15 +60,15 @@ int Stream::getc()
6060
{
6161
lock();
6262
fflush(_file);
63-
int ret = mbed_getc(_file);
63+
int ret = std::fgetc(_file);
6464
unlock();
6565
return ret;
6666
}
6767
char *Stream::gets(char *s, int size)
6868
{
6969
lock();
7070
fflush(_file);
71-
char *ret = mbed_gets(s, size, _file);
71+
char *ret = std::fgets(s, size, _file);
7272
unlock();
7373
return ret;
7474
}

platform/Stream.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ namespace mbed {
3232
*/
3333

3434
extern void mbed_set_unbuffered_stream(std::FILE *_file);
35-
extern int mbed_getc(std::FILE *_file);
36-
extern char *mbed_gets(char *s, int size, std::FILE *_file);
3735

3836
/** File stream
3937
*

platform/mbed_retarget.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,38 +1345,6 @@ void mbed_set_unbuffered_stream(std::FILE *_file)
13451345
#endif
13461346
}
13471347

1348-
int mbed_getc(std::FILE *_file)
1349-
{
1350-
#if defined(__IAR_SYSTEMS_ICC__ ) && (__VER__ < 8000000)
1351-
/*This is only valid for unbuffered streams*/
1352-
int res = std::fgetc(_file);
1353-
if (res >= 0) {
1354-
_file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
1355-
_file->_Rend = _file->_Wend;
1356-
_file->_Next = _file->_Wend;
1357-
}
1358-
return res;
1359-
#else
1360-
return std::fgetc(_file);
1361-
#endif
1362-
}
1363-
1364-
char *mbed_gets(char *s, int size, std::FILE *_file)
1365-
{
1366-
#if defined(__IAR_SYSTEMS_ICC__ ) && (__VER__ < 8000000)
1367-
/*This is only valid for unbuffered streams*/
1368-
char *str = fgets(s, size, _file);
1369-
if (str != NULL) {
1370-
_file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
1371-
_file->_Rend = _file->_Wend;
1372-
_file->_Next = _file->_Wend;
1373-
}
1374-
return str;
1375-
#else
1376-
return std::fgets(s, size, _file);
1377-
#endif
1378-
}
1379-
13801348
} // namespace mbed
13811349

13821350
#if defined (__ICCARM__)

0 commit comments

Comments
 (0)