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:
Scott Feldman
2023-06-09 14:03:03 -07:00
committed by Ron Evans
parent e7d51d3c73
commit 8238f96319
64 changed files with 628 additions and 1419 deletions
+36 -39
View File
@@ -30,14 +30,11 @@ import (
"sync" "sync"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers/netdev"
"tinygo.org/x/drivers/netlink"
) )
type Config struct { type Config struct {
// AP creditials
Ssid string
Passphrase string
// UART config // UART config
Uart *machine.UART Uart *machine.UART
Tx machine.Pin Tx machine.Pin
@@ -62,25 +59,18 @@ type Device struct {
mu sync.Mutex mu sync.Mutex
} }
func New(cfg *Config) *Device { func NewDevice(cfg *Config) *Device {
d := Device{ return &Device{
cfg: cfg, cfg: cfg,
response: make([]byte, 1500), response: make([]byte, 1500),
data: make([]byte, 0, 1500), data: make([]byte, 0, 1500),
} }
drivers.UseNetdev(&d)
// assert that driver implements Netlinker
var _ drivers.Netlinker = (*Device)(nil)
return &d
} }
func (d *Device) NetConnect() error { func (d *Device) NetConnect(params *netlink.ConnectParams) error {
if len(d.cfg.Ssid) == 0 { if len(params.Ssid) == 0 {
return drivers.ErrMissingSSID return netlink.ErrMissingSSID
} }
d.uart = d.cfg.Uart d.uart = d.cfg.Uart
@@ -98,17 +88,17 @@ func (d *Device) NetConnect() error {
if !d.Connected() { if !d.Connected() {
fmt.Printf("FAILED\r\n") fmt.Printf("FAILED\r\n")
return drivers.ErrConnectFailed return netlink.ErrConnectFailed
} }
fmt.Printf("CONNECTED\r\n") fmt.Printf("CONNECTED\r\n")
// Connect to Wifi AP // Connect to Wifi AP
fmt.Printf("Connecting to Wifi SSID '%s'...", d.cfg.Ssid) fmt.Printf("Connecting to Wifi SSID '%s'...", params.Ssid)
d.SetWifiMode(WifiModeClient) d.SetWifiMode(WifiModeClient)
err := d.ConnectToAP(d.cfg.Ssid, d.cfg.Passphrase, 10 /* secs */) err := d.ConnectToAP(params.Ssid, params.Passphrase, 10 /* secs */)
if err != nil { if err != nil {
fmt.Printf("FAILED\r\n") fmt.Printf("FAILED\r\n")
return err return err
@@ -128,20 +118,27 @@ func (d *Device) NetConnect() error {
func (d *Device) NetDisconnect() { func (d *Device) NetDisconnect() {
d.DisconnectFromAP() d.DisconnectFromAP()
fmt.Printf("\r\nDisconnected from Wifi SSID '%s'\r\n\r\n", d.cfg.Ssid) fmt.Printf("\r\nDisconnected from Wifi\r\n\r\n")
} }
func (d *Device) NetNotify(cb func(drivers.NetlinkEvent)) { func (d *Device) NetNotify(cb func(netlink.Event)) {
// Not supported // Not supported
} }
func (d *Device) SendEth(pkt []byte) error {
return netlink.ErrNotSupported
}
func (d *Device) RecvEthFunc(cb func(pkt []byte) error) {
}
func (d *Device) GetHostByName(name string) (net.IP, error) { func (d *Device) GetHostByName(name string) (net.IP, error) {
ip, err := d.GetDNS(name) ip, err := d.GetDNS(name)
return net.ParseIP(ip), err return net.ParseIP(ip), err
} }
func (d *Device) GetHardwareAddr() (net.HardwareAddr, error) { func (d *Device) GetHardwareAddr() (net.HardwareAddr, error) {
return net.HardwareAddr{}, drivers.ErrNotSupported return net.HardwareAddr{}, netlink.ErrNotSupported
} }
func (d *Device) GetIPAddr() (net.IP, error) { func (d *Device) GetIPAddr() (net.IP, error) {
@@ -162,22 +159,22 @@ func (d *Device) GetIPAddr() (net.IP, error) {
func (d *Device) Socket(domain int, stype int, protocol int) (int, error) { func (d *Device) Socket(domain int, stype int, protocol int) (int, error) {
switch domain { switch domain {
case drivers.AF_INET: case netdev.AF_INET:
default: default:
return -1, drivers.ErrFamilyNotSupported return -1, netdev.ErrFamilyNotSupported
} }
switch { switch {
case protocol == drivers.IPPROTO_TCP && stype == drivers.SOCK_STREAM: case protocol == netdev.IPPROTO_TCP && stype == netdev.SOCK_STREAM:
case protocol == drivers.IPPROTO_TLS && stype == drivers.SOCK_STREAM: case protocol == netdev.IPPROTO_TLS && stype == netdev.SOCK_STREAM:
case protocol == drivers.IPPROTO_UDP && stype == drivers.SOCK_DGRAM: case protocol == netdev.IPPROTO_UDP && stype == netdev.SOCK_DGRAM:
default: default:
return -1, drivers.ErrProtocolNotSupported return -1, netdev.ErrProtocolNotSupported
} }
// Only supporting single connection mode, so only one socket at a time // Only supporting single connection mode, so only one socket at a time
if d.socket.inUse { if d.socket.inUse {
return -1, drivers.ErrNoMoreSockets return -1, netdev.ErrNoMoreSockets
} }
d.socket.inUse = true d.socket.inUse = true
d.socket.protocol = protocol d.socket.protocol = protocol
@@ -198,11 +195,11 @@ func (d *Device) Connect(sockfd int, host string, ip net.IP, port int) error {
var lport = strconv.Itoa(d.socket.lport) var lport = strconv.Itoa(d.socket.lport)
switch d.socket.protocol { switch d.socket.protocol {
case drivers.IPPROTO_TCP: case netdev.IPPROTO_TCP:
err = d.ConnectTCPSocket(addr, rport) err = d.ConnectTCPSocket(addr, rport)
case drivers.IPPROTO_UDP: case netdev.IPPROTO_UDP:
err = d.ConnectUDPSocket(addr, rport, lport) err = d.ConnectUDPSocket(addr, rport, lport)
case drivers.IPPROTO_TLS: case netdev.IPPROTO_TLS:
err = d.ConnectSSLSocket(host, rport) err = d.ConnectSSLSocket(host, rport)
} }
@@ -219,22 +216,22 @@ func (d *Device) Connect(sockfd int, host string, ip net.IP, port int) error {
func (d *Device) Listen(sockfd int, backlog int) error { func (d *Device) Listen(sockfd int, backlog int) error {
switch d.socket.protocol { switch d.socket.protocol {
case drivers.IPPROTO_UDP: case netdev.IPPROTO_UDP:
default: default:
return drivers.ErrProtocolNotSupported return netdev.ErrProtocolNotSupported
} }
return nil return nil
} }
func (d *Device) Accept(sockfd int, ip net.IP, port int) (int, error) { func (d *Device) Accept(sockfd int, ip net.IP, port int) (int, error) {
return -1, drivers.ErrNotSupported return -1, netdev.ErrNotSupported
} }
func (d *Device) sendChunk(sockfd int, buf []byte, deadline time.Time) (int, error) { func (d *Device) sendChunk(sockfd int, buf []byte, deadline time.Time) (int, error) {
// Check if we've timed out // Check if we've timed out
if !deadline.IsZero() { if !deadline.IsZero() {
if time.Now().After(deadline) { if time.Now().After(deadline) {
return -1, drivers.ErrTimeout return -1, netdev.ErrTimeout
} }
} }
err := d.StartSocketSend(len(buf)) err := d.StartSocketSend(len(buf))
@@ -290,7 +287,7 @@ func (d *Device) Recv(sockfd int, buf []byte, flags int, deadline time.Time) (in
// Check if we've timed out // Check if we've timed out
if !deadline.IsZero() { if !deadline.IsZero() {
if time.Now().After(deadline) { if time.Now().After(deadline) {
return -1, drivers.ErrTimeout return -1, netdev.ErrTimeout
} }
} }
@@ -318,7 +315,7 @@ func (d *Device) Close(sockfd int) error {
} }
func (d *Device) SetSockOpt(sockfd int, level int, opt int, value interface{}) error { func (d *Device) SetSockOpt(sockfd int, level int, opt int, value interface{}) error {
return drivers.ErrNotSupported return netdev.ErrNotSupported
} }
// Connected checks if there is communication with the ESP8266/ESP32. // Connected checks if there is communication with the ESP8266/ESP32.
+12 -1
View File
@@ -9,6 +9,8 @@
// examples/net/webclient (for HTTP) // examples/net/webclient (for HTTP)
// examples/net/tlsclient (for HTTPS) // examples/net/tlsclient (for HTTPS)
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -20,6 +22,9 @@ import (
"net/url" "net/url"
"strings" "strings"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -31,7 +36,13 @@ func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+13 -2
View File
@@ -9,6 +9,8 @@
// examples/net/webclient (for HTTP) // examples/net/webclient (for HTTP)
// examples/net/tlsclient (for HTTPS) // examples/net/tlsclient (for HTTPS)
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -18,6 +20,9 @@ import (
"machine" "machine"
"net/http" "net/http"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -30,7 +35,13 @@ func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -46,7 +57,7 @@ func main() {
} }
fmt.Println(string(buf.Bytes())) fmt.Println(string(buf.Bytes()))
netdev.NetDisconnect() link.NetDisconnect()
} }
// Wait for user to open serial console // Wait for user to open serial console
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+13 -2
View File
@@ -9,6 +9,8 @@
// examples/net/webclient (for HTTP) // examples/net/webclient (for HTTP)
// examples/net/tlsclient (for HTTPS) // examples/net/tlsclient (for HTTPS)
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -19,6 +21,9 @@ import (
"machine" "machine"
"net/http" "net/http"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -30,7 +35,13 @@ func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -50,7 +61,7 @@ func main() {
fmt.Println(string(body)) fmt.Println(string(body))
netdev.NetDisconnect() link.NetDisconnect()
} }
// Wait for user to open serial console // Wait for user to open serial console
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+12 -1
View File
@@ -9,6 +9,8 @@
// examples/net/webclient (for HTTP) // examples/net/webclient (for HTTP)
// examples/net/tlsclient (for HTTPS) // examples/net/tlsclient (for HTTPS)
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -19,6 +21,9 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -30,7 +35,13 @@ func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
-23
View File
@@ -1,23 +0,0 @@
//go:build challenger_rp2040
// +build: challenger_rp2040
package main
import (
"machine"
"tinygo.org/x/drivers/espat"
)
var cfg = espat.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// UART
Uart: machine.UART1,
Tx: machine.UART1_TX_PIN,
Rx: machine.UART1_RX_PIN,
}
var netdev = espat.New(&cfg)
+12 -2
View File
@@ -4,6 +4,8 @@
// Note: It may be necessary to increase the stack size when using // Note: It may be necessary to increase the stack size when using
// paho.mqtt.golang. Use the -stack-size=4KB command line option. // paho.mqtt.golang. Use the -stack-size=4KB command line option.
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040
package main package main
import ( import (
@@ -18,6 +20,8 @@ import (
"time" "time"
mqtt "github.com/soypat/natiu-mqtt" mqtt "github.com/soypat/natiu-mqtt"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -30,7 +34,13 @@ var (
func main() { func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -99,7 +109,7 @@ func main() {
time.Sleep(time.Second) time.Sleep(time.Second)
conn.SetReadDeadline(time.Now().Add(10*time.Second)) conn.SetReadDeadline(time.Now().Add(10 * time.Second))
err = client.HandleNext() err = client.HandleNext()
if err != nil { if err != nil {
log.Fatal("handle next: ", err) log.Fatal("handle next: ", err)
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
-23
View File
@@ -1,23 +0,0 @@
//go:build challenger_rp2040
// +build: challenger_rp2040
package main
import (
"machine"
"tinygo.org/x/drivers/espat"
)
var cfg = espat.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// UART
Uart: machine.UART1,
Tx: machine.UART1_TX_PIN,
Rx: machine.UART1_RX_PIN,
}
var netdev = espat.New(&cfg)
+11 -1
View File
@@ -4,6 +4,8 @@
// Note: It may be necessary to increase the stack size when using // Note: It may be necessary to increase the stack size when using
// paho.mqtt.golang. Use the -stack-size=4KB command line option. // paho.mqtt.golang. Use the -stack-size=4KB command line option.
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040
package main package main
import ( import (
@@ -14,6 +16,8 @@ import (
"time" "time"
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -37,7 +41,13 @@ var connectionLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client,
func main() { func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
-23
View File
@@ -1,23 +0,0 @@
//go:build challenger_rp2040
// +build: challenger_rp2040
package main
import (
"machine"
"tinygo.org/x/drivers/espat"
)
var cfg = espat.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// UART
Uart: machine.UART1,
Tx: machine.UART1_TX_PIN,
Rx: machine.UART1_RX_PIN,
}
var netdev = espat.New(&cfg)
+13 -2
View File
@@ -3,6 +3,8 @@
// It creates a UDP connection to request the current time and parse the // It creates a UDP connection to request the current time and parse the
// response from a NTP server. The system time is set to NTP time. // response from a NTP server. The system time is set to NTP time.
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040
package main package main
import ( import (
@@ -13,6 +15,9 @@ import (
"net" "net"
"runtime" "runtime"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -30,7 +35,13 @@ func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -49,7 +60,7 @@ func main() {
} }
conn.Close() conn.Close()
netdev.NetDisconnect() link.NetDisconnect()
runtime.AdjustTimeOffset(-1 * int64(time.Since(t))) runtime.AdjustTimeOffset(-1 * int64(time.Since(t)))
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal arduino_nano33 nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
-23
View File
@@ -1,23 +0,0 @@
//go:build challenger_rp2040
// +build: challenger_rp2040
package main
import (
"machine"
"tinygo.org/x/drivers/espat"
)
var cfg = espat.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// UART
Uart: machine.UART1,
Tx: machine.UART1_TX_PIN,
Rx: machine.UART1_RX_PIN,
}
var netdev = espat.New(&cfg)
+20 -8
View File
@@ -4,6 +4,8 @@
// //
// nc -lk 8080 // nc -lk 8080
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040
package main package main
import ( import (
@@ -15,7 +17,9 @@ import (
"strconv" "strconv"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers/netdev"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -25,12 +29,20 @@ var (
) )
var buf = &bytes.Buffer{} var buf = &bytes.Buffer{}
var link netlink.Netlinker
var dev netdev.Netdever
func main() { func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, dev = probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -48,9 +60,9 @@ func sendBatch() {
// make TCP connection // make TCP connection
message("---------------\r\nDialing TCP connection") message("---------------\r\nDialing TCP connection")
fd, _ := netdev.Socket(drivers.AF_INET, drivers.SOCK_STREAM, drivers.IPPROTO_TCP) fd, _ := dev.Socket(netdev.AF_INET, netdev.SOCK_STREAM, netdev.IPPROTO_TCP)
err := netdev.Connect(fd, "", ip, port) err := dev.Connect(fd, "", ip, port)
for ; err != nil; err = netdev.Connect(fd, "", ip, port) { for ; err != nil; err = dev.Connect(fd, "", ip, port) {
message(err.Error()) message(err.Error())
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
} }
@@ -67,7 +79,7 @@ func sendBatch() {
fmt.Fprint(buf, fmt.Fprint(buf,
"\r---------------------------- i == ", i, " ----------------------------"+ "\r---------------------------- i == ", i, " ----------------------------"+
"\r---------------------------- i == ", i, " ----------------------------") "\r---------------------------- i == ", i, " ----------------------------")
if w, err = netdev.Send(fd, buf.Bytes(), 0, time.Time{}); err != nil { if w, err = dev.Send(fd, buf.Bytes(), 0, time.Time{}); err != nil {
println("error:", err.Error(), "\r") println("error:", err.Error(), "\r")
break break
} }
@@ -79,12 +91,12 @@ func sendBatch() {
fmt.Fprint(buf, "\nWrote ", n, " bytes in ", ms, " ms\r\n") fmt.Fprint(buf, "\nWrote ", n, " bytes in ", ms, " ms\r\n")
message(buf.String()) message(buf.String())
if _, err := netdev.Send(fd, buf.Bytes(), 0, time.Time{}); err != nil { if _, err := dev.Send(fd, buf.Bytes(), 0, time.Time{}); err != nil {
println("error:", err.Error(), "\r") println("error:", err.Error(), "\r")
} }
println("Disconnecting TCP...") println("Disconnecting TCP...")
netdev.Close(fd) dev.Close(fd)
} }
func message(msg string) { func message(msg string) {
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal arduino_nano33 nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
-23
View File
@@ -1,23 +0,0 @@
//go:build challenger_rp2040
// +build: challenger_rp2040
package main
import (
"machine"
"tinygo.org/x/drivers/espat"
)
var cfg = espat.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// UART
Uart: machine.UART1,
Tx: machine.UART1_TX_PIN,
Rx: machine.UART1_RX_PIN,
}
var netdev = espat.New(&cfg)
+12 -1
View File
@@ -5,6 +5,8 @@
// //
// nc -lk 8080 // nc -lk 8080
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040 || pico
package main package main
import ( import (
@@ -14,6 +16,9 @@ import (
"machine" "machine"
"net" "net"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -28,7 +33,13 @@ func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal arduino_nano33 nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+12 -1
View File
@@ -7,6 +7,8 @@
// //
// $ nc 10.0.0.2 8080 <file >copy ; cmp file copy // $ nc 10.0.0.2 8080 <file >copy ; cmp file copy
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -14,6 +16,9 @@ import (
"log" "log"
"net" "net"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -36,7 +41,13 @@ func main() {
time.Sleep(time.Second) time.Sleep(time.Second)
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal arduino_nano33 nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+12 -1
View File
@@ -5,6 +5,8 @@
// //
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -17,6 +19,9 @@ import (
"net" "net"
"strings" "strings"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -80,7 +85,13 @@ func makeRequest() {
func main() { func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal arduino_nano33 nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+8 -29
View File
@@ -6,8 +6,6 @@
//go:build wioterminal //go:build wioterminal
// +build: wioterminal
package main package main
import ( import (
@@ -21,9 +19,9 @@ import (
"strings" "strings"
"time" "time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/ili9341" "tinygo.org/x/drivers/ili9341"
"tinygo.org/x/drivers/rtl8720dn" "tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
"tinygo.org/x/tinyfont/proggy" "tinygo.org/x/tinyfont/proggy"
"tinygo.org/x/tinyterm" "tinygo.org/x/tinyterm"
) )
@@ -34,18 +32,6 @@ var (
) )
var ( var (
netcfg = rtl8720dn.Config{
Ssid: ssid,
Passphrase: pass,
En: machine.RTL8720D_CHIP_PU,
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
}
netdev = rtl8720dn.New(&netcfg)
display = ili9341.NewSPI( display = ili9341.NewSPI(
machine.SPI3, machine.SPI3,
machine.LCD_DC, machine.LCD_DC,
@@ -66,17 +52,6 @@ var (
font = &proggy.TinySZ8pt7b font = &proggy.TinySZ8pt7b
) )
func notify(e drivers.NetlinkEvent) {
switch e {
case drivers.NetlinkEventNetUp:
fmt.Println("Wifi connection UP")
fmt.Fprintf(terminal, "Wifi connection UP")
case drivers.NetlinkEventNetDown:
fmt.Println("Wifi connection DOWN")
fmt.Fprintf(terminal, "Wifi connection DOWN")
}
}
func main() { func main() {
machine.SPI3.Configure(machine.SPIConfig{ machine.SPI3.Configure(machine.SPIConfig{
@@ -100,9 +75,13 @@ func main() {
fmt.Fprintf(terminal, "Connecting to %s...\r\n", ssid) fmt.Fprintf(terminal, "Connecting to %s...\r\n", ssid)
netdev.NetNotify(notify) link, _ := probe.Probe()
if err := netdev.NetConnect(); err != nil { err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
+12 -1
View File
@@ -17,6 +17,8 @@
// } // }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -28,6 +30,9 @@ import (
"net" "net"
"strings" "strings"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -91,7 +96,13 @@ func closeConnection() {
func main() { func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal arduino_nano33 nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+13 -2
View File
@@ -6,6 +6,8 @@
// Note: It may be necessary to increase the stack size when using "net/http". // Note: It may be necessary to increase the stack size when using "net/http".
// Use the -stack-size=4KB command line option. // Use the -stack-size=4KB command line option.
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -15,6 +17,9 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -37,7 +42,13 @@ func main() {
led.Configure(machine.PinConfig{Mode: machine.PinOutput}) led.Configure(machine.PinConfig{Mode: machine.PinOutput})
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -48,7 +59,7 @@ func main() {
http.HandleFunc("/off", LED_OFF) http.HandleFunc("/off", LED_OFF)
http.HandleFunc("/on", LED_ON) http.HandleFunc("/on", LED_ON)
err := http.ListenAndServe(port, nil) err = http.ListenAndServe(port, nil)
for err != nil { for err != nil {
fmt.Printf("error: %s\r\n", err.Error()) fmt.Printf("error: %s\r\n", err.Error())
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+11 -1
View File
@@ -6,6 +6,8 @@
// Note: It may be necessary to increase the stack size when using // Note: It may be necessary to increase the stack size when using
// "golang.org/x/net/websocket". Use the -stack-size=4KB command line option. // "golang.org/x/net/websocket". Use the -stack-size=4KB command line option.
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -15,6 +17,8 @@ import (
"time" "time"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -33,7 +37,13 @@ func waitSerial() {
func main() { func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+12 -2
View File
@@ -6,6 +6,8 @@
// Note: It may be necessary to increase the stack size when using // Note: It may be necessary to increase the stack size when using
// "golang.org/x/net/websocket". Use the -stack-size=4KB command line option. // "golang.org/x/net/websocket". Use the -stack-size=4KB command line option.
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -16,6 +18,8 @@ import (
"time" "time"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -40,12 +44,18 @@ func waitSerial() {
func main() { func main() {
waitSerial() waitSerial()
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
http.Handle("/echo", websocket.Handler(EchoServer)) http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(port, nil) err = http.ListenAndServe(port, nil)
if err != nil { if err != nil {
panic("ListenAndServe: " + err.Error()) panic("ListenAndServe: " + err.Error())
} }
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+12 -1
View File
@@ -3,6 +3,8 @@
// Note: It may be necessary to increase the stack size when using "net/http". // Note: It may be necessary to increase the stack size when using "net/http".
// Use the -stack-size=4KB command line option. // Use the -stack-size=4KB command line option.
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
package main package main
import ( import (
@@ -10,6 +12,9 @@ import (
"log" "log"
"net/http" "net/http"
"time" "time"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
) )
var ( var (
@@ -25,7 +30,13 @@ func main() {
// wait a bit for console // wait a bit for console
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
if err := netdev.NetConnect(); err != nil { link, _ := probe.Probe()
err := link.NetConnect(&netlink.ConnectParams{
Ssid: ssid,
Passphrase: pass,
})
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
-29
View File
@@ -1,29 +0,0 @@
//go:build wioterminal
// +build: wioterminal
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var cfg = rtl8720dn.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// Device
En: machine.RTL8720D_CHIP_PU,
// UART
Uart: machine.UART3,
Tx: machine.PB24,
Rx: machine.PC24,
Baudrate: 614400,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = rtl8720dn.New(&cfg)
-33
View File
@@ -1,33 +0,0 @@
//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4
// +build: pyportal nano_rp2040 metro_m4_airlift arduino_mkrwifi1010 matrixportal_m4
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/wifinina"
)
var cfg = wifinina.Config{
// WiFi AP credentials
Ssid: ssid,
Passphrase: pass,
// 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,
// Watchdog (set to 0 to disable)
WatchdogTimeout: time.Duration(20 * time.Second),
}
var netdev = wifinina.New(&cfg)
+17 -11
View File
@@ -1,4 +1,6 @@
package drivers // L3/L4 network/transport layer
package netdev
import ( import (
"errors" "errors"
@@ -32,6 +34,7 @@ var (
var ( var (
ErrFamilyNotSupported = errors.New("Address family not supported") ErrFamilyNotSupported = errors.New("Address family not supported")
ErrProtocolNotSupported = errors.New("Socket protocol/type not supported") ErrProtocolNotSupported = errors.New("Socket protocol/type not supported")
ErrStartingDHCPClient = errors.New("Error starting DHPC client")
ErrNoMoreSockets = errors.New("No more sockets") ErrNoMoreSockets = errors.New("No more sockets")
ErrClosingSocket = errors.New("Error closing socket") ErrClosingSocket = errors.New("Error closing socket")
ErrNotSupported = errors.New("Not supported") ErrNotSupported = errors.New("Not supported")
@@ -47,29 +50,32 @@ func (e *timeoutError) Timeout() bool { return true }
func (e *timeoutError) Temporary() bool { return true } func (e *timeoutError) Temporary() bool { return true }
//go:linkname UseNetdev net.useNetdev //go:linkname UseNetdev net.useNetdev
func UseNetdev(dev netdever) func UseNetdev(dev Netdever)
// Netdev is TinyGo's network device driver model. Network drivers implement // Netdever is TinyGo's OSI L3/L4 network/transport layer interface. Network
// the netdever interface, providing a common network I/O interface to TinyGo's // drivers implement the Netdever interface, providing a common network L3/L4
// "net" package. The interface is modeled after the BSD socket interface. // interface to TinyGo's "net" package. net.Conn implementations (TCPConn,
// net.Conn implementations (TCPConn, UDPConn, and TLSConn) use the netdev // UDPConn, and TLSConn) use the Netdever interface for device I/O access.
// interface for device I/O access.
// //
// A netdever is passed to the "net" package using UseNetdev(). // A Netdever is passed to the "net" package using UseNetdev().
// //
// Just like a net.Conn, multiple goroutines may invoke methods on a netdever // Just like a net.Conn, multiple goroutines may invoke methods on a Netdever
// simultaneously. // simultaneously.
// //
// NOTE: The netdever interface is mirrored in tinygo/src/net/netdev.go. // NOTE: The Netdever interface is mirrored in tinygo/src/net/netdev.go.
// NOTE: If making changes to this interface, mirror the changes in // NOTE: If making changes to this interface, mirror the changes in
// NOTE: tinygo/src/net/netdev.go, and vice-versa. // NOTE: tinygo/src/net/netdev.go, and vice-versa.
type netdever interface { type Netdever interface {
// GetHostByName returns the IP address of either a hostname or IPv4 // GetHostByName returns the IP address of either a hostname or IPv4
// address in standard dot notation // address in standard dot notation
GetHostByName(name string) (net.IP, error) GetHostByName(name string) (net.IP, error)
// GetIPAddr returns IP address assigned to the interface, either by
// DHCP or statically
GetIPAddr() (net.IP, error)
// Berkely Sockets-like interface, Go-ified. See man page for socket(2), etc. // Berkely Sockets-like interface, Go-ified. See man page for socket(2), etc.
Socket(domain int, stype int, protocol int) (int, error) Socket(domain int, stype int, protocol int) (int, error)
Bind(sockfd int, ip net.IP, port int) error Bind(sockfd int, ip net.IP, port int) error
-49
View File
@@ -1,49 +0,0 @@
package drivers
import (
"errors"
"net"
)
// NetConnect() errors
var (
ErrConnected = errors.New("Already connected")
ErrConnectFailed = errors.New("Connect failed")
ErrConnectTimeout = errors.New("Connect timed out")
ErrMissingSSID = errors.New("Missing WiFi SSID")
ErrStartingDHCPClient = errors.New("Error starting DHPC client")
)
type NetlinkEvent int
// Netlink network events
const (
// The device's network connection is now UP
NetlinkEventNetUp NetlinkEvent = iota
// The device's network connection is now DOWN
NetlinkEventNetDown
)
// Network drivers (optionally) implement the Netlinker interface. This
// interface is not used by TinyGo's "net" package, but rather provides the
// TinyGo application direct access to the network device for common settings
// and control that fall outside of netdev's socket interface.
type Netlinker interface {
// NetConnect device to IP network
NetConnect() error
// NetDisconnect device from IP network
NetDisconnect()
// NetNotify to register callback for network events
NetNotify(func(NetlinkEvent))
// GetHardwareAddr returns device MAC address
GetHardwareAddr() (net.HardwareAddr, error)
// GetIPAddr returns IP address assigned to device, either by DHCP or
// statically
GetIPAddr() (net.IP, error)
}
+103
View File
@@ -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)
}
+26
View File
@@ -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
}
+36
View File
@@ -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
}
+29
View File
@@ -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
}
+33
View File
@@ -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
}
+68 -77
View File
@@ -16,7 +16,8 @@ import (
"sync" "sync"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers/netdev"
"tinygo.org/x/drivers/netlink"
) )
var _debug debug = debugBasic var _debug debug = debugBasic
@@ -42,10 +43,6 @@ type socket struct {
} }
type Config struct { type Config struct {
// AP creditials
Ssid string
Passphrase string
// Enable // Enable
En machine.Pin En machine.Pin
@@ -54,21 +51,11 @@ type Config struct {
Tx machine.Pin Tx machine.Pin
Rx machine.Pin Rx machine.Pin
Baudrate uint32 Baudrate uint32
// Retries is how many attempts to connect before returning with a
// "Connect failed" error. Zero means infinite retries.
Retries int
// Watchdog ticker duration. On tick, the watchdog will check for
// downed connection and try to recover the connection. Default is
// 0secs, which means no watchdog. Set to non-zero to enable
// watchodog.
WatchdogTimeout time.Duration
} }
type rtl8720dn struct { type rtl8720dn struct {
cfg *Config cfg *Config
notifyCb func(drivers.NetlinkEvent) notifyCb func(netlink.Event)
mu sync.Mutex mu sync.Mutex
uart *machine.UART uart *machine.UART
@@ -76,6 +63,8 @@ type rtl8720dn struct {
debug bool debug bool
params *netlink.ConnectParams
netConnected bool netConnected bool
driverShown bool driverShown bool
deviceShown bool deviceShown bool
@@ -91,46 +80,39 @@ func newSocket(protocol int) *socket {
} }
func New(cfg *Config) *rtl8720dn { func New(cfg *Config) *rtl8720dn {
r := rtl8720dn{ return &rtl8720dn{
debug: (_debug & debugRpc) != 0, debug: (_debug & debugRpc) != 0,
cfg: cfg, cfg: cfg,
sockets: make(map[sock]*socket), sockets: make(map[sock]*socket),
killWatchdog: make(chan bool), killWatchdog: make(chan bool),
} }
drivers.UseNetdev(&r)
// assert that rtl8720dn implements Netlinker
var _ drivers.Netlinker = (*rtl8720dn)(nil)
return &r
} }
func (r *rtl8720dn) startDhcpc() error { func (r *rtl8720dn) startDhcpc() error {
if result := r.rpc_tcpip_adapter_dhcpc_start(0); result == -1 { if result := r.rpc_tcpip_adapter_dhcpc_start(0); result == -1 {
return drivers.ErrStartingDHCPClient return netdev.ErrStartingDHCPClient
} }
return nil return nil
} }
func (r *rtl8720dn) connectToAP() error { func (r *rtl8720dn) connectToAP() error {
if len(r.cfg.Ssid) == 0 { if len(r.params.Ssid) == 0 {
return drivers.ErrMissingSSID return netlink.ErrMissingSSID
} }
if debugging(debugBasic) { if debugging(debugBasic) {
fmt.Printf("Connecting to Wifi SSID '%s'...", r.cfg.Ssid) fmt.Printf("Connecting to Wifi SSID '%s'...", r.params.Ssid)
} }
// Start the connection process // Start the connection process
securityType := uint32(0x00400004) securityType := uint32(0x00400004)
result := r.rpc_wifi_connect(r.cfg.Ssid, r.cfg.Passphrase, securityType, -1, 0) result := r.rpc_wifi_connect(r.params.Ssid, r.params.Passphrase, securityType, -1, 0)
if result == -1 { if result == -1 {
if debugging(debugBasic) { if debugging(debugBasic) {
fmt.Printf("FAILED\r\n") fmt.Printf("FAILED\r\n")
} }
return drivers.ErrConnectFailed return netlink.ErrConnectFailed
} }
if debugging(debugBasic) { if debugging(debugBasic) {
@@ -138,7 +120,7 @@ func (r *rtl8720dn) connectToAP() error {
} }
if r.notifyCb != nil { if r.notifyCb != nil {
r.notifyCb(drivers.NetlinkEventNetUp) r.notifyCb(netlink.EventNetUp)
} }
return r.startDhcpc() return r.startDhcpc()
@@ -226,7 +208,7 @@ func (r *rtl8720dn) networkDown() bool {
} }
func (r *rtl8720dn) watchdog() { func (r *rtl8720dn) watchdog() {
ticker := time.NewTicker(r.cfg.WatchdogTimeout) ticker := time.NewTicker(r.params.WatchdogTimeout)
for { for {
select { select {
case <-r.killWatchdog: case <-r.killWatchdog:
@@ -238,7 +220,7 @@ func (r *rtl8720dn) watchdog() {
fmt.Printf("Watchdog: Wifi NOT CONNECTED, trying again...\r\n") fmt.Printf("Watchdog: Wifi NOT CONNECTED, trying again...\r\n")
} }
if r.notifyCb != nil { if r.notifyCb != nil {
r.notifyCb(drivers.NetlinkEventNetDown) r.notifyCb(netlink.EventNetDown)
} }
r.netConnect(false) r.netConnect(false)
} }
@@ -255,9 +237,9 @@ func (r *rtl8720dn) netConnect(reset bool) error {
} }
r.showDevice() r.showDevice()
for i := 0; r.cfg.Retries == 0 || i < r.cfg.Retries; i++ { for i := 0; r.params.Retries == 0 || i < r.params.Retries; i++ {
if err := r.connectToAP(); err != nil { if err := r.connectToAP(); err != nil {
if err == drivers.ErrConnectFailed { if err == netlink.ErrConnectFailed {
continue continue
} }
return err return err
@@ -266,22 +248,24 @@ func (r *rtl8720dn) netConnect(reset bool) error {
} }
if r.networkDown() { if r.networkDown() {
return drivers.ErrConnectFailed return netlink.ErrConnectFailed
} }
r.showIP() r.showIP()
return nil return nil
} }
func (r *rtl8720dn) NetConnect() error { func (r *rtl8720dn) NetConnect(params *netlink.ConnectParams) error {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
if r.netConnected { if r.netConnected {
return drivers.ErrConnected return netlink.ErrConnected
} }
r.params = params
r.showDriver() r.showDriver()
if err := r.netConnect(true); err != nil { if err := r.netConnect(true); err != nil {
@@ -290,7 +274,7 @@ func (r *rtl8720dn) NetConnect() error {
r.netConnected = true r.netConnected = true
if r.cfg.WatchdogTimeout != 0 { if r.params.WatchdogTimeout != 0 {
go r.watchdog() go r.watchdog()
} }
@@ -310,7 +294,7 @@ func (r *rtl8720dn) NetDisconnect() {
return return
} }
if r.cfg.WatchdogTimeout != 0 { if r.params.WatchdogTimeout != 0 {
r.killWatchdog <- true r.killWatchdog <- true
} }
r.netDisconnect() r.netDisconnect()
@@ -319,18 +303,25 @@ func (r *rtl8720dn) NetDisconnect() {
r.netConnected = false r.netConnected = false
if debugging(debugBasic) { if debugging(debugBasic) {
fmt.Printf("\r\nDisconnected from Wifi SSID '%s'\r\n\r\n", r.cfg.Ssid) fmt.Printf("\r\nDisconnected from Wifi SSID '%s'\r\n\r\n", r.params.Ssid)
} }
if r.notifyCb != nil { if r.notifyCb != nil {
r.notifyCb(drivers.NetlinkEventNetDown) r.notifyCb(netlink.EventNetDown)
} }
} }
func (r *rtl8720dn) NetNotify(cb func(drivers.NetlinkEvent)) { func (r *rtl8720dn) NetNotify(cb func(netlink.Event)) {
r.notifyCb = cb r.notifyCb = cb
} }
func (r *rtl8720dn) SendEth(pkt []byte) error {
return netlink.ErrNotSupported
}
func (r *rtl8720dn) RecvEthFunc(cb func(pkt []byte) error) {
}
func (r *rtl8720dn) GetHostByName(name string) (net.IP, error) { func (r *rtl8720dn) GetHostByName(name string) (net.IP, error) {
if debugging(debugNetdev) { if debugging(debugNetdev) {
@@ -343,7 +334,7 @@ func (r *rtl8720dn) GetHostByName(name string) (net.IP, error) {
var ip [4]byte var ip [4]byte
result := r.rpc_netconn_gethostbyname(name, ip[:]) result := r.rpc_netconn_gethostbyname(name, ip[:])
if result == -1 { if result == -1 {
return net.IP{}, drivers.ErrHostUnknown return net.IP{}, netdev.ErrHostUnknown
} }
return net.IP(ip[:]), nil return net.IP(ip[:]), nil
@@ -396,9 +387,9 @@ func (r *rtl8720dn) Socket(domain int, stype int, protocol int) (int, error) {
} }
switch domain { switch domain {
case drivers.AF_INET: case netdev.AF_INET:
default: default:
return -1, drivers.ErrFamilyNotSupported return -1, netdev.ErrFamilyNotSupported
} }
var newSock int32 var newSock int32
@@ -407,22 +398,22 @@ func (r *rtl8720dn) Socket(domain int, stype int, protocol int) (int, error) {
defer r.mu.Unlock() defer r.mu.Unlock()
switch { switch {
case protocol == drivers.IPPROTO_TCP && stype == drivers.SOCK_STREAM: case protocol == netdev.IPPROTO_TCP && stype == netdev.SOCK_STREAM:
newSock = r.rpc_lwip_socket(drivers.AF_INET, drivers.SOCK_STREAM, newSock = r.rpc_lwip_socket(netdev.AF_INET, netdev.SOCK_STREAM,
drivers.IPPROTO_TCP) netdev.IPPROTO_TCP)
case protocol == drivers.IPPROTO_TLS && stype == drivers.SOCK_STREAM: case protocol == netdev.IPPROTO_TLS && stype == netdev.SOCK_STREAM:
// TODO Investigate: using client number as socket number; // TODO Investigate: using client number as socket number;
// TODO this may cause a problem if mixing TLS and non-TLS sockets? // TODO this may cause a problem if mixing TLS and non-TLS sockets?
newSock = int32(r.clientTLS()) newSock = int32(r.clientTLS())
case protocol == drivers.IPPROTO_UDP && stype == drivers.SOCK_DGRAM: case protocol == netdev.IPPROTO_UDP && stype == netdev.SOCK_DGRAM:
newSock = r.rpc_lwip_socket(drivers.AF_INET, drivers.SOCK_DGRAM, newSock = r.rpc_lwip_socket(netdev.AF_INET, netdev.SOCK_DGRAM,
drivers.IPPROTO_UDP) netdev.IPPROTO_UDP)
default: default:
return -1, drivers.ErrProtocolNotSupported return -1, netdev.ErrProtocolNotSupported
} }
if newSock == -1 { if newSock == -1 {
return -1, drivers.ErrNoMoreSockets return -1, netdev.ErrNoMoreSockets
} }
socket := newSocket(protocol) socket := newSocket(protocol)
@@ -434,7 +425,7 @@ func (r *rtl8720dn) Socket(domain int, stype int, protocol int) (int, error) {
func addrToName(ip net.IP, port int) []byte { func addrToName(ip net.IP, port int) []byte {
name := make([]byte, 16) name := make([]byte, 16)
name[0] = 0x00 name[0] = 0x00
name[1] = drivers.AF_INET name[1] = netdev.AF_INET
name[2] = byte(port >> 8) name[2] = byte(port >> 8)
name[3] = byte(port) name[3] = byte(port)
if len(ip) == 4 { if len(ip) == 4 {
@@ -461,13 +452,13 @@ func (r *rtl8720dn) Bind(sockfd int, ip net.IP, port int) error {
var name = addrToName(ip, port) var name = addrToName(ip, port)
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP, drivers.IPPROTO_UDP: case netdev.IPPROTO_TCP, netdev.IPPROTO_UDP:
result := r.rpc_lwip_bind(int32(sock), name, uint32(len(name))) result := r.rpc_lwip_bind(int32(sock), name, uint32(len(name)))
if result == -1 { if result == -1 {
return fmt.Errorf("Bind to %s:%d failed", ip, port) return fmt.Errorf("Bind to %s:%d failed", ip, port)
} }
default: default:
return drivers.ErrProtocolNotSupported return netdev.ErrProtocolNotSupported
} }
return nil return nil
@@ -492,12 +483,12 @@ func (r *rtl8720dn) Connect(sockfd int, host string, ip net.IP, port int) error
// Start the connection // Start the connection
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP, drivers.IPPROTO_UDP: case netdev.IPPROTO_TCP, netdev.IPPROTO_UDP:
result := r.rpc_lwip_connect(int32(sock), name, uint32(len(name))) result := r.rpc_lwip_connect(int32(sock), name, uint32(len(name)))
if result == -1 { if result == -1 {
return fmt.Errorf("Connect to %s:%d failed", ip, port) return fmt.Errorf("Connect to %s:%d failed", ip, port)
} }
case drivers.IPPROTO_TLS: case netdev.IPPROTO_TLS:
result := r.rpc_wifi_start_ssl_client(uint32(sock), result := r.rpc_wifi_start_ssl_client(uint32(sock),
host, uint32(port), 0) host, uint32(port), 0)
if result == -1 { if result == -1 {
@@ -521,22 +512,22 @@ func (r *rtl8720dn) Listen(sockfd int, backlog int) error {
var socket = r.sockets[sock] var socket = r.sockets[sock]
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP: case netdev.IPPROTO_TCP:
result := r.rpc_lwip_listen(int32(sock), int32(backlog)) result := r.rpc_lwip_listen(int32(sock), int32(backlog))
if result == -1 { if result == -1 {
return fmt.Errorf("Listen failed") return fmt.Errorf("Listen failed")
} }
result = r.rpc_lwip_fcntl(int32(sock), drivers.F_SETFL, O_NONBLOCK) result = r.rpc_lwip_fcntl(int32(sock), netdev.F_SETFL, O_NONBLOCK)
if result == -1 { if result == -1 {
return fmt.Errorf("Fcntl failed") return fmt.Errorf("Fcntl failed")
} }
case drivers.IPPROTO_UDP: case netdev.IPPROTO_UDP:
result := r.rpc_lwip_listen(int32(sock), int32(backlog)) result := r.rpc_lwip_listen(int32(sock), int32(backlog))
if result == -1 { if result == -1 {
return fmt.Errorf("Listen failed") return fmt.Errorf("Listen failed")
} }
default: default:
return drivers.ErrProtocolNotSupported return netdev.ErrProtocolNotSupported
} }
return nil return nil
@@ -557,9 +548,9 @@ func (r *rtl8720dn) Accept(sockfd int, ip net.IP, port int) (int, error) {
var addr = addrToName(ip, port) var addr = addrToName(ip, port)
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP: case netdev.IPPROTO_TCP:
default: default:
return -1, drivers.ErrProtocolNotSupported return -1, netdev.ErrProtocolNotSupported
} }
for { for {
@@ -606,18 +597,18 @@ func (r *rtl8720dn) sendChunk(sockfd int, buf []byte, deadline time.Time) (int,
// Check if we've timed out // Check if we've timed out
if !deadline.IsZero() { if !deadline.IsZero() {
if time.Now().After(deadline) { if time.Now().After(deadline) {
return -1, drivers.ErrTimeout return -1, netdev.ErrTimeout
} }
} }
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP, drivers.IPPROTO_UDP: case netdev.IPPROTO_TCP, netdev.IPPROTO_UDP:
result := r.rpc_lwip_send(int32(sock), buf, 0x00000008) result := r.rpc_lwip_send(int32(sock), buf, 0x00000008)
if result == -1 { if result == -1 {
return -1, fmt.Errorf("Send error") return -1, fmt.Errorf("Send error")
} }
return int(result), nil return int(result), nil
case drivers.IPPROTO_TLS: case netdev.IPPROTO_TLS:
result := r.rpc_wifi_send_ssl_data(uint32(sock), buf, uint16(len(buf))) result := r.rpc_wifi_send_ssl_data(uint32(sock), buf, uint16(len(buf)))
if result == -1 { if result == -1 {
return -1, fmt.Errorf("TLS Send error") return -1, fmt.Errorf("TLS Send error")
@@ -625,7 +616,7 @@ func (r *rtl8720dn) sendChunk(sockfd int, buf []byte, deadline time.Time) (int,
return int(result), nil return int(result), nil
} }
return -1, drivers.ErrProtocolNotSupported return -1, netdev.ErrProtocolNotSupported
} }
func (r *rtl8720dn) Send(sockfd int, buf []byte, flags int, func (r *rtl8720dn) Send(sockfd int, buf []byte, flags int,
@@ -681,15 +672,15 @@ func (r *rtl8720dn) Recv(sockfd int, buf []byte, flags int,
// Check if we've timed out // Check if we've timed out
if !deadline.IsZero() { if !deadline.IsZero() {
if time.Now().After(deadline) { if time.Now().After(deadline) {
return -1, drivers.ErrTimeout return -1, netdev.ErrTimeout
} }
} }
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP, drivers.IPPROTO_UDP: case netdev.IPPROTO_TCP, netdev.IPPROTO_UDP:
n = r.rpc_lwip_recv(int32(sock), buf[:length], n = r.rpc_lwip_recv(int32(sock), buf[:length],
uint32(length), 0x00000008, 0) uint32(length), 0x00000008, 0)
case drivers.IPPROTO_TLS: case netdev.IPPROTO_TLS:
n = r.rpc_wifi_get_ssl_receive(uint32(sock), n = r.rpc_wifi_get_ssl_receive(uint32(sock),
buf[:length], int32(length)) buf[:length], int32(length))
} }
@@ -734,15 +725,15 @@ func (r *rtl8720dn) Close(sockfd int) error {
} }
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP, drivers.IPPROTO_UDP: case netdev.IPPROTO_TCP, netdev.IPPROTO_UDP:
result = r.rpc_lwip_close(int32(sock)) result = r.rpc_lwip_close(int32(sock))
case drivers.IPPROTO_TLS: case netdev.IPPROTO_TLS:
r.rpc_wifi_stop_ssl_socket(uint32(sock)) r.rpc_wifi_stop_ssl_socket(uint32(sock))
r.rpc_wifi_ssl_client_destroy(uint32(sock)) r.rpc_wifi_ssl_client_destroy(uint32(sock))
} }
if result == -1 { if result == -1 {
return drivers.ErrClosingSocket return netdev.ErrClosingSocket
} }
socket.inuse = false socket.inuse = false
@@ -756,7 +747,7 @@ func (r *rtl8720dn) SetSockOpt(sockfd int, level int, opt int, value interface{}
fmt.Printf("[SetSockOpt] sockfd: %d\r\n", sockfd) fmt.Printf("[SetSockOpt] sockfd: %d\r\n", sockfd)
} }
return drivers.ErrNotSupported return netdev.ErrNotSupported
} }
func (r *rtl8720dn) disconnect() error { func (r *rtl8720dn) disconnect() error {
+70 -78
View File
@@ -21,6 +21,8 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/netdev"
"tinygo.org/x/drivers/netlink"
) )
var _debug debug = debugBasic var _debug debug = debugBasic
@@ -168,10 +170,6 @@ type socket struct {
} }
type Config struct { type Config struct {
// AP creditials
Ssid string
Passphrase string
// SPI config // SPI config
Spi drivers.SPI Spi drivers.SPI
Freq uint32 Freq uint32
@@ -189,24 +187,11 @@ type Config struct {
// Arduino MKR 1010, where the reset signal needs to go high instead of // Arduino MKR 1010, where the reset signal needs to go high instead of
// low. // low.
ResetIsHigh bool ResetIsHigh bool
// 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. Default is 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. Default is 0secs, which means no watchdog. Set to
// non-zero to enable watchodog.
WatchdogTimeout time.Duration
} }
type wifinina struct { type wifinina struct {
cfg *Config cfg *Config
notifyCb func(drivers.NetlinkEvent) notifyCb func(netlink.Event)
mu sync.Mutex mu sync.Mutex
spi drivers.SPI spi drivers.SPI
@@ -218,6 +203,8 @@ type wifinina struct {
buf [64]byte buf [64]byte
ssids [maxNetworks]string ssids [maxNetworks]string
params *netlink.ConnectParams
netConnected bool netConnected bool
driverShown bool driverShown bool
deviceShown bool deviceShown bool
@@ -244,15 +231,6 @@ func New(cfg *Config) *wifinina {
resetn: cfg.Resetn, resetn: cfg.Resetn,
} }
if w.cfg.ConnectTimeout == 0 {
w.cfg.ConnectTimeout = 10 * time.Second
}
drivers.UseNetdev(&w)
// assert that wifinina implements Netlinker
var _ drivers.Netlinker = (*wifinina)(nil)
return &w return &w
} }
@@ -273,20 +251,25 @@ func (w *wifinina) reason() string {
return fmt.Sprintf("%d", reason) return fmt.Sprintf("%d", reason)
} }
func (w *wifinina) connectToAP(timeout time.Duration) error { func (w *wifinina) connectToAP() error {
if len(w.cfg.Ssid) == 0 { timeout := w.params.ConnectTimeout
return drivers.ErrMissingSSID if timeout == 0 {
timeout = netlink.DefaultConnectTimeout
}
if len(w.params.Ssid) == 0 {
return netlink.ErrMissingSSID
} }
if debugging(debugBasic) { if debugging(debugBasic) {
fmt.Printf("Connecting to Wifi SSID '%s'...", w.cfg.Ssid) fmt.Printf("Connecting to Wifi SSID '%s'...", w.params.Ssid)
} }
start := time.Now() start := time.Now()
// Start the connection process // Start the connection process
w.setPassphrase(w.cfg.Ssid, w.cfg.Passphrase) w.setPassphrase(w.params.Ssid, w.params.Passphrase)
// Check if we connected // Check if we connected
for { for {
@@ -297,14 +280,14 @@ func (w *wifinina) connectToAP(timeout time.Duration) error {
fmt.Printf("CONNECTED\r\n") fmt.Printf("CONNECTED\r\n")
} }
if w.notifyCb != nil { if w.notifyCb != nil {
w.notifyCb(drivers.NetlinkEventNetUp) w.notifyCb(netlink.EventNetUp)
} }
return nil return nil
case statusConnectFailed: case statusConnectFailed:
if debugging(debugBasic) { if debugging(debugBasic) {
fmt.Printf("FAILED (%s)\r\n", w.reason()) fmt.Printf("FAILED (%s)\r\n", w.reason())
} }
return drivers.ErrConnectFailed return netlink.ErrConnectFailed
} }
if time.Since(start) > timeout { if time.Since(start) > timeout {
break break
@@ -316,7 +299,7 @@ func (w *wifinina) connectToAP(timeout time.Duration) error {
fmt.Printf("FAILED (timed out)\r\n") fmt.Printf("FAILED (timed out)\r\n")
} }
return drivers.ErrConnectTimeout return netlink.ErrConnectTimeout
} }
func (w *wifinina) netDisconnect() { func (w *wifinina) netDisconnect() {
@@ -404,7 +387,7 @@ func (w *wifinina) networkDown() bool {
} }
func (w *wifinina) watchdog() { func (w *wifinina) watchdog() {
ticker := time.NewTicker(w.cfg.WatchdogTimeout) ticker := time.NewTicker(w.params.WatchdogTimeout)
for { for {
select { select {
case <-w.killWatchdog: case <-w.killWatchdog:
@@ -423,7 +406,7 @@ func (w *wifinina) watchdog() {
fmt.Printf("Watchdog: Wifi NOT CONNECTED, trying again...\r\n") fmt.Printf("Watchdog: Wifi NOT CONNECTED, trying again...\r\n")
} }
if w.notifyCb != nil { if w.notifyCb != nil {
w.notifyCb(drivers.NetlinkEventNetDown) w.notifyCb(netlink.EventNetDown)
} }
w.netConnect(false) w.netConnect(false)
} }
@@ -438,10 +421,10 @@ func (w *wifinina) netConnect(reset bool) error {
} }
w.showDevice() w.showDevice()
for i := 0; w.cfg.Retries == 0 || i < w.cfg.Retries; i++ { for i := 0; w.params.Retries == 0 || i < w.params.Retries; i++ {
if err := w.connectToAP(w.cfg.ConnectTimeout); err != nil { if err := w.connectToAP(); err != nil {
switch err { switch err {
case drivers.ErrConnectTimeout, drivers.ErrConnectFailed: case netlink.ErrConnectTimeout, netlink.ErrConnectFailed:
continue continue
} }
return err return err
@@ -450,22 +433,24 @@ func (w *wifinina) netConnect(reset bool) error {
} }
if w.networkDown() { if w.networkDown() {
return drivers.ErrConnectFailed return netlink.ErrConnectFailed
} }
w.showIP() w.showIP()
return nil return nil
} }
func (w *wifinina) NetConnect() error { func (w *wifinina) NetConnect(params *netlink.ConnectParams) error {
w.mu.Lock() w.mu.Lock()
defer w.mu.Unlock() defer w.mu.Unlock()
if w.netConnected { if w.netConnected {
return drivers.ErrConnected return netlink.ErrConnected
} }
w.params = params
w.showDriver() w.showDriver()
w.setupSPI() w.setupSPI()
@@ -475,7 +460,7 @@ func (w *wifinina) NetConnect() error {
w.netConnected = true w.netConnected = true
if w.cfg.WatchdogTimeout != 0 { if w.params.WatchdogTimeout != 0 {
go w.watchdog() go w.watchdog()
} }
@@ -491,7 +476,7 @@ func (w *wifinina) NetDisconnect() {
return return
} }
if w.cfg.WatchdogTimeout != 0 { if w.params.WatchdogTimeout != 0 {
w.killWatchdog <- true w.killWatchdog <- true
} }
@@ -501,18 +486,25 @@ func (w *wifinina) NetDisconnect() {
w.netConnected = false w.netConnected = false
if debugging(debugBasic) { if debugging(debugBasic) {
fmt.Printf("\r\nDisconnected from Wifi SSID '%s'\r\n\r\n", w.cfg.Ssid) fmt.Printf("\r\nDisconnected from Wifi SSID '%s'\r\n\r\n", w.params.Ssid)
} }
if w.notifyCb != nil { if w.notifyCb != nil {
w.notifyCb(drivers.NetlinkEventNetDown) w.notifyCb(netlink.EventNetDown)
} }
} }
func (w *wifinina) NetNotify(cb func(drivers.NetlinkEvent)) { func (w *wifinina) NetNotify(cb func(netlink.Event)) {
w.notifyCb = cb w.notifyCb = cb
} }
func (w *wifinina) SendEth(pkt []byte) error {
return netlink.ErrNotSupported
}
func (w *wifinina) RecvEthFunc(cb func(pkt []byte) error) {
}
func (w *wifinina) GetHostByName(name string) (net.IP, error) { func (w *wifinina) GetHostByName(name string) (net.IP, error) {
if debugging(debugNetdev) { if debugging(debugNetdev) {
@@ -524,7 +516,7 @@ func (w *wifinina) GetHostByName(name string) (net.IP, error) {
ip := w.getHostByName(name) ip := w.getHostByName(name)
if ip == "" { if ip == "" {
return net.IP{}, drivers.ErrHostUnknown return net.IP{}, netdev.ErrHostUnknown
} }
return net.IP([]byte(ip)), nil return net.IP([]byte(ip)), nil
@@ -567,17 +559,17 @@ func (w *wifinina) Socket(domain int, stype int, protocol int) (int, error) {
} }
switch domain { switch domain {
case drivers.AF_INET: case netdev.AF_INET:
default: default:
return -1, drivers.ErrFamilyNotSupported return -1, netdev.ErrFamilyNotSupported
} }
switch { switch {
case protocol == drivers.IPPROTO_TCP && stype == drivers.SOCK_STREAM: case protocol == netdev.IPPROTO_TCP && stype == netdev.SOCK_STREAM:
case protocol == drivers.IPPROTO_TLS && stype == drivers.SOCK_STREAM: case protocol == netdev.IPPROTO_TLS && stype == netdev.SOCK_STREAM:
case protocol == drivers.IPPROTO_UDP && stype == drivers.SOCK_DGRAM: case protocol == netdev.IPPROTO_UDP && stype == netdev.SOCK_DGRAM:
default: default:
return -1, drivers.ErrProtocolNotSupported return -1, netdev.ErrProtocolNotSupported
} }
w.mu.Lock() w.mu.Lock()
@@ -585,7 +577,7 @@ func (w *wifinina) Socket(domain int, stype int, protocol int) (int, error) {
sock := w.getSocket() sock := w.getSocket()
if sock == noSocketAvail { if sock == noSocketAvail {
return -1, drivers.ErrNoMoreSockets return -1, netdev.ErrNoMoreSockets
} }
socket := newSocket(protocol) socket := newSocket(protocol)
@@ -607,9 +599,9 @@ func (w *wifinina) Bind(sockfd int, ip net.IP, port int) error {
var socket = w.sockets[sock] var socket = w.sockets[sock]
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP: case netdev.IPPROTO_TCP:
case drivers.IPPROTO_TLS: case netdev.IPPROTO_TLS:
case drivers.IPPROTO_UDP: case netdev.IPPROTO_UDP:
w.startServer(sock, uint16(port), protoModeUDP) w.startServer(sock, uint16(port), protoModeUDP)
} }
@@ -644,11 +636,11 @@ func (w *wifinina) Connect(sockfd int, host string, ip net.IP, port int) error {
// Start the connection // Start the connection
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP: case netdev.IPPROTO_TCP:
w.startClient(sock, "", toUint32(ip), uint16(port), protoModeTCP) w.startClient(sock, "", toUint32(ip), uint16(port), protoModeTCP)
case drivers.IPPROTO_TLS: case netdev.IPPROTO_TLS:
w.startClient(sock, host, 0, uint16(port), protoModeTLS) w.startClient(sock, host, 0, uint16(port), protoModeTLS)
case drivers.IPPROTO_UDP: case netdev.IPPROTO_UDP:
w.startClient(sock, "", toUint32(ip), uint16(port), protoModeUDP) w.startClient(sock, "", toUint32(ip), uint16(port), protoModeUDP)
return nil return nil
} }
@@ -677,11 +669,11 @@ func (w *wifinina) Listen(sockfd int, backlog int) error {
var socket = w.sockets[sock] var socket = w.sockets[sock]
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP: case netdev.IPPROTO_TCP:
w.startServer(sock, uint16(socket.port), protoModeTCP) w.startServer(sock, uint16(socket.port), protoModeTCP)
case drivers.IPPROTO_UDP: case netdev.IPPROTO_UDP:
default: default:
return drivers.ErrProtocolNotSupported return netdev.ErrProtocolNotSupported
} }
return nil return nil
@@ -701,9 +693,9 @@ func (w *wifinina) Accept(sockfd int, ip net.IP, port int) (int, error) {
var socket = w.sockets[sock] var socket = w.sockets[sock]
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP: case netdev.IPPROTO_TCP:
default: default:
return -1, drivers.ErrProtocolNotSupported return -1, netdev.ErrProtocolNotSupported
} }
for { for {
@@ -754,7 +746,7 @@ func (w *wifinina) Accept(sockfd int, ip net.IP, port int) (int, error) {
func (w *wifinina) sockDown(sock sock) bool { func (w *wifinina) sockDown(sock sock) bool {
var socket = w.sockets[sock] var socket = w.sockets[sock]
if socket.protocol == drivers.IPPROTO_UDP { if socket.protocol == netdev.IPPROTO_UDP {
return false return false
} }
return w.getClientState(sock) != tcpStateEstablished return w.getClientState(sock) != tcpStateEstablished
@@ -780,7 +772,7 @@ func (w *wifinina) sendTCP(sock sock, buf []byte, deadline time.Time) (int, erro
// Check if we've timed out // Check if we've timed out
if !deadline.IsZero() { if !deadline.IsZero() {
if time.Now().After(deadline) { if time.Now().After(deadline) {
return -1, drivers.ErrTimeout return -1, netdev.ErrTimeout
} }
} }
@@ -800,7 +792,7 @@ func (w *wifinina) sendTCP(sock sock, buf []byte, deadline time.Time) (int, erro
w.mu.Lock() w.mu.Lock()
} }
return -1, drivers.ErrTimeout return -1, netdev.ErrTimeout
} }
func (w *wifinina) sendUDP(sock sock, buf []byte, deadline time.Time) (int, error) { func (w *wifinina) sendUDP(sock sock, buf []byte, deadline time.Time) (int, error) {
@@ -827,18 +819,18 @@ func (w *wifinina) sendChunk(sockfd int, buf []byte, deadline time.Time) (int, e
// Check if we've timed out // Check if we've timed out
if !deadline.IsZero() { if !deadline.IsZero() {
if time.Now().After(deadline) { if time.Now().After(deadline) {
return -1, drivers.ErrTimeout return -1, netdev.ErrTimeout
} }
} }
switch socket.protocol { switch socket.protocol {
case drivers.IPPROTO_TCP, drivers.IPPROTO_TLS: case netdev.IPPROTO_TCP, netdev.IPPROTO_TLS:
return w.sendTCP(sock, buf, deadline) return w.sendTCP(sock, buf, deadline)
case drivers.IPPROTO_UDP: case netdev.IPPROTO_UDP:
return w.sendUDP(sock, buf, deadline) return w.sendUDP(sock, buf, deadline)
} }
return -1, drivers.ErrProtocolNotSupported return -1, netdev.ErrProtocolNotSupported
} }
func (w *wifinina) Send(sockfd int, buf []byte, flags int, func (w *wifinina) Send(sockfd int, buf []byte, flags int,
@@ -892,7 +884,7 @@ func (w *wifinina) Recv(sockfd int, buf []byte, flags int,
// Check if we've timed out // Check if we've timed out
if !deadline.IsZero() { if !deadline.IsZero() {
if time.Now().After(deadline) { if time.Now().After(deadline) {
return -1, drivers.ErrTimeout return -1, netdev.ErrTimeout
} }
} }
@@ -954,7 +946,7 @@ func (w *wifinina) Close(sockfd int) error {
w.stopClient(sock) w.stopClient(sock)
if socket.protocol == drivers.IPPROTO_UDP { if socket.protocol == netdev.IPPROTO_UDP {
socket.inuse = false socket.inuse = false
return nil return nil
} }
@@ -972,7 +964,7 @@ func (w *wifinina) Close(sockfd int) error {
w.mu.Lock() w.mu.Lock()
} }
return drivers.ErrClosingSocket return netdev.ErrClosingSocket
} }
func (w *wifinina) SetSockOpt(sockfd int, level int, opt int, value interface{}) error { func (w *wifinina) SetSockOpt(sockfd int, level int, opt int, value interface{}) error {
@@ -981,7 +973,7 @@ func (w *wifinina) SetSockOpt(sockfd int, level int, opt int, value interface{})
fmt.Printf("[SetSockOpt] sockfd: %d\r\n", sockfd) fmt.Printf("[SetSockOpt] sockfd: %d\r\n", sockfd)
} }
return drivers.ErrNotSupported return netdev.ErrNotSupported
} }
func (w *wifinina) startClient(sock sock, hostname string, addr uint32, port uint16, mode uint8) { func (w *wifinina) startClient(sock sock, hostname string, addr uint32, port uint16, mode uint8) {