v2.0.0
cyw43.h
Go to the documentation of this file.
1/*
2 * This file is part of the cyw43-driver
3 *
4 * Copyright (C) 2019-2022 George Robotics Pty Ltd
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Any redistribution, use, or modification in source or binary form is done
15 * solely for personal benefit and not for any commercial purpose or for
16 * monetary gain.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE LICENSOR AND COPYRIGHT OWNER "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE LICENSOR OR COPYRIGHT OWNER BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * This software is also available for use with certain devices under different
30 * terms, as set out in the top level LICENSE file. For commercial licensing
31 * options please email contact@georgerobotics.com.au.
32 */
33
34#ifndef CYW43_INCLUDED_CYW43_H
35#define CYW43_INCLUDED_CYW43_H
36
37#include "cyw43_config.h"
38
39#if CYW43_LWIP
40#include "lwip/netif.h"
41#include "lwip/dhcp.h"
42#endif
43
44#if CYW43_NETUTILS
45#include "shared/netutils/dhcpserver.h"
46#endif
47
48#include "cyw43_ll.h"
49#include <string.h>
50
54
66#define CYW43_VERSION_MAJOR 1
67#define CYW43_VERSION_MINOR 0
68#define CYW43_VERSION_MICRO 3
70
76#define CYW43_VERSION (CYW43_VERSION_MAJOR << 16 | CYW43_VERSION_MINOR << 8 | CYW43_VERSION_MICRO)
78
84#define CYW43_TRACE_ASYNC_EV (0x0001)
85#define CYW43_TRACE_ETH_TX (0x0002)
86#define CYW43_TRACE_ETH_RX (0x0004)
87#define CYW43_TRACE_ETH_FULL (0x0008)
88#define CYW43_TRACE_MAC (0x0010)
90
99#define CYW43_LINK_DOWN (0)
100#define CYW43_LINK_JOIN (1)
101#define CYW43_LINK_NOIP (2)
102#define CYW43_LINK_UP (3)
103#define CYW43_LINK_FAIL (-1)
104#define CYW43_LINK_NONET (-2)
105#define CYW43_LINK_BADAUTH (-3)
107
108 typedef struct _cyw43_t {
109 cyw43_ll_t cyw43_ll;
110
111 uint8_t itf_state;
112 uint32_t trace_flags;
113
114 // State for async events
115 volatile uint32_t wifi_scan_state;
116 uint32_t wifi_join_state;
117 void *wifi_scan_env;
118 int (*wifi_scan_cb)(void *, const cyw43_ev_scan_result_t *);
119
120 bool initted;
121 // Pending things to do
122 bool pend_disassoc;
123 bool pend_rejoin;
124 bool pend_rejoin_wpa;
125
126 // AP settings
127 uint32_t ap_auth;
128 uint8_t ap_channel;
129 uint8_t ap_ssid_len;
130 uint8_t ap_key_len;
131 uint8_t ap_ssid[32];
132 uint8_t ap_key[64];
133
134 #if CYW43_LWIP
135 // lwIP data
136 struct netif netif[2];
137 #if LWIP_IPV4 && LWIP_DHCP
138 struct dhcp dhcp_client;
139 #endif
140 #endif
141
142 #if CYW43_NETUTILS
143 dhcp_server_t dhcp_server;
144 #endif
145
146 // mac from otp (or from cyw43_hal_generate_laa_mac if not set)
147 uint8_t mac[6];
148
149 #if CYW43_ENABLE_BLUETOOTH
150 bool bt_loaded;
151 #endif
152} cyw43_t;
153
154extern cyw43_t cyw43_state;
155extern void (*cyw43_poll)(void);
156extern uint32_t cyw43_sleep;
157
165void cyw43_init(cyw43_t *self);
166
174void cyw43_deinit(cyw43_t *self);
175
188int cyw43_ioctl(cyw43_t *self, uint32_t cmd, size_t len, uint8_t *buf, uint32_t iface);
189
202int cyw43_send_ethernet(cyw43_t *self, int itf, size_t len, const void *buf, bool is_pbuf);
203
219int cyw43_wifi_pm(cyw43_t *self, uint32_t pm);
220
238int cyw43_wifi_get_pm(cyw43_t *self, uint32_t *pm);
239
260int cyw43_wifi_link_status(cyw43_t *self, int itf);
261
277void cyw43_wifi_set_up(cyw43_t *self, int itf, bool up, uint32_t country);
278
289int cyw43_wifi_get_mac(cyw43_t *self, int itf, uint8_t mac[6]);
290
302int cyw43_wifi_update_multicast_filter(cyw43_t *self, uint8_t *addr, bool add);
303
317int cyw43_wifi_scan(cyw43_t *self, cyw43_wifi_scan_options_t *opts, void *env, int (*result_cb)(void *, const cyw43_ev_scan_result_t *));
318
327 static inline bool cyw43_wifi_scan_active(cyw43_t *self) {
328 return self->wifi_scan_state == 1;
329}
330
350int cyw43_wifi_join(cyw43_t *self, size_t ssid_len, const uint8_t *ssid, size_t key_len, const uint8_t *key, uint32_t auth_type, const uint8_t *bssid, uint32_t channel);
351
361int cyw43_wifi_leave(cyw43_t *self, int itf);
362
373int cyw43_wifi_get_rssi(cyw43_t *self, int32_t *rssi);
374
382int cyw43_wifi_get_bssid(cyw43_t *self, uint8_t bssid[6]);
383
393 static inline void cyw43_wifi_ap_get_ssid(cyw43_t *self, size_t *len, const uint8_t **buf) {
394 *len = self->ap_ssid_len;
395 *buf = self->ap_ssid;
396}
397
406 static inline uint32_t cyw43_wifi_ap_get_auth(cyw43_t *self) {
407 return self->ap_auth;
408}
409
418 static inline void cyw43_wifi_ap_set_channel(cyw43_t *self, uint32_t channel) {
419 self->ap_channel = (uint8_t)channel;
420}
421
431 static inline void cyw43_wifi_ap_set_ssid(cyw43_t *self, size_t len, const uint8_t *buf) {
432 self->ap_ssid_len = (uint8_t)MIN(len, sizeof(self->ap_ssid));
433 memcpy(self->ap_ssid, buf, self->ap_ssid_len);
434}
435
445 static inline void cyw43_wifi_ap_set_password(cyw43_t *self, size_t len, const uint8_t *buf) {
446 self->ap_key_len = (uint8_t)MIN(len, sizeof(self->ap_key));
447 memcpy(self->ap_key, buf, self->ap_key_len);
448}
449
465 static inline void cyw43_wifi_ap_set_auth(cyw43_t *self, uint32_t auth) {
466 self->ap_auth = auth;
467}
468
479void cyw43_wifi_ap_get_max_stas(cyw43_t *self, int *max_stas);
480
494void cyw43_wifi_ap_get_stas(cyw43_t *self, int *num_stas, uint8_t *macs);
495
504 static inline bool cyw43_is_initialized(cyw43_t *self) {
505 return self->initted;
506}
507
517void cyw43_cb_tcpip_init(cyw43_t *self, int itf);
518
528void cyw43_cb_tcpip_deinit(cyw43_t *self, int itf);
529
540void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf);
541
551void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf);
552
573int cyw43_tcpip_link_status(cyw43_t *self, int itf);
574
575#if CYW43_GPIO
587int cyw43_gpio_set(cyw43_t *self, int gpio, bool val);
588
600int cyw43_gpio_get(cyw43_t *self, int gpio, bool *val);
601#endif
602
625 static inline uint32_t cyw43_pm_value(uint8_t pm_mode, uint16_t pm2_sleep_ret_ms, uint8_t li_beacon_period, uint8_t li_dtim_period, uint8_t li_assoc) {
626 return li_assoc << 20 // listen interval sent to ap
627 | li_dtim_period << 16
628 | li_beacon_period << 12
629 | (pm2_sleep_ret_ms / 10) << 4 // cyw43_ll_wifi_pm multiplies this by 10
630 | pm_mode; // CYW43_PM2_POWERSAVE_MODE etc
631}
632
636#define CYW43_DEFAULT_PM cyw43_pm_value(CYW43_PM2_POWERSAVE_MODE, 200, 1, 1, 10)
637
641#define CYW43_AGGRESSIVE_PM cyw43_pm_value(CYW43_PM2_POWERSAVE_MODE, 2000, 1, 1, 10)
642
646#define CYW43_PERFORMANCE_PM cyw43_pm_value(CYW43_PM2_POWERSAVE_MODE, 20, 1, 1, 1)
647
648#if CYW43_ENABLE_BLUETOOTH
654int cyw43_bluetooth_hci_init(void);
655
664int cyw43_bluetooth_hci_read(uint8_t *buf, uint32_t max_size, uint32_t *len);
665
673int cyw43_bluetooth_hci_write(uint8_t *buf, size_t len);
674
678void cyw43_bluetooth_hci_process(void);
679#endif
680
682
683#endif // CYW43_INCLUDED_CYW43_H
cyw43_wifi_set_up
void cyw43_wifi_set_up(cyw43_t *self, int itf, bool up, uint32_t country)
Set up and initialise wifi.
Definition: cyw43_ctrl.c:537
cyw43_wifi_ap_set_channel
static void cyw43_wifi_ap_set_channel(cyw43_t *self, uint32_t channel)
Set the the channel for the access point.
Definition: cyw43.h:418
cyw43_pm_value
static uint32_t cyw43_pm_value(uint8_t pm_mode, uint16_t pm2_sleep_ret_ms, uint8_t li_beacon_period, uint8_t li_dtim_period, uint8_t li_assoc)
Return a power management value to pass to cyw43_wifi_pm.
Definition: cyw43.h:625
cyw43_wifi_scan_active
static bool cyw43_wifi_scan_active(cyw43_t *self)
Determine if a wifi scan is in progress.
Definition: cyw43.h:327
cyw43_wifi_scan
int cyw43_wifi_scan(cyw43_t *self, cyw43_wifi_scan_options_t *opts, void *env, int(*result_cb)(void *, const cyw43_ev_scan_result_t *))
Perform a wifi scan for wifi networks.
Definition: cyw43_ctrl.c:568
cyw43_deinit
void cyw43_deinit(cyw43_t *self)
Shut the driver down.
Definition: cyw43_ctrl.c:114
cyw43_cb_tcpip_init
void cyw43_cb_tcpip_init(cyw43_t *self, int itf)
Initialise the IP stack.
Definition: cyw43_driver.c:187
cyw43_wifi_get_mac
int cyw43_wifi_get_mac(cyw43_t *self, int itf, uint8_t mac[6])
Get the mac address of the device.
Definition: cyw43_ctrl.c:523
cyw43_wifi_link_status
int cyw43_wifi_link_status(cyw43_t *self, int itf)
Get the wifi link status.
Definition: cyw43_ctrl.c:594
cyw43_wifi_join
int cyw43_wifi_join(cyw43_t *self, size_t ssid_len, const uint8_t *ssid, size_t key_len, const uint8_t *key, uint32_t auth_type, const uint8_t *bssid, uint32_t channel)
Connect or join a wifi network.
Definition: cyw43_ctrl.c:616
cyw43_cb_tcpip_deinit
void cyw43_cb_tcpip_deinit(cyw43_t *self, int itf)
Deinitialise the IP stack.
Definition: cyw43_driver.c:189
cyw43_wifi_ap_set_auth
static void cyw43_wifi_ap_set_auth(cyw43_t *self, uint32_t auth)
Set the security authorisation used in AP mode.
Definition: cyw43.h:465
cyw43_wifi_pm
int cyw43_wifi_pm(cyw43_t *self, uint32_t pm)
Set the wifi power management mode.
Definition: cyw43_ctrl.c:478
cyw43_wifi_leave
int cyw43_wifi_leave(cyw43_t *self, int itf)
Disassociate from a wifi network.
Definition: cyw43_ctrl.c:647
cyw43_wifi_ap_set_ssid
static void cyw43_wifi_ap_set_ssid(cyw43_t *self, size_t len, const uint8_t *buf)
Set the ssid for the access point.
Definition: cyw43.h:431
cyw43_wifi_get_rssi
int cyw43_wifi_get_rssi(cyw43_t *self, int32_t *rssi)
Get the signal strength (RSSI) of the wifi network.
Definition: cyw43_ctrl.c:653
cyw43_is_initialized
static bool cyw43_is_initialized(cyw43_t *self)
Determines if the cyw43 driver been initialised.
Definition: cyw43.h:504
cyw43_wifi_get_bssid
int cyw43_wifi_get_bssid(cyw43_t *self, uint8_t bssid[6])
Get the BSSID of the connected wifi network.
Definition: cyw43_ctrl.c:660
cyw43_wifi_ap_get_ssid
static void cyw43_wifi_ap_get_ssid(cyw43_t *self, size_t *len, const uint8_t **buf)
Get the ssid for the access point.
Definition: cyw43.h:393
cyw43_send_ethernet
int cyw43_send_ethernet(cyw43_t *self, int itf, size_t len, const void *buf, bool is_pbuf)
Send a raw ethernet packet.
Definition: cyw43_ctrl.c:442
cyw43_wifi_get_pm
int cyw43_wifi_get_pm(cyw43_t *self, uint32_t *pm)
Get the wifi power management mode.
Definition: cyw43_ctrl.c:499
cyw43_cb_tcpip_set_link_up
void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf)
Notify the IP stack that the link is up.
Definition: cyw43_driver.c:191
cyw43_wifi_ap_get_stas
void cyw43_wifi_ap_get_stas(cyw43_t *self, int *num_stas, uint8_t *macs)
Get the number of devices (STAs) associated with the wifi access point.
Definition: cyw43_ctrl.c:698
cyw43_wifi_update_multicast_filter
int cyw43_wifi_update_multicast_filter(cyw43_t *self, uint8_t *addr, bool add)
Add/remove multicast group address.
Definition: cyw43_ctrl.c:530
cyw43_wifi_ap_set_password
static void cyw43_wifi_ap_set_password(cyw43_t *self, size_t len, const uint8_t *buf)
Set the password for the wifi access point.
Definition: cyw43.h:445
cyw43_ioctl
int cyw43_ioctl(cyw43_t *self, uint32_t cmd, size_t len, uint8_t *buf, uint32_t iface)
Send an ioctl command to cyw43.
Definition: cyw43_ctrl.c:428
cyw43_init
void cyw43_init(cyw43_t *self)
Initialize the driver.
Definition: cyw43_ctrl.c:83
cyw43_wifi_ap_get_auth
static uint32_t cyw43_wifi_ap_get_auth(cyw43_t *self)
Get the security authorisation used in AP mode.
Definition: cyw43.h:406
cyw43_tcpip_link_status
int cyw43_tcpip_link_status(cyw43_t *self, int itf)
Get the link status.
Definition: cyw43_lwip.c:293
cyw43_wifi_ap_get_max_stas
void cyw43_wifi_ap_get_max_stas(cyw43_t *self, int *max_stas)
Get the maximum number of devices (STAs) that can be associated with the wifi access point.
Definition: cyw43_ctrl.c:694
cyw43_cb_tcpip_set_link_down
void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf)
Notify the IP stack that the link is down.
Definition: cyw43_driver.c:194
MIN
#define MIN(a, b)
Macro to return the minimum of two comparable values.
Definition: compiler.h:146
_cyw43_ev_scan_result_t
Structure to return wifi scan results.
Definition: cyw43_ll.h:215
_cyw43_ll_t
Definition: cyw43_ll.h:265
_cyw43_t
Definition: cyw43.h:108
_cyw43_wifi_scan_options_t
wifi scan options passed to cyw43_wifi_scan
Definition: cyw43_ll.h:247