xref: /aosp_15_r20/external/libwebsockets/lib/system/README.md (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1*1c60b9acSAndroid Build Coastguard Worker# LWS System Helpers
2*1c60b9acSAndroid Build Coastguard Worker
3*1c60b9acSAndroid Build Coastguard WorkerLws now has a little collection of helper utilities for common network-based
4*1c60b9acSAndroid Build Coastguard Workerfunctions necessary for normal device operation, eg, async DNS, ntpclient
5*1c60b9acSAndroid Build Coastguard Worker(necessary for tls validation), and DHCP client.
6*1c60b9acSAndroid Build Coastguard Worker
7*1c60b9acSAndroid Build Coastguard Worker## Conventions
8*1c60b9acSAndroid Build Coastguard Worker
9*1c60b9acSAndroid Build Coastguard WorkerIf any system helper is enabled for build, lws creates an additional vhost
10*1c60b9acSAndroid Build Coastguard Worker"system" at Context Creation time.  Wsi that are created for the system
11*1c60b9acSAndroid Build Coastguard Workerfeatures are bound to this.  In the context object, this is available as
12*1c60b9acSAndroid Build Coastguard Worker`.vhost_system`.
13*1c60b9acSAndroid Build Coastguard Worker
14*1c60b9acSAndroid Build Coastguard Worker# Attaching to an existing context from other threads
15*1c60b9acSAndroid Build Coastguard Worker
16*1c60b9acSAndroid Build Coastguard WorkerTo simplify the case different pieces of code want to attach to a single
17*1c60b9acSAndroid Build Coastguard Workerlws_context at runtime, from different thread contexts, lws_system has an api
18*1c60b9acSAndroid Build Coastguard Workervia an lws_system operation function pointer where the other threads can use
19*1c60b9acSAndroid Build Coastguard Workerplatform-specific locking to request callbacks to their own code from the
20*1c60b9acSAndroid Build Coastguard Workerlws event loop thread context safely.
21*1c60b9acSAndroid Build Coastguard Worker
22*1c60b9acSAndroid Build Coastguard WorkerFor convenience, the callback can be delayed until the system has entered or
23*1c60b9acSAndroid Build Coastguard Workerpassed a specified system state, eg, LWS_SYSTATE_OPERATIONAL so the code will
24*1c60b9acSAndroid Build Coastguard Workeronly get called back after the network, ntpclient and auth have been done.
25*1c60b9acSAndroid Build Coastguard WorkerAdditionally an opaque pointer can be passed to the callback when it is called
26*1c60b9acSAndroid Build Coastguard Workerfrom the lws event loop context.
27*1c60b9acSAndroid Build Coastguard Worker
28*1c60b9acSAndroid Build Coastguard Worker## Implementing the system-specific locking
29*1c60b9acSAndroid Build Coastguard Worker
30*1c60b9acSAndroid Build Coastguard Worker`lws_system_ops_t` struct has a member `.attach`
31*1c60b9acSAndroid Build Coastguard Worker
32*1c60b9acSAndroid Build Coastguard Worker```
33*1c60b9acSAndroid Build Coastguard Worker	int (*attach)(struct lws_context *context, int tsi, lws_attach_cb_t *cb,
34*1c60b9acSAndroid Build Coastguard Worker		      lws_system_states_t state, void *opaque,
35*1c60b9acSAndroid Build Coastguard Worker		      struct lws_attach_item **get);
36*1c60b9acSAndroid Build Coastguard Worker```
37*1c60b9acSAndroid Build Coastguard Worker
38*1c60b9acSAndroid Build Coastguard WorkerThis should be defined in user code as setting locking, then passing the
39*1c60b9acSAndroid Build Coastguard Workerarguments through to a non-threadsafe helper
40*1c60b9acSAndroid Build Coastguard Worker
41*1c60b9acSAndroid Build Coastguard Worker```
42*1c60b9acSAndroid Build Coastguard Workerint
43*1c60b9acSAndroid Build Coastguard Worker__lws_system_attach(struct lws_context *context, int tsi, lws_attach_cb_t *cb,
44*1c60b9acSAndroid Build Coastguard Worker		    lws_system_states_t state, void *opaque,
45*1c60b9acSAndroid Build Coastguard Worker		    struct lws_attach_item **get);
46*1c60b9acSAndroid Build Coastguard Worker```
47*1c60b9acSAndroid Build Coastguard Worker
48*1c60b9acSAndroid Build Coastguard Workerthat does the actual attach work.  When it returns, the locking should be
49*1c60b9acSAndroid Build Coastguard Workerunlocked and the return passed back.
50*1c60b9acSAndroid Build Coastguard Worker
51*1c60b9acSAndroid Build Coastguard Worker## Attaching the callback request
52*1c60b9acSAndroid Build Coastguard Worker
53*1c60b9acSAndroid Build Coastguard WorkerUser code should call the lws_system_ops_t `.attach` function like
54*1c60b9acSAndroid Build Coastguard Worker
55*1c60b9acSAndroid Build Coastguard Worker```
56*1c60b9acSAndroid Build Coastguard Worker	lws_system_get_ops(context)->attach(...);
57*1c60b9acSAndroid Build Coastguard Worker```
58*1c60b9acSAndroid Build Coastguard Worker
59*1c60b9acSAndroid Build Coastguard WorkerThe callback function which will be called from the lws event loop context
60*1c60b9acSAndroid Build Coastguard Workershould look like this
61*1c60b9acSAndroid Build Coastguard Worker
62*1c60b9acSAndroid Build Coastguard Worker```
63*1c60b9acSAndroid Build Coastguard Workervoid my_callback(struct lws_context *context, int tsi, void *opaque);
64*1c60b9acSAndroid Build Coastguard Worker```
65*1c60b9acSAndroid Build Coastguard Worker
66*1c60b9acSAndroid Build Coastguard Workerwith the callback function name passed into the (*attach)() call above.  When
67*1c60b9acSAndroid Build Coastguard Workerthe callback happens, the opaque user pointer set at the (*attach)() call is
68*1c60b9acSAndroid Build Coastguard Workerpassed back to it as an argument.
69