From bc9177726db44c7fc1ffd7f1b13ca1f0a2cfdef7 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 30 Apr 2023 23:42:29 +0200 Subject: [PATCH] wifinina: add generated strings, improved debugging system and messages Signed-off-by: deadprogram --- wifinina/README.md | 16 ++ wifinina/commands.go | 67 ++++++++ wifinina/commandtype_string.go | 118 ++++++++++++++ wifinina/connectionstatus_string.go | 39 +++++ wifinina/debug.go | 5 + wifinina/encryptiontype_string.go | 42 +++++ wifinina/error_string.go | 59 +++++++ wifinina/http.go | 4 +- wifinina/nodebug.go | 5 + wifinina/status.go | 75 +++++++++ wifinina/tcp.go | 10 +- wifinina/tcpstate_string.go | 33 ++++ wifinina/wifinina.go | 242 +++++----------------------- 13 files changed, 506 insertions(+), 209 deletions(-) create mode 100644 wifinina/commands.go create mode 100644 wifinina/commandtype_string.go create mode 100644 wifinina/connectionstatus_string.go create mode 100644 wifinina/debug.go create mode 100644 wifinina/encryptiontype_string.go create mode 100644 wifinina/error_string.go create mode 100644 wifinina/nodebug.go create mode 100644 wifinina/status.go create mode 100644 wifinina/tcpstate_string.go diff --git a/wifinina/README.md b/wifinina/README.md index b851fd7..868e07f 100644 --- a/wifinina/README.md +++ b/wifinina/README.md @@ -103,3 +103,19 @@ Verify correct version of nina-fw installed by flasing `CheckFirmwareVersion.ino Adafruit provides very good instructions for updating their boards that provide a ESP32 WiFi-BLE co-processor. For more information, please see: https://learn.adafruit.com/upgrading-esp32-firmware + +## Updating the driver + +If you modify the WiFiNINA status or command codes, you will also need to regenerate the display strings. + +``` +go generate ./wifinina/status.go ./wifinina/commands.go +``` + +## Debugging + +You can output debug information to the serial console by using the `wifidebug` tag. + +``` +tinygo flash -target pyportal -ldflags="-X main.ssid=something -X main.pass=somethingelse" -tags=wifidebug -monitor ./examples/wifinina/webclient/ +``` diff --git a/wifinina/commands.go b/wifinina/commands.go new file mode 100644 index 0000000..c43c1dc --- /dev/null +++ b/wifinina/commands.go @@ -0,0 +1,67 @@ +package wifinina + +type CommandType uint8 + +//go:generate stringer -type=CommandType -trimprefix=Cmd +const ( + CmdStart CommandType = 0xE0 + CmdEnd CommandType = 0xEE + CmdErr CommandType = 0xEF + + CmdSetNet CommandType = 0x10 + CmdSetPassphrase CommandType = 0x11 + CmdSetKey CommandType = 0x12 + CmdSetIPConfig CommandType = 0x14 + CmdSetDNSConfig CommandType = 0x15 + CmdSetHostname CommandType = 0x16 + CmdSetPowerMode CommandType = 0x17 + CmdSetAPNet CommandType = 0x18 + CmdSetAPPassphrase CommandType = 0x19 + CmdSetDebug CommandType = 0x1A + CmdGetTemperature CommandType = 0x1B + CmdGetReasonCode CommandType = 0x1F + // TEST_CMD = 0x13 + + CmdGetConnStatus CommandType = 0x20 + CmdGetIPAddr CommandType = 0x21 + CmdGetMACAddr CommandType = 0x22 + CmdGetCurrSSID CommandType = 0x23 + CmdGetCurrBSSID CommandType = 0x24 + CmdGetCurrRSSI CommandType = 0x25 + CmdGetCurrEncrType CommandType = 0x26 + CmdScanNetworks CommandType = 0x27 + CmdStartServerTCP CommandType = 0x28 + CmdGetStateTCP CommandType = 0x29 + CmdDataSentTCP CommandType = 0x2A + CmdAvailDataTCP CommandType = 0x2B + CmdGetDataTCP CommandType = 0x2C + CmdStartClientTCP CommandType = 0x2D + CmdStopClientTCP CommandType = 0x2E + CmdGetClientStateTCP CommandType = 0x2F + CmdDisconnect CommandType = 0x30 + CmdGetIdxRSSI CommandType = 0x32 + CmdGetIdxEncrType CommandType = 0x33 + CmdReqHostByName CommandType = 0x34 + CmdGetHostByName CommandType = 0x35 + CmdStartScanNetworks CommandType = 0x36 + CmdGetFwVersion CommandType = 0x37 + CmdSendDataUDP CommandType = 0x39 + CmdGetRemoteData CommandType = 0x3A + CmdGetTime CommandType = 0x3B + CmdGetIdxBSSID CommandType = 0x3C + CmdGetIdxChannel CommandType = 0x3D + CmdPing CommandType = 0x3E + CmdGetSocket CommandType = 0x3F + // GET_IDX_SSID_CMD = 0x31, + // GET_TEST_CMD = 0x38 + + // All command with DATA_FLAG 0x40 send a 16bit Len + CmdSendDataTCP CommandType = 0x44 + CmdGetDatabufTCP CommandType = 0x45 + CmdInsertDataBuf CommandType = 0x46 + + // regular format commands + CmdSetPinMode CommandType = 0x50 + CmdSetDigitalWrite CommandType = 0x51 + CmdSetAnalogWrite CommandType = 0x52 +) diff --git a/wifinina/commandtype_string.go b/wifinina/commandtype_string.go new file mode 100644 index 0000000..ffa15ac --- /dev/null +++ b/wifinina/commandtype_string.go @@ -0,0 +1,118 @@ +// Code generated by "stringer -type=CommandType -trimprefix=Cmd"; DO NOT EDIT. + +package wifinina + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[CmdStart-224] + _ = x[CmdEnd-238] + _ = x[CmdErr-239] + _ = x[CmdSetNet-16] + _ = x[CmdSetPassphrase-17] + _ = x[CmdSetKey-18] + _ = x[CmdSetIPConfig-20] + _ = x[CmdSetDNSConfig-21] + _ = x[CmdSetHostname-22] + _ = x[CmdSetPowerMode-23] + _ = x[CmdSetAPNet-24] + _ = x[CmdSetAPPassphrase-25] + _ = x[CmdSetDebug-26] + _ = x[CmdGetTemperature-27] + _ = x[CmdGetReasonCode-31] + _ = x[CmdGetConnStatus-32] + _ = x[CmdGetIPAddr-33] + _ = x[CmdGetMACAddr-34] + _ = x[CmdGetCurrSSID-35] + _ = x[CmdGetCurrBSSID-36] + _ = x[CmdGetCurrRSSI-37] + _ = x[CmdGetCurrEncrType-38] + _ = x[CmdScanNetworks-39] + _ = x[CmdStartServerTCP-40] + _ = x[CmdGetStateTCP-41] + _ = x[CmdDataSentTCP-42] + _ = x[CmdAvailDataTCP-43] + _ = x[CmdGetDataTCP-44] + _ = x[CmdStartClientTCP-45] + _ = x[CmdStopClientTCP-46] + _ = x[CmdGetClientStateTCP-47] + _ = x[CmdDisconnect-48] + _ = x[CmdGetIdxRSSI-50] + _ = x[CmdGetIdxEncrType-51] + _ = x[CmdReqHostByName-52] + _ = x[CmdGetHostByName-53] + _ = x[CmdStartScanNetworks-54] + _ = x[CmdGetFwVersion-55] + _ = x[CmdSendDataUDP-57] + _ = x[CmdGetRemoteData-58] + _ = x[CmdGetTime-59] + _ = x[CmdGetIdxBSSID-60] + _ = x[CmdGetIdxChannel-61] + _ = x[CmdPing-62] + _ = x[CmdGetSocket-63] + _ = x[CmdSendDataTCP-68] + _ = x[CmdGetDatabufTCP-69] + _ = x[CmdInsertDataBuf-70] + _ = x[CmdSetPinMode-80] + _ = x[CmdSetDigitalWrite-81] + _ = x[CmdSetAnalogWrite-82] +} + +const ( + _CommandType_name_0 = "SetNetSetPassphraseSetKey" + _CommandType_name_1 = "SetIPConfigSetDNSConfigSetHostnameSetPowerModeSetAPNetSetAPPassphraseSetDebugGetTemperature" + _CommandType_name_2 = "GetReasonCodeGetConnStatusGetIPAddrGetMACAddrGetCurrSSIDGetCurrBSSIDGetCurrRSSIGetCurrEncrTypeScanNetworksStartServerTCPGetStateTCPDataSentTCPAvailDataTCPGetDataTCPStartClientTCPStopClientTCPGetClientStateTCPDisconnect" + _CommandType_name_3 = "GetIdxRSSIGetIdxEncrTypeReqHostByNameGetHostByNameStartScanNetworksGetFwVersion" + _CommandType_name_4 = "SendDataUDPGetRemoteDataGetTimeGetIdxBSSIDGetIdxChannelPingGetSocket" + _CommandType_name_5 = "SendDataTCPGetDatabufTCPInsertDataBuf" + _CommandType_name_6 = "SetPinModeSetDigitalWriteSetAnalogWrite" + _CommandType_name_7 = "Start" + _CommandType_name_8 = "EndErr" +) + +var ( + _CommandType_index_0 = [...]uint8{0, 6, 19, 25} + _CommandType_index_1 = [...]uint8{0, 11, 23, 34, 46, 54, 69, 77, 91} + _CommandType_index_2 = [...]uint8{0, 13, 26, 35, 45, 56, 68, 79, 94, 106, 120, 131, 142, 154, 164, 178, 191, 208, 218} + _CommandType_index_3 = [...]uint8{0, 10, 24, 37, 50, 67, 79} + _CommandType_index_4 = [...]uint8{0, 11, 24, 31, 42, 55, 59, 68} + _CommandType_index_5 = [...]uint8{0, 11, 24, 37} + _CommandType_index_6 = [...]uint8{0, 10, 25, 39} + _CommandType_index_8 = [...]uint8{0, 3, 6} +) + +func (i CommandType) String() string { + switch { + case 16 <= i && i <= 18: + i -= 16 + return _CommandType_name_0[_CommandType_index_0[i]:_CommandType_index_0[i+1]] + case 20 <= i && i <= 27: + i -= 20 + return _CommandType_name_1[_CommandType_index_1[i]:_CommandType_index_1[i+1]] + case 31 <= i && i <= 48: + i -= 31 + return _CommandType_name_2[_CommandType_index_2[i]:_CommandType_index_2[i+1]] + case 50 <= i && i <= 55: + i -= 50 + return _CommandType_name_3[_CommandType_index_3[i]:_CommandType_index_3[i+1]] + case 57 <= i && i <= 63: + i -= 57 + return _CommandType_name_4[_CommandType_index_4[i]:_CommandType_index_4[i+1]] + case 68 <= i && i <= 70: + i -= 68 + return _CommandType_name_5[_CommandType_index_5[i]:_CommandType_index_5[i+1]] + case 80 <= i && i <= 82: + i -= 80 + return _CommandType_name_6[_CommandType_index_6[i]:_CommandType_index_6[i+1]] + case i == 224: + return _CommandType_name_7 + case 238 <= i && i <= 239: + i -= 238 + return _CommandType_name_8[_CommandType_index_8[i]:_CommandType_index_8[i+1]] + default: + return "CommandType(" + strconv.FormatInt(int64(i), 10) + ")" + } +} diff --git a/wifinina/connectionstatus_string.go b/wifinina/connectionstatus_string.go new file mode 100644 index 0000000..6e159bc --- /dev/null +++ b/wifinina/connectionstatus_string.go @@ -0,0 +1,39 @@ +// Code generated by "stringer -type=ConnectionStatus -trimprefix=Status"; DO NOT EDIT. + +package wifinina + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[StatusNoShield-255] + _ = x[StatusIdle-0] + _ = x[StatusNoSSIDAvail-1] + _ = x[StatusScanCompleted-2] + _ = x[StatusConnected-3] + _ = x[StatusConnectFailed-4] + _ = x[StatusConnectionLost-5] + _ = x[StatusDisconnected-6] +} + +const ( + _ConnectionStatus_name_0 = "IdleNoSSIDAvailScanCompletedConnectedConnectFailedConnectionLostDisconnected" + _ConnectionStatus_name_1 = "NoShield" +) + +var ( + _ConnectionStatus_index_0 = [...]uint8{0, 4, 15, 28, 37, 50, 64, 76} +) + +func (i ConnectionStatus) String() string { + switch { + case i <= 6: + return _ConnectionStatus_name_0[_ConnectionStatus_index_0[i]:_ConnectionStatus_index_0[i+1]] + case i == 255: + return _ConnectionStatus_name_1 + default: + return "ConnectionStatus(" + strconv.FormatInt(int64(i), 10) + ")" + } +} diff --git a/wifinina/debug.go b/wifinina/debug.go new file mode 100644 index 0000000..e4088c5 --- /dev/null +++ b/wifinina/debug.go @@ -0,0 +1,5 @@ +//go:build wifidebug + +package wifinina + +var _debug = true diff --git a/wifinina/encryptiontype_string.go b/wifinina/encryptiontype_string.go new file mode 100644 index 0000000..9741757 --- /dev/null +++ b/wifinina/encryptiontype_string.go @@ -0,0 +1,42 @@ +// Code generated by "stringer -type=EncryptionType -trimprefix=EncType"; DO NOT EDIT. + +package wifinina + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[EncTypeTKIP-2] + _ = x[EncTypeCCMP-4] + _ = x[EncTypeWEP-5] + _ = x[EncTypeNone-7] + _ = x[EncTypeAuto-8] +} + +const ( + _EncryptionType_name_0 = "TKIP" + _EncryptionType_name_1 = "CCMPWEP" + _EncryptionType_name_2 = "NoneAuto" +) + +var ( + _EncryptionType_index_1 = [...]uint8{0, 4, 7} + _EncryptionType_index_2 = [...]uint8{0, 4, 8} +) + +func (i EncryptionType) String() string { + switch { + case i == 2: + return _EncryptionType_name_0 + case 4 <= i && i <= 5: + i -= 4 + return _EncryptionType_name_1[_EncryptionType_index_1[i]:_EncryptionType_index_1[i+1]] + case 7 <= i && i <= 8: + i -= 7 + return _EncryptionType_name_2[_EncryptionType_index_2[i]:_EncryptionType_index_2[i+1]] + default: + return "EncryptionType(" + strconv.FormatInt(int64(i), 10) + ")" + } +} diff --git a/wifinina/error_string.go b/wifinina/error_string.go new file mode 100644 index 0000000..51a0017 --- /dev/null +++ b/wifinina/error_string.go @@ -0,0 +1,59 @@ +// Code generated by "stringer -type=Error -trimprefix=Err"; DO NOT EDIT. + +package wifinina + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[ErrTimeoutChipReady-1] + _ = x[ErrTimeoutChipSelect-2] + _ = x[ErrCheckStartCmd-3] + _ = x[ErrWaitRsp-4] + _ = x[ErrUnexpectedLength-224] + _ = x[ErrNoParamsReturned-225] + _ = x[ErrIncorrectSentinel-226] + _ = x[ErrCmdErrorReceived-239] + _ = x[ErrNotImplemented-240] + _ = x[ErrUnknownHost-241] + _ = x[ErrSocketAlreadySet-242] + _ = x[ErrConnectionTimeout-243] + _ = x[ErrNoData-244] + _ = x[ErrDataNotWritten-245] + _ = x[ErrCheckDataError-246] + _ = x[ErrBufferTooSmall-247] + _ = x[ErrNoSocketAvail-255] +} + +const ( + _Error_name_0 = "TimeoutChipReadyTimeoutChipSelectCheckStartCmdWaitRsp" + _Error_name_1 = "UnexpectedLengthNoParamsReturnedIncorrectSentinel" + _Error_name_2 = "CmdErrorReceivedNotImplementedUnknownHostSocketAlreadySetConnectionTimeoutNoDataDataNotWrittenCheckDataErrorBufferTooSmall" + _Error_name_3 = "NoSocketAvail" +) + +var ( + _Error_index_0 = [...]uint8{0, 16, 33, 46, 53} + _Error_index_1 = [...]uint8{0, 16, 32, 49} + _Error_index_2 = [...]uint8{0, 16, 30, 41, 57, 74, 80, 94, 108, 122} +) + +func (i Error) String() string { + switch { + case 1 <= i && i <= 4: + i -= 1 + return _Error_name_0[_Error_index_0[i]:_Error_index_0[i+1]] + case 224 <= i && i <= 226: + i -= 224 + return _Error_name_1[_Error_index_1[i]:_Error_index_1[i+1]] + case 239 <= i && i <= 247: + i -= 239 + return _Error_name_2[_Error_index_2[i]:_Error_index_2[i+1]] + case i == 255: + return _Error_name_3 + default: + return "Error(" + strconv.FormatInt(int64(i), 10) + ")" + } +} diff --git a/wifinina/http.go b/wifinina/http.go index e1358a0..4f73d83 100644 --- a/wifinina/http.go +++ b/wifinina/http.go @@ -212,11 +212,11 @@ func (c *client) stop() error { } // Wait max 5 secs for the connection to close - for i := 0; i < 50 && c.status() != TCPStateClosed; i++ { + for i := 0; i < 50 && c.status() != uint8(TCPStateClosed); i++ { time.Sleep(100 * time.Millisecond) } - if c.status() != TCPStateClosed { + if c.status() != uint8(TCPStateClosed) { return fmt.Errorf("stop failed, client status %x", c.status()) } diff --git a/wifinina/nodebug.go b/wifinina/nodebug.go new file mode 100644 index 0000000..78cd243 --- /dev/null +++ b/wifinina/nodebug.go @@ -0,0 +1,5 @@ +//go:build !wifidebug + +package wifinina + +var _debug = false diff --git a/wifinina/status.go b/wifinina/status.go new file mode 100644 index 0000000..fc2e3bf --- /dev/null +++ b/wifinina/status.go @@ -0,0 +1,75 @@ +package wifinina + +type ConnectionStatus uint8 + +//go:generate stringer -type=ConnectionStatus -trimprefix=Status +const ( + StatusNoShield ConnectionStatus = 255 + StatusIdle ConnectionStatus = 0 + StatusNoSSIDAvail ConnectionStatus = 1 + StatusScanCompleted ConnectionStatus = 2 + StatusConnected ConnectionStatus = 3 + StatusConnectFailed ConnectionStatus = 4 + StatusConnectionLost ConnectionStatus = 5 + StatusDisconnected ConnectionStatus = 6 +) + +// Default state value for Wifi state field +// #define NA_STATE -1 + +type EncryptionType uint8 + +//go:generate stringer -type=EncryptionType -trimprefix=EncType +const ( + EncTypeTKIP EncryptionType = 2 + EncTypeCCMP EncryptionType = 4 + EncTypeWEP EncryptionType = 5 + EncTypeNone EncryptionType = 7 + EncTypeAuto EncryptionType = 8 +) + +type TCPState uint8 + +//go:generate stringer -type=TCPState -trimprefix=TCPState +const ( + TCPStateClosed TCPState = 0 + TCPStateListen TCPState = 1 + TCPStateSynSent TCPState = 2 + TCPStateSynRcvd TCPState = 3 + TCPStateEstablished TCPState = 4 + TCPStateFinWait1 TCPState = 5 + TCPStateFinWait2 TCPState = 6 + TCPStateCloseWait TCPState = 7 + TCPStateClosing TCPState = 8 + TCPStateLastACK TCPState = 9 + TCPStateTimeWait TCPState = 10 +) + +type Error uint8 + +func (err Error) Error() string { + return "wifinina error: " + err.String() +} + +//go:generate stringer -type=Error -trimprefix=Err +const ( + 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 +) diff --git a/wifinina/tcp.go b/wifinina/tcp.go index ba5f41a..e27b788 100644 --- a/wifinina/tcp.go +++ b/wifinina/tcp.go @@ -233,9 +233,9 @@ func (d *Device) IsConnected() (bool, error) { if err != nil { return false, err } - isConnected := !(s == TCPStateListen || s == TCPStateClosed || - s == TCPStateFinWait1 || s == TCPStateFinWait2 || s == TCPStateTimeWait || - s == TCPStateSynSent || s == TCPStateSynRcvd || s == TCPStateCloseWait) + isConnected := !(s == uint8(TCPStateListen) || s == uint8(TCPStateClosed) || + s == uint8(TCPStateFinWait1) || s == uint8(TCPStateFinWait2) || s == uint8(TCPStateTimeWait) || + s == uint8(TCPStateSynSent) || s == uint8(TCPStateSynRcvd) || s == uint8(TCPStateCloseWait)) // TODO: investigate if the below is necessary (as per Arduino driver) //if !isConnected { // //close socket buffer? @@ -247,7 +247,7 @@ func (d *Device) IsConnected() (bool, error) { func (d *Device) status() (uint8, error) { if d.sock == NoSocketAvail { - return TCPStateClosed, nil + return uint8(TCPStateClosed), nil } return d.GetClientState(d.sock) } @@ -260,7 +260,7 @@ func (d *Device) stop() error { start := time.Now() for time.Since(start) < 5*time.Second { st, _ := d.status() - if st == TCPStateClosed { + if st == uint8(TCPStateClosed) { break } time.Sleep(1 * time.Millisecond) diff --git a/wifinina/tcpstate_string.go b/wifinina/tcpstate_string.go new file mode 100644 index 0000000..3d5bc1f --- /dev/null +++ b/wifinina/tcpstate_string.go @@ -0,0 +1,33 @@ +// Code generated by "stringer -type=TCPState -trimprefix=TCPState"; DO NOT EDIT. + +package wifinina + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[TCPStateClosed-0] + _ = x[TCPStateListen-1] + _ = x[TCPStateSynSent-2] + _ = x[TCPStateSynRcvd-3] + _ = x[TCPStateEstablished-4] + _ = x[TCPStateFinWait1-5] + _ = x[TCPStateFinWait2-6] + _ = x[TCPStateCloseWait-7] + _ = x[TCPStateClosing-8] + _ = x[TCPStateLastACK-9] + _ = x[TCPStateTimeWait-10] +} + +const _TCPState_name = "ClosedListenSynSentSynRcvdEstablishedFinWait1FinWait2CloseWaitClosingLastACKTimeWait" + +var _TCPState_index = [...]uint8{0, 6, 12, 19, 26, 37, 45, 53, 62, 69, 76, 84} + +func (i TCPState) String() string { + if i >= TCPState(len(_TCPState_index)-1) { + return "TCPState(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _TCPState_name[_TCPState_index[i]:_TCPState_index[i+1]] +} diff --git a/wifinina/wifinina.go b/wifinina/wifinina.go index c493035..e13a4d8 100644 --- a/wifinina/wifinina.go +++ b/wifinina/wifinina.go @@ -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 {