1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13
14#include <sel4/helpers.h>
15#include "sel4_stdio.h"
16
17#include <utils/circular_buffer.h>
18
19
20
21static circ_buf_t *buffer[RR_NUMIO];
22
23int rumpcomp_sel4_stdio_init(int stdio) {
24	if (stdio < 0 || stdio >= RR_NUMIO) {
25		return -1;
26	}
27
28	/* Allocate buffer for storing input characters */
29	buffer[stdio] = env.custom_simple.stdio_buf[stdio];
30	return circ_buf_init(BIT(RR_STDIO_PAGE_BITS)-sizeof(*buffer[0]), buffer[stdio]);
31}
32
33int rumpcomp_sel4_stdio_puts(int stdio, char *buf, int len) {
34		/* For each character call underlying putchar function */
35	int n;
36	for (n = 0; n < len; n++) {
37		if (circ_buf_is_full(buffer[stdio])) {
38			break;
39		} else {
40			circ_buf_put(buffer[stdio], *(buf+n));
41		}
42	}
43    seL4_Signal(env.custom_simple.stdio_ep[stdio]);
44	return n;
45}
46
47int rumpcomp_sel4_stdio_gets(int stdio, char *buf, int len) {
48    int n;
49	for (n = 0; n < len; n++) {
50		if (circ_buf_is_empty(buffer[stdio])) {
51			break;
52		} else {
53			*(buf+n) = circ_buf_get(buffer[stdio]);
54		}
55	}
56	return n;
57}
58
59
60// TODO: Add remove handler function
61void rumpcomp_register_handler(void (*handler)(void))
62{
63	env.custom_simple.get_char_handler = handler;
64}
65
66
67