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 zehpyr targets.
28 #include <shell/shell.h>
29 #include <shell/shell_uart.h>
31 #include <console/console.h>
41 // We assume that if Zephyr shell is enabled it will be responsible for reading and parsing
42 // a command line. Therefore, we only have to provide implentation for streamer_write() and
43 // may omit streamer_read() part.
45 int streamer_zephyr_init(streamer_t * streamer)
51 ssize_t streamer_zephyr_read(streamer_t * streamer, char * buffer, size_t length)
57 ssize_t streamer_zephyr_write(streamer_t * streamer, const char * buffer, size_t length)
60 for (size_t i = 0; i < length; ++i)
61 // TODO: Don't assume that UART backend is used.
62 shell_fprintf(shell_backend_uart_get_ptr(), SHELL_NORMAL, "%c", buffer[i]);
68 int streamer_zephyr_init(streamer_t * streamer)
71 return console_init();
74 ssize_t streamer_zephyr_read(streamer_t * streamer, char * buffer, size_t length)
77 const ssize_t rc = console_read(nullptr, buffer, length);
78 return rc >= 0 ? rc : 0;
81 ssize_t streamer_zephyr_write(streamer_t * streamer, const char * buffer, size_t length)
86 while (written < length)
88 const ssize_t rc = console_write(nullptr, buffer + written, length - written);
91 written += static_cast<size_t>(rc);
97 #endif // CONFIG_SHELL
99 static streamer_t streamer_zephyr = {
100 .init_cb = streamer_zephyr_init,
101 .read_cb = streamer_zephyr_read,
102 .write_cb = streamer_zephyr_write,
106 streamer_t * streamer_get(void)
108 return &streamer_zephyr;