hci_get_route() private method

private hci_get_route ( IntPtr bdaddr ) : int
bdaddr IntPtr
return int
        private int CreateDiscoverSocket()
        {
            int adapterId = -1;
            int socket    = -1;

            // Find a bluetooth adapter, repeat until found
            while (_DiscoverThread != null && adapterId == -1)
            {
                adapterId = NativeMethods.hci_get_route(IntPtr.Zero);
                Thread.Sleep(1000);
            }

            // did the loop exit because of _DiscoverThread or adapterId
            if (adapterId == -1)
            {
                return(-1);
            }

            // create the socket
            if ((socket = NativeMethods.hci_open_dev(adapterId)) == -1)
            {
                return(-1);
            }

            // configure the socket
            NativeMethods.hci_filter filter = default(NativeMethods.hci_filter);
            filter.opcode       = 0x0;
            filter.type_mask    = 16;
            filter.event_mask_a = 0xffffffff;
            filter.event_mask_b = 0xffffffff;
            NativeMethods.setsockopt(socket, NativeMethods.SOL_HCI, NativeMethods.HCI_FILTER, ref filter, 14);

            return(socket);
        }
Esempio n. 2
0
        private void Connect(NativeMethods.bdaddr_t bdaddress)
        {
            // get bluetooth address
            NativeMethods.bdaddr_t dongleAddr;
            int deviceId = NativeMethods.hci_get_route(IntPtr.Zero);

            NativeMethods.hci_devba(deviceId, out dongleAddr);

            // create l2cap address
            NativeMethods.sockaddr_l2 address;
            address.l2_family = NativeMethods.AF_BLUETOOTH;
            address.l2_cid    = 0;

            // allocate sockets
            _ControlSocket = -1;
            while (_ControlSocket == -1)
            {
                _ControlSocket = NativeMethods.socket(NativeMethods.AF_BLUETOOTH, NativeMethods.SOCK_SEQPACKET, NativeMethods.BTPROTO_L2CAP);
                int error = Marshal.GetLastWin32Error();
                if (_ControlSocket == -1)
                {
                    if (error != NativeMethods.EINTR)
                    {
                        throw new WiiDeviceLibrary.DeviceConnectException("Failed to allocate the control socket: " + NativeMethods.strerror(error));
                    }
                }
            }

            _InterruptSocket = -1;
            while (_InterruptSocket == -1)
            {
                _InterruptSocket = NativeMethods.socket(NativeMethods.AF_BLUETOOTH, NativeMethods.SOCK_SEQPACKET, NativeMethods.BTPROTO_L2CAP);
                int error = Marshal.GetLastWin32Error();
                if (_InterruptSocket == -1)
                {
                    if (error != NativeMethods.EINTR)
                    {
                        NativeMethods.close(_ControlSocket);
                        throw new WiiDeviceLibrary.DeviceConnectException("Failed to allocate the interrupt socket: " + NativeMethods.strerror(error));
                    }
                }
            }

            // bind the bluetooth socket
            address.l2_psm = 0x0;
            address.bdaddr = dongleAddr;
            if (NativeMethods.bind(_ControlSocket, ref address, (uint)Marshal.SizeOf(address)) == -1)
            {
                int error = Marshal.GetLastWin32Error();
                NativeMethods.close(_ControlSocket);
                NativeMethods.close(_InterruptSocket);
                throw new WiiDeviceLibrary.DeviceConnectException("Failed to bind the control socket: " + NativeMethods.strerror(error));
            }

            // connect the control socket
            address.bdaddr = bdaddress;
            address.l2_psm = 0x11;
            while (NativeMethods.connect(_ControlSocket, ref address, (uint)Marshal.SizeOf(address)) == -1)
            {
                int error = Marshal.GetLastWin32Error();
                if (error != NativeMethods.EINTR)
                {
                    NativeMethods.close(_ControlSocket);
                    NativeMethods.close(_InterruptSocket);
                    throw new WiiDeviceLibrary.DeviceConnectException("Failed to connect the control socket: " + NativeMethods.strerror(error));
                }
            }

            // connect the interrupt socket
            address.bdaddr = bdaddress;
            address.l2_psm = 0x13;
            while (NativeMethods.connect(_InterruptSocket, ref address, (uint)Marshal.SizeOf(address)) == -1)
            {
                int error = Marshal.GetLastWin32Error();
                if (error != NativeMethods.EINTR)
                {
                    NativeMethods.close(_ControlSocket);
                    NativeMethods.close(_InterruptSocket);
                    throw new WiiDeviceLibrary.DeviceConnectException("Failed to connect the interrupt socket: " + NativeMethods.strerror(error));
                }
            }
            _Connected = true;
        }