Example #1
0
        //private void RespondWithPinCode_Runner(object state)
        //{
        //    RespondWithPinCode((ResponderInfo)state);
        //}

        private BluetopiaError RespondWithPinCode(ResponderInfo rspInfo)
        {
            BluetopiaError ret;
            var            rsp = new Structs.GAP_Authentication_Information(
                StackConsts.GAP_Authentication_Type_t.atPINCode,
                rspInfo.PPItem._pin, _factory.ApiVersion);

            //, "Zero len Pins not allowed.
            Debug.WriteLine("  Sending Auth Response: PinCode");
            Debug.WriteLine("    " + rsp.DebugToString());
            ret = _factory.Api.GAP_Authentication_Response(
                _factory.StackId, rspInfo.AddrI, ref rsp);
            Debug.Assert(BluetopiaUtils.IsSuccess(ret),
                         "GAP_Authentication_Response: " + ret);
            return(ret);
        }
Example #2
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
            }
        }