1 /*
2  * Copyright (c) 2008-2015 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #pragma once
9 
10 #include <lk/compiler.h>
11 #include <lk/list.h>
12 #include <sys/types.h>
13 
14 /* LK specific calls to register to get input/output of the main console */
15 
16 __BEGIN_CDECLS
17 
18 typedef struct __print_callback print_callback_t;
19 struct __print_callback {
20     struct list_node entry;
21     void (*print)(print_callback_t *cb, const char *str, size_t len);
22     void *context;
23 };
24 
25 /* register callback to receive debug prints */
26 void register_print_callback(print_callback_t *cb);
27 void unregister_print_callback(print_callback_t *cb);
28 
29 /* the underlying handle to talk to io devices */
30 struct io_handle;
31 typedef struct io_handle_hooks {
32     ssize_t (*write)(struct io_handle *handle, const char *buf, size_t len);
33     ssize_t (*read)(struct io_handle *handle, char *buf, size_t len);
34 } io_handle_hooks_t;
35 
36 #define IO_HANDLE_MAGIC (0x696f6820)  // "ioh "
37 
38 typedef struct io_handle {
39     uint32_t magic;
40     const io_handle_hooks_t *hooks;
41 } io_handle_t;
42 
43 /* routines to call through the io handle */
44 ssize_t io_write(io_handle_t *io, const char *buf, size_t len);
45 ssize_t io_read(io_handle_t *io, char *buf, size_t len);
46 
47 /* initialization routine */
48 #define IO_HANDLE_INITIAL_VALUE(_hooks) { .magic = IO_HANDLE_MAGIC, .hooks = _hooks }
49 
io_handle_init(io_handle_t * io,io_handle_hooks_t * hooks)50 static inline void io_handle_init(io_handle_t *io, io_handle_hooks_t *hooks) {
51     *io = (io_handle_t)IO_HANDLE_INITIAL_VALUE(hooks);
52 }
53 
54 /* the main console io handle */
55 extern io_handle_t console_io;
56 
57 #ifndef CONSOLE_HAS_INPUT_BUFFER
58 #define CONSOLE_HAS_INPUT_BUFFER 0
59 #endif
60 
61 #if CONSOLE_HAS_INPUT_BUFFER
62 /* main input circular buffer that acts as the default input queue */
63 typedef struct cbuf cbuf_t;
64 extern cbuf_t console_input_cbuf;
65 #endif
66 
67 __END_CDECLS
68