rtl8720dn: refactor by bringing this driver more in line with wifinina and espat

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2022-08-28 10:40:36 +02:00
committed by Ron Evans
parent 6f05615f3e
commit 54ddb895cc
6 changed files with 171 additions and 199 deletions
+11 -11
View File
@@ -7,15 +7,16 @@
// //
// You must install the Paho MQTT package to build this program: // You must install the Paho MQTT package to build this program:
// //
// go get -u github.com/eclipse/paho.mqtt.golang // go get -u github.com/eclipse/paho.mqtt.golang
// //
// You can check that mqttpub is running successfully with the following command. // You can check that mqttpub is running successfully with the following command.
// //
// mosquitto_sub -h test.mosquitto.org -t tinygo // mosquitto_sub -h test.mosquitto.org -t tinygo
//
package main package main
import ( import (
"machine"
"fmt" "fmt"
"math/rand" "math/rand"
"time" "time"
@@ -44,7 +45,7 @@ var buf [0x400]byte
var lastRequestTime time.Time var lastRequestTime time.Time
var conn net.Conn var conn net.Conn
var adaptor *rtl8720dn.RTL8720DN var adaptor *rtl8720dn.Driver
func main() { func main() {
err := run() err := run()
@@ -59,18 +60,17 @@ var (
) )
func run() error { func run() error {
rtl, err := setupRTL8720DN() // change the UART and pins as needed for platforms other than the WioTerminal.
if err != nil { adaptor = rtl8720dn.New(machine.UART3, machine.PB24, machine.PC24, machine.RTL8720D_CHIP_PU)
return err adaptor.Debug(debug)
} adaptor.Configure()
net.UseDriver(rtl)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) err := adaptor.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil { if err != nil {
return err return err
} }
ip, subnet, gateway, err := rtl.GetIP() ip, subnet, gateway, err := adaptor.GetIP()
if err != nil { if err != nil {
return err return err
} }
@@ -1,74 +0,0 @@
//go:build wioterminal
// +build wioterminal
package main
import (
"device/sam"
"machine"
"runtime/interrupt"
"time"
"tinygo.org/x/drivers/rtl8720dn"
)
var (
uart UARTx
)
func handleInterrupt(interrupt.Interrupt) {
// should reset IRQ
uart.Receive(byte((uart.Bus.DATA.Get() & 0xFF)))
uart.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
}
func setupRTL8720DN() (*rtl8720dn.RTL8720DN, error) {
machine.RTL8720D_CHIP_PU.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.RTL8720D_CHIP_PU.Low()
time.Sleep(100 * time.Millisecond)
machine.RTL8720D_CHIP_PU.High()
time.Sleep(1000 * time.Millisecond)
if debug {
waitSerial()
}
uart = UARTx{
UART: &machine.UART{
Buffer: machine.NewRingBuffer(),
Bus: sam.SERCOM0_USART_INT,
SERCOM: 0,
},
}
uart.Interrupt = interrupt.New(sam.IRQ_SERCOM0_2, handleInterrupt)
uart.Configure(machine.UARTConfig{TX: machine.PB24, RX: machine.PC24, BaudRate: 614400})
rtl := rtl8720dn.New(uart)
rtl.Debug(debug)
_, err := rtl.Rpc_tcpip_adapter_init()
if err != nil {
return nil, err
}
return rtl, nil
}
// Wait for user to open serial console
func waitSerial() {
for !machine.Serial.DTR() {
time.Sleep(100 * time.Millisecond)
}
}
type UARTx struct {
*machine.UART
}
func (u UARTx) Read(p []byte) (n int, err error) {
if u.Buffered() == 0 {
time.Sleep(1 * time.Millisecond)
return 0, nil
}
return u.UART.Read(p)
}
+38 -6
View File
@@ -1,25 +1,57 @@
package rtl8720dn package rtl8720dn
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers/net" "tinygo.org/x/drivers/net"
) )
func (r *RTL8720DN) ConnectToAccessPoint(ssid, pass string, timeout time.Duration) error { type Driver struct {
*RTL8720DN
}
// New returns a new RTL8720DN driver. The UART that is passed in
// will be reconfigured at the baud rate required by the device.
func New(uart *machine.UART, tx, rx, en machine.Pin) *Driver {
enable(en)
uart.Configure(machine.UARTConfig{TX: tx, RX: rx, BaudRate: 614400})
return &Driver{
RTL8720DN: &RTL8720DN{
port: &UARTx{UART: uart},
seq: 1,
sema: make(chan bool, 1),
debug: false,
},
}
}
func (d *Driver) Configure() error {
net.UseDriver(d)
_, err := d.Rpc_tcpip_adapter_init()
if err != nil {
return err
}
return nil
}
func (d *Driver) ConnectToAccessPoint(ssid, pass string, timeout time.Duration) error {
if len(ssid) == 0 { if len(ssid) == 0 {
return net.ErrWiFiMissingSSID return net.ErrWiFiMissingSSID
} }
return r.ConnectToAP(ssid, pass) return d.ConnectToAP(ssid, pass)
} }
func (r *RTL8720DN) Disconnect() error { func (d *Driver) Disconnect() error {
_, err := r.Rpc_wifi_disconnect() _, err := d.Rpc_wifi_disconnect()
return err return err
} }
func (r *RTL8720DN) GetClientIP() (string, error) { func (d *Driver) GetClientIP() (string, error) {
ip, _, _, err := r.GetIP() ip, _, _, err := d.GetIP()
return ip.String(), err return ip.String(), err
} }
+74 -74
View File
@@ -7,27 +7,27 @@ import (
// Here is the implementation of tinygo-org/x/drivers/net.DeviceDriver. // Here is the implementation of tinygo-org/x/drivers/net.DeviceDriver.
func (r *RTL8720DN) GetDNS(domain string) (string, error) { func (d *Driver) GetDNS(domain string) (string, error) {
if r.debug { if d.debug {
fmt.Printf("GetDNS(%q)\r\n", domain) fmt.Printf("GetDNS(%q)\r\n", domain)
} }
ipaddr := make([]byte, 4) ipaddr := make([]byte, 4)
_, err := r.Rpc_netconn_gethostbyname(domain, &ipaddr) _, err := d.Rpc_netconn_gethostbyname(domain, &ipaddr)
if err != nil { if err != nil {
return "", err return "", err
} }
ret, err := fmt.Sprintf("%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]), nil ret, err := fmt.Sprintf("%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]), nil
if r.debug { if d.debug {
fmt.Printf("-> %s\r\n", ret) fmt.Printf("-> %s\r\n", ret)
fmt.Printf("-> %02X.%02X.%02X.%02X\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]) fmt.Printf("-> %02X.%02X.%02X.%02X\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3])
} }
return ret, err return ret, err
} }
func (r *RTL8720DN) ConnectTCPSocket(addr, port string) error { func (d *Driver) ConnectTCPSocket(addr, port string) error {
if r.debug { if d.debug {
fmt.Printf("ConnectTCPSocket(%q, %q)\r\n", addr, port) fmt.Printf("ConnectTCPSocket(%q, %q)\r\n", addr, port)
} }
@@ -35,7 +35,7 @@ func (r *RTL8720DN) ConnectTCPSocket(addr, port string) error {
if len(addr) == 4 { if len(addr) == 4 {
copy(ipaddr, addr) copy(ipaddr, addr)
} else { } else {
_, err := r.Rpc_netconn_gethostbyname(addr, &ipaddr) _, err := d.Rpc_netconn_gethostbyname(addr, &ipaddr)
if err != nil { if err != nil {
return err return err
} }
@@ -46,19 +46,19 @@ func (r *RTL8720DN) ConnectTCPSocket(addr, port string) error {
return err return err
} }
socket, err := r.Rpc_lwip_socket(0x02, 0x01, 0x00) socket, err := d.Rpc_lwip_socket(0x02, 0x01, 0x00)
if err != nil { if err != nil {
return err return err
} }
r.socket = socket d.socket = socket
r.connectionType = ConnectionTypeTCP d.connectionType = ConnectionTypeTCP
_, err = r.Rpc_lwip_fcntl(socket, 0x00000003, 0x00000000) _, err = d.Rpc_lwip_fcntl(socket, 0x00000003, 0x00000000)
if err != nil { if err != nil {
return err return err
} }
_, err = r.Rpc_lwip_fcntl(socket, 0x00000004, 0x00000001) _, err = d.Rpc_lwip_fcntl(socket, 0x00000004, 0x00000001)
if err != nil { if err != nil {
return err return err
} }
@@ -71,7 +71,7 @@ func (r *RTL8720DN) ConnectTCPSocket(addr, port string) error {
name[6] = byte(ipaddr[2]) name[6] = byte(ipaddr[2])
name[7] = byte(ipaddr[3]) name[7] = byte(ipaddr[3])
_, err = r.Rpc_lwip_connect(socket, name, uint32(len(name))) _, err = d.Rpc_lwip_connect(socket, name, uint32(len(name)))
if err != nil { if err != nil {
return err return err
} }
@@ -80,24 +80,24 @@ func (r *RTL8720DN) ConnectTCPSocket(addr, port string) error {
writeset := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} writeset := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
exceptset := []byte{} exceptset := []byte{}
timeout := []byte{} timeout := []byte{}
_, err = r.Rpc_lwip_select(0x01, readset, writeset, exceptset, timeout) _, err = d.Rpc_lwip_select(0x01, readset, writeset, exceptset, timeout)
if err != nil { if err != nil {
return err return err
} }
optval := make([]byte, 4) optval := make([]byte, 4)
optlen := uint32(len(optval)) optlen := uint32(len(optval))
_, err = r.Rpc_lwip_getsockopt(socket, 0x00000FFF, 0x00001007, []byte{0xA5, 0xA5, 0xA5, 0xA5}, &optval, &optlen) _, err = d.Rpc_lwip_getsockopt(socket, 0x00000FFF, 0x00001007, []byte{0xA5, 0xA5, 0xA5, 0xA5}, &optval, &optlen)
if err != nil { if err != nil {
return err return err
} }
_, err = r.Rpc_lwip_fcntl(socket, 0x00000003, 0x00000000) _, err = d.Rpc_lwip_fcntl(socket, 0x00000003, 0x00000000)
if err != nil { if err != nil {
return err return err
} }
_, err = r.Rpc_lwip_fcntl(socket, 0x00000004, 0x00000000) _, err = d.Rpc_lwip_fcntl(socket, 0x00000004, 0x00000000)
if err != nil { if err != nil {
return err return err
} }
@@ -106,7 +106,7 @@ func (r *RTL8720DN) ConnectTCPSocket(addr, port string) error {
writeset = []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} writeset = []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
exceptset = []byte{} exceptset = []byte{}
timeout = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x42, 0x0F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF} timeout = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x42, 0x0F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}
_, err = r.Rpc_lwip_select(0x01, readset, writeset, exceptset, timeout) _, err = d.Rpc_lwip_select(0x01, readset, writeset, exceptset, timeout)
if err != nil { if err != nil {
return err return err
} }
@@ -114,63 +114,63 @@ func (r *RTL8720DN) ConnectTCPSocket(addr, port string) error {
return nil return nil
} }
func (r *RTL8720DN) ConnectSSLSocket(addr, port string) error { func (d *Driver) ConnectSSLSocket(addr, port string) error {
if r.debug { if d.debug {
fmt.Printf("ConnectSSLSocket(%q, %q)\r\n", addr, port) fmt.Printf("ConnectSSLSocket(%q, %q)\r\n", addr, port)
} }
if r.root_ca == nil { if d.root_ca == nil {
return fmt.Errorf("root_ca is not set") return fmt.Errorf("root_ca is not set")
} }
client, err := r.Rpc_wifi_ssl_client_create() client, err := d.Rpc_wifi_ssl_client_create()
if err != nil { if err != nil {
return err return err
} }
r.client = client d.client = client
r.connectionType = ConnectionTypeTLS d.connectionType = ConnectionTypeTLS
err = r.Rpc_wifi_ssl_init(client) err = d.Rpc_wifi_ssl_init(client)
if err != nil { if err != nil {
return err return err
} }
err = r.Rpc_wifi_ssl_set_timeout(client, 0x0001D4C0) err = d.Rpc_wifi_ssl_set_timeout(client, 0x0001D4C0)
if err != nil { if err != nil {
return err return err
} }
_, err = r.Rpc_wifi_ssl_set_rootCA(client, *r.root_ca) _, err = d.Rpc_wifi_ssl_set_rootCA(client, *d.root_ca)
if err != nil { if err != nil {
return err return err
} }
// TODO: use port // TODO: use port
_, err = r.Rpc_wifi_start_ssl_client(client, addr, 443, 0x0001D4C0) _, err = d.Rpc_wifi_start_ssl_client(client, addr, 443, 0x0001D4C0)
if err != nil { if err != nil {
return err return err
} }
_, err = r.Rpc_wifi_ssl_get_socket(client) _, err = d.Rpc_wifi_ssl_get_socket(client)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
func (r *RTL8720DN) ConnectUDPSocket(addr, sendport, listenport string) error { func (d *Driver) ConnectUDPSocket(addr, sendport, listenport string) error {
if r.debug { if d.debug {
fmt.Printf("ConnectUDPSocket(\"%d.%d.%d.%d\", %q, %q)\r\n", byte(addr[0]), byte(addr[1]), byte(addr[2]), byte(addr[3]), sendport, listenport) fmt.Printf("ConnectUDPSocket(\"%d.%d.%d.%d\", %q, %q)\r\n", byte(addr[0]), byte(addr[1]), byte(addr[2]), byte(addr[3]), sendport, listenport)
} }
socket, err := r.Rpc_lwip_socket(0x02, 0x02, 0x00) socket, err := d.Rpc_lwip_socket(0x02, 0x02, 0x00)
if err != nil { if err != nil {
return err return err
} }
r.socket = socket d.socket = socket
r.connectionType = ConnectionTypeUDP d.connectionType = ConnectionTypeUDP
optval := []byte{0x01, 0x00, 0x00, 0x00} optval := []byte{0x01, 0x00, 0x00, 0x00}
_, err = r.Rpc_lwip_setsockopt(socket, 0x00000FFF, 0x00000004, optval, uint32(len(optval))) _, err = d.Rpc_lwip_setsockopt(socket, 0x00000FFF, 0x00000004, optval, uint32(len(optval)))
if err != nil { if err != nil {
return err return err
} }
@@ -183,12 +183,12 @@ func (r *RTL8720DN) ConnectUDPSocket(addr, sendport, listenport string) error {
ip := []byte(addr) ip := []byte(addr)
// remote info // remote info
r.udpInfo[0] = byte(port >> 8) d.udpInfo[0] = byte(port >> 8)
r.udpInfo[1] = byte(port) d.udpInfo[1] = byte(port)
r.udpInfo[2] = ip[0] d.udpInfo[2] = ip[0]
r.udpInfo[3] = ip[1] d.udpInfo[3] = ip[1]
r.udpInfo[4] = ip[2] d.udpInfo[4] = ip[2]
r.udpInfo[5] = ip[3] d.udpInfo[5] = ip[3]
port, err = strconv.ParseUint(listenport, 10, 0) port, err = strconv.ParseUint(listenport, 10, 0)
if err != nil { if err != nil {
@@ -196,7 +196,7 @@ func (r *RTL8720DN) ConnectUDPSocket(addr, sendport, listenport string) error {
} }
ip_info := make([]byte, 12) ip_info := make([]byte, 12)
_, err = r.Rpc_tcpip_adapter_get_ip_info(0, &ip_info) _, err = d.Rpc_tcpip_adapter_get_ip_info(0, &ip_info)
if err != nil { if err != nil {
return err return err
} }
@@ -209,12 +209,12 @@ func (r *RTL8720DN) ConnectUDPSocket(addr, sendport, listenport string) error {
name[6] = ip_info[2] name[6] = ip_info[2]
name[7] = ip_info[3] name[7] = ip_info[3]
_, err = r.Rpc_lwip_bind(socket, name, uint32(len(name))) _, err = d.Rpc_lwip_bind(socket, name, uint32(len(name)))
if err != nil { if err != nil {
return err return err
} }
_, err = r.Rpc_lwip_fcntl(socket, 0x00000004, 0x00000000) _, err = d.Rpc_lwip_fcntl(socket, 0x00000004, 0x00000000)
if err != nil { if err != nil {
return err return err
} }
@@ -222,62 +222,62 @@ func (r *RTL8720DN) ConnectUDPSocket(addr, sendport, listenport string) error {
return nil return nil
} }
func (r *RTL8720DN) DisconnectSocket() error { func (d *Driver) DisconnectSocket() error {
if r.debug { if d.debug {
fmt.Printf("DisconnectSocket()\r\n") fmt.Printf("DisconnectSocket()\r\n")
} }
switch r.connectionType { switch d.connectionType {
case ConnectionTypeTCP, ConnectionTypeUDP: case ConnectionTypeTCP, ConnectionTypeUDP:
_, err := r.Rpc_lwip_close(r.socket) _, err := d.Rpc_lwip_close(d.socket)
if err != nil { if err != nil {
return err return err
} }
case ConnectionTypeTLS: case ConnectionTypeTLS:
err := r.Rpc_wifi_stop_ssl_socket(r.client) err := d.Rpc_wifi_stop_ssl_socket(d.client)
if err != nil { if err != nil {
return err return err
} }
err = r.Rpc_wifi_ssl_client_destroy(r.client) err = d.Rpc_wifi_ssl_client_destroy(d.client)
if err != nil { if err != nil {
return err return err
} }
default: default:
} }
r.connectionType = ConnectionTypeNone d.connectionType = ConnectionTypeNone
return nil return nil
} }
func (r *RTL8720DN) StartSocketSend(size int) error { func (d *Driver) StartSocketSend(size int) error {
if r.debug { if d.debug {
fmt.Printf("StartSocketSend(%d)\r\n", size) fmt.Printf("StartSocketSend(%d)\r\n", size)
} }
// No implementation required // No implementation required
return nil return nil
} }
func (r *RTL8720DN) Write(b []byte) (n int, err error) { func (d *Driver) Write(b []byte) (n int, err error) {
if r.debug { if d.debug {
fmt.Printf("Write(%#v)\r\n", b) fmt.Printf("Write(%#v)\r\n", b)
} }
switch r.connectionType { switch d.connectionType {
case ConnectionTypeTCP: case ConnectionTypeTCP:
sn, err := r.Rpc_lwip_send(r.socket, b, 0x00000008) sn, err := d.Rpc_lwip_send(d.socket, b, 0x00000008)
if err != nil { if err != nil {
return 0, err return 0, err
} }
n = int(sn) n = int(sn)
case ConnectionTypeUDP: case ConnectionTypeUDP:
to := []byte{0x00, 0x02, 0x0D, 0x05, 0xC0, 0xA8, 0x01, 0x76, 0xEB, 0x43, 0x00, 0x00, 0xD5, 0x27, 0x01, 0x00} to := []byte{0x00, 0x02, 0x0D, 0x05, 0xC0, 0xA8, 0x01, 0x76, 0xEB, 0x43, 0x00, 0x00, 0xD5, 0x27, 0x01, 0x00}
copy(to[2:], r.udpInfo[:]) copy(to[2:], d.udpInfo[:])
sn, err := r.Rpc_lwip_sendto(r.socket, b, 0x00000000, to, uint32(len(to))) sn, err := d.Rpc_lwip_sendto(d.socket, b, 0x00000000, to, uint32(len(to)))
if err != nil { if err != nil {
return 0, err return 0, err
} }
n = int(sn) n = int(sn)
case ConnectionTypeTLS: case ConnectionTypeTLS:
sn, err := r.Rpc_wifi_send_ssl_data(r.client, b, uint16(len(b))) sn, err := d.Rpc_wifi_send_ssl_data(d.client, b, uint16(len(b)))
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -288,22 +288,22 @@ func (r *RTL8720DN) Write(b []byte) (n int, err error) {
return n, nil return n, nil
} }
func (r *RTL8720DN) ReadSocket(b []byte) (n int, err error) { func (d *Driver) ReadSocket(b []byte) (n int, err error) {
if r.debug { if d.debug {
//fmt.Printf("ReadSocket(b)\r\n") //fmt.Printf("ReadSocket(b)\r\n")
} }
if r.connectionType == ConnectionTypeNone { if d.connectionType == ConnectionTypeNone {
return 0, nil return 0, nil
} }
switch r.connectionType { switch d.connectionType {
case ConnectionTypeTCP: case ConnectionTypeTCP:
length := len(b) length := len(b)
if length > maxUartRecvSize-16 { if length > maxUartRecvSize-16 {
length = maxUartRecvSize - 16 length = maxUartRecvSize - 16
} }
buf := b[:length] buf := b[:length]
nn, err := r.Rpc_lwip_recv(r.socket, &buf, uint32(length), 0x00000008, 0x00002800) nn, err := d.Rpc_lwip_recv(d.socket, &buf, uint32(length), 0x00000008, 0x00002800)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -311,7 +311,7 @@ func (r *RTL8720DN) ReadSocket(b []byte) (n int, err error) {
if nn == -1 { if nn == -1 {
return 0, nil return 0, nil
} else if nn == 0 { } else if nn == 0 {
return 0, r.DisconnectSocket() return 0, d.DisconnectSocket()
} }
n = int(nn) n = int(nn)
case ConnectionTypeUDP: case ConnectionTypeUDP:
@@ -322,7 +322,7 @@ func (r *RTL8720DN) ReadSocket(b []byte) (n int, err error) {
buf := b[:length] buf := b[:length]
from := make([]byte, 16) from := make([]byte, 16)
fromLen := uint32(len(from)) fromLen := uint32(len(from))
nn, err := r.Rpc_lwip_recvfrom(r.socket, &buf, uint32(length), 0x00000008, &from, &fromLen, 10000) nn, err := d.Rpc_lwip_recvfrom(d.socket, &buf, uint32(length), 0x00000008, &from, &fromLen, 10000)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -337,14 +337,14 @@ func (r *RTL8720DN) ReadSocket(b []byte) (n int, err error) {
length = maxUartRecvSize - 16 length = maxUartRecvSize - 16
} }
buf := b[:length] buf := b[:length]
nn, err := r.Rpc_wifi_get_ssl_receive(r.client, &buf, int32(length)) nn, err := d.Rpc_wifi_get_ssl_receive(d.client, &buf, int32(length))
if err != nil { if err != nil {
return 0, err return 0, err
} }
if nn < 0 { if nn < 0 {
return 0, fmt.Errorf("error %d", n) return 0, fmt.Errorf("error %d", n)
} else if nn == 0 || nn == -30848 { } else if nn == 0 || nn == -30848 {
return 0, r.DisconnectSocket() return 0, d.DisconnectSocket()
} }
n = int(nn) n = int(nn)
default: default:
@@ -353,11 +353,11 @@ func (r *RTL8720DN) ReadSocket(b []byte) (n int, err error) {
return n, nil return n, nil
} }
func (r *RTL8720DN) IsSocketDataAvailable() bool { func (d *Driver) IsSocketDataAvailable() bool {
if r.debug { if d.debug {
fmt.Printf("IsSocketDataAvailable()\r\n") fmt.Printf("IsSocketDataAvailable()\r\n")
} }
ret, err := r.Rpc_lwip_available(r.socket) ret, err := d.Rpc_lwip_available(d.socket)
if err != nil { if err != nil {
fmt.Printf("error: %s\r\n", err.Error()) fmt.Printf("error: %s\r\n", err.Error())
return false return false
@@ -368,8 +368,8 @@ func (r *RTL8720DN) IsSocketDataAvailable() bool {
return false return false
} }
func (r *RTL8720DN) Response(timeout int) ([]byte, error) { func (d *Driver) Response(timeout int) ([]byte, error) {
if r.debug { if d.debug {
fmt.Printf("Response(%d))\r\n", timeout) fmt.Printf("Response(%d))\r\n", timeout)
} }
// No implementation required // No implementation required
+39 -25
View File
@@ -1,6 +1,11 @@
package rtl8720dn package rtl8720dn
import "io" import (
"machine"
"io"
"time"
)
const maxUartRecvSize = 128 const maxUartRecvSize = 128
@@ -27,29 +32,38 @@ const (
ConnectionTypeTLS ConnectionTypeTLS
) )
func New(r io.ReadWriter) *RTL8720DN { func (d *Driver) SetSeq(s uint64) {
ret := &RTL8720DN{ d.seq = s
port: r, }
seq: 1,
sema: make(chan bool, 1), func (d *Driver) Debug(b bool) {
debug: false, d.debug = b
}
func (d *Driver) SetRootCA(s *string) {
d.root_ca = s
}
func (d *Driver) Version() (string, error) {
return d.Rpc_system_version()
}
func enable(en machine.Pin) {
en.Configure(machine.PinConfig{Mode: machine.PinOutput})
en.Low()
time.Sleep(100 * time.Millisecond)
en.High()
time.Sleep(1000 * time.Millisecond)
}
type UARTx struct {
*machine.UART
}
func (u *UARTx) Read(p []byte) (n int, err error) {
if u.Buffered() == 0 {
time.Sleep(1 * time.Millisecond)
return 0, nil
} }
return u.UART.Read(p)
return ret
}
func (r *RTL8720DN) SetSeq(s uint64) {
r.seq = s
}
func (r *RTL8720DN) Debug(b bool) {
r.debug = b
}
func (r *RTL8720DN) SetRootCA(s *string) {
r.root_ca = s
}
func (r *RTL8720DN) Version() (string, error) {
return r.Rpc_system_version()
} }
+9 -9
View File
@@ -5,21 +5,21 @@ import (
"time" "time"
) )
func (r *RTL8720DN) ConnectToAP(ssid string, password string) error { func (d *Driver) ConnectToAP(ssid string, password string) error {
if len(ssid) == 0 || len(password) == 0 { if len(ssid) == 0 || len(password) == 0 {
return fmt.Errorf("connection failed: either ssid or password not set") return fmt.Errorf("connection failed: either ssid or password not set")
} }
_, err := r.Rpc_wifi_off() _, err := d.Rpc_wifi_off()
if err != nil { if err != nil {
return err return err
} }
_, err = r.Rpc_wifi_on(0x00000001) _, err = d.Rpc_wifi_on(0x00000001)
if err != nil { if err != nil {
return err return err
} }
_, err = r.Rpc_wifi_disconnect() _, err = d.Rpc_wifi_disconnect()
if err != nil { if err != nil {
return err return err
} }
@@ -27,7 +27,7 @@ func (r *RTL8720DN) ConnectToAP(ssid string, password string) error {
numTry := 5 numTry := 5
securityType := uint32(0x00400004) securityType := uint32(0x00400004)
for i := 0; i < numTry; i++ { for i := 0; i < numTry; i++ {
ret, err := r.Rpc_wifi_connect(ssid, password, securityType, -1, 0) ret, err := d.Rpc_wifi_connect(ssid, password, securityType, -1, 0)
if err != nil { if err != nil {
return err return err
} }
@@ -41,13 +41,13 @@ func (r *RTL8720DN) ConnectToAP(ssid string, password string) error {
} }
} }
_, err = r.Rpc_tcpip_adapter_dhcpc_start(0) _, err = d.Rpc_tcpip_adapter_dhcpc_start(0)
if err != nil { if err != nil {
return err return err
} }
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
_, err = r.Rpc_wifi_is_connected_to_ap() _, err = d.Rpc_wifi_is_connected_to_ap()
if err != nil { if err != nil {
return err return err
} }
@@ -57,9 +57,9 @@ func (r *RTL8720DN) ConnectToAP(ssid string, password string) error {
return nil return nil
} }
func (r *RTL8720DN) GetIP() (ip, subnet, gateway IPAddress, err error) { func (d *Driver) GetIP() (ip, subnet, gateway IPAddress, err error) {
ip_info := make([]byte, 12) ip_info := make([]byte, 12)
_, err = r.Rpc_tcpip_adapter_get_ip_info(0, &ip_info) _, err = d.Rpc_tcpip_adapter_get_ip_info(0, &ip_info)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }