espat: some improvements for response handling

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2022-02-02 12:20:16 +01:00
parent f7dce6ae22
commit 2d644c8b60
3 changed files with 20 additions and 15 deletions
+4 -1
View File
@@ -11,7 +11,10 @@ func (d *Device) ConnectToAccessPoint(ssid, pass string, timeout time.Duration)
return net.ErrWiFiMissingSSID
}
d.SetWifiMode(WifiModeClient)
if err := d.SetWifiMode(WifiModeClient); err != nil {
return err
}
return d.ConnectToAP(ssid, pass, 10)
}
+9 -10
View File
@@ -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.
func (d *Device) Response(timeout int) ([]byte, error) {
// read data
var size int
var start, end int
pause := 100 // pause to wait for 100 ms
retries := timeout / pause
pause := 10 * time.Millisecond
starting := time.Now()
for {
size = d.bus.Buffered()
if size > 0 {
if size := d.bus.Buffered(); size > 0 {
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 strings.Contains(string(d.response[:end]), "+IPD") {
@@ -185,12 +185,11 @@ func (d *Device) Response(timeout int) ([]byte, error) {
}
// wait longer?
retries--
if retries == 0 {
if time.Since(starting) > time.Duration(timeout)*time.Millisecond {
return nil, errors.New("response timeout error:" + string(d.response[start:end]))
}
time.Sleep(time.Duration(pause) * time.Millisecond)
time.Sleep(pause)
}
}
+7 -4
View File
@@ -36,7 +36,7 @@ func (d *Device) GetDNS(domain string) (string, error) {
// Currently only supports single connection mode.
func (d *Device) ConnectTCPSocket(addr, port string) error {
protocol := "TCP"
val := "\"" + protocol + "\",\"" + addr + "\"," + port + ",120"
val := "\"" + protocol + "\"," + addr + "," + port + ",120"
err := d.Set(TCPConnect, val)
if err != nil {
return err
@@ -51,7 +51,7 @@ func (d *Device) ConnectTCPSocket(addr, port string) error {
// ConnectUDPSocket creates a new UDP connection for the ESP8266/ESP32.
func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error {
protocol := "UDP"
val := "\"" + protocol + "\",\"" + addr + "\"," + sendport + "," + listenport + ",2"
val := "\"" + protocol + "\"," + addr + "," + sendport + "," + listenport + ",2"
err := d.Set(TCPConnect, val)
if err != nil {
return err
@@ -67,7 +67,7 @@ func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error {
// Currently only supports single connection mode.
func (d *Device) ConnectSSLSocket(addr, port string) error {
protocol := "SSL"
val := "\"" + protocol + "\",\"" + addr + "\"," + port + ",120"
val := "\"" + protocol + "\"," + addr + "," + port + ",120"
d.Set(TCPConnect, val)
// this operation takes longer, so wait up to 6 seconds to complete.
_, 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.
func (d *Device) StartSocketSend(size int) error {
val := strconv.Itoa(size)
d.Set(TCPSend, val)
err := d.Set(TCPSend, val)
if err != nil {
return err
}
// when ">" is received, it indicates
// ready to receive data