/** * Constructor of DataCode * @param u8 the character to be transformed * @param index the index of the char */ public DataCode(char u8, int index) { if (index > INDEX_MAX) { throw new Exception("index > INDEX_MAX"); } byte[] dataBytes = ByteUtil.splitUint8To2bytes(u8); mDataHigh = dataBytes[0]; mDataLow = dataBytes[1]; CRC8 crc8 = new CRC8(); crc8.update(ByteUtil.convertUint8toByte(u8)); crc8.update(index); byte[] crcBytes = ByteUtil.splitUint8To2bytes((char)crc8.getValue()); mCrcHigh = crcBytes[0]; mCrcLow = crcBytes[1]; mSeqHeader = (byte)index; }
/** * 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); }