mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-13 11:23:40 +00:00
move netdev and netlink into their own packages and out of drivers
This moves the netdev/netlink namespace out of drivers and into their own packages. Also, defines netdev and netlink as L3/L4 and L2 OSI layers, respectively. Move some L3 functionality from netlink to netdev (GetIPAddr). For netlink, add ConnectParams for NetConnect to pass in L2 connection parameters (ssid, pass, auth_type, etc). Also adds connection mode (STA, AP, etc). For netlink, add SendEth and RecvEthFunc funcs to handle L2 send/recv of Ethernet pkts.
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
// L2 data link layer
|
||||
|
||||
package netlink
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrConnected = errors.New("Already connected")
|
||||
ErrConnectFailed = errors.New("Connect failed")
|
||||
ErrConnectTimeout = errors.New("Connect timed out")
|
||||
ErrMissingSSID = errors.New("Missing WiFi SSID")
|
||||
ErrAuthTypeNoGood = errors.New("Wifi authorization type not supported")
|
||||
ErrConnectModeNoGood = errors.New("Connect mode not supported")
|
||||
ErrNotSupported = errors.New("Not supported")
|
||||
)
|
||||
|
||||
type Event int
|
||||
|
||||
// Network events
|
||||
const (
|
||||
// The device's network connection is now UP
|
||||
EventNetUp Event = iota
|
||||
// The device's network connection is now DOWN
|
||||
EventNetDown
|
||||
)
|
||||
|
||||
type ConnectMode int
|
||||
|
||||
// Connect modes
|
||||
const (
|
||||
ConnectModeSTA = iota // Connect as Wifi station (default)
|
||||
ConnectModeAP // Connect as Wifi Access Point
|
||||
)
|
||||
|
||||
type AuthType int
|
||||
|
||||
// Wifi authorization types. Used when setting up an access point, or
|
||||
// connecting to an access point
|
||||
const (
|
||||
AuthTypeWPA2 = iota // WPA2 authorization (default)
|
||||
AuthTypeOpen // No authorization required (open)
|
||||
AuthTypeWPA // WPA authorization
|
||||
AuthTypeWPA2Mixed // WPA2/WPA mixed authorization
|
||||
)
|
||||
|
||||
const DefaultConnectTimeout = 10 * time.Second
|
||||
|
||||
type ConnectParams struct {
|
||||
// Connect mode
|
||||
ConnectMode
|
||||
// SSID of Wifi AP
|
||||
Ssid string
|
||||
// Passphrase of Wifi AP
|
||||
Passphrase string
|
||||
// Wifi authorization type
|
||||
AuthType
|
||||
// Wifi country code as two-char string. E.g. "XX" for world-wide,
|
||||
// "US" for USA, etc.
|
||||
Country string
|
||||
|
||||
// Retries is how many attempts to connect before returning with a
|
||||
// "Connect failed" error. Zero means infinite retries.
|
||||
Retries int
|
||||
|
||||
// Timeout duration for each connection attempt. The default zero
|
||||
// value means 10sec.
|
||||
ConnectTimeout time.Duration
|
||||
|
||||
// Watchdog ticker duration. On tick, the watchdog will check for
|
||||
// downed connection or hardware fault and try to recover the
|
||||
// connection. Set to zero to disable watchodog.
|
||||
WatchdogTimeout time.Duration
|
||||
}
|
||||
|
||||
// Netlinker is TinyGo's OSI L2 data link layer interface. Network device
|
||||
// drivers implement Netlinker to expose the device's L2 functionality.
|
||||
|
||||
type Netlinker interface {
|
||||
|
||||
// Connect device to network
|
||||
NetConnect(params *ConnectParams) error
|
||||
|
||||
// Disconnect device from network
|
||||
NetDisconnect()
|
||||
|
||||
// Notify to register callback for network events
|
||||
NetNotify(cb func(Event))
|
||||
|
||||
// GetHardwareAddr returns device MAC address
|
||||
GetHardwareAddr() (net.HardwareAddr, error)
|
||||
|
||||
// SendEth sends an Ethernet packet
|
||||
// TODO describe content of pkt
|
||||
SendEth(pkt []byte) error
|
||||
|
||||
// RecvEth callback function for receiving Ethernet pkt
|
||||
// TODO describe content of pkt
|
||||
RecvEthFunc(func(pkt []byte) error)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
//go:build challenger_rp2040
|
||||
|
||||
package probe
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers/espat"
|
||||
"tinygo.org/x/drivers/netdev"
|
||||
"tinygo.org/x/drivers/netlink"
|
||||
)
|
||||
|
||||
func Probe() (netlink.Netlinker, netdev.Netdever) {
|
||||
|
||||
cfg := espat.Config{
|
||||
// UART
|
||||
Uart: machine.UART1,
|
||||
Tx: machine.UART1_TX_PIN,
|
||||
Rx: machine.UART1_RX_PIN,
|
||||
}
|
||||
|
||||
esp := espat.NewDevice(&cfg)
|
||||
netdev.UseNetdev(esp)
|
||||
|
||||
return esp, esp
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//go:build arduino_mkrwifi1010
|
||||
|
||||
package probe
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/netdev"
|
||||
"tinygo.org/x/drivers/netlink"
|
||||
"tinygo.org/x/drivers/wifinina"
|
||||
)
|
||||
|
||||
func Probe() (netlink.Netlinker, netdev.Netdever) {
|
||||
|
||||
cfg := wifinina.Config{
|
||||
// Configure SPI for 8Mhz, Mode 0, MSB First
|
||||
Spi: machine.NINA_SPI,
|
||||
Freq: 8 * 1e6,
|
||||
Sdo: machine.NINA_SDO,
|
||||
Sdi: machine.NINA_SDI,
|
||||
Sck: machine.NINA_SCK,
|
||||
// Device pins
|
||||
Cs: machine.NINA_CS,
|
||||
Ack: machine.NINA_ACK,
|
||||
Gpio0: machine.NINA_GPIO0,
|
||||
Resetn: machine.NINA_RESETN,
|
||||
// mMKR 1010 resets High
|
||||
ResetIsHigh: true,
|
||||
}
|
||||
|
||||
nina := wifinina.New(&cfg)
|
||||
netdev.UseNetdev(nina)
|
||||
|
||||
return nina, nina
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//go:build wioterminal
|
||||
|
||||
package probe
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers/netdev"
|
||||
"tinygo.org/x/drivers/netlink"
|
||||
"tinygo.org/x/drivers/rtl8720dn"
|
||||
)
|
||||
|
||||
func Probe() (netlink.Netlinker, netdev.Netdever) {
|
||||
|
||||
cfg := rtl8720dn.Config{
|
||||
// Device
|
||||
En: machine.RTL8720D_CHIP_PU,
|
||||
// UART
|
||||
Uart: machine.UART3,
|
||||
Tx: machine.PB24,
|
||||
Rx: machine.PC24,
|
||||
Baudrate: 614400,
|
||||
}
|
||||
|
||||
rtl := rtl8720dn.New(&cfg)
|
||||
netdev.UseNetdev(rtl)
|
||||
|
||||
return rtl, rtl
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || matrixportal_m4
|
||||
|
||||
package probe
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers/netdev"
|
||||
"tinygo.org/x/drivers/netlink"
|
||||
"tinygo.org/x/drivers/wifinina"
|
||||
)
|
||||
|
||||
func Probe() (netlink.Netlinker, netdev.Netdever) {
|
||||
|
||||
cfg := wifinina.Config{
|
||||
// Configure SPI for 8Mhz, Mode 0, MSB First
|
||||
Spi: machine.NINA_SPI,
|
||||
Freq: 8 * 1e6,
|
||||
Sdo: machine.NINA_SDO,
|
||||
Sdi: machine.NINA_SDI,
|
||||
Sck: machine.NINA_SCK,
|
||||
// Device pins
|
||||
Cs: machine.NINA_CS,
|
||||
Ack: machine.NINA_ACK,
|
||||
Gpio0: machine.NINA_GPIO0,
|
||||
Resetn: machine.NINA_RESETN,
|
||||
}
|
||||
|
||||
nina := wifinina.New(&cfg)
|
||||
netdev.UseNetdev(nina)
|
||||
|
||||
return nina, nina
|
||||
}
|
||||
Reference in New Issue
Block a user