internal IrDADeviceInfo(IrDAAddress id, string name, IrDAHints hints, IrDACharacterSet charset) { this.address = id; this.name = name; this.hints = hints; this.charset = charset; }
public TestHolderIrDADeviceInfo(IrDAAddress address, String name, IrDAHints hints, IrDACharacterSet charset) { m_address = address; m_name = name; m_hints = hints; m_charset = charset; }
/// <summary> /// Gets the name of the peer device using the specified socket. /// </summary> /// <param name="irdaSocket">A connected IrDA <c>Socket</c>.</param> /// <returns>The name of the remote device.</returns> /// - /// <remarks> /// This finds the name of the device to which the socket is connection, /// an exception will occur if the socket is not connected. /// </remarks> /// - /// <exception cref="T:System.ArgumentNullException"> /// <c>s</c> is null (<c>Nothing</c> in Visual Basic). /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// The remote device is not present in the list of discovered devices. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// The socket is not connected. /// </exception> public static string GetRemoteMachineName(Socket irdaSocket) { if (irdaSocket == null) { throw new ArgumentNullException("irdaSocket", "GetRemoteMachineName requires a valid Socket"); } if (!irdaSocket.Connected) { throw new InvalidOperationException("The socket must be connected to a device to get the remote machine name."); } //get remote endpoint IrDAEndPoint ep = (IrDAEndPoint)irdaSocket.RemoteEndPoint; IrDAAddress a = ep.Address; //lookup devices and search for match IrDADeviceInfo[] idia = DiscoverDevices(10, irdaSocket); foreach (IrDADeviceInfo idi in idia) { if (a == idi.DeviceAddress) { return(idi.DeviceName); } } // See unit-test "CheckExceptionThrownByGetRemoteMachineName". throw ExceptionFactory.ArgumentOutOfRangeException(null, "No matching device discovered."); }
internal IrDADeviceInfo(IrDAAddress id, string name, IrDAHints hints, IrDACharacterSet charset) { DeviceAddress = id; DeviceName = name; Hints = hints; CharacterSet = charset; }
//---------------- private void DoTestParseFails(String s) { IrDAAddress result = IrDAAddress.Parse(s); // Expected an Exception Assert.Fail("Expected an error from Parse"); }
public void KeepBadEndianBehaviourByteArray() { IrDAAddress addr = new IrDAAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, }); Assert.AreEqual(0x04030201, addr.ToInt32()); Assert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x04, }, addr.ToByteArray()); Assert.AreEqual("04030201", addr.ToString("N")); }
private void DoTestTryParse(IrDAAddress expected, String s) { IrDAAddress result; bool success = IrDAAddress.TryParse(s, out result); Assert.IsTrue(success, "TryParse failed."); Assert.AreEqual(expected, result); }
public void MutationNotAllowedViaToByte() { const String addrAsString = "04030201"; IrDAAddress addr = new IrDAAddress(0x04030201); Assert.AreEqual(addrAsString, addr.ToString()); byte[] internalBytes = addr.ToByteArray(); internalBytes[1] = 0xFF; // Attempt to mutate the IrDAAddress!!! Assert.AreEqual(addrAsString, addr.ToString()); }
public void MutationNotAllowedViaArrayCtor() { const String addrAsString = "04030201"; byte[] bytes = { 0x01, 0x02, 0x03, 0x04 }; IrDAAddress addr = new IrDAAddress(bytes); Assert.AreEqual(addrAsString, addr.ToString()); bytes[1] = 0xFF; // Attempt to mutate the IrDAAddress!!! Assert.AreEqual(addrAsString, addr.ToString()); }
private static void DoTestTryParseFails(String s) { IrDAAddress addr; try { bool success = IrDAAddress.TryParse(s, out addr); Assert.IsFalse(success); } catch { Assert.Fail("TryParse should never throw an Exception."); } }
//-------------------------------------------------------------- private void DoTestParse(IrDAAddress expected, String s) { IrDAAddress result = IrDAAddress.Parse(s); Assert.AreEqual(expected, result); }
public void PCodeAddrZeros() { IrDAAddress addr = new IrDAAddress(0); DoTest("00.00.00.00", addr, FcodePeriods); }
public void PCodeAddrA() { IrDAAddress addr = new IrDAAddress(0x01020304); DoTest("01.02.03.04", addr, FcodePeriods); }
public void CCodeAddrZeros() { IrDAAddress addr = new IrDAAddress(0); DoTest("00:00:00:00", addr, FcodeColons); }
public void CCodeAddrA() { IrDAAddress addr = new IrDAAddress(0x01020304); DoTest("01:02:03:04", addr, FcodeColons); }
public void NCodeAddrZeros() { IrDAAddress addr = new IrDAAddress(0); DoTest("00000000", addr, FcodePlainNumbers); }
public void AddrAllOnes() { IrDAAddress addr = new IrDAAddress(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }); DoTest("FFFFFFFF", addr); }
public void EmptyCodeAddrZeros() { IrDAAddress addr = new IrDAAddress(0); DoTest("00000000", addr, FcodeEmpty); }
public void AddrB() { IrDAAddress addr = new IrDAAddress(new byte[] { 0x01, 0x02, 0x03, 0x04 }); DoTest("04030201", addr); }
public static IrDADeviceInfo[] ParseDeviceList(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } const int BytesInAnInt32 = 4; if (buffer.Length < BytesInAnInt32) { throw new ArgumentException("DEVICE_LIST buffer must be at least four bytes long."); } // int count = BitConverter.ToInt32(buffer, 0); //create array for results IrDADeviceInfo[] idia = new IrDADeviceInfo[count]; const int devInfoLen = 29; //We could check that the buffer is big enough to suit its 'count' value. //If this ever happened currently we would just fail with IndexOutOfRangeException //int expectedLength = BytesInAnInt32 + count * devInfoLen; //if (expectedLength > buffer.Length) { // throw new ArgumentException("Buffer is smaller than the count of items requires."); //} for (int iDev = 0; iDev < count; iDev++) { byte[] id = new byte[4]; Buffer.BlockCopy(buffer, 4 + (iDev * devInfoLen), id, 0, 4); IrDAAddress devid = new IrDAAddress(id); //hints IrDAHints hints = (IrDAHints)BitConverter.ToInt16(buffer, 30 + (iDev * devInfoLen)); //charset IrDACharacterSet charset = (IrDACharacterSet)buffer[32 + (iDev * devInfoLen)]; //name Encoding e = null; switch (charset) { case IrDACharacterSet.ASCII: e = Encoding.ASCII; break; case IrDACharacterSet.Unicode: e = Encoding.Unicode; break; default: e = Encoding.GetEncoding(28590 + (int)charset); break; } string name = e.GetString(buffer, 8 + (iDev * devInfoLen), 22); int nullIndex = name.IndexOf('\0'); //trim nulls if (nullIndex > -1) { name = name.Substring(0, nullIndex); } #if NETCF // PPC doesn't fill the charset field! :-( We'll attempt // to detect the Unicode encoding, but not the ISO-8859-X ones: // as their strings will display at least partially -- dropping // the high chars, but also because those encodings are not // really supported by CF anyway. if (Environment.OSVersion.Platform == PlatformID.WinCE) { // This assert is valid, but very annoying when running the // unit-tests so we'll remove it for now. // System.Diagnostics.Debug.Assert(charset == 0, "should charset==0 as field unset on CE"); try { e = Encoding.Unicode; string nameGuessUnicode = e.GetString(buffer, 8 + (iDev * devInfoLen), 22); //trim nulls int nullIndexGU = nameGuessUnicode.IndexOf('\0'); if (nullIndexGU > -1) { nameGuessUnicode = nameGuessUnicode.Substring(0, nullIndexGU); } // If more sense was made of the bytes as Unicode, then return // that string. // e.g. a unicode string "abc" is 61-00-62-00-63-00 which // ASCII will see as "A" and Unicode as "ABC" if (nameGuessUnicode.Length > name.Length) { name = nameGuessUnicode; } } catch { } } #endif idia[iDev] = new IrDADeviceInfo(devid, name, hints, charset); } return(idia); }
public void BadCode() { IrDAAddress addr = new IrDAAddress(0x01020304); DoTest("01020304", addr, FcodeNotExists); }
public void AddrNone() { IrDAAddress addr = IrDAAddress.None; DoTest("00000000", addr); }
public void AddrNegativeOne() { IrDAAddress addr = new IrDAAddress(-1); DoTest("FFFFFFFF", addr); }
public void AddrA() { IrDAAddress addr = new IrDAAddress(0x01020304); DoTest("01020304", addr); }
public void NCodeAddrA() { IrDAAddress addr = new IrDAAddress(0x01020304); DoTest("01020304", addr, FcodePlainNumbers); }
public void EmptyCodeAddrA() { IrDAAddress addr = new IrDAAddress(0x01020304); DoTest("01020304", addr, FcodeEmpty); }
public void AddrZeros() { IrDAAddress addr = new IrDAAddress(0); DoTest("00000000", addr); }
public void NullCodeAddrA() { IrDAAddress addr = new IrDAAddress(0x01020304); DoTest("01020304", addr, FcodeNull); }
public static IrDADeviceInfo[] ParseDeviceList(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } const int BytesInAnInt32 = 4; if (buffer.Length < BytesInAnInt32) { throw new ArgumentException("DEVICE_LIST buffer must be at least four bytes long."); } // int count = BitConverter.ToInt32(buffer, 0); //create array for results IrDADeviceInfo[] idia = new IrDADeviceInfo[count]; const int devInfoLen = 29; //We could check that the buffer is big enough to suit its 'count' value. //If this ever happened currently we would just fail with IndexOutOfRangeException //int expectedLength = BytesInAnInt32 + count * devInfoLen; //if (expectedLength > buffer.Length) { // throw new ArgumentException("Buffer is smaller than the count of items requires."); //} for (int iDev = 0; iDev < count; iDev++) { byte[] id = new byte[4]; Buffer.BlockCopy(buffer, 4 + (iDev * devInfoLen), id, 0, 4); IrDAAddress devid = new IrDAAddress(id); //hints IrDAHints hints = (IrDAHints)BitConverter.ToInt16(buffer, 30 + (iDev * devInfoLen)); //charset IrDACharacterSet charset = (IrDACharacterSet)buffer[32 + (iDev * devInfoLen)]; //name Encoding e = Encoding.ASCII; switch (charset) { case IrDACharacterSet.ASCII: e = Encoding.ASCII; break; case IrDACharacterSet.Unicode: e = Encoding.Unicode; break; default: e = Encoding.GetEncoding(28590 + (int)charset); break; } string name = e.GetString(buffer, 8 + (iDev * devInfoLen), 22); int nullIndex = name.IndexOf('\0'); // trim nulls if (nullIndex > -1) { name = name.Substring(0, nullIndex); } idia[iDev] = new IrDADeviceInfo(devid, name, hints, charset); } return(idia); }
public void NullCodeAddrZeros() { IrDAAddress addr = new IrDAAddress(0); DoTest("00000000", addr, FcodeNull); }
public static IrDADeviceInfo[] ParseDeviceList(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } const int BytesInAnInt32 = 4; if (buffer.Length < BytesInAnInt32) { throw new ArgumentException("DEVICE_LIST buffer must be at least four bytes long."); } // int count = BitConverter.ToInt32(buffer, 0); //create array for results IrDADeviceInfo[] idia = new IrDADeviceInfo[count]; const int devInfoLen = 29; //We could check that the buffer is big enough to suit its 'count' value. //If this ever happened currently we would just fail with IndexOutOfRangeException //int expectedLength = BytesInAnInt32 + count * devInfoLen; //if (expectedLength > buffer.Length) { // throw new ArgumentException("Buffer is smaller than the count of items requires."); //} for(int iDev = 0; iDev < count; iDev++) { byte[] id = new byte[4]; Buffer.BlockCopy(buffer, 4 + (iDev*devInfoLen), id, 0, 4); IrDAAddress devid = new IrDAAddress(id); //hints IrDAHints hints = (IrDAHints)BitConverter.ToInt16(buffer, 30 + (iDev * devInfoLen)); //charset IrDACharacterSet charset = (IrDACharacterSet)buffer[32 + (iDev * devInfoLen)]; //name Encoding e = null; switch (charset) { case IrDACharacterSet.ASCII: e = Encoding.ASCII; break; case IrDACharacterSet.Unicode: e = Encoding.Unicode; break; default: e = Encoding.GetEncoding(28590 + (int)charset); break; } string name = e.GetString(buffer, 8 + (iDev*devInfoLen), 22); int nullIndex = name.IndexOf('\0'); //trim nulls if(nullIndex > -1) { name = name.Substring(0, nullIndex); } #if PocketPC // PPC doesn't fill the charset field! :-( We'll attempt // to detect the Unicode encoding, but not the ISO-8859-X ones: // as their strings will display at least partially -- dropping // the high chars, but also because those encodings are not // really supported by CF anyway. if (Environment.OSVersion.Platform == PlatformID.WinCE) { // This assert is valid, but very annoying when running the // unit-tests so we'll remove it for now. // System.Diagnostics.Debug.Assert(charset == 0, "should charset==0 as field unset on CE"); try { e = Encoding.Unicode; string nameGuessUnicode = e.GetString(buffer, 8 + (iDev * devInfoLen), 22); //trim nulls int nullIndexGU = nameGuessUnicode.IndexOf('\0'); if (nullIndexGU > -1) { nameGuessUnicode = nameGuessUnicode.Substring(0, nullIndexGU); } // If more sense was made of the bytes as Unicode, then return // that string. // e.g. a unicode string "abc" is 61-00-62-00-63-00 which // ASCII will see as "A" and Unicode as "ABC" if (nameGuessUnicode.Length > name.Length) { name = nameGuessUnicode; } } catch { } } #endif idia[iDev] = new IrDADeviceInfo(devid, name, hints, charset); } return idia; }