/// <exception cref="FailedToParseResponse"></exception> /// <exception cref="FailedToConnectToUSBMux"></exception> /// <exception cref="FailedToSendMessage"></exception> public static Device[] ListDevices() { using (var stream = ConnectToUsbMux()) { var reader = new BinaryReader(stream); var writer = new BinaryWriter(stream); writer.SerializePlist(PlistSerializer.CreatePlistMessage("ListDevices")); var listDevicesResult = reader.DeserializePlist(); var deviceList = (NSArray)listDevicesResult["DeviceList"]; var ret = new List <Device>(); for (uint i = 0; i < deviceList.Count; ++i) { var device = deviceList.GetItem <NSDictionary>(i); var deviceId = (NSNumber)device["DeviceID"]; var properties = (NSMutableDictionary)device["Properties"]; var connectionType = (NSString)properties["ConnectionType"]; if (connectionType != "USB") { continue; } var productId = (NSNumber)properties["ProductID"]; var serialNumber = (NSString)properties["SerialNumber"]; var locationId = (NSNumber)properties["LocationID"]; ret.Add(new Device(deviceId.UInt32Value, (ushort)productId, StringToByteArray(serialNumber.ToString()), (uint)locationId)); } return(ret.ToArray()); } }
/// <exception cref="FailedToParseResponse"></exception> public static NSDictionary DeserializePlist(this BinaryReader reader) { try { return(PlistSerializer.PayloadToNSDictionary(MessageData.Deserialize(reader).Payload)); } catch (Exception e) { throw new FailedToParseResponse(e); } }
public static NetworkStream Listen() { var stream = ConnectToUsbMux(); var reader = new BinaryReader(stream, Encoding.UTF8, true); var writer = new BinaryWriter(stream, Encoding.UTF8, true); writer.SerializePlist(PlistSerializer.CreatePlistMessage("Listen")); var parsedPayload = reader.DeserializePlist(); var resultCode = ((NSNumber)parsedPayload["Number"]).Int32Value; var response = (Result)resultCode; if (response != Result.Ok) { throw new FailedToListen("Expected response to be 'Ok', but was '" + response + "'"); } return(stream); }
/// <exception cref="FailedToConnectToDevice"></exception> /// <exception cref="FailedToParseResponse"></exception> /// <exception cref="FailedToConnectToUSBMux"></exception> /// <exception cref="FailedToSendMessage"></exception> public static Stream Connect(Device device, short port) { var stream = ConnectToUsbMux(); var reader = new BinaryReader(stream, Encoding.UTF8, true); var writer = new BinaryWriter(stream, Encoding.UTF8, true); writer.SerializePlist( PlistSerializer.CreatePlistMessage("Connect", new object[] { "DeviceID", "PortNumber" }, new object[] { device.DeviceID, IPAddress.HostToNetworkOrder(port) })); var parsedPayload = reader.DeserializePlist(); var resultCode = ((NSNumber)parsedPayload["Number"]).Int32Value; var response = (Result)resultCode; if (response != Result.Ok) { throw new FailedToConnectToDevice("Expected response to be 'Ok', but was '" + response + "'", device); } return(stream); }