wifinina: add generated strings, improved debugging system and messages

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2023-04-30 23:42:29 +02:00
committed by Ron Evans
parent aac959eb7b
commit bc9177726d
13 changed files with 506 additions and 209 deletions
+40 -202
View File
@@ -20,8 +20,6 @@ import (
"tinygo.org/x/drivers/net"
)
const _debug = false
const (
MaxSockets = 4
MaxNetworks = 10
@@ -36,38 +34,9 @@ const (
WlFailure = -1
WlSuccess = 1
)
StatusNoShield ConnectionStatus = 255
StatusIdle ConnectionStatus = 0
StatusNoSSIDAvail ConnectionStatus = 1
StatusScanCompleted ConnectionStatus = 2
StatusConnected ConnectionStatus = 3
StatusConnectFailed ConnectionStatus = 4
StatusConnectionLost ConnectionStatus = 5
StatusDisconnected ConnectionStatus = 6
EncTypeTKIP EncryptionType = 2
EncTypeCCMP EncryptionType = 4
EncTypeWEP EncryptionType = 5
EncTypeNone EncryptionType = 7
EncTypeAuto EncryptionType = 8
TCPStateClosed = 0
TCPStateListen = 1
TCPStateSynSent = 2
TCPStateSynRcvd = 3
TCPStateEstablished = 4
TCPStateFinWait1 = 5
TCPStateFinWait2 = 6
TCPStateCloseWait = 7
TCPStateClosing = 8
TCPStateLastACK = 9
TCPStateTimeWait = 10
/*
// Default state value for Wifi state field
#define NA_STATE -1
*/
const (
FlagCmd = 0
FlagReply = 1 << 7
FlagData = 0x40
@@ -75,88 +44,7 @@ const (
NinaCmdPos = 1
NinaParamLenPos = 2
CmdStart = 0xE0
CmdEnd = 0xEE
CmdErr = 0xEF
dummyData = 0xFF
CmdSetNet = 0x10
CmdSetPassphrase = 0x11
CmdSetKey = 0x12
CmdSetIPConfig = 0x14
CmdSetDNSConfig = 0x15
CmdSetHostname = 0x16
CmdSetPowerMode = 0x17
CmdSetAPNet = 0x18
CmdSetAPPassphrase = 0x19
CmdSetDebug = 0x1A
CmdGetTemperature = 0x1B
CmdGetReasonCode = 0x1F
// TEST_CMD = 0x13
CmdGetConnStatus = 0x20
CmdGetIPAddr = 0x21
CmdGetMACAddr = 0x22
CmdGetCurrSSID = 0x23
CmdGetCurrBSSID = 0x24
CmdGetCurrRSSI = 0x25
CmdGetCurrEncrType = 0x26
CmdScanNetworks = 0x27
CmdStartServerTCP = 0x28
CmdGetStateTCP = 0x29
CmdDataSentTCP = 0x2A
CmdAvailDataTCP = 0x2B
CmdGetDataTCP = 0x2C
CmdStartClientTCP = 0x2D
CmdStopClientTCP = 0x2E
CmdGetClientStateTCP = 0x2F
CmdDisconnect = 0x30
CmdGetIdxRSSI = 0x32
CmdGetIdxEncrType = 0x33
CmdReqHostByName = 0x34
CmdGetHostByName = 0x35
CmdStartScanNetworks = 0x36
CmdGetFwVersion = 0x37
CmdSendDataUDP = 0x39
CmdGetRemoteData = 0x3A
CmdGetTime = 0x3B
CmdGetIdxBSSID = 0x3C
CmdGetIdxChannel = 0x3D
CmdPing = 0x3E
CmdGetSocket = 0x3F
// GET_IDX_SSID_CMD = 0x31,
// GET_TEST_CMD = 0x38
// All command with DATA_FLAG 0x40 send a 16bit Len
CmdSendDataTCP = 0x44
CmdGetDatabufTCP = 0x45
CmdInsertDataBuf = 0x46
// regular format commands
CmdSetPinMode = 0x50
CmdSetDigitalWrite = 0x51
CmdSetAnalogWrite = 0x52
ErrTimeoutChipReady Error = 0x01
ErrTimeoutChipSelect Error = 0x02
ErrCheckStartCmd Error = 0x03
ErrWaitRsp Error = 0x04
ErrUnexpectedLength Error = 0xE0
ErrNoParamsReturned Error = 0xE1
ErrIncorrectSentinel Error = 0xE2
ErrCmdErrorReceived Error = 0xEF
ErrNotImplemented Error = 0xF0
ErrUnknownHost Error = 0xF1
ErrSocketAlreadySet Error = 0xF2
ErrConnectionTimeout Error = 0xF3
ErrNoData Error = 0xF4
ErrDataNotWritten Error = 0xF5
ErrCheckDataError Error = 0xF6
ErrBufferTooSmall Error = 0xF7
ErrNoSocketAvail Error = 0xFF
NoSocketAvail uint8 = 0xFF
)
const (
@@ -166,50 +54,6 @@ const (
ProtoModeMul
)
type ConnectionStatus uint8
func (c ConnectionStatus) String() string {
switch c {
case StatusIdle:
return "Idle"
case StatusNoSSIDAvail:
return "No SSID Available"
case StatusScanCompleted:
return "Scan Completed"
case StatusConnected:
return "Connected"
case StatusConnectFailed:
return "Connect Failed"
case StatusConnectionLost:
return "Connection Lost"
case StatusDisconnected:
return "Disconnected"
case StatusNoShield:
return "No Shield"
default:
return "Unknown"
}
}
type EncryptionType uint8
func (e EncryptionType) String() string {
switch e {
case EncTypeTKIP:
return "TKIP"
case EncTypeCCMP:
return "WPA2"
case EncTypeWEP:
return "WEP"
case EncTypeNone:
return "None"
case EncTypeAuto:
return "Auto"
default:
return "Unknown"
}
}
type IPAddress string // TODO: does WiFiNINA support ipv6???
func (addr IPAddress) String() string {
@@ -252,12 +96,6 @@ func (addr MACAddress) String() string {
return result
}
type Error uint8
func (err Error) Error() string {
return "wifinina error: 0x" + hex.EncodeToString([]byte{uint8(err)})
}
// Cmd Struct Message */
// ._______________________________________________________________________.
// | START CMD | C/R | CMD | N.PARAM | PARAM LEN | PARAM | .. | END CMD |
@@ -798,7 +636,7 @@ func (d *Device) getMACAddress(l uint8, err error) (MACAddress, error) {
}
// req0 sends a command to the device with no request parameters
func (d *Device) req0(cmd uint8) (l uint8, err error) {
func (d *Device) req0(cmd CommandType) (l uint8, err error) {
if err := d.sendCmd0(cmd); err != nil {
return 0, err
}
@@ -806,12 +644,12 @@ func (d *Device) req0(cmd uint8) (l uint8, err error) {
}
// req1 sends a command to the device with a single dummy parameters of 0xFF
func (d *Device) req1(cmd uint8) (l uint8, err error) {
func (d *Device) req1(cmd CommandType) (l uint8, err error) {
return d.reqUint8(cmd, dummyData)
}
// reqUint8 sends a command to the device with a single uint8 parameter
func (d *Device) reqUint8(cmd uint8, data uint8) (l uint8, err error) {
func (d *Device) reqUint8(cmd CommandType, data uint8) (l uint8, err error) {
if err := d.sendCmdPadded1(cmd, data); err != nil {
return 0, err
}
@@ -819,7 +657,7 @@ func (d *Device) reqUint8(cmd uint8, data uint8) (l uint8, err error) {
}
// req2Uint8 sends a command to the device with two uint8 parameters
func (d *Device) req2Uint8(cmd, p1, p2 uint8) (l uint8, err error) {
func (d *Device) req2Uint8(cmd CommandType, p1, p2 uint8) (l uint8, err error) {
if err := d.sendCmdPadded2(cmd, p1, p2); err != nil {
return 0, err
}
@@ -827,7 +665,7 @@ func (d *Device) req2Uint8(cmd, p1, p2 uint8) (l uint8, err error) {
}
// reqStr sends a command to the device with a single string parameter
func (d *Device) reqStr(cmd uint8, p1 string) (uint8, error) {
func (d *Device) reqStr(cmd CommandType, p1 string) (uint8, error) {
if err := d.sendCmdStr(cmd, p1); err != nil {
return 0, err
}
@@ -835,7 +673,7 @@ func (d *Device) reqStr(cmd uint8, p1 string) (uint8, error) {
}
// reqStr sends a command to the device with 2 string parameters
func (d *Device) reqStr2(cmd uint8, p1 string, p2 string) (uint8, error) {
func (d *Device) reqStr2(cmd CommandType, p1 string, p2 string) (uint8, error) {
if err := d.sendCmdStr2(cmd, p1, p2); err != nil {
return 0, err
}
@@ -843,7 +681,7 @@ func (d *Device) reqStr2(cmd uint8, p1 string, p2 string) (uint8, error) {
}
// reqStrRsp0 sends a command passing a string slice for the response
func (d *Device) reqRspStr0(cmd uint8, sl []string) (l uint8, err error) {
func (d *Device) reqRspStr0(cmd CommandType, sl []string) (l uint8, err error) {
if err := d.sendCmd0(cmd); err != nil {
return 0, err
}
@@ -855,7 +693,7 @@ func (d *Device) reqRspStr0(cmd uint8, sl []string) (l uint8, err error) {
}
// reqStrRsp1 sends a command with a uint8 param and a string slice for the response
func (d *Device) reqRspStr1(cmd uint8, data uint8, sl []string) (uint8, error) {
func (d *Device) reqRspStr1(cmd CommandType, data uint8, sl []string) (uint8, error) {
if err := d.sendCmdPadded1(cmd, data); err != nil {
return 0, err
}
@@ -866,7 +704,7 @@ func (d *Device) reqRspStr1(cmd uint8, data uint8, sl []string) (uint8, error) {
return d.waitRspStr(cmd, sl)
}
func (d *Device) sendCmd0(cmd uint8) error {
func (d *Device) sendCmd0(cmd CommandType) error {
defer d.spiChipDeselect()
if err := d.waitForChipSelect(); err != nil {
return err
@@ -875,7 +713,7 @@ func (d *Device) sendCmd0(cmd uint8) error {
return nil
}
func (d *Device) sendCmdPadded1(cmd uint8, data uint8) error {
func (d *Device) sendCmdPadded1(cmd CommandType, data uint8) error {
defer d.spiChipDeselect()
if err := d.waitForChipSelect(); err != nil {
return err
@@ -887,7 +725,7 @@ func (d *Device) sendCmdPadded1(cmd uint8, data uint8) error {
return nil
}
func (d *Device) sendCmdPadded2(cmd, data1, data2 uint8) error {
func (d *Device) sendCmdPadded2(cmd CommandType, data1, data2 uint8) error {
defer d.spiChipDeselect()
if err := d.waitForChipSelect(); err != nil {
return err
@@ -899,7 +737,7 @@ func (d *Device) sendCmdPadded2(cmd, data1, data2 uint8) error {
return nil
}
func (d *Device) sendCmdStr(cmd uint8, p1 string) (err error) {
func (d *Device) sendCmdStr(cmd CommandType, p1 string) (err error) {
defer d.spiChipDeselect()
if err := d.waitForChipSelect(); err != nil {
return err
@@ -910,7 +748,7 @@ func (d *Device) sendCmdStr(cmd uint8, p1 string) (err error) {
return nil
}
func (d *Device) sendCmdStr2(cmd uint8, p1 string, p2 string) (err error) {
func (d *Device) sendCmdStr2(cmd CommandType, p1 string, p2 string) (err error) {
defer d.spiChipDeselect()
if err := d.waitForChipSelect(); err != nil {
return err
@@ -922,7 +760,7 @@ func (d *Device) sendCmdStr2(cmd uint8, p1 string, p2 string) (err error) {
return nil
}
func (d *Device) waitRspCmd1(cmd uint8) (l uint8, err error) {
func (d *Device) waitRspCmd1(cmd CommandType) (l uint8, err error) {
defer d.spiChipDeselect()
if err = d.waitForChipSelect(); err != nil {
return
@@ -930,21 +768,21 @@ func (d *Device) waitRspCmd1(cmd uint8) (l uint8, err error) {
return d.waitRspCmd(cmd, 1)
}
func (d *Device) sendCmd(cmd uint8, numParam uint8) (l int) {
func (d *Device) sendCmd(cmd CommandType, numParam uint8) (l int) {
if _debug {
fmt.Printf(
"sendCmd: %02X %02X %02X",
CmdStart, cmd & ^(uint8(FlagReply)), numParam)
"sendCmd: %s %s(%02X) %02X",
CmdStart, cmd, uint8(cmd) & ^(uint8(FlagReply)), numParam)
}
l = 3
d.SPI.Transfer(CmdStart)
d.SPI.Transfer(cmd & ^(uint8(FlagReply)))
d.SPI.Transfer(byte(CmdStart))
d.SPI.Transfer(uint8(cmd) & ^(uint8(FlagReply)))
d.SPI.Transfer(numParam)
if numParam == 0 {
d.SPI.Transfer(CmdEnd)
d.SPI.Transfer(byte(CmdEnd))
l += 1
if _debug {
fmt.Printf(" %02X", CmdEnd)
fmt.Printf(" %s", CmdEnd)
}
}
if _debug {
@@ -976,9 +814,9 @@ func (d *Device) sendParamBuf(p []byte, isLastParam bool) (l int) {
}
if isLastParam {
if _debug {
fmt.Printf(" %02X", CmdEnd)
fmt.Printf(" %s", CmdEnd)
}
d.SPI.Transfer(CmdEnd)
d.SPI.Transfer(byte(CmdEnd))
l += 1
}
if _debug {
@@ -994,7 +832,7 @@ func (d *Device) sendParamStr(p string, isLastParam bool) (l int) {
d.SPI.Tx([]byte(p), nil)
}
if isLastParam {
d.SPI.Transfer(CmdEnd)
d.SPI.Transfer(byte(CmdEnd))
l += 1
}
return
@@ -1008,7 +846,7 @@ func (d *Device) sendParam8(p uint8, isLastParam bool) (l int) {
d.SPI.Transfer(1)
d.SPI.Transfer(p)
if isLastParam {
d.SPI.Transfer(CmdEnd)
d.SPI.Transfer(byte(CmdEnd))
l += 1
}
return
@@ -1020,7 +858,7 @@ func (d *Device) sendParam16(p uint16, isLastParam bool) (l int) {
d.SPI.Transfer(uint8(p >> 8))
d.SPI.Transfer(uint8(p & 0xFF))
if isLastParam {
d.SPI.Transfer(CmdEnd)
d.SPI.Transfer(byte(CmdEnd))
l += 1
}
return
@@ -1034,14 +872,14 @@ func (d *Device) sendParam32(p uint32, isLastParam bool) (l int) {
d.SPI.Transfer(uint8(p >> 8))
d.SPI.Transfer(uint8(p & 0xFF))
if isLastParam {
d.SPI.Transfer(CmdEnd)
d.SPI.Transfer(byte(CmdEnd))
l += 1
}
return
}
func (d *Device) checkStartCmd() (bool, error) {
check, err := d.waitSpiChar(CmdStart)
check, err := d.waitSpiChar(byte(CmdStart))
if err != nil {
return false, err
}
@@ -1100,7 +938,7 @@ func (d *Device) waitSpiChar(wait byte) (bool, error) {
for first := true; first || (timeout > 0 && read != wait); timeout-- {
first = false
d.readParam(&read)
if read == CmdErr {
if read == byte(CmdErr) {
return false, ErrCmdErrorReceived
}
}
@@ -1110,7 +948,7 @@ func (d *Device) waitSpiChar(wait byte) (bool, error) {
return read == wait, nil
}
func (d *Device) waitRspCmd(cmd uint8, np uint8) (l uint8, err error) {
func (d *Device) waitRspCmd(cmd CommandType, np uint8) (l uint8, err error) {
if _debug {
println("waitRspCmd")
}
@@ -1119,7 +957,7 @@ func (d *Device) waitRspCmd(cmd uint8, np uint8) (l uint8, err error) {
if check, err = d.checkStartCmd(); !check {
return
}
if check = d.readAndCheckByte(cmd|FlagReply, &data); !check {
if check = d.readAndCheckByte(byte(cmd)|FlagReply, &data); !check {
return
}
if check = d.readAndCheckByte(np, &data); check {
@@ -1128,13 +966,13 @@ func (d *Device) waitRspCmd(cmd uint8, np uint8) (l uint8, err error) {
d.readParam(&d.buf[i])
}
}
if !d.readAndCheckByte(CmdEnd, &data) {
if !d.readAndCheckByte(byte(CmdEnd), &data) {
err = ErrIncorrectSentinel
}
return
}
func (d *Device) waitRspBuf16(cmd uint8, buf []byte) (l uint16, err error) {
func (d *Device) waitRspBuf16(cmd CommandType, buf []byte) (l uint16, err error) {
if _debug {
println("waitRspBuf16")
}
@@ -1143,7 +981,7 @@ func (d *Device) waitRspBuf16(cmd uint8, buf []byte) (l uint16, err error) {
if check, err = d.checkStartCmd(); !check {
return
}
if check = d.readAndCheckByte(cmd|FlagReply, &data); !check {
if check = d.readAndCheckByte(byte(cmd)|FlagReply, &data); !check {
return
}
if check = d.readAndCheckByte(1, &data); check {
@@ -1152,13 +990,13 @@ func (d *Device) waitRspBuf16(cmd uint8, buf []byte) (l uint16, err error) {
d.readParam(&buf[i])
}
}
if !d.readAndCheckByte(CmdEnd, &data) {
if !d.readAndCheckByte(byte(CmdEnd), &data) {
err = ErrIncorrectSentinel
}
return
}
func (d *Device) waitRspStr(cmd uint8, sl []string) (numRead uint8, err error) {
func (d *Device) waitRspStr(cmd CommandType, sl []string) (numRead uint8, err error) {
if _debug {
println("waitRspStr")
}
@@ -1167,7 +1005,7 @@ func (d *Device) waitRspStr(cmd uint8, sl []string) (numRead uint8, err error) {
if check, err = d.checkStartCmd(); !check {
return
}
if check = d.readAndCheckByte(cmd|FlagReply, &data); !check {
if check = d.readAndCheckByte(byte(cmd)|FlagReply, &data); !check {
return
}
numRead, _ = d.SPI.Transfer(dummyData)
@@ -1193,7 +1031,7 @@ func (d *Device) waitRspStr(cmd uint8, sl []string) (numRead uint8, err error) {
}
sl[j] = ""
}
if !d.readAndCheckByte(CmdEnd, &data) {
if !d.readAndCheckByte(byte(CmdEnd), &data) {
err = ErrIncorrectSentinel
}
if numRead > maxNumRead {