Exemple #1
0
        /// <summary>
        /// B、用户导入升级包
        /// </summary>
        /// <returns></returns>
        public static UInt32 UserUpdatePkg()
        {
            //1.枚举锁
            //2.打开锁
            //3.验证用户PIN
            //4.导入升级包
            //5.验证升级包内容(读取验证内容与升级内容一致)
            //6.关闭锁

            UInt32 ret = MowayCS.MW_SUCCESS;
            UInt32 devInfoArraySize = 32;       // 获取设备的最大数量

            MW_DEVICE_INFO_CTX[] devInfoArray = new MW_DEVICE_INFO_CTX[devInfoArraySize];

            UInt32 devCount = 0;
            IntPtr hHandle  = IntPtr.Zero;

            //1.枚举锁
            ret = MowayCS.mw_enum(devInfoArray, devInfoArraySize, ref devCount);
            if (ret != MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_enum failed, {0:X8}", ret);
                return(ret);
            }

            if (devCount == 0)
            {
                Console.WriteLine("Not found device");
                return(ret);
            }

            //2.打开锁
            ret = MowayCS.mw_open(ref devInfoArray[0], MowayCS.MW_OPEN_EXCLUSIVE_MODE, ref hHandle);
            if (ret != MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_open failed, {0:X8}", ret);
                return(ret);
            }

            //3.验证用户PIN
            ret = MowayCS.mw_verify_pin(hHandle, Convert.ToByte(MowayCS.MW_PIN_TYPE_USER), Encoding.ASCII.GetBytes(Program.userPIN));
            if (ret != MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_verify_pin failed, {0:X8}", ret);
                MowayCS.mw_close(hHandle);
                return(ret);
            }

            //4.导入升级包
            // 注意:导入前已经调用 DeveloperMakeUpdatePkg() 生成数据包内容,并且当前加密锁的PID与升级包PID相同。
            ret = MowayCS.mw_update(hHandle, pkgBuffer, pkgBufferLen);
            if (ret == MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_update success.");
            }
            else
            {
                Console.WriteLine("mw_update failed, {0:X8}", ret);
            }


            //5.验证升级包内容(读取验证内容与升级内容一致)
            // 5.1 读取升级后的锁内文件内容
            byte[] readBuffer    = null;
            UInt32 readBufferLen = 0;
            UInt32 readOffset    = 0;

            readBuffer    = new byte[128];
            readBufferLen = (UInt32)readBuffer.Length;
            ret           = MowayCS.mw_read_file(hHandle, updatePkgUpdateFilename, readOffset, readBufferLen, readBuffer);
            if (ret == MowayCS.MW_SUCCESS)
            {
                Console.WriteLine("mw_read_file {0} success, {1}", updatePkgUpdateFilename, Encoding.ASCII.GetString(readBuffer));
            }
            else
            {
                Console.WriteLine("mw_read_file {0} failed, {1:X8}", updatePkgUpdateFilename, ret);
            }

            // 5.2 对比文件内容与升级包写入内容相同
            FileStream   fileStream = new FileStream(updatePkgUpdateLocalFilename, FileMode.Open);
            BinaryReader bReader    = new BinaryReader(fileStream);

            byte[] updateDataBuffer    = bReader.ReadBytes((int)fileStream.Length);
            int    updateDataBufferLen = updateDataBuffer.Length;

            bReader.Close();
            fileStream.Close();

            if (Program.CompareBytes(readBuffer, updateDataBuffer, updateDataBufferLen))
            {
                Console.WriteLine("mw_update success, readBuffer = updateDataBuffer");
            }
            else
            {
                Console.WriteLine("mw_update failed, readBuffer != updateDataBuffer");
            }

            //6.关闭锁
            MowayCS.mw_close(hHandle);

            return(MowayCS.MW_SUCCESS);
        }