blob: 7659f3d28fbb1df9e871c6edd7974fb0aad29393 [file] [log] [blame]
Harald Welte361ed202019-02-24 21:15:39 +01001/**
2 * \file
3 *
4 * \brief STDIO redirection terminal
5 *
6 * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Subject to your compliance with these terms, you may use Microchip
13 * software and any derivatives exclusively with Microchip products.
14 * It is your responsibility to comply with third party license terms applicable
15 * to your use of third party software (including open source software) that
16 * may accompany Microchip software.
17 *
18 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
26 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29 *
30 * \asf_license_stop
31 *
32 */
33
34#include <stdio.h>
35#include <stdio_io.h>
36
37/** IO descriptor for STDIO access. */
38static struct io_descriptor *stdio_io = NULL;
39
40void stdio_io_init(struct io_descriptor *io)
41{
42#if defined(__GNUC__)
43 /* Specify that stdout and stdin should not be buffered. */
44 setbuf(stdout, NULL);
45 setbuf(stdin, NULL);
46 /* Note: Already the case in IAR's Normal DLIB default configuration
47 * and AVR GCC library:
48 * - printf() emits one character at a time.
49 * - getchar() requests only 1 byte to exit.
50 */
51#endif
52 stdio_io = io;
53}
54
55void stdio_io_set_io(struct io_descriptor *io)
56{
57 stdio_io = io;
58}
59
60int32_t stdio_io_read(uint8_t *buf, const int32_t len)
61{
62 if (stdio_io == NULL) {
63 return 0;
64 }
65 return io_read(stdio_io, buf, len);
66}
67
68int32_t stdio_io_write(const uint8_t *buf, const int32_t len)
69{
70 if (stdio_io == NULL) {
71 return 0;
72 }
73 return io_write(stdio_io, buf, len);
74}