mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-30 19:39:02 +00:00
Make TLS work over WiFiNINA
Verified on Arduino Nano33 IoT and nina fw v1.4.5
This commit is contained in:
+17
-12
@@ -50,19 +50,24 @@ func (drv *Driver) connectSocket(addr, portStr string, mode uint8) error {
|
||||
drv.proto, drv.ip, drv.port = mode, 0, 0
|
||||
|
||||
// convert port to uint16
|
||||
p64, err := strconv.ParseUint(portStr, 10, 16)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not convert port to uint16: %s", err.Error())
|
||||
}
|
||||
port := uint16(p64)
|
||||
|
||||
// look up the hostname if necessary; if an IP address was specified, the
|
||||
// same will be returned. Otherwise, an IPv4 for the hostname is returned.
|
||||
ipAddr, err := drv.dev.GetHostByName(addr)
|
||||
port, err := convertPort(portStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ip := ipAddr.AsUint32()
|
||||
|
||||
hostname := addr
|
||||
ip := uint32(0)
|
||||
|
||||
if mode != ProtoModeTLS {
|
||||
// look up the hostname if necessary; if an IP address was specified, the
|
||||
// same will be returned. Otherwise, an IPv4 for the hostname is returned.
|
||||
ipAddr, err := drv.dev.GetHostByName(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hostname = ""
|
||||
ip = ipAddr.AsUint32()
|
||||
}
|
||||
|
||||
// check to see if socket is already set; if so, stop it
|
||||
if drv.sock != NoSocketAvail {
|
||||
@@ -77,7 +82,7 @@ func (drv *Driver) connectSocket(addr, portStr string, mode uint8) error {
|
||||
}
|
||||
|
||||
// attempt to start the client
|
||||
if err := drv.dev.StartClient(ip, port, drv.sock, mode); err != nil {
|
||||
if err := drv.dev.StartClient(hostname, ip, port, drv.sock, mode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -169,7 +174,7 @@ func (drv *Driver) Write(b []byte) (n int, err error) {
|
||||
return 0, ErrNoData
|
||||
}
|
||||
if drv.proto == ProtoModeUDP {
|
||||
if err := drv.dev.StartClient(drv.ip, drv.port, drv.sock, drv.proto); err != nil {
|
||||
if err := drv.dev.StartClient("", drv.ip, drv.port, drv.sock, drv.proto); err != nil {
|
||||
return 0, fmt.Errorf("error in startClient: %w", err)
|
||||
}
|
||||
if _, err := drv.dev.InsertDataBuf(b, drv.sock); err != nil {
|
||||
|
||||
+14
-6
@@ -303,22 +303,30 @@ func (d *Device) Configure() {
|
||||
|
||||
// ----------- client methods (should this be a separate struct?) ------------
|
||||
|
||||
func (d *Device) StartClient(addr uint32, port uint16, sock uint8, mode uint8) error {
|
||||
func (d *Device) StartClient(hostname string, addr uint32, port uint16, sock uint8, mode uint8) error {
|
||||
if _debug {
|
||||
println("[StartClient] called StartClient()\r")
|
||||
fmt.Printf("[StartClient] addr: % 02X, port: %d, sock: %d\r\n", addr, port, sock)
|
||||
fmt.Printf("[StartClient] hostname: %s addr: % 02X, port: %d, sock: %d\r\n", hostname, addr, port, sock)
|
||||
}
|
||||
if err := d.waitForChipSelect(); err != nil {
|
||||
d.spiChipDeselect()
|
||||
return err
|
||||
}
|
||||
|
||||
d.sendCmd(CmdStartClientTCP, 4)
|
||||
if len(hostname) > 0 {
|
||||
d.sendCmd(CmdStartClientTCP, 5)
|
||||
d.sendParamStr(hostname, false)
|
||||
} else {
|
||||
d.sendCmd(CmdStartClientTCP, 4)
|
||||
}
|
||||
d.sendParam32(addr, false)
|
||||
d.sendParam16(port, false)
|
||||
d.sendParam8(sock, false)
|
||||
d.sendParam8(mode, true)
|
||||
|
||||
if len(hostname) > 0 {
|
||||
d.padTo4(17 + len(hostname))
|
||||
}
|
||||
|
||||
d.spiChipDeselect()
|
||||
|
||||
_, err := d.waitRspCmd1(CmdStartClientTCP)
|
||||
@@ -818,7 +826,7 @@ func (d *Device) sendCmdStr(cmd uint8, p1 string) (err error) {
|
||||
}
|
||||
l := d.sendCmd(cmd, 1)
|
||||
l += d.sendParamStr(p1, true)
|
||||
d.addPadding(l)
|
||||
d.padTo4(5 + len(p1))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1144,7 +1152,7 @@ func (d *Device) addPadding(l int) {
|
||||
|
||||
func (d *Device) padTo4(l int) {
|
||||
if _debug {
|
||||
println("addPadding", l, "\r")
|
||||
println("padTo4", l, "\r")
|
||||
}
|
||||
|
||||
for l%4 != 0 {
|
||||
|
||||
Reference in New Issue
Block a user