示例#1
0
        public SerialNumbers(NetIfManager manager, string masterSN)
        {
            int k;

            Manager = manager;
            if (!String.IsNullOrWhiteSpace(masterSN))
            {
                MasterSNString = masterSN;
                if (!MasterSNString.EndsWith("\0"))
                {
                    MasterSNString += "\0";
                }
            }
            else
            {
                int n = Convert.ToInt32(Manager.ShellId);
                MasterSNString = Settings.SerialNumberFormatter(Lima.GetComputerCode(), (char)(Primitives.NibbleToHexChar((byte)n)), '0');
            }
            MasterSN = Primitives.StringToByteArray(MasterSNString);
            UpdateProductCode(MasterSN);
            SerialNumbersFreeHeap = new TokenAllocator(SerialNumbersSize);
            IoSerialNumbers       = new SerialNumber[SerialNumbersSize];
            for (k = 0; k < SerialNumbersSize; k++)
            {
                IoSerialNumbers[k] = new SerialNumber();
            }
        }
示例#2
0
        // Returns true if device was switched into Android accessory mode.
        // Note: only use u for ControlTransfer since vid/pid may be stale.
        public static bool TryOpeningAccessory(UsbLinkDevice u)
        {
            byte[] ioBuffer = new byte[2];
            int    outLen   = 0;
            bool   response;
            string serialNumber = ACCSerialNumber;

            if (String.IsNullOrWhiteSpace(serialNumber) || (serialNumber.Length < 2))
            {
                serialNumber = Settings.SerialNumberFormatter(Lima.GetComputerCode(), 'B', 'X');
            }

            // response = u.mUsb.ResetDevice();
            // if(!response) { ShowLastError(); return null; }

            // Note: this clears strings on device too.
            response = u.ControlTransfer(UsbRqstInVendor, ACCESSORY_GET_PROTOCOL, 0, 0, ioBuffer, 2, 0, out outLen);
            if (!response)
            {
                return(false);
            }
            TmpDevVersion = ioBuffer[1] << 8 | ioBuffer[0]; //This should be >0 if Accessory mode supported.
            if (TmpDevVersion == 0)
            {
                return(false);
            }

            Thread.Sleep(1000);                                          // Allow space  after In cmd otherwise sometimes hangs on the next transfer.

            ioBuffer = Encoding.UTF8.GetBytes(Settings.AppManufacturer); // Strings must be in UTF8 encoding.
            response = u.ControlTransfer(UsbRqstOutVendor, ACCESSORY_SEND_STRING, 0, ACCESSORY_STRING_MANUFACTURER, ioBuffer, ioBuffer.Length);
            if (!response)
            {
                return(false);
            }
            ioBuffer = Encoding.UTF8.GetBytes(Settings.AppModelName);
            response = u.ControlTransfer(UsbRqstOutVendor, ACCESSORY_SEND_STRING, 0, ACCESSORY_STRING_MODEL, ioBuffer, ioBuffer.Length);
            if (!response)
            {
                return(false);
            }
            ioBuffer = Encoding.UTF8.GetBytes(Settings.AppDescription);
            response = u.ControlTransfer(UsbRqstOutVendor, ACCESSORY_SEND_STRING, 0, ACCESSORY_STRING_DESCRIPTION, ioBuffer, ioBuffer.Length);
            if (!response)
            {
                return(false);
            }
            ioBuffer = Encoding.UTF8.GetBytes(Settings.AppVersion);
            response = u.ControlTransfer(UsbRqstOutVendor, ACCESSORY_SEND_STRING, 0, ACCESSORY_STRING_VERSION, ioBuffer, ioBuffer.Length);
            if (!response)
            {
                return(false);
            }
            ioBuffer = Encoding.UTF8.GetBytes(Settings.AppURI);
            response = u.ControlTransfer(UsbRqstOutVendor, ACCESSORY_SEND_STRING, 0, ACCESSORY_STRING_URI, ioBuffer, ioBuffer.Length);
            if (!response)
            {
                return(false);
            }
            ioBuffer = Encoding.UTF8.GetBytes(serialNumber);
            response = u.ControlTransfer(UsbRqstOutVendor, ACCESSORY_SEND_STRING, 0, ACCESSORY_STRING_SERIAL, ioBuffer, ioBuffer.Length);
            if (!response)
            {
                return(false);
            }

            // Control request for starting device in accessory mode.
            // The host sends this after setting all its strings to the device.
            response = u.ControlTransfer(UsbRqstOutVendor, ACCESSORY_START, 0, 0);
            Thread.Sleep(2000); // Allow time to switch.
            return(response);
        }