private void LoadDeviceData(MTPDataResponse res) { ErrorCodes.GetException(res.ErrorCode); DeviceInfo.Manufacturer = SelectedDevice.Manufacturer; DeviceInfo.Model = SelectedDevice.Model; int index = 2 + 4 + 2; int vendorDescCount = res.Data[index]; index += vendorDescCount * 2; index += 3; int comandsCount = res.Data[index]; index += 2; // load commands for (int i = 0; i < comandsCount; i++) { index += 2; DeviceInfo.AvaiableCommands.Add(new XmlCommandDescriptor() { Code = BitConverter.ToUInt16(res.Data, index) }); } index += 2; int eventcount = res.Data[index]; index += 2; // load events for (int i = 0; i < eventcount; i++) { index += 2; DeviceInfo.AvaiableEvents.Add(new XmlEventDescriptor() { Code = BitConverter.ToUInt16(res.Data, index) }); } index += 2; int propertycount = res.Data[index]; index += 2; // load properties codes for (int i = 0; i < propertycount; i++) { index += 2; DeviceInfo.AvaiableProperties.Add(new XmlPropertyDescriptor() { Code = BitConverter.ToUInt16(res.Data, index) }); } MTPDataResponse vendor_res = MTPCamera.ExecuteReadDataEx(0x90CA); if (vendor_res.Data.Length > 0) { index = 0; propertycount = vendor_res.Data[index]; index += 2; for (int i = 0; i < propertycount; i++) { index += 2; DeviceInfo.AvaiableProperties.Add(new XmlPropertyDescriptor() { Code = BitConverter.ToUInt16(vendor_res.Data, index) }); } } }
public MTPDataResponse ExecuteReadData(uint code, params uint[] parameters) { lock (_syncRoot) { ReconnectIfNeeded(); DataBlockContainer data; var res = new MTPDataResponse(); _client.Write(new CommandBlockContainer((int) code, parameters)); int len = _client.ReadInt(); Container resp = _client.ReadContainer(); if (resp.Header.Length >= len - 4) { return new MTPDataResponse() {ErrorCode = (uint) resp.Header.Code}; } data = (DataBlockContainer) resp; resp = _client.ReadContainer(); return new MTPDataResponse() {Data = data.Payload, ErrorCode = (uint) data.Header.Code}; } }
/// <summary> /// Transfers the file and writes it to the specified stream. /// </summary> /// <param name="o">The object handle.</param> /// <param name="stream">The stream.</param> public override void TransferFile(object o, Stream stream) { // Sanity checks. if (!stream.CanWrite) throw new ArgumentException("Specified stream is not writable.", "stream"); int retryes = TransferRetries; lock (Locker) { _timer.Stop(); MTPDataResponse result = new MTPDataResponse(); //=================== managed file write do { try { result = StillImageDevice.ExecuteReadBigData(CONST_CMD_GetObject, (total, current) => { double i = (double) current/total; TransferProgress = Convert.ToUInt32(i*100); },Convert.ToUInt32(o) ); } // if not enough memory for transfer catch it and wait and try again catch (OutOfMemoryException) { } catch (COMException) { } if (result != null && result.Data != null) { stream.Write(result.Data, 0, result.Data.Length); } else { Log.Error("Transfer error code retrying " + result.ErrorCode.ToString("X")); Thread.Sleep(200); retryes--; } } while (result.Data == null && retryes > 0); //================================================================== //=================== direct file write //StillImageDevice.ExecuteReadBigDataWriteToFile(CONST_CMD_GetObject, // Convert.ToInt32(o), -1, // (total, current) => // { // double i = (double)current / total; // TransferProgress = // Convert.ToUInt32(i * 100); // }, filename); //================================================================== _timer.Start(); TransferProgress = 0; } }
public MTPDataResponse ExecuteReadDataEx(uint code) { int counter = 0; DeviceIsBusy = true; MTPDataResponse res = new MTPDataResponse(); bool allok; do { res = StillImageDevice.ExecuteReadData(code); allok = true; if ((res.ErrorCode == ErrorCodes.MTP_Device_Busy || res.ErrorCode == PortableDeviceErrorCodes.ERROR_BUSY) && counter < CONST_LOOP_TIME) { Thread.Sleep(CONST_READY_TIME); counter++; allok = false; } } while (!allok); DeviceIsBusy = false; return res; }
public MTPDataResponse ExecuteReadDataEx(uint code, uint param1, uint param2, uint param3, int loop, int counter) { DeviceIsBusy = true; MTPDataResponse res = new MTPDataResponse(); bool allok; do { allok = true; res = StillImageDevice.ExecuteReadData(code, param1, param2, param3); if ((res.ErrorCode == ErrorCodes.MTP_Device_Busy || res.ErrorCode == PortableDeviceErrorCodes.ERROR_BUSY) && counter < loop) { Thread.Sleep(CONST_READY_TIME); counter++; allok = false; } } while (!allok); DeviceIsBusy = false; return res; }
public MTPDataResponse ExecuteReadBigData(uint code, TransferCallback callback, params uint[] parameters) { MTPDataResponse res = new MTPDataResponse(); // source: http://msdn.microsoft.com/en-us/library/windows/desktop/ff384843(v=vs.85).aspx // and view-source:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26860397.html // error codes http://msdn.microsoft.com/en-us/library/windows/desktop/dd319335(v=vs.85).aspx byte[] imgdate = new byte[8]; IPortableDeviceValues commandValues = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass(); IPortableDeviceValues pParameters = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValues(); IPortableDevicePropVariantCollection propVariant = (IPortableDevicePropVariantCollection)new PortableDeviceTypesLib.PortableDevicePropVariantCollection(); IPortableDeviceValues pResults; //commandValues.SetGuidValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref command.fmtid); commandValues.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.fmtid); commandValues.SetUnsignedIntegerValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.pid); commandValues.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref imgdate[0], (uint)imgdate.Length); foreach (uint parameter in parameters) { tag_inner_PROPVARIANT vparam = new tag_inner_PROPVARIANT(); UintToPropVariant(parameter, out vparam); propVariant.Add(ref vparam); } commandValues.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant); commandValues.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_CODE, (uint)code); // According to documentation, first parameter should be 0 (see http://msdn.microsoft.com/en-us/library/dd375691%28v=VS.85%29.aspx) this.portableDeviceClass.SendCommand(0, commandValues, out pResults); try { int pValue = 0; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { // check if the device is busy, and after 100 ms seconds try again if (((uint)pValue) == PortableDeviceErrorCodes.ERROR_BUSY) { Thread.Sleep(50); return ExecuteReadBigData(code, callback, parameters); } } } catch (Exception) { } //string pwszContext = string.Empty; //pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out pwszContext); //uint cbReportedDataSize = 0; //pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out cbReportedDataSize); uint tmpBufferSize = 0; uint tmpTransferSize = 0; string tmpTransferContext = string.Empty; { pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out tmpTransferContext); pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out tmpBufferSize); pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPTIMAL_TRANSFER_BUFFER_SIZE, out tmpTransferSize); try { int pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { return null; } } catch { } } pParameters.Clear(); pResults.Clear(); uint offset = 0; res.Data = new byte[(int)tmpBufferSize]; bool cont = true; do { if (offset + tmpTransferSize >= tmpBufferSize) { cont = false; tmpTransferSize = (uint)(tmpBufferSize - offset); } byte[] tmpData = new byte[(int)tmpTransferSize]; pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.fmtid); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.pid); pParameters.SetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext); pParameters.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref tmpData[0], (uint)tmpTransferSize); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_TO_READ, (uint)tmpTransferSize); pParameters.SetIPortableDevicePropVariantCollectionValue( ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant); portableDeviceClass.SendCommand(0, pParameters, out pResults); uint cbBytesRead = 0; try { int pValue = 0; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { res.ErrorCode = (uint)pValue; return res; } } catch (Exception) { } callback((int)tmpBufferSize, (int)offset); GCHandle pinnedArray = GCHandle.Alloc(imgdate, GCHandleType.Pinned); IntPtr ptr = pinnedArray.AddrOfPinnedObject(); uint dataread = 0; pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_READ, out dataread); pResults.GetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ptr, out cbBytesRead); IntPtr tmpPtr = new IntPtr(Marshal.ReadInt64(ptr)); //Marshal.Copy(tmpPtr, res.Data, (int)offset, (int)cbBytesRead); for (int i = 0; i < cbBytesRead; i++) { res.Data[offset + i] = Marshal.ReadByte(tmpPtr, i); } Marshal.FreeHGlobal(tmpPtr); pinnedArray.Free(); offset += cbBytesRead; } while (cont); pParameters.Clear(); pResults.Clear(); { pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.fmtid); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.pid); pParameters.SetStringValue(PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext); } portableDeviceClass.SendCommand(0, pParameters, out pResults); try { int tmpResult = 0; pResults.GetErrorValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out tmpResult); if (tmpResult != 0) { } } catch { } return res; }
private MTPDataResponse ExecuteReadData(uint code, IPortableDevicePropVariantCollection propVariant) { MTPDataResponse resp = new MTPDataResponse(); // source: http://msdn.microsoft.com/en-us/library/windows/desktop/ff384843(v=vs.85).aspx // and view-source:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26860397.html // error codes http://msdn.microsoft.com/en-us/library/windows/desktop/dd319335(v=vs.85).aspx byte[] imgdate = new byte[8]; IPortableDeviceValues commandValues = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass(); IPortableDeviceValues pParameters = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValues(); IPortableDeviceValues pResults; //commandValues.SetGuidValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref command.fmtid); commandValues.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.fmtid); commandValues.SetUnsignedIntegerValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.pid); commandValues.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref imgdate[0], (uint)imgdate.Length); commandValues.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant); commandValues.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_CODE, (uint)code); // According to documentation, first parameter should be 0 (see http://msdn.microsoft.com/en-us/library/dd375691%28v=VS.85%29.aspx) this.portableDeviceClass.SendCommand(0, commandValues, out pResults); try { int pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return resp; } } catch (Exception) { } uint tmpBufferSize = 0; uint tmpTransferSize = 0; string tmpTransferContext = string.Empty; { pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out tmpTransferContext); pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out tmpBufferSize); pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPTIMAL_TRANSFER_BUFFER_SIZE, out tmpTransferSize); } pParameters.Clear(); pResults.Clear(); byte[] tmpData = new byte[(int)tmpTransferSize]; pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.fmtid); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.pid); pParameters.SetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext); pParameters.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref tmpData[0], (uint)tmpTransferSize); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_TO_READ, (uint)tmpTransferSize); pParameters.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant); portableDeviceClass.SendCommand(0, pParameters, out pResults); uint cbBytesRead = 0; try { int pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return resp; } } catch (Exception) { } // 24,142,174,9 // 18, 8E GCHandle pinnedArray = GCHandle.Alloc(imgdate, GCHandleType.Pinned); IntPtr ptr = pinnedArray.AddrOfPinnedObject(); uint dataread = 0; pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_READ, out dataread); pResults.GetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ptr, out cbBytesRead); IntPtr tmpPtr = new IntPtr(Marshal.ReadInt64(ptr)); resp.Data = new byte[(int)cbBytesRead]; Marshal.Copy(tmpPtr, resp.Data, 0, (int)cbBytesRead); //for (int i = 0; i < cbBytesRead; i++) //{ // resp.Data[i] = Marshal.ReadByte(tmpPtr, i); //} Marshal.FreeHGlobal(tmpPtr); pinnedArray.Free(); pParameters.Clear(); pResults.Clear(); { pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.fmtid); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.pid); pParameters.SetStringValue(PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext); } portableDeviceClass.SendCommand(0, pParameters, out pResults); try { int pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return resp; } uint iValue; pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out iValue); if (iValue != 0) { resp.ErrorCode = iValue; return resp; } } catch { } return resp; }
public override void TransferFile(object o, string filename) { int retryes = 10; lock (Locker) { _timer.Stop(); MTPDataResponse result = new MTPDataResponse(); //=================== managed file write do { try { result = StillImageDevice.ExecuteReadBigData(CONST_CMD_GetObject, (total, current) => { double i = (double) current/total; TransferProgress = Convert.ToUInt32(i*100); },Convert.ToUInt32(o) ); } // if not enough memory for transfer catch it and wait and try again catch (OutOfMemoryException) { } catch (COMException) { } if (result != null && result.Data != null) { using (BinaryWriter writer = new BinaryWriter(File.Open(filename, FileMode.Create))) { writer.Write(result.Data); } } else { Log.Error("Transfer error code retrying " + result.ErrorCode.ToString("X")); Thread.Sleep(200); retryes--; } } while (result.Data == null && retryes > 0); //================================================================== //=================== direct file write //StillImageDevice.ExecuteReadBigDataWriteToFile(CONST_CMD_GetObject, // Convert.ToInt32(o), -1, // (total, current) => // { // double i = (double)current / total; // TransferProgress = // Convert.ToUInt32(i * 100); // }, filename); //================================================================== _timer.Start(); TransferProgress = 0; } }
public MTPDataResponse ExecuteReadBigData(uint code, TransferCallback callback, params uint[] parameters) { MTPDataResponse res = new MTPDataResponse(); // source: http://msdn.microsoft.com/en-us/library/windows/desktop/ff384843(v=vs.85).aspx // and view-source:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26860397.html // error codes http://msdn.microsoft.com/en-us/library/windows/desktop/dd319335(v=vs.85).aspx byte[] imgdate = new byte[8]; IPortableDeviceValues commandValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); IPortableDeviceValues pParameters = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); IPortableDevicePropVariantCollection propVariant = (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection(); IPortableDeviceValues pResults; //commandValues.SetGuidValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref command.fmtid); commandValues.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.fmtid); commandValues.SetUnsignedIntegerValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.pid); commandValues.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref imgdate[0], (uint)imgdate.Length); foreach (uint parameter in parameters) { tag_inner_PROPVARIANT vparam = new tag_inner_PROPVARIANT(); UintToPropVariant(parameter, out vparam); propVariant.Add(ref vparam); } commandValues.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant); commandValues.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_CODE, (uint)code); // According to documentation, first parameter should be 0 (see http://msdn.microsoft.com/en-us/library/dd375691%28v=VS.85%29.aspx) this.portableDeviceClass.SendCommand(0, commandValues, out pResults); try { int pValue = 0; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { // check if the device is busy, and after 100 ms seconds try again if (((uint)pValue) == PortableDeviceErrorCodes.ERROR_BUSY) { Thread.Sleep(50); return(ExecuteReadBigData(code, callback, parameters)); } } } catch (Exception) { } //string pwszContext = string.Empty; //pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out pwszContext); //uint cbReportedDataSize = 0; //pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out cbReportedDataSize); uint tmpBufferSize = 0; uint tmpTransferSize = 0; string tmpTransferContext = string.Empty; { pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out tmpTransferContext); pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out tmpBufferSize); pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPTIMAL_TRANSFER_BUFFER_SIZE, out tmpTransferSize); try { int pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { return(null); } } catch { } } pParameters.Clear(); pResults.Clear(); uint offset = 0; res.Data = new byte[(int)tmpBufferSize]; bool cont = true; do { if (offset + tmpTransferSize >= tmpBufferSize) { cont = false; tmpTransferSize = (uint)(tmpBufferSize - offset); } byte[] tmpData = new byte[(int)tmpTransferSize]; pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.fmtid); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.pid); pParameters.SetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext); pParameters.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref tmpData[0], (uint)tmpTransferSize); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_TO_READ, (uint)tmpTransferSize); pParameters.SetIPortableDevicePropVariantCollectionValue( ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant); portableDeviceClass.SendCommand(0, pParameters, out pResults); uint cbBytesRead = 0; try { int pValue = 0; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { res.ErrorCode = (uint)pValue; return(res); } } catch (Exception) { } callback((int)tmpBufferSize, (int)offset); GCHandle pinnedArray = GCHandle.Alloc(imgdate, GCHandleType.Pinned); IntPtr ptr = pinnedArray.AddrOfPinnedObject(); uint dataread = 0; pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_READ, out dataread); pResults.GetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ptr, out cbBytesRead); IntPtr tmpPtr = new IntPtr(Marshal.ReadInt64(ptr)); //Marshal.Copy(tmpPtr, res.Data, (int)offset, (int)cbBytesRead); for (int i = 0; i < cbBytesRead; i++) { res.Data[offset + i] = Marshal.ReadByte(tmpPtr, i); } Marshal.FreeHGlobal(tmpPtr); pinnedArray.Free(); offset += cbBytesRead; } while (cont); pParameters.Clear(); pResults.Clear(); { pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.fmtid); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.pid); pParameters.SetStringValue(PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext); } portableDeviceClass.SendCommand(0, pParameters, out pResults); try { int tmpResult = 0; pResults.GetErrorValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out tmpResult); if (tmpResult != 0) { } } catch { } return(res); }
private MTPDataResponse ExecuteReadData(uint code, IPortableDevicePropVariantCollection propVariant) { MTPDataResponse resp = new MTPDataResponse(); // source: http://msdn.microsoft.com/en-us/library/windows/desktop/ff384843(v=vs.85).aspx // and view-source:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26860397.html // error codes http://msdn.microsoft.com/en-us/library/windows/desktop/dd319335(v=vs.85).aspx byte[] imgdate = new byte[8]; IPortableDeviceValues commandValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); IPortableDeviceValues pParameters = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); IPortableDeviceValues pResults; //commandValues.SetGuidValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref command.fmtid); commandValues.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.fmtid); commandValues.SetUnsignedIntegerValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.pid); commandValues.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref imgdate[0], (uint)imgdate.Length); commandValues.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant); commandValues.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_CODE, (uint)code); // According to documentation, first parameter should be 0 (see http://msdn.microsoft.com/en-us/library/dd375691%28v=VS.85%29.aspx) this.portableDeviceClass.SendCommand(0, commandValues, out pResults); try { int pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return(resp); } } catch (Exception) { } uint tmpBufferSize = 0; uint tmpTransferSize = 0; string tmpTransferContext = string.Empty; { pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out tmpTransferContext); pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out tmpBufferSize); pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPTIMAL_TRANSFER_BUFFER_SIZE, out tmpTransferSize); } pParameters.Clear(); pResults.Clear(); byte[] tmpData = new byte[(int)tmpTransferSize]; pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.fmtid); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.pid); pParameters.SetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext); pParameters.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref tmpData[0], (uint)tmpTransferSize); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_TO_READ, (uint)tmpTransferSize); pParameters.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant); portableDeviceClass.SendCommand(0, pParameters, out pResults); uint cbBytesRead = 0; try { int pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return(resp); } } catch (Exception) { } // 24,142,174,9 // 18, 8E GCHandle pinnedArray = GCHandle.Alloc(imgdate, GCHandleType.Pinned); IntPtr ptr = pinnedArray.AddrOfPinnedObject(); uint dataread = 0; pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_READ, out dataread); pResults.GetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ptr, out cbBytesRead); IntPtr tmpPtr = new IntPtr(Marshal.ReadInt64(ptr)); resp.Data = new byte[(int)cbBytesRead]; Marshal.Copy(tmpPtr, resp.Data, 0, (int)cbBytesRead); //for (int i = 0; i < cbBytesRead; i++) //{ // resp.Data[i] = Marshal.ReadByte(tmpPtr, i); //} Marshal.FreeHGlobal(tmpPtr); pinnedArray.Free(); pParameters.Clear(); pResults.Clear(); { pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.fmtid); pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.pid); pParameters.SetStringValue(PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext); } portableDeviceClass.SendCommand(0, pParameters, out pResults); try { int pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return(resp); } uint iValue; pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out iValue); if (iValue != 0) { resp.ErrorCode = iValue; return(resp); } } catch { } return(resp); }
private static void ParseDeviceInfo(MTPDataResponse rep) { int offset = 0; int readlen; int StandardVersion = 0; readlen = GetUWord(rep.Data, offset, ref StandardVersion); offset += readlen; }
public static long GetValue(MTPDataResponse result, int index, int dataLength) { long val = 0; switch (dataLength) { case 1: val = result.Data[index]; break; case 2: val = BitConverter.ToUInt16(result.Data, index); break; case 4: val = BitConverter.ToUInt32(result.Data, index); break; default: val = (long) BitConverter.ToUInt64(result.Data, index); break; } return val; }
protected XmlDeviceData LoadDeviceData(MTPDataResponse res) { XmlDeviceData deviceInfo = new XmlDeviceData(); ErrorCodes.GetException(res.ErrorCode); deviceInfo.Manufacturer = Manufacturer; int index = 2 + 4 + 2; int vendorDescCount = res.Data[index]; index += vendorDescCount * 2; index += 3; int comandsCount = res.Data[index]; index += 2; // load commands for (int i = 0; i < comandsCount; i++) { index += 2; deviceInfo.AvaiableCommands.Add(new XmlCommandDescriptor() { Code = BitConverter.ToUInt16(res.Data, index) }); } index += 2; int eventcount = res.Data[index]; index += 2; // load events for (int i = 0; i < eventcount; i++) { index += 2; deviceInfo.AvaiableEvents.Add(new XmlEventDescriptor() { Code = BitConverter.ToUInt16(res.Data, index) }); } index += 2; int propertycount = res.Data[index]; index += 2; // load properties codes for (int i = 0; i < propertycount; i++) { index += 2; deviceInfo.AvaiableProperties.Add(new XmlPropertyDescriptor() { Code = BitConverter.ToUInt16(res.Data, index) }); } try { MTPDataResponse vendor_res = ExecuteReadDataEx(0x90CA); if (vendor_res.Data.Length > 0) { index = 0; propertycount = vendor_res.Data[index]; index += 2; for (int i = 0; i < propertycount; i++) { index += 2; deviceInfo.AvaiableProperties.Add(new XmlPropertyDescriptor() { Code = BitConverter.ToUInt16(vendor_res.Data, index) }); } } } catch (Exception) { } return deviceInfo; }