public unsafe void GetiPhonesAsync() { void *voidPtr; _deviceNotificationCallback = DeviceNotifyCallback; int num = MobileDevice.AMDeviceNotificationSubscribe(_deviceNotificationCallback, 0, 0, 0, out voidPtr); if (num != 0) { throw new Exception("AMDeviceNotificationSubscribe failed with error " + num); } }
/// <summary> /// 关闭Session,此函数无需外界单独调用,对应功能模块调用已经和开启Session成对存在 /// </summary> /// <returns></returns> private kAMDError StopSession() { this.isSessionOpen = false; try { return((kAMDError)MobileDevice.AMDeviceStopSession(this.DevicePtr)); } catch { return(kAMDError.kAMDUndefinedError); } }
/// <summary> /// 判断文件是否存在 /// </summary> /// <param name="path">完整设备内路径</param> /// <returns>1为存在,0为不存在</returns> public unsafe bool Exists(string path) { void *dict = null; int num = MobileDevice.AFCFileInfoOpen(this.hAFC, System.Text.Encoding.UTF8.GetBytes(path), ref dict); if (num == 0) { //释放dict指针 MobileDevice.AFCKeyValueClose(dict); } return(num == 0); }
public bool EnterRecovery() { int i = MobileDevice.AMDeviceEnterRecovery(this.iPhoneHandle); if (i == (int)kAMDError.kAMDSuccess) { return(true); } else { return(false); } }
/// <summary> /// 断开设备链接,但不会断开USB树链接,因而可以通过调用Connect来重新链接到设备 /// </summary> /// <returns></returns> public kAMDError Disconnect() { var kAMDSuccess = kAMDError.kAMDSuccess; this.isConnected = false; try { kAMDSuccess = (kAMDError)MobileDevice.AMDeviceDisconnect(DevicePtr); } catch (Exception exception) { Console.WriteLine(exception.ToString()); } return(kAMDSuccess); }
/// <summary> /// 开启Socket服务 /// </summary> /// <param name="serviceName">服务名</param> /// <param name="serviceSocket">Socket链接</param> /// <returns></returns> private bool StartSocketService(string serviceName, ref int serviceSocket) { var kAMDSuccess = kAMDError.kAMDSuccess; if (serviceSocket > 0)//已经开启服务 { return(true); } if (!this.isConnected) { if (Connect() != (int)kAMDError.kAMDSuccess) { Console.WriteLine("StartService()执行Connect()失败"); return(false); } } bool openSession = false; if (!this.isSessionOpen) { kAMDSuccess = StartSession(); if (kAMDSuccess == kAMDError.kAMDSuccess) { openSession = true; } else { return(false); } } var result = false; var zero = IntPtr.Zero; if ((MobileDevice.AMDeviceSecureStartService(this.DevicePtr, CoreFoundation.StringToCFString(serviceName), IntPtr.Zero, ref zero) == (int)kAMDError.kAMDSuccess)) { serviceSocket = MobileDevice.AMDServiceConnectionGetSocket(zero); result = true; } else if (MobileDevice.AMDeviceStartService(this.DevicePtr, CoreFoundation.StringToCFString(serviceName), ref serviceSocket, IntPtr.Zero) == (int)kAMDError.kAMDSuccess) { result = true; } if (openSession) { StopSession(); } return(result); }
/// <summary> /// 获取设备信息 /// </summary> /// <param name="domain">The domain.</param> /// <param name="key">The key.</param> /// <returns>System.Object.</returns> public object GetDeviceValue(string domain, string key) { object resultValue = null; try { var isReconnect = false; var isReOpenSession = false; if (!this.isConnected) { if (Connect() != (int)kAMDError.kAMDSuccess) { return(null); } isReconnect = true; } if (!this.isSessionOpen) { if (StartSession(false) == (int)kAMDError.kAMDSuccess) { isReOpenSession = true; } else { if (isReconnect) { Disconnect(); } } } resultValue = MobileDevice.AMDeviceCopyValue(this.DevicePtr, domain, key); if (isReOpenSession) { StopSession(); } if (isReconnect) { Disconnect(); } } catch { } return(resultValue); }
/// <summary> /// 开启Session。Session需要在调用服务前先行打开,然后完成服务操作后自行关闭(函数会自动关闭),否则可能会遇到无法打开服务的错误 /// </summary> /// <returns></returns> public kAMDError StartSession(bool isRretry = false) { var kAMDSuccess = kAMDError.kAMDSuccess; try { if (!this.isSessionOpen) { kAMDSuccess = (kAMDError)MobileDevice.AMDeviceStartSession(this.DevicePtr); if (kAMDSuccess != kAMDError.kAMDInvalidHostIDError) { if (kAMDSuccess != (int)kAMDError.kAMDSuccess) { //修复:Session关闭后一段时间无法再次打开的问题 if (!isRretry) { this.Disconnect(); this.Connect(); return(StartSession(true)); } return(kAMDSuccess); } this.isSessionOpen = true; return(kAMDSuccess); } if ((MobileDevice.AMDeviceUnpair(this.DevicePtr) == (int)kAMDError.kAMDSuccess) && (MobileDevice.AMDevicePair(this.DevicePtr) == (int)kAMDError.kAMDSuccess)) { kAMDSuccess = (kAMDError)MobileDevice.AMDeviceStartSession(this.DevicePtr); if (kAMDSuccess != kAMDError.kAMDSuccess) { return(kAMDSuccess); } this.isSessionOpen = true; return(kAMDSuccess); } } return(kAMDSuccess); } catch { kAMDSuccess = kAMDError.kAMDUndefinedError; } return(kAMDSuccess); }
/// <summary> /// 停止Socket服务 /// </summary> /// <param name="inSocket"></param> /// <returns></returns> private bool StopSocketService(ref int socket) { kAMDError kAMDSuccess = kAMDError.kAMDSuccess; if (socket > 0) { try { kAMDSuccess = (kAMDError)MobileDevice.closesocket(socket); } catch (Exception ex) { return(false); } } socket = 0; return(kAMDSuccess != kAMDError.kAMDSuccess); }
public override unsafe void Write(byte[] buffer, int offset, int count) { byte[] buffer2; if (!this.CanWrite) { throw new NotImplementedException("Stream open for reading only"); } if (offset == 0) { buffer2 = buffer; } else { buffer2 = new byte[count]; Buffer.BlockCopy(buffer, offset, buffer2, 0, count); } int num = MobileDevice.AFCFileRefWrite(this.phone.AFCHandle, this.handle, buffer2, (uint)count); }
/// <summary> /// 链接到设备并开启相应服务,无需单独调用,在NotifyCallback已经调用 /// </summary> /// <returns></returns> private unsafe bool ConnectToPhone() { if (MobileDevice.AMDeviceConnect(this.iPhoneHandle) == 1) { throw new Exception("Phone in recovery mode, support not yet implemented"); } if (MobileDevice.AMDeviceIsPaired(this.iPhoneHandle) == 0) { return(false); } if (MobileDevice.AMDeviceValidatePairing(this.iPhoneHandle) != 0) { return(false); } if (MobileDevice.AMDeviceStartSession(this.iPhoneHandle) == 1) { return(false); } //检测是否存在afc2服务 if (MobileDevice.AMDeviceStartService(this.iPhoneHandle, MobileDevice.CFStringMakeConstantString("com.apple.afc2"), ref this.hService, null) != 0) { //打开afc服务 if (MobileDevice.AMDeviceStartService(this.iPhoneHandle, MobileDevice.CFStringMakeConstantString("com.apple.afc"), ref this.hService, null) != 0) { return(false); } } else { this.wasAFC2 = true; } if (MobileDevice.AFCConnectionOpen(this.hService, 0, ref this.hAFC) != 0) { return(false); } this.connected = true; //开启安装IPA服务 if (MobileDevice.AMDeviceStartService(this.iPhoneHandle, MobileDevice.CFStringMakeConstantString("com.apple.mobile.installation_proxy"), ref this.installFD, null) != 0) { return(false); } return(true); }
public unsafe Dictionary <string, string> GetFileInfo(string path) { var dictionary = new Dictionary <string, string>(); void *dict = null; if ((MobileDevice.AFCFileInfoOpen(this.hAFC, path, ref dict) == 0) && (dict != null)) { void *voidPtr2; void *voidPtr3; while (((MobileDevice.AFCKeyValueRead(dict, out voidPtr2, out voidPtr3) == 0) && (voidPtr2 != null)) && (voidPtr3 != null)) { string key = Marshal.PtrToStringAnsi(new IntPtr(voidPtr2)); string str2 = Marshal.PtrToStringAnsi(new IntPtr(voidPtr3)); dictionary.Add(key, str2); } MobileDevice.AFCKeyValueClose(dict); } return(dictionary); }
/// <summary> /// 发送消息通过Socket,大部分用途为发送plist文件(指令)给设备 /// </summary> /// <param name="sock"></param> /// <param name="message"></param> /// <returns></returns> public bool SendMessageToSocket(int sock, IntPtr message) { if ((sock < 1) || (message == IntPtr.Zero)) { return(false); } var flag = false; var stream = CoreFoundation.CFWriteStreamCreateWithAllocatedBuffers(IntPtr.Zero, IntPtr.Zero); if (stream != IntPtr.Zero) { if (!CoreFoundation.CFWriteStreamOpen(stream)) { return(false); } var zero = IntPtr.Zero; if (CoreFoundation.CFPropertyListWriteToStream(message, stream, CFPropertyListFormat.kCFPropertyListBinaryFormat_v1_0, ref zero) > 0) { var propertyName = CoreFoundation.kCFStreamPropertyDataWritten; var srcRef = CoreFoundation.CFWriteStreamCopyProperty(stream, propertyName); var buffer = CoreFoundation.CFDataGetBytePtr(srcRef); var bufferlen = CoreFoundation.CFDataGetLength(srcRef); var structure = MobileDevice.htonl((uint)bufferlen); var num3 = Marshal.SizeOf(structure); if (MobileDevice.send_UInt32(sock, ref structure, num3, 0) != num3) { Console.WriteLine("could not send message size"); } else if (MobileDevice.send(sock, buffer, bufferlen, 0) != bufferlen) { Console.WriteLine("Could not send message."); } else { flag = true; } CoreFoundation.CFRelease(srcRef); } CoreFoundation.CFWriteStreamClose(stream); } return(flag); }
public override unsafe void Write(byte[] buffer, int offset, int count) { byte[] buffer2; if (_mode != OpenMode.Write) { throw new NotImplementedException("Stream open for reading only"); } if (offset == 0) { buffer2 = buffer; } else { buffer2 = new byte[count]; Buffer.BlockCopy(buffer, offset, buffer2, 0, count); } uint len = (uint)count; MobileDevice.AFCFileRefWrite(_phone.AFCHandle, _handle, buffer2, len); }
/// <summary> /// 链接到设备,用于设备链接失效或者手动推出后重链接操作,此函数只要设备还在USB树上,是无需用户插拔的 /// </summary> /// <returns></returns> public kAMDError Connect() { kAMDError kAMDSuccess = kAMDError.kAMDSuccess; try { if (!this.isConnected) { kAMDSuccess = (kAMDError)MobileDevice.AMDeviceConnect(this.DevicePtr); if (kAMDSuccess == kAMDError.kAMDSuccess) { this.isConnected = true; } } } catch (Exception exception) { Console.WriteLine(exception.ToString()); } return(kAMDSuccess); }
static void Main(string[] args) { MobileDevice Nokia = new MobileDevice("Nokia", "Lumia 650", 99, "Mtel", new Display(30, 10000), new Battery()); //MobileDevice Samsung = new MobileDevice("Samsung", "S600", 199, "Globul", // new Display(10, 10000), new Battery("Samsung LongLiveBattery", 20, 5)); Console.WriteLine(Nokia); //Console.WriteLine(Samsung); Nokia.AddCall("0876253132", 120); Nokia.AddCall("0876253452", 22); Nokia.AddCall("0876245555", 42); Nokia.AddCall("0876356565", 52); Nokia.RemoveCall(1); Nokia.CallHistory(); Nokia.AllCallsPrice(); Console.WriteLine("Total Call Price: {0}", Nokia.AllCallsPrice()); }
/// <summary> /// 开始监听链接 /// </summary> /// <param name="errorCallBack">链接出错回调</param> public void StartListen() { if (!iSStartListen) { iSStartListen = true; try { deviceNotificationCallback = NotificationCallback; deviceDFUConnectedNotificationCallback = DfuConnectCallback; deviceRecoveryConnectedNotificationCallback = RecoveryConnectCallback; deviceDFUDisConnectedNotificationCallback = DfuDisconnectCallback; deviceRecoveryDisConnectedNotificationCallback = RecoveryDisconnectCallback; IntPtr zero = IntPtr.Zero; kAMDError error = (kAMDError)MobileDevice.AMDeviceNotificationSubscribe(this.deviceNotificationCallback, 0, 1, 0, ref zero); if (error != kAMDError.kAMDSuccess) { if (ListenErrorEvent != null) { ListenErrorEvent(this, new ListenErrorEventHandlerEventArgs("AMDeviceNotificationSubscribe failed with error : " + error.ToString(), ListenErrorEventType.StartListen)); } } IntPtr userInfo = IntPtr.Zero; error = (kAMDError)MobileDevice.AMRestoreRegisterForDeviceNotifications(this.deviceDFUConnectedNotificationCallback, this.deviceRecoveryConnectedNotificationCallback, this.deviceDFUDisConnectedNotificationCallback, this.deviceRecoveryDisConnectedNotificationCallback, 0, ref userInfo); if (error != kAMDError.kAMDSuccess) { if (ListenErrorEvent != null) { ListenErrorEvent(this, new ListenErrorEventHandlerEventArgs("AMRestoreRegisterForDeviceNotifications failed with error : " + error.ToString(), ListenErrorEventType.StartListen)); } } } catch (Exception ex) { if (ListenErrorEvent != null) { ListenErrorEvent(this, new ListenErrorEventHandlerEventArgs(ex.Message, ListenErrorEventType.StartListen)); } } } }
private unsafe void doConstruction() { void *voidPtr; this.dnc = new DeviceNotificationCallback(this.NotifyCallback); this.drn1 = new DeviceRestoreNotificationCallback(this.DfuConnectCallback); this.drn2 = new DeviceRestoreNotificationCallback(this.RecoveryConnectCallback); this.drn3 = new DeviceRestoreNotificationCallback(this.DfuDisconnectCallback); this.drn4 = new DeviceRestoreNotificationCallback(this.RecoveryDisconnectCallback); int num = MobileDevice.AMDeviceNotificationSubscribe(this.dnc, 0, 0, 0, out voidPtr); if (num != 0) { throw new Exception("AMDeviceNotificationSubscribe failed with error " + num); } num = MobileDevice.AMRestoreRegisterForDeviceNotifications(this.drn1, this.drn2, this.drn3, this.drn4, 0, null); if (num != 0) { throw new Exception("AMRestoreRegisterForDeviceNotifications failed with error " + num); } this.current_directory = "/"; }
public unsafe bool ConnectViaAFC() { if (_connected) { MobileDevice.AFCConnectionClose(hAFC); _connected = false; } var connectResult = MobileDevice.AMDeviceConnect(iPhoneHandle); if (connectResult == 1) { throw new Exception("Phone in recovery mode, support not yet implemented."); } if (MobileDevice.AMDeviceIsPaired(iPhoneHandle) == 0) { return(false); } if (MobileDevice.AMDeviceValidatePairing(iPhoneHandle) != 0) { return(false); } if (MobileDevice.AMDeviceStartSession(iPhoneHandle) == 1) { return(false); } // Connect via AFC, only Media directory is accessible on device if (0 != MobileDevice.AMDeviceStartService(iPhoneHandle, MobileDevice.__CFStringMakeConstantString(MobileDevice.StringToCString("com.apple.afc2")), ref hService, null)) { if (0 != MobileDevice.AMDeviceStartService(iPhoneHandle, MobileDevice.__CFStringMakeConstantString(MobileDevice.StringToCString("com.apple.afc")), ref hService, null)) { return(false); } } _connected = true; return(true); }
public unsafe void GetFileInfo(string path, out ulong size, out bool directory) { Dictionary <string, string> fileInfo = this.GetFileInfo(path); size = fileInfo.ContainsKey("st_size") ? ulong.Parse(fileInfo["st_size"]) : ((ulong)0L); bool flag = false; directory = false; if (fileInfo.ContainsKey("st_ifmt")) { string str = fileInfo["st_ifmt"]; if (str != null) { if (!(str == "S_IFDIR")) { if (str == "S_IFLNK") { flag = true; } } else { directory = true; } } } if (flag) { bool flag3; void *dir = null; directory = flag3 = MobileDevice.AFCDirectoryOpen(this.hAFC, path, ref dir) == 0; if (flag3) { MobileDevice.AFCDirectoryClose(this.hAFC, dir); } } }
private unsafe bool ConnectToPhone() { if (MobileDevice.AMDeviceConnect(this.iPhoneHandle) == 1) { throw new Exception("Phone in recovery mode, support not yet implemented"); } if (MobileDevice.AMDeviceIsPaired(this.iPhoneHandle) == 0) { return(false); } if (MobileDevice.AMDeviceValidatePairing(this.iPhoneHandle) != 0) { return(false); } if (MobileDevice.AMDeviceStartSession(this.iPhoneHandle) == 1) { return(false); } if (0 != MobileDevice.AMDeviceStartService(this.iPhoneHandle, MobileDevice.__CFStringMakeConstantString(MobileDevice.StringToCString("com.apple.afc2")), ref this.hService, null)) { if (0 != MobileDevice.AMDeviceStartService(this.iPhoneHandle, MobileDevice.__CFStringMakeConstantString(MobileDevice.StringToCString("com.apple.afc")), ref this.hService, null)) { return(false); } } else { this.wasAFC2 = true; } if (MobileDevice.AFCConnectionOpen(this.hService, 0, ref this.hAFC) != 0) { return(false); } this.connected = true; return(true); }
public override unsafe void Flush() { MobileDevice.AFCFlushData(this.phone.AFCHandle, this.handle); }
public override unsafe void SetLength(long value) { MobileDevice.AFCFileRefSetFileSize(this.phone.AFCHandle, this.handle, (uint)value); }
public int setAutoBoot(bool value) { return(MobileDevice.AMRecoveryModeDeviceSetAutoBoot(this.RecoveryHandle, Conversions.ToByte(Interaction.IIf(value, 1, 0)))); }
public void reboot() { MobileDevice.AMRecoveryModeDeviceReboot(this.RecoveryHandle); }
public unsafe void SendCommandToDevice(string command) { MobileDevice.sendCommandToDevice(iPhoneHandle, MobileDevice.__CFStringMakeConstantString(MobileDevice.StringToCFString(command)), 0); }
public unsafe bool Rename(string sourceName, string destName) { return(MobileDevice.AFCRenamePath(hAFC, sourceName, destName) == 0); }
public unsafe bool ConnectViaHouseArrest(string bundleIdentifier) { if (_connected) { MobileDevice.AFCConnectionClose(hAFC); _connected = false; } var connectResult = MobileDevice.AMDeviceConnect(iPhoneHandle); if (connectResult == 1) { throw new Exception("Phone in recovery mode, support not yet implemented."); } if (MobileDevice.AMDeviceIsPaired(iPhoneHandle) == 0) { return(false); } if (MobileDevice.AMDeviceValidatePairing(iPhoneHandle) != 0) { return(false); } if (MobileDevice.AMDeviceStartSession(iPhoneHandle) == 1) { return(false); } if (string.IsNullOrEmpty(bundleIdentifier)) { Console.WriteLine("Bundle identifier cannot be null when using house arrest service."); return(false); } // Connect via house arrest, only Documents directory is accessible on device if (MobileDevice.AMDeviceStartHouseArrestService(iPhoneHandle, MobileDevice.__CFStringMakeConstantString(MobileDevice.StringToCString(bundleIdentifier)), null, ref hService, 0) != 0) { Console.WriteLine("Unable to find bundle with id: {0}", bundleIdentifier); return(false); } // Need to stop session and disconnect for house arrest to work, comment out for AFC if (MobileDevice.AMDeviceStopSession(iPhoneHandle) != 0) { return(false); } if (MobileDevice.AMDeviceDisconnect(iPhoneHandle) != 0) { return(false); } if (MobileDevice.AFCConnectionOpen(hService, 0, ref hAFC) != 0) { return(false); } _connected = true; return(true); }
public unsafe bool Rename(string sourceName, string destName) { return(MobileDevice.AFCRenamePath(this.hAFC, this.FullPath(this.current_directory, sourceName), this.FullPath(this.current_directory, destName)) == 0); }
public unsafe void RebootRestore() { MobileDevice.AMRestoreModeDeviceReboot(this.iPhoneHandle); }