Ejemplo n.º 1
0
        private void DispatchPacket(byte[] packet)
        {
            int opcode = packet[0];

            switch (opcode)
            {
            case EventPacket.EVT_ADVERTISEMENT_PACKET_OPCODE:
            {
                var pkt = new EvtAdvertisementPacket();
                pkt.Parse(packet);
                ButtonScanner scanner;
                if (_scanners.TryGetValue(pkt.ScanId, out scanner))
                {
                    scanner.OnAdvertisementPacket(new AdvertisementPacketEventArgs {
                            BdAddr = pkt.BdAddr, Name = pkt.Name, Rssi = pkt.Rssi, IsPrivate = pkt.IsPrivate, AlreadyVerified = pkt.AlreadyVerified, AlreadyConnectedToThisDevice = pkt.AlreadyConnectedToThisDevice, AlreadyConnectedToOtherDevice = pkt.AlreadyConnectedToOtherDevice
                        });
                }
            }
            break;

            case EventPacket.EVT_CREATE_CONNECTION_CHANNEL_RESPONSE_OPCODE:
            {
                var pkt = new EvtCreateConnectionChannelResponse();
                pkt.Parse(packet);
                var channel = _connectionChannels[pkt.ConnId];
                if (pkt.Error != CreateConnectionChannelError.NoError)
                {
                    _connectionChannels.TryRemove(channel.ConnId, out channel);
                }
                channel.OnCreateConnectionChannelResponse(new CreateConnectionChannelResponseEventArgs {
                        Error = pkt.Error, ConnectionStatus = pkt.ConnectionStatus
                    });
            }
            break;

            case EventPacket.EVT_CONNECTION_STATUS_CHANGED_OPCODE:
            {
                var pkt = new EvtConnectionStatusChanged();
                pkt.Parse(packet);
                var channel = _connectionChannels[pkt.ConnId];
                channel.OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs {
                        ConnectionStatus = pkt.ConnectionStatus, DisconnectReason = pkt.DisconnectReason
                    });
            }
            break;

            case EventPacket.EVT_CONNECTION_CHANNEL_REMOVED_OPCODE:
            {
                var pkt = new EvtConnectionChannelRemoved();
                pkt.Parse(packet);
                ButtonConnectionChannel channel;
                _connectionChannels.TryRemove(pkt.ConnId, out channel);
                channel.OnRemoved(new ConnectionChannelRemovedEventArgs {
                        RemovedReason = pkt.RemovedReason
                    });
            }
            break;

            case EventPacket.EVT_BUTTON_UP_OR_DOWN_OPCODE:
            case EventPacket.EVT_BUTTON_CLICK_OR_HOLD_OPCODE:
            case EventPacket.EVT_BUTTON_SINGLE_OR_DOUBLE_CLICK_OPCODE:
            case EventPacket.EVT_BUTTON_SINGLE_OR_DOUBLE_CLICK_OR_HOLD_OPCODE:
            {
                var pkt = new EvtButtonEvent();
                pkt.Parse(packet);
                var channel   = _connectionChannels[pkt.ConnId];
                var eventArgs = new ButtonEventEventArgs {
                    ClickType = pkt.ClickType, WasQueued = pkt.WasQueued, TimeDiff = pkt.TimeDiff
                };
                switch (opcode)
                {
                case EventPacket.EVT_BUTTON_UP_OR_DOWN_OPCODE:
                    channel.OnButtonUpOrDown(eventArgs);
                    break;

                case EventPacket.EVT_BUTTON_CLICK_OR_HOLD_OPCODE:
                    channel.OnButtonClickOrHold(eventArgs);
                    break;

                case EventPacket.EVT_BUTTON_SINGLE_OR_DOUBLE_CLICK_OPCODE:
                    channel.OnButtonSingleOrDoubleClick(eventArgs);
                    break;

                case EventPacket.EVT_BUTTON_SINGLE_OR_DOUBLE_CLICK_OR_HOLD_OPCODE:
                    channel.OnButtonSingleOrDoubleClickOrHold(eventArgs);
                    break;
                }
            }
            break;

            case EventPacket.EVT_NEW_VERIFIED_BUTTON_OPCODE:
            {
                var pkt = new EvtNewVerifiedButton();
                pkt.Parse(packet);
                NewVerifiedButton.RaiseEvent(this, new NewVerifiedButtonEventArgs {
                        BdAddr = pkt.BdAddr
                    });
            }
            break;

            case EventPacket.EVT_GET_INFO_RESPONSE_OPCODE:
            {
                var pkt = new EvtGetInfoResponse();
                pkt.Parse(packet);
                GetInfoResponseCallback callback;
                _getInfoResponseCallbackQueue.TryDequeue(out callback);
                callback(pkt.BluetoothControllerState, pkt.MyBdAddr, pkt.MyBdAddrType, pkt.MaxPendingConnections, pkt.MaxConcurrentlyConnectedButtons, pkt.CurrentPendingConnections, pkt.CurrentlyNoSpaceForNewConnection, pkt.BdAddrOfVerifiedButtons);
            }
            break;

            case EventPacket.EVT_NO_SPACE_FOR_NEW_CONNECTION_OPCODE:
            {
                var pkt = new EvtNoSpaceForNewConnection();
                pkt.Parse(packet);
                NoSpaceForNewConnection.RaiseEvent(this, new SpaceForNewConnectionEventArgs {
                        MaxConcurrentlyConnectedButtons = pkt.MaxConcurrentlyConnectedButtons
                    });
            }
            break;

            case EventPacket.EVT_GOT_SPACE_FOR_NEW_CONNECTION_OPCODE:
            {
                var pkt = new EvtGotSpaceForNewConnection();
                pkt.Parse(packet);
                GotSpaceForNewConnection.RaiseEvent(this, new SpaceForNewConnectionEventArgs {
                        MaxConcurrentlyConnectedButtons = pkt.MaxConcurrentlyConnectedButtons
                    });
            }
            break;

            case EventPacket.EVT_BLUETOOTH_CONTROLLER_STATE_CHANGE_OPCODE:
            {
                var pkt = new EvtBluetoothControllerStateChange();
                pkt.Parse(packet);
                BluetoothControllerStateChange.RaiseEvent(this, new BluetoothControllerStateChangeEventArgs {
                        State = pkt.State
                    });
            }
            break;

            case EventPacket.EVT_GET_BUTTON_INFO_RESPONSE_OPCODE:
            {
                var pkt = new EvtGetButtonInfoResponse();
                pkt.Parse(packet);
                GetButtonInfoResponseCallback callback;
                lock (_getButtonInfoResponseCallbackQueue)
                {
                    callback = _getButtonInfoResponseCallbackQueue.Dequeue();
                }
                callback(pkt.BdAddr, pkt.Uuid, pkt.Color, pkt.SerialNumber, pkt.FlicVersion, pkt.FirmwareVersion);
            }
            break;

            case EventPacket.EVT_SCAN_WIZARD_FOUND_PRIVATE_BUTTON_OPCODE:
            {
                var pkt = new EvtScanWizardFoundPrivateButton();
                pkt.Parse(packet);
                _scanWizards[pkt.ScanWizardId].OnFoundPrivateButton();
            }
            break;

            case EventPacket.EVT_SCAN_WIZARD_FOUND_PUBLIC_BUTTON_OPCODE:
            {
                var pkt = new EvtScanWizardFoundPublicButton();
                pkt.Parse(packet);
                var wizard = _scanWizards[pkt.ScanWizardId];
                wizard.BdAddr = pkt.BdAddr;
                wizard.Name   = pkt.Name;
                wizard.OnFoundPublicButton(new ScanWizardButtonInfoEventArgs {
                        BdAddr = wizard.BdAddr, Name = wizard.Name
                    });
            }
            break;

            case EventPacket.EVT_SCAN_WIZARD_BUTTON_CONNECTED_OPCODE:
            {
                var pkt = new EvtScanWizardButtonConnected();
                pkt.Parse(packet);
                var wizard = _scanWizards[pkt.ScanWizardId];
                wizard.OnButtonConnected(new ScanWizardButtonInfoEventArgs {
                        BdAddr = wizard.BdAddr, Name = wizard.Name
                    });
            }
            break;

            case EventPacket.EVT_SCAN_WIZARD_COMPLETED_OPCODE:
            {
                var pkt = new EvtScanWizardCompleted();
                pkt.Parse(packet);
                ScanWizard wizard;
                _scanWizards.TryRemove(pkt.ScanWizardId, out wizard);
                var eventArgs = new ScanWizardCompletedEventArgs {
                    BdAddr = wizard.BdAddr, Name = wizard.Name, Result = pkt.Result
                };
                wizard.BdAddr = null;
                wizard.Name   = null;
                wizard.OnCompleted(eventArgs);
            }
            break;

            case EventPacket.EVT_BUTTON_DELETED_OPCODE:
            {
                var pkt = new EvtButtonDeleted();
                pkt.Parse(packet);
                ButtonDeleted.RaiseEvent(this, new ButtonDeletedEventArgs {
                        BdAddr = pkt.BdAddr, DeletedByThisClient = pkt.DeletedByThisClient
                    });
            }
            break;
            }
        }
Ejemplo n.º 2
0
 protected internal virtual void OnCompleted(ScanWizardCompletedEventArgs e)
 {
     Completed.RaiseEvent(this, e);
 }