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