1 /*
2  * Copyright (c) 2015 Carlos Pizano-Uribe  [email protected]
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #ifndef __KERNEL_PORT_H
24 #define __KERNEL_PORT_H
25 
26 #include <sys/types.h>
27 #include <compiler.h>
28 
29 
30 __BEGIN_CDECLS;
31 
32 /* Ports are named, opaque objects and come in three flavors, the
33  * write-side, the read-side and a port group which is a collection
34  * of read-side ports.
35  */
36 
37 #define PORT_NAME_LEN 12
38 
39 typedef void *port_t;
40 
41 typedef struct {
42     char value[8];
43 } port_packet_t;
44 
45 typedef struct {
46     void *ctx;
47     port_packet_t packet;
48 } port_result_t;
49 
50 typedef enum {
51     PORT_MODE_BROADCAST   = 0,
52     PORT_MODE_UNICAST     = 1,
53     PORT_MODE_BIG_BUFFER  = 2,
54 } port_mode_t;
55 
56 /* Inits the port subsystem
57  */
58 void port_init(void);
59 
60 /* Make a named write-side port. broadcast ports can be opened by any
61  * number of read-clients. |name| can be up to PORT_NAME_LEN chars. If
62  * the write port exists it is returned even if the |mode| does not match.
63  */
64 status_t port_create(const char *name, port_mode_t mode, port_t *port);
65 
66 /* Make a read-side port. Only non-destroyed existing write ports can
67  * be opened with this api. Unicast ports can only be opened once. For
68  * broadcast ports, each call if successful returns a new port.
69  */
70 status_t port_open(const char *name, void *ctx, port_t *port);
71 
72 /* Creates a read-side port group which behaves just like a regular
73  * read-side port. A given port can only be assoicated with one port group.
74  */
75 status_t port_group(port_t *ports, size_t count, port_t *group);
76 
77 /* Adds a read-side port to an existing port group.
78  */
79 status_t port_group_add(port_t group, port_t port);
80 
81 /* Removes a read-side port to an existing port group.
82  */
83 status_t port_group_remove(port_t group, port_t port);
84 
85 /* Write to a port |count| packets, non-blocking, all or none atomic success.
86  */
87 status_t port_write(port_t port, const port_packet_t *pk, size_t count);
88 
89 /* Read one packet from the port or port group, blocking. The |result| contains
90  * the port that the message was read from. If |timeout| is zero the call
91  * does not block.
92  */
93 status_t port_read(port_t port, lk_time_t timeout, port_result_t *result);
94 
95 /* Destroy the write-side port, flush queued packets and release all resources,
96  * all calls will now fail on that port. Only a closed port can be destroyed.
97  */
98 status_t port_destroy(port_t port);
99 
100 /* Close the read-side port or the write side port. A closed write side port
101  * can be opened and the pending packets read. closing a port group does not
102  * close the included ports.
103  */
104 status_t port_close(port_t port);
105 
106 __END_CDECLS;
107 
108 #endif
109 
110