Ejemplo n.º 1
0
        internal static BluetopiaDeviceInfo CreateFromInquiry(
            Structs.GAP_Inquiry_Entry_Event_Data data, BluetopiaFactory factory)
        {
            var bdi = new BluetopiaDeviceInfo(factory,
                                              BluetopiaUtils.ToBluetoothAddress(data.BD_ADDR));

            bdi._cod = BluetopiaUtils.ToClassOfDevice(data.Class_of_Device);
            bdi._blockFindNameForNow = true;
            bdi._clockOffsetEtc      = new ClockOffsetEtc(data);
            return(bdi);
        }
Ejemplo n.º 2
0
        private Structs.SPP_Event_Data EventConnectInd(Structs.SPP_Event_Data SPP_Event_Data)
        {
            var data = (Structs.SPP_Open_Port_Indication_Data)
                       Marshal.PtrToStructure(SPP_Event_Data.pEventData,
                                              typeof(Structs.SPP_Open_Port_Indication_Data));

            Debug.WriteLine("EventConnectInd portId: " + data.SerialPortID);
            Debug.Assert(_hPortServer.Value == data.SerialPortID, "thought this should be the same id");
            _serverIndAddress = BluetopiaUtils.ToBluetoothAddress(data.BD_ADDR);
            ThreadPool.QueueUserWorkItem(delegate {
                HandleCONNECTED("INDICATE");
            });
            return(SPP_Event_Data);
        }
Ejemplo n.º 3
0
        internal BluetopiaRadio(BluetopiaFactory factory)
        {
            _fcty = factory;
            byte[] bd_addr = new byte[StackConsts.BD_ADDR_SIZE];
            var    ret     = _fcty.Api.GAP_Query_Local_BD_ADDR(_fcty.StackId, bd_addr);

            BluetopiaUtils.CheckAndThrow(ret, "GAP_Query_Local_BD_ADDR");
            _addr = BluetopiaUtils.ToBluetoothAddress(bd_addr);
            //
            ReadName();
            //
            uint cod;

            ret = _fcty.Api.GAP_Query_Class_Of_Device(_fcty.StackId, out cod);
            BluetopiaUtils.CheckAndThrow(ret, "GAP_Query_Class_Of_Device");
            _cod = new ClassOfDevice(cod);
        }
Ejemplo n.º 4
0
        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);
            });
        }
Ejemplo n.º 5
0
        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
            }
        }