protected override void Dispose(bool disposing) { if (_Connected) { _SendBuffer[0] = 0x15; _SendBuffer[1] = 0x1; NativeMethods.send(_ControlSocket, _SendBuffer, 2, 0); NativeMethods.close(_InterruptSocket); NativeMethods.close(_ControlSocket); } base.Dispose(disposing); }
public override void Write(byte[] buffer, int offset, int count) { if (!_Connected) { throw new IOException("The control socket is not connected"); } _SendBuffer[0] = 0x52; Array.Copy(buffer, offset, _SendBuffer, 1, count); while (NativeMethods.send(_ControlSocket, _SendBuffer, count + 1, 0) == -1) { int error = Marshal.GetLastWin32Error(); if (error != NativeMethods.EINTR) { NativeMethods.close(_InterruptSocket); NativeMethods.close(_ControlSocket); _Connected = false; throw new IOException("Failed to write to the control socket: " + NativeMethods.strerror(error)); } } }
public override int Read(byte[] buffer, int offset, int count) { int receivedByteCount = NativeMethods.recv(_InterruptSocket, _ReceiveBuffer, _ReceiveBuffer.Length, 0); if (receivedByteCount > 0) { // with bluez you get a hid byte, this must not be copied into the buffer count = Math.Min(count, receivedByteCount - 1); Array.Copy(_ReceiveBuffer, 1, buffer, offset, count); return(count); } else if (receivedByteCount <= 0) { NativeMethods.close(_InterruptSocket); NativeMethods.close(_ControlSocket); _Connected = false; if (receivedByteCount < 0) { throw new IOException("Failed to read from the interrupt socket."); } } return(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; }