mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-29 02:49:02 +00:00
apply some of @scottfeldman's suggestions
This commit is contained in:
+40
-39
@@ -2,12 +2,13 @@ package nets
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:linkname UseStack net.useNetdev
|
//go:linkname UseSocketStack net.useNetdev
|
||||||
func UseStack(stack Stack)
|
func UseSocketStack(stack SocketStack)
|
||||||
|
|
||||||
// GethostByName() errors
|
// GethostByName() errors
|
||||||
var (
|
var (
|
||||||
@@ -36,36 +37,36 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AF_INET = 0x2
|
_AF_INET = 0x2
|
||||||
SOCK_STREAM = 0x1
|
_SOCK_STREAM = 0x1
|
||||||
SOCK_DGRAM = 0x2
|
_SOCK_DGRAM = 0x2
|
||||||
SOL_SOCKET = 0x1
|
_SOL_SOCKET = 0x1
|
||||||
SO_KEEPALIVE = 0x9
|
_SO_KEEPALIVE = 0x9
|
||||||
SOL_TCP = 0x6
|
_SOL_TCP = 0x6
|
||||||
TCP_KEEPINTVL = 0x5
|
_TCP_KEEPINTVL = 0x5
|
||||||
IPPROTO_TCP = 0x6
|
_IPPROTO_TCP = 0x6
|
||||||
IPPROTO_UDP = 0x11
|
_IPPROTO_UDP = 0x11
|
||||||
// Made up, not a real IP protocol number. This is used to create a
|
// Made up, not a real IP protocol number. This is used to create a
|
||||||
// TLS socket on the device, assuming the device supports mbed TLS.
|
// TLS socket on the device, assuming the device supports mbed TLS.
|
||||||
IPPROTO_TLS = 0xFE
|
_IPPROTO_TLS = 0xFE
|
||||||
F_SETFL = 0x4
|
_F_SETFL = 0x4
|
||||||
)
|
)
|
||||||
|
|
||||||
// Link is the minimum interface that need be implemented by any network
|
// Interface is the minimum interface that need be implemented by any network
|
||||||
// device driver.
|
// device driver and is based on [net.Interface].
|
||||||
type Link interface {
|
type Interface interface {
|
||||||
// HardwareAddr6 returns the device's 6-byte [MAC address], a.k.a EUI-48.
|
// HardwareAddr6 returns the device's 6-byte [MAC address].
|
||||||
//
|
//
|
||||||
// [MAC address]: https://en.wikipedia.org/wiki/MAC_address
|
// [MAC address]: https://en.wikipedia.org/wiki/MAC_address
|
||||||
HardwareAddr6() ([6]byte, error)
|
HardwareAddr6() ([6]byte, error)
|
||||||
// LinkStatus returns the state of the connection.
|
// Flags returns the net.Flag values for the interface. It includes state of connection.
|
||||||
LinkStatus() LinkStatus
|
Flags() net.Flags
|
||||||
// MTU returns the maximum transmission unit size.
|
// MTU returns the maximum transmission unit size.
|
||||||
MTU() int
|
MTU() int
|
||||||
}
|
}
|
||||||
|
|
||||||
type EthPollerLink interface {
|
type EthPoller interface {
|
||||||
Link
|
Interface
|
||||||
// SendEth sends an Ethernet packet
|
// SendEth sends an Ethernet packet
|
||||||
SendEth(pkt []byte) error
|
SendEth(pkt []byte) error
|
||||||
// RecvEthHandle sets recieve Ethernet packet callback function
|
// RecvEthHandle sets recieve Ethernet packet callback function
|
||||||
@@ -74,8 +75,8 @@ type EthPollerLink interface {
|
|||||||
PollOne() (bool, error)
|
PollOne() (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type LinkWifi interface {
|
type InterfaceWifi interface {
|
||||||
Link
|
Interface
|
||||||
// Connect device to network
|
// Connect device to network
|
||||||
NetConnect(params WifiParams) error
|
NetConnect(params WifiParams) error
|
||||||
// Disconnect device from network
|
// Disconnect device from network
|
||||||
@@ -84,10 +85,10 @@ type LinkWifi interface {
|
|||||||
NetNotify(cb func(Event))
|
NetNotify(cb func(Event))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stack interface {
|
type SocketStack 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) (netip.Addr, error)
|
// GetHostByName(name string) (netip.Addr, error)
|
||||||
|
|
||||||
// Addr returns IP address assigned to the interface, either by
|
// Addr returns IP address assigned to the interface, either by
|
||||||
// DHCP or statically
|
// DHCP or statically
|
||||||
@@ -105,10 +106,18 @@ type Stack interface {
|
|||||||
SetSockOpt(sockfd int, level int, opt int, value interface{}) error
|
SetSockOpt(sockfd int, level int, opt int, value interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Netdev is returned by `Probe` function.
|
// Should have UseResolver package level function that replaces the Go Resolver?
|
||||||
type Netdev interface {
|
type Resolver interface {
|
||||||
LinkWifi
|
// GetHostByName returns the IP address of either a hostname or IPv4
|
||||||
Stack
|
// address in standard dot notation
|
||||||
|
GetHostByName(name string) (netip.Addr, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4WifiInterface is returned by `Probe` function for devices that communicate
|
||||||
|
// on the OSI level 4 (transport) layer.
|
||||||
|
type L4WifiInterface interface {
|
||||||
|
InterfaceWifi
|
||||||
|
SocketStack
|
||||||
}
|
}
|
||||||
|
|
||||||
type WifiParams struct {
|
type WifiParams struct {
|
||||||
@@ -158,14 +167,6 @@ const (
|
|||||||
AuthTypeWPA2Mixed // WPA2/WPA mixed authorization
|
AuthTypeWPA2Mixed // WPA2/WPA mixed authorization
|
||||||
)
|
)
|
||||||
|
|
||||||
type LinkStatus uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
LinkDown LinkStatus = iota
|
|
||||||
LinkConnecting
|
|
||||||
LinkUp
|
|
||||||
)
|
|
||||||
|
|
||||||
type WifiAutoconnectParams struct {
|
type WifiAutoconnectParams struct {
|
||||||
WifiParams
|
WifiParams
|
||||||
|
|
||||||
@@ -183,7 +184,7 @@ type WifiAutoconnectParams struct {
|
|||||||
WatchdogTimeout time.Duration
|
WatchdogTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartWifiAutoconnect(dev Netdev, cfg WifiAutoconnectParams) error {
|
func StartWifiAutoconnect(dev L4WifiInterface, cfg WifiAutoconnectParams) error {
|
||||||
if dev == nil {
|
if dev == nil {
|
||||||
return ErrConnectModeNoGood
|
return ErrConnectModeNoGood
|
||||||
}
|
}
|
||||||
@@ -199,7 +200,7 @@ func StartWifiAutoconnect(dev Netdev, cfg WifiAutoconnectParams) error {
|
|||||||
}
|
}
|
||||||
for cfg.WatchdogTimeout != 0 {
|
for cfg.WatchdogTimeout != 0 {
|
||||||
time.Sleep(cfg.WatchdogTimeout)
|
time.Sleep(cfg.WatchdogTimeout)
|
||||||
if dev.LinkStatus() == LinkDown {
|
if dev.Flags()&net.FlagRunning == 0 {
|
||||||
i = 0
|
i = 0
|
||||||
goto RECONNECT
|
goto RECONNECT
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user