1/*
2 * Copyright 2016, 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(D61_BSD)
11 */
12
13#ifndef _CONSOLE_SERVER_DEVICE_INPUT_H_
14#define _CONSOLE_SERVER_DEVICE_INPUT_H_
15
16#include <stdio.h>
17#include <stdint.h>
18#include <stdbool.h>
19#include <sel4/sel4.h>
20#include <data_struct/cvector.h>
21#include <data_struct/cqueue.h>
22
23/*! @file
24    @brief Console Server input device implementation. */
25
26#define CONSERV_DEVICE_INPUT_MAGIC 0x54F1A770
27#define CONSERV_DEVICE_INPUT_BACKLOG_MAXSIZE 2
28#define CONSERV_DEVICE_INPUT_WAITER_MAGIC 0x341A8321
29
30#define INPUT_WAITERTYPE_GETC 0x0
31#define INPUT_WAITERTYPE_READ 0x1
32
33struct srv_client;
34
35struct input_waiter {
36    uint32_t magic;
37    seL4_CPtr reply;
38    struct srv_client *client; /*!< No ownership, Weak Reference. */
39    bool type; /*!< Whether getc or read. */
40};
41
42struct input_state {
43    uint32_t magic;
44    cqueue_t inputBacklog; /*!< char */
45    cvector_t waiterList; /*!< input_waiter */
46};
47
48/*! @brief Initialise input state manager and waiter list.
49    @param s The input state structure. (No ownership transfer)
50*/
51void input_init(struct input_state *s);
52
53/*! @brief Read from the input device backlog.
54    @param s The input state structure. (No ownership transfer)
55    @param dest Output buffer which the inputted chars will be written to. (No ownership transfer)
56    @param count Maximum output buffer length in bytes.
57    @return Number of characters read successfully. 0 means no characters to be read, and if this is
58            a blocking syscall, need to use input_save_caller_as_waiter() to block the calling
59            client.
60*/
61int input_read(struct input_state *s, int *dest, uint32_t count);
62
63/*! @brief Block current calling client and save its reply cap for when there is input available.
64    @param s The input state structure. (No ownership transfer)
65    @param c The client to be blocked. (No ownership transfer)
66    @param type The syscall type. Currently must be INPUT_WAITERTYPE_GETC.
67    @return ESUCCESS on success, refos_err_t otherwise.
68*/
69int input_save_caller_as_waiter(struct input_state *s, struct srv_client *c, bool type);
70
71/*! @brief Purge all weak references to client form waiting list. Used when client dies.
72    @param client The dying client to be purged.
73*/
74void input_purge_client(struct srv_client *client);
75
76#endif /* _CONSOLE_SERVER_DEVICE_INPUT_H_ */