All Classes Namespaces Files Functions Variables Enumerations Enumerator
libs/controller/rescanWiimote.cpp
Go to the documentation of this file.
00001 
00010 #include "rescanWiimote.h"
00011 #include "DetectOS.h"
00012 #ifdef LINUX_OS
00013 bool qrk::rescanWiimote(void)
00014 {
00015   // !!! 実装方法、未検討
00016   return false;
00017 }
00018 #else
00019 #include <windows.h>
00020 #include <bthsdpdef.h>
00021 #include <bthdef.h>
00022 #include <BluetoothAPIs.h>
00023 #include <string>
00024 #include <vector>
00025 
00026 #pragma comment (lib, "Irprops.lib")
00027 
00028 using namespace qrk;
00029 
00030 
00031 // Wii リモコン用ドライバのリロード
00032 bool qrk::rescanWiimote(void)
00033 {
00034   // Bluetooth デバイス名の取得。ただし、接続中のデバイスは無視する
00035   BLUETOOTH_DEVICE_SEARCH_PARAMS btsp;
00036   ZeroMemory(&btsp, sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));
00037   btsp.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
00038   btsp.fReturnAuthenticated = TRUE;
00039   btsp.fReturnRemembered = TRUE;
00040   btsp.fReturnUnknown = TRUE;
00041   btsp.fReturnConnected = TRUE;
00042   btsp.fIssueInquiry = TRUE;
00043   btsp.cTimeoutMultiplier = 2;
00044   btsp.hRadio = NULL;
00045 
00046   BLUETOOTH_DEVICE_INFO btdi;
00047   btdi.dwSize = sizeof(btdi);
00048 
00049   HBLUETOOTH_DEVICE_FIND hbf = BluetoothFindFirstDevice(&btsp, &btdi);
00050   if (hbf == NULL) {
00051     //fprintf(stderr, "hFind is NULL\n");
00052     return false;
00053   }
00054 
00055   typedef vector<BLUETOOTH_DEVICE_INFO*> WiiDevices;
00056   WiiDevices devices;
00057   do {
00058     // 接続中以外の Wii デバイスを登録
00059     if (btdi.fConnected == 0) {
00060       char device_name[MAX_PATH];
00061       int i = 0;
00062       while (btdi.szName[i] != '\0') {
00063         device_name[i] = static_cast<char>(btdi.szName[i]);
00064         ++i;
00065       }
00066       device_name[i] = '\0';
00067       //fprintf(stderr, "%d: %s\n", btdi.fConnected, device_name);
00068 
00069       if (! strcmp(device_name, "Nintendo RVL-CNT-01")) {
00070         devices.push_back(&btdi);
00071         //fprintf(stderr, "register: %p\n", &btdi);
00072       }
00073     }
00074   } while (BluetoothFindNextDevice(hbf, &btdi) != FALSE);
00075   BluetoothFindDeviceClose(hbf);
00076 
00077   // Bluetooth デバイスのアンロード
00078   for (WiiDevices::iterator it = devices.begin(); it != devices.end(); ++it) {
00079 
00080     BluetoothRemoveDevice(&(*it)->Address);
00081   }
00082 
00083   // 接続有効化
00084   HANDLE hRadio;
00085   BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(btfrp) };
00086   HBLUETOOTH_RADIO_FIND hbtrf = BluetoothFindFirstRadio(&btfrp, &hRadio);
00087   if (hbtrf == NULL) {
00088 
00089     //fprintf(stderr, "BluetoothFindFirstRadio() is NULL\n");
00090     return false;
00091   }
00092   do {
00093     BLUETOOTH_RADIO_INFO radioInfo;
00094     radioInfo.dwSize = sizeof(radioInfo);
00095     BluetoothGetRadioInfo(hRadio, &radioInfo);
00096 
00097     for (WiiDevices::iterator it = devices.begin(); it != devices.end(); ++it) {
00098 
00099       BluetoothSetServiceState(hRadio, *it,
00100                                &HumanInterfaceDeviceServiceClass_UUID,
00101                                BLUETOOTH_SERVICE_ENABLE );
00102     }
00103 
00104   } while (BluetoothFindNextRadio(hbtrf, &hRadio));
00105   BluetoothFindRadioClose(hbtrf);
00106 
00107   return true;
00108 }
00109 #endif