public void MutationNotAllowedViaDeviceID() { byte[] addr = new byte[] { 1, 2, 3, 4 }; String name = "abcdef"; byte[] hints = new byte[] { 5, 6 }; byte charset = 0x01; #if NETCF charset = 0x00; // Latin-1 not supported on CF(?). #endif IrDADeviceInfo Device1 = Factory_IrDADeviceInfo(addr, name, hints, charset); // Assert.AreEqual(new IrDAAddress(addr), Device1.DeviceAddress); // #if FX1_1 /* #endif #pragma warning disable 618 #if FX1_1 */ #endif byte[] internalBytes = Device1.DeviceID; // Don't fix the 'Obsolete' warning! #if FX1_1 /* #endif #pragma warning restore 618 #if FX1_1 */ #endif Assert.AreEqual(addr, internalBytes); internalBytes[1] = 0xFF; // Attempt to mutate the Address!!! // Assert.AreEqual(new IrDAAddress(addr), Device1.DeviceAddress); }
private void btnBeam_Click(object sender, System.EventArgs e) { if (cbDevices.SelectedIndex > -1) { IrDADeviceInfo idi = (IrDADeviceInfo)cbDevices.SelectedItem; if (ofdFileToBeam.ShowDialog() == DialogResult.OK) { Cursor.Current = Cursors.WaitCursor; System.Uri uri = new Uri("obex://" + idi.DeviceAddress.ToString() + "/" + System.IO.Path.GetFileName(ofdFileToBeam.FileName)); ObexWebRequest request = new ObexWebRequest(uri); Stream requestStream = request.GetRequestStream(); FileStream fs = File.OpenRead(ofdFileToBeam.FileName); byte[] buffer = new byte[1024]; int readBytes = 1; while (readBytes != 0) { readBytes = fs.Read(buffer, 0, buffer.Length); requestStream.Write(buffer, 0, readBytes); } requestStream.Close(); ObexWebResponse response = (ObexWebResponse)request.GetResponse(); MessageBox.Show(response.StatusCode.ToString()); response.Close(); Cursor.Current = Cursors.Default; } } }
// Function with recieve/send string endless loop on client socket void ClientFn() { Socket socket = null; try { IrDADeviceInfo dev = selectedDevice; if (dev == null) { Log("Device not selected"); return; } IrDAEndPoint ep = new IrDAEndPoint (new byte[] { 0, 0, 0, 0 }, "Pop"); Socket socket = new Socket (AddressFamily.Irda, SocketType.Stream, 0); IrDAEndPoint ep2 = new IrDAEndPoint(dev.DeviceID, ep.ServiceName); socket.Connect(ep2); NetworkStream stream = new NetworkStream(socket, true); BinaryWriter bw = new BinaryWriter(stream); BinaryReader br = new BinaryReader(stream); int no = 0; while (true) { Log("Reading"); Log(br.ReadString()); Log("Writing"); bw.Write("hello no. " + (++no) + " from client"); Log("OK"); } } catch (Exception ex) { Log("Error while running client"); Log(ex.ToString()); } finally { if (socket != null) { socket.Close(); } } }
public void EqualsDifferentInstancesAllSameFields() { byte[] addr = new byte[] { 1, 2, 3, 4 }; String name = "abcdef"; byte[] hints = new byte[] { 5, 6 }; byte charset = 0x01; #if NETCF charset = 0x00; // Latin-1 not supported on CF(?). #endif IrDADeviceInfo Device1 = Factory_IrDADeviceInfo(addr, name, hints, charset); IrDADeviceInfo Device2 = Factory_IrDADeviceInfo(addr, name, hints, charset); Assert.AreNotSame(Device1, Device2); Assert.AreEqual(Device1, Device2); }
private void bConnect_Click(object sender, EventArgs e) { DevItem item = lbDevices.SelectedItem as DevItem; if (item == null) { MessageBox.Show("Discover and select device first!"); return; } selectedDevice = item.Device; tbLog.Text = ""; Log("Connecting to " + item.ToString()); Thread thread = new Thread(new ThreadStart(ClientFn)); thread.Start(); }
public void EqualsDifferentInstancesOnlyAddressSame() { byte[] addr = new byte[] { 1, 2, 3, 4 }; String name1 = "abcdef"; String name2 = "zzzzzz"; byte[] hints1 = new byte[] { 15, 16 }; byte[] hints2 = new byte[] { 12, 2 }; byte charset1 = 0x00; byte charset2 = 0x01; #if NETCF charset2 = 0x00; // Latin-1 not supported on CF(?). #endif IrDADeviceInfo Device1 = Factory_IrDADeviceInfo(addr, name1, hints1, charset1); IrDADeviceInfo Device2 = Factory_IrDADeviceInfo(addr, name2, hints2, charset2); Assert.AreNotSame(Device1, Device2); Assert.AreEqual(Device1, Device2); }
public void MutationNotAllowedViaDeviceAddressToByteArray() { byte[] addr = new byte[] { 1, 2, 3, 4 }; String name = "abcdef"; byte[] hints = new byte[] { 5, 6 }; byte charset = 0x01; #if NETCF charset = 0x00; // Latin-1 not supported on CF(?). #endif IrDADeviceInfo Device1 = Factory_IrDADeviceInfo(addr, name, hints, charset); // Assert.AreEqual(new IrDAAddress(addr), Device1.DeviceAddress); // byte[] internalBytes = Device1.DeviceAddress.ToByteArray(); Assert.AreEqual(addr, internalBytes); internalBytes[1] = 0xFF; // Attempt to mutate the Address!!! // Assert.AreEqual(new IrDAAddress(addr), Device1.DeviceAddress); }
public void AssertAreEqualIrDADeviceInfoEveryField(IrDADeviceInfo x, IrDADeviceInfo y) { Assert.AreEqual(x.DeviceName, y.DeviceName); Assert.AreEqual(x.DeviceAddress, y.DeviceAddress); // Check the byte array version too, so not depending on IrDAAddress.Equals. #if FX1_1 /* #endif #pragma warning disable 618 #if FX1_1 */ #endif Assert.AreEqual(x.DeviceID, y.DeviceID); // Don't fix the 'Obsolete' warning! #if FX1_1 /* #endif #pragma warning restore 618 #if FX1_1 */ #endif Assert.AreEqual(x.Hints, y.Hints); Assert.AreEqual(x.CharacterSet, y.CharacterSet); }
public static IrDADeviceInfo Factory_IrDADeviceInfo(byte[] address, String name, byte[] hints, byte charset) { IrDADeviceInfo item = TestHolderIrDADeviceInfo.CreateDeviceListInfo(address, name, hints, charset); return(item); }
public DevItem(IrDADeviceInfo device) { this.Device = device; }
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; }