static DWORD WINAPI 
AsyncBluetoothUnpairDeviceThreadFunction(LPVOID lpParam)
{
    BluetoothUnpairDeviceState *state= reinterpret_cast<BluetoothUnpairDeviceState *>(lpParam);

    state->worker_thread_id = GetCurrentThreadId();

    BLUETOOTH_ADDRESS bt_address;
    bool success= string_to_bluetooth_address(state->getControllerAddress(), &bt_address);

    if (!state->getIsCanceled_WorkerThread())
    {
        state->setSubStatus_WorkerThread(BluetoothUnpairDeviceState::removingBluetoothDevice);

        // Tell windows to remove the device
        if (success && BluetoothRemoveDevice(&bt_address) != ERROR_SUCCESS)
        {
            SERVER_MT_LOG_ERROR("AsyncBluetoothUnpairDeviceRequest") 
                << "Controller " << state->getControllerID() 
                << " failed to remove bluetooth device";
            success= false;
        }

        if (state->getIsCanceled_WorkerThread())
        {
            SERVER_MT_LOG_ERROR("AsyncBluetoothUnpairDeviceRequest") 
                << "Ignoring cancel unpair request for Controller " << state->getControllerID() 
                << ". Already removed.";
        }

        if (success)
        {
            state->setSubStatus_WorkerThread(BluetoothUnpairDeviceState::success);
        }
        else
        {
            state->setSubStatus_WorkerThread(BluetoothUnpairDeviceState::failed);
        }
    }
    else
    {
        SERVER_MT_LOG_ERROR("AsyncBluetoothUnpairDeviceRequest") << "Canceled from the main thread.";
        state->setSubStatus_WorkerThread(BluetoothUnpairDeviceState::failed);
    }

    return 0;
}
Exemple #2
0
static void
handle_windows8_and_later(const BLUETOOTH_ADDRESS *move_addr, const BLUETOOTH_ADDRESS *radio_addr, const HANDLE hRadio)
{
    unsigned int scan = 0;
    int connected = 0;
    while (!connected) {
        BLUETOOTH_DEVICE_INFO device_info;
        if (get_bluetooth_device_info(hRadio, move_addr, &device_info, scan == 0) != 0) {
            WINPAIR_DEBUG("No Bluetooth device found matching the given address");
        } else {
            if (is_move_motion_controller(&device_info)) {
                WINPAIR_DEBUG("Found Move Motion Controller matching the given address");

                unsigned int conn_try;
                for (conn_try = 1; conn_try <= CONN_RETRIES; conn_try++) {
                    WINPAIR_DEBUG("Connection try %d/%d", conn_try, CONN_RETRIES);

                    if (update_device_info(hRadio, &device_info) != 0) {
                        break;
                    }

                    if (device_info.fConnected) {
                        /* Windows 8 (and later) seems to require manual help with setting up
                         * the device in the registry.
                         */
                        WINPAIR_DEBUG("Patching the registry ...");
                        if (patch_registry(move_addr, radio_addr) != 0) {
                            WINPAIR_DEBUG("Failed to patch the registry");
                        }

                        /* enable HID service only if necessary */
                        WINPAIR_DEBUG("Checking HID service ...");
                        if (!is_hid_service_enabled(hRadio, &device_info)) {
                            WINPAIR_DEBUG("Enabling HID service ...");
                            GUID service = HumanInterfaceDeviceServiceClass_UUID;
                            DWORD result = BluetoothSetServiceState(hRadio, &device_info, &service, BLUETOOTH_SERVICE_ENABLE);
                            if (result != ERROR_SUCCESS) {
                                WINPAIR_DEBUG("Failed to enable HID service");
                            }

                            WINPAIR_DEBUG("Patching the registry ...");
                            if (patch_registry(move_addr, radio_addr) != 0) {
                                WINPAIR_DEBUG("Failed to patch the registry");
                            }
                        }

                        WINPAIR_DEBUG("Verifying successful connection ...");
                        if (is_connection_established(hRadio, &device_info)) {
                            /* if we have a connection, stop trying to connect this device */
                            printf("Connection verified.\n");
                            connected = 1;
                            break;
                        }
                    }

                    Sleep(CONN_DELAY);
                }

                if(!device_info.fConnected) {
                    BluetoothRemoveDevice(&(device_info.Address));
                    WINPAIR_DEBUG("Device removed, starting all over again");
                }
            } else {
                WINPAIR_DEBUG("Bluetooth device matching the given address is not a Move Motion Controller");
            }
        }

        Sleep(SLEEP_BETWEEN_SCANS);
        scan = (scan + 1) % BT_SCAN_NEW_INQUIRY;
    }
}