diff --git a/espat/espat.go b/espat/espat.go index 050c9ac..3985308 100644 --- a/espat/espat.go +++ b/espat/espat.go @@ -30,14 +30,11 @@ import ( "sync" "time" - "tinygo.org/x/drivers" + "tinygo.org/x/drivers/netdev" + "tinygo.org/x/drivers/netlink" ) type Config struct { - // AP creditials - Ssid string - Passphrase string - // UART config Uart *machine.UART Tx machine.Pin @@ -62,25 +59,18 @@ type Device struct { mu sync.Mutex } -func New(cfg *Config) *Device { - d := Device{ +func NewDevice(cfg *Config) *Device { + return &Device{ cfg: cfg, response: make([]byte, 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 { - return drivers.ErrMissingSSID + if len(params.Ssid) == 0 { + return netlink.ErrMissingSSID } d.uart = d.cfg.Uart @@ -98,17 +88,17 @@ func (d *Device) NetConnect() error { if !d.Connected() { fmt.Printf("FAILED\r\n") - return drivers.ErrConnectFailed + return netlink.ErrConnectFailed } fmt.Printf("CONNECTED\r\n") // 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) - err := d.ConnectToAP(d.cfg.Ssid, d.cfg.Passphrase, 10 /* secs */) + err := d.ConnectToAP(params.Ssid, params.Passphrase, 10 /* secs */) if err != nil { fmt.Printf("FAILED\r\n") return err @@ -128,20 +118,27 @@ func (d *Device) NetConnect() error { func (d *Device) NetDisconnect() { 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 } +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) { ip, err := d.GetDNS(name) return net.ParseIP(ip), err } func (d *Device) GetHardwareAddr() (net.HardwareAddr, error) { - return net.HardwareAddr{}, drivers.ErrNotSupported + return net.HardwareAddr{}, netlink.ErrNotSupported } 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) { switch domain { - case drivers.AF_INET: + case netdev.AF_INET: default: - return -1, drivers.ErrFamilyNotSupported + return -1, netdev.ErrFamilyNotSupported } switch { - case protocol == drivers.IPPROTO_TCP && stype == drivers.SOCK_STREAM: - case protocol == drivers.IPPROTO_TLS && stype == drivers.SOCK_STREAM: - case protocol == drivers.IPPROTO_UDP && stype == drivers.SOCK_DGRAM: + case protocol == netdev.IPPROTO_TCP && stype == netdev.SOCK_STREAM: + case protocol == netdev.IPPROTO_TLS && stype == netdev.SOCK_STREAM: + case protocol == netdev.IPPROTO_UDP && stype == netdev.SOCK_DGRAM: default: - return -1, drivers.ErrProtocolNotSupported + return -1, netdev.ErrProtocolNotSupported } // Only supporting single connection mode, so only one socket at a time if d.socket.inUse { - return -1, drivers.ErrNoMoreSockets + return -1, netdev.ErrNoMoreSockets } d.socket.inUse = true 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) switch d.socket.protocol { - case drivers.IPPROTO_TCP: + case netdev.IPPROTO_TCP: err = d.ConnectTCPSocket(addr, rport) - case drivers.IPPROTO_UDP: + case netdev.IPPROTO_UDP: err = d.ConnectUDPSocket(addr, rport, lport) - case drivers.IPPROTO_TLS: + case netdev.IPPROTO_TLS: 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 { switch d.socket.protocol { - case drivers.IPPROTO_UDP: + case netdev.IPPROTO_UDP: default: - return drivers.ErrProtocolNotSupported + return netdev.ErrProtocolNotSupported } return nil } 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) { // Check if we've timed out if !deadline.IsZero() { if time.Now().After(deadline) { - return -1, drivers.ErrTimeout + return -1, netdev.ErrTimeout } } 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 if !deadline.IsZero() { 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 { - return drivers.ErrNotSupported + return netdev.ErrNotSupported } // Connected checks if there is communication with the ESP8266/ESP32. diff --git a/examples/net/http-get/main.go b/examples/net/http-get/main.go index 210c48f..7f8de67 100644 --- a/examples/net/http-get/main.go +++ b/examples/net/http-get/main.go @@ -9,6 +9,8 @@ // examples/net/webclient (for HTTP) // examples/net/tlsclient (for HTTPS) +//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -20,6 +22,9 @@ import ( "net/url" "strings" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -31,7 +36,13 @@ func main() { 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) } diff --git a/examples/net/http-get/rtl8720dn.go b/examples/net/http-get/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/http-get/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/http-get/wifinina.go b/examples/net/http-get/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/http-get/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/http-head/main.go b/examples/net/http-head/main.go index 1b0dcae..07c1f45 100644 --- a/examples/net/http-head/main.go +++ b/examples/net/http-head/main.go @@ -9,6 +9,8 @@ // examples/net/webclient (for HTTP) // examples/net/tlsclient (for HTTPS) +//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -18,6 +20,9 @@ import ( "machine" "net/http" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -30,7 +35,13 @@ func main() { 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) } @@ -46,7 +57,7 @@ func main() { } fmt.Println(string(buf.Bytes())) - netdev.NetDisconnect() + link.NetDisconnect() } // Wait for user to open serial console diff --git a/examples/net/http-head/rtl8720dn.go b/examples/net/http-head/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/http-head/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/http-head/wifinina.go b/examples/net/http-head/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/http-head/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/http-post/main.go b/examples/net/http-post/main.go index e1506a3..d28f62d 100644 --- a/examples/net/http-post/main.go +++ b/examples/net/http-post/main.go @@ -9,6 +9,8 @@ // examples/net/webclient (for HTTP) // examples/net/tlsclient (for HTTPS) +//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -19,6 +21,9 @@ import ( "machine" "net/http" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -30,7 +35,13 @@ func main() { 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) } @@ -50,7 +61,7 @@ func main() { fmt.Println(string(body)) - netdev.NetDisconnect() + link.NetDisconnect() } // Wait for user to open serial console diff --git a/examples/net/http-post/rtl8720dn.go b/examples/net/http-post/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/http-post/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/http-post/wifinina.go b/examples/net/http-post/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/http-post/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/http-postform/main.go b/examples/net/http-postform/main.go index 4c7ac59..e72aac8 100644 --- a/examples/net/http-postform/main.go +++ b/examples/net/http-postform/main.go @@ -9,6 +9,8 @@ // examples/net/webclient (for HTTP) // examples/net/tlsclient (for HTTPS) +//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -19,6 +21,9 @@ import ( "net/http" "net/url" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -30,7 +35,13 @@ func main() { 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) } diff --git a/examples/net/http-postform/rtl8720dn.go b/examples/net/http-postform/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/http-postform/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/http-postform/wifinina.go b/examples/net/http-postform/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/http-postform/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/mqttclient/natiu/espat.go b/examples/net/mqttclient/natiu/espat.go deleted file mode 100644 index 3a197fe..0000000 --- a/examples/net/mqttclient/natiu/espat.go +++ /dev/null @@ -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) diff --git a/examples/net/mqttclient/natiu/main.go b/examples/net/mqttclient/natiu/main.go index 8faa9a9..8d82fe7 100644 --- a/examples/net/mqttclient/natiu/main.go +++ b/examples/net/mqttclient/natiu/main.go @@ -4,6 +4,8 @@ // Note: It may be necessary to increase the stack size when using // 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 import ( @@ -18,6 +20,8 @@ import ( "time" mqtt "github.com/soypat/natiu-mqtt" + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -30,7 +34,13 @@ var ( func main() { 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) } @@ -99,7 +109,7 @@ func main() { time.Sleep(time.Second) - conn.SetReadDeadline(time.Now().Add(10*time.Second)) + conn.SetReadDeadline(time.Now().Add(10 * time.Second)) err = client.HandleNext() if err != nil { log.Fatal("handle next: ", err) diff --git a/examples/net/mqttclient/natiu/rtl8720dn.go b/examples/net/mqttclient/natiu/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/mqttclient/natiu/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/mqttclient/natiu/wifinina.go b/examples/net/mqttclient/natiu/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/mqttclient/natiu/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/mqttclient/paho/espat.go b/examples/net/mqttclient/paho/espat.go deleted file mode 100644 index 3a197fe..0000000 --- a/examples/net/mqttclient/paho/espat.go +++ /dev/null @@ -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) diff --git a/examples/net/mqttclient/paho/main.go b/examples/net/mqttclient/paho/main.go index 8fe6a8a..293e24d 100644 --- a/examples/net/mqttclient/paho/main.go +++ b/examples/net/mqttclient/paho/main.go @@ -4,6 +4,8 @@ // Note: It may be necessary to increase the stack size when using // 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 import ( @@ -14,6 +16,8 @@ import ( "time" mqtt "github.com/eclipse/paho.mqtt.golang" + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -37,7 +41,13 @@ var connectionLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, func main() { 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) } diff --git a/examples/net/mqttclient/paho/rtl8720dn.go b/examples/net/mqttclient/paho/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/mqttclient/paho/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/mqttclient/paho/wifinina.go b/examples/net/mqttclient/paho/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/mqttclient/paho/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/ntpclient/espat.go b/examples/net/ntpclient/espat.go deleted file mode 100644 index 3a197fe..0000000 --- a/examples/net/ntpclient/espat.go +++ /dev/null @@ -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) diff --git a/examples/net/ntpclient/main.go b/examples/net/ntpclient/main.go index e6f9d6b..6c84193 100644 --- a/examples/net/ntpclient/main.go +++ b/examples/net/ntpclient/main.go @@ -3,6 +3,8 @@ // 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. +//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040 + package main import ( @@ -13,6 +15,9 @@ import ( "net" "runtime" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -30,7 +35,13 @@ func main() { 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) } @@ -49,7 +60,7 @@ func main() { } conn.Close() - netdev.NetDisconnect() + link.NetDisconnect() runtime.AdjustTimeOffset(-1 * int64(time.Since(t))) diff --git a/examples/net/ntpclient/rtl8720dn.go b/examples/net/ntpclient/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/ntpclient/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/ntpclient/wifinina.go b/examples/net/ntpclient/wifinina.go deleted file mode 100644 index 59d3c5d..0000000 --- a/examples/net/ntpclient/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/socket/espat.go b/examples/net/socket/espat.go deleted file mode 100644 index 3a197fe..0000000 --- a/examples/net/socket/espat.go +++ /dev/null @@ -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) diff --git a/examples/net/socket/main.go b/examples/net/socket/main.go index 01791aa..f17ede5 100644 --- a/examples/net/socket/main.go +++ b/examples/net/socket/main.go @@ -4,6 +4,8 @@ // // nc -lk 8080 +//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040 + package main import ( @@ -15,7 +17,9 @@ import ( "strconv" "time" - "tinygo.org/x/drivers" + "tinygo.org/x/drivers/netdev" + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -25,12 +29,20 @@ var ( ) var buf = &bytes.Buffer{} +var link netlink.Netlinker +var dev netdev.Netdever func main() { 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) } @@ -48,9 +60,9 @@ func sendBatch() { // make TCP connection message("---------------\r\nDialing TCP connection") - fd, _ := netdev.Socket(drivers.AF_INET, drivers.SOCK_STREAM, drivers.IPPROTO_TCP) - err := netdev.Connect(fd, "", ip, port) - for ; err != nil; err = netdev.Connect(fd, "", ip, port) { + fd, _ := dev.Socket(netdev.AF_INET, netdev.SOCK_STREAM, netdev.IPPROTO_TCP) + err := dev.Connect(fd, "", ip, port) + for ; err != nil; err = dev.Connect(fd, "", ip, port) { message(err.Error()) time.Sleep(5 * time.Second) } @@ -67,7 +79,7 @@ func sendBatch() { fmt.Fprint(buf, "\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") break } @@ -79,12 +91,12 @@ func sendBatch() { fmt.Fprint(buf, "\nWrote ", n, " bytes in ", ms, " ms\r\n") 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("Disconnecting TCP...") - netdev.Close(fd) + dev.Close(fd) } func message(msg string) { diff --git a/examples/net/socket/rtl8720dn.go b/examples/net/socket/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/socket/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/socket/wifinina.go b/examples/net/socket/wifinina.go deleted file mode 100644 index 59d3c5d..0000000 --- a/examples/net/socket/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/tcpclient/espat.go b/examples/net/tcpclient/espat.go deleted file mode 100644 index 3a197fe..0000000 --- a/examples/net/tcpclient/espat.go +++ /dev/null @@ -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) diff --git a/examples/net/tcpclient/main.go b/examples/net/tcpclient/main.go index 76fd562..10d2935 100644 --- a/examples/net/tcpclient/main.go +++ b/examples/net/tcpclient/main.go @@ -5,6 +5,8 @@ // // nc -lk 8080 +//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040 || pico + package main import ( @@ -14,6 +16,9 @@ import ( "machine" "net" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -28,7 +33,13 @@ func main() { 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) } diff --git a/examples/net/tcpclient/rtl8720dn.go b/examples/net/tcpclient/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/tcpclient/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/tcpclient/wifinina.go b/examples/net/tcpclient/wifinina.go deleted file mode 100644 index 59d3c5d..0000000 --- a/examples/net/tcpclient/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/tcpecho/main.go b/examples/net/tcpecho/main.go index f91c6df..0356617 100644 --- a/examples/net/tcpecho/main.go +++ b/examples/net/tcpecho/main.go @@ -7,6 +7,8 @@ // // $ nc 10.0.0.2 8080 copy ; cmp file copy +//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -14,6 +16,9 @@ import ( "log" "net" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -36,7 +41,13 @@ func main() { 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) } diff --git a/examples/net/tcpecho/rtl8720dn.go b/examples/net/tcpecho/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/tcpecho/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/tcpecho/wifinina.go b/examples/net/tcpecho/wifinina.go deleted file mode 100644 index 59d3c5d..0000000 --- a/examples/net/tcpecho/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/tlsclient/main.go b/examples/net/tlsclient/main.go index 57caad4..186fdd5 100644 --- a/examples/net/tlsclient/main.go +++ b/examples/net/tlsclient/main.go @@ -5,6 +5,8 @@ // // 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 import ( @@ -17,6 +19,9 @@ import ( "net" "strings" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -80,7 +85,13 @@ func makeRequest() { func main() { 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) } diff --git a/examples/net/tlsclient/rtl8720dn.go b/examples/net/tlsclient/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/tlsclient/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/tlsclient/wifinina.go b/examples/net/tlsclient/wifinina.go deleted file mode 100644 index 59d3c5d..0000000 --- a/examples/net/tlsclient/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/webclient-tinyterm/main.go b/examples/net/webclient-tinyterm/main.go index 47dfa48..9720aaa 100644 --- a/examples/net/webclient-tinyterm/main.go +++ b/examples/net/webclient-tinyterm/main.go @@ -6,8 +6,6 @@ //go:build wioterminal -// +build: wioterminal - package main import ( @@ -21,9 +19,9 @@ import ( "strings" "time" - "tinygo.org/x/drivers" "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/tinyterm" ) @@ -34,18 +32,6 @@ 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( machine.SPI3, machine.LCD_DC, @@ -66,17 +52,6 @@ var ( 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() { machine.SPI3.Configure(machine.SPIConfig{ @@ -100,9 +75,13 @@ func main() { 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) } diff --git a/examples/net/webclient/main.go b/examples/net/webclient/main.go index 15b1d2c..bb2fe9d 100644 --- a/examples/net/webclient/main.go +++ b/examples/net/webclient/main.go @@ -17,6 +17,8 @@ // } // --------------------------------------------------------------------------- +//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -28,6 +30,9 @@ import ( "net" "strings" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -91,7 +96,13 @@ func closeConnection() { func main() { 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) } diff --git a/examples/net/webclient/rtl8720dn.go b/examples/net/webclient/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/webclient/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/webclient/wifinina.go b/examples/net/webclient/wifinina.go deleted file mode 100644 index 59d3c5d..0000000 --- a/examples/net/webclient/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/webserver/main.go b/examples/net/webserver/main.go index aeed6b3..3816391 100644 --- a/examples/net/webserver/main.go +++ b/examples/net/webserver/main.go @@ -6,6 +6,8 @@ // Note: It may be necessary to increase the stack size when using "net/http". // Use the -stack-size=4KB command line option. +//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -15,6 +17,9 @@ import ( "net/http" "strconv" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -37,7 +42,13 @@ func main() { 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) } @@ -48,7 +59,7 @@ func main() { http.HandleFunc("/off", LED_OFF) http.HandleFunc("/on", LED_ON) - err := http.ListenAndServe(port, nil) + err = http.ListenAndServe(port, nil) for err != nil { fmt.Printf("error: %s\r\n", err.Error()) time.Sleep(5 * time.Second) diff --git a/examples/net/webserver/rtl8720dn.go b/examples/net/webserver/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/webserver/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/webserver/wifinina.go b/examples/net/webserver/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/webserver/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/websocket/dial/main.go b/examples/net/websocket/dial/main.go index d53d6e8..5a7288d 100644 --- a/examples/net/websocket/dial/main.go +++ b/examples/net/websocket/dial/main.go @@ -6,6 +6,8 @@ // 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. +//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -15,6 +17,8 @@ import ( "time" "golang.org/x/net/websocket" + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -33,7 +37,13 @@ func waitSerial() { func main() { 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) } diff --git a/examples/net/websocket/dial/rtl8720dn.go b/examples/net/websocket/dial/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/websocket/dial/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/websocket/dial/wifinina.go b/examples/net/websocket/dial/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/websocket/dial/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/websocket/handler/main.go b/examples/net/websocket/handler/main.go index 4ff4fc8..709f14c 100644 --- a/examples/net/websocket/handler/main.go +++ b/examples/net/websocket/handler/main.go @@ -6,6 +6,8 @@ // 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. +//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -16,6 +18,8 @@ import ( "time" "golang.org/x/net/websocket" + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -40,12 +44,18 @@ func waitSerial() { func main() { 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) } http.Handle("/echo", websocket.Handler(EchoServer)) - err := http.ListenAndServe(port, nil) + err = http.ListenAndServe(port, nil) if err != nil { panic("ListenAndServe: " + err.Error()) } diff --git a/examples/net/websocket/handler/rtl8720dn.go b/examples/net/websocket/handler/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/websocket/handler/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/websocket/handler/wifinina.go b/examples/net/websocket/handler/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/websocket/handler/wifinina.go +++ /dev/null @@ -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) diff --git a/examples/net/webstatic/main.go b/examples/net/webstatic/main.go index cab7279..9130822 100644 --- a/examples/net/webstatic/main.go +++ b/examples/net/webstatic/main.go @@ -3,6 +3,8 @@ // Note: It may be necessary to increase the stack size when using "net/http". // Use the -stack-size=4KB command line option. +//go:build pyportal || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal + package main import ( @@ -10,6 +12,9 @@ import ( "log" "net/http" "time" + + "tinygo.org/x/drivers/netlink" + "tinygo.org/x/drivers/netlink/probe" ) var ( @@ -25,7 +30,13 @@ func main() { // wait a bit for console 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) } diff --git a/examples/net/webstatic/rtl8720dn.go b/examples/net/webstatic/rtl8720dn.go deleted file mode 100644 index 94d5277..0000000 --- a/examples/net/webstatic/rtl8720dn.go +++ /dev/null @@ -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) diff --git a/examples/net/webstatic/wifinina.go b/examples/net/webstatic/wifinina.go deleted file mode 100644 index a135548..0000000 --- a/examples/net/webstatic/wifinina.go +++ /dev/null @@ -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) diff --git a/netdev.go b/netdev/netdev.go similarity index 74% rename from netdev.go rename to netdev/netdev.go index 05731f9..cf07c07 100644 --- a/netdev.go +++ b/netdev/netdev.go @@ -1,4 +1,6 @@ -package drivers +// L3/L4 network/transport layer + +package netdev import ( "errors" @@ -32,6 +34,7 @@ var ( var ( ErrFamilyNotSupported = errors.New("Address family not supported") ErrProtocolNotSupported = errors.New("Socket protocol/type not supported") + ErrStartingDHCPClient = errors.New("Error starting DHPC client") ErrNoMoreSockets = errors.New("No more sockets") ErrClosingSocket = errors.New("Error closing socket") ErrNotSupported = errors.New("Not supported") @@ -47,29 +50,32 @@ func (e *timeoutError) Timeout() bool { return true } func (e *timeoutError) Temporary() bool { return true } //go:linkname UseNetdev net.useNetdev -func UseNetdev(dev netdever) +func UseNetdev(dev Netdever) -// Netdev is TinyGo's network device driver model. Network drivers implement -// the netdever interface, providing a common network I/O interface to TinyGo's -// "net" package. The interface is modeled after the BSD socket interface. -// net.Conn implementations (TCPConn, UDPConn, and TLSConn) use the netdev -// interface for device I/O access. +// Netdever is TinyGo's OSI L3/L4 network/transport layer interface. Network +// drivers implement the Netdever interface, providing a common network L3/L4 +// interface to TinyGo's "net" package. net.Conn implementations (TCPConn, +// UDPConn, and TLSConn) use the Netdever 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. // -// 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: 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 // address in standard dot notation 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. Socket(domain int, stype int, protocol int) (int, error) Bind(sockfd int, ip net.IP, port int) error diff --git a/netlink.go b/netlink.go deleted file mode 100644 index 955289f..0000000 --- a/netlink.go +++ /dev/null @@ -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) -} diff --git a/netlink/netlink.go b/netlink/netlink.go new file mode 100644 index 0000000..025dc56 --- /dev/null +++ b/netlink/netlink.go @@ -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) +} diff --git a/netlink/probe/espat.go b/netlink/probe/espat.go new file mode 100644 index 0000000..ead122a --- /dev/null +++ b/netlink/probe/espat.go @@ -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 +} diff --git a/netlink/probe/mrkwifi1010.go b/netlink/probe/mrkwifi1010.go new file mode 100644 index 0000000..b890732 --- /dev/null +++ b/netlink/probe/mrkwifi1010.go @@ -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 +} diff --git a/netlink/probe/rtl8720dn.go b/netlink/probe/rtl8720dn.go new file mode 100644 index 0000000..09e5ce9 --- /dev/null +++ b/netlink/probe/rtl8720dn.go @@ -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 +} diff --git a/netlink/probe/wifinina.go b/netlink/probe/wifinina.go new file mode 100644 index 0000000..bae9647 --- /dev/null +++ b/netlink/probe/wifinina.go @@ -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 +} diff --git a/rtl8720dn/rtl8720dn.go b/rtl8720dn/rtl8720dn.go index c2641d8..67ff3a5 100644 --- a/rtl8720dn/rtl8720dn.go +++ b/rtl8720dn/rtl8720dn.go @@ -16,7 +16,8 @@ import ( "sync" "time" - "tinygo.org/x/drivers" + "tinygo.org/x/drivers/netdev" + "tinygo.org/x/drivers/netlink" ) var _debug debug = debugBasic @@ -42,10 +43,6 @@ type socket struct { } type Config struct { - // AP creditials - Ssid string - Passphrase string - // Enable En machine.Pin @@ -54,21 +51,11 @@ type Config struct { Tx machine.Pin Rx machine.Pin 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 { cfg *Config - notifyCb func(drivers.NetlinkEvent) + notifyCb func(netlink.Event) mu sync.Mutex uart *machine.UART @@ -76,6 +63,8 @@ type rtl8720dn struct { debug bool + params *netlink.ConnectParams + netConnected bool driverShown bool deviceShown bool @@ -91,46 +80,39 @@ func newSocket(protocol int) *socket { } func New(cfg *Config) *rtl8720dn { - r := rtl8720dn{ + return &rtl8720dn{ debug: (_debug & debugRpc) != 0, cfg: cfg, sockets: make(map[sock]*socket), killWatchdog: make(chan bool), } - - drivers.UseNetdev(&r) - - // assert that rtl8720dn implements Netlinker - var _ drivers.Netlinker = (*rtl8720dn)(nil) - - return &r } func (r *rtl8720dn) startDhcpc() error { if result := r.rpc_tcpip_adapter_dhcpc_start(0); result == -1 { - return drivers.ErrStartingDHCPClient + return netdev.ErrStartingDHCPClient } return nil } func (r *rtl8720dn) connectToAP() error { - if len(r.cfg.Ssid) == 0 { - return drivers.ErrMissingSSID + if len(r.params.Ssid) == 0 { + return netlink.ErrMissingSSID } 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 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 debugging(debugBasic) { fmt.Printf("FAILED\r\n") } - return drivers.ErrConnectFailed + return netlink.ErrConnectFailed } if debugging(debugBasic) { @@ -138,7 +120,7 @@ func (r *rtl8720dn) connectToAP() error { } if r.notifyCb != nil { - r.notifyCb(drivers.NetlinkEventNetUp) + r.notifyCb(netlink.EventNetUp) } return r.startDhcpc() @@ -226,7 +208,7 @@ func (r *rtl8720dn) networkDown() bool { } func (r *rtl8720dn) watchdog() { - ticker := time.NewTicker(r.cfg.WatchdogTimeout) + ticker := time.NewTicker(r.params.WatchdogTimeout) for { select { case <-r.killWatchdog: @@ -238,7 +220,7 @@ func (r *rtl8720dn) watchdog() { fmt.Printf("Watchdog: Wifi NOT CONNECTED, trying again...\r\n") } if r.notifyCb != nil { - r.notifyCb(drivers.NetlinkEventNetDown) + r.notifyCb(netlink.EventNetDown) } r.netConnect(false) } @@ -255,9 +237,9 @@ func (r *rtl8720dn) netConnect(reset bool) error { } 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 == drivers.ErrConnectFailed { + if err == netlink.ErrConnectFailed { continue } return err @@ -266,22 +248,24 @@ func (r *rtl8720dn) netConnect(reset bool) error { } if r.networkDown() { - return drivers.ErrConnectFailed + return netlink.ErrConnectFailed } r.showIP() return nil } -func (r *rtl8720dn) NetConnect() error { +func (r *rtl8720dn) NetConnect(params *netlink.ConnectParams) error { r.mu.Lock() defer r.mu.Unlock() if r.netConnected { - return drivers.ErrConnected + return netlink.ErrConnected } + r.params = params + r.showDriver() if err := r.netConnect(true); err != nil { @@ -290,7 +274,7 @@ func (r *rtl8720dn) NetConnect() error { r.netConnected = true - if r.cfg.WatchdogTimeout != 0 { + if r.params.WatchdogTimeout != 0 { go r.watchdog() } @@ -310,7 +294,7 @@ func (r *rtl8720dn) NetDisconnect() { return } - if r.cfg.WatchdogTimeout != 0 { + if r.params.WatchdogTimeout != 0 { r.killWatchdog <- true } r.netDisconnect() @@ -319,18 +303,25 @@ func (r *rtl8720dn) NetDisconnect() { r.netConnected = false 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 { - 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 } +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) { if debugging(debugNetdev) { @@ -343,7 +334,7 @@ func (r *rtl8720dn) GetHostByName(name string) (net.IP, error) { var ip [4]byte result := r.rpc_netconn_gethostbyname(name, ip[:]) if result == -1 { - return net.IP{}, drivers.ErrHostUnknown + return net.IP{}, netdev.ErrHostUnknown } return net.IP(ip[:]), nil @@ -396,9 +387,9 @@ func (r *rtl8720dn) Socket(domain int, stype int, protocol int) (int, error) { } switch domain { - case drivers.AF_INET: + case netdev.AF_INET: default: - return -1, drivers.ErrFamilyNotSupported + return -1, netdev.ErrFamilyNotSupported } var newSock int32 @@ -407,22 +398,22 @@ func (r *rtl8720dn) Socket(domain int, stype int, protocol int) (int, error) { defer r.mu.Unlock() switch { - case protocol == drivers.IPPROTO_TCP && stype == drivers.SOCK_STREAM: - newSock = r.rpc_lwip_socket(drivers.AF_INET, drivers.SOCK_STREAM, - drivers.IPPROTO_TCP) - case protocol == drivers.IPPROTO_TLS && stype == drivers.SOCK_STREAM: + case protocol == netdev.IPPROTO_TCP && stype == netdev.SOCK_STREAM: + newSock = r.rpc_lwip_socket(netdev.AF_INET, netdev.SOCK_STREAM, + netdev.IPPROTO_TCP) + case protocol == netdev.IPPROTO_TLS && stype == netdev.SOCK_STREAM: // TODO Investigate: using client number as socket number; // TODO this may cause a problem if mixing TLS and non-TLS sockets? newSock = int32(r.clientTLS()) - case protocol == drivers.IPPROTO_UDP && stype == drivers.SOCK_DGRAM: - newSock = r.rpc_lwip_socket(drivers.AF_INET, drivers.SOCK_DGRAM, - drivers.IPPROTO_UDP) + case protocol == netdev.IPPROTO_UDP && stype == netdev.SOCK_DGRAM: + newSock = r.rpc_lwip_socket(netdev.AF_INET, netdev.SOCK_DGRAM, + netdev.IPPROTO_UDP) default: - return -1, drivers.ErrProtocolNotSupported + return -1, netdev.ErrProtocolNotSupported } if newSock == -1 { - return -1, drivers.ErrNoMoreSockets + return -1, netdev.ErrNoMoreSockets } 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 { name := make([]byte, 16) name[0] = 0x00 - name[1] = drivers.AF_INET + name[1] = netdev.AF_INET name[2] = byte(port >> 8) name[3] = byte(port) 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) 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))) if result == -1 { return fmt.Errorf("Bind to %s:%d failed", ip, port) } default: - return drivers.ErrProtocolNotSupported + return netdev.ErrProtocolNotSupported } return nil @@ -492,12 +483,12 @@ func (r *rtl8720dn) Connect(sockfd int, host string, ip net.IP, port int) error // Start the connection 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))) if result == -1 { 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), host, uint32(port), 0) if result == -1 { @@ -521,22 +512,22 @@ func (r *rtl8720dn) Listen(sockfd int, backlog int) error { var socket = r.sockets[sock] switch socket.protocol { - case drivers.IPPROTO_TCP: + case netdev.IPPROTO_TCP: result := r.rpc_lwip_listen(int32(sock), int32(backlog)) if result == -1 { 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 { return fmt.Errorf("Fcntl failed") } - case drivers.IPPROTO_UDP: + case netdev.IPPROTO_UDP: result := r.rpc_lwip_listen(int32(sock), int32(backlog)) if result == -1 { return fmt.Errorf("Listen failed") } default: - return drivers.ErrProtocolNotSupported + return netdev.ErrProtocolNotSupported } return nil @@ -557,9 +548,9 @@ func (r *rtl8720dn) Accept(sockfd int, ip net.IP, port int) (int, error) { var addr = addrToName(ip, port) switch socket.protocol { - case drivers.IPPROTO_TCP: + case netdev.IPPROTO_TCP: default: - return -1, drivers.ErrProtocolNotSupported + return -1, netdev.ErrProtocolNotSupported } for { @@ -606,18 +597,18 @@ func (r *rtl8720dn) sendChunk(sockfd int, buf []byte, deadline time.Time) (int, // Check if we've timed out if !deadline.IsZero() { if time.Now().After(deadline) { - return -1, drivers.ErrTimeout + return -1, netdev.ErrTimeout } } 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) if result == -1 { return -1, fmt.Errorf("Send error") } 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))) if result == -1 { 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 -1, drivers.ErrProtocolNotSupported + return -1, netdev.ErrProtocolNotSupported } 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 if !deadline.IsZero() { if time.Now().After(deadline) { - return -1, drivers.ErrTimeout + return -1, netdev.ErrTimeout } } 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], uint32(length), 0x00000008, 0) - case drivers.IPPROTO_TLS: + case netdev.IPPROTO_TLS: n = r.rpc_wifi_get_ssl_receive(uint32(sock), buf[:length], int32(length)) } @@ -734,15 +725,15 @@ func (r *rtl8720dn) Close(sockfd int) error { } switch socket.protocol { - case drivers.IPPROTO_TCP, drivers.IPPROTO_UDP: + case netdev.IPPROTO_TCP, netdev.IPPROTO_UDP: 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_ssl_client_destroy(uint32(sock)) } if result == -1 { - return drivers.ErrClosingSocket + return netdev.ErrClosingSocket } 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) } - return drivers.ErrNotSupported + return netdev.ErrNotSupported } func (r *rtl8720dn) disconnect() error { diff --git a/wifinina/wifinina.go b/wifinina/wifinina.go index ceeb658..9132db1 100644 --- a/wifinina/wifinina.go +++ b/wifinina/wifinina.go @@ -21,6 +21,8 @@ import ( "time" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/netdev" + "tinygo.org/x/drivers/netlink" ) var _debug debug = debugBasic @@ -168,10 +170,6 @@ type socket struct { } type Config struct { - // AP creditials - Ssid string - Passphrase string - // SPI config Spi drivers.SPI Freq uint32 @@ -189,24 +187,11 @@ type Config struct { // Arduino MKR 1010, where the reset signal needs to go high instead of // low. 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 { cfg *Config - notifyCb func(drivers.NetlinkEvent) + notifyCb func(netlink.Event) mu sync.Mutex spi drivers.SPI @@ -218,6 +203,8 @@ type wifinina struct { buf [64]byte ssids [maxNetworks]string + params *netlink.ConnectParams + netConnected bool driverShown bool deviceShown bool @@ -244,15 +231,6 @@ func New(cfg *Config) *wifinina { 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 } @@ -273,20 +251,25 @@ func (w *wifinina) reason() string { return fmt.Sprintf("%d", reason) } -func (w *wifinina) connectToAP(timeout time.Duration) error { +func (w *wifinina) connectToAP() error { - if len(w.cfg.Ssid) == 0 { - return drivers.ErrMissingSSID + timeout := w.params.ConnectTimeout + if timeout == 0 { + timeout = netlink.DefaultConnectTimeout + } + + if len(w.params.Ssid) == 0 { + return netlink.ErrMissingSSID } 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 the connection process - w.setPassphrase(w.cfg.Ssid, w.cfg.Passphrase) + w.setPassphrase(w.params.Ssid, w.params.Passphrase) // Check if we connected for { @@ -297,14 +280,14 @@ func (w *wifinina) connectToAP(timeout time.Duration) error { fmt.Printf("CONNECTED\r\n") } if w.notifyCb != nil { - w.notifyCb(drivers.NetlinkEventNetUp) + w.notifyCb(netlink.EventNetUp) } return nil case statusConnectFailed: if debugging(debugBasic) { fmt.Printf("FAILED (%s)\r\n", w.reason()) } - return drivers.ErrConnectFailed + return netlink.ErrConnectFailed } if time.Since(start) > timeout { break @@ -316,7 +299,7 @@ func (w *wifinina) connectToAP(timeout time.Duration) error { fmt.Printf("FAILED (timed out)\r\n") } - return drivers.ErrConnectTimeout + return netlink.ErrConnectTimeout } func (w *wifinina) netDisconnect() { @@ -404,7 +387,7 @@ func (w *wifinina) networkDown() bool { } func (w *wifinina) watchdog() { - ticker := time.NewTicker(w.cfg.WatchdogTimeout) + ticker := time.NewTicker(w.params.WatchdogTimeout) for { select { case <-w.killWatchdog: @@ -423,7 +406,7 @@ func (w *wifinina) watchdog() { fmt.Printf("Watchdog: Wifi NOT CONNECTED, trying again...\r\n") } if w.notifyCb != nil { - w.notifyCb(drivers.NetlinkEventNetDown) + w.notifyCb(netlink.EventNetDown) } w.netConnect(false) } @@ -438,10 +421,10 @@ func (w *wifinina) netConnect(reset bool) error { } w.showDevice() - for i := 0; w.cfg.Retries == 0 || i < w.cfg.Retries; i++ { - if err := w.connectToAP(w.cfg.ConnectTimeout); err != nil { + for i := 0; w.params.Retries == 0 || i < w.params.Retries; i++ { + if err := w.connectToAP(); err != nil { switch err { - case drivers.ErrConnectTimeout, drivers.ErrConnectFailed: + case netlink.ErrConnectTimeout, netlink.ErrConnectFailed: continue } return err @@ -450,22 +433,24 @@ func (w *wifinina) netConnect(reset bool) error { } if w.networkDown() { - return drivers.ErrConnectFailed + return netlink.ErrConnectFailed } w.showIP() return nil } -func (w *wifinina) NetConnect() error { +func (w *wifinina) NetConnect(params *netlink.ConnectParams) error { w.mu.Lock() defer w.mu.Unlock() if w.netConnected { - return drivers.ErrConnected + return netlink.ErrConnected } + w.params = params + w.showDriver() w.setupSPI() @@ -475,7 +460,7 @@ func (w *wifinina) NetConnect() error { w.netConnected = true - if w.cfg.WatchdogTimeout != 0 { + if w.params.WatchdogTimeout != 0 { go w.watchdog() } @@ -491,7 +476,7 @@ func (w *wifinina) NetDisconnect() { return } - if w.cfg.WatchdogTimeout != 0 { + if w.params.WatchdogTimeout != 0 { w.killWatchdog <- true } @@ -501,18 +486,25 @@ func (w *wifinina) NetDisconnect() { w.netConnected = false 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 { - 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 } +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) { if debugging(debugNetdev) { @@ -524,7 +516,7 @@ func (w *wifinina) GetHostByName(name string) (net.IP, error) { ip := w.getHostByName(name) if ip == "" { - return net.IP{}, drivers.ErrHostUnknown + return net.IP{}, netdev.ErrHostUnknown } return net.IP([]byte(ip)), nil @@ -567,17 +559,17 @@ func (w *wifinina) Socket(domain int, stype int, protocol int) (int, error) { } switch domain { - case drivers.AF_INET: + case netdev.AF_INET: default: - return -1, drivers.ErrFamilyNotSupported + return -1, netdev.ErrFamilyNotSupported } switch { - case protocol == drivers.IPPROTO_TCP && stype == drivers.SOCK_STREAM: - case protocol == drivers.IPPROTO_TLS && stype == drivers.SOCK_STREAM: - case protocol == drivers.IPPROTO_UDP && stype == drivers.SOCK_DGRAM: + case protocol == netdev.IPPROTO_TCP && stype == netdev.SOCK_STREAM: + case protocol == netdev.IPPROTO_TLS && stype == netdev.SOCK_STREAM: + case protocol == netdev.IPPROTO_UDP && stype == netdev.SOCK_DGRAM: default: - return -1, drivers.ErrProtocolNotSupported + return -1, netdev.ErrProtocolNotSupported } w.mu.Lock() @@ -585,7 +577,7 @@ func (w *wifinina) Socket(domain int, stype int, protocol int) (int, error) { sock := w.getSocket() if sock == noSocketAvail { - return -1, drivers.ErrNoMoreSockets + return -1, netdev.ErrNoMoreSockets } socket := newSocket(protocol) @@ -607,9 +599,9 @@ func (w *wifinina) Bind(sockfd int, ip net.IP, port int) error { var socket = w.sockets[sock] switch socket.protocol { - case drivers.IPPROTO_TCP: - case drivers.IPPROTO_TLS: - case drivers.IPPROTO_UDP: + case netdev.IPPROTO_TCP: + case netdev.IPPROTO_TLS: + case netdev.IPPROTO_UDP: 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 switch socket.protocol { - case drivers.IPPROTO_TCP: + case netdev.IPPROTO_TCP: w.startClient(sock, "", toUint32(ip), uint16(port), protoModeTCP) - case drivers.IPPROTO_TLS: + case netdev.IPPROTO_TLS: w.startClient(sock, host, 0, uint16(port), protoModeTLS) - case drivers.IPPROTO_UDP: + case netdev.IPPROTO_UDP: w.startClient(sock, "", toUint32(ip), uint16(port), protoModeUDP) return nil } @@ -677,11 +669,11 @@ func (w *wifinina) Listen(sockfd int, backlog int) error { var socket = w.sockets[sock] switch socket.protocol { - case drivers.IPPROTO_TCP: + case netdev.IPPROTO_TCP: w.startServer(sock, uint16(socket.port), protoModeTCP) - case drivers.IPPROTO_UDP: + case netdev.IPPROTO_UDP: default: - return drivers.ErrProtocolNotSupported + return netdev.ErrProtocolNotSupported } return nil @@ -701,9 +693,9 @@ func (w *wifinina) Accept(sockfd int, ip net.IP, port int) (int, error) { var socket = w.sockets[sock] switch socket.protocol { - case drivers.IPPROTO_TCP: + case netdev.IPPROTO_TCP: default: - return -1, drivers.ErrProtocolNotSupported + return -1, netdev.ErrProtocolNotSupported } 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 { var socket = w.sockets[sock] - if socket.protocol == drivers.IPPROTO_UDP { + if socket.protocol == netdev.IPPROTO_UDP { return false } 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 if !deadline.IsZero() { 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() } - return -1, drivers.ErrTimeout + return -1, netdev.ErrTimeout } 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 if !deadline.IsZero() { if time.Now().After(deadline) { - return -1, drivers.ErrTimeout + return -1, netdev.ErrTimeout } } switch socket.protocol { - case drivers.IPPROTO_TCP, drivers.IPPROTO_TLS: + case netdev.IPPROTO_TCP, netdev.IPPROTO_TLS: return w.sendTCP(sock, buf, deadline) - case drivers.IPPROTO_UDP: + case netdev.IPPROTO_UDP: return w.sendUDP(sock, buf, deadline) } - return -1, drivers.ErrProtocolNotSupported + return -1, netdev.ErrProtocolNotSupported } 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 if !deadline.IsZero() { 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) - if socket.protocol == drivers.IPPROTO_UDP { + if socket.protocol == netdev.IPPROTO_UDP { socket.inuse = false return nil } @@ -972,7 +964,7 @@ func (w *wifinina) Close(sockfd int) error { w.mu.Lock() } - return drivers.ErrClosingSocket + return netdev.ErrClosingSocket } 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) } - return drivers.ErrNotSupported + return netdev.ErrNotSupported } func (w *wifinina) startClient(sock sock, hostname string, addr uint32, port uint16, mode uint8) {