libwebsockets
Lightweight C library for HTML5 websockets
lws-netdev.h
Go to the documentation of this file.
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #define LWS_WIFI_MAX_SCAN_TRACK 16
26 #define LWS_ETH_ALEN 6
27 
29 typedef int8_t lws_wifi_rssi_t;
30 struct lws_netdev_instance;
31 
32 typedef enum {
37 
38 /*
39  * Base class for netdev configuration
40  */
41 
42 typedef struct lws_netdev_config {
43  void *plat_config;
45 
46 /*
47  * Const Logical generic network interface ops
48  */
49 
50 typedef struct lws_netdev_ops {
51  struct lws_netdev_instance * (*create)(struct lws_context *ctx,
52  const struct lws_netdev_ops *ops,
53  const char *name, void *platinfo);
54  int (*configure)(struct lws_netdev_instance *nd,
55  lws_netdev_config_t *config);
56  int (*up)(struct lws_netdev_instance *nd);
57  int (*down)(struct lws_netdev_instance *nd);
58  int (*event)(struct lws_netdev_instance *nd, lws_usec_t timestamp,
59  void *buf, size_t len);
61  void (*destroy)(struct lws_netdev_instance **pnd);
62  int (*connect)(struct lws_netdev_instance *wnd, const char *ssid,
63  const char *passphrase, uint8_t *bssid);
64  void (*scan)(struct lws_netdev_instance *nd);
66 
67 /*
68  * Network devices on this platform
69  *
70  * We also hold a list of all known network credentials (when they are needed
71  * because there is a network interface without anything to connect to) and
72  * the lws_settings instance they are stored in
73  */
74 
75 typedef struct lws_netdevs {
81  struct lwsac *ac_creds;
84 
86 
90 
91 /*
92  * Base class for an allocated instantiated derived object using lws_netdev_ops,
93  * ie, a specific ethernet device
94  */
95 
96 typedef struct lws_netdev_instance {
97  const char *name;
99  void *platinfo;
102  uint8_t type; /* lws_netdev_type_t */
104 
105 enum {
108 
109  LNDIW_MODE_STA = (1 << 0),
110  LNDIW_MODE_AP = (1 << 1),
111  LNDIW_UP = (1 << 7),
112 
113  LNDIW_ACQ_IPv4 = (1 << 0),
114  LNDIW_ACQ_IPv6 = (1 << 1),
115 };
116 
117 /*
118  * Group AP / Station State
119  */
120 
121 typedef enum {
123  /*
124  * We should gratuitously try whatever last worked for us, then
125  * if that fails, worry about the rest of the logic
126  */
128  /*
129  * Unconnected, scanning: AP known in one of the config slots ->
130  * configure it, start timeout + LWSNDVWIFI_STATE_STAT, if no AP
131  * already up in same group with lower MAC, after a random
132  * period start up our AP (LWSNDVWIFI_STATE_AP)
133  */
135  /* Trying to be the group AP... periodically do a scan
136  * LWSNDVWIFI_STATE_AP_SCAN, faster and then slower
137  */
139  /*
140  * doing a scan while trying to be the group AP... if we see a
141  * lower MAC being the AP for the same group AP, abandon being
142  * an AP and join that AP as a station
143  */
145  /*
146  * We have decided to join another group member who is being the
147  * AP, as its MAC is lower than ours. This is a stable state,
148  * but we still do periodic scans
149  * LWSNDVWIFI_STATE_STAT_GRP_AP_SCAN and will always prefer an
150  * AP configured in a slot.
151  */
153  /*
154  * We have joined a group member who is doing the AP job... we
155  * want to check every now and then if a configured AP has
156  * appeared that we should better use instead. Otherwise stay
157  * in LWSNDVWIFI_STATE_STAT_GRP_AP
158  */
160  /*
161  * trying to connect to another non-group AP. If we don't get an
162  * IP within a timeout and retries, mark it as unusable it and go back
163  */
166 
167 /*
168  * Generic WIFI credentials
169  */
170 
171 typedef struct lws_wifi_creds {
173 
175  char passphrase[64];
176  char ssid[33];
179 
180 /*
181  * Generic WIFI Network Device Instance
182  */
183 
184 typedef struct lws_netdev_instance_wifi {
186  lws_dll2_owner_t scan; /* sorted scan results */
188 
190  const char *ap_ip;
191 
192  const char *sta_ads;
193 
196 
198  uint8_t state; /* lws_netdev_wifi_state_t */
200 
201 /*
202  * Logical scan results sorted list item
203  */
204 
205 typedef struct lws_wifi_sta {
207 
208  uint32_t last_seen; /* unix time */
209  uint32_t last_tried; /* unix time */
210 
212  char *ssid; /* points to overallocation */
216  int16_t rssi_avg;
218 
221 
222  /* ssid overallocated afterwards */
224 
225 #define rssi_averaged(_x) (_x->rssi_count ? \
226  ((int)_x->rssi_avg / (int)_x->rssi_count) : \
227  -200)
228 
230 lws_netdevs_from_ctx(struct lws_context *ctx);
231 
234 
237 
239 lws_netdev_wifi_create_plat(struct lws_context *ctx,
240  const lws_netdev_ops_t *ops, const char *name,
241  void *platinfo);
244  lws_netdev_config_t *config);
247  void *buf, size_t len);
256 
259  const char *passphrase, uint8_t *bssid);
260 
262 lws_netdev_find(lws_netdevs_t *netdevs, const char *ifname);
263 
264 #define lws_netdev_wifi_plat_ops \
265  .create = lws_netdev_wifi_create_plat, \
266  .configure = lws_netdev_wifi_configure_plat, \
267  .event = lws_netdev_wifi_event_plat, \
268  .up = lws_netdev_wifi_up_plat, \
269  .down = lws_netdev_wifi_down_plat, \
270  .connect = lws_netdev_wifi_connect_plat, \
271  .scan = lws_netdev_wifi_scan_plat, \
272  .destroy = lws_netdev_wifi_destroy_plat
273 
274 /*
275  * This is for plat / OS level init that is necessary to be able to use
276  * networking or wifi at all, without mentioning any specific device
277  */
278 
281 
unsigned int uint32_t
#define LWS_EXTERN
int64_t lws_usec_t
unsigned char uint8_t
#define LWS_VISIBLE
LWS_VISIBLE LWS_EXTERN int lws_netdev_credentials_settings_get(lws_netdevs_t *nds)
struct lws_wifi_sta lws_wifi_sta_t
struct lws_netdevs lws_netdevs_t
char passphrase[64]
Definition: lws-netdev.h:175
#define LWS_ETH_ALEN
Definition: lws-netdev.h:26
int8_t lws_wifi_rssi_t
Definition: lws-netdev.h:29
uint8_t bssid[LWS_ETH_ALEN]
Definition: lws-netdev.h:211
lws_dll2_owner_t owner_creds
Definition: lws-netdev.h:79
LWS_VISIBLE LWS_EXTERN int lws_netdev_wifi_configure_plat(struct lws_netdev_instance *nd, lws_netdev_config_t *config)
struct lws_wifi_creds lws_wifi_creds_t
char ssid[33]
Definition: lws-netdev.h:176
LWS_VISIBLE LWS_EXTERN int lws_netdev_wifi_down_plat(struct lws_netdev_instance *nd)
lws_netdev_type_t
Definition: lws-netdev.h:32
@ LWSNDTYP_ETH
Definition: lws-netdev.h:35
@ LWSNDTYP_WIFI
Definition: lws-netdev.h:34
@ LWSNDTYP_UNKNOWN
Definition: lws-netdev.h:33
@ LNDIW_ACQ_IPv6
Definition: lws-netdev.h:114
@ LNDIW_ALG_WPA2
Definition: lws-netdev.h:107
@ LNDIW_MODE_AP
Definition: lws-netdev.h:110
@ LNDIW_ACQ_IPv4
Definition: lws-netdev.h:113
@ LNDIW_UP
Definition: lws-netdev.h:111
@ LNDIW_MODE_STA
Definition: lws-netdev.h:109
@ LNDIW_ALG_OPEN
Definition: lws-netdev.h:106
uint8_t refcount_creds
Definition: lws-netdev.h:87
LWS_VISIBLE LWS_EXTERN void lws_netdev_wifi_destroy_plat(struct lws_netdev_instance **pnd)
uint8_t current_attempt_bssid[LWS_ETH_ALEN]
Definition: lws-netdev.h:195
struct lws_netdev_ops lws_netdev_ops_t
uint8_t mac[LWS_ETH_ALEN]
Definition: lws-netdev.h:101
char * ssid
Definition: lws-netdev.h:212
uint8_t lws_wifi_ch_t
Definition: lws-netdev.h:28
const char * name
Definition: lws-netdev.h:97
LWS_VISIBLE LWS_EXTERN lws_netdevs_t * lws_netdevs_from_ctx(struct lws_context *ctx)
lws_sorted_usec_list_t sul_scan
Definition: lws-netdev.h:187
struct lws_netdev_instance lws_netdev_instance_t
struct lwsac * ac_creds
Definition: lws-netdev.h:81
uint32_t last_tried
Definition: lws-netdev.h:209
LWS_VISIBLE LWS_EXTERN int lws_netdev_wifi_up_plat(struct lws_netdev_instance *nd)
struct lws_netdev_config lws_netdev_config_t
lws_wifi_rssi_t rssi[8]
Definition: lws-netdev.h:215
lws_wifi_ch_t ch
Definition: lws-netdev.h:214
const lws_netdev_ops_t * ops
Definition: lws-netdev.h:98
lws_settings_instance_t * si
Definition: lws-netdev.h:83
LWS_VISIBLE LWS_EXTERN int lws_netdev_plat_wifi_init(void)
uint8_t rssi_count
Definition: lws-netdev.h:219
uint8_t authmode
Definition: lws-netdev.h:217
lws_netdev_wifi_state_t
Definition: lws-netdev.h:121
@ LWSNDVWIFI_STATE_AP_SCAN
Definition: lws-netdev.h:138
@ LWSNDVWIFI_STATE_STAT_GRP_AP
Definition: lws-netdev.h:144
@ LWSNDVWIFI_STATE_STAT_HAPPY
Definition: lws-netdev.h:164
@ LWSNDVWIFI_STATE_SCAN
Definition: lws-netdev.h:127
@ LWSNDVWIFI_STATE_STAT
Definition: lws-netdev.h:159
@ LWSNDVWIFI_STATE_INITIAL
Definition: lws-netdev.h:122
@ LWSNDVWIFI_STATE_STAT_GRP_AP_SCAN
Definition: lws-netdev.h:152
@ LWSNDVWIFI_STATE_AP
Definition: lws-netdev.h:134
lws_dll2_owner_t scan
Definition: lws-netdev.h:186
uint32_t last_seen
Definition: lws-netdev.h:208
lws_netdev_instance_t inst
Definition: lws-netdev.h:185
struct lws_netdev_instance_wifi lws_netdev_instance_wifi_t
lws_dll2_t list
Definition: lws-netdev.h:206
uint8_t bssid[LWS_ETH_ALEN]
Definition: lws-netdev.h:174
LWS_VISIBLE LWS_EXTERN int lws_netdev_wifi_event_plat(struct lws_netdev_instance *nd, lws_usec_t timestamp, void *buf, size_t len)
lws_wifi_creds_t * ap_cred
Definition: lws-netdev.h:189
LWS_VISIBLE LWS_EXTERN int lws_netdev_credentials_settings_set(lws_netdevs_t *nds)
int16_t rssi_avg
Definition: lws-netdev.h:216
LWS_VISIBLE LWS_EXTERN struct lws_netdev_instance * lws_netdev_wifi_create_plat(struct lws_context *ctx, const lws_netdev_ops_t *ops, const char *name, void *platinfo)
LWS_VISIBLE LWS_EXTERN void lws_netdev_wifi_scan_plat(lws_netdev_instance_t *nd)
LWS_VISIBLE LWS_EXTERN int lws_netdev_wifi_connect_plat(lws_netdev_instance_t *wnd, const char *ssid, const char *passphrase, uint8_t *bssid)
LWS_VISIBLE LWS_EXTERN lws_netdev_instance_t * lws_netdev_find(lws_netdevs_t *netdevs, const char *ifname)
LWS_VISIBLE LWS_EXTERN int lws_netdev_plat_init(void)
lws_dll2_t list
Definition: lws-netdev.h:172
lws_dll2_owner_t owner
Definition: lws-netdev.h:76
uint8_t rssi_next
Definition: lws-netdev.h:220
void * plat_config
Definition: lws-netdev.h:43
lws_sockaddr46 sa46_dns_resolver
Definition: lws-netdev.h:85
uint8_t ssid_len
Definition: lws-netdev.h:213
void(* destroy)(struct lws_netdev_instance **pnd)
Definition: lws-netdev.h:61
int(* down)(struct lws_netdev_instance *nd)
Definition: lws-netdev.h:57
int(* event)(struct lws_netdev_instance *nd, lws_usec_t timestamp, void *buf, size_t len)
Definition: lws-netdev.h:58
int(* configure)(struct lws_netdev_instance *nd, lws_netdev_config_t *config)
Definition: lws-netdev.h:54
void(* scan)(struct lws_netdev_instance *nd)
Definition: lws-netdev.h:64
int(* connect)(struct lws_netdev_instance *wnd, const char *ssid, const char *passphrase, uint8_t *bssid)
Definition: lws-netdev.h:62
int(* up)(struct lws_netdev_instance *nd)
Definition: lws-netdev.h:56