Implementation of the functions mentioned before:
public int OpenServerSocket(int port, SPWF04SxConnectionType connectionType, SPWF04SxConnectionSecurityType connectionSecurity)
{
var cmd = this.GetCommand()
.AddParameter(port.ToString())
.AddParameter(connectionType == SPWF04SxConnectionType.Tcp ? (connectionSecurity == SPWF04SxConnectionSecurityType.Tls ? “s1” : “t”) : “u”)
.Finalize(SPWF04SxCommandIds.SOCKDON);
this.EnqueueCommand(cmd);
var a = cmd.ReadString();
var b = cmd.ReadString();
if (connectionSecurity == SPWF04SxConnectionSecurityType.Tls && b.IndexOf("Loading:") == 0)
{
a = cmd.ReadString();
b = cmd.ReadString();
}
this.FinishCommand(cmd);
return a.Split(':') is var result && result[0] == "On" ? int.Parse(result[1]) : throw new Exception("Request failed");
}
public int ReadServerSocket(int SockIdServer, int SockIdClient, byte[] buffer, int buflen = 0)
{
var cmd = this.GetCommand()
.AddParameter(SockIdServer.ToString())
.AddParameter(SockIdClient.ToString())
.AddParameter(buflen.ToString())
.Finalize(SPWF04SxCommandIds.SOCKDR, buffer, 0, buflen);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
if (buflen == 0)
buflen = buffer.Length;
var current = 0;
var total = 0;
do
{
current = cmd.ReadBuffer(buffer, total, buflen);
total += current;
} while (current != 0);
this.FinishCommand(cmd);
return total;
}
public void WriteServerSocket(int SockIdServer, int SockIdClient, byte[] buffer, int buflen)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
if (buflen < 0) throw new ArgumentOutOfRangeException();
if (buflen > buffer.Length) throw new ArgumentOutOfRangeException();
var cmd = this.GetCommand()
.AddParameter(SockIdServer.ToString())
.AddParameter(SockIdClient.ToString())
.AddParameter(buflen.ToString())
.Finalize(SPWF04SxCommandIds.SOCKDW, buffer, 0, buflen);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
}
public void CloseServerSocket(int SockIdServer, int SockIdClient = -1)
{
SPWF04SxCommand cmd;
if (SockIdClient >= 0)
cmd = this.GetCommand()
.AddParameter(SockIdServer.ToString())
.AddParameter(SockIdClient.ToString())
.Finalize(SPWF04SxCommandIds.SOCKDC);
else
cmd = this.GetCommand()
.AddParameter(SockIdServer.ToString())
.Finalize(SPWF04SxCommandIds.SOCKDC);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
}
public string GetStatus(string sts_var)
{
SPWF04SxCommand cmd;
cmd = this.GetCommand()
.AddParameter(sts_var)
.Finalize(SPWF04SxCommandIds.STS);
this.EnqueueCommand(cmd);
var a = cmd.ReadString();
var b = cmd.ReadString();
this.FinishCommand(cmd);
if (a.IndexOf(':') >= 0)
return a.Split(':')[1];
else
return "";
}
public string GetConfigValue(string cfg_var)
{
SPWF04SxCommand cmd;
cmd = this.GetCommand()
.AddParameter(cfg_var)
.Finalize(SPWF04SxCommandIds.GCFG);
this.EnqueueCommand(cmd);
var a = cmd.ReadString();
var b = cmd.ReadString();
this.FinishCommand(cmd);
if (a.IndexOf(':') >= 0)
return a.Split(':')[1];
else
return "";
}
public void SetConfigValue(string cfg_var, string newval)
{
SPWF04SxCommand cmd;
cmd = this.GetCommand()
.AddParameter(cfg_var)
.AddParameter(newval)
.Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
cmd = this.GetCommand()
.Finalize(SPWF04SxCommandIds.WCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
}
public string GetSSID()
{
var cmd = this.GetCommand()
.Finalize(SPWF04SxCommandIds.SSIDTXT);
this.EnqueueCommand(cmd);
var a = cmd.ReadString();
var b = cmd.ReadString();
this.FinishCommand(cmd);
if (a.IndexOf(':') >= 0)
return a.Split(':')[1];
else
return "";
}
public void SetMiniAP(string ssid, string password, string ipaddress = null)
{
this.DisableRadio();
var cmd = this.GetCommand()
.AddParameter(ssid)
.Finalize(SPWF04SxCommandIds.SSIDTXT);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
cmd = this.GetCommand()
.AddParameter("wifi_priv_mode")
.AddParameter("2")
.Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
cmd = this.GetCommand()
.AddParameter("wifi_auth_type")
.AddParameter("0")
.Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
cmd = this.GetCommand()
.AddParameter("wifi_mode")
.AddParameter("3")
.Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
cmd = this.GetCommand()
.AddParameter("wifi_wpa_psk_text")
.AddParameter(password)
.Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
this.EnableRadio();
if (ipaddress != null && ipaddress.Length > 0)
{
this.DisableRadio(); //reset??
cmd = this.GetCommand()
.AddParameter("ip_ipaddr")
.AddParameter(ipaddress)
.Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
this.EnableRadio();
}
cmd = this.GetCommand()
.Finalize(SPWF04SxCommandIds.WCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);
}