public void OneNoExtAttr() { var mocks = new Mockery(); var hackApi = mocks.NewMock <IBluesoleilApi>(); SetupNoSearchAppExtSPPService(hackApi); Expect.Never.On(hackApi).Method("Btsdk_FreeMemory"); // byte[] setSvcName = { (byte)'a', /* \u00E4 */ 0xC3, 0xA4, (byte)'b', (byte)'b', (byte)'c', (byte)'c', (byte)'d', (byte)'d' }; var attrs = new Structs.BtSdkRemoteServiceAttrStru(0, 0x1112, setSvcName, IntPtr.Zero); ServiceRecord sr = BluesoleilDeviceInfo.CreateServiceRecord(ref attrs, hackApi); // const string NewLine = "\r\n"; const string expectedDump = "AttrId: 0x0001 -- ServiceClassIdList" + NewLine + "ElementSequence" + NewLine + " Uuid16: 0x1112 -- HeadsetAudioGateway" + NewLine + NewLine + "AttrId: 0x0006 -- LanguageBaseAttributeIdList" + NewLine + "ElementSequence" + NewLine + " UInt16: 0x656E" + NewLine + " UInt16: 0x6A" + NewLine + " UInt16: 0x100" + NewLine + NewLine + "AttrId: 0x0100 -- ServiceName" + NewLine + "TextString: [en] 'a\u00E4bbccdd'" + NewLine + NewLine + "AttrId: 0xFFFF" + NewLine + "TextString (guessing UTF-8): '<partial BlueSoleil decode>'" + NewLine ; string dump = ServiceRecordUtilities.Dump(sr); Assert.AreEqual(expectedDump, dump, "dump"); // ServiceElement e; // e = sr.GetAttributeById(UniversalAttributeId.ServiceClassIdList).Value; ServiceElement eSvcClass = e.GetValueAsElementArray()[0]; UInt16 svcC = (UInt16)eSvcClass.Value; Assert.AreEqual(0x1112, svcC, "svcC"); // string sn = sr.GetPrimaryMultiLanguageStringAttributeById(UniversalAttributeId.ServiceName); Assert.AreEqual("a\u00E4bbccdd", sn, "sn"); //---- mocks.VerifyAllExpectationsHaveBeenMet(); }
public void OneNoExtAttr_ManualMock() { var hackApi = new TestSdBluesoleilApi(); byte[] setSvcName = Encoding.UTF8.GetBytes("aabbccdd"); var attrs = new Structs.BtSdkRemoteServiceAttrStru(0, 0x1112, setSvcName, IntPtr.Zero); ServiceRecord sr = BluesoleilDeviceInfo.CreateServiceRecord(ref attrs, hackApi); // const string NewLine = "\r\n"; const string expectedDump = "AttrId: 0x0001 -- ServiceClassIdList" + NewLine + "ElementSequence" + NewLine + " Uuid16: 0x1112 -- HeadsetAudioGateway" + NewLine + NewLine + "AttrId: 0x0006 -- LanguageBaseAttributeIdList" + NewLine + "ElementSequence" + NewLine + " UInt16: 0x656E" + NewLine + " UInt16: 0x6A" + NewLine + " UInt16: 0x100" + NewLine + NewLine + "AttrId: 0x0100 -- ServiceName" + NewLine + "TextString: [en] 'aabbccdd'" + NewLine + NewLine + "AttrId: 0xFFFF" + NewLine + "TextString (guessing UTF-8): '<partial BlueSoleil decode>'" + NewLine ; string dump = ServiceRecordUtilities.Dump(sr); Assert.AreEqual(expectedDump, dump, "dump"); // ServiceElement e; // e = sr.GetAttributeById(UniversalAttributeId.ServiceClassIdList).Value; ServiceElement eSvcClass = e.GetValueAsElementArray()[0]; UInt16 svcC = (UInt16)eSvcClass.Value; Assert.AreEqual(0x1112, svcC, "svcC"); // string sn = sr.GetPrimaryMultiLanguageStringAttributeById(UniversalAttributeId.ServiceName); Assert.AreEqual("aabbccdd", sn, "sn"); //---- hackApi.AssertActionsNoMore(); }
private void OnDeviceLost(BluesoleilDeviceInfo deviceInfo) { lookupDeviceInfo.Remove(deviceInfo.Address); foundDevices.Remove(deviceInfo); OnDeviceLost(new DeviceInfoEventArgs(deviceInfo)); }
private void OnDeviceFound(BluesoleilDeviceInfo deviceInfo) { lookupDeviceInfo.Add(deviceInfo.Address, deviceInfo); foundDevices.Add(deviceInfo); OnDeviceFound(new DeviceInfoEventArgs(deviceInfo)); }
protected void Discovering() { while (discoveringThread == Thread.CurrentThread) { MsHid.NativeMethods.WSAStartup(); bluesoleil = BluesoleilService.Instance; bluesoleil.Initialize(); bluesoleil.ConnectionClosed += OnConnectionClosed; while (discoveringThread == Thread.CurrentThread) { EnsureBluesoleilStarted(bluesoleil); BluetoothDevice[] devices; try { // Scan for bluetooth-devices (like devices). devices = bluesoleil.InquireDevices(pollingTime); } catch (BluesoleilFailException) { // Happens sometimes randomly, but also happens sometimes when the bluetooth-dongle is unplugged. continue; } catch (BluesoleilNotReadyException) { // Happens when bluetooth is stopped or when the bluetooth-dongle is unplugged. Thread.Sleep(NotReadySleepTimeout); continue; } List<BluetoothAddress> notFoundAddresses = new List<BluetoothAddress>(lookupDeviceInfo.Keys); foreach (BluetoothDevice device in devices) { if (!IsWiiDevice(device)) continue; BluetoothAddress address = new BluetoothAddress(device.Address); if (lookupDeviceInfo.ContainsKey(address)) { notFoundAddresses.Remove(address); break; } BluetoothService[] services = null; try { // Scan for bluetooth-devices (like devices). services = bluesoleil.BrowseServices(device); Thread.Sleep(PostBrowseSleepTimeout); } catch (BluesoleilFailException) { // Happens sometimes randomly, but also happens sometimes when the bluetooth-dongle is unplugged. continue; } catch (BluesoleilNotReadyException) { // Happens when bluetooth is stopped or when the bluetooth-dongle is unplugged. continue; } if (services.Length != 3) continue; if (!lookupDeviceInfo.ContainsKey(address)) { BluesoleilDeviceInfo foundDevice = new BluesoleilDeviceInfo(device, services[1]); OnDeviceFound(foundDevice); } } // Remove the lost devices from the list and notify DeviceLost event. foreach (BluetoothAddress notFoundAddress in notFoundAddresses) { BluesoleilDeviceInfo notFoundDeviceInfo = lookupDeviceInfo[notFoundAddress]; OnDeviceLost(notFoundDeviceInfo); } Thread.Sleep(PollSleepTimeout); } bluesoleil.ConnectionClosed -= OnConnectionClosed; bluesoleil.Dispose(); } }
public void DeviceIdExtAttr_NoServiceName() { var mocks = new Mockery(); var hackApi = mocks.NewMock <IBluesoleilApi>(); SetupNoSearchAppExtSPPService(hackApi); // var ext = new Structs.BtSdkRmtDISvcExtAttrStru(1, 2, 3, 4, true, 6, 0); IntPtr pExt = Marshal.AllocHGlobal(Marshal.SizeOf(ext)); Marshal.StructureToPtr(ext, pExt, false); Expect.Once.On(hackApi).Method("Btsdk_FreeMemory").With(pExt); // byte[] setSvcName = new byte[0]; const int SvcClassPnp = 0x1200; var attrs = new Structs.BtSdkRemoteServiceAttrStru(0, SvcClassPnp, setSvcName, pExt); ServiceRecord sr = BluesoleilDeviceInfo.CreateServiceRecord(ref attrs, hackApi); // const string NewLine = "\r\n"; const string expectedDump = "AttrId: 0x0001 -- ServiceClassIdList" + NewLine + "ElementSequence" + NewLine + " Uuid16: 0x1200 -- PnPInformation" + NewLine + NewLine // + "AttrId: 0x0200 -- SpecificationId" + NewLine + "UInt16: 0x1" + NewLine + NewLine + "AttrId: 0x0201 -- VendorId" + NewLine + "UInt16: 0x2" + NewLine + NewLine + "AttrId: 0x0202 -- ProductId" + NewLine + "UInt16: 0x3" + NewLine + NewLine + "AttrId: 0x0203 -- Version" + NewLine + "UInt16: 0x4" + NewLine + NewLine + "AttrId: 0x0204 -- PrimaryRecord" + NewLine + "Boolean: True" + NewLine + NewLine + "AttrId: 0x0205 -- VendorIdSource" + NewLine + "UInt16: 0x6" + NewLine + NewLine // + "AttrId: 0xFFFF" + NewLine + "TextString (guessing UTF-8): '<partial BlueSoleil decode>'" + NewLine ; string dump = ServiceRecordUtilities.Dump(sr, typeof(DeviceIdProfileAttributeId)); Assert.AreEqual(expectedDump, dump, "dump"); // ServiceElement e; // e = sr.GetAttributeById(UniversalAttributeId.ServiceClassIdList).Value; ServiceElement eSvcClass = e.GetValueAsElementArray()[0]; UInt16 svcC = (UInt16)eSvcClass.Value; Assert.AreEqual(0x1200, svcC, "svcC"); // //---- mocks.VerifyAllExpectationsHaveBeenMet(); }
//Not supported! Can't tell between little-endian and big-endian Unicode! //[TestMethod] public void OneNoExtAttr_PanuBeUnicodeSvcName() { var mocks = new Mockery(); var hackApi = mocks.NewMock <IBluesoleilApi>(); SetupNoSearchAppExtSPPService(hackApi); Expect.Never.On(hackApi).Method("Btsdk_FreeMemory"); // byte[] setSvcName = { 0x00, 0x50, 0x00, 0x65, 0x00, 0x72, 0x00, 0x73, 0x00, 0x6F, 0x00, 0x6E, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x20, 0x00, 0x41, 0x00, 0x64, 0x00, 0x2D, 0x00, 0x68, 0x00, 0x6F, 0x00, 0x63, 0x00, 0x20, 0x00, 0x55, 0x00, 0x73, 0x00, 0x65, 0x00, 0x72, 0x00, 0x20, 0x00, 0x53, 0x00, 0x65, 0x00, 0x72, 0x00, 0x76, 0x00, 0x69, 0x00, 0x63, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; ushort SvcClassPanu = 0x1115; var attrs = new Structs.BtSdkRemoteServiceAttrStru(0, SvcClassPanu, setSvcName, IntPtr.Zero); ServiceRecord sr = BluesoleilDeviceInfo.CreateServiceRecord(ref attrs, hackApi); // const string NewLine = "\r\n"; const string expectedDump = "AttrId: 0x0001 -- ServiceClassIdList" + NewLine + "ElementSequence" + NewLine + " Uuid16: 0x1115 -- Panu" + NewLine + NewLine + "AttrId: 0x0006 -- LanguageBaseAttributeIdList" + NewLine + "ElementSequence" + NewLine + " UInt16: 0x656E" + NewLine + " UInt16: 0x6A" + NewLine + " UInt16: 0x100" + NewLine + NewLine + "AttrId: 0x0100 -- ServiceName" + NewLine + "TextString: [en] 'Personal Ad-hoc User Service'" + NewLine + NewLine + "AttrId: 0xFFFF" + NewLine + "TextString (guessing UTF-8): '<partial BlueSoleil decode>'" + NewLine ; string dump = ServiceRecordUtilities.Dump(sr); Assert.AreEqual(expectedDump, dump, "dump"); // ServiceElement e; // e = sr.GetAttributeById(UniversalAttributeId.ServiceClassIdList).Value; ServiceElement eSvcClass = e.GetValueAsElementArray()[0]; UInt16 svcC = (UInt16)eSvcClass.Value; Assert.AreEqual(0x1115, svcC, "svcC"); // string sn = sr.GetPrimaryMultiLanguageStringAttributeById(UniversalAttributeId.ServiceName); Assert.AreEqual("Personal Ad-hoc User Service", sn, "sn"); //---- mocks.VerifyAllExpectationsHaveBeenMet(); }
public void OneSppExtAttr_FullLenServiceName() { var mocks = new Mockery(); var hackApi = mocks.NewMock <IBluesoleilApi>(); SetupNoSearchAppExtSPPService(hackApi); // const byte Port = 23; var ext = new Structs.BtSdkRmtSPPSvcExtAttrStru(Port); IntPtr pExt = Marshal.AllocHGlobal(Marshal.SizeOf(ext)); Marshal.StructureToPtr(ext, pExt, false); Expect.Once.On(hackApi).Method("Btsdk_FreeMemory").With(pExt); // Debug.Assert(80 == StackConsts.BTSDK_SERVICENAME_MAXLENGTH, "BTSDK_SERVICENAME_MAXLENGTH: " + StackConsts.BTSDK_SERVICENAME_MAXLENGTH); byte[] setSvcName = new byte[StackConsts.BTSDK_SERVICENAME_MAXLENGTH]; for (int i = 0; i < (setSvcName.Length - 1); ++i) { setSvcName[i] = (byte)'a'; } setSvcName[setSvcName.Length - 1] = (byte)'b'; var attrs = new Structs.BtSdkRemoteServiceAttrStru(0, 0x1112, setSvcName, pExt); ServiceRecord sr = BluesoleilDeviceInfo.CreateServiceRecord(ref attrs, hackApi); // const string NewLine = "\r\n"; const string expectedDump = "AttrId: 0x0001 -- ServiceClassIdList" + NewLine + "ElementSequence" + NewLine + " Uuid16: 0x1112 -- HeadsetAudioGateway" + NewLine + NewLine + "AttrId: 0x0004 -- ProtocolDescriptorList" + NewLine + "ElementSequence" + NewLine + " ElementSequence" + NewLine + " Uuid16: 0x100 -- L2CapProtocol" + NewLine + " ElementSequence" + NewLine + " Uuid16: 0x3 -- RFCommProtocol" + NewLine + " UInt8: 0x17" + NewLine + "( ( L2Cap ), ( Rfcomm, ChannelNumber=23 ) )" + NewLine + NewLine + "AttrId: 0x0006 -- LanguageBaseAttributeIdList" + NewLine + "ElementSequence" + NewLine + " UInt16: 0x656E" + NewLine + " UInt16: 0x6A" + NewLine + " UInt16: 0x100" + NewLine + NewLine + "AttrId: 0x0100 -- ServiceName" + NewLine + "TextString: [en] 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab'" + NewLine + NewLine + "AttrId: 0xFFFF" + NewLine + "TextString (guessing UTF-8): '<partial BlueSoleil decode>'" + NewLine ; string dump = ServiceRecordUtilities.Dump(sr); Assert.AreEqual(expectedDump, dump, "dump"); // ServiceElement e; // e = sr.GetAttributeById(UniversalAttributeId.ServiceClassIdList).Value; ServiceElement eSvcClass = e.GetValueAsElementArray()[0]; UInt16 svcC = (UInt16)eSvcClass.Value; Assert.AreEqual(0x1112, svcC, "svcC"); // e = sr.GetAttributeById(UniversalAttributeId.ProtocolDescriptorList).Value; ServiceElement listRfcomm = e.GetValueAsElementArray()[1]; ServiceElement eScn = listRfcomm.GetValueAsElementArray()[1]; byte scn = (byte)eScn.Value; Assert.AreEqual(Port, scn, "scn"); // string sn = sr.GetPrimaryMultiLanguageStringAttributeById(UniversalAttributeId.ServiceName); Assert.AreEqual("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", sn, "sn"); //---- mocks.VerifyAllExpectationsHaveBeenMet(); }
public void OneSppExtAttr_ManualMock() { var hackApi = new TestSdBluesoleilApi(); // const byte Port = 23; var ext = new Structs.BtSdkRmtSPPSvcExtAttrStru(Port); IntPtr pExt = Marshal.AllocHGlobal(Marshal.SizeOf(ext)); Marshal.StructureToPtr(ext, pExt, false); // byte[] setSvcName = Encoding.UTF8.GetBytes("aabbccdd"); var attrs = new Structs.BtSdkRemoteServiceAttrStru(0, 0x1112, setSvcName, pExt); ServiceRecord sr = BluesoleilDeviceInfo.CreateServiceRecord(ref attrs, hackApi); // const string NewLine = "\r\n"; const string expectedDump = "AttrId: 0x0001 -- ServiceClassIdList" + NewLine + "ElementSequence" + NewLine + " Uuid16: 0x1112 -- HeadsetAudioGateway" + NewLine + NewLine + "AttrId: 0x0004 -- ProtocolDescriptorList" + NewLine + "ElementSequence" + NewLine + " ElementSequence" + NewLine + " Uuid16: 0x100 -- L2CapProtocol" + NewLine + " ElementSequence" + NewLine + " Uuid16: 0x3 -- RFCommProtocol" + NewLine + " UInt8: 0x17" + NewLine + "( ( L2Cap ), ( Rfcomm, ChannelNumber=23 ) )" + NewLine + NewLine + "AttrId: 0x0006 -- LanguageBaseAttributeIdList" + NewLine + "ElementSequence" + NewLine + " UInt16: 0x656E" + NewLine + " UInt16: 0x6A" + NewLine + " UInt16: 0x100" + NewLine + NewLine + "AttrId: 0x0100 -- ServiceName" + NewLine + "TextString: [en] 'aabbccdd'" + NewLine + NewLine + "AttrId: 0xFFFF" + NewLine + "TextString (guessing UTF-8): '<partial BlueSoleil decode>'" + NewLine ; string dump = ServiceRecordUtilities.Dump(sr); Assert.AreEqual(expectedDump, dump, "dump"); // ServiceElement e; // e = sr.GetAttributeById(UniversalAttributeId.ServiceClassIdList).Value; ServiceElement eSvcClass = e.GetValueAsElementArray()[0]; UInt16 svcC = (UInt16)eSvcClass.Value; Assert.AreEqual(0x1112, svcC, "svcC"); // e = sr.GetAttributeById(UniversalAttributeId.ProtocolDescriptorList).Value; ServiceElement listRfcomm = e.GetValueAsElementArray()[1]; ServiceElement eScn = listRfcomm.GetValueAsElementArray()[1]; byte scn = (byte)eScn.Value; Assert.AreEqual(Port, scn, "scn"); // string sn = sr.GetPrimaryMultiLanguageStringAttributeById(UniversalAttributeId.ServiceName); Assert.AreEqual("aabbccdd", sn, "sn"); //---- hackApi.AssertActionsInOrder("Btsdk_FreeMemory", pExt); hackApi.AssertActionsNoMore(); }