mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-03 06:27:47 +00:00
8238f96319
This moves the netdev/netlink namespace out of drivers and into their own packages. Also, defines netdev and netlink as L3/L4 and L2 OSI layers, respectively. Move some L3 functionality from netlink to netdev (GetIPAddr). For netlink, add ConnectParams for NetConnect to pass in L2 connection parameters (ssid, pass, auth_type, etc). Also adds connection mode (STA, AP, etc). For netlink, add SendEth and RecvEthFunc funcs to handle L2 send/recv of Ethernet pkts.
104 lines
1.9 KiB
Go
104 lines
1.9 KiB
Go
// This example opens a TCP connection and sends some data, for the purpose of
|
|
// testing speed and connectivity.
|
|
//
|
|
// You can open a server to accept connections from this program using:
|
|
//
|
|
// nc -lk 8080
|
|
|
|
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal || challenger_rp2040 || pico
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"machine"
|
|
"net"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/netlink"
|
|
"tinygo.org/x/drivers/netlink/probe"
|
|
)
|
|
|
|
var (
|
|
ssid string
|
|
pass string
|
|
addr string = "10.0.0.100:8080"
|
|
)
|
|
|
|
var buf = &bytes.Buffer{}
|
|
|
|
func main() {
|
|
|
|
waitSerial()
|
|
|
|
link, _ := probe.Probe()
|
|
|
|
err := link.NetConnect(&netlink.ConnectParams{
|
|
Ssid: ssid,
|
|
Passphrase: pass,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for {
|
|
sendBatch()
|
|
time.Sleep(500 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func sendBatch() {
|
|
|
|
// make TCP connection
|
|
message("---------------\r\nDialing TCP connection")
|
|
conn, err := net.Dial("tcp", addr)
|
|
for ; err != nil; conn, err = net.Dial("tcp", addr) {
|
|
message(err.Error())
|
|
time.Sleep(5 * time.Second)
|
|
}
|
|
|
|
n := 0
|
|
w := 0
|
|
start := time.Now()
|
|
|
|
// send data
|
|
message("Sending data")
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
buf.Reset()
|
|
fmt.Fprint(buf,
|
|
"\r---------------------------- i == ", i, " ----------------------------"+
|
|
"\r---------------------------- i == ", i, " ----------------------------")
|
|
if w, err = conn.Write(buf.Bytes()); err != nil {
|
|
println("error:", err.Error(), "\r")
|
|
break
|
|
}
|
|
n += w
|
|
}
|
|
|
|
buf.Reset()
|
|
ms := time.Now().Sub(start).Milliseconds()
|
|
fmt.Fprint(buf, "\nWrote ", n, " bytes in ", ms, " ms\r\n")
|
|
message(buf.String())
|
|
|
|
if _, err := conn.Write(buf.Bytes()); err != nil {
|
|
println("error:", err.Error(), "\r")
|
|
}
|
|
|
|
println("Disconnecting TCP...")
|
|
conn.Close()
|
|
}
|
|
|
|
func message(msg string) {
|
|
println(msg, "\r")
|
|
}
|
|
|
|
// Wait for user to open serial console
|
|
func waitSerial() {
|
|
for !machine.Serial.DTR() {
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
}
|