mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 12:53:23 +00:00
espat: some improvements for response handling
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
+4
-1
@@ -11,7 +11,10 @@ func (d *Device) ConnectToAccessPoint(ssid, pass string, timeout time.Duration)
|
|||||||
return net.ErrWiFiMissingSSID
|
return net.ErrWiFiMissingSSID
|
||||||
}
|
}
|
||||||
|
|
||||||
d.SetWifiMode(WifiModeClient)
|
if err := d.SetWifiMode(WifiModeClient); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return d.ConnectToAP(ssid, pass, 10)
|
return d.ConnectToAP(ssid, pass, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-10
@@ -152,17 +152,17 @@ func (d *Device) ReadSocket(b []byte) (n int, err error) {
|
|||||||
// The call will retry for up to timeout milliseconds before returning nothing.
|
// The call will retry for up to timeout milliseconds before returning nothing.
|
||||||
func (d *Device) Response(timeout int) ([]byte, error) {
|
func (d *Device) Response(timeout int) ([]byte, error) {
|
||||||
// read data
|
// read data
|
||||||
var size int
|
|
||||||
var start, end int
|
var start, end int
|
||||||
pause := 100 // pause to wait for 100 ms
|
pause := 10 * time.Millisecond
|
||||||
retries := timeout / pause
|
starting := time.Now()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
size = d.bus.Buffered()
|
if size := d.bus.Buffered(); size > 0 {
|
||||||
|
|
||||||
if size > 0 {
|
|
||||||
end += size
|
end += size
|
||||||
d.bus.Read(d.response[start:end])
|
_, err := d.bus.Read(d.response[start:end])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// if "+IPD" then read socket data
|
// if "+IPD" then read socket data
|
||||||
if strings.Contains(string(d.response[:end]), "+IPD") {
|
if strings.Contains(string(d.response[:end]), "+IPD") {
|
||||||
@@ -185,12 +185,11 @@ func (d *Device) Response(timeout int) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// wait longer?
|
// wait longer?
|
||||||
retries--
|
if time.Since(starting) > time.Duration(timeout)*time.Millisecond {
|
||||||
if retries == 0 {
|
|
||||||
return nil, errors.New("response timeout error:" + string(d.response[start:end]))
|
return nil, errors.New("response timeout error:" + string(d.response[start:end]))
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Duration(pause) * time.Millisecond)
|
time.Sleep(pause)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-4
@@ -36,7 +36,7 @@ func (d *Device) GetDNS(domain string) (string, error) {
|
|||||||
// Currently only supports single connection mode.
|
// Currently only supports single connection mode.
|
||||||
func (d *Device) ConnectTCPSocket(addr, port string) error {
|
func (d *Device) ConnectTCPSocket(addr, port string) error {
|
||||||
protocol := "TCP"
|
protocol := "TCP"
|
||||||
val := "\"" + protocol + "\",\"" + addr + "\"," + port + ",120"
|
val := "\"" + protocol + "\"," + addr + "," + port + ",120"
|
||||||
err := d.Set(TCPConnect, val)
|
err := d.Set(TCPConnect, val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -51,7 +51,7 @@ func (d *Device) ConnectTCPSocket(addr, port string) error {
|
|||||||
// ConnectUDPSocket creates a new UDP connection for the ESP8266/ESP32.
|
// ConnectUDPSocket creates a new UDP connection for the ESP8266/ESP32.
|
||||||
func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error {
|
func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error {
|
||||||
protocol := "UDP"
|
protocol := "UDP"
|
||||||
val := "\"" + protocol + "\",\"" + addr + "\"," + sendport + "," + listenport + ",2"
|
val := "\"" + protocol + "\"," + addr + "," + sendport + "," + listenport + ",2"
|
||||||
err := d.Set(TCPConnect, val)
|
err := d.Set(TCPConnect, val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -67,7 +67,7 @@ func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error {
|
|||||||
// Currently only supports single connection mode.
|
// Currently only supports single connection mode.
|
||||||
func (d *Device) ConnectSSLSocket(addr, port string) error {
|
func (d *Device) ConnectSSLSocket(addr, port string) error {
|
||||||
protocol := "SSL"
|
protocol := "SSL"
|
||||||
val := "\"" + protocol + "\",\"" + addr + "\"," + port + ",120"
|
val := "\"" + protocol + "\"," + addr + "," + port + ",120"
|
||||||
d.Set(TCPConnect, val)
|
d.Set(TCPConnect, val)
|
||||||
// this operation takes longer, so wait up to 6 seconds to complete.
|
// this operation takes longer, so wait up to 6 seconds to complete.
|
||||||
_, err := d.Response(6000)
|
_, err := d.Response(6000)
|
||||||
@@ -123,7 +123,10 @@ func (d *Device) GetTCPTransferMode() ([]byte, error) {
|
|||||||
// StartSocketSend gets the ESP8266/ESP32 ready to receive TCP/UDP socket data.
|
// StartSocketSend gets the ESP8266/ESP32 ready to receive TCP/UDP socket data.
|
||||||
func (d *Device) StartSocketSend(size int) error {
|
func (d *Device) StartSocketSend(size int) error {
|
||||||
val := strconv.Itoa(size)
|
val := strconv.Itoa(size)
|
||||||
d.Set(TCPSend, val)
|
err := d.Set(TCPSend, val)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// when ">" is received, it indicates
|
// when ">" is received, it indicates
|
||||||
// ready to receive data
|
// ready to receive data
|
||||||
|
|||||||
Reference in New Issue
Block a user