1/*
2 * Copyright 2019, 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 GNU General Public License version 2. Note that NO WARRANTY is provided.
8 * See "LICENSE_GPLv2.txt" for details.
9 *
10 * @TAG(DATA61_GPL)
11 */
12
13#pragma once
14
15#include "khash.h"
16#include <sel4/sel4.h>
17#undef PACKED
18#include "picoserver_socket.h"
19#include <picoserver_event.h>
20
21/* Declare a hash map to keep track of sockets by ID*/
22KHASH_MAP_INIT_INT(socket, picoserver_socket_t *)
23
24/* Declare a hash map to keep track of sockets by their address,
25 * used to find the correct socket structure in the PicoTCP socket callbacks
26 * (only the address of the socket is given, and we can't pass cookies) */
27KHASH_MAP_INIT_INT64(socket_addr, picoserver_socket_t *);
28
29/* Declare a hash set to keep track of which sockets have events */
30KHASH_SET_INIT_INT(socket_event);
31
32typedef struct picoserver_client {
33    khint32_t bump_index;                    // Used to help keep track of the next free socket FD
34    khash_t(socket) *socket_table;
35    khash_t(socket_event) *socket_event_set;
36} picoserver_client_t;
37
38/*
39 * Initialises each of the client's bookkeeping structures, i.e. their
40 * picoserver_client_t struct.
41 */
42void picoserver_clients_init(int num_clients);
43
44/*
45 * Gets the number of sockets currently assigned to a particular client.
46 */
47uint32_t client_get_num_sockets(seL4_Word client_id);
48
49/*
50 * Gets the picoserver_socket_t struct that belongs to a particular socket ID
51 * for a client.
52 */
53picoserver_socket_t *client_get_socket(seL4_Word client_id, int socket_id);
54
55/*
56 * Gets the picoserver_socket_t struct that houses a particular pico_socket
57 * struct.
58 *
59 * Used in socket_cb() in picoserver.c.
60 */
61picoserver_socket_t *client_get_socket_by_addr(struct pico_socket *socket_addr);
62
63/*
64 * Finds an empty socket ID and adds a new entry to a client's socket table.
65 */
66int client_put_socket(seL4_Word client_id, picoserver_socket_t *new_socket);
67
68/*
69 * Deletes all bookkeeping structures related to a client's assigned socket.
70 */
71int client_delete_socket(seL4_Word client_id, int socket_id);
72
73/*
74 * Populates the given picoserver_event_t struct with an outstanding socket
75 * event for a client.
76 */
77void client_get_event(seL4_Word client_id, picoserver_event_t *ret_event);
78
79/*
80 * Adds an outstanding socket event to a client's event set.
81 */
82int client_put_event(seL4_Word client_id, int socket_id, uint16_t event);
83