public void TestRealWorldCase() { var args = new SmartConfigArguments { Ssid = "MMKD", Bssid = new PhysicalAddress(new byte[] { 0xc4, 0x12, 0xf5, 0xc4, 0x92, 0x69 }), Password = "******", LocalAddress = IPAddress.Parse("192.168.1.6") }; var ctx = this.CreateContext(); var encoder = new EspProcedureEncoder(); var segments = encoder.Encode(ctx, args); var allFrames = segments.SelectMany(x => x.Frames).ToArray(); var expectedFrames = new ushort[] { 515, 514, 513, 512, 217, 296, 187, 280, 297, 110, 132, 298, 251, 234, 299, 114, 177, 300, 67, 292, 315, 44, 180, 301, 216, 114, 302, 128, 104, 303, 153, 169, 316, 58, 136, 304, 174, 155, 305, 121, 235, 306, 74, 247, 317, 109, 123, 307, 171, 219, 308, 124, 75, 309, 285, 244, 318, 284, 187, 310, 174, 124, 311, 101, 172, 312, 293, 113, 319, 234, 156, 313, 227, 172, 314, 60, 222, 320, 161 }; Assert.AreEqual(expectedFrames.Length, allFrames.Count()); for (int i = 0; i < expectedFrames.Length; i++) { Assert.AreEqual(expectedFrames[i], allFrames[i], "Failed to compare frames: index={0}, expected={1}, actual={2}", i, expectedFrames[i], allFrames[i]); } }
public void TestEmptyPasswordCase() { var args = new SmartConfigArguments { Ssid = "MMKD", Bssid = new PhysicalAddress(new byte[] { 0xc4, 0x12, 0xf5, 0xc4, 0x92, 0x69 }), Password = null, LocalAddress = IPAddress.Parse("192.168.1.6") }; var ctx = this.CreateContext(); var encoder = new EspProcedureEncoder(); var segments = encoder.Encode(ctx, args); var allFrames = segments.SelectMany(x => x.Frames).ToArray(); var expectedFrames = new ushort[] { 515, 514, 513, 512, //guide 168, 296, 197, 120, 297, 264, 132, 298, 251, 234, 299, 114, 160, 300, 124, // datum 164, 309, 76, 180, 301, 216, 114, 302, 128, 104, 303, 153, 137, 310, 74, 136, 304, 174, 172, 305, 277, 140, 306, 245, 87, 311, 157, 188, 307, 179, 44, 308, 92, 68, 312, 76, 193, 313, 58, 238, 314, 193 }; Assert.AreEqual(expectedFrames.Length, allFrames.Count()); for (int i = 0; i < expectedFrames.Length; i++) { Assert.AreEqual(expectedFrames[i], allFrames[i], "Failed to compare frames: index={0}, expected={1}, actual={2}", i, expectedFrames[i], allFrames[i]); } }
public void TestEmptyPasswordRealWorldCase() { var args = new SmartConfigArguments { Ssid = "MMKD", Bssid = new PhysicalAddress(new byte[] { 0xc4, 0x12, 0xf5, 0xc4, 0x92, 0x69 }), }; var context = this.CreateContext(); context.SetOption(AirkissOptionNames.RandomNumber, (byte)67); var encoder = new AirkissProcedureEncoder(); var segments = encoder.Encode(context, args); var frames = segments.SelectMany(x => x.Frames).ToArray(); var expectedBuf = new ushort[] { 1, 2, 3, 4, // Guide Code 0-3 8, 21, 44, 51, // Magic Code 4-7 64, 80, 96, 112, // Prefix Code 8-11 // Data 189, 128, 323, 333, 333, 331, 227, 129, 324 }; //Assert.AreEqual(expectedBuf.Length, frames.Length); for (int i = 0; i < expectedBuf.Length; i++) { Assert.AreEqual(expectedBuf[i], frames[i], "Failed index={0}", i); } }
public IEnumerable <ushort> Encode(SmartConfigContext ctx, SmartConfigArguments args) { // Data = total len(1 byte) + apPwd len(1 byte) + SSID CRC(1 byte) + // BSSID CRC(1 byte) + TOTAL XOR(1 byte)+ ipAddress(4 byte) + apPwd + apSsid apPwdLen <= // 105 at the moment var senderIPAddress = args.LocalAddress.GetAddressBytes(); 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() ?? Constants.EmptyBuffer; var bssidCrc8 = Crc8.ComputeOnceOnly(bssid); var totalLength = (byte)(ExtraHeaderLength + senderIPAddress.Length + passwordBytes.Length + ssid.Length); byte totalXor = ComputeTotalXor(senderIPAddress, passwordBytes, ssid, ssidCrc8, bssidCrc8, totalLength); _framesBuilder.Clear(); this.DoEncode(totalLength, (byte)passwordBytes.Length, ssidCrc8, bssidCrc8, totalXor, senderIPAddress, passwordBytes, ssid, bssid); return(_framesBuilder); }
static async Task <int> Main(string[] args) { Console.WriteLine("******** ESPTouch SmartConfig Demo/Utility ********"); if (args.Length != 3) { ShowUsage(); return(-1); } var wifiInterface = FindFirstWifiInterfaceOrDefault(); if (wifiInterface == null) { Console.WriteLine("Cannot find any available WiFi adapter."); return(-1); } Console.WriteLine("WiFi interface: {0}", wifiInterface.Name); var localAddress = GetIPv4AddressOrDefault(wifiInterface); if (localAddress == null) { Console.WriteLine("Cannot find IPv4 address for WiFi interface: {0}", wifiInterface.Name); return(-1); } Console.WriteLine("Local address: {0}", localAddress); var provider = new EspSmartConfigProvider(); var ctx = provider.CreateContext(); ctx.DeviceDiscoveredEvent += (s, e) => { Console.WriteLine("Found device: IP={0} MAC={1}", e.Device.IPAddress, e.Device.MacAddress); }; var scArgs = new SmartConfigArguments() { Ssid = args[0], Bssid = PhysicalAddress.Parse(args[1].ToUpperInvariant().Replace(':', '-')), Password = args[2], LocalAddress = localAddress }; // Do the SmartConfig job using (var job = new SmartConfigJob(TimeSpan.FromSeconds(20))) // Set the timeout to 20 seconds { job.Elapsed += Job_Elapsed; await job.ExecuteAsync(ctx, scArgs); } Console.WriteLine("SmartConfig finished."); return(0); }
public IEnumerable <Segment> Encode(SmartConfigContext context, SmartConfigArguments args) { 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[] { new Segment(EspWellKnownConstants.GuideCodes, frameInterval, guideTimeout), new Segment(datumEncoder.Encode(context, args), frameInterval, datumTimeout) }; return(segFrames); }
public IEnumerable <Segment> Encode(SmartConfigContext context, SmartConfigArguments args) { 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 = args.Password != null?Encoding.UTF8.GetBytes(args.Password) : Constants.EmptyBuffer; // Guide Segment 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>(AirkissOptionNames.MagicCodeTimeout); builder.Add(new Segment(magicCodeFrames, frameInterval, magicCodeTimeout)); // Prefix Code Segment var prefixCodeFrames = AirkissPrefixCodeFrameEncoder.Encode(password.Length); var prefixCodeTimeout = context.GetOption <TimeSpan>(AirkissOptionNames.PrefixCodeTimeout); builder.Add(new Segment(prefixCodeFrames, frameInterval, prefixCodeTimeout)); // Data(password/random/ssid) Segment 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; foreach (var bytes in buf.Partition(4)) { var seqEntryFrames = AirkissSeqEntryFrameEncoder.Encode(index, bytes); dataFrames.AddRange(seqEntryFrames); index++; } builder.Add(new Segment(dataFrames, frameInterval, TimeSpan.FromSeconds(4))); return(builder); }
public async Task BroadcastAsync(SmartConfigContext context, SmartConfigArguments args, CancellationToken cancelToken) { if (_isStarted) { throw new InvalidOperationException("Already started"); } try { _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)); await this.BroadcastProcedureAsync(context, segments, broadcastBuffer, cancelToken); } finally { _isStarted = false; } }
public void TestRealWorldCase() { var args = new SmartConfigArguments { Ssid = "MMKD", Bssid = new PhysicalAddress(new byte[] { 0xc4, 0x12, 0xf5, 0xc4, 0x92, 0x69 }), Password = "******", }; var context = this.CreateContext(); context.SetOption(AirkissOptionNames.RandomNumber, (byte)186); var encoder = new AirkissProcedureEncoder(); var segments = encoder.Encode(context, args); var frames = segments.SelectMany(x => x.Frames).ToArray(); var expectedBuf = new ushort[] { 1, 2, 3, 4, // Guide Code 0-3 1, 16, 44, 51, // Magic Code 4-7 64, 91, 98, 112, // Prefix Code 8-11 // Data 144, 128, 364, 361, 376, 361, // seq0 145, 129, 353, 367, 376, 361, // seq1 160, 130, 367, 366, 359, 442, // seq2 141, 131, 333, 333, 331, 324 // seq3 }; Assert.AreEqual(expectedBuf.Length, frames.Length); for (int i = 0; i < expectedBuf.Length; i++) { Assert.AreEqual(expectedBuf[i], frames[i], "Failed index={0}", i); } }