xref: /aosp_15_r20/external/libwebsockets/READMEs/README.lws_system.md (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1*1c60b9acSAndroid Build Coastguard Worker# `lws_system`
2*1c60b9acSAndroid Build Coastguard Worker
3*1c60b9acSAndroid Build Coastguard WorkerSee `include/libwebsockets/lws-system.h` for function and object prototypes.
4*1c60b9acSAndroid Build Coastguard Worker
5*1c60b9acSAndroid Build Coastguard Worker## System integration api
6*1c60b9acSAndroid Build Coastguard Worker
7*1c60b9acSAndroid Build Coastguard Worker`lws_system` allows you to set a `system_ops` struct at context creation time,
8*1c60b9acSAndroid Build Coastguard Workerwhich can write up some function callbacks for system integration.  The goal
9*1c60b9acSAndroid Build Coastguard Workeris the user code calls these by getting the ops struct pointer from the
10*1c60b9acSAndroid Build Coastguard Workercontext using `lws_system_get_ops(context)` and so does not spread system
11*1c60b9acSAndroid Build Coastguard Workerdependencies around the user code, making it directly usable on completely
12*1c60b9acSAndroid Build Coastguard Workerdifferent platforms.
13*1c60b9acSAndroid Build Coastguard Worker
14*1c60b9acSAndroid Build Coastguard Worker```
15*1c60b9acSAndroid Build Coastguard Workertypedef struct lws_system_ops {
16*1c60b9acSAndroid Build Coastguard Worker	int (*reboot)(void);
17*1c60b9acSAndroid Build Coastguard Worker	int (*set_clock)(lws_usec_t us);
18*1c60b9acSAndroid Build Coastguard Worker	int (*attach)(struct lws_context *context, int tsi, lws_attach_cb_t cb,
19*1c60b9acSAndroid Build Coastguard Worker		      lws_system_states_t state, void *opaque,
20*1c60b9acSAndroid Build Coastguard Worker		      struct lws_attach_item **get);
21*1c60b9acSAndroid Build Coastguard Worker} lws_system_ops_t;
22*1c60b9acSAndroid Build Coastguard Worker```
23*1c60b9acSAndroid Build Coastguard Worker
24*1c60b9acSAndroid Build Coastguard Worker|Item|Meaning|
25*1c60b9acSAndroid Build Coastguard Worker|---|---|
26*1c60b9acSAndroid Build Coastguard Worker|`(*reboot)()`|Reboot the system|
27*1c60b9acSAndroid Build Coastguard Worker|`(*set_clock)()`|Set the system clock|
28*1c60b9acSAndroid Build Coastguard Worker|`(*attach)()`|Request an event loop callback from another thread context|
29*1c60b9acSAndroid Build Coastguard Worker
30*1c60b9acSAndroid Build Coastguard Worker### `reboot`
31*1c60b9acSAndroid Build Coastguard Worker
32*1c60b9acSAndroid Build Coastguard WorkerReboots the device
33*1c60b9acSAndroid Build Coastguard Worker
34*1c60b9acSAndroid Build Coastguard Worker### `set_clock`
35*1c60b9acSAndroid Build Coastguard Worker
36*1c60b9acSAndroid Build Coastguard WorkerSet the system clock to us-resolution Unix time in seconds
37*1c60b9acSAndroid Build Coastguard Worker
38*1c60b9acSAndroid Build Coastguard Worker### `attach`
39*1c60b9acSAndroid Build Coastguard Worker
40*1c60b9acSAndroid Build Coastguard WorkerRequest a callback from the event loop from a foreign thread.  This is used, for
41*1c60b9acSAndroid Build Coastguard Workerexample, for foreign threads to set up their event loop activity in their
42*1c60b9acSAndroid Build Coastguard Workercallback, and eg, exit once it is done, with their event loop activity able to
43*1c60b9acSAndroid Build Coastguard Workercontinue wholly from the lws event loop thread and stack context.
44*1c60b9acSAndroid Build Coastguard Worker
45*1c60b9acSAndroid Build Coastguard Worker## Foreign thread `attach` architecture
46*1c60b9acSAndroid Build Coastguard Worker
47*1c60b9acSAndroid Build Coastguard WorkerWhen lws is started, it should define an `lws_system_ops_t` at context creation
48*1c60b9acSAndroid Build Coastguard Workertime which defines its `.attach` handler.  In the `.attach` handler
49*1c60b9acSAndroid Build Coastguard Workerimplementation, it should perform platform-specific locking around a call to
50*1c60b9acSAndroid Build Coastguard Worker`__lws_system_attach()`, a public lws api that actually queues the callback
51*1c60b9acSAndroid Build Coastguard Workerrequest and does the main work.  The platform-specific wrapper is just there to
52*1c60b9acSAndroid Build Coastguard Workerdo the locking so multiple calls from different threads to the `.attach()`
53*1c60b9acSAndroid Build Coastguard Workeroperation can't conflict.
54*1c60b9acSAndroid Build Coastguard Worker
55*1c60b9acSAndroid Build Coastguard WorkerUser code can indicate it wants a callback from the lws event loop like this:
56*1c60b9acSAndroid Build Coastguard Worker
57*1c60b9acSAndroid Build Coastguard Worker```
58*1c60b9acSAndroid Build Coastguard Workerlws_system_get_ops(context)->attach(context, tsi, cb, state, opaque, NULL)
59*1c60b9acSAndroid Build Coastguard Worker```
60*1c60b9acSAndroid Build Coastguard Worker
61*1c60b9acSAndroid Build Coastguard Worker`context` is a pointer to the lws_context, `tsi` is normally 0, `cb` is the user
62*1c60b9acSAndroid Build Coastguard Workercallback in the form
63*1c60b9acSAndroid Build Coastguard Worker
64*1c60b9acSAndroid Build Coastguard Worker```
65*1c60b9acSAndroid Build Coastguard Workervoid (*lws_attach_cb_t)(struct lws_context *context, int tsi, void *opaque);
66*1c60b9acSAndroid Build Coastguard Worker```
67*1c60b9acSAndroid Build Coastguard Worker
68*1c60b9acSAndroid Build Coastguard Worker`state` is the `lws_system` state we should have reached before performing the
69*1c60b9acSAndroid Build Coastguard Workercallback (usually, `LWS_SYSTATE_OPERATIONAL`), and `opaque` is a user pointer that
70*1c60b9acSAndroid Build Coastguard Workerwill be passed into the callback.
71*1c60b9acSAndroid Build Coastguard Worker
72*1c60b9acSAndroid Build Coastguard Worker`cb` will normally want to create scheduled events and set up lws network-related
73*1c60b9acSAndroid Build Coastguard Workeractivity from the event loop thread and stack context.
74*1c60b9acSAndroid Build Coastguard Worker
75*1c60b9acSAndroid Build Coastguard WorkerOnce the event loop callback has been booked by calling this api, the thread and
76*1c60b9acSAndroid Build Coastguard Workerits stack context that booked it may be freed.  It will be called back and can
77*1c60b9acSAndroid Build Coastguard Workercontinue operations from the lws event loop thread and stack context.  For that
78*1c60b9acSAndroid Build Coastguard Workerreason, if `opaque` is needed it will usually point to something on the heap,
79*1c60b9acSAndroid Build Coastguard Workersince the stack context active at the time the callback was booked may be long
80*1c60b9acSAndroid Build Coastguard Workerdead by the time of the callback.
81*1c60b9acSAndroid Build Coastguard Worker
82*1c60b9acSAndroid Build Coastguard WorkerSee ./lib/system/README.md for more details.
83*1c60b9acSAndroid Build Coastguard Worker
84*1c60b9acSAndroid Build Coastguard Worker## `lws_system` blobs
85*1c60b9acSAndroid Build Coastguard Worker
86*1c60b9acSAndroid Build Coastguard Worker"Blobs" are arbitrary binary objects that have a total length.  Lws lets you set
87*1c60b9acSAndroid Build Coastguard Workerthem in two ways
88*1c60b9acSAndroid Build Coastguard Worker
89*1c60b9acSAndroid Build Coastguard Worker - "directly", by pointing to them, which has no heap implication
90*1c60b9acSAndroid Build Coastguard Worker
91*1c60b9acSAndroid Build Coastguard Worker - "heap", by adding one or more arbitrary chunk to a chained heap object
92*1c60b9acSAndroid Build Coastguard Worker
93*1c60b9acSAndroid Build Coastguard WorkerIn the "heap" case, it can be incrementally defined and the blob doesn't all
94*1c60b9acSAndroid Build Coastguard Workerhave to be declared at once.
95*1c60b9acSAndroid Build Coastguard Worker
96*1c60b9acSAndroid Build Coastguard WorkerFor read, the same api allows you to read all or part of the blob into a user
97*1c60b9acSAndroid Build Coastguard Workerbuffer.
98*1c60b9acSAndroid Build Coastguard Worker
99*1c60b9acSAndroid Build Coastguard WorkerThe following kinds of blob are defined
100*1c60b9acSAndroid Build Coastguard Worker
101*1c60b9acSAndroid Build Coastguard Worker|Item|Meaning|
102*1c60b9acSAndroid Build Coastguard Worker|---|---|
103*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSBLOB_TYPE_AUTH`|Auth-related blob 1, typically a registration token|
104*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSBLOB_TYPE_AUTH + 1`|Auth-related blob 2, typically an auth token|
105*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSBLOB_TYPE_CLIENT_CERT_DER`|Client cert public part|
106*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSBLOB_TYPE_CLIENT_KEY_DER`|Client cert key part|
107*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSBLOB_TYPE_DEVICE_SERIAL`|Arbitrary device serial number|
108*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSBLOB_TYPE_DEVICE_FW_VERSION`|Arbitrary firmware version|
109*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSBLOB_TYPE_DEVICE_TYPE`|Arbitrary Device Type identifier|
110*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSBLOB_TYPE_NTP_SERVER`|String with the ntp server address (defaults to pool.ntp.org)|
111*1c60b9acSAndroid Build Coastguard Worker
112*1c60b9acSAndroid Build Coastguard Worker### Blob handle api
113*1c60b9acSAndroid Build Coastguard Worker
114*1c60b9acSAndroid Build Coastguard WorkerReturns an object representing the blob for a particular type (listed above)
115*1c60b9acSAndroid Build Coastguard Worker
116*1c60b9acSAndroid Build Coastguard Worker```
117*1c60b9acSAndroid Build Coastguard Workerlws_system_blob_t *
118*1c60b9acSAndroid Build Coastguard Workerlws_system_get_blob(struct lws_context *context, lws_system_blob_item_t type,
119*1c60b9acSAndroid Build Coastguard Worker                    int idx);
120*1c60b9acSAndroid Build Coastguard Worker```
121*1c60b9acSAndroid Build Coastguard Worker
122*1c60b9acSAndroid Build Coastguard Worker### Blob Setting apis
123*1c60b9acSAndroid Build Coastguard Worker
124*1c60b9acSAndroid Build Coastguard WorkerSets the blob to point length `len` at `ptr`.  No heap allocation is used.
125*1c60b9acSAndroid Build Coastguard Worker
126*1c60b9acSAndroid Build Coastguard Worker```
127*1c60b9acSAndroid Build Coastguard Workervoid
128*1c60b9acSAndroid Build Coastguard Workerlws_system_blob_direct_set(lws_system_blob_t *b, const uint8_t *ptr, size_t len);
129*1c60b9acSAndroid Build Coastguard Worker```
130*1c60b9acSAndroid Build Coastguard Worker
131*1c60b9acSAndroid Build Coastguard WorkerAllocates and copied `len` bytes from `buf` into heap and chains it on the end of
132*1c60b9acSAndroid Build Coastguard Workerany existing.
133*1c60b9acSAndroid Build Coastguard Worker
134*1c60b9acSAndroid Build Coastguard Worker```
135*1c60b9acSAndroid Build Coastguard Workerint
136*1c60b9acSAndroid Build Coastguard Workerlws_system_blob_heap_append(lws_system_blob_t *b, const uint8_t *buf, size_t len)
137*1c60b9acSAndroid Build Coastguard Worker```
138*1c60b9acSAndroid Build Coastguard Worker
139*1c60b9acSAndroid Build Coastguard WorkerRemove any content from the blob, freeing it if it was on the heap
140*1c60b9acSAndroid Build Coastguard Worker
141*1c60b9acSAndroid Build Coastguard Worker```
142*1c60b9acSAndroid Build Coastguard Workervoid
143*1c60b9acSAndroid Build Coastguard Workerlws_system_blob_heap_empty(lws_system_blob_t *b)
144*1c60b9acSAndroid Build Coastguard Worker```
145*1c60b9acSAndroid Build Coastguard Worker
146*1c60b9acSAndroid Build Coastguard Worker### Blob getting apis
147*1c60b9acSAndroid Build Coastguard Worker
148*1c60b9acSAndroid Build Coastguard WorkerGet the total size of the blob (ie, if on the heap, the aggreate size of all the
149*1c60b9acSAndroid Build Coastguard Workerchunks that were appeneded)
150*1c60b9acSAndroid Build Coastguard Worker
151*1c60b9acSAndroid Build Coastguard Worker```
152*1c60b9acSAndroid Build Coastguard Workersize_t
153*1c60b9acSAndroid Build Coastguard Workerlws_system_blob_get_size(lws_system_blob_t *b)
154*1c60b9acSAndroid Build Coastguard Worker```
155*1c60b9acSAndroid Build Coastguard Worker
156*1c60b9acSAndroid Build Coastguard WorkerCopy part or all of the blob starting at offset ofs into a user buffer at buf.
157*1c60b9acSAndroid Build Coastguard Worker`*len` should be the length of the user buffer on entry, on exit it's set to
158*1c60b9acSAndroid Build Coastguard Workerthe used extent of `buf`.  This works the same whether the bob is a direct pointer
159*1c60b9acSAndroid Build Coastguard Workeror on the heap.
160*1c60b9acSAndroid Build Coastguard Worker
161*1c60b9acSAndroid Build Coastguard Worker```
162*1c60b9acSAndroid Build Coastguard Workerint
163*1c60b9acSAndroid Build Coastguard Workerlws_system_blob_get(lws_system_blob_t *b, uint8_t *buf, size_t *len, size_t ofs)
164*1c60b9acSAndroid Build Coastguard Worker```
165*1c60b9acSAndroid Build Coastguard Worker
166*1c60b9acSAndroid Build Coastguard WorkerIf you know that the blob was handled as a single direct pointer, or a single
167*1c60b9acSAndroid Build Coastguard Workerallocation, you can get a pointer to it without copying using this.
168*1c60b9acSAndroid Build Coastguard Worker
169*1c60b9acSAndroid Build Coastguard Worker```
170*1c60b9acSAndroid Build Coastguard Workerint
171*1c60b9acSAndroid Build Coastguard Workerlws_system_blob_get_single_ptr(lws_system_blob_t *b, const uint8_t **ptr)
172*1c60b9acSAndroid Build Coastguard Worker```
173*1c60b9acSAndroid Build Coastguard Worker
174*1c60b9acSAndroid Build Coastguard Worker### Blob destroy api
175*1c60b9acSAndroid Build Coastguard Worker
176*1c60b9acSAndroid Build Coastguard WorkerDeallocates any heap allocation for the blob
177*1c60b9acSAndroid Build Coastguard Worker
178*1c60b9acSAndroid Build Coastguard Worker```
179*1c60b9acSAndroid Build Coastguard Workervoid
180*1c60b9acSAndroid Build Coastguard Workerlws_system_blob_destroy(lws_system_blob_t *b)
181*1c60b9acSAndroid Build Coastguard Worker```
182*1c60b9acSAndroid Build Coastguard Worker
183*1c60b9acSAndroid Build Coastguard Worker
184*1c60b9acSAndroid Build Coastguard Worker## System state and notifiers
185*1c60b9acSAndroid Build Coastguard Worker
186*1c60b9acSAndroid Build Coastguard WorkerLws implements a state in the context that reflects the readiness of the system
187*1c60b9acSAndroid Build Coastguard Workerfor various steps leading up to normal operation.  By default it acts in a
188*1c60b9acSAndroid Build Coastguard Workerbackwards-compatible way and directly reaches the OPERATIONAL state just after
189*1c60b9acSAndroid Build Coastguard Workerthe context is created.
190*1c60b9acSAndroid Build Coastguard Worker
191*1c60b9acSAndroid Build Coastguard WorkerHowever other pieces of lws, and user, code may define notification handlers
192*1c60b9acSAndroid Build Coastguard Workerthat get called back when the state changes incrementally, and may veto or delay
193*1c60b9acSAndroid Build Coastguard Workerthe changes until work necessary for the new state has completed asynchronously.
194*1c60b9acSAndroid Build Coastguard Worker
195*1c60b9acSAndroid Build Coastguard WorkerThe generic states defined are:
196*1c60b9acSAndroid Build Coastguard Worker
197*1c60b9acSAndroid Build Coastguard Worker|State|Meaning|
198*1c60b9acSAndroid Build Coastguard Worker|---|---|
199*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_CONTEXT_CREATED`|The context was just created.|
200*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_INITIALIZED`|The vhost protocols have been initialized|
201*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_IFACE_COLDPLUG`|Existing network interfaces have been iterated|
202*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_DHCP`|Network identity is available|
203*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_TIME_VALID`|The system knows the time|
204*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_POLICY_VALID`|If the system needs information about how to act from the net, it has it|
205*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_REGISTERED`|The device has a registered identity|
206*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_AUTH1`|The device identity has produced a time-limited access token|
207*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_AUTH2`|Optional second access token for different services|
208*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_OPERATIONAL`|The system is ready for user code to work normally|
209*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_POLICY_INVALID`|All connections are being dropped because policy information is changing.  It will transition back to `LWS_SYSTATE_INITIALIZED` and onward to `OPERATIONAL` again afterwards with the new policy|
210*1c60b9acSAndroid Build Coastguard Worker|`LWS_SYSTATE_CONTEXT_DESTROYING`|Context is going down and smd with it|
211*1c60b9acSAndroid Build Coastguard Worker
212*1c60b9acSAndroid Build Coastguard Worker### Inserting a notifier
213*1c60b9acSAndroid Build Coastguard Worker
214*1c60b9acSAndroid Build Coastguard WorkerYou should create an object `lws_system_notify_link_t` in non-const memory and zero it down.
215*1c60b9acSAndroid Build Coastguard WorkerSet the `notify_cb` member and the `name` member and then register it using either
216*1c60b9acSAndroid Build Coastguard Worker`lws_system_reg_notifier()` or the `.register_notifier_list`
217*1c60b9acSAndroid Build Coastguard Workermember of the context creation info struct to make sure it will exist early
218*1c60b9acSAndroid Build Coastguard Workerenough to see all events.  The context creation info method takes a list of
219*1c60b9acSAndroid Build Coastguard Workerpointers to notify_link structs ending with a NULL entry.
220*1c60b9acSAndroid Build Coastguard Worker
221