1 /*
2  * Copyright (c) 2015 Carlos Pizano-Uribe  cpu@chromium.org
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 <sys/types.h>
12 
13 __BEGIN_CDECLS
14 
15 // Ports are named, opaque objects and come in three flavors, the
16 // write-side, the read-side and a port group which is a collection
17 // of read-side ports.
18 
19 #define PORT_NAME_LEN 12
20 
21 typedef void *port_t;
22 
23 // A Port packet is wide enough to carry two full words of data
24 #define PORT_PACKET_LEN (sizeof(void *) * 2)
25 typedef struct {
26     char value[PORT_PACKET_LEN];
27 } port_packet_t;
28 
29 typedef struct {
30     void *ctx;
31     port_packet_t packet;
32 } port_result_t;
33 
34 typedef enum {
35     PORT_MODE_BROADCAST   = 0,
36     PORT_MODE_UNICAST     = 1,
37     PORT_MODE_BIG_BUFFER  = 2,
38 } port_mode_t;
39 
40 // Inits the port subsystem
41 void port_init(void);
42 
43 // Make a named write-side port. broadcast ports can be opened by any
44 // number of read-clients. |name| can be up to PORT_NAME_LEN chars. If
45 // the write port exists it is returned even if the |mode| does not match.
46 status_t port_create(const char *name, port_mode_t mode, port_t *port);
47 
48 // Make a read-side port. Only non-destroyed existing write ports can
49 // be opened with this api. Unicast ports can only be opened once. For
50 // broadcast ports, each call if successful returns a new port.
51 status_t port_open(const char *name, void *ctx, port_t *port);
52 
53 // Creates a read-side port group which behaves just like a regular
54 // read-side port. A given port can only be assoicated with one port group.
55 status_t port_group(port_t *ports, size_t count, port_t *group);
56 
57 // Adds a read-side port to an existing port group.
58 status_t port_group_add(port_t group, port_t port);
59 
60 // Removes a read-side port to an existing port group.
61 status_t port_group_remove(port_t group, port_t port);
62 
63 // Write to a port |count| packets, non-blocking, all or none atomic success.
64 status_t port_write(port_t port, const port_packet_t *pk, size_t count);
65 
66 // Read one packet from the port or port group, blocking. The |result| contains
67 // the port that the message was read from. If |timeout| is zero the call
68 // does not block.
69 status_t port_read(port_t port, lk_time_t timeout, port_result_t *result);
70 
71 // Destroy the write-side port, flush queued packets and release all resources,
72 // all calls will now fail on that port. Only a closed port can be destroyed.
73 status_t port_destroy(port_t port);
74 
75 // Close the read-side port or the write side port. A closed write side port
76 // can be opened and the pending packets read. closing a port group does not
77 // close the included ports.
78 status_t port_close(port_t port);
79 
80 __END_CDECLS
81