Example #1
0
        private async void button1_Click(object sender, EventArgs e)
        {
            pass         = txtPass.Text;
            generator    = new EsptouchGenerator(ssid, bssid, pass, EspNetUtil.getLocalInetAddress(), false);
            mIsInterrupt = false;
            this.Enabled = false;
            Config();
            bool isSucces = await WaitSuccess(mParameter.getEsptouchResultTotalLen());

            if (isSucces)
            {
                MessageBox.Show("SmartConfig connect cuccess to : " + ssid + ", IP = " + mEsptouchResultList[0].InetAddress.ToString());
            }
            this.Enabled = true;
        }
Example #2
0
        /**
         * Constructor of DatumCode
         * @param apSsid the Ap's ssid
         * @param apBssid the Ap's bssid
         * @param apPassword the Ap's password
         * @param ipAddress the ip address of the phone or pad
         * @param isSsidHidden whether the Ap's ssid is hidden
         */
        public DatumCode(string apSsid, string apBssid, string apPassword,
                         IPAddress ipAddress, bool isSsidHiden)
        {
            // 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

            // total xor
            char totalXor = (char)0;

            char apPwdLen = (char)ByteUtil.getBytesBystring(apPassword).Length;
            CRC8 crc      = new CRC8();

            crc.update(ByteUtil.getBytesBystring(apSsid));
            char apSsidCrc = (char)crc.getValue();

            crc.reset();
            crc.update(EspNetUtil.parseBssid2bytes(apBssid));
            char apBssidCrc = (char)crc.getValue();

            char apSsidLen = (char)ByteUtil.getBytesBystring(apSsid).Length;

            // hostname parse
            string[] ipAddrStrs = ipAddress.ToString().Split(new char[] { '.' });
            int      ipLen      = ipAddrStrs.Length;

            char[] ipAddrChars = new char[ipLen];
            // only support ipv4 at the moment
            for (int i = 0; i < ipLen; ++i)
            {
                ipAddrChars[i] = (char)int.Parse(ipAddrStrs[i]);
            }


            char _totalLen = (char)(EXTRA_HEAD_LEN + ipLen + apPwdLen + apSsidLen);
            char totalLen  = isSsidHiden ? (char)(EXTRA_HEAD_LEN + ipLen + apPwdLen + apSsidLen)
                    : (char)(EXTRA_HEAD_LEN + ipLen + apPwdLen);

            // build data codes
            mDataCodes    = new DataCode[totalLen];
            mDataCodes[0] = new DataCode(_totalLen, 0);
            totalXor     ^= _totalLen;
            mDataCodes[1] = new DataCode(apPwdLen, 1);
            totalXor     ^= apPwdLen;
            mDataCodes[2] = new DataCode(apSsidCrc, 2);
            totalXor     ^= apSsidCrc;
            mDataCodes[3] = new DataCode(apBssidCrc, 3);
            totalXor     ^= apBssidCrc;
            mDataCodes[4] = null;
            for (int i = 0; i < ipLen; ++i)
            {
                mDataCodes[i + EXTRA_HEAD_LEN] = new DataCode(ipAddrChars[i], i + EXTRA_HEAD_LEN);
                totalXor ^= ipAddrChars[i];
            }

            byte[] apPwdBytes = ByteUtil.getBytesBystring(apPassword);
            char[] apPwdChars = new char[apPwdBytes.Length];
            for (int i = 0; i < apPwdBytes.Length; i++)
            {
                apPwdChars[i] = ByteUtil.convertByte2Uint8(apPwdBytes[i]);
            }
            for (int i = 0; i < apPwdChars.Length; i++)
            {
                mDataCodes[i + EXTRA_HEAD_LEN + ipLen] = new DataCode(
                    apPwdChars[i], i + EXTRA_HEAD_LEN + ipLen);
                totalXor ^= apPwdChars[i];
            }

            byte[] apSsidBytes = ByteUtil.getBytesBystring(apSsid);
            char[] apSsidChars = new char[apSsidBytes.Length];

            // totalXor will xor apSsidChars no matter whether the ssid is hidden
            for (int i = 0; i < apSsidBytes.Length; i++)
            {
                apSsidChars[i] = ByteUtil.convertByte2Uint8(apSsidBytes[i]);
                totalXor      ^= apSsidChars[i];
            }

            if (isSsidHiden)
            {
                for (int i = 0; i < apSsidChars.Length; i++)
                {
                    mDataCodes[i + EXTRA_HEAD_LEN + ipLen + apPwdLen] = new DataCode(
                        apSsidChars[i], i + EXTRA_HEAD_LEN + ipLen + apPwdLen);
                }
            }

            // set total xor last
            mDataCodes[4] = new DataCode(totalXor, 4);
        }
Example #3
0
        private async Task <bool> WaitSuccess(int expectDataLen)
        {
            long startTimestamp = DateTime.Now.Ticks / 10000;

            byte[] apSsidAndPassword = ByteUtil.getBytesBystring(ssid + pass);
            byte   expectOneByte     = (byte)(apSsidAndPassword.Length + 9);

            Console.WriteLine("expectOneByte: " + (0 + expectOneByte));
            int receiveOneByte = -1;

            byte[]      receiveBytes = null;
            Task <bool> t            = new Task <bool>(() =>
            {
                while (mEsptouchResultList.Count < mParameter.getExpectTaskResultCount() && !mIsInterrupt)
                {
                    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                    if (udp.Available > 0)
                    {
                        receiveBytes = udp.Receive(ref sender).Take(expectDataLen).ToArray();
                        if (receiveBytes != null)
                        {
                            receiveOneByte = receiveBytes[0];
                        }
                        else
                        {
                            receiveOneByte = -1;
                        }
                        if (receiveOneByte == expectOneByte)
                        {
                            Console.WriteLine("receive correct broadcast");
                            // change the socket's timeout
                            long consume = (DateTime.Now.Ticks / 10000) - startTimestamp;
                            int timeout  = (int)(mParameter.getWaitUdpTotalMillisecond() - consume);
                            if (timeout < 0)
                            {
                                Console.WriteLine("esptouch timeout");
                                break;
                            }
                            else
                            {
                                Console.WriteLine("mSocketServer's new timeout is " + timeout + " milliseconds");
                                udp.Client.SendTimeout = timeout; //<<<<<<<<<<<
                                Console.WriteLine("receive correct broadcast");

                                if (receiveBytes != null)
                                {
                                    string bssid = ByteUtil.parseBssid(
                                        receiveBytes,
                                        mParameter.getEsptouchResultOneLen(),
                                        mParameter.getEsptouchResultMacLen());
                                    IPAddress inetAddress = EspNetUtil
                                                            .parseInetAddr(
                                        receiveBytes,
                                        mParameter.getEsptouchResultOneLen()
                                        + mParameter.getEsptouchResultMacLen(),
                                        mParameter.getEsptouchResultIpLen());
                                    mEsptouchResultList.Add(new EsptouchResult()
                                    {
                                        Suc = true, Bssid = bssid, InetAddress = inetAddress
                                    });
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("receive rubbish message, just ignore");
                        }
                    }
                }
                mIsInterrupt = true;
                Console.WriteLine("__listenAsyn() finish");
                return(mEsptouchResultList.Count >= mParameter.getExpectTaskResultCount());
            });

            t.Start();
            return(await t);
        }