Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netdev #537

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8d4dddf
Add network device driver model, netdev
scottfeldman Mar 28, 2023
1b69253
Bug Fix: Get MAC after getting FW version
scottfeldman Apr 11, 2023
c32072c
BUG fix: wrong time math on i/o deadline timeouts
scottfeldman Apr 14, 2023
0215630
BUG Fix: missing expired deadline checks in some drivers
scottfeldman Apr 16, 2023
dc6a533
Typo on last BUG fix
scottfeldman Apr 17, 2023
d9c92b0
adjust timing loops to match [other] wifinina drivers
scottfeldman Apr 27, 2023
e902c69
wifinina: improve AP connect error reporting using reason code
scottfeldman Apr 27, 2023
60d976c
wifinina: don't timeout connect; just test connection status
scottfeldman Apr 27, 2023
9f2dc36
move syscall constants for networking into net space to avoid windows…
scottfeldman May 2, 2023
909976b
rename rawsocket example to just socket as raw socket means something…
scottfeldman May 2, 2023
f2749db
wifinina: add ResetIsHigh cfg switch for MKR 1010 (copied from #561)
scottfeldman May 17, 2023
dd25944
wifinina: add 'unknown failure' reason code for AP connect
scottfeldman May 17, 2023
2428ab5
s/ConnectTimeo/ConnectTimeout, s/WatchdogTimeo/WatchdogTimeout/
scottfeldman May 18, 2023
ae13191
Add new MQTT client example using natiu-mqtt
scottfeldman May 22, 2023
bb111a7
natiu: log client error on disconnect
scottfeldman May 25, 2023
ebed528
natiu: SetReadDeadline to limit blocking read to 10secs
scottfeldman May 25, 2023
49958f2
move netdev and netlink into their own packages and out of drivers
scottfeldman Jun 9, 2023
4d78f03
examples/net: move device-specific init code to common netlink/probe …
scottfeldman Jun 9, 2023
c9366c5
update net drivers to use new netdev/netlink packages
scottfeldman Jun 9, 2023
7cead22
migrate examples/net examples to use new netdev/netlink packages
scottfeldman Jun 9, 2023
5e036b4
netlink: add L2 bridge/bond/vlan intermediate drivers
scottfeldman Jun 9, 2023
041e3c6
add tcpip stack framework
scottfeldman Jun 9, 2023
603d4c4
s/RecvEthFunc/RecvEthHandle
scottfeldman Jun 12, 2023
e6e8438
add new err type for wifi auth failure
scottfeldman Jun 27, 2023
f329cb7
netlink: tighten up API comments on send/recv eth
scottfeldman Jun 27, 2023
674d6d6
net: update/add READMEs for net, netdev, and netlink
scottfeldman Jun 27, 2023
fc5e3af
recover from rebase mistakes
scottfeldman Jun 27, 2023
61477df
netlink: remove send/recv eth API for now
scottfeldman Jul 3, 2023
11e4360
Merge branch 'dev' into netdev3
deadprogram Oct 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions README-net.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
### Table of Contents

- ["net" Package](#net-package)
- [Using "net" Package](#using-net-package)
- [Using "net/http" Package](#using-nethttp-package)
- [Using "crypto/tls" Package](#using-cryptotls-package)
- [Using Sockets](#using-sockets)

## "net" Package

TinyGo's "net" package is ported from Go. The port offers a subset of Go's
"net" package. The subset maintains Go 1 compatiblity guarantee. A Go
application that uses "net" will most-likey just work on TinyGo if the usage is
within the subset offered. (There may be external constraints such as limited
SRAM on some targets that may limit full "net" functionality).

Continue below for details on using "net" and "net/http" packages.

See src/net/READMD.md in the TinyGo repo for more details on maintaining
TinyGo's "net" package.

## Using "net" Package

Ideally, TinyGo's "net" package would be Go's "net" package and applications
using "net" would just work, as-is. TinyGo's net package is a partial port of
Go's net package, so some things may not work because they have not been
ported.

There are a few features excluded during the porting process, in particular:

- No IPv6 support
- No DualStack support

Run ```go doc -all ./src/net``` in TinyGo repo to see full listing of what has
been ported. Here is a list of things known to work. You can find examples
of these at [examples/net](examples/net/).

### What is Known to Work

(These are all IPv4 only).

- TCP client and server
- UDP client
- TLS client
- HTTP client and server
- HTTPS client
- NTP client (UDP)
- MQTT client (paho & natiu)
- WebSocket client and server

Multiple sockets can be opened in a single app. For example, the app could run
as an http server listen on port :80 and also use NTP to get the current time
or send something over MQTT. There is a practical limit to the number of
active sockets per app, around 8 or 10, so don't go crazy.

Applications using Go's net package will need a few setup steps to work with
TinyGo's net package. The steps are required before using "net".

### Step 1: Probe to Load Network Driver

Call Probe() to load the correct network driver for your target. Probe()
allows the app to work on multiple targets.

```go
package main

import (
"tinygo.org/x/drivers/netlink/probe"
)

func main() {

// load network driver for target
link, dev := probe.Probe()

...
}
```

Probe() will load the driver with default configuration for the target. For
custom configuration, the app can open code Probe() for the target
requirements.

Probe() returns a [Netlinker](netlink/README.md) and a
[Netdever](netdev/README.md), interfaces implemented by the network driver.
Next, we'll use the Netlinker interface to connect the target to an IP network.

### Step 2: Connect to an IP Network

Before the net package is fully functional, we need to connect the target to an
IP network.

```go
package main

import (
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
)

func main() {

// load network driver for target
link, _ := probe.Probe()

// Connect target to IP network
link.NetConnect(&netlink.ConnectParams{
Ssid: "my SSID",
Passphrase: "my passphrase",
})

// OK to use "net" from here on
...
}
```

Optionally, get notified of IP network connects and disconnects:

```go
link.Notify(func(e netlink.Event) {
switch e {
case netlink.EventNetUp: println("Network UP")
case netlink.EventNetDown: println("Network DOWN")
})
```

Here is an example of an http server listening on port :8080:

```go
package main

import (
"fmt"
"net/http"

"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
)

func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}

func main() {

// load network driver for target
link, _ := probe.Probe()

// Connect target to IP network
link.NetConnect(&netlink.ConnectParams{
Ssid: "my SSID",
Passphrase: "my passphrase",
})

// Serve it up
http.HandleFunc("/", HelloServer)
http.ListenAndServe(":8080", nil)
}
```

## Using "net/http" Package

TinyGo's net/http package is a partial port of Go's net/http package, providing
a subset of the full net/http package. There are a few features excluded
during the porting process, in particular:

- No HTTP/2 support
- No TLS support for HTTP servers (no https servers)
- HTTP client request can't be reused

HTTP client methods (http.Get, http.Head, http.Post, and http.PostForm) are
functional. Dial clients support both HTTP and HTTPS URLs.

HTTP server methods and objects are mostly ported, but for HTTP only; HTTPS
servers are not supported.

HTTP request and response handling code is mostly ported, so most the intricacy
of parsing and writing headers is handled as in the full net/http package.

Run ```go doc -all ./src/net/http``` in TinyGo repo to see full listing.

## Using "crypto/tls" Package

TinyGo's TLS support (crypto/tls) relies on hardware offload of the TLS
protocol. This is different from Go's crypto/tls package which handles the TLS
protocol in software.

TinyGo's TLS support is only available for client applications. You can
http.Get() to an https:// address, but you cannot http.ListenAndServeTLS() an
https server.

The offloading hardware has pre-defined TLS certificates built-in.

## Using Sockets

The Netdever interface is a BSD socket-like interface so an application can make direct
socket calls, bypassing the "net" package for the lowest overhead.

Here is a simple TCP client application using direct sockets:

```go
package main

import (
"net" // only need to parse IP address

"tinygo.org/x/drivers/netdev"
"tinygo.org/x/drivers/netlink"
"tinygo.org/x/drivers/netlink/probe"
)

func main() {

// load network driver for target
link, dev := probe.Probe()

// Connect target to IP network
link.NetConnect(&netlink.ConnectParams{
Ssid: "my SSID",
Passphrase: "my passphrase",
})

// omit error handling

sock, _ := dev.Socket(netdev.AF_INET, netdev.SOCK_STREAM, netdev.IPPROTO_TCP)

dev.Connect(sock, "", net.ParseIP("10.0.0.100"), 8080)
dev.Send(sock, []bytes("hello"), 0, 0)

dev.Close(sock)
link.NetDisconnect()
}
```
20 changes: 0 additions & 20 deletions espat/adapter.go

This file was deleted.

Loading