Skip to content

Commit

Permalink
Merge pull request #2 from oldrev/dev
Browse files Browse the repository at this point in the history
time to 1.0
  • Loading branch information
oldrev authored Apr 9, 2020
2 parents b16f493 + c9b775a commit 657ffa4
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Sandwych.SmartConfig.Airkiss
{
public static class AirkissProperties
public static class AirkissOptionNames
{
public const string RandomNumber = "airkiss.random";
public const string PrefixCodeTimeout = "airkiss.prefix_timeout";
Expand Down
16 changes: 8 additions & 8 deletions src/Sandwych.SmartConfig/Airkiss/AirkissSmartConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public class AirkissSmartConfigProvider : AbstractSmartConfigProvider

public override IEnumerable<(string key, object value)> GetDefaultOptions()
{
yield return (StandardProperties.BroadcastingTargetPort, 10001); // The port to broadcast doesn't matter
yield return (StandardProperties.ListeningPort, 10000);
yield return (StandardProperties.FrameInterval, TimeSpan.FromMilliseconds(0));
yield return (StandardProperties.SegmentInterval, TimeSpan.FromMilliseconds(5));
yield return (StandardProperties.GuideCodeTimeout, TimeSpan.FromSeconds(2));
yield return (StandardOptionNames.BroadcastingTargetPort, 10001); // The port to broadcast doesn't matter
yield return (StandardOptionNames.ListeningPort, 10000);
yield return (StandardOptionNames.FrameInterval, TimeSpan.FromMilliseconds(0));
yield return (StandardOptionNames.SegmentInterval, TimeSpan.FromMilliseconds(5));
yield return (StandardOptionNames.GuideCodeTimeout, TimeSpan.FromSeconds(2));

yield return (AirkissProperties.MagicCodeTimeout, TimeSpan.FromMilliseconds(500));
yield return (AirkissProperties.PrefixCodeTimeout, TimeSpan.FromMilliseconds(500));
yield return (AirkissOptionNames.MagicCodeTimeout, TimeSpan.FromMilliseconds(500));
yield return (AirkissOptionNames.PrefixCodeTimeout, TimeSpan.FromMilliseconds(500));

var randomValue = (byte)(Environment.TickCount % 256);
yield return (AirkissProperties.RandomNumber, randomValue);
yield return (AirkissOptionNames.RandomNumber, randomValue);
}

public override IProcedureEncoder CreateProcedureEncoder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PhysicalAddress ParseMacAddress(byte[] packet)

public bool Validate(SmartConfigContext context, byte[] packet)
{
var randomValue = context.GetOption<byte>(AirkissProperties.RandomNumber);
var randomValue = context.GetOption<byte>(AirkissOptionNames.RandomNumber);
return (packet.Length == AirkissWellknownConstants.DevicePacketLength && packet[0] == randomValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static IEnumerable<ushort> Encode(int totalLength, byte ssidCrc)
{
ushort[] frames = new ushort[4];
var blen = (byte)totalLength;
frames[0] = BytesHelper.CombineUshort(0x00, blen.Bisect().high);
var firstFrame = BytesHelper.CombineUshort(0x00, blen.Bisect().high);
frames[0] = firstFrame != 0 ? firstFrame : (ushort)0x08;
frames[1] = BytesHelper.CombineUshort(0x01, blen.Bisect().low);
frames[2] = BytesHelper.CombineUshort(0x02, ssidCrc.Bisect().high);
frames[3] = BytesHelper.CombineUshort(0x03, ssidCrc.Bisect().low);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,30 @@ public struct AirkissProcedureEncoder : IProcedureEncoder
{
public IEnumerable<Segment> Encode(SmartConfigContext context, SmartConfigArguments args)
{
var frameInterval = context.GetOption<TimeSpan>(StandardProperties.FrameInterval);
var frameInterval = context.GetOption<TimeSpan>(StandardOptionNames.FrameInterval);

var builder = new List<Segment>(128);

var ssid = Encoding.UTF8.GetBytes(args.Ssid);
var ssidCrc8 = Crc8.ComputeOnceOnly(ssid);
var password = Encoding.UTF8.GetBytes(args.Password);

var password = args.Password != null ? Encoding.UTF8.GetBytes(args.Password) : Constants.EmptyBuffer;

// Guide Segment
var guidePeriod = context.GetOption<TimeSpan>(StandardProperties.GuideCodeTimeout);
var guidePeriod = context.GetOption<TimeSpan>(StandardOptionNames.GuideCodeTimeout);
builder.Add(new Segment(AirkissWellknownConstants.GuideCodes, frameInterval, guidePeriod));

// Magic Code Segment
var magicCodeFrames = AirkissMagicCodeFrameEncoder.Encode(password.Length + ssid.Length + 1, ssidCrc8);
var magicCodeTimeout = context.GetOption<TimeSpan>(AirkissProperties.MagicCodeTimeout);
var magicCodeTimeout = context.GetOption<TimeSpan>(AirkissOptionNames.MagicCodeTimeout);
builder.Add(new Segment(magicCodeFrames, frameInterval, magicCodeTimeout));

// Prefix Code Segment
var prefixCodeFrames = AirkissPrefixCodeFrameEncoder.Encode(password.Length);
var prefixCodeTimeout = context.GetOption<TimeSpan>(AirkissProperties.PrefixCodeTimeout);
var prefixCodeTimeout = context.GetOption<TimeSpan>(AirkissOptionNames.PrefixCodeTimeout);
builder.Add(new Segment(prefixCodeFrames, frameInterval, prefixCodeTimeout));

// Data(password/random/ssid) Segment
var randValue = context.GetOption<byte>(AirkissProperties.RandomNumber);
var randValue = context.GetOption<byte>(AirkissOptionNames.RandomNumber);
var buf = password.Append<byte>(randValue).Concat(ssid).ToList();
var dataFrames = new List<ushort>(buf.Count * 2);
var index = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/Sandwych.SmartConfig/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Sandwych.SmartConfig
{
internal class Constants
{
public static readonly byte[] EmptyBuffer = new byte[] { };
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Sandwych.SmartConfig.Esptouch
{
public static class EspProperties
public static class EspOptionNames
{
public const string DatumPeriodTimeout = "esp.datum_period";
}
Expand Down
12 changes: 6 additions & 6 deletions src/Sandwych.SmartConfig/Esptouch/EspSmartConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public override IDevicePacketInterpreter CreateDevicePacketInterpreter()

public override IEnumerable<(string key, object value)> GetDefaultOptions()
{
yield return (StandardProperties.BroadcastingTargetPort, 7001);
yield return (StandardProperties.ListeningPort, 18266);
yield return (StandardProperties.FrameInterval, TimeSpan.FromMilliseconds(8));
yield return (StandardProperties.SegmentInterval, TimeSpan.FromMilliseconds(8));
yield return (StandardProperties.GuideCodeTimeout, TimeSpan.FromSeconds(2));
yield return (EspProperties.DatumPeriodTimeout, TimeSpan.FromSeconds(4));
yield return (StandardOptionNames.BroadcastingTargetPort, 7001);
yield return (StandardOptionNames.ListeningPort, 18266);
yield return (StandardOptionNames.FrameInterval, TimeSpan.FromMilliseconds(8));
yield return (StandardOptionNames.SegmentInterval, TimeSpan.FromMilliseconds(8));
yield return (StandardOptionNames.GuideCodeTimeout, TimeSpan.FromSeconds(2));
yield return (EspOptionNames.DatumPeriodTimeout, TimeSpan.FromSeconds(4));
}

public override IProcedureEncoder CreateProcedureEncoder()
Expand Down
45 changes: 21 additions & 24 deletions src/Sandwych.SmartConfig/Esptouch/Protocol/EspDatumFrameEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Sandwych.SmartConfig.Esptouch.Protocol
public sealed class EspDatumFrameEncoder
{
public const int ExtraHeaderLength = 5;
public const int ExtraLength = 40;
public const ushort ExtraLength = 40;

private List<ushort> _framesBuilder = new List<ushort>(128);

Expand All @@ -20,15 +20,14 @@ public IEnumerable<ushort> Encode(SmartConfigContext ctx, SmartConfigArguments a
// BSSID CRC(1 byte) + TOTAL XOR(1 byte)+ ipAddress(4 byte) + apPwd + apSsid apPwdLen <=
// 105 at the moment

//this.IsHiddenSsid = ctx.GetProperty<bool>(StandardProperties.IsHiddenSsid);
var senderIPAddress = args.LocalAddress.GetAddressBytes();

var passwordBytes = args.Password != null ? Encoding.ASCII.GetBytes(args.Password) : new byte[] { };
var passwordBytes = args.Password != null ? Encoding.ASCII.GetBytes(args.Password) : Constants.EmptyBuffer;

var ssid = Encoding.UTF8.GetBytes(args.Ssid);
var ssidCrc8 = Crc8.ComputeOnceOnly(ssid);

var bssid = args.Bssid.GetAddressBytes();
var bssid = args.Bssid?.GetAddressBytes() ?? Constants.EmptyBuffer;
var bssidCrc8 = Crc8.ComputeOnceOnly(bssid);

var totalLength = (byte)(ExtraHeaderLength + senderIPAddress.Length + passwordBytes.Length + ssid.Length);
Expand All @@ -38,7 +37,7 @@ public IEnumerable<ushort> Encode(SmartConfigContext ctx, SmartConfigArguments a
_framesBuilder.Clear();

this.DoEncode(totalLength, (byte)passwordBytes.Length,
ssidCrc8, bssidCrc8, totalXor, senderIPAddress, passwordBytes, ssid);
ssidCrc8, bssidCrc8, totalXor, senderIPAddress, passwordBytes, ssid, bssid);
return _framesBuilder;
}

Expand Down Expand Up @@ -66,7 +65,7 @@ private static byte ComputeTotalXor(
return totalXor;
}

public static (ushort, ushort, ushort) ByteToFrames(int index, byte b)
public static IEnumerable<ushort> ByteToFrames(int index, byte b)
{
if (index > byte.MaxValue || index < 0)
{
Expand All @@ -76,10 +75,13 @@ public static (ushort, ushort, ushort) ByteToFrames(int index, byte b)
var crc = new Crc8();
crc.Update(b);
crc.Update((byte)index);
ushort first = (ushort)((crc.Value & 0xF0) | ((b >> 4) & 0x0F));
ushort second = (ushort)(0x100 | index);
ushort third = (ushort)(((crc.Value << 4) & 0xF0) | (b & 0x0F));
return new ValueTuple<ushort, ushort, ushort>(first, second, third);
var first = (crc.Value & 0xF0) | ((b >> 4) & 0x0F);
var seq = (0x100 | index);
var last = ((crc.Value << 4) & 0xF0) | (b & 0x0F);

yield return (ushort)(first + ExtraLength);
yield return (ushort)(seq + ExtraLength);
yield return (ushort)(last + ExtraLength);
}

private void AppendByte(byte b)
Expand All @@ -90,9 +92,7 @@ private void AppendByte(byte b)
private void AppendByte(int frameIndex, byte b)
{
var fs = ByteToFrames(frameIndex, b);
_framesBuilder.Add((ushort)(fs.Item1 + ExtraLength));
_framesBuilder.Add((ushort)(fs.Item2 + ExtraLength));
_framesBuilder.Add((ushort)(fs.Item3 + ExtraLength));
_framesBuilder.AddRange(fs);
}

private void AppendBytes(IEnumerable<byte> bytes)
Expand All @@ -111,8 +111,8 @@ private void DoEncode(
byte totalXor,
byte[] senderIpAddress,
byte[] stationPassword,
byte[] ssid
)
byte[] ssid,
byte[] bssid)
{
this.AppendByte(totalLength);
this.AppendByte(passwordLength);
Expand All @@ -124,23 +124,20 @@ byte[] ssid
this.AppendBytes(ssid);

var bssidPos = ExtraHeaderLength;
for (int i = 0; i < ssid.Length; i++)
for (int i = 0; i < bssid.Length; i++)
{
int frameIndex = totalLength + i;
var byteValue = ssid[i];
if (bssidPos >= _framesBuilder.Count / 3)
var byteValue = bssid[i];
var codeCount = _framesBuilder.Count / 3;
if (bssidPos >= codeCount)
{
this.AppendByte(byteValue);
}
else
{
var fs = ByteToFrames(frameIndex, byteValue);
_framesBuilder.InsertRange(bssidPos * 3, new ushort[]
{
(ushort)(fs.Item1 + ExtraLength),
(ushort)(fs.Item2 + ExtraLength),
(ushort)(fs.Item3 + ExtraLength),
});
var framesInsertPos = bssidPos * 3;
_framesBuilder.InsertRange(framesInsertPos, fs);
}
bssidPos += 4;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public sealed class EspProcedureEncoder : IProcedureEncoder
{
public IEnumerable<Segment> Encode(SmartConfigContext context, SmartConfigArguments args)
{
var guideTimeout = context.GetOption<TimeSpan>(StandardProperties.GuideCodeTimeout);
var datumTimeout = context.GetOption<TimeSpan>(EspProperties.DatumPeriodTimeout);
var frameInterval = context.GetOption<TimeSpan>(StandardProperties.FrameInterval);
var guideTimeout = context.GetOption<TimeSpan>(StandardOptionNames.GuideCodeTimeout);
var datumTimeout = context.GetOption<TimeSpan>(EspOptionNames.DatumPeriodTimeout);
var frameInterval = context.GetOption<TimeSpan>(StandardOptionNames.FrameInterval);

var datumEncoder = new EspDatumFrameEncoder();
var segFrames = new Segment[]
Expand Down
8 changes: 4 additions & 4 deletions src/Sandwych.SmartConfig/Networking/DatagramBroadcaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task BroadcastAsync(SmartConfigContext context, SmartConfigArgument

try
{
_broadcastTarget = new IPEndPoint(IPAddress.Broadcast, context.GetOption<int>(StandardProperties.BroadcastingTargetPort));
_broadcastTarget = new IPEndPoint(IPAddress.Broadcast, context.GetOption<int>(StandardOptionNames.BroadcastingTargetPort));
var encoder = context.Provider.CreateProcedureEncoder();
var segments = encoder.Encode(context, args);
var broadcastBuffer = this.CreateBroadcastBuffer(segments.SelectMany(x => x.Frames));
Expand All @@ -53,7 +53,7 @@ private async Task BroadcastProcedureAsync(
byte[] broadcastBuffer,
CancellationToken userCancelToken)
{
var segmentInterval = context.GetOption<TimeSpan>(StandardProperties.SegmentInterval);
var segmentInterval = context.GetOption<TimeSpan>(StandardOptionNames.SegmentInterval);
while (true)
{
userCancelToken.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -95,7 +95,7 @@ private async Task BroadcastProcedureAsync(
private async Task BroadcastSegmentForeverAsync(
SmartConfigContext context, Segment segment, byte[] broadcastBuffer, CancellationToken token)
{
var segmentInterval = context.GetOption<TimeSpan>(StandardProperties.SegmentInterval);
var segmentInterval = context.GetOption<TimeSpan>(StandardOptionNames.SegmentInterval);
while (true)
{
await this.BroadcastSingleSegmentAsync(segment, broadcastBuffer, segmentInterval, token);
Expand All @@ -105,7 +105,7 @@ private async Task BroadcastSegmentForeverAsync(
private async Task BroadcastSegmentByTimesAsync(
SmartConfigContext context, Segment segment, byte[] broadcastBuffer, CancellationToken token)
{
var segmentInterval = context.GetOption<TimeSpan>(StandardProperties.FrameInterval);
var segmentInterval = context.GetOption<TimeSpan>(StandardOptionNames.FrameInterval);
for (int i = 0; i < segment.BroadcastingMaxTimes; i++)
{
await this.BroadcastSingleSegmentAsync(segment, broadcastBuffer, segmentInterval, token);
Expand Down
2 changes: 1 addition & 1 deletion src/Sandwych.SmartConfig/Networking/DatagramReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task ListenAsync(

private void SetupSocket(SmartConfigContext context)
{
var listeningPort = context.GetOption<int>(StandardProperties.ListeningPort);
var listeningPort = context.GetOption<int>(StandardOptionNames.ListeningPort);
_listeningSocket.Bind(new IPEndPoint(IPAddress.Any, listeningPort));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sandwych.SmartConfig/SmartConfigArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Sandwych.SmartConfig
{
public class SmartConfigArguments
{
public string Password { get; set; }
public string Password { get; set; } = string.Empty;
public string Ssid { get; set; }
public PhysicalAddress Bssid { get; set; }
public IPAddress LocalAddress { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Sandwych.SmartConfig
{
public static class StandardProperties
public static class StandardOptionNames
{
public const string BroadcastingTargetPort = "std.btp";
public const string ListeningPort = "std.lp";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void CanParseMacAddress()
var packet = new byte[] { 0x53, 0xc8, 0x2b, 0x96, 0xa1, 0x57, 0x70 };
var provider = new AirkissSmartConfigProvider();
var ctx = provider.CreateContext();
ctx.SetOption<byte>(AirkissProperties.RandomNumber, 0x53);
ctx.SetOption<byte>(AirkissOptionNames.RandomNumber, 0x53);
Assert.True(interpreter.Validate(ctx, packet));
var mac = interpreter.ParseMacAddress(packet);
var expectedMac = new PhysicalAddress(new byte[] { 0xc8, 0x2b, 0x96, 0xa1, 0x57, 0x70 });
Expand Down
Loading

0 comments on commit 657ffa4

Please sign in to comment.