tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/netlink/netlink.go (about)

     1  // L2 data link layer
     2  
     3  package netlink
     4  
     5  import (
     6  	"errors"
     7  	"net"
     8  	"time"
     9  )
    10  
    11  var (
    12  	ErrConnected         = errors.New("Already connected")
    13  	ErrConnectFailed     = errors.New("Connect failed")
    14  	ErrConnectTimeout    = errors.New("Connect timed out")
    15  	ErrMissingSSID       = errors.New("Missing WiFi SSID")
    16  	ErrShortPassphrase   = errors.New("Invalid Wifi Passphrase < 8 chars")
    17  	ErrAuthFailure       = errors.New("Wifi authentication failure")
    18  	ErrAuthTypeNoGood    = errors.New("Wifi authorization type not supported")
    19  	ErrConnectModeNoGood = errors.New("Connect mode not supported")
    20  	ErrNotSupported      = errors.New("Not supported")
    21  )
    22  
    23  type Event int
    24  
    25  // Network events
    26  const (
    27  	// The device's network connection is now UP
    28  	EventNetUp Event = iota
    29  	// The device's network connection is now DOWN
    30  	EventNetDown
    31  )
    32  
    33  type ConnectMode int
    34  
    35  // Connect modes
    36  const (
    37  	ConnectModeSTA = iota // Connect as Wifi station (default)
    38  	ConnectModeAP         // Connect as Wifi Access Point
    39  )
    40  
    41  type AuthType int
    42  
    43  // Wifi authorization types.  Used when setting up an access point, or
    44  // connecting to an access point
    45  const (
    46  	AuthTypeWPA2      = iota // WPA2 authorization (default)
    47  	AuthTypeOpen             // No authorization required (open)
    48  	AuthTypeWPA              // WPA authorization
    49  	AuthTypeWPA2Mixed        // WPA2/WPA mixed authorization
    50  )
    51  
    52  const DefaultConnectTimeout = 10 * time.Second
    53  
    54  type ConnectParams struct {
    55  
    56  	// Connect mode
    57  	ConnectMode
    58  
    59  	// SSID of Wifi AP
    60  	Ssid string
    61  
    62  	// Passphrase of Wifi AP
    63  	Passphrase string
    64  
    65  	// Wifi authorization type
    66  	AuthType
    67  
    68  	// Wifi country code as two-char string.  E.g. "XX" for world-wide,
    69  	// "US" for USA, etc.
    70  	Country string
    71  
    72  	// Retries is how many attempts to connect before returning with a
    73  	// "Connect failed" error.  Zero means infinite retries.
    74  	Retries int
    75  
    76  	// Timeout duration for each connection attempt.  The default zero
    77  	// value means 10sec.
    78  	ConnectTimeout time.Duration
    79  
    80  	// Watchdog ticker duration.  On tick, the watchdog will check for
    81  	// downed connection or hardware fault and try to recover the
    82  	// connection.  Set to zero to disable watchodog.
    83  	WatchdogTimeout time.Duration
    84  }
    85  
    86  // Netlinker is TinyGo's OSI L2 data link layer interface.  Network device
    87  // drivers implement Netlinker to expose the device's L2 functionality.
    88  
    89  type Netlinker interface {
    90  
    91  	// Connect device to network
    92  	NetConnect(params *ConnectParams) error
    93  
    94  	// Disconnect device from network
    95  	NetDisconnect()
    96  
    97  	// Notify to register callback for network events
    98  	NetNotify(cb func(Event))
    99  
   100  	// GetHardwareAddr returns device MAC address
   101  	GetHardwareAddr() (net.HardwareAddr, error)
   102  }