xref: /freebsd/contrib/wpa/src/common/proc_coord.h (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
1 /*
2  * Coordination of operations between processes
3  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef PROC_COORD_H
10 #define PROC_COORD_H
11 
12 struct proc_coord;
13 
14 enum proc_coord_message_types {
15 	PROC_COORD_MSG_REQUEST = 0,
16 	PROC_COORD_MSG_RESPONSE = 1,
17 	PROC_COORD_MSG_EVENT = 2,
18 };
19 
20 enum proc_coord_commands {
21 	PROC_COORD_CMD_STARTING = 0,
22 	PROC_COORD_CMD_STOPPING = 1,
23 	PROC_COORD_CMD_PING = 2,
24 	PROC_COORD_CMD_TEST = 3,
25 };
26 
27 /**
28  * proc_coord_init - Initialize process coordinations
29  * @dir: Access controlled directory for process coordination
30  * Returns: Context pointer on success or %NULL on failure
31  *
32  * The returned context must be released with a call to proc_coord_deinit().
33  */
34 struct proc_coord * proc_coord_init(const char *dir);
35 
36 /**
37  * proc_coord_deinit - Deinitialize process coordinations
38  * @pc: Process coordination context from proc_coord_init()
39  */
40 void proc_coord_deinit(struct proc_coord *pc);
41 
42 typedef bool (*proc_coord_cb)(void *ctx, int src,
43 			      enum proc_coord_message_types msg_type,
44 			      enum proc_coord_commands cmd,
45 			      u32 seq, const struct wpabuf *msg);
46 
47 /**
48  * proc_coord_register_handler - Register a handler for process coordination
49  * @pc: Process coordination context from proc_coord_init()
50  * @cb: Callback function
51  * @cb_ctx: Context for the callback function
52  * Returns: 0 on success or -1 on failure
53  *
54  * The registered handler will be called for received request and event
55  * messages. Received request messages are delivered to the separate handler
56  * registered with proc_coord_send_request().
57  *
58  * The handler function can return true to stop iteration of handler functions
59  * or false to allow the iteration to continue reporting the message to other
60  * registered handler functions, if any.
61  */
62 int proc_coord_register_handler(struct proc_coord *pc, proc_coord_cb cb,
63 				void *cb_ctx);
64 
65 /**
66  * proc_coord_unregister_handler - Unregister a handler for process coordination
67  * @pc: Process coordination context from proc_coord_init()
68  * @cb: Callback function
69  * @cb_ctx: Context for the callback function
70  */
71 void proc_coord_unregister_handler(struct proc_coord *pc, proc_coord_cb cb,
72 				   void *cb_ctx);
73 
74 /**
75  * proc_coord_send_event - Send an event message
76  * @pc: Process coordination context from proc_coord_init()
77  * @dst: Destination peer (PID) or 0 for all active peers
78  * @cmd: The command ID for the message
79  * @msg: Payload of the message
80  * Returns: The number of peers the message was sent to
81  */
82 int proc_coord_send_event(struct proc_coord *pc, int dst,
83 			  enum proc_coord_commands cmd,
84 			  const struct wpabuf *msg);
85 
86 typedef void (*proc_coord_response_cb)(void *ctx, int pid,
87 				       const struct wpabuf *msg);
88 
89 /**
90  * proc_coord_send_request - Send a request message
91  * @pc: Process coordination context from proc_coord_init()
92  * @dst: Destination peer (PID) or 0 for all active peers
93  * @cmd: The command ID for the message
94  * @msg: Payload of the message
95  * @timeout_ms: Timeout for receiving a response
96  * @cb: Callback function to report the responses or %NULL for no callback
97  * @cb_ctx: Context for the callback function
98  * Returns: The number of peers the message was sent to
99  *
100  * If a response is received from a peer, the response is reported to the
101  * callback function. If no response is received within the specified timeout,
102  * the callback function is called with msg == NULL. The specified @cb_ctx has
103  * to remain valid until all the pending responses have been reported or until
104  * proc_coord_cancel_wait() has been used to cancel any pending wait.
105  */
106 int proc_coord_send_request(struct proc_coord *pc, int dst,
107 			    enum proc_coord_commands cmd,
108 			    const struct wpabuf *msg,
109 			    unsigned int timeout_ms,
110 			    proc_coord_response_cb cb,
111 			    void *cb_ctx);
112 
113 /**
114  * proc_coord_cancel_wait - Cancel wait for a pending response message
115  * @pc: Process coordination context from proc_coord_init()
116  * @cb: Callback function registered with proc_coord_send_request()
117  * @cb_ctx: Context for the callback function
118  */
119 void proc_coord_cancel_wait(struct proc_coord *pc, proc_coord_response_cb cb,
120 			    void *cb_ctx);
121 
122 /**
123  * proc_coord_send_event - Send a response message
124  * @pc: Process coordination context from proc_coord_init()
125  * @dst: Destination peer (PID)
126  * @cmd: The command ID for the message
127  * @seq: The sequence number from the received request message
128  * @msg: Payload of the message
129  * Returns: 0 on success or -1 on failure
130  *
131  * This is used to send a response to a request message that was reported
132  * through a call to the handler function that was registered with
133  * proc_coord_register_handler().
134  */
135 int proc_coord_send_response(struct proc_coord *pc, int dst,
136 			     enum proc_coord_commands cmd, u32 seq,
137 			     const struct wpabuf *msg);
138 
139 #endif /* PROC_COORD_H */
140