//---- internal void HandleInquiryEvent(uint BluetoothStackID, ref Structs.GAP_Event_Data GAP_Event_Data, uint CallbackParameter) { Debug.Assert(GAP_Event_Data.Event_Data_Type == StackConsts.GAP_Event_Type.Inquiry_Entry_Result || GAP_Event_Data.Event_Data_Type == StackConsts.GAP_Event_Type.Inquiry_Result, "Unexpected event type: " + GAP_Event_Data.Event_Data_Type); // Structs.GAP_Inquiry_Entry_Event_Data gapInquiryEntryEventData; Structs.GAP_Inquiry_Event_Data gapInquiryEventData; // if (GAP_Event_Data.Event_Data_Type == StackConsts.GAP_Event_Type.Inquiry_Entry_Result) { gapInquiryEntryEventData = (Structs.GAP_Inquiry_Entry_Event_Data)Marshal.PtrToStructure( GAP_Event_Data.pData, typeof(Structs.GAP_Inquiry_Entry_Event_Data)); // TODO ThreadPool??? -- need to copy first if so. HandleInquiryResultInd(gapInquiryEntryEventData); } else { gapInquiryEventData = (Structs.GAP_Inquiry_Event_Data)Marshal.PtrToStructure( GAP_Event_Data.pData, typeof(Structs.GAP_Inquiry_Event_Data)); // TODO ThreadPool??? -- need to copy first if so. HandleInquiryCompleteInd(gapInquiryEventData); } }
void HandleNameLookup(uint BluetoothStackID, ref Structs.GAP_Event_Data GAP_Event_Data, uint CallbackParameter) { try { HandleNameLookup2(BluetoothStackID, ref GAP_Event_Data, CallbackParameter); } catch (Exception ex) { Utils.MiscUtils.Trace_WriteLine("Exception from our HandleNameLookup2!!!: " + ex); } }
private void HandleAuthenticate_Callback(uint BluetoothStackID, ref Structs.GAP_Event_Data GAP_Event_Data, uint CallbackParameter) { // Don't allow an exception to travel back to the BTPS stack! try { HandleAuthenticate_Callback2(BluetoothStackID, ref GAP_Event_Data, CallbackParameter); } catch (Exception ex) { Utils.MiscUtils.Trace_WriteLine("Exception in our HandleAuthenticate_Callback2: " + ex); } }
void HandleNameLookup2(uint BluetoothStackID, ref Structs.GAP_Event_Data GAP_Event_Data, uint CallbackParameter) { Debug.Assert(GAP_Event_Data.Event_Data_Type == StackConsts.GAP_Event_Type.Remote_Name_Result, "Unexpected event type: " + GAP_Event_Data.Event_Data_Type); // Structs.GAP_Remote_Name_Event_Data data; // data = (Structs.GAP_Remote_Name_Event_Data)Marshal.PtrToStructure( GAP_Event_Data.pData, typeof(Structs.GAP_Remote_Name_Event_Data)); var addr = BluetopiaUtils.ToBluetoothAddress(data._Remote_Device); Debug.WriteLine("GAP_Remote_Name_Event_Data: status: " + data._Remote_Name_Status + ", addr: " + addr); if (data._Remote_Name_Status != 0) { return; } // TODO ThreadPool??? but need to marshal data._Remote_Name first. List <BluetopiaDeviceInfo> queryList; lock (_lockDevices) { var got = _nameQueryList.TryGetValue(addr, out queryList); var gotR = _nameQueryList.Remove(addr); } // TO-DO if (list == null) return; var arr = Widcomm.WidcommUtils.GetByteArrayNullTerminated(data._Remote_Name, 250); var name = BluetopiaUtils.FromNameString(arr); Debug.WriteLine(" name: " + name); if (queryList == null) { return; // duplicate above exit. } ThreadPool.QueueUserWorkItem( delegate { foreach (var cur in queryList) { cur.SetName(name); } //_inquiryHandler.GotNameManually(addr, name); AddNamedKnownDevice(addr, name); }); }
public void One() { var stuff = BluetopiaTesting.InitMockery_Client(new ClientTestingBluetopia.Behaviour()); // const uint NullPeriodLengths = 0; const uint InquiryLen = 12; const uint CallbackParameter = 0; const uint MaximumResponses = 255; var callback = stuff.GetFactory()._inquiryEventCallback; Expect.Once.On(stuff.MockedApi).Method("GAP_Perform_Inquiry") .With(stuff.StackId, StackConsts.GAP_Inquiry_Type.GeneralInquiry, NullPeriodLengths, NullPeriodLengths, InquiryLen, MaximumResponses, callback, CallbackParameter) .Will(Return.Value(BluetopiaError.OK)); // IBluetoothClient cli = stuff.CreateBluetoothClient(); var ar = cli.BeginDiscoverDevices(255, false, false, false, true, null, null); Assert.IsFalse(ar.IsCompleted, "IsCompleted 0"); // IntPtr pItemData = IntPtr.Zero; IntPtr pCompleteData = IntPtr.Zero; try { Structs.GAP_Inquiry_Entry_Event_Data itemData = new Structs.GAP_Inquiry_Entry_Event_Data( Addr1Bytes, new byte[] { 0x03, 0x02, 0x01 } ); pItemData = Marshal.AllocHGlobal(Marshal.SizeOf(itemData)); Marshal.StructureToPtr(itemData, pItemData, false); Structs.GAP_Event_Data item1 = new Structs.GAP_Event_Data( StackConsts.GAP_Event_Type.Inquiry_Entry_Result, pItemData); // const ushort NumDevices = 1; Structs.GAP_Inquiry_Event_Data completeData = new Structs.GAP_Inquiry_Event_Data( NumDevices /*, pCompleteDeviceData*/); pCompleteData = Marshal.AllocHGlobal(Marshal.SizeOf(completeData)); Marshal.StructureToPtr(completeData, pCompleteData, false); Structs.GAP_Event_Data complete = new Structs.GAP_Event_Data( StackConsts.GAP_Event_Type.Inquiry_Result, pCompleteData); // // using (var done = new ManualResetEvent(false)) { ThreadPool.QueueUserWorkItem(delegate { try { callback(stuff.StackId, ref item1, CallbackParameter); callback(stuff.StackId, ref complete, CallbackParameter); } finally { done.Set(); } }); bool signalled = done.WaitOne(30000); Debug.Assert(signalled, "NOT signalled"); } } finally { if (pItemData != IntPtr.Zero) { Marshal.FreeHGlobal(pItemData); } if (pCompleteData != IntPtr.Zero) { Marshal.FreeHGlobal(pCompleteData); } } // Thread.Sleep(200); Assert.IsFalse(ar.IsCompleted, "notIsCompleted2 before rnr"); Thread.Sleep(2500); // Space for (possible!) name queries Thread.Sleep(200); Assert.IsTrue(ar.IsCompleted, "IsCompleted3"); var deviceList = cli.EndDiscoverDevices(ar); Assert.AreEqual(1, deviceList.Length, "deviceList.Length"); var dev = deviceList[0]; Assert.AreEqual(BluetoothAddress.Parse("001122334455"), dev.DeviceAddress, "dev.DeviceAddress"); Assert.AreEqual(new ClassOfDevice(0x010203), dev.ClassOfDevice, "dev.ClassOfDevice"); // stuff.Mockery_VerifyAllExpectationsHaveBeenMet(); }
private void HandleAuthenticate_Callback2(uint BluetoothStackID, ref Structs.GAP_Event_Data GAP_Event_Data, uint CallbackParameter) { //Debug.WriteLine("Authenticate_Callback Event_Data_Type: " + GAP_Event_Data.Event_Data_Type); Debug.Assert(GAP_Event_Data.Event_Data_Type == StackConsts.GAP_Event_Type.Authentication, "Unexpected Authenticate_Callback Event_Data_Type: " + GAP_Event_Data.Event_Data_Type); if (GAP_Event_Data.Event_Data_Type == StackConsts.GAP_Event_Type.Authentication) { var data = (Structs.GAP_Authentication_Event_Data__Status) Marshal.PtrToStructure(GAP_Event_Data.pData, typeof(Structs.GAP_Authentication_Event_Data__Status)); var addr8 = new byte[8]; data._Remote_Device.CopyTo(addr8, 0); var addrI = BitConverter.ToInt64(addr8, 0); var addr = BluetopiaUtils.ToBluetoothAddress(addr8); #if DEBUG var addrI2 = BluetopiaUtils.BluetoothAddressAsInteger(addr); Trace.Assert(addrI == addrI2, "addrI: + " + addrI + " != addrI2: " + addrI2); #endif Debug.WriteLine("Authenticate_Callback: type: " + data._GAP_Authentication_Event_Type + ", addr: " + addr.ToString()); if (data._GAP_Authentication_Event_Type == StackConsts.GAP_Authentication_Event_Type .AuthenticationStatus) { Debug.WriteLine(" Status: " + data.GetAuthenticationStatus(_factory.ApiVersion) + ")"); } // PinPairItem ppItem; byte[] key = null; ppItem = GetPinPairItem_willLock(addr); // Got Pin? // Use LinkKey if not PairRequest active. if (ppItem == null || ppItem._eventForPairRequest == null) { lock (_pins) { // Got LinkKey? var got = _keys.TryGetValue(addr, out key); Debug.Assert(!got || (key != null)); } } if (ppItem == null && key == null) { Debug.WriteLine(" No Pin or LinkKey for that device, exiting."); return; } Debug.WriteLine(" Have Pin: " + (ppItem != null) + ", LinkKey: " + (key != null)); // BluetopiaError ret; switch (data._GAP_Authentication_Event_Type) { case StackConsts.GAP_Authentication_Event_Type.LinkKeyRequest: if (key == null) { ret = RespondWithNoLinkKey(addrI); } else { ret = RespondWithLinkKey(addrI, key); } break; case StackConsts.GAP_Authentication_Event_Type.PINCodeRequest: if (ppItem == null) { break; } // Respond with Pin Debug.Assert(ppItem != null, "Would have exited above if not a known device."); var rspndrInfo = new ResponderInfo { AddrI = addrI, PPItem = ppItem }; ret = RespondWithPinCode(rspndrInfo); break; case StackConsts.GAP_Authentication_Event_Type.AuthenticationStatus: if (ppItem == null) { break; } // Success or Fail?? Debug.Assert(ppItem != null, "Would have exited above if not a known device."); lock (_pins) { ppItem.status = data.GetAuthenticationStatus(_factory.ApiVersion); ppItem.success = (ppItem.status == StackConsts.HCI_ERROR_CODE.NO_ERROR); ppItem._eventForPairRequest.Set(); } break; case StackConsts.GAP_Authentication_Event_Type.LinkKeyCreation: // Store the LinkKey for the next time we connect -- the stack doesn't! var authInfoKey = (Structs.GAP_Authentication_Event_Data__LinkKey) Marshal.PtrToStructure(GAP_Event_Data.pData, typeof(Structs.GAP_Authentication_Event_Data__LinkKey)); AddOrUpdateLinkKey_willLock(addr, authInfoKey.GetLinkKey(_factory.ApiVersion)); break; }//switch } }