3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Source implementation of an input / output stream for stdio targets.
34 #ifndef SHELL_STREAMER_APP_SPECIFIC
36 static struct termios the_original_stdin_termios;
38 static void streamer_restore_termios()
40 int in_fd = fileno(stdin);
41 tcsetattr(in_fd, TCSAFLUSH, &the_original_stdin_termios);
44 int streamer_stdio_init(streamer_t * streamer)
47 int in_fd = fileno(stdin);
48 struct termios termios;
52 ret = tcgetattr(in_fd, &the_original_stdin_termios);
53 atexit(&streamer_restore_termios);
55 ret = tcgetattr(in_fd, &termios);
56 termios.c_lflag &= ~static_cast<tcflag_t>(ECHO); // Disable echo mode
57 termios.c_lflag &= ~static_cast<tcflag_t>(ICANON); // Disable canonical line editing mode
58 ret = tcsetattr(in_fd, TCSANOW, &termios);
64 ssize_t streamer_stdio_read(streamer_t * streamer, char * buf, size_t len)
66 return read(STDIN_FILENO, buf, len);
69 ssize_t streamer_stdio_write(streamer_t * streamer, const char * buf, size_t len)
71 return write(STDOUT_FILENO, buf, len);
74 static streamer_t streamer_stdio = {
75 .init_cb = streamer_stdio_init,
76 .read_cb = streamer_stdio_read,
77 .write_cb = streamer_stdio_write,
80 streamer_t * streamer_get()
82 return &streamer_stdio;
85 #endif //#ifndef SHELL_STREAMER_APP_SPECIFIC