diff --git a/cpp/wiiscan/Debug/DelcomDLL.dll b/cpp/wiiscan/Debug/DelcomDLL.dll new file mode 100644 index 0000000..ecfa470 Binary files /dev/null and b/cpp/wiiscan/Debug/DelcomDLL.dll differ diff --git a/cpp/wiiscan/Debug/USBm.dll b/cpp/wiiscan/Debug/USBm.dll new file mode 100644 index 0000000..1460ff3 Binary files /dev/null and b/cpp/wiiscan/Debug/USBm.dll differ diff --git a/cpp/wiiscan/Debug/WiimoteLib.dll b/cpp/wiiscan/Debug/WiimoteLib.dll new file mode 100644 index 0000000..da8e126 Binary files /dev/null and b/cpp/wiiscan/Debug/WiimoteLib.dll differ diff --git a/cpp/wiiscan/Debug/bdi.out b/cpp/wiiscan/Debug/bdi.out new file mode 100644 index 0000000..3e47aaa Binary files /dev/null and b/cpp/wiiscan/Debug/bdi.out differ diff --git a/cpp/wiiscan/Debug/wiimotelibpoll.exe b/cpp/wiiscan/Debug/wiimotelibpoll.exe new file mode 100644 index 0000000..72937a2 Binary files /dev/null and b/cpp/wiiscan/Debug/wiimotelibpoll.exe differ diff --git a/cpp/wiiscan/Debug/wiiscan.exe b/cpp/wiiscan/Debug/wiiscan.exe new file mode 100644 index 0000000..3b0f862 Binary files /dev/null and b/cpp/wiiscan/Debug/wiiscan.exe differ diff --git a/cpp/wiiscan/Debug/wiiscan.ini b/cpp/wiiscan/Debug/wiiscan.ini new file mode 100644 index 0000000..8833211 --- /dev/null +++ b/cpp/wiiscan/Debug/wiiscan.ini @@ -0,0 +1,81 @@ +% Configfile_begin + % config file for wiiscan + + % all usb device ids, not used yet in the scanning + all_usb_hubs="" + + % single power controlling usb hub, only nessesary when powerering over usb, for automatic resetting. If not used, put "" + % active_usb_hub="@PCI\VEN_8086&DEV_2934&SUBSYS_20F017AA&REV_03\3&B1BFB68&0&E8" + active_usb_hub="" + + % a list of allowed wiimotes in the form xx:xx:xx:xx:xx:xx, wildcard match is 00:00:00:00:00:00 + %allowed_wiimote_adr=00:00:00:00:00:00 + % my set of wiimotes + allowed_wiimote_adr=00:19:?D:??:6?:?? 00:1F:C5:??:??:?? 00:17:AB:39:69:87 + + % launching exe file have a problem with spaces, hence renamed exe, replaced space with underscores + % The original Johnny Chung Lee wb software version 0.3, downloaded from [http://johnnylee.net/projects/wii/] + % may not work yet, missing library integration + %whiteboard_software="d:/WiimoteWhiteboard/WiimoteWhiteboard_v0.3.exe" + + % same stuff, but binary build from sources and redistributed in this package + whiteboard_software="WiimoteWhiteboard.exe" + + % Boon Jin's wb software version 0.6, does not work yet, missing library integration + %whiteboard_software="d:/wsb/WiimoteSmoothboard.exe" + + % wiiscan parameters + %option_device="Nintendo RVL-CNT-01" + + % number of bt scanning attempts in a autoscan, timout is increased for every scan as retryattempt*option_timeout + option_scanretries=1 + + % long time (4000 and above, or increase option_scanretries) when matching name, short (1000-2000) when matching address + option_timeout=4000 + + % wiimote scanning time, only applicable used for wiilib + option_wiitimeout=1000 + + % delay bewteen powerdown/up, see man for details + option_usbsleep=1000 + option_usbmsleep=1000 + %option_btsleep=2000 + + % start wb at successfull connect, 0 or 1 + option_startwhiteboard=1 + + % output verbose flag, 0 or 1 + option_debug=1 + + % logging output file, put nothing if no logging is wanted + option_logfile=wiiscan_log.txt + %option_logfile= + + % wiimote connection library, use none, wiiuse, or wiimotelib + option_wiilib=wiimotelib + + % 0=no power cycle, 1=USB hub control, 2=USBm IO, 3=USB Devcon + option_usbpowermode=3 + + % 0=remove if not connectable, 1=always remove, 2=never remove + %option_forceremove=0 + + % bt radio to use, not working yet + %option_btradio=00:50:B6:A0:48:8C + + % wiiscantray options + % number of connection attempts before autoscan i restarted + option_tray_pool_retries=2 + + % retry sleep, keep it low (<1000) to catch an open connection + option_tray_sleep_between_pool_retries=250 + + % sleep bewteen polls, when wiimote is succeful connected, keep it hight (>2000) to avoid to much CPU usage + option_tray_sleep_between_pool_loops=2000 + + % final connection step reqires some sleep before windows PnP is ready + option_sleep_before_final_connect=1500 + + % delay after launch of wb software, keep it at a medium value (2000-10000) + option_sleep_after_startwhiteboard=4000 +% Configfile_end diff --git a/cpp/wiiscan/Debug/wiiuse.dll b/cpp/wiiscan/Debug/wiiuse.dll new file mode 100644 index 0000000..a7e228c Binary files /dev/null and b/cpp/wiiscan/Debug/wiiuse.dll differ diff --git a/cpp/wiiscan/Doc/CHANGEINGBTSTACK.rtf b/cpp/wiiscan/Doc/CHANGEINGBTSTACK.rtf new file mode 100644 index 0000000..2f5810a --- /dev/null +++ b/cpp/wiiscan/Doc/CHANGEINGBTSTACK.rtf @@ -0,0 +1,57 @@ +CHANGING the Widcomm BT stack to Microsoft BT stack + + [From http://forum.eeeuser.com/viewtopic.php?id=38533] + + After making my post in the 1000H post I thought it probably best If it here as I don't see why it wouldn't work on the 901 models either... + + Basically it's all to just to change from using the Widcomm stack for using bluetooth connections to the built in Windows stack, it's fairly simple to do and should be a performance boost as Widcomm runs fairly sloppy. + + basically if you follow a guide similar to this>>> http://www.x-setup.net/forum/showthread.php?p=4762 + + Open file 'C:\Windows\inf\bth.inf' in notepad, (The inf folder is hidden by default so using Explorer go to Folder Options and change it to show all hidden folders) + + (A good time to back up the bth.inf file if your worried about editing stuff wrongly but it's only like 3 lines worth) + + Now you should have the section like this... + + [Manufacturer] + %Microsoft%=Microsoft + ALPS=ALPS, NT.5.1 + Belkin=Belkin, NT.5.1 + Brain Boxes=BrainBoxes, NT.5.1 + Broadcom=Broadcom, NT.5.1 + Cambridge Silicon Radio Ltd.=Cambridge, NT.5.1 + Dell=Dell, NT.5.1 + FIC=FIC, NT.5.1 + GVC=GVC, NT.5.1 + HP=HP, NT.5.1 + IBM=IBM, NT.5.1 + Microsoft=Microsoft, NT.5.1 + Motion Computing=MotionComputing, NT.5.1 + Silicon Wave=SiliconWave, NT.5.1 + Sony=Sony, NT.5.1 + TDK=TDK, NT.5.1 + TOSHIBA=Toshiba, NT.5.1 + Wistron NeWeb=Wistron, NT.5.1 + Zeevo=Zeevo, NT.5.1 + + Now add a line to the that section a line such as (without quotes of course): 'Azwave=Azwave, NT.5.1 ' + + There should then be sections below this one that starts with: + + ;------------- Device section - Start ----------------------- + + Then insert a section that looks like this (Please note that you can check that the Vid and Pid are the same as your card by using device manage and looking at the properties of the device, and then the details tab, if not edit the following values, keeping the same format): + + [Azwave.NT.5.1] + BT253= BthUsb, USB\Vid_0b05&Pid_b700 + + Then save and close the notepad. + + Then just either do a 'I'll choose from a list of drivers install' or if you haven't installed the drivers yet do a disable and enable of the bluetooth and it should install it automatically. The driver won't be signed so continue with it anyways. + + I've now tested it with receiving a file from a phone and as such it seems to work absolutely fine. + + Updated due to my own incompetence, don't worry this fix still works great. + + Last edited by Sly (2008-08-30 8:51:54 pm) diff --git a/cpp/wiiscan/Doc/CHANGELOG.rtf b/cpp/wiiscan/Doc/CHANGELOG.rtf new file mode 100644 index 0000000..af63f06 --- /dev/null +++ b/cpp/wiiscan/Doc/CHANGELOG.rtf @@ -0,0 +1,59 @@ +Copyright © 2008 MergeIt, Aps. +License GPLv3+: GNU GPL version 3 or later . +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. + +Permission to use, copy, modify, and distribute this software +and its documentation for any purpose and without fee is hereby granted, provided that +the above copyright notice appear in all copies and that both that copyright notice +and this permission notice appear in supporting documentation. + +WIISCAN: + a connection utility for wii console remotes + +VERSION: + 0.9 + + Added: + + * wildcard matches on wiimote adr. + + * changed wiiscantray to hook into wiimotelib poll (major change). + + * allowed for seamless connection with wiiscantray. + + * (numerious minor changes, fixes) + +VERSION: + 0.8 + + Added: + * Minor code refactoring. + + * Configfile entries for all options. Change the order of parsing options, first look in configfile, then read the command line (that eventually overrules the configfile options). + + * Modified wiiscan.ini file to be more versatile. + + * Added logging class, rewritten logging method. + + * Changed calls to System, to CreatProc, that do not display a console window (for win trayicon mode). + + * Changed project settings, links with MT lib instead of MT DLL; fixes strange MS bug when not being able to find msvcp90.dll and friends. + + * Added hardware support for USBmicro IO board; power-control now possible with this board instead of the USB controller. + + * Added USBio up/down functionality. + + * Added usbmsleep, postsleep after USBmicro up/down cycle. + + Fixed: + * Fix of defect "1: restart pop-up": the 'Change of system settings ... Do you want to reboot now?' popup, happens only when the program runs for longer than 19 seconds. Keeping the connection time within 19 sec seems to cure the problem, Adding a sleep at the end of the program, say Sleep(20000) will eventually bring up the dialog again. The dialog can anyway safely be ignored! + + * Fix of defect "2: discoverable mode fast shutdown": Initialization of wiilib has been rewritten, making it recall the HID initialization routine. Code for testing the wiimote connection has also been introduced. + + * Fixed defect "15: BluetoothFindFirstDevice() stalls". + +VERSION: + 0.7 + + First published version. \ No newline at end of file diff --git a/cpp/wiiscan/Doc/INSTALL.rtf b/cpp/wiiscan/Doc/INSTALL.rtf new file mode 100644 index 0000000..f8cdc4e --- /dev/null +++ b/cpp/wiiscan/Doc/INSTALL.rtf @@ -0,0 +1,153 @@ +INSTALLATION + + Version: 0.9 + + Procedure for wiiscan or wiiscantray: + + Prerequisites + + 1a: unzip the wiiscan zip file(s). + + 1b: if you are not running a Microsoft Bluetooth stack, then change it from Widcomm, Bluesoleil or whatever you are using. See the file CHANGINGBTSTACK.rtf. + + Running + + 1a: GUI version, run "wiiscantray.exe" in bin/Release or bin/Debug directory. + + 1b: CLI version, run "wiiscan.exe" in bin/Release or bin/Debug directory from a command prompt. + + User optimizations: + + Tune scan speed by giving wiiscan your specific wiimote address. Run "wiiscan -s nintendo" from a prompt to find the address, or use the windows system setting. + + Replace the "allowed_wiimote_adr=00:00:00:00:00:00" by our specific addres in the wiiscan.ini file, and lower the value of "option_timeout" to 1000 or 2000. + + Avoid annoying baloon tips by running: "popup_disable.reg" found in src/wiiscan-0.9.zip, under Tools. + + Avoid annoying crash reports from the Wiimote library by running: "no_error_report.reg" found the same place as above. + +ENJOY + Mar 26 2009 + + Carsten Frigaard, + MergeIt ApS, Kongsvang Allé 37, DK-8000 Århus C, www.mergit.dk + + +INSTALLATION + + Version: 0.8 + + Procedure for wiiscan or wiiscantray: + + Prerequisites + + 1a: unzip the wiiscan zip file (wiiscan-0.8.zip, wiiscantray-0.8.zip or wiiscan-0.8-release-1.0.zip) + + 1b: if you are not running a Microsoft Bluetooth stack, then change it from Widcomm, Bluesoleil or whatever you are using. See the file CHANGINGBTSTACK.rtf. + + Testing the exe files + + 2: run the wiiscan.exe --- if it terminates with an error indicating that it can not run the executable, you will need to recompile the .exe from the source code, or install .NET framework 3.5 SP1. Otherwise goto 5. + + Note: even though the executable is a primitive console app it apparently needs a .NET framework! You can install .NET framework 3.5 SP1 or just install the Visual Studio C++ Express compiler (that also installs the needed framework). + + Rebuilding the source code + + 3a: Install VS express form http://www.microsoft.com/express/vc/ [version: Microsoft Visual C++ 2008 Express Edition with SP1 - ENU] + + 3b: Open the wiiscan.sln and rebuild the executable. + + 3c: Re-try to launch wiiscan.exe (as in 2). + + Finding and testing the +5V power in the USB controllers [do this, only if USB Hub power control is needed, that is the case of no USBmicro device hardware] + + 4a: download free, but non-redistributable, device console program, devcon.exe, from http:support.microsoft.com/kb/311272 + + 4b: unzip devcon.zip and copy devcon.exe (i386/devcon.exe) to path (say copy it into c:\WINDOWS\ directory) + + 4c: Some USB controllers cannot do a power-down; it seems that ICH7 controllers are not able to cut the power while ICH9 controllers can. + + On an ASUS eee PC the controller version reads + + "Intel(R) 82801G (ICH7 Family) USB Universal Host Controller" + "Intel(R) 82801G (ICH7 Family) USB2 Enhanced Host Controller" + + On a Lenovo R599 the controller version reads + + "Intel(R) ICH9 Family USB Universal Host Controller" + "Intel(R) ICH9 Family USB2 Enhanced Host Controller" + + where only the later (ICH9) is usable for the wiimote auto-power procedure here. Check our system specification, before proceeding. + + 4d: Find the id of the USB hubs on the system, open "Control panel | System | Hardware | Device management" and look for USB-controllers. + + Open the USB-controller tap and write down the address of all the USB Universal host controllers, and USB2 Enhanced host controllers. + + The id of the controllers are found by right-clicking on the device, selecting "Properties | Details" + + On my eee PC I have four Intel controllers + + PCI\VEN_8086&DEV_27C8&SUBSYS_830F1043&REV_02\3&11583659&0&E8 + PCI\VEN_8086&DEV_27C9&SUBSYS_830F1043&REV_02\3&11583659&0&E9 + PCI\VEN_8086&DEV_27CA&SUBSYS_830F1043&REV_02\3&11583659&0&EA + PCI\VEN_8086&DEV_27CB&SUBSYS_830F1043&REV_02\3&11583659&0&EB + + and one USB2 controller + + PCI\VEN_8086&DEV_27CC&SUBSYS_830F1043&REV_02\3&11583659&0&EF + + Try to manually disable all the controllers bu + + > devcon status "@PCI\VEN_8086&DEV_27C8&SUBSYS_830F1043&REV_02\3&11583659&0&E8" + + or, if possible, you could use a pattern match to select all the devices...for my particular system the following expression works + + > devcon status "@PCI\VEN_8086&DEV_27C*&SUBSYS_830F1043&REV_02\3&11583659&0&E*" + + Then disable all devices + + > devcon disable "@PCI\VEN_8086&DEV_27C*&SUBSYS_830F1043&REV_02\3&11583659&0&E*" + + and check the result in the windows device manager (or manually disable all the USB controllers here) + + Finally, pick an active controller and put its address into the wiiscan.ini file, for my particular setup the file then reads + + % Configfile_begin + % config file for wiiscan + all_usb_hubs="@PCI\VEN_8086&DEV_27C*&SUBSYS_830F1043&REV_02\3&11583659&0&E*" + active_usb_hub="@PCI\VEN_8086&DEV_27C8&SUBSYS_830F1043&REV_02\3&11583659&0&E8" + allowed_wiimote_adr=00:00:00:00:00:00 + whiteboard_software="d:/WiimoteWhiteboard/WiimoteWhiteboard_v0.3.exe" + % Configfile_end + + It is not that important to choose a particular USB controller as the active one, important is that power is only cut to the USB when ALL controllers are disabled, and on when just a single controller is enabled. + + Note: the use of other USB devices may be troublesome, when powering the wiimote over USB as here. It is not recommended to use USB devices together with this procedure!! + + Add the addresses of your wiimotes to the .ini file + + 5: To enable faster an more robust connection, find and add all wiimote addresses to the .ini file in the form of a single address or a list, say + + allowed_wiimote_adr=00:19:FD:CC:60:61 00:19:1D:D6:65:E5 + + Test an automated, unattended connection + + 6; Re-run the wiiscan.exe in a console. If it still fails try to give it a -v option for verbose output, tune the timing parameters (-b,-t,-u,-w), or mail all the results to me: carsten AT mergit.dk + + Run the wiiscantray GUI version + + 7; Run wiiscantray.exe, + + * click on "Rescan for wiimotes" to connect. + + * click "Start whiteboard..." to start the whiteboard executable. Use the option "option_startwhiteboard=1" in the .ini file to start the software after a successful connection. + + * click "Display log..." to show the log output, enable and control the log via the options "option_logfile=wiiscan_log.txt" and "option_debug=1 + + * use "Reset log..." to clear the log. + +ENJOY + Feb 12 2009 + + Carsten Frigaard, + MergeIt ApS, Kongsvang Allé 37, DK-8000 Århus C, www.mergit.dk diff --git a/cpp/wiiscan/Doc/LICENSE.rtf b/cpp/wiiscan/Doc/LICENSE.rtf new file mode 100644 index 0000000..65d0555 --- /dev/null +++ b/cpp/wiiscan/Doc/LICENSE.rtf @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/cpp/wiiscan/Doc/MANUAL_wiiscan.rtf b/cpp/wiiscan/Doc/MANUAL_wiiscan.rtf new file mode 100644 index 0000000..7557404 --- /dev/null +++ b/cpp/wiiscan/Doc/MANUAL_wiiscan.rtf @@ -0,0 +1,500 @@ + +wiiscan(1) wiiscan(1) + + + +NAME + wiiscan - a connection utility for wii console remotes + + +SYNOPSIS + wiiscan <-a | -c | -d | -r | -s | -usbup | + -usbdown> > [-cf ] [-lf ] [-b ] [-t ] [-u + ] [-p ] [-w ] [-q ] [-f ] + [-m ] [-l ] [-wb] [-v] + + +OVERVIEW + wiiscan is a canvas function for a number of different scanning and + connection utilities. It can detect build-in bluetooth radios, scan for + nearby bluetooth devices, connect to a specific device and remove that + device again from the hardware. + + The main feature of wiiscan is to automatically connect to a wii remote + (wiimote). This can be quiet cumbersome on a Windows system, and the + nesseccary steps for doing a robust, working connection is done by + + + Delete wiimote hardware HID bluetooth entry: + delete old entries of the wiimote in the bluetooth hardware. On + some windows system the wiimote is readily detected, after the + first manually installation. Pressing the "1-2" makes the + wiimote discoverable. On other systems (including mine), any + attempt to connect to the wiimote, after one successful or un- + successful connection attempt, will always fail! Removing the + HID and wiimote registry entries before the next attempt cures + this feature. + + + Cycle USB bus: + the wiimote can be switched automatically to discoverable mode, + if the power is briefly cut from the device. This can be done + on a wiimote powered by the USB +5 volt (with a proper voltage + regulator to bring it under +3 volt). This step hence turns the + USB hub off, killing the power, and turns it on again. The + "1-2" buttons on the wiimote must be pressed at all times, done + by say some tape or gaffa!. + + + Scan for wiimote: + now comes the main part of the connection; scanning for a + wiimote. This includes bringing up the bluetooth radio device, + initialize seek parameters, scanning and matching for a device + name or address, connecting to a matched device, installing the + HID interface (that was removed in the first step), and finally + trying to open the wiimote and reading some data from it. All + steps involve a time variable, meaning that say installing the + HID causes some windows registry fiddling, that takes a rather + variable amount of time, and the next step is critically depen‐ + dent on the former step to be finished. Hence a number of tun‐ + able waiting variable is introduced, Important is to being able + to reach the final connection step before the wiimote goes out + of the discoverable mode and automatically turns off. + + + A note on USB voltage cycling: + This software solution is able to restart the wiimote in dis‐ + coverable mode automatically. But this require that the wiimote + is powered by a USB cable to the PC and that the "1-2" button + combination is permanently pressed. Cycling the power for the + wiimote with the "1-2" buttons pressed enables the discoverable + mode, and the continuously pressing "1-2" does not interfere + with wiimote operations hereafter. + + So this is a non-intrusive fix to the so called "Ladder prob‐ + lem" (http:wyxs.net/web/wiimote/digital_whiteboard.html). + + Power control over USB hubs is dependent on the particular hub + devices, and disabling the power may not be possible for a sin‐ + gle USB port only. wiiscan uses a trick of disabling all USB + hubs and then only enable and disable a singe hub. + + Disabling all hubs is a precondition to running wiiscan and it + can be accomblised by either going into "Control Panel | System + | Hardware | Device manager", and manually disable all USB hubs + or by using "devcon", say (careful, these commands applies only + to my system) + + devcon disable "@PCIN_8086&DEV_293*&SUB‐ + SYS_20F117AA&REV_033&B1BfB6*" + + devcon disable "@PCIN_8086&DEV_293*&SUB‐ + SYS_20F017AA&REV_033&B1BfB6*" + + being careful that the pattern matches only our USB hubs!. You + can test this by + + devcon status "@PCIN_8086&DEV_293*&SUB‐ + SYS_20F117AA&REV_033&B1BfB6*" + + devcon status "@PCIN_8086&DEV_293*&SUB‐ + SYS_20F017AA&REV_033&B1BfB6*" + + A single USB now may be enabled or disabled by + + devcon disable @PCIN_8086*DEV_2934*SUBSYS_20F017AA* + + again with the particular address for your system as a variable + you need to lookup. The voltage on the USB bus can now be + checked Using a voltmeter (see details in + http:wyxs.net/web/wiimote/digital_whiteboard.html). + + + NOTE: The configuration file of wiiscan contains the particular system + dependent USB hub pattern matches. + + + NOTE: be careful of connecting other devices to the USB bus, since the + power cycle may cause severe interfere with the device. + + +DESCRIPTION + -a  + auto-connect to a device. + + + -c  + connects to a device. + + + -d  + deletes a device, clears HID and bluetooth registry entries. + + + -r looks for active internal bluetooth radio devices. + + + -s scans for external bluetooth devices. + + + -usbup turn the USB hub on. + + + -usbdown + turn the USB hub off. + + + Default mode: wiiscan -a " Nintendo RVL-CNT-01" + + + Note: "nintendo" is a shortcut for "Nintendo RVL-CNT-01" + + +OPTIONS + -cf  + load a specific configuration file. + + + -lf  + specify a distinct logfile. + + + -b  + auto-mode bluetooth connection sleep in milliseconds. + + + -t  + bluetooth scanning interval in milliseconds. + + + -u  + auto-mode USB connection sleep in milliseconds. + + + -p  + automode usbm post-connection sleep in milliseconds. + + + -w  + timeout for wiimote in milliseconds. + + + -q  + use bluetooth radio with this address. Note, this functionality + is not working yet. + + + -f  + pre-remove mode of device, 0=remove if not connectable, + 1=always remove, 2=never remove + + + -m  + choose USB powercycle mode, 0=no power cycle, 1=use USB hub, + 2=use USBm IO hardware, 3=use USB Delcon IO hardware. + + + -l  + use specific wiimote library, lib can be one-of + (wiiuse,wiimotelib). + + + -y scan retries in automode. + + + -wb start whiteboard in auto-mode + + + -v enable extra debugging printouts + + +FILES + wiiscan looks for a file names wiiscan.ini when executing in the auto‐ + mode. See detail in the file. + +TIMING + Various timing parametes can be set on the command line or in the + inifile. The process of connecting is + + + 1: USB power down + + + 2: Delete old HID entries + + + 3: Sleep at least option_usbsleep (from 1) before commencing + + + 4: USB power up + + + 5: Sleep at least option_usbmsleep (from 3) before commencing. Sleep is + only nesseccary for USBmicro devices, since step 4 is much faster using + this device compared to the USB hub. + + + 6: Find and wiimote and connect. Scan bluetooth the duration in + option_timeout. + + + 7: Sleep option_btsleep before commencing; waiting for HID entries and + new-hardware-found to finish in windows. + + + 8: Connect to the wiimote, Scan for wiimotes using the duration + option_wiitimeout. + + +EXAMPLE + Scanning for devices nearby: + + wiiscan -s + + Auto-connect to a nintendo device, scan bluetooth for four seconds, + verbose on, and enable start of whiteboard software after a successful + connection + + wiiscan -a nintendo -t 4000 -v -wb + + Cycle USB bus voltage + + wiiscan -usbdown + + wiiscan -usbup + + +COMPILING + The source code can be compiled with MS Visual C++ Express 2008 + (http:www.microsoft.com/express/vc/) or similar. It also needs wiiuse + dlls (http:www.wiiuse.net/). If wiiuse is to be compiled by it self it + needs Windows SDK and DDK, but running wiiscan with just the wiiuse + binaries is the easiest option. + + It does not work on Windows 2000, and has not been tried out on a Vista + system. + + +TESTED SYSTEMS + The wiiscan has been tested on a Lenovo Thinkpad R500, XP professional + (without build-in bluetooth) with a Trendnet TBW-102UB bluetooth don‐ + gle, and on a ASUS eeePC 1000H with XP home. + + Only the MS bluetooth stack was tested. + + + Lenovo setup + Windows XP professional, version 2002, SP2 USB dongle: Trendnet + TBW-102UB bluetooth(Broadcom Ultimate Low Cost Bluetooth + 2.0+EDR USB), date 24-02-2004, driver 5.1.2535.0 Microsoft BT + stack: date 03-08-2004, driver 5.1.2600.2180 + + + eee setup + Windows XP home, version 2002, SP3 USB dongle: buildin Azware + BT252, date 13-04-2008, driver 5.1.2600.5512 Microsoft BT + stack: date 13-04-2008, driver 5.1.2600.5512 + + +BUGS + 1: restart pop-up (FIXED) + Installing new hardware causes windows to require restart. Hap‐ + pens once in a while, balloon pop-ups reports hardware, that + where installed but not working properly. A restart pop-up + wants to reboot the PC. Small fix: just delete the device and + re-run "wiiscan -c nintendo". + + FIX: the 'Change of systemsettings ... Do you want to reboot + now?' popup, happens only when the program runs for longer than + 19 seconds. Keeping the connection time within 19 sec seems to + cure the problem, Adding a sleep at the end of the program, say + Sleep(20000) will eventually bring up the dialog again. The + dialog can anyway safly be ignored! + + + 2: discoverable mode fast shutdown (FIXED) + Sometimes the wiimote goes quickly out of discoverable mode, it + takes it only about 3 seconds from turn-on to turn-off. This + makes it hard to obtain a connection to it. Both my wiimotes + does this once in a while, after failed connection attempts. + + Pressing one button only "1" or "2" makes the wiimote blink for + a short time, but it is not really discoverable. + + FIX: Initialization of wiilib has been rewritten, making it + recall the HID inialization routine. Code for testing the + wiimote connection has also been introduced. + + + 3: buttons not working (OPEN) + Pressing the 1-2 button combination sometimes fails to turn-on + the wiimote, pressing sync or power makes it work again. The + the "1-2 button freeze" happens after a failed connection + attempt. See also bug-2. + + + 4: radio null (OPEN) + Sometimes the BT radio fails to reinitialize after a USB + down/up flip, this means that "RadioInfo(time‐ + out,true,dbg)"return NULL. Can be fixed by placing the blue‐ + tooth radio device on another bus than the USB. + + + 5: keep blinking (OPEN) + Sometimes the wiimote is found and connected OK, but the LEDs + keeps blinking (normal connect mode: LEDs are turned perma‐ + nently on). This does however not affect connectability, and + the wiimote does not turn off again automatically. + + + 6: failed to find wiimote (OPEN) + Wiimote failed to find devices. This may be a non-fatal error + or an error caused by an undervolted wiimote. The + "wiiuse_find(0x0,4,2/4/6)" keeps returning 0. + + + 7: remove failed (OPEN) + Sometimes the remove steps fails, but this may be non-fatal + + Removing device + ** error: failed to find device + Done [FAILED] + + + 8; balloon-tips (FIXED) + Balloon-tips are annoying when connecting new hardware. Small + fix: do + + Windows Registry Editor Version 5.00 + + [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVer‐ + sion\Explorer1] + "EnableBalloonTips"=dword:00000000 + + + 9: double delete (FIXED) + Double delete of nintendo device may cause BluetoothFindFirst‐ + Device() to return null Fixed by removing the throw, replacing + it with a "if (hbf()==NULL) then return false" + + + 10: BSoD (OPEN) + The "Blue Screen of Death" was encountered a number of times, + indicating a errorneous device driver. The cause may be in the + MS bluetooth stack or in the wiiuse lib. The BSoD mainly + occured in the first phase of this project and havent been seen + for a while with the current version (v0.7). + + Only happens for MS bluetooth stack version "Microsoft BT + stack: date 03-08-2004, driver 5.1.2600.2180" (Lenovo version). + + + 11: New Hardware found wizard (OPEN) + Sometimes the wizard appears out of the blue, when deleting the + HID entry and trying to reinstall it. It is the call Bluetooth‐ + SetServiceState(...) that messes the Window system up. Dis‐ + abling the wizard or disabling the Plug and play system do not + seem to be an option, since the HID then newer get installed, + and ends up in a failty state. A reboot of the system does not + cure this defect. + + + 12: Devcon hangs (FIXED) + The devcon commands sometimes hangs at the diable USB command. + The state of the USB controllers, and BT devices are undefined + (some BT devices disabled, others not) and manually trying to + disable or enable the USB constroller fails. A status on the + USB device gives a strange result. + + FIX: reboot the machine. + + + 13: Open Device fails to find a Nintendo (PARTLY FIXED) + When opening a named nintendo device, say 'Nintendo RVL- + CNT-01', the bluetooth fails to get the name from the device. + This results in an empty name, and hence matching on the name + fails. + + FIX: use a device adresss instead of a name. Put, say + 'allowed_wiimote_adr=00:19:FD:CC:60:61 00:19:1D:D6:65:E5' in + the ini file. + + + 14: Wiiscan fails to run (FIXED) + Running wiiscan in a dosbox makes it terminate immidiatly. Run‐ + ning the tray version, or under MS VS Express causes it to dis‐ + play an missing DLL dialog (msvcp90.dll and friends). + + FIX: this is a MS bug, but can be fixed by setting the MT + library in the project setting to use static libraries instead + of dynamic DLLs. + + + 15: BluetoothFindFirstDevice() stalls (FIXED) + The BluetoothFindFirstDevice() sometimes newer returns. Happens + only for MS stack version "Microsoft BT stack: date 13-04-2008, + driver 5.1.2600.5512" (eee stack). + + Code stalls here: + + DeviceAutoClose hbf(BluetoothFind‐ + FirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + + FIX: start the BluetoothFindFirstDevice() function in a thread, + terminate the thread if it has run for longer than, say 2+time‐ + out. + + + 16: Wiimote drops connection after scanning (PARTLY FIXED) + If a connection attempt goes well until the last part, it is + most likely due to low-power batteries. The final stage of con‐ + necting draw extra power, that might cause the wiimote to shut‐ + down. + + +SEE ALSO + wiiuse Wiiuse is a library written in C that connects with several + Nintendo Wii remotes. Supports motion sensing, IR tracking, + nunchuk, classic controller, and the Guitar Hero 3 controller. + Single threaded and nonblocking makes a light weight and clean + API. + + Licensed under GNU GPLv3 and GNU LGPLv3 (non-commercial) by + Michael Laforest, + + (http:www.wiiuse.net/) + + + Wiimote Whiteboard + + Whiteboard software by Johnny Chung Lee. + + (http:www.cs.cmu.edu/~johnny/projects/wii/, + http:www.wiimoteproject.com/) + + + devcon(1) + + USB management software. + + (http:support.microsoft.com/kb/311272) + + +VERSION + Version 0.9 NDEBUG + + +AUTHOR + Carsten Frigaard, Mergeit ApS, Kongsvang Allé 37, DK-8000 Århus C, + www.mergit.dk + + +COPYRIGHT + Copyright © 2009 MergeIt, Aps. License LGPL3 : GNU lesser GPL, version + 3, . This is free software: you are + free to change and redistribute it. There is NO WARRANTY, to the extent + permitted by law. + + + + 26 Mar 2009 wiiscan(1) diff --git a/cpp/wiiscan/Doc/README.rtf b/cpp/wiiscan/Doc/README.rtf new file mode 100644 index 0000000..67b0697 --- /dev/null +++ b/cpp/wiiscan/Doc/README.rtf @@ -0,0 +1,25 @@ +Copyright © 2008 MergeIt, Aps. +License GPLv3+: GNU GPL version 3 or later . +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. + +Permission to use, copy, modify, and distribute this software +and its documentation for any purpose and without fee is hereby granted, provided that +the above copyright notice appear in all copies and that both that copyright notice +and this permission notice appear in supporting documentation. + +WIISCAN: + a connection utility for wii console remotes + +VERSION: + 0.9 + +INSTALL: + just run Release/wiiscan or Release/wiiscantray + +DOC: + read wiiscan.1 ("man ./wiiscan.1" or "more wiiscan.1.txt") + +NOTE: + modify the Release/wiiscan.ini file according to the USB hub device remarks in the documentation before runnnig wiiscan! + diff --git a/cpp/wiiscan/Doc/README_FIRST.rtf b/cpp/wiiscan/Doc/README_FIRST.rtf new file mode 100644 index 0000000..be7b0bd --- /dev/null +++ b/cpp/wiiscan/Doc/README_FIRST.rtf @@ -0,0 +1,30 @@ +Copyright © 2008 MergeIt, Aps. +License GPLv3+: GNU GPL version 3 or later . +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. + +Permission to use, copy, modify, and distribute this software +and its documentation for any purpose and without fee is hereby granted, provided that +the above copyright notice appear in all copies and that both that copyright notice +and this permission notice appear in supporting documentation. + +WIISCAN: + a connection utility for wii console remotes + +VERSION: + 0.9 + +INSTALL: + just run bin/Release/wiiscantray + +PREREQUISITES: + .NET framework 3.5 + Windows XP or above. + Microsoft Bluetooth stack. + +NOTE: + 1: Modify the Release/wiiscan.ini file according to the USB hub device remarks in + the documentation before runnnig wiiscan! + 2: If you are running a non-Microsoft Bluetooth stack (like Widcomm ), + you must follow the procedure found in the document doc/CHANGEINGBTSTACK.rtf + diff --git a/cpp/wiiscan/Doc/wiiscan.1 b/cpp/wiiscan/Doc/wiiscan.1 new file mode 100644 index 0000000..e1a54fd --- /dev/null +++ b/cpp/wiiscan/Doc/wiiscan.1 @@ -0,0 +1,358 @@ + +.TH wiiscan 1 "26 Mar 2009" + +.SH NAME + +\fIwiiscan\fP - a connection utility for wii console remotes + +.SH SYNOPSIS + +wiiscan <-a | -c | -d | -r | -s | -usbup | -usbdown> > [-cf ] [-lf ] [-b ] [-t ] [-u ] [-p ] [-w ] [-q ] [-f ] [-m ] [-l ] [-wb] [-v] + +.SH OVERVIEW + +\fIwiiscan\fP is a canvas function for a number of different scanning and connection utilities. It can detect build-in bluetooth radios, scan for nearby bluetooth devices, connect to a specific device and remove that device again from the hardware. + +The main feature of \fIwiiscan\fP is to automatically connect to a wii remote (wiimote). This can be quiet cumbersome on a Windows system, and the nesseccary steps for doing a robust, working connection is done by + +.TP 8 +.B Delete wiimote hardware HID bluetooth entry: +delete old entries of the wiimote in the bluetooth hardware. On some windows system the wiimote is readily detected, after the first manually installation. Pressing the "1-2" makes the wiimote discoverable. On other systems (including mine), any attempt to connect to the wiimote, after one successful or un-successful connection attempt, will always fail! Removing the HID and wiimote registry entries before the next attempt cures this feature. + +.TP 8 +.B Cycle USB bus: +the wiimote can be switched automatically to discoverable mode, if the power is briefly cut from the device. This can be done on a wiimote powered by the USB +5 volt (with a proper voltage regulator to bring it under +3 volt). This step hence turns the USB hub off, killing the power, and turns it on again. The "1-2" buttons on the wiimote must be pressed at all times, done by say some tape or gaffa!. + +.TP 8 +.B Scan for wiimote: +now comes the main part of the connection; scanning for a wiimote. This includes bringing up the bluetooth radio device, initialize seek parameters, scanning and matching for a device name or address, connecting to a matched device, installing the HID interface (that was removed in the first step), and finally trying to open the wiimote and reading some data from it. All steps involve a time variable, meaning that say installing the HID causes some windows registry fiddling, that takes a rather variable amount of time, and the next step is critically dependent on the former step to be finished. Hence a number of tunable waiting variable is introduced, Important is to being able to reach the final connection step before the wiimote goes out of the discoverable mode and automatically turns off. + +.TP 8 +A note on USB voltage cycling: +This software solution is able to restart the wiimote in discoverable mode automatically. But this require that the wiimote is powered by a USB cable to the PC and that the "1-2" button combination is permanently pressed. Cycling the power for the wiimote with the "1-2" buttons pressed enables the discoverable mode, and the continuously pressing "1-2" does not interfere with wiimote operations hereafter. + +So this is a non-intrusive fix to the so called "Ladder problem" (http:\/\/wyxs.net/web/wiimote/digital_whiteboard.html). + +Power control over USB hubs is dependent on the particular hub devices, and disabling the power may not be possible for a single USB port only. \fIwiiscan\fP uses a trick of disabling all USB hubs and then only enable and disable a singe hub. + +Disabling all hubs is a precondition to running \fIwiiscan\fP and it can be accomblised by either going into "Control Panel | System | Hardware | Device manager", and manually disable all USB hubs or by using "devcon", say (careful, these commands applies only to my system) + + devcon disable "@PCI\VEN_8086&DEV_293*&SUBSYS_20F117AA&REV_03\3&B1BfB6*" + + devcon disable "@PCI\VEN_8086&DEV_293*&SUBSYS_20F017AA&REV_03\3&B1BfB6*" + +being careful that the pattern matches only our USB hubs!. You can test this by + + devcon status "@PCI\VEN_8086&DEV_293*&SUBSYS_20F117AA&REV_03\3&B1BfB6*" + + devcon status "@PCI\VEN_8086&DEV_293*&SUBSYS_20F017AA&REV_03\3&B1BfB6*" + +A single USB now may be enabled or disabled by + + devcon disable @PCI\VEN_8086*DEV_2934*SUBSYS_20F017AA* + +again with the particular address for your system as a variable you need to lookup. The voltage on the USB bus can now be checked Using a voltmeter (see details in http:\/\/wyxs.net/web/wiimote/digital_whiteboard.html). + +.TP 8 +NOTE: The configuration file of wiiscan contains the particular system dependent USB hub pattern matches. + +.TP 8 +NOTE: be careful of connecting other devices to the USB bus, since the power cycle may cause severe interfere with the device. + +.SH DESCRIPTION + +.TP 8 +.B -a +auto-connect to a device. + +.TP 8 +.B -c +connects to a device. + +.TP 8 +.B -d +deletes a device, clears HID and bluetooth registry entries. + +.TP 8 +.B -r +looks for active internal bluetooth radio devices. + +.TP 8 +.B -s +scans for external bluetooth devices. + +.TP 8 +.B -usbup +turn the USB hub on. + +.TP 8 +.B -usbdown +turn the USB hub off. + +.TP 8 +Default mode: wiiscan -a " Nintendo RVL-CNT-01" + +.TP 8 +Note: "nintendo" is a shortcut for "Nintendo RVL-CNT-01" + +.SH OPTIONS + +.TP 8 +.B -cf +load a specific configuration file. + +.TP 8 +.B -lf +specify a distinct logfile. + +.TP 8 +.B -b +auto-mode bluetooth connection sleep in milliseconds. + +.TP 8 +.B -t +bluetooth scanning interval in milliseconds. + +.TP 8 +.B -u +auto-mode USB connection sleep in milliseconds. + +.TP 8 +.B -p +automode usbm post-connection sleep in milliseconds. + +.TP 8 +.B -w +timeout for wiimote in milliseconds. + +.TP 8 +.B -q +use bluetooth radio with this address. Note, this functionality is not working yet. + +.TP 8 +.B -f +pre-remove mode of device, 0=remove if not connectable, 1=always remove, 2=never remove + +.TP 8 +.B -m +choose USB powercycle mode, 0=no power cycle, 1=use USB hub, 2=use USBm IO hardware, 3=use USB Delcon IO hardware. + +.TP 8 +.B -l +use specific wiimote library, lib can be one-of (wiiuse,wiimotelib). + +.TP 8 +.B -y +scan retries in automode. + +.TP 8 +.B -wb +start whiteboard in auto-mode + +.TP 8 +.B -v +enable extra debugging printouts + +.SH FILES + +\fIwiiscan\fP looks for a file names wiiscan.ini when executing in the automode. See detail in the file. +.SH TIMING + +Various timing parametes can be set on the command line or in the inifile. The process of connecting is + +.TP 8 +1: USB power down + +.TP 8 +2: Delete old HID entries + +.TP 8 +3: Sleep at least option_usbsleep (from 1) before commencing + +.TP 8 +4: USB power up + +.TP 8 +5: Sleep at least option_usbmsleep (from 3) before commencing. Sleep is only nesseccary for USBmicro devices, since step 4 is much faster using this device compared to the USB hub. + +.TP 8 +6: Find and wiimote and connect. Scan bluetooth the duration in option_timeout. + +.TP 8 +7: Sleep option_btsleep before commencing; waiting for HID entries and new-hardware-found to finish in windows. + +.TP 8 +8: Connect to the wiimote, Scan for wiimotes using the duration option_wiitimeout. + +.SH EXAMPLE +Scanning for devices nearby: + + wiiscan -s + +Auto-connect to a nintendo device, scan bluetooth for four seconds, verbose on, and enable start of whiteboard software after a successful connection + + wiiscan -a nintendo -t 4000 -v -wb + +Cycle USB bus voltage + + wiiscan -usbdown + + wiiscan -usbup + +.SH COMPILING + +The source code can be compiled with MS Visual C++ Express 2008 (http:\/\/www.microsoft.com/express/vc/) or similar. It also needs wiiuse dlls (http:\/\/www.wiiuse.net/). If wiiuse is to be compiled by it self it needs Windows SDK and DDK, but running \fIwiiscan\fP with just the wiiuse binaries is the easiest option. + +It does not work on Windows 2000, and has not been tried out on a Vista system. + +.SH TESTED SYSTEMS + +The \fIwiiscan\fP has been tested on a Lenovo Thinkpad R500, XP professional (without build-in bluetooth) with a Trendnet TBW-102UB bluetooth dongle, and on a ASUS eeePC 1000H with XP home. + +Only the MS bluetooth stack was tested. + +.TP 8 +.B Lenovo setup +Windows XP professional, version 2002, SP2 +USB dongle: Trendnet TBW-102UB bluetooth(Broadcom Ultimate Low Cost Bluetooth 2.0+EDR USB), date 24-02-2004, driver 5.1.2535.0 +Microsoft BT stack: date 03-08-2004, driver 5.1.2600.2180 + +.TP 8 +.B eee setup +Windows XP home, version 2002, SP3 +USB dongle: buildin Azware BT252, date 13-04-2008, driver 5.1.2600.5512 +Microsoft BT stack: date 13-04-2008, driver 5.1.2600.5512 + +.SH BUGS + +.TP 8 +.B 1: restart pop-up (FIXED) +Installing new hardware causes windows to require restart. Happens once in a while, balloon pop-ups reports hardware, that where installed but not working properly. A restart pop-up wants to reboot the PC. Small fix: just delete the device and re-run "wiiscan -c nintendo". + +FIX: the 'Change of systemsettings ... Do you want to reboot now?' popup, happens only when the program runs for longer than 19 seconds. Keeping the connection time within 19 sec seems to cure the problem, Adding a sleep at the end of the program, say Sleep(20000) will eventually bring up the dialog again. The dialog can anyway safly be ignored! + +.TP 8 +.B 2: discoverable mode fast shutdown (FIXED) +Sometimes the wiimote goes quickly out of discoverable mode, it takes it only about 3 seconds from turn-on to turn-off. This makes it hard to obtain a connection to it. Both my wiimotes does this once in a while, after failed connection attempts. + +Pressing one button only "1" or "2" makes the wiimote blink for a short time, but it is not really discoverable. + +FIX: Initialization of wiilib has been rewritten, making it recall the HID inialization routine. Code for testing the wiimote connection has also been introduced. + +.TP 8 +.B 3: buttons not working (OPEN) +Pressing the 1-2 button combination sometimes fails to turn-on the wiimote, pressing sync or power makes it work again. The the "1-2 button freeze" happens after a failed connection attempt. See also bug-2. + +.TP 8 +.B 4: radio null (OPEN) +Sometimes the BT radio fails to reinitialize after a USB down/up flip, this means that "RadioInfo(timeout,true,dbg)"return NULL. Can be fixed by placing the bluetooth radio device on another bus than the USB. + +.TP 8 +.B 5: keep blinking (OPEN) +Sometimes the wiimote is found and connected OK, but the LEDs keeps blinking (normal connect mode: +LEDs are turned permanently on). This does however not affect connectability, and the wiimote does not turn off again automatically. + +.TP 8 +.B 6: failed to find wiimote (OPEN) +Wiimote failed to find devices. This may be a non-fatal error or an error caused by an undervolted wiimote. The "wiiuse_find(0x0,4,2/4/6)" keeps returning 0. + +.TP 8 +.B 7: remove failed (OPEN) +Sometimes the remove steps fails, but this may be non-fatal + + Removing device + ** error: failed to find device + Done [FAILED] + +.TP 8 +.B 8; balloon-tips (FIXED) +Balloon-tips are annoying when connecting new hardware. Small fix: do + + Windows Registry Editor Version 5.00 + + [HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\Advanced] + "EnableBalloonTips"=dword:00000000 + +.TP 8 +.B 9: double delete (FIXED) +Double delete of nintendo device may cause BluetoothFindFirstDevice() to return null Fixed by removing the throw, replacing it with a "if (hbf()==NULL) then return false" + +.TP 8 +.B 10: BSoD (OPEN) +The "Blue Screen of Death" was encountered a number of times, indicating a errorneous device driver. The cause may be in the MS bluetooth stack or in the wiiuse lib. The BSoD mainly occured in the first phase of this project and havent been seen for a while with the current version (v0.7). + +Only happens for MS bluetooth stack version "Microsoft BT stack: date 03-08-2004, driver 5.1.2600.2180" (Lenovo version). + +.TP 8 +.B 11: New Hardware found wizard (OPEN) +Sometimes the wizard appears out of the blue, when deleting the HID entry and trying to reinstall it. It is the call BluetoothSetServiceState(...) that messes the Window system up. Disabling the wizard or disabling the Plug and play system do not seem to be an option, since the HID then newer get installed, and ends up in a failty state. A reboot of the system does not cure this defect. + +.TP 8 +.B 12: Devcon hangs (FIXED) +The devcon commands sometimes hangs at the diable USB command. The state of the USB controllers, and BT devices are undefined (some BT devices disabled, others not) and manually trying to disable or enable the USB constroller fails. A status on the USB device gives a strange result. + +FIX: reboot the machine. + +.TP 8 +.B 13: Open Device fails to find a Nintendo (PARTLY FIXED) +When opening a named nintendo device, say 'Nintendo RVL-CNT-01', the bluetooth fails to get the name from the device. This results in an empty name, and hence matching on the name fails. + +FIX: use a device adresss instead of a name. Put, say 'allowed_wiimote_adr=00:19:FD:CC:60:61 00:19:1D:D6:65:E5' in the ini file. + +.TP 8 +.B 14: Wiiscan fails to run (FIXED) +Running wiiscan in a dosbox makes it terminate immidiatly. Running the tray version, or under MS VS Express causes it to display an missing DLL dialog (msvcp90.dll and friends). + +FIX: this is a MS bug, but can be fixed by setting the MT library in the project setting to use static libraries instead of dynamic DLLs. + +.TP 8 +.B 15: BluetoothFindFirstDevice() stalls (FIXED) +The BluetoothFindFirstDevice() sometimes newer returns. Happens only for MS stack version +"Microsoft BT stack: date 13-04-2008, driver 5.1.2600.5512" (eee stack). + +Code stalls here: + +DeviceAutoClose hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + +FIX: start the BluetoothFindFirstDevice() function in a thread, terminate the thread if it has run for longer than, say 2+timeout. + +.TP 8 +.B 16: Wiimote drops connection after scanning (PARTLY FIXED) +If a connection attempt goes well until the last part, it is most likely due to low-power batteries. The final stage of connecting draw extra power, that might cause the wiimote to shutdown. + +.SH SEE ALSO + +.TP 8 +.B wiiuse + +Wiiuse is a library written in C that connects with several Nintendo Wii remotes. Supports motion sensing, IR tracking, nunchuk, classic controller, and the Guitar Hero 3 controller. Single threaded and nonblocking makes a light weight and clean API. + +Licensed under GNU GPLv3 and GNU LGPLv3 (non-commercial) by Michael Laforest, + +(http:\/\/www.wiiuse.net/) + +.TP 8 +.B Wiimote Whiteboard + +Whiteboard software by Johnny Chung Lee. + +(http:\/\/www.cs.cmu.edu/~johnny/projects/wii/, http:\/\/www.wiimoteproject.com/) + +.TP 8 +.B devcon(1) + +USB management software. + +(http:\/\/support.microsoft.com/kb/311272) + +.SH VERSION + +Version 0.9 NDEBUG + +.SH AUTHOR + +Carsten Frigaard, Mergeit ApS, Kongsvang Allé 37, DK-8000 Århus C, www.mergit.dk + +.SH COPYRIGHT + +Copyright © 2009 MergeIt, Aps. License LGPL3 : GNU lesser GPL, version 3, . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. diff --git a/cpp/wiiscan/Lib/Debug/WiimoteLib.dll b/cpp/wiiscan/Lib/Debug/WiimoteLib.dll new file mode 100644 index 0000000..da8e126 Binary files /dev/null and b/cpp/wiiscan/Lib/Debug/WiimoteLib.dll differ diff --git a/cpp/wiiscan/Lib/Debug/WiimoteWhiteboard.exe b/cpp/wiiscan/Lib/Debug/WiimoteWhiteboard.exe new file mode 100644 index 0000000..28f2c9f Binary files /dev/null and b/cpp/wiiscan/Lib/Debug/WiimoteWhiteboard.exe differ diff --git a/cpp/wiiscan/Lib/Debug/wiimotelibpoll.exe b/cpp/wiiscan/Lib/Debug/wiimotelibpoll.exe new file mode 100644 index 0000000..72937a2 Binary files /dev/null and b/cpp/wiiscan/Lib/Debug/wiimotelibpoll.exe differ diff --git a/cpp/wiiscan/Lib/DelcomDLL.dll b/cpp/wiiscan/Lib/DelcomDLL.dll new file mode 100644 index 0000000..ecfa470 Binary files /dev/null and b/cpp/wiiscan/Lib/DelcomDLL.dll differ diff --git a/cpp/wiiscan/Lib/DelcomDLL.lib b/cpp/wiiscan/Lib/DelcomDLL.lib new file mode 100644 index 0000000..0c540c6 Binary files /dev/null and b/cpp/wiiscan/Lib/DelcomDLL.lib differ diff --git a/cpp/wiiscan/Lib/Release/WiimoteLib.dll b/cpp/wiiscan/Lib/Release/WiimoteLib.dll new file mode 100644 index 0000000..4408358 Binary files /dev/null and b/cpp/wiiscan/Lib/Release/WiimoteLib.dll differ diff --git a/cpp/wiiscan/Lib/Release/WiimoteWhiteboard.exe b/cpp/wiiscan/Lib/Release/WiimoteWhiteboard.exe new file mode 100644 index 0000000..1ca21b8 Binary files /dev/null and b/cpp/wiiscan/Lib/Release/WiimoteWhiteboard.exe differ diff --git a/cpp/wiiscan/Lib/Release/wiimotelibpoll.exe b/cpp/wiiscan/Lib/Release/wiimotelibpoll.exe new file mode 100644 index 0000000..7e13580 Binary files /dev/null and b/cpp/wiiscan/Lib/Release/wiimotelibpoll.exe differ diff --git a/cpp/wiiscan/Lib/USBm.dll b/cpp/wiiscan/Lib/USBm.dll new file mode 100644 index 0000000..1460ff3 Binary files /dev/null and b/cpp/wiiscan/Lib/USBm.dll differ diff --git a/cpp/wiiscan/Lib/wiiuse.dll b/cpp/wiiscan/Lib/wiiuse.dll new file mode 100644 index 0000000..a7e228c Binary files /dev/null and b/cpp/wiiscan/Lib/wiiuse.dll differ diff --git a/cpp/wiiscan/Lib/wiiuse.lib b/cpp/wiiscan/Lib/wiiuse.lib new file mode 100644 index 0000000..4f1b69b Binary files /dev/null and b/cpp/wiiscan/Lib/wiiuse.lib differ diff --git a/cpp/wiiscan/Release/DelcomDLL.dll b/cpp/wiiscan/Release/DelcomDLL.dll new file mode 100644 index 0000000..ecfa470 Binary files /dev/null and b/cpp/wiiscan/Release/DelcomDLL.dll differ diff --git a/cpp/wiiscan/Release/USBm.dll b/cpp/wiiscan/Release/USBm.dll new file mode 100644 index 0000000..1460ff3 Binary files /dev/null and b/cpp/wiiscan/Release/USBm.dll differ diff --git a/cpp/wiiscan/Release/WiimoteLib.dll b/cpp/wiiscan/Release/WiimoteLib.dll new file mode 100644 index 0000000..4408358 Binary files /dev/null and b/cpp/wiiscan/Release/WiimoteLib.dll differ diff --git a/cpp/wiiscan/Release/wiimotelibpoll.exe b/cpp/wiiscan/Release/wiimotelibpoll.exe new file mode 100644 index 0000000..7e13580 Binary files /dev/null and b/cpp/wiiscan/Release/wiimotelibpoll.exe differ diff --git a/cpp/wiiscan/Release/wiiscan.exe b/cpp/wiiscan/Release/wiiscan.exe new file mode 100644 index 0000000..9e9e443 Binary files /dev/null and b/cpp/wiiscan/Release/wiiscan.exe differ diff --git a/cpp/wiiscan/Release/wiiscan.ini b/cpp/wiiscan/Release/wiiscan.ini new file mode 100644 index 0000000..8833211 --- /dev/null +++ b/cpp/wiiscan/Release/wiiscan.ini @@ -0,0 +1,81 @@ +% Configfile_begin + % config file for wiiscan + + % all usb device ids, not used yet in the scanning + all_usb_hubs="" + + % single power controlling usb hub, only nessesary when powerering over usb, for automatic resetting. If not used, put "" + % active_usb_hub="@PCI\VEN_8086&DEV_2934&SUBSYS_20F017AA&REV_03\3&B1BFB68&0&E8" + active_usb_hub="" + + % a list of allowed wiimotes in the form xx:xx:xx:xx:xx:xx, wildcard match is 00:00:00:00:00:00 + %allowed_wiimote_adr=00:00:00:00:00:00 + % my set of wiimotes + allowed_wiimote_adr=00:19:?D:??:6?:?? 00:1F:C5:??:??:?? 00:17:AB:39:69:87 + + % launching exe file have a problem with spaces, hence renamed exe, replaced space with underscores + % The original Johnny Chung Lee wb software version 0.3, downloaded from [http://johnnylee.net/projects/wii/] + % may not work yet, missing library integration + %whiteboard_software="d:/WiimoteWhiteboard/WiimoteWhiteboard_v0.3.exe" + + % same stuff, but binary build from sources and redistributed in this package + whiteboard_software="WiimoteWhiteboard.exe" + + % Boon Jin's wb software version 0.6, does not work yet, missing library integration + %whiteboard_software="d:/wsb/WiimoteSmoothboard.exe" + + % wiiscan parameters + %option_device="Nintendo RVL-CNT-01" + + % number of bt scanning attempts in a autoscan, timout is increased for every scan as retryattempt*option_timeout + option_scanretries=1 + + % long time (4000 and above, or increase option_scanretries) when matching name, short (1000-2000) when matching address + option_timeout=4000 + + % wiimote scanning time, only applicable used for wiilib + option_wiitimeout=1000 + + % delay bewteen powerdown/up, see man for details + option_usbsleep=1000 + option_usbmsleep=1000 + %option_btsleep=2000 + + % start wb at successfull connect, 0 or 1 + option_startwhiteboard=1 + + % output verbose flag, 0 or 1 + option_debug=1 + + % logging output file, put nothing if no logging is wanted + option_logfile=wiiscan_log.txt + %option_logfile= + + % wiimote connection library, use none, wiiuse, or wiimotelib + option_wiilib=wiimotelib + + % 0=no power cycle, 1=USB hub control, 2=USBm IO, 3=USB Devcon + option_usbpowermode=3 + + % 0=remove if not connectable, 1=always remove, 2=never remove + %option_forceremove=0 + + % bt radio to use, not working yet + %option_btradio=00:50:B6:A0:48:8C + + % wiiscantray options + % number of connection attempts before autoscan i restarted + option_tray_pool_retries=2 + + % retry sleep, keep it low (<1000) to catch an open connection + option_tray_sleep_between_pool_retries=250 + + % sleep bewteen polls, when wiimote is succeful connected, keep it hight (>2000) to avoid to much CPU usage + option_tray_sleep_between_pool_loops=2000 + + % final connection step reqires some sleep before windows PnP is ready + option_sleep_before_final_connect=1500 + + % delay after launch of wb software, keep it at a medium value (2000-10000) + option_sleep_after_startwhiteboard=4000 +% Configfile_end diff --git a/cpp/wiiscan/Release/wiiuse.dll b/cpp/wiiscan/Release/wiiuse.dll new file mode 100644 index 0000000..a7e228c Binary files /dev/null and b/cpp/wiiscan/Release/wiiuse.dll differ diff --git a/cpp/wiiscan/Tools/disable_all_usb.sh b/cpp/wiiscan/Tools/disable_all_usb.sh new file mode 100644 index 0000000..56a70d0 --- /dev/null +++ b/cpp/wiiscan/Tools/disable_all_usb.sh @@ -0,0 +1,2 @@ +devcon disable "@PCI\VEN_8086&DEV_293*&SUBSYS_20F117AA&REV_03\3&B1BfB6*" +devcon disable "@PCI\VEN_8086&DEV_293*&SUBSYS_20F017AA&REV_03\3&B1BfB6*" diff --git a/cpp/wiiscan/Tools/enable_all_usb.sh b/cpp/wiiscan/Tools/enable_all_usb.sh new file mode 100644 index 0000000..2fb50dc --- /dev/null +++ b/cpp/wiiscan/Tools/enable_all_usb.sh @@ -0,0 +1,2 @@ +devcon enable "@PCI\VEN_8086&DEV_293*&SUBSYS_20F117AA&REV_03\3&B1BfB6*" +devcon enable "@PCI\VEN_8086&DEV_293*&SUBSYS_20F017AA&REV_03\3&B1BfB6*" diff --git a/cpp/wiiscan/Tools/no_error_report.reg b/cpp/wiiscan/Tools/no_error_report.reg new file mode 100644 index 0000000..fb092ca --- /dev/null +++ b/cpp/wiiscan/Tools/no_error_report.reg @@ -0,0 +1,10 @@ +REGEDIT4 + +[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PCHealth\ErrorReporting] +"AllOrNone"=dword:00000001 +"IncludeMicrosoftApps"=dword:00000001 +"IncludeWindowsApps"=dword:00000001 +"IncludeKernelFaults"=dword:00000001 +"DoReport"=dword:00000000 +"ShowUI"=dword:00000000 + diff --git a/cpp/wiiscan/Tools/popup_disable.reg b/cpp/wiiscan/Tools/popup_disable.reg new file mode 100644 index 0000000..9be1735 Binary files /dev/null and b/cpp/wiiscan/Tools/popup_disable.reg differ diff --git a/cpp/wiiscan/Tools/popup_enable.reg b/cpp/wiiscan/Tools/popup_enable.reg new file mode 100644 index 0000000..a8ace89 Binary files /dev/null and b/cpp/wiiscan/Tools/popup_enable.reg differ diff --git a/cpp/wiiscan/Tools/wiitest.sh b/cpp/wiiscan/Tools/wiitest.sh new file mode 100644 index 0000000..bc7ea53 --- /dev/null +++ b/cpp/wiiscan/Tools/wiitest.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +MODE=Release +if [ $# = 1 ]; then + MODE=$1 + echo using mode $MODE +fi + +N=0 +F=0 +S=0 +N_MAX=1000000 +OUTFILE=$MODE/wiitest.out + +echo "##" Testing wiiscan... "(N_MAX="$N_MAX",OUTFILE="$OUTFILE",mode="$MODE")" | tee $OUTFILE +echo -n "## date " | tee -a $OUTFILE +date | tee -a $OUTFILE + +while [ $N -lt $N_MAX ]; do + let N=N+1 + echo "##" Loop $N " " | tee -a $OUTFILE + echo -n "## date " | tee -a $OUTFILE + date | tee -a $OUTFILE + + rm -f $OUTFILE.tmp 2>/dev/null + + pushd $MODE ; wiiscan -f -v -lf ../$OUTFILE.tmp 2>&1 ; R1=$? ; popd + # pushd $MODE ; wiiscan -v -d nintendo -lf ../$OUTFILE.tmp ; R2=$? ; popd + R2=0; + + cat $OUTFILE.tmp >> $OUTFILE + + if [ $R1 = 0 ]; then + let S=S+1 + echo "##" Test succes $S | tee -a $OUTFILE + else + let F=F+1 + echo "##" Test failed $F | tee -a $OUTFILE + fi + + STATUS=$(echo $S*100/$N | bc) + echo "##" Status $S/$N = $STATUS "%" | tee -a $OUTFILE + echo "## (result R1="$R1 " R2="$R2 " S="$S " F="$F " N="$N ")" | tee -a $OUTFILE + + sleep 4 +done + +echo done | tee -a $OUTFILE +exit 0 diff --git a/cpp/wiiscan/src/DelcomDLL.h b/cpp/wiiscan/src/DelcomDLL.h new file mode 100644 index 0000000..8fd16b5 --- /dev/null +++ b/cpp/wiiscan/src/DelcomDLL.h @@ -0,0 +1,128 @@ +// Delcom Engineering +// DelcomDLL.h +// Version 0.6 +// 02/11/07 + +#ifndef DELCOMDLL_HEADER +#define DELCOMDLL_HEADER 1 + + +// defines +#define OFF 0 +#define ON 1 +#define FLASH 2 + +#define USBIODS 1 +#define USBDELVI 2 +#define USBNDSPY 3 + +#define GREENLED 0 +#define REDLED 1 +#define BLUELED 2 + +#define MaxDeviceLen 512 + +// Typedef +typedef struct _DeviceNameStruct{ + char DeviceName[MaxDeviceLen]; +} DeviceNameStruct, *pDeviceNameStruct; + + +typedef unsigned long DWORD; +typedef int BOOL; +typedef unsigned char BYTE; +typedef char CHAR; +typedef unsigned short WORD; +typedef void * HANDLE; +typedef CHAR *LPSTR, *PSTR; +typedef unsigned char uchar; +typedef unsigned int uint; + +typedef struct _PacketStruct{ + uchar Recipient; + uchar DeviceModel; + uchar MajorCmd; + uchar MinorCmd; + uchar DataLSB; + uchar DataMSB; + short Length; + uchar ExtData[8]; + } PacketStruct,*pPacketStruct; + + + + +// DLL Generic Functions +HANDLE DelcomGetDLLHandle(void); +double __stdcall DelcomGetDLLVersion(void); +DWORD __stdcall DelcomGetDLLDate(LPSTR); +DWORD __stdcall DelcomVerboseControl( DWORD Mode, LPSTR Header ); + +DWORD __stdcall DelcomGetNthDevice(DWORD, DWORD, LPSTR); +DWORD __stdcall DelcomScanDevices( DWORD, DeviceNameStruct[], DWORD ); +HANDLE __stdcall DelcomOpenDevice( LPSTR, DWORD); +DWORD __stdcall DelcomCloseDevice(HANDLE); +DWORD __stdcall DelcomGetDeviceCount( DWORD Type ); + +DWORD __stdcall DelcomReadDeviceVersion(HANDLE ); +DWORD __stdcall DelcomReadDeviceSerialNum(LPSTR, HANDLE ); +DWORD __stdcall DelcomSendPacket( HANDLE, pPacketStruct, pPacketStruct); + + +// USBDELVI - Visual Indicator Functions +DWORD __stdcall DelcomLEDControl( HANDLE hFile, DWORD Color, DWORD Mode ); +DWORD __stdcall DelcomEnableAutoConfirm(HANDLE hUsb, DWORD Mode); +DWORD __stdcall DelcomEnableAutoClear(HANDLE hUsb, DWORD Mode); +DWORD __stdcall DelcomBuzzer(HANDLE hUsb, uchar Mode, uchar Freq, uchar Repeat, uchar OnTime, uchar OffTime); +DWORD __stdcall DelcomLoadInitialPhaseDelay(HANDLE hUsb, BYTE Color, BYTE Delay); +DWORD __stdcall DelcomSyncLeds(HANDLE hUsb); +DWORD __stdcall DelcomLoadPreScalar(HANDLE hUsb, BYTE PreScalar); +DWORD __stdcall DelcomLoadLedFreqDuty(HANDLE hUsb, BYTE Color, BYTE Low, BYTE High); +DWORD __stdcall DelcomGetButtonStatus(HANDLE hUsb ); +DWORD __stdcall DelcomReadVersionNumber(HANDLE hUsb ); +DWORD __stdcall DelcomReadSerialNumber(HANDLE hUsb ); +DWORD __stdcall DelcomLEDPower( HANDLE hUsb, DWORD Color, DWORD Power ); + + + +// USBIODS - USB IO Functions +DWORD __stdcall DelcomWritePortPin(HANDLE hUsb, BYTE Port, BYTE Pin, BYTE Value ); +DWORD __stdcall DelcomWritePorts(HANDLE hUsb, BYTE Port0, BYTE Port1 ); +DWORD __stdcall DelcomReadPorts(HANDLE hUsb, BYTE* Port0, BYTE* Port1 ); + +DWORD __stdcall DelcomWrite64Bit(HANDLE hUsb, LPSTR DataExt ); +DWORD __stdcall DelcomRead64Bit(HANDLE hUsb, LPSTR DataExt ); + +DWORD __stdcall DelcomWriteI2C(HANDLE hUsb, BYTE CmdAdd, BYTE Length, LPSTR DataExt ); +DWORD __stdcall DelcomReadI2C(HANDLE hUsb, BYTE CmdAdd, BYTE Length, LPSTR DataExt ); +DWORD __stdcall DelcomSelReadI2C(HANDLE hUsb, BYTE SetAddCmd, BYTE Address, BYTE ReadCmd, BYTE Length, LPSTR DataExt ); +DWORD __stdcall DelcomWriteI2CEEPROM(HANDLE hUsb, DWORD Address, DWORD Length, BYTE CtrlCode, BYTE WriteDelay, LPSTR pData ); +DWORD __stdcall DelcomReadI2CEEPROM(HANDLE hUsb, DWORD Address, DWORD Length, BYTE CtrlCode, LPSTR pData ); + + +DWORD __stdcall DelcomRS232Ctrl(HANDLE hUsb, DWORD Mode, DWORD Value ); +DWORD __stdcall DelcomWriteRS232(HANDLE hUsb, DWORD Length, LPSTR DataExt ); +DWORD __stdcall DelcomReadRS232(HANDLE hUsb, LPSTR DataExt ); + + // SPI Commands +DWORD __stdcall DelcomSPIWrite( HANDLE hUsb, DWORD ClockCount, LPSTR pData); +DWORD __stdcall DelcomSPISetClock( HANDLE hUsb, DWORD ClockPeriod ); +DWORD __stdcall DelcomSPIRead(HANDLE hUsb, LPSTR pData); +DWORD __stdcall DelcomSPIWr8Read64( HANDLE hUsb, DWORD WrData, DWORD ClockCount, LPSTR pData); + + + +// USBNDSPY Functions +DWORD __stdcall DelcomNumericMode(HANDLE hUsb, BYTE Mode, BYTE Rate ); +DWORD __stdcall DelcomNumericScanRate(HANDLE hUsb, BYTE ScanRate ); +DWORD __stdcall DelcomNumericSetup(HANDLE hUsb, BYTE Digits ); +DWORD __stdcall DelcomNumericRaw(HANDLE hUsb, LPSTR Str ); +DWORD __stdcall DelcomNumericInteger(HANDLE hUsb, DWORD Number, DWORD Base ); +DWORD __stdcall DelcomNumericHexaDecimal(HANDLE hUsb, DWORD Number, DWORD Base ); +DWORD __stdcall DelcomNumericDouble(HANDLE hUsb, double Number, DWORD Base ); + + +#endif + + + diff --git a/cpp/wiiscan/src/args.h b/cpp/wiiscan/src/args.h new file mode 100644 index 0000000..21ae941 --- /dev/null +++ b/cpp/wiiscan/src/args.h @@ -0,0 +1,143 @@ +// The code is copyrighted 2008 by Carsten Frigaard. +// All rights placed in public domain under GNU licence V2, 2008 +// +// © 2008 Carsten Frigaard. Permission to use, copy, modify, and distribute this software +// and its documentation for any purpose and without fee is hereby granted, provided that +// the above copyright notice appear in all copies and that both that copyright notice +// and this permission notice appear in supporting documentation. + +#ifndef __ARGS_H__ +#define __ARGS_H__ + +class args{ + private: + typedef vector t_args; + t_args m_args; + + void Remove(t_args::iterator itt1,int n) + { + t_args args2; + for(t_args::iterator itt2=m_args.begin();itt2!=m_args.end();++itt2) { + if(itt2!=itt1) args2.push_back(*itt2); + else {if (--n>0) ++itt1;} + } + m_args=args2; + } + + t_args::iterator Find(const string& v) + { + for(t_args::iterator itt=m_args.begin();itt!=m_args.end();++itt) { + if (v==*itt) return itt; + } + return m_args.end(); + } + + t_args::const_iterator Find(const string& v) const + { + for(t_args::const_iterator itt=m_args.begin();itt!=m_args.end();++itt) { + if (v==*itt) return itt; + } + return m_args.end(); + } + + public: + args(const int argc,char **argv,const bool printargs=false) + { + for(int i=0;i=size()) throw_("argument[] out of range"); + return m_args[i]; + } + + size_t size() const {return m_args.size();} + bool hasopt(const string& tag) const {return Find(tag)!=m_args.end();} + template T Totype(const size_t i) const {return totype((*this)[i]);} + template T Tounit(const size_t i) const { + const string& s=(*this)[i+1]; + #ifndef USE_UNITS + // fake test of units, primitive test, must be one-of: kpc, per_kpc and msun1E10 + if (s!="kpc/h" && s!="h/kpc" && s!="msun1E10/h") throw_("bad unit=<" + s + ">, can only handle units of type: kpc/h, h/kpc, and msun1E10/h"); + #endif + return totype((*this)[i] + " " + s); + } + + template + T parseval(const string& tag,const T& defaultval) + { + t_args::iterator itt=Find(tag); + if (itt==m_args.end() || itt+1==m_args.end()) return defaultval; + + const T v=totype(*(++itt)); + Remove(--itt,2); + + return v; + } + + template + pair parseval(const string& tag,const T& defaultval1,const R& defaultval2) + { + t_args::iterator itt=Find(tag); + if (itt==m_args.end() || itt+1==m_args.end() || itt+2==m_args.end()) return make_pair(defaultval1,defaultval2); + + const T v1=totype(*(++itt)); + const R v2=totype(*(++itt)); + Remove(----itt,3); + + return make_pair(v1,v2); + } + + template + T parseunit(const string& tag,const T& defaultval) + { + t_args::iterator itt=Find(tag); + if (itt==m_args.end() || itt+2==m_args.end()) return defaultval; + + const string s1=*(++itt); + const string s2=*(++itt); + const T v=totype(s1 + " " + s2); + Remove(----itt,3); + + return v; + } + + bool parseopt(const string& tag) + { + t_args::iterator itt=Find(tag); + if (itt==m_args.end()) return false; + Remove(itt,1); + + return true; + } + + int getoptindex(const string& tag) const + { + int n=0; + for(t_args::const_iterator itt=m_args.begin();itt!=m_args.end();++itt,++n) { + if (tag==*itt) { + assert( n>=0 && size_t(n)m_args.size() || i+n>m_args.size()) throw_("out of range"); + m_args.erase(m_args.begin()+i,m_args.begin()+i+n); + } + + friend ostream& operator<<(ostream& s,const args& v) + { + for(t_args::const_iterator itt=v.m_args.begin();itt!=v.m_args.end();++itt){ + s << *itt << " "; + } + return s; + } +}; + +#endif // __ARGS_H__ diff --git a/cpp/wiiscan/src/configfile.h b/cpp/wiiscan/src/configfile.h new file mode 100644 index 0000000..574d738 --- /dev/null +++ b/cpp/wiiscan/src/configfile.h @@ -0,0 +1,143 @@ +// The code is copyrighted 2008 by Carsten Frigaard. +// All rights placed in public domain under GNU licence V2, 2008 +// +// © 2008 Carsten Frigaard. Permission to use, copy, modify, and distribute this software +// and its documentation for any purpose and without fee is hereby granted, provided that +// the above copyright notice appear in all copies and that both that copyright notice +// and this permission notice appear in supporting documentation. + +#ifndef __CONFIGFILE_H__ +#define __CONFIGFILE_H__ + +class Configfile { + private: + typedef vector t_value; + t_value m_text; + map m_vals; + string m_rem; + string m_strip; + string m_file; + + void Load(istream &s) + { + if (!s) throw_("bad stream in configfile input operator"); + char buff[32*1024]; + bool ini=true; + while(s.getline(buff,32*1024)) { + const string b(strip(buff)); + if (ini && b!=m_rem + " Configfile_begin") throw_("bad stream in configfile input operator, missing begin tag"); + if (b==m_rem + " Configfile_end") return; + const string t(strip(removerems(b,m_rem))); + if (t.size()>0){ + m_text.push_back(t); + const size_t n=t.find_first_of("="); + istringstream s2(t.substr(0,n) + " " + (n==string::npos ? "" : t.substr(n+1))); + string v,k; + s2 >> k; + while ((s2 >> v)) m_vals[k].push_back(v); + } + ini=false; + } + throw_("bad stream in configfile input operator, missing end tag"); + } + + string Strip(const string& s) const + { + for(size_t i=0;i does not exist"); + Load(s); + } + + Configfile(istream& s,const string rem="%") : m_rem(rem) + { + if (!s) throw_("stream is invalid"); + Load(s); + } + + size_t size() const {return m_text.size();} + const string& operator[](const size_t n) const {assert(n::const_iterator itt=m_vals.find(key); return itt!=m_vals.end();} + void Save(const string& filename) {ofstream s(filename.c_str()); s << *this;} + + bool operator==(const Configfile& c) const + { + if (m_text!=c.m_text) return false; + else if (m_vals!=c.m_vals) return false; + else if (m_rem!=c.m_rem) return false; + else if (m_strip!=c.m_strip) return false; + else if (m_file!=c.m_file) return false; + return true; + } + bool operator!=(const Configfile& c) const {return !this->operator==(c);} + + void Checkfilechange() + { + if (m_file.empty()) return; + Configfile c(m_file); + if (c!=*this) *this=c; + } + + const t_value& Get(const string& key) const + { + map::const_iterator itt=m_vals.find(key); + if (itt==m_vals.end()) throw_("No such entry, <" + key + ">, in configfile"); + return itt->second; + } + + template const T Get(const string& key,const bool fullline=false) const + { + const t_value& v=Get(key); + assert( v.size()>0 ); + string s=v[0]; + if (fullline) for(size_t i=1;i(s); + return t; + } + + template const T Initialize(const string& key,const T valiueifnotfound,const bool fullline=false) const + { + if (!hasEntry(key)) return valiueifnotfound; + else return Get(key,fullline); + } + + template + pair GetConfig(const string& e) const + { + if (!hasEntry(e)) return make_pair(false,T()); + else return make_pair(true,Get(e,true)); + } + + template void Set(const string& key,const T& v) + { + t_value val; + val.push_back(tostring(v)); + m_vals[key]=val; + m_text.push_back(key + " " + val[0]); + } + + friend ostream& operator<<(ostream& s,const Configfile& x) + { + s << x.m_rem << " Configfile_begin\n"; + for(map::const_iterator itt1=x.m_vals.begin();itt1!=x.m_vals.end();++itt1){ + s << "\t" << itt1->first << " = "; + for(t_value::const_iterator itt2=itt1->second.begin();itt2!=itt1->second.end();++itt2) s << *itt2 << " "; + s << "\n"; + } + s << x.m_rem << " Configfile_end\n"; + // if (!s) throw_("bad stream in configfile output operator");, XXX throws in cout!? + return s; + } + + friend istream& operator>>(istream& s,Configfile& x){x.Load(s); return s;} +}; +#endif // __CONFIGFILE_H__ diff --git a/cpp/wiiscan/src/exception.h b/cpp/wiiscan/src/exception.h new file mode 100644 index 0000000..5721a37 --- /dev/null +++ b/cpp/wiiscan/src/exception.h @@ -0,0 +1,197 @@ +// The code is copyrighted 2008 by Carsten Frigaard. +// All rights placed in public domain under GNU licence V2, 2008 +// +// © 2008 Carsten Frigaard. Permission to use, copy, modify, and distribute this software +// and its documentation for any purpose and without fee is hereby granted, provided that +// the above copyright notice appear in all copies and that both that copyright notice +// and this permission notice appear in supporting documentation. + +#ifndef __EXCEPTION_H__ +#define __EXCEPTION_H__ + +// forward defs to funs.h +string Getlocaltime(); +size_t GetThreadId(); +int Message(const string& title,const string& msg,const int type=0); + +// simple class for debugging call stack +#ifdef _DEBUG + struct Stackinfo : public vector { + friend ostream& operator<<(ostream& s,const Stackinfo& x) + { + s << "Function stack {" << endl; + for(int i=x.size();i>0;--i){ + const string& f=x[i-1]; + s << " [" << i-1 << "]: " << f << "(...)" << endl; + } + return s << "}" << endl; + } + }; + + class Funstack { + private: + const string m_f; + static map m_s; + + Funstack(const Funstack&); + void operator=(const Funstack&); + + public: + Funstack(const string& f,const int line,const string& file) : m_f(f) {m_s[GetThreadId()].push_back(f);} + ~Funstack() + { + const size_t tid=GetThreadId(); + assert(m_s.find(tid)!=m_s.end()); + assert(m_s[tid].size()>0 && m_s[tid].back()==m_f); + m_s[tid].pop_back(); + } + + static const Stackinfo GetStack() + { + const size_t tid=GetThreadId(); + if (m_s.find(tid)==m_s.end()) return Stackinfo(); + else return m_s[tid]; + } + }; + map Funstack::m_s; // initialize static var + #define FUNSTACK Funstack __f_stack__(__FUNCTION__,__LINE__,__FILE__) +#else + #define FUNSTACK + #define DUMPSTACK(s) + struct Stackinfo { + friend ostream& operator<<(ostream& s,const Stackinfo& x) {return s;} + }; + class Funstack { + public: + static Stackinfo GetStack() {return Stackinfo();} + }; +#endif + +// tee like logger class +class Logger +{ +private: + //ostream* m_log; + string m_logfilename; + ofstream m_logfile; + ostream* m_log; + const bool m_logstdout,m_logstderr; + + Logger(const Logger&); + void operator=(const Logger&); + +public: + Logger(ostream* log,const bool logstdout=true,const bool logstderr=false) : m_log(log), m_logstdout(logstdout), m_logstderr(logstderr) {} + + void operator=(ostream* log){m_logfilename=""; m_logfile.close(); m_log=log;} + void open(const string& logfilename,const ios_base::openmode mode) + { + m_log=0; + if (m_logfile.is_open()) m_logfile.close(); + m_logfilename=logfilename; + m_logfile.open(m_logfilename.c_str(),mode); + if (!m_logfile) throw("cannot write to logfile <" + logfilename + ">"); // Exception uses logger class, so do not throw a nice Exception class here, use a plain throw + m_log=&m_logfile; + } + + void clear() + { + if (m_logfile.is_open()) m_logfile.close(); + m_logfile.open(m_logfilename.c_str()); + } + + template + friend Logger& operator<<(Logger& log,const T& t) + { + if(log.m_logstdout) cout << t; + if(log.m_logstderr) cerr << t; + if(log.m_log!=0) (*(log.m_log)) << t; + return log; + } + + // handle endl and like + friend Logger& operator<<(Logger& log,std::ostream& (*fn)(std::ostream&)) + { + if(log.m_logstdout) fn(cout); + if(log.m_logstderr) fn(cerr); + if(log.m_log!=0) fn(*(log.m_log)); + return log; + } + + void writelogheader(const string& msg) + { + if(m_log==0) return; + else{ + (*m_log) << "********************************************************" << endl; + (*m_log) << "** Logentry: " << msg << endl; + (*m_log) << "** Time: " << Getlocaltime(); + (*m_log) << "********************************************************" << endl; + } + } +}; + +// static global logging, default standand out +static Logger g_log(0,true,false); + +class Exception{ +private: + const string m_msg; + const string m_file; + const int m_line; + const Stackinfo m_stack; + +public: + Exception(const string msg,const string file,const int line,const Stackinfo s) : m_msg(msg), m_file(file), m_line(line), m_stack(s) {} + Exception(const char* msg,const string file,const int line,const Stackinfo s) : m_msg(msg), m_file(file), m_line(line), m_stack(s) {} + + inline static string FormatCompilerMsg(const string& file,const int line,const bool warnonly=false) + { + #ifdef WIN32 + return file + "(" + Exception::tostring(line) + ") : " + (warnonly ? "warning : " : "error : "); + #else + return file + ":" + Exception::tostring(line) + ": " + (warnonly ? "warning: " : "error: "); + #endif + } + + inline static void throw_fun(const string& msg,const string& file,const int line) + { + const string msg2=Exception::FormatCompilerMsg(file,line) + "throwing exception: " + msg; + g_log << msg2; + #ifdef _WINDOWS_ + const string f=tostring(Funstack::GetStack()); + const string msg3=msg2 + (f.size()>0 ? "\n\n" : "") + f.substr(0,300) + (f.size()>300 ? "\n..." : ""); + Message("Exception encountered...",msg3,0); + #endif + throw Exception(msg,file,line,Funstack::GetStack()); + } + + inline string Msg() const + { + return FormatCompilerMsg(m_file,m_line) + "Exception: " + m_msg; + } + + friend ostream& operator<<(ostream& os,const Exception& e) + { + return os << e.Msg() << endl << e.m_stack; + } + + template + static string tostring(const T& x) + { + ostringstream os; + os << x; + return os.str(); + } +}; + +#define throw_(msg) Exception::throw_fun(msg,__FILE__, __LINE__) +#define warn_(msg) cerr << Exception::FormatCompilerMsg(__FILE__, __LINE__,true) << msg << endl; + +#define CATCH_ALL\ + catch(const char* s) {cout.flush(); cerr << Exception::FormatCompilerMsg(__FILE__, __LINE__) << "caught exception chars: " << s;}\ + catch(const string& s) {cout.flush(); cerr << Exception::FormatCompilerMsg(__FILE__, __LINE__) << "caught exception string: " << s;}\ + catch(const Exception& s){cout.flush(); cerr << Exception::FormatCompilerMsg(__FILE__, __LINE__) << "caught Exception class: " << s;}\ + catch(...) {cout.flush(); cerr << Exception::FormatCompilerMsg(__FILE__, __LINE__) << "caught unknown exception";}\ + cerr << "...aborting" << endl; + +#endif // __EXCEPTION_H__ diff --git a/cpp/wiiscan/src/file.h b/cpp/wiiscan/src/file.h new file mode 100644 index 0000000..915f6ed --- /dev/null +++ b/cpp/wiiscan/src/file.h @@ -0,0 +1,208 @@ +// The code is copyrighted 2008 by Carsten Frigaard. +// All rights placed in public domain under GNU licence V2, 2008 +// +// © 2008 Carsten Frigaard. Permission to use, copy, modify, and distribute this software +// and its documentation for any purpose and without fee is hereby granted, provided that +// the above copyright notice appear in all copies and that both that copyright notice +// and this permission notice appear in supporting documentation. + +#ifndef __FILE_H__ +#define __FILE_H__ + +inline bool FileExists(const std::string& f) +{ + ifstream s(f.c_str()); + return (!s)==false; +} + +inline void AssertFileExists(const std::string& f) +{ + if (!FileExists(f)) throw_("File <" + f + "> does not exist"); +} + +inline size_t FileSize(const std::string& f) +{ + AssertFileExists(f); + ifstream s(f.c_str()); + if (!s) throw_("Stream is bad (file <" + f + ">)"); + s.seekg(0,ios::end); + if (!s) throw_("Stream is bad (file <" + f + ">)"); + return s.tellg(); +} + +inline void AssertFileNotEmpty(const std::string& f) +{ + if (FileSize(f)==0) throw_("File <" + f + "> is empty"); +} + +inline size_t FileTime(const string& file) +{ + AssertFileExists(file); + const string t=System("date -r " + file + " +%s",true,true); // seconds since 1970-01-01 00:00:00 UTC + return totype(t); + // #include + // #include + // struct stat s; + // int n=stat(file.c_str(),&s); + // if (n!=0) throw_("cannot stat file <" + file + ">"); + // assert( sizeof(time_t)==sizeof(size_t) ); + // return s.st_mtime; +} + +inline bool isFileNewer(const string& file0,const string& file1) +{ + return FileTime(file0)>FileTime(file1); +} + +inline bool DirExists(const std::string& f) +{ + const string file=f + "/.dummy.txt"; + ofstream s(file.c_str()); + if(!s) return false; + s << "testfile"; + if(!s) return false; + s.close(); + return FileSize(file)==8; +} + +inline string MakeSuffix(const int n) +{ + assert(n>=0); + if (n<=9) return "00" + tostring(n); + else if (n<=99) return "0" + tostring(n); + else return tostring(n); +} + +template +void Readdata(const string& tok,istream& is,T& t) +{ + if (!is) throw_("Stream is bad"); + is >> t; + if (!is) throw_("Reading {" + tok + "} settings"); +} + +inline string Readline(istream& is) +{ + char buff[16*1024]; + is.getline(buff,16*1024); + return string(buff); +} + +template +inline T Readtyp(ifstream& s){ + T x; + s.read(reinterpret_cast(&x),sizeof(x)); + if(!s) throw_("bad stream"); + return x; +} + +inline string Readstring(ifstream& s){ + char c=0; + string t; + do{ + c=Readtyp(s); + if(c!=0) t+=c; + } + while (c!=0); + return t; +} + +template +inline vector Readbin(std::ifstream& s,const int size) +{ + if(!s) throw_("bad stream"); + vector x(size); + s.read(reinterpret_cast(&x.front()),x.size()*sizeof(T)); + if(!s) throw_( "bad write"); + return x; +} + +template +inline void Writetyp(ofstream& s,const T& x){ + s.write(reinterpret_cast(&x),sizeof(x)); + if(!s) throw_( "bad stream"); +} + +template +inline void Writebin(std::ofstream& s,const std::vector& x,const bool writetag) +{ + if(!s) throw_("bad stream"); + const size_t sz=x.size()*sizeof(T); + if(writetag){ + Writetyp(s,sz); + } + if(!s) throw_( "bad stream" ); + s.write(reinterpret_cast(&x.front()),sz); + if(!s) throw_( "bad write"); + if (writetag) Writetyp(s,sz); + if(!s) throw_( "bad stream"); +} + +template +inline void Writebin(std::ofstream& s,const std::map& x,const bool writetag) +{ + vector t; + vector r; + t.reserve(x.size()); + r.reserve(x.size()); + for(typename std::map::const_iterator itt=x.begin();itt!=x.end();++itt){ + t.push_back(itt->first); + r.push_back(itt->second); + } + if (writetag) { + Writetyp(s,x.size()); + Writetyp(s,static_cast(sizeof(T))); + Writetyp(s,static_cast(sizeof(R))); + } + Writebin(s,t,writetag); + Writebin(s,r,writetag); +} + +template +inline void Readbin(std::ifstream& s,vector& x) +{ + if(!s) throw_("bad stream"); + const size_t sz=Readtyp(s); + if(!s) throw_( "bad stream" ); + if(sz%sizeof(T)!=0) throw_("bad size tag"); + x.resize(sz/sizeof(T)); + s.read(reinterpret_cast(&x.front()),sz); + if(!s) throw_( "bad write"); + if (Readtyp(s)!=sz) throw_("bad size tag"); + if(!s) throw_( "bad stream"); +} + +template +inline void Readbin(std::ifstream& s,map& x) +{ + vector t; + vector r; + + const size_t sz=Readtyp(s); + const size_t szT=Readtyp(s); + const size_t szR=Readtyp(s); + + if (szT!=sizeof(T)) throw_("type T size mismatch in Readbin (map)"); + if (szR!=sizeof(R)) throw_("type R size mismatch in Readbin (map)"); + + Readbin(s,t); + Readbin(s,r); + if (t.size()!=r.size()) throw_("size mismatch in Readbin (map)"); + x.clear(); + for(size_t i=0;i +inline void Writeascii(const string& filename,const std::vector& x,const string& comment="",const char& newline='\n') +{ + ofstream s(filename.c_str()); + if(!s) throw_("bad file <" + filename + ">"); + s << "% Writeascii: size=" << x.size() << " " << comment << "\n"; + for(size_t i=0;i"); +} + +#endif // __FILE_H__ diff --git a/cpp/wiiscan/src/funs.h b/cpp/wiiscan/src/funs.h new file mode 100644 index 0000000..2d61630 --- /dev/null +++ b/cpp/wiiscan/src/funs.h @@ -0,0 +1,413 @@ +// The code is copyrighted 2008 by Carsten Frigaard. +// All rights placed in public domain under GNU licence V2, 2008 +// +// © 2008 Carsten Frigaard. Permission to use, copy, modify, and distribute this software +// and its documentation for any purpose and without fee is hereby granted, provided that +// the above copyright notice appear in all copies and that both that copyright notice +// and this permission notice appear in supporting documentation. + +#ifndef __FUNS_H__ +#define __FUNS_H__ + +#define Unimplemented throw_("Function unimplemented") +#define Dontgethere throw_("Dontgethere") + +#ifndef NDEBUG + #define ON_DEBUG(a) a +#else + #define ON_DEBUG(a) +#endif + +#ifdef USE_FFTW + extern int posix_memalign(void **__memptr, size_t __alignment, size_t __size); +#endif + +// Small template funs +template bool isinmap (const map& m,const T& t) {return m.size()>=2 && m.begin()->first<=t && t<(--m.end())->first;} +template const size_t getsteps(const T& r0,const T& r1,const T& rstep,const bool logarithmic) {size_t N=0; for(T r=r0;r(&one))); + if (big) s+=" BIGENDIAN"; + else s+=" LITENDIAN"; + + return s; +} + +inline string FormatCompilerMsg(const string& file,const int line) +{ + #ifdef WIN32 + return file + ":" + line + ":"; + #else + return file + "(" + line + ")"; + #endif +} + +#ifdef WIN32 + extern "C"{ + #ifdef _UNICODE + #pragma message("ERROR: toolsfun cannot handle unicode...giving up compilation"); + ERROR_complier + #endif + + #ifdef _AFXDLL + __declspec(dllimport) void* __stdcall GetCurrentThread(); + __declspec(dllimport) void* __stdcall GetCurrentThreadId(); + __declspec(dllimport) int __stdcall SetThreadPriority(void* hThread,int nPriority); + #else + //void* __stdcall GetCurrentThread(); + //void* __stdcall GetCurrentThreadId(); + //int __stdcall SetThreadPriority(void* hThread,int nPriority); + __declspec(dllimport) void* __stdcall GetCurrentThread(); + __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(); + __declspec(dllimport) int __stdcall SetThreadPriority(void* hThread,int nPriority); + __declspec(dllimport) int __stdcall GetThreadPriority(void* hThread); + __declspec(dllimport) unsigned int __stdcall WinExec(const char* lpCmdLine,unsigned int uCmdShow); + #ifndef _WINDOWS_ + __declspec(dllimport) unsigned int __stdcall MessageBox(void* hWnd,const char* lpText,const char* lpCaption,unsigned int Type); + __declspec(dllimport) unsigned long __stdcall GetCurrentDirectory(unsigned long nBufferLength,char* lpBuffer); + #endif + #endif + } +#else + int nice(int inc); +#endif + +inline int Message(const string& title,const string& msg,const int type) +{ +#ifdef WIN32 + // 0 = MB_OK + // 1 = MB_OKCANCEL + // 2 = MB_ABORTRETRYIGNORE + // 3 = MB_YESNOCANCEL + // 4 = MB_YESNO + // 5 = MB_RETRYCANCEL + // 6 = MB_CANCELTRYCONTINUE: if(WINVER >= 0x0500) + const long hr=MessageBox(NULL,msg.c_str(),title.c_str(),type); +#else + Unimplemented; +#endif + return 1; +} +/* +string GetCurrentDir() +{ +#ifdef WIN32 + char buff[16*1024]; + if(GetCurrentDirectory(16*1024,buff)==0) throw_("GetCurrentDirectory() failed"); + return tostring(buff); +#else + Unimplemented; + return ""; +#endif +} +*/ + +inline void SetNiceLevel(const int level) +{ + #ifdef WIN32 + // THREAD_PRIORITY_ABOVE_NORMAL 1 Priority 1 point above the priority class. + // THREAD_PRIORITY_BELOW_NORMAL -1 Priority 1 point below the priority class. + // THREAD_PRIORITY_HIGHEST 2 Priority 2 points above the priority class. + // THREAD_PRIORITY_IDLE -15 Base priority of 1 for IDLE_PRIORITY_CLASS,... + // THREAD_PRIORITY_LOWEST -2 Priority 2 points below the priority class. + // THREAD_PRIORITY_NORMAL 0 Normal priority for the priority class. + // THREAD_PRIORITY_TIME_CRITICAL 15 Base priority of 15 for IDLE_PRIORITY_CLASS,... + if (level!=0 && level!=1 && level!=-1 && level!=2 && level!=-2 && level!=15 && level!=-15) throw_("wrong Win32 nice level, must be oneof -15,-2,-1,0,1,2,15"); + SetThreadPriority(GetCurrentThread(),-level); + assert( GetThreadPriority(GetCurrentThread())==-level ); + #else + const int n=nice(level); + if (n<0) throw_("Could not set nice level"); + #endif +} + +inline size_t GetThreadId() +{ + #ifdef WIN32 + assert( sizeof(size_t)==sizeof(unsigned long) ); + return GetCurrentThreadId(); + #else + // may be replaced by return 0; if phtread not found! + assert( sizeof(size_t)==sizeof(pthread_t) ); + const pthread_t p=pthread_self(); + size_t q=0; + memcpy(&q,&p,sizeof(q)); + return q; + #endif +} + +#ifdef TOOLSFUN_QUIET_WIN32_SYSTEM +#ifdef WIN32 + // make a special non-console system call, instead of the standard system() + // XXX SystemWin, does not wait for process to finish, CreatProc still create window + + int SystemWin(const string& cmd) + { + //ShellExecute, CreateProcess, WinExec or system + // HINSTANCE hi=ShellExecute(NULL,NULL,cmdx,"","",SW_SHOSNOACTIVATE); + // if (reinterpret_cast(hi)<32) MessageBox(NULL,(string("ShellExecute <") + cmd + "> failed").c_str(),"Error",0); + + // const string cmd2="\\\"" + cmd + "\\\""; // fix problem with spaces in executable, not working yet + unsigned int r=WinExec(cmd.c_str(),4); + return r<32 ? -1 : 0; + } + + bool CreateProc(const string& cmd,const bool throwexception=true,const bool waitforprocesstofinish=true) + { + STARTUPINFO s; + PROCESS_INFORMATION p; + memset(&s,0,sizeof(s)); + memset(&p,0,sizeof(p)); + s.cb=sizeof(s); + + // to avoid const cast of char* in CreateProcess + char cmdx[16*1024]; + strcpy_s(cmdx,16*1024,cmd.c_str()); + + const int r=CreateProcess(0,cmdx,0,0,false,CREATE_DEFAULT_ERROR_MODE,0,0,&s,&p); + if (r!=0) {if(waitforprocesstofinish) WaitForSingleObject(p.hProcess,INFINITE);} + else { + if (throwexception) throw_(string("CreateProcess() failed with return code <") + GetLastError() + ">"); + else return false; + } + + // Release handles + assert(r!=0); + CloseHandle(p.hProcess); + CloseHandle(p.hThread); + return true; + } +#endif +#endif + +inline string System(const string& cmd,const bool throwexception=true,const bool captureoutput=false,int* pret=0) +{ + if (!captureoutput){ + #ifdef TOOLSFUN_QUIET_WIN32_SYSTEM + const int n=SystemWin(cmd); + #else + const int n=system(cmd.c_str()); + #endif + if (n!=0 && throwexception) throw_(string("system command failed with code=") + n + " cmd=<" + cmd + ">"); + if (pret!=0) *pret=n; + return ""; + } else { + static size_t n=0; + #ifdef WIN32 + const string rm="del "; + char tmp[1024]; + if (tmpnam_s(tmp,1024)) throw_("error in creating win32 temp name"); + const string file(tmp); + #else + const string rm="rm "; + const string file=tmpnam("tempfile"); + #endif + ifstream s1(file.c_str()); + if(s1) { + s1.close(); + System((rm + file).c_str(),true,false); + } + System(cmd + " > " + file,throwexception,false,pret); + + string t; + char buff[16*1024]; + ifstream s2(file.c_str()); + while(s2) { + s2.getline(buff,16*1024); + if (s2) t += buff; + } + s2.close(); + System((rm + file).c_str(),true,false); + return t; + } +} + +string Getlocaltime() +{ + FUNSTACK; + time_t rawtime; + time(&rawtime); + struct tm timeinfo; + localtime_s(&timeinfo,&rawtime); + char buff[1024]; + asctime_s(buff,1014,&timeinfo); + return string(buff); +} + +class timer +{ + private: + double m_t,m_cpu_t; + double* m_addt; + mutable double m_last_t,m_last_eta; + + static double gettime() + { + #ifdef WIN32 + return 1.0*clock()/CLOCKS_PER_SEC; // use low-res clock + // FILETIME ft; + // unsigned __int64 tmpres = 0; + // + // GetSystemTimeAsFileTime(&ft); + // + // tmpres |= ft.dwHighDateTime; + // tmpres <<= 32; + // tmpres |= ft.dwLowDateTime; + // + // converting file time to unix epoch + // tmpres -= DELTA_EPOCH_IN_MICROSECS; + // tmpres /= 10; // convert into microseconds + // tv->tv_sec = (long)(tmpres / 1000000UL); + // tv->tv_usec = (long)(tmpres % 1000000UL); + #else + struct timeval tv; + gettimeofday(&tv,NULL); + return tv.tv_sec + static_cast(tv.tv_usec)/1000000; + #endif + } + + static double getcputime() + { + static const double f=1.0/CLOCKS_PER_SEC; + return f*clock(); + } + + template + static double Remaining(const double t,const T& n,const T& N) + { + if (n>=N || N<=T(0)) throw_("value out of range in timer::Remaining, n>=N or N<=0, n=" + tostring(n) + " N=" + tostring(N)); + const double p=static_cast(n/T(1)+1)/(N/T(1)); + const double p2=p>0 ? t/p : 0; + return p2>t ? p2-t : 0; + } + + public: + timer() : m_t(gettime()), m_cpu_t(getcputime()), m_addt(0), m_last_t(-1), m_last_eta(-1) {} + timer(double& t) : m_t(gettime()), m_cpu_t(getcputime()), m_addt(&t), m_last_t(-1), m_last_eta(-1) {} + ~timer() {if (m_addt!=0) (*m_addt) += elapsed();} + + void reset () {m_t=gettime(); m_cpu_t=getcputime(); m_last_t=-1; m_last_eta=-1;} + double elapsed() const {return gettime()-m_t;} + double cputime() const {return getcputime()-m_cpu_t;} + + static string ToHMS(const double& t) + { + assert( t>=0 ); + const unsigned int it=static_cast(t+.5); + const unsigned int hours=it/(60*60); + const unsigned int mins=(it-hours*60*60)/(60); + const unsigned int secs=(it-hours*60*60-mins*60); + assert( secs<60 && mins<60); + return tostring(hours) + ":" + (mins<10 ? "0": "") + tostring(mins) + ":" + (secs<10 ? "0": "") + tostring(secs); + } + + template static inline int Topercent(const T x,const T N,const int decimals=-1) + { + assert(x(x/T(1))/(N/T(1)); + if (decimals>0) pf=static_cast(pf*decimals)/(1.0*decimals); + return static_cast(pf*100+.5); + } + + template void ToEta(const T& n,const T& N,ostream& s,const double timeprintsteps=30,const bool verboseprint=false) const + { + if (n>=N) return; + assert( ntimeprintsteps) { + const double f=timeprintsteps*60; + const double r=Remaining(t,n,N); + if (m_last_eta<0 || rf && e>f) /*|| (m_last_eta>0 && r>1.2*m_last_eta) */ ){ + time_t tm; + time(&tm); + const string systime=ctime(&tm); + if (m_last_eta<0) s << "Current system time: " << systime; + tm += static_cast(r); + const string eta=ctime(&tm); + const bool extraday=(eta.substr(0,3)!=systime.substr(0,3)); + const string eday=extraday ? " "+eta.substr(0,3)+" " : ""; + s << "Time [h:m:s]=" << ToHMS(t) << ", R=" << ToHMS(r) << ", ETA=" << eday << eta.substr(11,8); + if(verboseprint) { + const string t=", n/N=" + tostring(n) + "/" + tostring(N) + "=" + tostring(Topercent(n,N,10)) + " percent,"; + s << t; + for(size_t i=t.size();i<42;++i) s << " "; + s << " CPU=" << ToHMS(cputime()); + } + s << endl; + + m_last_t=t; + m_last_eta=r; + } + } + } + friend ostream& operator<<(ostream& s,const timer x) + { + return s << "Time [h:m:s]= " << x.ToHMS(x.elapsed()) << " CPU=" << x.ToHMS(x.cputime()); + } +}; + +/* +int SwapEndian(void *data,const size_t size) { + + short xs; + long xl; + + switch (size){ + case 2: + xs = *(short *)data; + *(short *)data = ( ((xs & 0x0000ff00) >> 8) | ((xs & 0x000000ff) << 8) ); + break; + case 4: + xl = *(long *)data; + *(long *)data = ( ((xl & 0xff000000) >> 24) | ((xl & 0x00ff0000) >> 8) | + ((xl & 0x0000ff00) << 8) | ((xl & 0x000000ff) << 24) ); + break; + default: break; + } + + return 0; +} +*/ + +#endif // __FUNS_H__ diff --git a/cpp/wiiscan/src/process.h b/cpp/wiiscan/src/process.h new file mode 100644 index 0000000..77ffc52 --- /dev/null +++ b/cpp/wiiscan/src/process.h @@ -0,0 +1,282 @@ +#ifndef __PROCESS_H__ +#define __PROCESS_H__ + +#include + +pair ProcessWalkInit() +{ + // Take a snapshot of all processes in the system. + HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); + if( hProcessSnap == INVALID_HANDLE_VALUE ) throw_("CreateToolhelp32Snapshot() returned invalid handle"); + + // Set the size of the structure before using it. + PROCESSENTRY32 pe32; + pe32.dwSize = sizeof(PROCESSENTRY32); + + // Retrieve information about the first process, + // and exit if unsuccessful + if(!Process32First(hProcessSnap,&pe32)) { + CloseHandle(hProcessSnap); // clean the snapshot object + throw_("Process32First"); // show cause of failure + } + + return make_pair(hProcessSnap,pe32); +} + +DWORD GetProcessID(const string& processname) +{ + pair h=ProcessWalkInit(); + HANDLE hProcessSnap=h.first; + PROCESSENTRY32 pe32=h.second; + + do { + if (pe32.szExeFile==processname) { + CloseHandle(hProcessSnap); + return pe32.th32ProcessID; + } + } while( Process32Next(hProcessSnap,&pe32) ); + + CloseHandle(hProcessSnap); + return 0; +} + +PROCESSENTRY32 GetProcessInfo(const DWORD id) +{ + pair h=ProcessWalkInit(); + HANDLE hProcessSnap=h.first; + PROCESSENTRY32 pe32=h.second; + + int n=0; + do { + if (pe32.th32ProcessID==id) { + CloseHandle(hProcessSnap); + return pe32; + } + } while( Process32Next(hProcessSnap,&pe32) ); + + CloseHandle(hProcessSnap); + ZeroMemory( &pe32, sizeof(pe32) ); + pe32.dwSize = sizeof(PROCESSENTRY32); + + return pe32; +} + +DWORD GetProcessCount(const string& processname) +{ + pair h=ProcessWalkInit(); + HANDLE hProcessSnap=h.first; + PROCESSENTRY32 pe32=h.second; + int n=0; + do { + if (pe32.szExeFile==processname) ++n; + } while( Process32Next(hProcessSnap,&pe32) ); + + CloseHandle(hProcessSnap); + return n; +} + +bool KillProcessID(const DWORD id) +{ + HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, id); + if(hProcess==NULL) return false; // might have gone in the meantime, so no throw_("OpenProcess() got null handle"); + + const BOOL t=TerminateProcess(hProcess,-1); + CloseHandle(hProcess); + return t!=0; +} + +void KillAllProcesses(const string& exe) +{ + // kill existing polls + DWORD id=GetProcessID(exe); + while(id!=0){ + KillProcessID(id); + id=GetProcessID(exe); + } +} + +/* +#include +#include +#include + +BOOL GetProcessList( ); +BOOL ListProcessModules( DWORD dwPID ); +BOOL ListProcessThreads( DWORD dwOwnerPID ); +void printError( TCHAR* msg ); + +FILE* log=fopen("d:\\plog.txt","w"); + +BOOL GetProcessList( ) +{ + HANDLE hProcessSnap; + HANDLE hProcess; + PROCESSENTRY32 pe32; + DWORD dwPriorityClass; + + // Take a snapshot of all processes in the system. + hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); + if( hProcessSnap == INVALID_HANDLE_VALUE ) + { + printError( TEXT("CreateToolhelp32Snapshot (of processes)") ); + return( FALSE ); + } + + // Set the size of the structure before using it. + pe32.dwSize = sizeof( PROCESSENTRY32 ); + + // Retrieve information about the first process, + // and exit if unsuccessful + if( !Process32First( hProcessSnap, &pe32 ) ) + { + printError( TEXT("Process32First") ); // show cause of failure + CloseHandle( hProcessSnap ); // clean the snapshot object + return( FALSE ); + } + + // Now walk the snapshot of processes, and + // display information about each process in turn + do + { + fprintf(log, "\n\n=====================================================" ); + fprintf(log, TEXT("\nPROCESS NAME: %s"), pe32.szExeFile ); + fprintf(log, "\n-----------------------------------------------------" ); + + // Retrieve the priority class. + dwPriorityClass = 0; + hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID ); + if( hProcess == NULL ) + printError( TEXT("OpenProcess") ); + else + { + dwPriorityClass = GetPriorityClass( hProcess ); + if( !dwPriorityClass ) + printError( TEXT("GetPriorityClass") ); + CloseHandle( hProcess ); + } + + fprintf(log, "\n Process ID = 0x%08X", pe32.th32ProcessID ); + fprintf(log, "\n Thread count = %d", pe32.cntThreads ); + fprintf(log, "\n Parent process ID = 0x%08X", pe32.th32ParentProcessID ); + fprintf(log, "\n Priority base = %d", pe32.pcPriClassBase ); + if( dwPriorityClass ) + fprintf(log, "\n Priority class = %d", dwPriorityClass ); + + // List the modules and threads associated with this process + ListProcessModules( pe32.th32ProcessID ); + ListProcessThreads( pe32.th32ProcessID ); + + } while( Process32Next( hProcessSnap, &pe32 ) ); + + CloseHandle( hProcessSnap ); + return( TRUE ); +} + + +BOOL ListProcessModules( DWORD dwPID ) +{ + HANDLE hModuleSnap = INVALID_HANDLE_VALUE; + MODULEENTRY32 me32; + + // Take a snapshot of all modules in the specified process. + hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID ); + if( hModuleSnap == INVALID_HANDLE_VALUE ) + { + printError( TEXT("CreateToolhelp32Snapshot (of modules)") ); + return( FALSE ); + } + + // Set the size of the structure before using it. + me32.dwSize = sizeof( MODULEENTRY32 ); + + // Retrieve information about the first module, + // and exit if unsuccessful + if( !Module32First( hModuleSnap, &me32 ) ) + { + printError( TEXT("Module32First") ); // show cause of failure + CloseHandle( hModuleSnap ); // clean the snapshot object + return( FALSE ); + } + + // Now walk the module list of the process, + // and display information about each module + do + { + fprintf(log, TEXT("\n\n MODULE NAME: %s"), me32.szModule ); + fprintf(log, TEXT("\n Executable = %s"), me32.szExePath ); + fprintf(log, "\n Process ID = 0x%08X", me32.th32ProcessID ); + fprintf(log, "\n Ref count (g) = 0x%04X", me32.GlblcntUsage ); + fprintf(log, "\n Ref count (p) = 0x%04X", me32.ProccntUsage ); + fprintf(log, "\n Base address = 0x%08X", (DWORD) me32.modBaseAddr ); + fprintf(log, "\n Base size = %d", me32.modBaseSize ); + + } while( Module32Next( hModuleSnap, &me32 ) ); + + CloseHandle( hModuleSnap ); + return( TRUE ); +} + +BOOL ListProcessThreads( DWORD dwOwnerPID ) +{ + HANDLE hThreadSnap = INVALID_HANDLE_VALUE; + THREADENTRY32 te32; + + // Take a snapshot of all running threads + hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 ); + if( hThreadSnap == INVALID_HANDLE_VALUE ) + return( FALSE ); + + // Fill in the size of the structure before using it. + te32.dwSize = sizeof(THREADENTRY32); + + // Retrieve information about the first thread, + // and exit if unsuccessful + if( !Thread32First( hThreadSnap, &te32 ) ) + { + printError( TEXT("Thread32First") ); // show cause of failure + CloseHandle( hThreadSnap ); // clean the snapshot object + return( FALSE ); + } + + // Now walk the thread list of the system, + // and display information about each thread + // associated with the specified process + do + { + if( te32.th32OwnerProcessID == dwOwnerPID ) + { + fprintf(log, "\n\n THREAD ID = 0x%08X", te32.th32ThreadID ); + fprintf(log, "\n Base priority = %d", te32.tpBasePri ); + fprintf(log, "\n Delta priority = %d", te32.tpDeltaPri ); + } + } while( Thread32Next(hThreadSnap, &te32 ) ); + + CloseHandle( hThreadSnap ); + return( TRUE ); +} + +void printError( TCHAR* msg ) +{ + DWORD eNum; + TCHAR sysMsg[256]; + TCHAR* p; + + eNum = GetLastError( ); + FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, eNum, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + sysMsg, 256, NULL ); + + // Trim the end of the line and terminate it with a null + p = sysMsg; + while( ( *p > 31 ) || ( *p == 9 ) ) + ++p; + do { *p-- = 0; } while( ( p >= sysMsg ) && + ( ( *p == '.' ) || ( *p < 33 ) ) ); + + // Display the message + fprintf(log, TEXT("\n WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg ); +} +*/ + +#endif // __PROCESS_H__ \ No newline at end of file diff --git a/cpp/wiiscan/src/reg.h b/cpp/wiiscan/src/reg.h new file mode 100644 index 0000000..5d3d565 --- /dev/null +++ b/cpp/wiiscan/src/reg.h @@ -0,0 +1,95 @@ +#ifndef __REGISTRY_H__ +#define __REGISTRY_H__ + +#ifdef WIN32 +#ifdef _WINDOWS + +namespace Registry{ + void* StringtoRoot(const string& t) + { + if (t=="HKEY_CLASSES_ROOT") return HKEY_CLASSES_ROOT; + else if (t=="HKEY_CURRENT_USER") return HKEY_CURRENT_USER; + else if (t=="HKEY_LOCAL_MACHINE") return HKEY_LOCAL_MACHINE; + else if (t=="HKEY_USERS") return HKEY_USERS; + else {throw_("bad root path in registry"); return 0;} + } + + pair > GetPath(const string& fullpath) + { + const int n=fullpath.find_first_of('\\'); + if (n==string::npos) throw_("mallformated registry entry"); + const string t=fullpath.substr(0,n); + void* root=StringtoRoot(t); + + const string r=fullpath.substr(n+1,-1); + const int m=r.find_last_of('\\'); + if (m==string::npos) throw_("mallformated registry entry"); + + const string path=r.substr(0,m); + const string key =r.substr(m+1,-1); + + return make_pair(root,make_pair(path,key)); + } + + bool hasKey(void* root,const string& path,const string& key,string* val=0) + { + assert( sizeof(void*)==sizeof(HKEY) && root!=0 ); + if (root!=HKEY_CLASSES_ROOT && root!=HKEY_CURRENT_USER && root!=HKEY_LOCAL_MACHINE && root!=HKEY_USERS) throw_("unknown root path in registry"); + + DWORD buffersize=1024*16; + char buff[1024*16]; + buff[0]=0; + + HKEY hKey; + if (ERROR_SUCCESS!=RegOpenKeyEx(static_cast(root),path.c_str(),NULL,KEY_READ,&hKey)) return false; + if (ERROR_SUCCESS!=RegQueryValueEx(hKey,key.c_str(),NULL,NULL,(LPBYTE)buff,&buffersize)) return false; + if (ERROR_SUCCESS!=RegCloseKey(hKey)) return false; + + if (val!=0) *val=buff; + return true; + } + + string GetKey(void* root,const string& path,const string& key) + { + string val; + if (!hasKey(root,path,key,&val)) throw_("could not read registry entry"); + return val; + } + + string GetKey(const string& fullpath) + { + const pair > p=GetPath(fullpath); + return GetKey(p.first,p.second.first,p.second.second); + } + + bool SetKey(void* root,const string& path,const string& key,const string& val) + { + assert( sizeof(void*)==sizeof(HKEY) && root!=0 ); + if (root!=HKEY_CLASSES_ROOT && root!=HKEY_CURRENT_USER && root!=HKEY_LOCAL_MACHINE && root!=HKEY_USERS) throw_("unknown root path in registry"); + if (val.size()+1>1024*16) throw_("lenght of value to long"); + + char buff[1024*16]; + size_t i; + for(i=0;i(root),path.c_str(),0,NULL,REG_OPTION_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,NULL)) return false; + if (ERROR_SUCCESS!=RegSetValueEx(hKey,key.c_str(),NULL,REG_SZ,(LPBYTE)buff,buffersize)) return false; + if (ERROR_SUCCESS!=RegCloseKey(hKey)) return false; + + return true; + } + + bool SetKey(const string& fullpath,const string& val) + { + const pair > p=GetPath(fullpath); + return SetKey(p.first,p.second.first,p.second.second,val); + } + +}; // namespace Registry + +#endif // _WINDOWS +#endif // WIN32 +#endif // __REGISTRY_H__ \ No newline at end of file diff --git a/cpp/wiiscan/src/stringfun.h b/cpp/wiiscan/src/stringfun.h new file mode 100644 index 0000000..9624292 --- /dev/null +++ b/cpp/wiiscan/src/stringfun.h @@ -0,0 +1,284 @@ +// The code is copyrighted 2008 by Carsten Frigaard. +// All rights placed in public domain under GNU licence V2, 2008 +// +// © 2008 Carsten Frigaard. Permission to use, copy, modify, and distribute this software +// and its documentation for any purpose and without fee is hereby granted, provided that +// the above copyright notice appear in all copies and that both that copyright notice +// and this permission notice appear in supporting documentation. + +#ifndef __STRINGFUN_H__ +#define __STRINGFUN_H__ + +struct outputoperator_tags +{ + string preline,endline; + int prewidth,postwidth,precision; + bool maptags,printnumbers,serialize; + + outputoperator_tags() + : endline("\n"), prewidth(-1), postwidth(-1), precision(-1), maptags(false), printnumbers(false), serialize(false) {} +}; + +static outputoperator_tags g_tags; + +inline ostream& operator<<(ostream& os,const outputoperator_tags& x){ + g_tags=x; + if (g_tags.precision>0) os.precision(g_tags.precision); + return os; +} + +template +inline string tostring(const T& x) +{ + ostringstream os; + outputoperator_tags t1=g_tags; + g_tags=outputoperator_tags(); + g_tags.serialize=true; + g_tags.endline=""; + os << x; + g_tags=t1; + return os.str(); +} + +template +inline T totype(const string& s) +{ + istringstream is(s); + T x; + is >> x; + return x; +} + +inline string operator+ (const string& s,const char* c) {return s+string(c);} +inline string operator+ (const string& s,const short& c) {return s+tostring(c);} +inline string operator+ (const string& s,const unsigned short& c) {return s+tostring(c);} +inline string operator+ (const string& s,const int& c) {return s+tostring(c);} +inline string operator+ (const string& s,const long& c) {return s+tostring(c);} +inline string operator+ (const string& s,const unsigned long& c) {return s+tostring(c);} +inline string operator+ (const string& s,const float& c) {return s+tostring(c);} +inline string operator+ (const string& s,const double& c) {return s+tostring(c);} +inline string operator+ (const string& s,const long double& c) {return s+tostring(c);} +inline string operator+ (const char* a ,const string& b) {return string(a)+b;} + +#ifdef OS_Linux +#ifdef __GCC_V4__ + // does not work under some compilers where size_t==unsigned int + inline string operator+ (const string& s,const size_t& c) {return s+tostring(c);} +#endif +#endif + +inline string tostring(const int argc,char** argv) +{ + string s; + for(int i=0;i +inline string fwidth2(const T& x,const size_t wdt,const bool prefix) +{ + string s=tostring(x); + if(wdt>1024 || s.size()>=wdt) return s; + size_t n=wdt-s.size(); + while(n>0) { + if (prefix) s=' '+ s; + else s+=' '; + --n; + } + return s; +} + +template +inline string fwidth(const T& x,const size_t wdt=8,const size_t tailwdt=4) +{ + string s=tostring(x); + if(wdt>1024 || tailwdt>1024 || s.size()>wdt+tailwdt) return s; + + const size_t m=min(s.find('.'),s.find(' ')); + if (m==string::npos) { + s=fwidth2(s,wdt,true); + return fwidth2(s,wdt+tailwdt,false); + } + else{ + if(wdt=m ); + size_t n1=wdt-m; + while(n1>0) {s= ' ' + s; --n1;} + return fwidth2(s,wdt+tailwdt,false); + } +} + +inline string strip(const string& s,const char ch=' ') +{ + const size_t n=s.find_first_not_of(ch); + const size_t m=s.find_last_not_of (ch); + + if (n==string::npos || m==string::npos) return ""; + return s.substr(n,m-n+1); +} + +inline string replace(const string& s,const string& f,const string& r) +{ + if (f.size()==0) return s; + const size_t n=s.find(f); + if (n==string::npos) return s; + else return replace(s.substr(0,n) + r + s.substr(n+f.size()),f,r); +} + +inline string indent(const string& s,const string& indent) +{ + string t,q=s; + while(q.size()){ + const string::size_type n=q.find_first_of("\n"); + t += indent + q.substr(0,n) + "\n"; + if (n==string::npos) break; + assert(n+1<=q.size()); + q = q.substr(n+1,q.size()); + } + return t; +} + +inline string removerems(const string& s,const string rem) +{ + const size_t n=s.find_first_of(rem); + return s.substr(0,n); +} + +inline string suffix(const int n) +{ + assert(n>=0 && n<999); + if (n<=9) return "00" + tostring(n); + else if (n<=99) return "0" + tostring(n); + else return tostring(n); +} + +string tail(const string& s,const string& delimiter) +{ + const size_t n=s.find_last_of(delimiter); + if (n==string::npos) return s; + else return s.substr(n+delimiter.size(),-1); +} + +string tail(const string& s,const char c) { + string t; + t.resize(1); + t[0]=c; + return tail(s,t); +} + +template +inline ostream& operator<<(ostream& os,const pair x) +{ + for(int i=0;i +inline istream& operator>>(istream& is,pair x) +{ + for(int i=0;i> x.first[i]; + return is; +} + +template +inline ostream& operator<<(ostream& s,const vector& x) +{ + int i=0; + if (g_tags.serialize) s << "vector: " << x.size() << " { "; + for(typename vector::const_iterator itt=x.begin();itt!=x.end();++itt) { + s << g_tags.preline; + if(g_tags.printnumbers) s << "[" << fwidth(++i,3,0) << "] "; + s << *itt << " " << g_tags.endline; + } + if (g_tags.serialize) s << "} "; + return s; +} + +template +inline istream& operator>>(istream& s,vector& x) +{ + size_t n=0; + string t; + s >> t; + if (t!="vector:") throw_("bad format in vector serialization stream, tag missing"); + s >> n >> t; + if (t!="{") throw_("bad format in vector serialization stream, missing begin brace"); + x.resize(n); + for(size_t i=0;i> x[i]; + s >> t; + if (t!="}") throw_("bad format in vector serialization stream, missing end brace"); + return s; +} + +template +inline ostream& operator<<(ostream& s,const list& x) +{ + if (g_tags.serialize) s << "list: " << x.size() << " { "; + int i=0; + for(typename list::const_iterator itt=x.begin();itt!=x.end();++itt){ + s << g_tags.preline; + if(g_tags.printnumbers) s << "[" << fwidth(++i,3,0) << "] "; + s << *itt << " " << g_tags.endline; + } + if (g_tags.serialize) s << "} "; + return s; +} + +template +inline istream& operator>>(istream& s,list& x) +{ + size_t n=0; + string t; + s >> t; + if (t!="list:") throw_("bad format in list serialization stream, tag missing"); + s >> n >> t; + if (t!="{") throw_("bad format in list serialization stream, missing begin brace"); + for(size_t i=0;i> y; + x.push_back(y); + } + s >> t; + if (t!="}") throw_("bad format in list serialization stream, missing end brace"); + return s; +} + +template +inline ostream& operator<<(ostream& s,const map& x) +{ + if (g_tags.serialize) s << "map: " << x.size() << " { "; + int i=0; + for(typename map::const_iterator itt=x.begin();itt!=x.end();++itt) { + s << g_tags.preline; + if(g_tags.printnumbers) s << "[" << fwidth(++i,3,0) << "] "; + s << fwidth(itt->first,g_tags.prewidth,g_tags.postwidth) << (g_tags.maptags ? " |-> " : " "); + s << fwidth(itt->second,g_tags.prewidth,g_tags.postwidth) << " " << g_tags.endline; + } + if (g_tags.serialize) s << "} "; + return s; +} + +template +inline istream& operator>>(istream& s,map& x) +{ + size_t n=0; + string t; + s >> t; + if (t!="map:") throw_("bad format in map serialization stream, tag missing"); + s >> n >> t; + if (t!="{") throw_("bad format in map serialization stream, missing begin brace"); + for(size_t i=0;i> y >> z; + if (x.find(y)!=x.end()) throw_("bad stream, key value no unique"); + x[y]=z; + } + s >> t; + if (t!="}") throw_("bad format in map serialization stream, missing end brace"); + return s; +} + +#endif // __STRINGFUNS_H__ diff --git a/cpp/wiiscan/src/templatefun.h b/cpp/wiiscan/src/templatefun.h new file mode 100644 index 0000000..0cb60e9 --- /dev/null +++ b/cpp/wiiscan/src/templatefun.h @@ -0,0 +1,52 @@ +#ifndef __TEMPLATE_FUN__ +#define __TEMPLATE_FUN__ + +template +class Autobool +{ +private: + volatile T* m_b; + Autobool(const Autobool&); + void operator=(const Autobool&); +public: + Autobool(volatile T* b) : m_b(b) {assert(m_b && *m_b==false); *m_b=true;} + ~Autobool() {assert(m_b && *m_b==true); *m_b=false; m_b=0;} +}; + +template +class DeviceAutoClose +{ +private: + T m_dev; + R (__stdcall *m_fun)(void *); + bool m_init; + + // private Copy CTOR and assignment operator + DeviceAutoClose(const DeviceAutoClose&); + void operator=(const DeviceAutoClose&); + +public: + DeviceAutoClose(T dev,R(__stdcall *fun)(void*)) : m_dev(dev), m_fun(fun), m_init(true) + { + FUNSTACK; + assert(m_fun!=NULL); + } + ~DeviceAutoClose() + { + FUNSTACK; + assert(m_init); + assert(m_fun!=NULL); + if (m_dev!=NULL){ + R r=m_fun(m_dev); + if (!r) throw_("DeviceClose() failed"); // throw in DTOR -> bad, bad! + } + m_dev=NULL; + m_fun=NULL; + m_init=false; + } + const T& operator()() const {FUNSTACK; assert(m_init); return m_dev;} + T& operator()() {FUNSTACK; assert(m_init); return m_dev;} +}; + + +#endif // __TEMPLATE_FUN__ \ No newline at end of file diff --git a/cpp/wiiscan/src/toolsfun.h b/cpp/wiiscan/src/toolsfun.h new file mode 100644 index 0000000..211ba55 --- /dev/null +++ b/cpp/wiiscan/src/toolsfun.h @@ -0,0 +1,34 @@ +// The code is copyrighted 2008 by Carsten Frigaard. +// All rights placed in public domain under GNU licence V2, 2008 +// +// © 2008 Carsten Frigaard. Permission to use, copy, modify, and distribute this software +// and its documentation for any purpose and without fee is hereby granted, provided that +// the above copyright notice appear in all copies and that both that copyright notice +// and this permission notice appear in supporting documentation. + +#ifndef __TOOLSFUN_H__ +#define __TOOLSFUN__H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#include "exception.h" +#include "stringfun.h" +#include "args.h" +#include "funs.h" +#include "file.h" +#include "configfile.h" +#include "templatefun.h" +#include "reg.h" + +#endif // __TOOLSFUN_H__ \ No newline at end of file diff --git a/cpp/wiiscan/src/usbm.h b/cpp/wiiscan/src/usbm.h new file mode 100644 index 0000000..802c942 --- /dev/null +++ b/cpp/wiiscan/src/usbm.h @@ -0,0 +1,204 @@ +#ifndef __USBM_H__ +#define __USBM_H__ + +class USBio +{ +private: + HINSTANCE m_hDll; + + // Discovery routine + typedef int (__stdcall *USBm_FindDevices_type) (); + + // Info about devices + typedef int (__stdcall *USBm_NumberOfDevices_type) (void); + typedef int (__stdcall *USBm_DeviceValid_type) (unsigned char); + typedef int (__stdcall *USBm_DeviceVID_type) (unsigned char device); + typedef int (__stdcall *USBm_DevicePID_type) (unsigned char device); + typedef int (__stdcall *USBm_DeviceDID_type) (unsigned char device); + typedef int (__stdcall *USBm_DeviceMfr_type) (unsigned char, char *); + typedef int (__stdcall *USBm_DeviceProd_type) (unsigned char, char *); + typedef int (__stdcall *USBm_DeviceSer_type) (unsigned char, char *); + + // General U4xx functions + typedef int (__stdcall *USBm_InitPorts_type) (unsigned char); + typedef int (__stdcall *USBm_WriteA_type) (unsigned char, unsigned char); + typedef int (__stdcall *USBm_WriteB_type) (unsigned char, unsigned char); + typedef int (__stdcall *USBm_WriteABit_type) (unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_WriteBBit_type) (unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_ReadA_type) (unsigned char, unsigned char *); + typedef int (__stdcall *USBm_ReadB_type) (unsigned char, unsigned char *); + typedef int (__stdcall *USBm_SetBit_type) (unsigned char, unsigned char); + typedef int (__stdcall *USBm_ResetBit_type) (unsigned char, unsigned char); + typedef int (__stdcall *USBm_DirectionA_type) (unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_DirectionB_type) (unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_StrobeWrite_type) (unsigned char, unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_StrobeRead_type) (unsigned char, unsigned char *, unsigned char, unsigned char); + typedef int (__stdcall *USBm_StrobeWrite2_type) (unsigned char, unsigned char, unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_StrobeRead2_type) (unsigned char, unsigned char *, unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_StrobeWrites_type) (unsigned char, unsigned char *, unsigned char *); + typedef int (__stdcall *USBm_StrobeReads_type) (unsigned char, unsigned char *, unsigned char *); + typedef int (__stdcall *USBm_InitLCD_type) (unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_LCDCmd_type) (unsigned char, unsigned char); + typedef int (__stdcall *USBm_LCDData_type) (unsigned char, unsigned char); + typedef int (__stdcall *USBm_InitSPI_type) (unsigned char, unsigned char); + typedef int (__stdcall *USBm_SPIMaster_type) (unsigned char, unsigned char *, unsigned char *); + typedef int (__stdcall *USBm_SPISlaveWrite_type) (unsigned char, unsigned char, unsigned char *); + typedef int (__stdcall *USBm_SPISlaveRead_type) (unsigned char, unsigned char *, unsigned char *); + typedef int (__stdcall *USBm_Stepper_type) (unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char); + typedef int (__stdcall *USBm_Reset1Wire_type) (unsigned char, unsigned char *); + typedef int (__stdcall *USBm_Write1Wire_type) (unsigned char, unsigned char); + typedef int (__stdcall *USBm_Read1Wire_type) (unsigned char, unsigned char *); + + // DLL string info access + typedef int (__stdcall *USBm_RecentError_type) (char *); + typedef int (__stdcall *USBm_ClearRecentError_type) (void); + typedef int (__stdcall *USBm_DebugString_type) (char *); + typedef int (__stdcall *USBm_Copyright_type) (char *); + typedef int (__stdcall *USBm_About_type) (char *); + typedef int (__stdcall *USBm_Version_type) (char *); + +public: + USBio() : m_hDll(0) + { + m_hDll = LoadLibrary("USBm.dll"); + if (m_hDll==0) throw_("could not locate USBm.dll"); + + USBm_FindDevices = (USBm_FindDevices_type)GetProcAddress(m_hDll, "USBm_FindDevices"); + USBm_NumberOfDevices = (USBm_NumberOfDevices_type)GetProcAddress(m_hDll, "USBm_NumberOfDevices"); + USBm_DeviceValid = (USBm_DeviceValid_type)GetProcAddress(m_hDll, "USBm_DeviceValid"); + USBm_DeviceVID = (USBm_DeviceVID_type)GetProcAddress(m_hDll, "USBm_DeviceVID"); + USBm_DevicePID = (USBm_DevicePID_type)GetProcAddress(m_hDll, "USBm_DevicePID"); + USBm_DeviceDID = (USBm_DeviceDID_type)GetProcAddress(m_hDll, "USBm_DeviceDID"); + USBm_DeviceMfr = (USBm_DeviceMfr_type)GetProcAddress(m_hDll, "USBm_DeviceMfr"); + USBm_DeviceProd = (USBm_DeviceProd_type)GetProcAddress(m_hDll, "USBm_DeviceProd"); + USBm_DeviceSer = (USBm_DeviceSer_type)GetProcAddress(m_hDll, "USBm_DeviceSer"); + USBm_InitPorts = (USBm_InitPorts_type)GetProcAddress(m_hDll, "USBm_InitPorts"); + USBm_WriteA = (USBm_WriteA_type)GetProcAddress(m_hDll, "USBm_WriteA"); + USBm_WriteB = (USBm_WriteB_type)GetProcAddress(m_hDll, "USBm_WriteB"); + USBm_WriteABit = (USBm_WriteABit_type)GetProcAddress(m_hDll, "USBm_WriteABit"); + USBm_WriteBBit = (USBm_WriteBBit_type)GetProcAddress(m_hDll, "USBm_WriteBBit"); + USBm_ReadA = (USBm_ReadA_type)GetProcAddress(m_hDll, "USBm_ReadA"); + USBm_ReadB = (USBm_ReadB_type)GetProcAddress(m_hDll, "USBm_ReadB"); + USBm_SetBit = (USBm_SetBit_type)GetProcAddress(m_hDll, "USBm_SetBit"); + USBm_ResetBit = (USBm_ResetBit_type)GetProcAddress(m_hDll, "USBm_ResetBit"); + USBm_DirectionA = (USBm_DirectionA_type)GetProcAddress(m_hDll, "USBm_DirectionA"); + USBm_DirectionB = (USBm_DirectionB_type)GetProcAddress(m_hDll, "USBm_DirectionB"); + USBm_StrobeWrite = (USBm_StrobeWrite_type)GetProcAddress(m_hDll, "USBm_StrobeWrite"); + USBm_StrobeRead = (USBm_StrobeRead_type)GetProcAddress(m_hDll, "USBm_StrobeRead"); + USBm_StrobeWrite2 = (USBm_StrobeWrite2_type)GetProcAddress(m_hDll, "USBm_StrobeWrite2"); + USBm_StrobeRead2 = (USBm_StrobeRead2_type)GetProcAddress(m_hDll, "USBm_StrobeRead2"); + USBm_StrobeWrites = (USBm_StrobeWrites_type)GetProcAddress(m_hDll, "USBm_StrobeWrites"); + USBm_StrobeReads = (USBm_StrobeReads_type)GetProcAddress(m_hDll, "USBm_StrobeReads"); + USBm_InitLCD = (USBm_InitLCD_type)GetProcAddress(m_hDll, "USBm_InitLCD"); + USBm_LCDCmd = (USBm_LCDCmd_type)GetProcAddress(m_hDll, "USBm_LCDCmd"); + USBm_LCDData = (USBm_LCDData_type)GetProcAddress(m_hDll, "USBm_LCDData"); + USBm_InitSPI = (USBm_InitSPI_type)GetProcAddress(m_hDll, "USBm_InitSPI"); + USBm_SPIMaster = (USBm_SPIMaster_type)GetProcAddress(m_hDll, "USBm_SPIMaster"); + USBm_SPISlaveWrite = (USBm_SPISlaveWrite_type)GetProcAddress(m_hDll, "USBm_SPISlaveWrite"); + USBm_SPISlaveRead = (USBm_SPISlaveRead_type)GetProcAddress(m_hDll, "USBm_SPISlaveRead"); + USBm_Stepper = (USBm_Stepper_type)GetProcAddress(m_hDll, "USBm_Stepper"); + USBm_Reset1Wire = (USBm_Reset1Wire_type)GetProcAddress(m_hDll, "USBm_Reset1Wire"); + USBm_Write1Wire = (USBm_Write1Wire_type)GetProcAddress(m_hDll, "USBm_Write1Wire"); + USBm_Read1Wire = (USBm_Read1Wire_type)GetProcAddress(m_hDll, "USBm_Read1Wire"); + USBm_RecentError = (USBm_RecentError_type)GetProcAddress(m_hDll, "USBm_RecentError"); + USBm_ClearRecentError = (USBm_ClearRecentError_type)GetProcAddress(m_hDll, "USBm_ClearRecentError"); + USBm_DebugString = (USBm_DebugString_type)GetProcAddress(m_hDll, "USBm_DebugString"); + USBm_Copyright = (USBm_Copyright_type)GetProcAddress(m_hDll, "USBm_Copyright"); + USBm_About = (USBm_About_type)GetProcAddress(m_hDll, "USBm_About"); + USBm_Version = (USBm_Version_type)GetProcAddress(m_hDll, "USBm_Version"); + } + + ~USBio() + { + assert( m_hDll ); +// XXX FreeLibrary(m_hDll); + m_hDll=0; + } + + int Devices() const + { + assert( m_hDll ); + unsigned char numdev=USBm_NumberOfDevices(); + return numdev; + } + + string version() const + { + assert( m_hDll ); + + string s; + char textstr[300]; + const int d=Devices(); + + s = string(" USBm.dll version = ") + USBm_Version(textstr) + "\n"; + s += string(" USBm.dll version = ") + textstr + "\n"; + + USBm_Copyright(textstr); + s += string(" Copyright = ") + textstr + "\n"; + + USBm_About(textstr); + s += string(" About =") + textstr + "\n"; + s += string(" Number of devices = ") + d + "\n"; + + // Gather info from each discovered device + for(int i=0;i. +// This is free software: you are free to change and redistribute it. +// There is NO WARRANTY, to the extent permitted by law. +// +// This file is part of wiiscan. +// +// wiiscan is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// wiiscan is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with wiiscan. If not, see . + +#include "wiiscan.h" + +int main(int argc,char** argv) +{ + return Wiiscan::main(argc,argv); +} \ No newline at end of file diff --git a/cpp/wiiscan/src/wiiscan.h b/cpp/wiiscan/src/wiiscan.h new file mode 100644 index 0000000..c082934 --- /dev/null +++ b/cpp/wiiscan/src/wiiscan.h @@ -0,0 +1,1108 @@ +// Copyright © 2009 MergeIt, Aps. +// +// License LGPLv3+: GNU lesser LGPL version 3 or later . +// This is free software: you are free to change and redistribute it. +// There is NO WARRANTY, to the extent permitted by law. +// +// This file is part of wiiscan. +// +// wiiscan is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// wiiscan is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with wiiscan. If not, see . + +#define VERSION 0 +#define VERSION_REV 9 + +#define VC_EXTRALEAN + +#include "toolsfun.h" +#include "wiiuse.h" +#include "usbm.h" +#include "delcomdll.h" +#include +#include + +#pragma comment ( lib, "Lib/wiiuse.lib") +#pragma comment ( lib, "Lib/delcomdll.lib") +#pragma comment ( lib, "Irprops.lib") +#pragma comment ( lib, "Ws2_32.lib") + +namespace Wiiscan { + +// default values +const int MAX_DEVICE_COUNT =256; +const int DEFAULT_timeout =2000; +const int DEFAULT_wiitimeout=2000; +const int DEFAULT_usbsleep =500; +const int DEFAULT_usbmsleep =3000; +const int DEFAULT_btsleep =2000; +const int DEFAULT_usbmode =0; +const int DEFAULT_removemode=0; +const int DEFAULT_scanretries=1; +const string NINTENDO_DEV="Nintendo RVL-CNT-01"; +const string DEFAULT_configfile="wiiscan.ini"; +const string DEFAULT_logfile="cout"; +const string DEFAULT_wiilib="wiimotelib"; + +Logger& log=g_log; +void (*g_automode_callback)(const int)=0; +void DummyCallback(const int){FUNSTACK;} + +string ConvertName(const WCHAR* szName) +{ + FUNSTACK; + assert( MAX_PATH >= BLUETOOTH_MAX_NAME_SIZE ); + CHAR szDevName[MAX_PATH]; + memset(szDevName, 0x00, sizeof(szDevName)); + sprintf_s(szDevName,MAX_PATH,"%S", szName); + return string(szDevName); +} + +string ConvertAddress(const BLUETOOTH_ADDRESS& address) +{ + FUNSTACK; + string s; + char t[256]; + for(int i=5;i>=0;i--) { + sprintf_s(t,256,"%X",address.rgBytes[i]); + string x=t; + if (x.size()==1) x="0"+x; + s+=x+(i==0 ? "" : ":"); + } + return s; +} + +string tostring(const SYSTEMTIME& x) +{ + FUNSTACK; + // XXX warning: no converstion of GMT to local time + ostringstream s; + if (x.wHour==0 && x.wMinute==0 && x.wDay==0 && x.wYear==0 && x.wYear==0) s << ""; + else s << x.wHour << ":" << (x.wMinute<10 ? "0" : "") << x.wMinute << " d. " << x.wDay << "/" << x.wMonth << ", " << x.wYear; + return s.str(); +} + +string tostring(const BLUETOOTH_DEVICE_INFO& x) +{ + FUNSTACK; + ostringstream s; + s << " BLUETOOTH_DEVICE_INFO:" << endl; + s << " dwSize: " << x.dwSize << endl; + s << " Address: " << ConvertAddress(x.Address) << endl; + s << " ulClassofDevice:" << x.ulClassofDevice << endl; + s << " fConnected: " << x.fConnected << endl; + s << " fRemembered: " << x.fRemembered << endl; + s << " fAuthenticated: " << x.fAuthenticated << endl; + s << " stLastSeen: " << tostring(x.stLastSeen) << " [GMT]" << endl; + s << " stLastUsed: " << tostring(x.stLastUsed) << " [GMT]" << endl; + s << " szName: " << ConvertName(x.szName) << endl; + return s.str(); +} + +string tostring(const BLUETOOTH_RADIO_INFO& x) +{ + FUNSTACK; + ostringstream s; + s << " PBLUETOOTH_RADIO_INFO:" << endl; + s << " dwSize: " << x.dwSize << endl; + s << " Address: " << ConvertAddress(x.address) << endl; + s << " szName: " << ConvertName(x.szName) << endl; + s << " ulClassofDevice:" << x.ulClassofDevice << endl; + s << " lmpSubversion: " << x.lmpSubversion << endl; + s << " manufacturer: " << x.manufacturer << endl; + return s.str(); +} + +bool USB_hub_updown(const bool up,const string& usbhub) +{ + FUNSTACK; + log << (up ? "Enabling" : "Disabling") << " 'USB hub..." << endl; + int n=-1; + System(string("devcon ") + (up ? "enable " : "disable ") + usbhub,false,false,&n); + if (n!=0) throw_("devcon failed, this can be to a erroneous usbhub string or if devcon.exe is not found in path, please install it from http://support.microsoft.com/kb/311272"); + log << "Done [OK]" << endl; + return true; +} + +int ScanforUSBmicrodevs() +{ + USBio io; + io.USBm_FindDevices(); + return io.Devices(); +} + +bool USB_microio_updown(const bool up,const bool dbg) +{ + FUNSTACK; + log << (up ? "Enabling" : "Disabling") << " USBm io..." << endl; + + // Discover the USBmicro devices + USBio io; + io.USBm_FindDevices(); + const int d=io.Devices(); + if (d==0) return false; // throw_("could not find an USBm device"); + else if (d>1) throw_("found more that one USBm devices"); + + static bool premable=true; + if (dbg && premable) { + premable=false; + log << " USBm info:" << endl << io.version(); + } + + io.USBm_InitPorts(0); + io.USBm_DirectionA(0, 0xff, 0xff); + + if(up) io.USBm_ResetBit(0,5); + else io.USBm_SetBit (0,5); + + log << "Done [OK]" << endl; + return true; +} + +bool __stdcall CloseDelcomhandle(HANDLE h){return DelcomCloseDevice(h)==0;} + +void do_cmd(HANDLE di, int ma, int mi, int lsb, int msb, unsigned char *data, unsigned datalen) +{ + //static struct delcom_packet p; + PacketStruct p; + memset(&p, 0, sizeof(p)); + p.MajorCmd = ma; + p.MinorCmd = mi; + p.DataLSB = lsb; + p.DataMSB = msb; + if (DelcomSendPacket(di,&p,&p)<0) throw_("DelcomSendPacket() failed"); +} + +int ScanforDelcomdevs() +{ + // Discover the USB Delcom devices + DeviceNameStruct names[10]; // array to hold the device names found + return DelcomScanDevices(USBIODS, names, 10); +} + +bool USB_delconio_updown(const bool up,const bool dbg) +{ + FUNSTACK; + log << (up ? "Enabling" : "Disabling") << " USB Delcom io..." << endl; + + DeviceNameStruct names[10]; // array to hold the device names found + const int d=DelcomScanDevices(USBIODS, names, 10); + if (d==0) return false; //throw_("could not find an USB Delcon device"); + else if (d>1) throw_("found more that one USB Delcon devices"); + + static bool premable=true; + if (dbg && premable) { + premable=false; + log << " USB Delcon info:" << (char*)&names[0] << "\n SN=" << DelcomReadDeviceSerialNum((char*)&names[0], NULL) << endl; + } + + DeviceAutoClose h(DelcomOpenDevice((char*)&names[0],0),CloseDelcomhandle); + if(up) do_cmd(h(),10,1,0xFE,0,0,0); + else do_cmd(h(),10,1,0xFF,0,0,0); + + log << "Done [OK]" << endl; + return true; +} + +bool USBupdown(const int iomode,const bool up,const bool dbg,const string& usbhub="") +{ + if (iomode==0) return true; + if (iomode==1) return USB_hub_updown(up,usbhub); + if (iomode==2) return USB_microio_updown(up,dbg); + else if (iomode==3) return USB_delconio_updown(up,dbg); + else throw_("bad io mode"); + return false; +} + +class Wiilib +{ + // Wiilib class based on wiiuse library by Michael Laforest. + // Library located at http://www.wiiuse.net + // Wiiuse premable from wiiuse_v0.12/example/example.c: + + // + // wiiuse + // + // Written By: + // Michael Laforest < para > + // Email: < thepara (--AT--) g m a i l [--DOT--] com > + // + // Copyright 2006-2007 + // + // This file is part of wiiuse. + // + // This program is free software; you can redistribute it and/or modify + // it under the terms of the GNU General Public License as published by + // the Free Software Foundation; either version 3 of the License, or + // (at your option) any later version. + // + // This program is distributed in the hope that it will be useful, + // but WITHOUT ANY WARRANTY; without even the implied warranty of + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + // GNU General Public License for more details. + // + // You should have received a copy of the GNU General Public License + // along with this program. If not, see . + +private: + enum {MAX_WIIMOTES=1}; // use only one wiimote, but can connect to four + wiimote** m_wiimotes; + static int m_instantiations; + + int finddevices(const int wiitimeout,const bool rumble,const bool dbg) + { + FUNSTACK; + + // Find wiimote devices + const int to=wiitimeout/1000; + if (to<1) throw_("bad wiitimeout value, should be >=1000"); + const int found=wiiuse_find(m_wiimotes,MAX_WIIMOTES,to); + if (dbg) log << " wiiuse_find(0x0," << MAX_WIIMOTES << "," << to << ") return " << found << endl; + + if (found==0) { + if (dbg) log << " ** error: no wiimotes found" << endl; + return 0; + } + + // Connect to the wiimotes + assert(found>0); + + const int connected = wiiuse_connect(m_wiimotes, MAX_WIIMOTES); + if (connected==0){ + log << " ** error: could not connect to any wiimotes" << endl; + return 0; + } + if (dbg) log << " connected to " << connected<< " wiimotes (of " << MAX_WIIMOTES << " found)" << endl; + + + // Now set the LEDs and rumble for a second so it's easy + // to tell which wiimotes are connected (just like the wii does). + wiiuse_set_leds(m_wiimotes[0], WIIMOTE_LED_1); + + if (rumble){ + wiiuse_rumble(m_wiimotes[0], 1); + #ifndef WIN32 + usleep(200000); + #else + Sleep(200); + #endif + wiiuse_rumble(m_wiimotes[0], 0); + } + + // read some data to be sure of connection + for(int i=0;i<8;++i){ + Sleep(50); + + int led=0; + switch(i%6){ + case 0 : led=WIIMOTE_LED_1; break; + case 1 : led=WIIMOTE_LED_2; break; + case 2 : led=WIIMOTE_LED_3; break; + case 3 : led=WIIMOTE_LED_4; break; + case 4 : led=WIIMOTE_LED_3; break; + case 5 : led=WIIMOTE_LED_2; break; + } + wiiuse_rumble(m_wiimotes[0], 0); // to be sure + wiiuse_set_leds(m_wiimotes[0],led); + wiiuse_status(m_wiimotes[0]); + if (led!=m_wiimotes[0]->leds) throw_("bad LED status on wiimote, is it connected properly?"); + } + + // set all leds, retry a couple of time to be sure + for(int i=0;i<4;++i){ + wiiuse_set_leds(m_wiimotes[0],WIIMOTE_LED_1 | WIIMOTE_LED_2 | WIIMOTE_LED_3 | WIIMOTE_LED_4); + Sleep(100); + } + + return connected; + } + + Wiilib() : m_wiimotes(0) + { + FUNSTACK; + if (m_instantiations!=0) throw_("can only instatiate one Wiilib at a time"); + ++m_instantiations; // assumes a single-thread application, else we have a race condition here + + // Initialize an array of wiimote objects. + // The parameter is the number of wiimotes I want to create. + m_wiimotes = wiiuse_init(MAX_WIIMOTES); + // wiiuse_set_bluetooth_stack(m_wiimotes, MAX_WIIMOTES,WIIUSE_STACK_MS); must be done automatically + assert(m_instantiations==1); + } + + ~Wiilib() + { + FUNSTACK; + assert( m_instantiations==1 ); + + // Disconnect the wiimotes + if (m_wiimotes!=0) wiiuse_cleanup(m_wiimotes, MAX_WIIMOTES); + m_wiimotes=0; + --m_instantiations; + assert(m_instantiations==0); + } + + static int OpenDevices_wiilib(const int wiitimeout,const bool rumble,const bool dbg,const bool precheck=false) + { + FUNSTACK; + int n=0,retries=0; + while(n==0 && retries++<10) { + n=Wiilib().finddevices(wiitimeout,rumble,dbg); + if (precheck) break; + if (n==0) { + if (dbg) log << " OpenDevice()...delaying " << (retries<5 ? "250" : "1000") << " [ms]" << endl; + if (retries<5) Sleep(250); + else Sleep(1000); + } + } + if (dbg) {cerr.flush(); log << " Wiilib::OpenDevice..." << n << endl;} + return n; + } + + static int OpenDevices_wiimotelib(const int wiitimeout,const bool rumble,const bool dbg,const bool precheck=false) + { + FUNSTACK; + int n=0,retries=0; + while(n==0 && retries++<10) { + n=system("wiimotelibpoll")==0 ? 1 : 0; + if (precheck) break; + if (n==0) { + if (dbg) log << " OpenDevice()...delaying " << (retries<5 ? "250" : "1000") << " [ms]" << endl; + if (retries<5) Sleep(250); + else Sleep(1000); + } + } + if (dbg) {cerr.flush(); log << " Wiilib::OpenDevice..." << n << endl;} + return n; + } + +public: + + static int OpenDevices(const string& wiilib,const int wiitimeout,const bool rumble,const bool dbg,const bool precheck=false) + { + FUNSTACK; + if (wiilib=="wiiuse") return OpenDevices_wiilib(wiitimeout,rumble,dbg,precheck); + else if (wiilib=="wiimotelib") return OpenDevices_wiimotelib(wiitimeout,rumble,dbg,precheck); + else if (wiilib=="none") return -1; + else throw_("Wiilib::OpenDevices(), library must be one-of {wiiuse,wiimotelib,none}"); + return -1; // avoid compiler error + } + +}; + +int Wiilib::m_instantiations=0; + +BLUETOOTH_DEVICE_SEARCH_PARAMS Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(const int timeout) +{ + FUNSTACK; + BLUETOOTH_DEVICE_SEARCH_PARAMS b; + ZeroMemory(&b, sizeof(b)); + b.dwSize = sizeof(b); + b.fReturnAuthenticated = TRUE; + b.fReturnRemembered = TRUE; + b.fReturnUnknown = TRUE; + b.fReturnConnected = TRUE; + b.fIssueInquiry = TRUE; + + const int to=static_cast(1.0*timeout/1000.0/1.28+0.5); + assert(to>=0 && to<48 ); + b.cTimeoutMultiplier = (to==0 ? 1 : to); // timeout of 0 causes havac + b.hRadio = NULL; + + assert( b.cTimeoutMultiplier>0 ); + return b; +} + +BLUETOOTH_DEVICE_INFO Get_BLUETOOTH_DEVICE_INFO() +{ + FUNSTACK; + BLUETOOTH_DEVICE_INFO b; + ZeroMemory(&b, sizeof(b)); + b.dwSize = sizeof(b); + return b; +} + +BLUETOOTH_RADIO_INFO Get_BLUETOOTH_RADIO_INFO() +{ + FUNSTACK; + BLUETOOTH_RADIO_INFO b; + ZeroMemory(&b, sizeof(b)); + b.dwSize = sizeof(b); + return b; +} + +BLUETOOTH_FIND_RADIO_PARAMS Get_BLUETOOTH_FIND_RADIO_PARAMS() +{ + FUNSTACK; + BLUETOOTH_FIND_RADIO_PARAMS b; + ZeroMemory(&b, sizeof(b)); + b.dwSize = sizeof(b); + return b; +} + +bool ReachedMax(const int i) +{ + FUNSTACK; + if (i>MAX_DEVICE_COUNT) { + log << " ** warning: too many devices found, can only handle " << MAX_DEVICE_COUNT << " devices" << endl; + return true; + } + return false; +} + +bool RemoveDev(const BLUETOOTH_DEVICE_INFO& bdi) +{ + FUNSTACK; + DWORD status=BluetoothUpdateDeviceRecord(&bdi); + if (status!=ERROR_SUCCESS) throw_("BluetoothUpdateDeviceRecord() failed"); + + status=BluetoothRemoveDevice(&bdi.Address); + if (status==ERROR_SUCCESS) log << " removed device successfully..." << endl << "Done [OK]" << endl; + else log << " ** error: removing the device failed, was it registred at all?" << endl <<"Done [FAILED]" << endl; + + return status==ERROR_SUCCESS; +} + +HANDLE RadioInfo(const string& btr,const bool dbg) +{ + FUNSTACK; + if (dbg) log << " Radio info" << endl; + assert( btr.size()==17 || btr.size()==0 || btr=="all"); + + HANDLE hRadio=NULL; + BLUETOOTH_FIND_RADIO_PARAMS btfrp=Get_BLUETOOTH_FIND_RADIO_PARAMS(); + DeviceAutoClose hbf(BluetoothFindFirstRadio(&btfrp,&hRadio),&BluetoothFindRadioClose); + + if (hbf()==NULL) { + if (dbg) log << " " << endl; + return NULL; + } + + while(hbf()!=NULL){ + if (hbf()==NULL) throw_("BluetoothFindFirstRadio() failed"); + + BLUETOOTH_RADIO_INFO r=Get_BLUETOOTH_RADIO_INFO(); + if (BluetoothGetRadioInfo(hRadio,&r)!=ERROR_SUCCESS) throw_("BluetoothGetRadioInfo() failed"); + + const int c=BluetoothIsConnectable(hRadio); + const int d=BluetoothIsDiscoverable(hRadio); + + if (dbg){ + log << indent(tostring(r)," "); + log << " connectable: " << c << endl; + log << " discoverabel: " << d << endl; + } + + if (btr.size()==0) break; + else if (btr==ConvertAddress(r.address)){ + if (dbg) log << " found radio address match " << (btr.size()>0 ? ": " + btr : "") << endl; + break; + } + + if (!BluetoothFindNextRadio(&btfrp,&hRadio)) break; + } + + if (dbg) log << "Done [" << (hRadio==NULL ? "FAILED" : "OK") << "]" << endl; + return hRadio; +} + +bool ScanDevices(const int timeout) +{ + FUNSTACK; + log << "Scanning devices" << endl; + + vector infos; + + BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp=Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(timeout); + BLUETOOTH_DEVICE_INFO bdi=Get_BLUETOOTH_DEVICE_INFO(); + + DeviceAutoClose hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + const DWORD dwErr = GetLastError(); + + if (hbf()==NULL) { + log << " ** warning: no devices found" << endl << "Done [FAILED]" << endl; + return false;; + } + + if (hbf()!= NULL) { + log << "Looking for devices..." << endl; + + int i=0; + while(1){ + if (ReachedMax(i)) break; + + const string t=ConvertName(bdi.szName); + log << " found device [" << i << "]: <" << (t.size()>0 ? t : "NONAME") << ">" << endl; + + infos.push_back(bdi); + + if (BluetoothFindNextDevice(hbf(), &bdi) == FALSE) + break; + + ++i; + } + } + + log << "BTDevices info:" << endl; + if (infos.size()==0) log << " " << endl; + else for(size_t i=0;i0 ? s : "") << endl; + log << tostring(t); + } + log << "Done [OK]" << endl; + + return true; +} + +bool RemoveDevice(const string& d,const bool dbg) +{ + FUNSTACK; + log << "Removing device <" << d << ">" << endl; + + BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp=Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(1); // timout must be greater than zero, eventhough I only return remembered devices! + BLUETOOTH_DEVICE_INFO bdi=Get_BLUETOOTH_DEVICE_INFO(); + + bdsp.fReturnAuthenticated = FALSE; + bdsp.fReturnRemembered = TRUE; + bdsp.fReturnUnknown = FALSE; + bdsp.fReturnConnected = TRUE; + + DeviceAutoClose hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + + const DWORD dwErr = GetLastError(); + + if (hbf()==NULL) { + log << " ** error: failed to find device" << endl << "Done [FAILED]" << endl; + return false; + } + + int i=0; + while(true){ + if (ReachedMax(i)) break; + const string fd=ConvertName(bdi.szName); + log << " found device [" << i << "]: <" << (fd.size()>0 ? fd : "NONAME") << ">" << endl; + + if (fd==d){ + log << " found match..." << endl; + log << " trying to remove..." << endl; + + return RemoveDev(bdi); + } + if (BluetoothFindNextDevice(hbf(), &bdi) == FALSE) break; + ++i; + } + log << " ** error: failed to find device" << endl << "Done [FAILED]" << endl; + return false; +} + +bool MatchAdr(const string& pattern,const string& adr) +{ + assert(pattern.size()==17 && adr.size()==17); + for(size_t i=0;i<17;++i){ + if (!(pattern[i]=='?' || pattern[i]==adr[i])) return false; + } + return true; +} + +bool MatchDevice(const string& d,const vector& known_adr,const BLUETOOTH_DEVICE_INFO& bdi,const bool dbg) +{ + FUNSTACK; + + const string found_adr=ConvertAddress(bdi.Address); + bool wildcard=false; + for(size_t i=0;i(p); + assert(p!=0 && q->done==false && q->timeout>0 && q->hbf==0 ); + + BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp=Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(q->timeout); + + q->hbf=BluetoothFindFirstDevice(&bdsp,&q->bdi); + const DWORD status=GetLastError(); + + if (status==ERROR_INVALID_PARAMETER) throw_("BluetoothFindFirstDevice(), returned status=ERROR_INVALID_PARAMETER"); + else if (status==ERROR_REVISION_MISMATCH) throw_("BluetoothFindFirstDevice(), returned status=ERROR_REVISION_MISMATCH"); + + q->done=true; + return 0; +} + +bool OpenDevice(const string& d,const string& btr,const int timeout,const int wiitimeout,const vector& known_adr,const int btsleep,const string& lib,const bool precheckwiimotes,const bool rumble,const bool dbg,bool& invalidargflag) +{ + FUNSTACK; + assert( invalidargflag==false ); + log << "Open device <" << d << ">" << endl; + + if (precheckwiimotes && Wiilib::OpenDevices(lib,wiitimeout,false,dbg,true)>=1) { + log << " service is already opened..." << endl << "Done [OK]" << endl; + return true; + } + + if (dbg && known_adr.size()>0) { + log << " known addresses="; + for(size_t i=0;i hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + // + // FIX: start the BluetoothFindFirstDevice() function in a thread, terminate the thread if it has run for longer than, say 2+timeout. + // + // Old code: + // BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp=Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(timeout); + // BLUETOOTH_DEVICE_INFO bdi=Get_BLUETOOTH_DEVICE_INFO(); + // DeviceAutoClose hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + // DWORD status=GetLastError(); + + unsigned long tid=0; + DeviceAutoClose h(CreateThread(0,0,BluetoothFindFirstDevice_thread,&p,0,&tid),CloseHandle); + timer tthread; + + Sleep(timeout); + while(!p.done && tthread.elapsed()*1000<2*timeout) Sleep(20); // or use WaitForMultipleObjects(...); + } + + if(!p.done){ + log << " ** error: BluetoothFindFirstDevice() stalled" << endl<< "Done [FAILED]" << endl; + return false; + } + else if(p.hbf==0){ + log << " ** error: BluetoothFindFirstDevice() returned null hbf" << endl<< "Done [FAILED]" << endl; + return false; + } + + assert(p.hbf!=0); + BLUETOOTH_DEVICE_INFO bdi=p.bdi; + DeviceAutoClose hbf(p.hbf,&BluetoothFindDeviceClose); + + if (hbf()==NULL) { + log << " ** error: no devices found" << endl << "Done [FAILED]" << endl; + return false; + } + + int i=0; + while(true){ + if (ReachedMax(i)) break; + const string fd=ConvertName(bdi.szName); + log << " found device [" << i << "]: <" << (fd.size()>0 ? fd : "NONAME") << ">" << endl; + + if (MatchDevice(d,known_adr,bdi,dbg)){ + log << " found match..." << endl; + if (dbg) log << tostring(bdi); + + // write binary dbi, test code for now + //ofstream ox("bdi.out",ios::binary); + //ox.write(reinterpret_cast(&bdi),sizeof(bdi)); + + log << " trying to open..." << endl; + DWORD status=BluetoothUpdateDeviceRecord(&bdi); + if (status!=ERROR_SUCCESS) throw_("BluetoothUpdateDeviceRecord() failed"); + if (dbg) log << " BluetoothUpdateDeviceRecord()...OK" << endl; + + { + DeviceAutoClose hRadio(RadioInfo(btr,dbg),&CloseHandle); + if (hRadio()==NULL) throw_("failed to get radio"); + if (dbg) log << " RadioInfo()...OK" << endl; + + const GUID service=HumanInterfaceDeviceServiceClass_UUID; + status=BluetoothSetServiceState(hRadio(),&bdi,&service,BLUETOOTH_SERVICE_ENABLE); + + if (dbg && status==ERROR_SUCCESS) log << " BluetoothSetServiceState()...OK" << endl; + if (dbg && status!=ERROR_SUCCESS) log << " BluetoothSetServiceState()...ERROR" << endl; + } + + if (status!=ERROR_SUCCESS) { + string t; + if (status==ERROR_INVALID_PARAMETER) t="ERROR_INVALID_PARAMETER"; + else if (status==ERROR_SERVICE_DOES_NOT_EXIST) t="ERROR_SERVICE_DOES_NOT_EXIST"; + else if (status==E_INVALIDARG) t="E_INVALIDARG"; + // if (status!=E_INVALIDARG) throw_("BluetoothSetServiceState() failed with errorcode " + t); + // else log << " ** error: BluetoothSetServiceState() returned " << t << endl; + + if (status==E_INVALIDARG) invalidargflag=true; + log << " ** error: BluetoothSetServiceState() returned " << t << endl; + log << endl << "Done [FAILED]" << endl; + return false; + } + + // sleep while windows slowly does strange things + Sleep(btsleep); + + if (lib!="none") + { + if (Wiilib::OpenDevices(lib,wiitimeout,rumble,dbg)==0) { + log << " ** error: service could not be opened..." << endl<< "Done [FAILED]" << endl; + return false; + } + } + + log << " service on device enabled..." << endl << " opended device successfully..." << endl << "Done [OK]" << endl; + return true; + } + + if (BluetoothFindNextDevice(hbf(), &bdi) == FALSE) break; + ++i; + } + log << " ** error: device not mathced" << endl<< "Done [FAILED]" << endl; + return false; +} + +bool AutoOpenDevice(const string& d,const string& btr,const int timeout,const int wiitimeout,const int usbsleep,const int btsleep,const int usbmsleep,const string& cf,const string& lib,const int removemode,const int usbmode,const bool wb,const int scanretries,const bool dbg,bool& invalidargflag) +{ + FUNSTACK; + + int cl=0; + if (g_automode_callback==0) g_automode_callback=&DummyCallback; + + if (usbmode==2 && ScanforUSBmicrodevs()!=1) { + log << "Could not find any USBm devices" << endl << "Wiiscan done [FAILED]" << endl; + return false; + } + if (usbmode==3 && ScanforDelcomdevs()!=1) { + log << "Could not find any Delcom devices" << endl << "Wiiscan done [FAILED]" << endl; + return false; + } + + g_automode_callback(cl++); + + timer total; + assert(usbsleep>=0 && btsleep>=0 && usbmsleep>=0); + + log << "Auto-connecting to device <" << d << ">" << endl; + + // check valid config file + if (!FileExists(cf)) throw_("config file <" + cf + "> is missing"); + const Configfile c(cf); + if (dbg) log << c; + if (!c.hasEntry("all_usb_hubs")) throw_("config file missing entry"); + if (!c.hasEntry("active_usb_hub")) throw_("config file missing entry"); + if (!c.hasEntry("allowed_wiimote_adr")) throw_("config file missing entry"); + if (!c.hasEntry("whiteboard_software")) throw_("config file missing entry"); + + // test if already opened + g_automode_callback(cl++); + + if (removemode==0 && Wiilib::OpenDevices(lib,wiitimeout,false,dbg,true)>=1) { + log << " service is already opened..." << endl << "Done [OK]" << endl; + + if (wb){ + // fireup whiteboard software + if (!c.hasEntry("whiteboard_software")) log << " ** warning: config file is missing entry " << endl; + else System(c.Get("whiteboard_software",true),false,false); + } + + return true; + } + + // power down now if using USB io board + g_automode_callback(cl++); + USBupdown(usbmode,false,dbg); + timer t; + + // remove old entries in HID + g_automode_callback(cl++); + if(removemode!=2) RemoveDevice(d,dbg); + + // Cycle usb hub, turn off usb power and restart the wiimote + g_automode_callback(cl++); + if(usbmode==1){ + const string usbhub=c.Get("active_usb_hub"); + if(usbhub!="\"\""){ + USBupdown(usbmode,false,dbg,usbhub); + Sleep(usbsleep); + USBupdown(usbmode,true,dbg,usbhub); + + g_automode_callback(cl++); + + // wait for usb hub to be up, takes some seconds (1000ms to 2000ms approx) + Sleep(1800); + + // wait for usb radio to be ready + g_automode_callback(cl++); + HANDLE hRadio=RadioInfo(btr,dbg); + + int i=0; + while(hRadio==NULL) { + if (i++==0) log << " radio not ready, delaying."; + else log << "."; + Sleep(500); + hRadio=RadioInfo(btr,dbg); + if (i>10) throw_("could not connect to bluetooth radio device"); + } + CloseHandle(hRadio); + if (i>0) log << endl; + } + } else if (usbmode!=0) { + while (t.elapsed()*1000=1 ); + while(!openok && ++retries<=scanretries){ + openok=OpenDevice(d,btr,min(retries*timeout,8000),wiitimeout,c("allowed_wiimote_adr"),btsleep,lib,false,false,dbg,invalidargflag); + } + + if (openok){ + g_automode_callback(cl++); + if (wb){ + g_automode_callback(cl++); + // terminate running wb's + + // fireup whiteboard software + if (!c.hasEntry("whiteboard_software")) log << " ** warning: config file is missing entry " << endl; + else System(c.Get("whiteboard_software",true),false,false); + } + } + if (dbg) log << " Elapsed time: " << total.elapsed() << " [s]" << endl; + + // taking longer than 20 sec will bring up an annoying windows reboot dialog, + // that can be ignored, hence warn at 18 sec + if (total.elapsed()>18) log << " ** warning: connection took a long time to finish, this may cause a windows reboot dialog, that can be ignored" << endl; + + if (!openok) log << " ** warning: could not open device" << endl << "Done [FAILED]" << endl; + + assert(cl<10); + g_automode_callback(42); + return openok; +} + +int Usage(const args& arg,const string msg="") +{ + FUNSTACK; + if (msg.size()>0) log << msg << endl; + log << "Usage: " << arg[0] << " <-a | -c | -d | -r | -s | -usbup | -usbdown> [-cf ] [-lf ] [-b ] [-t ] [-u ] [-p ] [-w ] [-q ] [-f ] [-m ] [-l ] [-y] [-wb] [-v] " << endl; + log << " " << Version() << " " << Config() << endl; + log << " modes:" << endl; + log << " -a : autoconnect to device" << endl; + log << " -c : connect the device, that matches this name" << endl; + log << " -d : deletes the device, that matches this name" << endl; + log << " -r: lookup and list bluetooth radio devices" << endl; + log << " -s: scan external bluetooth devices" << endl; + log << " -usbdown: disable usb hubs" << endl; + log << " -usbup: enable usb hubs" << endl; + log << " options:" << endl; + log << " -cf : specify a distinct configurationfile, default=" << DEFAULT_configfile << endl; + log << " -lf : specify a distinct logfile, default=" << DEFAULT_logfile << endl; + log << " -b : automode bluetooth connection sleep in milliseconds, default=" << DEFAULT_btsleep << endl; + log << " -t : timeout for bluetooth stack in milliseconds, default=" << DEFAULT_timeout << endl; + log << " -u : automode usb connection sleep in milliseconds, default=" << DEFAULT_usbsleep << endl; + log << " -p : automode usbm post-connection sleep in milliseconds, default=" << DEFAULT_usbmsleep << endl; + log << " -w : timeout for wiimote in milliseconds, default=" << DEFAULT_wiitimeout << endl; + log << " -q : use bluetooth radio with this address (not working), default=any device" << endl; + log << " -f : pre-remove mode of device, 0=remove if not connectable, 1=always remove, 2=never remove, default=" << DEFAULT_removemode << endl; + log << " -m : choose USB powercycle mode, 0=no power cycle, 1=use USB hub, 2=use USBm IO hardware, 3=use USB Delcon IO hardware" << endl; + log << " -l : use specific wiimote library, lib can be one-of {wiiuse,wiimotelib, default=" << DEFAULT_wiilib << endl; + log << " -y : scan retries in automode, default=" << DEFAULT_scanretries<< endl; + log << " -wb: start whiteboard in automode" << endl; + log << " -nowb: do not start whiteboard in automode" << endl; + log << " -v: enable extra debugging printouts" << endl; + log << " default mode: -a \"" << NINTENDO_DEV << "\"" << endl; + log << " note: \"nintendo\" is a shortcut for \"" << NINTENDO_DEV << "\"" << endl; + return -1; +} + +int main(int argc,char** argv) +{ + FUNSTACK; + bool invalidargflag=false; + try{ + SetNiceLevel(-15); // set to time-critical + args arg(argc,argv); + + // first, parse the command line configfile, then load the config, finally override any configfile options with given command line options + string cf=arg.parseval("-cf",DEFAULT_configfile); + + bool v=false,wb=false; + int timeout =DEFAULT_timeout; + int wiitimeout=DEFAULT_wiitimeout; + int usbsleep =DEFAULT_usbsleep; + int usbmsleep =DEFAULT_usbmsleep; + int btsleep =DEFAULT_btsleep; + int usbmode =DEFAULT_usbmode; + int removemode=DEFAULT_removemode; + int scanretries=DEFAULT_scanretries; + string a,c,d,btr,lf=DEFAULT_logfile,dev=NINTENDO_DEV,lib=DEFAULT_wiilib; + + if (FileExists(cf)){ + const Configfile cnf(cf); + if (cnf.hasEntry("option_device")) dev=strip(cnf.Get("option_device"),'"'); + if (cnf.hasEntry("option_timeout")) timeout=cnf.Get("option_timeout"); + if (cnf.hasEntry("option_wiitimeout")) wiitimeout=cnf.Get("option_wiitimeout"); + if (cnf.hasEntry("option_usbsleep")) usbsleep=cnf.Get("option_usbsleep"); + if (cnf.hasEntry("option_usbmsleep")) usbmsleep=cnf.Get("option_usbmsleep"); + if (cnf.hasEntry("option_btsleep")) btsleep=cnf.Get("option_btsleep"); + if (cnf.hasEntry("option_usbpowermode")) usbmode=cnf.Get("option_usbpowermode"); + if (cnf.hasEntry("option_removemode")) removemode=cnf.Get("option_removemode"); + if (cnf.hasEntry("option_debug")) v=cnf.Get("option_debug"); + if (cnf.hasEntry("option_startwhiteboard")) wb=cnf.Get("option_startwhiteboard"); + if (cnf.hasEntry("option_logfile")) lf=cnf.Get("option_logfile"); + if (cnf.hasEntry("option_btradio")) btr=cnf.Get("option_btradio"); + if (cnf.hasEntry("option_wiilib")) lib=cnf.Get("option_wiilib"); + if (cnf.hasEntry("option_scanretries"))scanretries=cnf.Get("option_scanretries"); + } + + // parse argumets via args class + const bool r=arg.parseopt("-r"); + const bool s=arg.parseopt("-s"); + const bool u=arg.parseopt("-?"); + const bool usbdown=arg.parseopt("-usbdown"); + const bool usbup=arg.parseopt("-usbup"); + v=arg.parseopt("-v") || v; + wb=arg.parseopt("-wb") || wb; + wb=!arg.parseopt("-nowb") && wb; + timeout =arg.parseval("-t",timeout); + wiitimeout=arg.parseval("-w",wiitimeout); + usbsleep =arg.parseval("-u",usbsleep); + usbmsleep =arg.parseval("-p",usbmsleep); + btsleep =arg.parseval("-b",btsleep); + usbmode =arg.parseval("-m",usbmode); + removemode=arg.parseval("-f",removemode); + a=arg.parseval("-a",""); + c=arg.parseval("-c",""); + d=arg.parseval("-d",""); + lf=arg.parseval("-lf",lf); + btr=arg.parseval("-q",btr); + lib=arg.parseval("-l",lib); + scanretries=arg.parseval("-y",scanretries); + + if (arg.size()!=1) return Usage(arg); + + // apply shortcuts + if (a=="nintendo") a=dev; + if (c=="nintendo") c=dev; + if (d=="nintendo") d=dev; + + // open log file before Usage call + if(lf!=DEFAULT_logfile){ + lf=strip(lf,'"'); // cannot handle filenames in citation quotes + if (lf=="cout" || lf=="std::cout") g_log=&cout; + else if (lf=="cerr" || lf=="std::cerr") g_log=&cerr; + else { + g_log.open(lf,ios_base::app); + g_log.writelogheader("Wiiscan::main()"); + if (v) log << "Using logfile <" << lf << ">" << endl; + } + } + + const int check=(r ? 1 : 0)+ (s ? 1 : 0) + (a.size()>0 ? 1 : 0) + (c.size()>0 ? 1 : 0) + (d.size()>0 ? 1 : 0) + usbup + usbdown; + if (check>1 || u) return Usage(arg); + + if (timeout<20) throw_("bad value of timeout, should be >=20"); + if (wiitimeout<20)throw_("bad value of wiitimeout, should be >=20"); + if (usbsleep<20) throw_("bad value of usbsleep, should be >=20"); + if (usbmsleep<20) throw_("bad value of usbmsleep, should be >=20"); + if (btsleep<20) throw_("bad value of btsleep, should be >=20"); + if (usbdown && usbup) throw_("bad value of usbdown/up, cannot be used at the same time"); + if (wiitimeout%1000!=0) throw_("bad value, wiitimeout should be divisable with 1000"); + if (btr.size()>0 && btr.size()!=17) throw_("bluetooth radio must be in the form xx:xx:xx:xx:xx:xx"); + if (usbmode<0 || usbmode>3) throw_("bad usbpowermode, must be 0,1,2, or 3"); + if (removemode<0 || removemode>2) throw_("bad removemode, must be 0,1 or 2"); + if (scanretries<=0) throw_("bad value of scanretries, should be > 0"); + if (a.size()==0) a=NINTENDO_DEV; + + if (v) { + log << Version() << " " << Config() << endl; + log << "Values:" << endl; + log << " a=" << a << endl; + log << " c=" << c << endl; + log << " d=" << d << endl; + log << " r=" << r << endl; + log << " s=" << s << endl; + log << " v=" << v << endl; + log << " cf=" << cf << endl; + log << " lf=" << lf << endl; + log << " wb=" << wb << endl; + log << " btr=" << btr << endl; + log << " lib=" << lib << endl; + log << " usbmode=" << usbmode << endl; + log << " removemode=" << removemode << endl; + log << " scanretries=" << scanretries << endl; + log << " usbup =" << usbup << endl; + log << " usbdown=" << usbup << endl; + log << "Timeouts:" << endl; + log << " timeout =" << timeout << endl; + log << " wiitimeout=" << wiitimeout << endl; + log << " usbsleep =" << usbsleep << endl; + log << " usbmsleep =" << usbmsleep << endl; + log << " btsleep =" << btsleep << endl; + } + + bool ret=true; + + if (r) ret=RadioInfo (btr.size()==0 ? "all" : btr,v)!=NULL; + else if (s) ret=ScanDevices (timeout); + else if (c.size()>0) ret=OpenDevice (c,btr,timeout,wiitimeout,vector(),btsleep,lib,true,true,v,invalidargflag); + else if (d.size()>0) ret=RemoveDevice(d,v); + else if (usbup) ret=USBupdown (usbmode,true, v,Configfile(cf).Get("active_usb_hub",true)); + else if (usbdown) ret=USBupdown (usbmode,false,v,Configfile(cf).Get("active_usb_hub",true)); + else ret=AutoOpenDevice(a,btr,timeout,wiitimeout,usbsleep,btsleep,usbmsleep,cf,lib,removemode,usbmode,wb,scanretries,v,invalidargflag); // default mode + + log << "Wiiscan done " << (ret ? "[OK]" : "[FAILED]") << endl; + + if (invalidargflag) { + if (v) log << "** warning: return=-3 (E_INVALIDARG)" << endl; + return -3; + } + + if (v) log << "returning: return=" << (ret ? "0" : "-1") << endl; + return ret ? 0 : -1; // 0=Ok, -1=fail, -2=fail with exception, -3=fail with E_INVALIDARG + } + CATCH_ALL; + + assert(invalidargflag==false); // flag not setted => no throw in when E_INVALIDARG encoutered + log << "Wiiscan done [FAILED]" << endl; + return -2; +} + +}; // namespace Wiiscan diff --git a/cpp/wiiscan/src/wiiuse.h b/cpp/wiiscan/src/wiiuse.h new file mode 100644 index 0000000..9dff81c --- /dev/null +++ b/cpp/wiiscan/src/wiiuse.h @@ -0,0 +1,653 @@ +/* + * wiiuse + * + * Written By: + * Michael Laforest < para > + * Email: < thepara (--AT--) g m a i l [--DOT--] com > + * + * Copyright 2006-2007 + * + * This file is part of wiiuse. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * $Header$ + * + */ + +/** + * @file + * + * @brief API header file. + * + * If this file is included from inside the wiiuse source + * and not from a third party program, then wiimote_internal.h + * is also included which extends this file. + */ + +#ifndef WIIUSE_H_INCLUDED +#define WIIUSE_H_INCLUDED + +#ifdef _WIN32 + /* windows */ + #include +#else + /* nix */ + #include +#endif + +#ifdef WIIUSE_INTERNAL_H_INCLUDED + #define WCONST +#else + #define WCONST const +#endif + +/* led bit masks */ +#define WIIMOTE_LED_NONE 0x00 +#define WIIMOTE_LED_1 0x10 +#define WIIMOTE_LED_2 0x20 +#define WIIMOTE_LED_3 0x40 +#define WIIMOTE_LED_4 0x80 + +/* button codes */ +#define WIIMOTE_BUTTON_TWO 0x0001 +#define WIIMOTE_BUTTON_ONE 0x0002 +#define WIIMOTE_BUTTON_B 0x0004 +#define WIIMOTE_BUTTON_A 0x0008 +#define WIIMOTE_BUTTON_MINUS 0x0010 +#define WIIMOTE_BUTTON_ZACCEL_BIT6 0x0020 +#define WIIMOTE_BUTTON_ZACCEL_BIT7 0x0040 +#define WIIMOTE_BUTTON_HOME 0x0080 +#define WIIMOTE_BUTTON_LEFT 0x0100 +#define WIIMOTE_BUTTON_RIGHT 0x0200 +#define WIIMOTE_BUTTON_DOWN 0x0400 +#define WIIMOTE_BUTTON_UP 0x0800 +#define WIIMOTE_BUTTON_PLUS 0x1000 +#define WIIMOTE_BUTTON_ZACCEL_BIT4 0x2000 +#define WIIMOTE_BUTTON_ZACCEL_BIT5 0x4000 +#define WIIMOTE_BUTTON_UNKNOWN 0x8000 +#define WIIMOTE_BUTTON_ALL 0x1F9F + +/* nunchul button codes */ +#define NUNCHUK_BUTTON_Z 0x01 +#define NUNCHUK_BUTTON_C 0x02 +#define NUNCHUK_BUTTON_ALL 0x03 + +/* classic controller button codes */ +#define CLASSIC_CTRL_BUTTON_UP 0x0001 +#define CLASSIC_CTRL_BUTTON_LEFT 0x0002 +#define CLASSIC_CTRL_BUTTON_ZR 0x0004 +#define CLASSIC_CTRL_BUTTON_X 0x0008 +#define CLASSIC_CTRL_BUTTON_A 0x0010 +#define CLASSIC_CTRL_BUTTON_Y 0x0020 +#define CLASSIC_CTRL_BUTTON_B 0x0040 +#define CLASSIC_CTRL_BUTTON_ZL 0x0080 +#define CLASSIC_CTRL_BUTTON_FULL_R 0x0200 +#define CLASSIC_CTRL_BUTTON_PLUS 0x0400 +#define CLASSIC_CTRL_BUTTON_HOME 0x0800 +#define CLASSIC_CTRL_BUTTON_MINUS 0x1000 +#define CLASSIC_CTRL_BUTTON_FULL_L 0x2000 +#define CLASSIC_CTRL_BUTTON_DOWN 0x4000 +#define CLASSIC_CTRL_BUTTON_RIGHT 0x8000 +#define CLASSIC_CTRL_BUTTON_ALL 0xFEFF + +/* guitar hero 3 button codes */ +#define GUITAR_HERO_3_BUTTON_STRUM_UP 0x0001 +#define GUITAR_HERO_3_BUTTON_YELLOW 0x0008 +#define GUITAR_HERO_3_BUTTON_GREEN 0x0010 +#define GUITAR_HERO_3_BUTTON_BLUE 0x0020 +#define GUITAR_HERO_3_BUTTON_RED 0x0040 +#define GUITAR_HERO_3_BUTTON_ORANGE 0x0080 +#define GUITAR_HERO_3_BUTTON_PLUS 0x0400 +#define GUITAR_HERO_3_BUTTON_MINUS 0x1000 +#define GUITAR_HERO_3_BUTTON_STRUM_DOWN 0x4000 +#define GUITAR_HERO_3_BUTTON_ALL 0xFEFF + + +/* wiimote option flags */ +#define WIIUSE_SMOOTHING 0x01 +#define WIIUSE_CONTINUOUS 0x02 +#define WIIUSE_ORIENT_THRESH 0x04 +#define WIIUSE_INIT_FLAGS (WIIUSE_SMOOTHING | WIIUSE_ORIENT_THRESH) + +#define WIIUSE_ORIENT_PRECISION 100.0f + +/* expansion codes */ +#define EXP_NONE 0 +#define EXP_NUNCHUK 1 +#define EXP_CLASSIC 2 +#define EXP_GUITAR_HERO_3 3 + +/* IR correction types */ +typedef enum ir_position_t { + WIIUSE_IR_ABOVE, + WIIUSE_IR_BELOW +} ir_position_t; + +/** + * @brief Check if a button is pressed. + * @param dev Pointer to a wiimote_t or expansion structure. + * @param button The button you are interested in. + * @return 1 if the button is pressed, 0 if not. + */ +#define IS_PRESSED(dev, button) ((dev->btns & button) == button) + +/** + * @brief Check if a button is being held. + * @param dev Pointer to a wiimote_t or expansion structure. + * @param button The button you are interested in. + * @return 1 if the button is held, 0 if not. + */ +#define IS_HELD(dev, button) ((dev->btns_held & button) == button) + +/** + * @brief Check if a button is released on this event. \n\n + * This does not mean the button is not pressed, it means \n + * this button was just now released. + * @param dev Pointer to a wiimote_t or expansion structure. + * @param button The button you are interested in. + * @return 1 if the button is released, 0 if not. + * + */ +#define IS_RELEASED(dev, button) ((dev->btns_released & button) == button) + +/** + * @brief Check if a button has just been pressed this event. + * @param dev Pointer to a wiimote_t or expansion structure. + * @param button The button you are interested in. + * @return 1 if the button is pressed, 0 if not. + */ +#define IS_JUST_PRESSED(dev, button) (IS_PRESSED(dev, button) && !IS_HELD(dev, button)) + +/** + * @brief Return the IR sensitivity level. + * @param wm Pointer to a wiimote_t structure. + * @param lvl [out] Pointer to an int that will hold the level setting. + * If no level is set 'lvl' will be set to 0. + */ +#define WIIUSE_GET_IR_SENSITIVITY(dev, lvl) \ + do { \ + if ((wm->state & 0x0200) == 0x0200) *lvl = 1; \ + else if ((wm->state & 0x0400) == 0x0400) *lvl = 2; \ + else if ((wm->state & 0x0800) == 0x0800) *lvl = 3; \ + else if ((wm->state & 0x1000) == 0x1000) *lvl = 4; \ + else if ((wm->state & 0x2000) == 0x2000) *lvl = 5; \ + else *lvl = 0; \ + } while (0) + +#define WIIUSE_USING_ACC(wm) ((wm->state & 0x020) == 0x020) +#define WIIUSE_USING_EXP(wm) ((wm->state & 0x040) == 0x040) +#define WIIUSE_USING_IR(wm) ((wm->state & 0x080) == 0x080) +#define WIIUSE_USING_SPEAKER(wm) ((wm->state & 0x100) == 0x100) + +#define WIIUSE_IS_LED_SET(wm, num) ((wm->leds & WIIMOTE_LED_##num) == WIIMOTE_LED_##num) + +/* + * Largest known payload is 21 bytes. + * Add 2 for the prefix and round up to a power of 2. + */ +#define MAX_PAYLOAD 32 + +/* + * This is left over from an old hack, but it may actually + * be a useful feature to keep so it wasn't removed. + */ +#ifdef WIN32 + #define WIIMOTE_DEFAULT_TIMEOUT 10 + #define WIIMOTE_EXP_TIMEOUT 10 +#endif + +typedef unsigned char byte; +typedef char sbyte; + +struct wiimote_t; +struct vec3b_t; +struct orient_t; +struct gforce_t; + + +/** + * @brief Callback that handles a read event. + * + * @param wm Pointer to a wiimote_t structure. + * @param data Pointer to the filled data block. + * @param len Length in bytes of the data block. + * + * @see wiiuse_init() + * + * A registered function of this type is called automatically by the wiiuse + * library when the wiimote has returned the full data requested by a previous + * call to wiiuse_read_data(). + */ +typedef void (*wiiuse_read_cb)(struct wiimote_t* wm, byte* data, unsigned short len); + + +/** + * @struct read_req_t + * @brief Data read request structure. + */ +struct read_req_t { + wiiuse_read_cb cb; /**< read data callback */ + byte* buf; /**< buffer where read data is written */ + unsigned int addr; /**< the offset that the read started at */ + unsigned short size; /**< the length of the data read */ + unsigned short wait; /**< num bytes still needed to finish read */ + byte dirty; /**< set to 1 if not using callback and needs to be cleaned up */ + + struct read_req_t* next; /**< next read request in the queue */ +}; + + +/** + * @struct vec2b_t + * @brief Unsigned x,y byte vector. + */ +typedef struct vec2b_t { + byte x, y; +} vec2b_t; + + +/** + * @struct vec3b_t + * @brief Unsigned x,y,z byte vector. + */ +typedef struct vec3b_t { + byte x, y, z; +} vec3b_t; + + +/** + * @struct vec3f_t + * @brief Signed x,y,z float struct. + */ +typedef struct vec3f_t { + float x, y, z; +} vec3f_t; + + +/** + * @struct orient_t + * @brief Orientation struct. + * + * Yaw, pitch, and roll range from -180 to 180 degrees. + */ +typedef struct orient_t { + float roll; /**< roll, this may be smoothed if enabled */ + float pitch; /**< pitch, this may be smoothed if enabled */ + float yaw; + + float a_roll; /**< absolute roll, unsmoothed */ + float a_pitch; /**< absolute pitch, unsmoothed */ +} orient_t; + + +/** + * @struct gforce_t + * @brief Gravity force struct. + */ +typedef struct gforce_t { + float x, y, z; +} gforce_t; + + +/** + * @struct accel_t + * @brief Accelerometer struct. For any device with an accelerometer. + */ +typedef struct accel_t { + struct vec3b_t cal_zero; /**< zero calibration */ + struct vec3b_t cal_g; /**< 1g difference around 0cal */ + + float st_roll; /**< last smoothed roll value */ + float st_pitch; /**< last smoothed roll pitch */ + float st_alpha; /**< alpha value for smoothing [0-1] */ +} accel_t; + + +/** + * @struct ir_dot_t + * @brief A single IR source. + */ +typedef struct ir_dot_t { + byte visible; /**< if the IR source is visible */ + + unsigned int x; /**< interpolated X coordinate */ + unsigned int y; /**< interpolated Y coordinate */ + + short rx; /**< raw X coordinate (0-1023) */ + short ry; /**< raw Y coordinate (0-767) */ + + byte order; /**< increasing order by x-axis value */ + + byte size; /**< size of the IR dot (0-15) */ +} ir_dot_t; + + +/** + * @enum aspect_t + * @brief Screen aspect ratio. + */ +typedef enum aspect_t { + WIIUSE_ASPECT_4_3, + WIIUSE_ASPECT_16_9 +} aspect_t; + + +/** + * @struct ir_t + * @brief IR struct. Hold all data related to the IR tracking. + */ +typedef struct ir_t { + struct ir_dot_t dot[4]; /**< IR dots */ + byte num_dots; /**< number of dots at this time */ + + enum aspect_t aspect; /**< aspect ratio of the screen */ + + enum ir_position_t pos; /**< IR sensor bar position */ + + unsigned int vres[2]; /**< IR virtual screen resolution */ + int offset[2]; /**< IR XY correction offset */ + int state; /**< keeps track of the IR state */ + + int ax; /**< absolute X coordinate */ + int ay; /**< absolute Y coordinate */ + + int x; /**< calculated X coordinate */ + int y; /**< calculated Y coordinate */ + + float distance; /**< pixel distance between first 2 dots*/ + float z; /**< calculated distance */ +} ir_t; + + +/** + * @struct joystick_t + * @brief Joystick calibration structure. + * + * The angle \a ang is relative to the positive y-axis into quadrant I + * and ranges from 0 to 360 degrees. So if the joystick is held straight + * upwards then angle is 0 degrees. If it is held to the right it is 90, + * down is 180, and left is 270. + * + * The magnitude \a mag is the distance from the center to where the + * joystick is being held. The magnitude ranges from 0 to 1. + * If the joystick is only slightly tilted from the center the magnitude + * will be low, but if it is closer to the outter edge the value will + * be higher. + */ +typedef struct joystick_t { + struct vec2b_t max; /**< maximum joystick values */ + struct vec2b_t min; /**< minimum joystick values */ + struct vec2b_t center; /**< center joystick values */ + + float ang; /**< angle the joystick is being held */ + float mag; /**< magnitude of the joystick (range 0-1) */ +} joystick_t; + + +/** + * @struct nunchuk_t + * @brief Nunchuk expansion device. + */ +typedef struct nunchuk_t { + struct accel_t accel_calib; /**< nunchuk accelerometer calibration */ + struct joystick_t js; /**< joystick calibration */ + + int* flags; /**< options flag (points to wiimote_t.flags) */ + + byte btns; /**< what buttons have just been pressed */ + byte btns_held; /**< what buttons are being held down */ + byte btns_released; /**< what buttons were just released this */ + + float orient_threshold; /**< threshold for orient to generate an event */ + int accel_threshold; /**< threshold for accel to generate an event */ + + struct vec3b_t accel; /**< current raw acceleration data */ + struct orient_t orient; /**< current orientation on each axis */ + struct gforce_t gforce; /**< current gravity forces on each axis */ +} nunchuk_t; + + +/** + * @struct classic_ctrl_t + * @brief Classic controller expansion device. + */ +typedef struct classic_ctrl_t { + short btns; /**< what buttons have just been pressed */ + short btns_held; /**< what buttons are being held down */ + short btns_released; /**< what buttons were just released this */ + + float r_shoulder; /**< right shoulder button (range 0-1) */ + float l_shoulder; /**< left shoulder button (range 0-1) */ + + struct joystick_t ljs; /**< left joystick calibration */ + struct joystick_t rjs; /**< right joystick calibration */ +} classic_ctrl_t; + + +/** + * @struct guitar_hero_3_t + * @brief Guitar Hero 3 expansion device. + */ +typedef struct guitar_hero_3_t { + short btns; /**< what buttons have just been pressed */ + short btns_held; /**< what buttons are being held down */ + short btns_released; /**< what buttons were just released this */ + + float whammy_bar; /**< whammy bar (range 0-1) */ + + struct joystick_t js; /**< joystick calibration */ +} guitar_hero_3_t; + + +/** + * @struct expansion_t + * @brief Generic expansion device plugged into wiimote. + */ +typedef struct expansion_t { + int type; /**< type of expansion attached */ + + union { + struct nunchuk_t nunchuk; + struct classic_ctrl_t classic; + struct guitar_hero_3_t gh3; + }; +} expansion_t; + + +/** + * @enum win32_bt_stack_t + * @brief Available bluetooth stacks for Windows. + */ +typedef enum win_bt_stack_t { + WIIUSE_STACK_UNKNOWN, + WIIUSE_STACK_MS, + WIIUSE_STACK_BLUESOLEIL +} win_bt_stack_t; + + +/** + * @struct wiimote_state_t + * @brief Significant data from the previous event. + */ +typedef struct wiimote_state_t { + /* expansion_t */ + float exp_ljs_ang; + float exp_rjs_ang; + float exp_ljs_mag; + float exp_rjs_mag; + unsigned short exp_btns; + struct orient_t exp_orient; + struct vec3b_t exp_accel; + float exp_r_shoulder; + float exp_l_shoulder; + + /* ir_t */ + int ir_ax; + int ir_ay; + float ir_distance; + + struct orient_t orient; + unsigned short btns; + + struct vec3b_t accel; +} wiimote_state_t; + + +/** + * @enum WIIUSE_EVENT_TYPE + * @brief Events that wiiuse can generate from a poll. + */ +typedef enum WIIUSE_EVENT_TYPE { + WIIUSE_NONE = 0, + WIIUSE_EVENT, + WIIUSE_STATUS, + WIIUSE_CONNECT, + WIIUSE_DISCONNECT, + WIIUSE_UNEXPECTED_DISCONNECT, + WIIUSE_READ_DATA, + WIIUSE_NUNCHUK_INSERTED, + WIIUSE_NUNCHUK_REMOVED, + WIIUSE_CLASSIC_CTRL_INSERTED, + WIIUSE_CLASSIC_CTRL_REMOVED, + WIIUSE_GUITAR_HERO_3_CTRL_INSERTED, + WIIUSE_GUITAR_HERO_3_CTRL_REMOVED +} WIIUSE_EVENT_TYPE; + +/** + * @struct wiimote_t + * @brief Wiimote structure. + */ +typedef struct wiimote_t { + WCONST int unid; /**< user specified id */ + + #ifndef WIN32 + WCONST bdaddr_t bdaddr; /**< bt address */ + WCONST char bdaddr_str[18]; /**< readable bt address */ + WCONST int out_sock; /**< output socket */ + WCONST int in_sock; /**< input socket */ + #else + WCONST HANDLE dev_handle; /**< HID handle */ + WCONST OVERLAPPED hid_overlap; /**< overlap handle */ + WCONST enum win_bt_stack_t stack; /**< type of bluetooth stack to use */ + WCONST int timeout; /**< read timeout */ + WCONST byte normal_timeout; /**< normal timeout */ + WCONST byte exp_timeout; /**< timeout for expansion handshake */ + #endif + + WCONST int state; /**< various state flags */ + WCONST byte leds; /**< currently lit leds */ + WCONST float battery_level; /**< battery level */ + + WCONST int flags; /**< options flag */ + + WCONST byte handshake_state; /**< the state of the connection handshake */ + + WCONST struct read_req_t* read_req; /**< list of data read requests */ + WCONST struct accel_t accel_calib; /**< wiimote accelerometer calibration */ + WCONST struct expansion_t exp; /**< wiimote expansion device */ + + WCONST struct vec3b_t accel; /**< current raw acceleration data */ + WCONST struct orient_t orient; /**< current orientation on each axis */ + WCONST struct gforce_t gforce; /**< current gravity forces on each axis */ + + WCONST struct ir_t ir; /**< IR data */ + + WCONST unsigned short btns; /**< what buttons have just been pressed */ + WCONST unsigned short btns_held; /**< what buttons are being held down */ + WCONST unsigned short btns_released; /**< what buttons were just released this */ + + WCONST float orient_threshold; /**< threshold for orient to generate an event */ + WCONST int accel_threshold; /**< threshold for accel to generate an event */ + + WCONST struct wiimote_state_t lstate; /**< last saved state */ + + WCONST WIIUSE_EVENT_TYPE event; /**< type of event that occured */ + WCONST byte event_buf[MAX_PAYLOAD]; /**< event buffer */ +} wiimote; + + +/***************************************** + * + * Include API specific stuff + * + *****************************************/ + +#ifdef _WIN32 + #define WIIUSE_EXPORT_DECL __declspec(dllexport) + #define WIIUSE_IMPORT_DECL __declspec(dllimport) +#else + #define WIIUSE_EXPORT_DECL + #define WIIUSE_IMPORT_DECL +#endif + +#ifdef WIIUSE_COMPILE_LIB + #define WIIUSE_EXPORT WIIUSE_EXPORT_DECL +#else + #define WIIUSE_EXPORT WIIUSE_IMPORT_DECL +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* wiiuse.c */ +WIIUSE_EXPORT extern const char* wiiuse_version(); + +WIIUSE_EXPORT extern struct wiimote_t** wiiuse_init(int wiimotes); +WIIUSE_EXPORT extern void wiiuse_disconnected(struct wiimote_t* wm); +WIIUSE_EXPORT extern void wiiuse_cleanup(struct wiimote_t** wm, int wiimotes); +WIIUSE_EXPORT extern void wiiuse_rumble(struct wiimote_t* wm, int status); +WIIUSE_EXPORT extern void wiiuse_toggle_rumble(struct wiimote_t* wm); +WIIUSE_EXPORT extern void wiiuse_set_leds(struct wiimote_t* wm, int leds); +WIIUSE_EXPORT extern void wiiuse_motion_sensing(struct wiimote_t* wm, int status); +WIIUSE_EXPORT extern int wiiuse_read_data(struct wiimote_t* wm, byte* buffer, unsigned int offset, unsigned short len); +WIIUSE_EXPORT extern int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, byte* data, byte len); +WIIUSE_EXPORT extern void wiiuse_status(struct wiimote_t* wm); +WIIUSE_EXPORT extern struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid); +WIIUSE_EXPORT extern int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable); +WIIUSE_EXPORT extern float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha); +WIIUSE_EXPORT extern void wiiuse_set_bluetooth_stack(struct wiimote_t** wm, int wiimotes, enum win_bt_stack_t type); +WIIUSE_EXPORT extern void wiiuse_set_orient_threshold(struct wiimote_t* wm, float threshold); +WIIUSE_EXPORT extern void wiiuse_resync(struct wiimote_t* wm); +WIIUSE_EXPORT extern void wiiuse_set_timeout(struct wiimote_t** wm, int wiimotes, byte normal_timeout, byte exp_timeout); +WIIUSE_EXPORT extern void wiiuse_set_accel_threshold(struct wiimote_t* wm, int threshold); + +/* connect.c */ +WIIUSE_EXPORT extern int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout); +WIIUSE_EXPORT extern int wiiuse_connect(struct wiimote_t** wm, int wiimotes); +WIIUSE_EXPORT extern void wiiuse_disconnect(struct wiimote_t* wm); + +/* events.c */ +WIIUSE_EXPORT extern int wiiuse_poll(struct wiimote_t** wm, int wiimotes); + +/* ir.c */ +WIIUSE_EXPORT extern void wiiuse_set_ir(struct wiimote_t* wm, int status); +WIIUSE_EXPORT extern void wiiuse_set_ir_vres(struct wiimote_t* wm, unsigned int x, unsigned int y); +WIIUSE_EXPORT extern void wiiuse_set_ir_position(struct wiimote_t* wm, enum ir_position_t pos); +WIIUSE_EXPORT extern void wiiuse_set_aspect_ratio(struct wiimote_t* wm, enum aspect_t aspect); +WIIUSE_EXPORT extern void wiiuse_set_ir_sensitivity(struct wiimote_t* wm, int level); + +/* nunchuk.c */ +WIIUSE_EXPORT extern void wiiuse_set_nunchuk_orient_threshold(struct wiimote_t* wm, float threshold); +WIIUSE_EXPORT extern void wiiuse_set_nunchuk_accel_threshold(struct wiimote_t* wm, int threshold); + + +#ifdef __cplusplus +} +#endif + + +#endif /* WIIUSE_H_INCLUDED */ + diff --git a/cpp/wiiscan/wiiscan.sln b/cpp/wiiscan/wiiscan.sln new file mode 100644 index 0000000..bb26b13 --- /dev/null +++ b/cpp/wiiscan/wiiscan.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual C++ Express 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wiiscan", "wiiscan.vcproj", "{6E522A84-9956-44F9-B30A-CF44F3CED276}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6E522A84-9956-44F9-B30A-CF44F3CED276}.Debug|Win32.ActiveCfg = Debug|Win32 + {6E522A84-9956-44F9-B30A-CF44F3CED276}.Debug|Win32.Build.0 = Debug|Win32 + {6E522A84-9956-44F9-B30A-CF44F3CED276}.Release|Win32.ActiveCfg = Release|Win32 + {6E522A84-9956-44F9-B30A-CF44F3CED276}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/cpp/wiiscan/wiiscan.test.h b/cpp/wiiscan/wiiscan.test.h new file mode 100644 index 0000000..dd5c4e5 --- /dev/null +++ b/cpp/wiiscan/wiiscan.test.h @@ -0,0 +1,1662 @@ +// Copyright © 2009 MergeIt, Aps. +// +// License LGPLv3+: GNU lesser LGPL version 3 or later . +// This is free software: you are free to change and redistribute it. +// There is NO WARRANTY, to the extent permitted by law. +// +// This file is part of wiiscan. +// +// wiiscan is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// wiiscan is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with wiiscan. If not, see . + +#define VERSION 0 +#define VERSION_REV 9 + +#define VC_EXTRALEAN + +#include "toolsfun.h" +#include "wiiuse.h" +#include "usbm.h" +#include "delcomdll.h" +#include +#include + +#pragma comment ( lib, "Lib/wiiuse.lib") +#pragma comment ( lib, "Lib/delcomdll.lib") +#pragma comment ( lib, "Irprops.lib") +#pragma comment ( lib, "Ws2_32.lib") + +void Connect_using_BT_Api(); + +namespace Wiiscan { + +// default values +const int MAX_DEVICE_COUNT =256; +const int DEFAULT_timeout =2000; +const int DEFAULT_wiitimeout=2000; +const int DEFAULT_usbsleep =500; +const int DEFAULT_usbmsleep =3000; +const int DEFAULT_btsleep =2000; +const int DEFAULT_usbmode =0; +const int DEFAULT_removemode=0; +const int DEFAULT_scanretries=1; +const string NINTENDO_DEV="Nintendo RVL-CNT-01"; +const string DEFAULT_configfile="wiiscan.ini"; +const string DEFAULT_logfile="cout"; +const string DEFAULT_wiilib="wiimotelib"; + +Logger& log=g_log; +void (*g_automode_callback)(const int)=0; +void DummyCallback(const int){FUNSTACK;} + +string ConvertName(const WCHAR* szName) +{ + FUNSTACK; + assert( MAX_PATH >= BLUETOOTH_MAX_NAME_SIZE ); + CHAR szDevName[MAX_PATH]; + memset(szDevName, 0x00, sizeof(szDevName)); + sprintf_s(szDevName,MAX_PATH,"%S", szName); + return string(szDevName); +} + +string ConvertAddress(const BLUETOOTH_ADDRESS& address) +{ + FUNSTACK; + string s; + char t[256]; + for(int i=5;i>=0;i--) { + sprintf_s(t,256,"%X",address.rgBytes[i]); + string x=t; + if (x.size()==1) x="0"+x; + s+=x+(i==0 ? "" : ":"); + } + return s; +} + +string tostring(const SYSTEMTIME& x) +{ + FUNSTACK; + // XXX warning: no converstion of GMT to local time + ostringstream s; + if (x.wHour==0 && x.wMinute==0 && x.wDay==0 && x.wYear==0 && x.wYear==0) s << ""; + else s << x.wHour << ":" << (x.wMinute<10 ? "0" : "") << x.wMinute << " d. " << x.wDay << "/" << x.wMonth << ", " << x.wYear; + return s.str(); +} + +string tostring(const BLUETOOTH_DEVICE_INFO& x) +{ + FUNSTACK; + ostringstream s; + s << " BLUETOOTH_DEVICE_INFO:" << endl; + s << " dwSize: " << x.dwSize << endl; + s << " Address: " << ConvertAddress(x.Address) << endl; + s << " ulClassofDevice:" << x.ulClassofDevice << endl; + s << " fConnected: " << x.fConnected << endl; + s << " fRemembered: " << x.fRemembered << endl; + s << " fAuthenticated: " << x.fAuthenticated << endl; + s << " stLastSeen: " << tostring(x.stLastSeen) << " [GMT]" << endl; + s << " stLastUsed: " << tostring(x.stLastUsed) << " [GMT]" << endl; + s << " szName: " << ConvertName(x.szName) << endl; + return s.str(); +} + +string tostring(const BLUETOOTH_RADIO_INFO& x) +{ + FUNSTACK; + ostringstream s; + s << " PBLUETOOTH_RADIO_INFO:" << endl; + s << " dwSize: " << x.dwSize << endl; + s << " Address: " << ConvertAddress(x.address) << endl; + s << " szName: " << ConvertName(x.szName) << endl; + s << " ulClassofDevice:" << x.ulClassofDevice << endl; + s << " lmpSubversion: " << x.lmpSubversion << endl; + s << " manufacturer: " << x.manufacturer << endl; + return s.str(); +} + +bool USB_hub_updown(const bool up,const string& usbhub) +{ + FUNSTACK; + log << (up ? "Enabling" : "Disabling") << " 'USB hub..." << endl; + int n=-1; + System(string("devcon ") + (up ? "enable " : "disable ") + usbhub,false,false,&n); + if (n!=0) throw_("devcon failed, this can be to a erroneous usbhub string or if devcon.exe is not found in path, please install it from http://support.microsoft.com/kb/311272"); + log << "Done [OK]" << endl; + return true; +} + +int ScanforUSBmicrodevs() +{ + USBio io; + io.USBm_FindDevices(); + return io.Devices(); +} + +bool USB_microio_updown(const bool up,const bool dbg) +{ + FUNSTACK; + log << (up ? "Enabling" : "Disabling") << " USBm io..." << endl; + + // Discover the USBmicro devices + USBio io; + io.USBm_FindDevices(); + const int d=io.Devices(); + if (d==0) return false; // throw_("could not find an USBm device"); + else if (d>1) throw_("found more that one USBm devices"); + + static bool premable=true; + if (dbg && premable) { + premable=false; + log << " USBm info:" << endl << io.version(); + } + + io.USBm_InitPorts(0); + io.USBm_DirectionA(0, 0xff, 0xff); + + if(up) io.USBm_ResetBit(0,5); + else io.USBm_SetBit (0,5); + + log << "Done [OK]" << endl; + return true; +} + +bool __stdcall CloseDelcomhandle(HANDLE h){return DelcomCloseDevice(h)==0;} + +void do_cmd(HANDLE di, int ma, int mi, int lsb, int msb, unsigned char *data, unsigned datalen) +{ + //static struct delcom_packet p; + PacketStruct p; + memset(&p, 0, sizeof(p)); + p.MajorCmd = ma; + p.MinorCmd = mi; + p.DataLSB = lsb; + p.DataMSB = msb; + if (DelcomSendPacket(di,&p,&p)<0) throw_("DelcomSendPacket() failed"); +} + +int ScanforDelcomdevs() +{ + // Discover the USB Delcom devices + DeviceNameStruct names[10]; // array to hold the device names found + return DelcomScanDevices(USBIODS, names, 10); +} + +bool USB_delconio_updown(const bool up,const bool dbg) +{ + FUNSTACK; + log << (up ? "Enabling" : "Disabling") << " USB Delcom io..." << endl; + + DeviceNameStruct names[10]; // array to hold the device names found + const int d=DelcomScanDevices(USBIODS, names, 10); + if (d==0) return false; //throw_("could not find an USB Delcon device"); + else if (d>1) throw_("found more that one USB Delcon devices"); + + static bool premable=true; + if (dbg && premable) { + premable=false; + log << " USB Delcon info:" << (char*)&names[0] << "\n SN=" << DelcomReadDeviceSerialNum((char*)&names[0], NULL) << endl; + } + + DeviceAutoClose h(DelcomOpenDevice((char*)&names[0],0),CloseDelcomhandle); + if(up) do_cmd(h(),10,1,0xFE,0,0,0); + else do_cmd(h(),10,1,0xFF,0,0,0); + + log << "Done [OK]" << endl; + return true; +} + +bool USBupdown(const int iomode,const bool up,const bool dbg,const string& usbhub="") +{ + if (iomode==0) return true; + if (iomode==1) return USB_hub_updown(up,usbhub); + if (iomode==2) return USB_microio_updown(up,dbg); + else if (iomode==3) return USB_delconio_updown(up,dbg); + else throw_("bad io mode"); + return false; +} + +class Wiilib +{ + // Wiilib class based on wiiuse library by Michael Laforest. + // Library located at http://www.wiiuse.net + // Wiiuse premable from wiiuse_v0.12/example/example.c: + + // + // wiiuse + // + // Written By: + // Michael Laforest < para > + // Email: < thepara (--AT--) g m a i l [--DOT--] com > + // + // Copyright 2006-2007 + // + // This file is part of wiiuse. + // + // This program is free software; you can redistribute it and/or modify + // it under the terms of the GNU General Public License as published by + // the Free Software Foundation; either version 3 of the License, or + // (at your option) any later version. + // + // This program is distributed in the hope that it will be useful, + // but WITHOUT ANY WARRANTY; without even the implied warranty of + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + // GNU General Public License for more details. + // + // You should have received a copy of the GNU General Public License + // along with this program. If not, see . + +private: + enum {MAX_WIIMOTES=1}; // use only one wiimote, but can connect to four + wiimote** m_wiimotes; + static int m_instantiations; + + int finddevices(const int wiitimeout,const bool rumble,const bool dbg) + { + FUNSTACK; + + // Find wiimote devices + const int to=wiitimeout/1000; + if (to<1) throw_("bad wiitimeout value, should be >=1000"); + const int found=wiiuse_find(m_wiimotes,MAX_WIIMOTES,to); + if (dbg) log << " wiiuse_find(0x0," << MAX_WIIMOTES << "," << to << ") return " << found << endl; + + if (found==0) { + if (dbg) log << " ** error: no wiimotes found" << endl; + return 0; + } + + // Connect to the wiimotes + assert(found>0); + + const int connected = wiiuse_connect(m_wiimotes, MAX_WIIMOTES); + if (connected==0){ + log << " ** error: could not connect to any wiimotes" << endl; + return 0; + } + if (dbg) log << " connected to " << connected<< " wiimotes (of " << MAX_WIIMOTES << " found)" << endl; + + + // Now set the LEDs and rumble for a second so it's easy + // to tell which wiimotes are connected (just like the wii does). + wiiuse_set_leds(m_wiimotes[0], WIIMOTE_LED_1); + + if (rumble){ + wiiuse_rumble(m_wiimotes[0], 1); + #ifndef WIN32 + usleep(200000); + #else + Sleep(200); + #endif + wiiuse_rumble(m_wiimotes[0], 0); + } + + // read some data to be sure of connection + for(int i=0;i<8;++i){ + Sleep(50); + + int led=0; + switch(i%6){ + case 0 : led=WIIMOTE_LED_1; break; + case 1 : led=WIIMOTE_LED_2; break; + case 2 : led=WIIMOTE_LED_3; break; + case 3 : led=WIIMOTE_LED_4; break; + case 4 : led=WIIMOTE_LED_3; break; + case 5 : led=WIIMOTE_LED_2; break; + } + wiiuse_rumble(m_wiimotes[0], 0); // to be sure + wiiuse_set_leds(m_wiimotes[0],led); + wiiuse_status(m_wiimotes[0]); + if (led!=m_wiimotes[0]->leds) throw_("bad LED status on wiimote, is it connected properly?"); + } + + // set all leds, retry a couple of time to be sure + for(int i=0;i<4;++i){ + wiiuse_set_leds(m_wiimotes[0],WIIMOTE_LED_1 | WIIMOTE_LED_2 | WIIMOTE_LED_3 | WIIMOTE_LED_4); + Sleep(100); + } + + return connected; + } + + Wiilib() : m_wiimotes(0) + { + FUNSTACK; + if (m_instantiations!=0) throw_("can only instatiate one Wiilib at a time"); + ++m_instantiations; // assumes a single-thread application, else we have a race condition here + + // Initialize an array of wiimote objects. + // The parameter is the number of wiimotes I want to create. + m_wiimotes = wiiuse_init(MAX_WIIMOTES); + // wiiuse_set_bluetooth_stack(m_wiimotes, MAX_WIIMOTES,WIIUSE_STACK_MS); must be done automatically + assert(m_instantiations==1); + } + + ~Wiilib() + { + FUNSTACK; + assert( m_instantiations==1 ); + + // Disconnect the wiimotes + if (m_wiimotes!=0) wiiuse_cleanup(m_wiimotes, MAX_WIIMOTES); + m_wiimotes=0; + --m_instantiations; + assert(m_instantiations==0); + } + + static int OpenDevices_wiilib(const int wiitimeout,const bool rumble,const bool dbg,const bool precheck=false) + { + FUNSTACK; + int n=0,retries=0; + while(n==0 && retries++<10) { + n=Wiilib().finddevices(wiitimeout,rumble,dbg); + if (precheck) break; + if (n==0) { + if (dbg) log << " OpenDevice()...delaying " << (retries<5 ? "250" : "1000") << " [ms]" << endl; + if (retries<5) Sleep(250); + else Sleep(1000); + } + } + if (dbg) {cerr.flush(); log << " Wiilib::OpenDevice..." << n << endl;} + return n; + } + + static int OpenDevices_wiimotelib(const int wiitimeout,const bool rumble,const bool dbg,const bool precheck=false) + { + FUNSTACK; + int n=0,retries=0; + while(n==0 && retries++<10) { + n=system("wiimotelibpoll")==0 ? 1 : 0; + if (precheck) break; + if (n==0) { + if (dbg) log << " OpenDevice()...delaying " << (retries<5 ? "250" : "1000") << " [ms]" << endl; + if (retries<5) Sleep(250); + else Sleep(1000); + } + } + if (dbg) {cerr.flush(); log << " Wiilib::OpenDevice..." << n << endl;} + return n; + } + +public: + + static int OpenDevices(const string& wiilib,const int wiitimeout,const bool rumble,const bool dbg,const bool precheck=false) + { + FUNSTACK; + if (wiilib=="wiiuse") return OpenDevices_wiilib(wiitimeout,rumble,dbg,precheck); + else if (wiilib=="wiimotelib") return OpenDevices_wiimotelib(wiitimeout,rumble,dbg,precheck); + else if (wiilib=="none") return -1; + else throw_("Wiilib::OpenDevices(), library must be one-of {wiiuse,wiimotelib,none}"); + return -1; // avoid compiler error + } + +}; + +int Wiilib::m_instantiations=0; + +BLUETOOTH_DEVICE_SEARCH_PARAMS Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(const int timeout) +{ + FUNSTACK; + BLUETOOTH_DEVICE_SEARCH_PARAMS b; + ZeroMemory(&b, sizeof(b)); + b.dwSize = sizeof(b); + b.fReturnAuthenticated = TRUE; + b.fReturnRemembered = TRUE; + b.fReturnUnknown = TRUE; + b.fReturnConnected = TRUE; + b.fIssueInquiry = TRUE; + + const int to=static_cast(1.0*timeout/1000.0/1.28+0.5); + assert(to>=0 && to<48 ); + b.cTimeoutMultiplier = (to==0 ? 1 : to); // timeout of 0 causes havac + b.hRadio = NULL; + + assert( b.cTimeoutMultiplier>0 ); + return b; +} + +BLUETOOTH_DEVICE_INFO Get_BLUETOOTH_DEVICE_INFO() +{ + FUNSTACK; + BLUETOOTH_DEVICE_INFO b; + ZeroMemory(&b, sizeof(b)); + b.dwSize = sizeof(b); + return b; +} + +BLUETOOTH_RADIO_INFO Get_BLUETOOTH_RADIO_INFO() +{ + FUNSTACK; + BLUETOOTH_RADIO_INFO b; + ZeroMemory(&b, sizeof(b)); + b.dwSize = sizeof(b); + return b; +} + +BLUETOOTH_FIND_RADIO_PARAMS Get_BLUETOOTH_FIND_RADIO_PARAMS() +{ + FUNSTACK; + BLUETOOTH_FIND_RADIO_PARAMS b; + ZeroMemory(&b, sizeof(b)); + b.dwSize = sizeof(b); + return b; +} + +bool ReachedMax(const int i) +{ + FUNSTACK; + if (i>MAX_DEVICE_COUNT) { + log << " ** warning: too many devices found, can only handle " << MAX_DEVICE_COUNT << " devices" << endl; + return true; + } + return false; +} + +bool RemoveDev(const BLUETOOTH_DEVICE_INFO& bdi) +{ + FUNSTACK; + DWORD status=BluetoothUpdateDeviceRecord(&bdi); + if (status!=ERROR_SUCCESS) throw_("BluetoothUpdateDeviceRecord() failed"); + + status=BluetoothRemoveDevice(&bdi.Address); + if (status==ERROR_SUCCESS) log << " removed device successfully..." << endl << "Done [OK]" << endl; + else log << " ** error: removing the device failed, was it registred at all?" << endl <<"Done [FAILED]" << endl; + + return status==ERROR_SUCCESS; +} + +HANDLE RadioInfo(const string& btr,const bool dbg) +{ + FUNSTACK; + if (dbg) log << " Radio info" << endl; + assert( btr.size()==17 || btr.size()==0 || btr=="all"); + + HANDLE hRadio=NULL; + BLUETOOTH_FIND_RADIO_PARAMS btfrp=Get_BLUETOOTH_FIND_RADIO_PARAMS(); + DeviceAutoClose hbf(BluetoothFindFirstRadio(&btfrp,&hRadio),&BluetoothFindRadioClose); + + if (hbf()==NULL) { + if (dbg) log << " " << endl; + return NULL; + } + + while(hbf()!=NULL){ + if (hbf()==NULL) throw_("BluetoothFindFirstRadio() failed"); + + BLUETOOTH_RADIO_INFO r=Get_BLUETOOTH_RADIO_INFO(); + if (BluetoothGetRadioInfo(hRadio,&r)!=ERROR_SUCCESS) throw_("BluetoothGetRadioInfo() failed"); + + const int c=BluetoothIsConnectable(hRadio); + const int d=BluetoothIsDiscoverable(hRadio); + + if (dbg){ + log << indent(tostring(r)," "); + log << " connectable: " << c << endl; + log << " discoverabel: " << d << endl; + } + + if (btr.size()==0) break; + else if (btr==ConvertAddress(r.address)){ + if (dbg) log << " found radio address match " << (btr.size()>0 ? ": " + btr : "") << endl; + break; + } + + if (!BluetoothFindNextRadio(&btfrp,&hRadio)) break; + } + + if (dbg) log << "Done [" << (hRadio==NULL ? "FAILED" : "OK") << "]" << endl; + return hRadio; +} + +bool ScanDevices(const int timeout) +{ + FUNSTACK; + log << "Scanning devices" << endl; + + vector infos; + + BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp=Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(timeout); + BLUETOOTH_DEVICE_INFO bdi=Get_BLUETOOTH_DEVICE_INFO(); + + DeviceAutoClose hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + const DWORD dwErr = GetLastError(); + + if (hbf()==NULL) { + log << " ** warning: no devices found" << endl << "Done [FAILED]" << endl; + return false;; + } + + if (hbf()!= NULL) { + log << "Looking for devices..." << endl; + + int i=0; + while(1){ + if (ReachedMax(i)) break; + + const string t=ConvertName(bdi.szName); + log << " found device [" << i << "]: <" << (t.size()>0 ? t : "NONAME") << ">" << endl; + + infos.push_back(bdi); + + if (BluetoothFindNextDevice(hbf(), &bdi) == FALSE) + break; + + ++i; + } + } + + log << "BTDevices info:" << endl; + if (infos.size()==0) log << " " << endl; + else for(size_t i=0;i0 ? s : "") << endl; + log << tostring(t); + } + log << "Done [OK]" << endl; + + return true; +} + +bool RemoveDevice(const string& d,const bool dbg) +{ + FUNSTACK; + log << "Removing device <" << d << ">" << endl; + + BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp=Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(1); // timout must be greater than zero, eventhough I only return remembered devices! + BLUETOOTH_DEVICE_INFO bdi=Get_BLUETOOTH_DEVICE_INFO(); + + bdsp.fReturnAuthenticated = FALSE; + bdsp.fReturnRemembered = TRUE; + bdsp.fReturnUnknown = FALSE; + bdsp.fReturnConnected = TRUE; + + DeviceAutoClose hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + + const DWORD dwErr = GetLastError(); + + if (hbf()==NULL) { + log << " ** error: failed to find device" << endl << "Done [FAILED]" << endl; + return false; + } + + int i=0; + while(true){ + if (ReachedMax(i)) break; + const string fd=ConvertName(bdi.szName); + log << " found device [" << i << "]: <" << (fd.size()>0 ? fd : "NONAME") << ">" << endl; + + if (fd==d){ + log << " found match..." << endl; + log << " trying to remove..." << endl; + + return RemoveDev(bdi); + } + if (BluetoothFindNextDevice(hbf(), &bdi) == FALSE) break; + ++i; + } + log << " ** error: failed to find device" << endl << "Done [FAILED]" << endl; + return false; +} + +bool MatchAdr(const string& pattern,const string& adr) +{ + assert(pattern.size()==17 && adr.size()==17); + for(size_t i=0;i<17;++i){ + if (!(pattern[i]=='?' || pattern[i]==adr[i])) return false; + } + return true; +} + +bool MatchDevice(const string& d,const vector& known_adr,const BLUETOOTH_DEVICE_INFO& bdi,const bool dbg) +{ + FUNSTACK; + + const string found_adr=ConvertAddress(bdi.Address); + bool wildcard=false; + for(size_t i=0;i(p); + assert(p!=0 && q->done==false && q->timeout>0 && q->hbf==0 ); + + BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp=Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(q->timeout); + + q->hbf=BluetoothFindFirstDevice(&bdsp,&q->bdi); + const DWORD status=GetLastError(); + + if (status==ERROR_INVALID_PARAMETER) throw_("BluetoothFindFirstDevice(), returned status=ERROR_INVALID_PARAMETER"); + else if (status==ERROR_REVISION_MISMATCH) throw_("BluetoothFindFirstDevice(), returned status=ERROR_REVISION_MISMATCH"); + + q->done=true; + return 0; +} + +bool OpenDevice(const string& d,const string& btr,const int timeout,const int wiitimeout,const vector& known_adr,const int btsleep,const string& lib,const bool precheckwiimotes,const bool rumble,const bool dbg,bool& invalidargflag) +{ + FUNSTACK; + assert( invalidargflag==false ); + log << "Open device <" << d << ">" << endl; + + if (precheckwiimotes && Wiilib::OpenDevices(lib,wiitimeout,false,dbg,true)>=1) { + log << " service is already opened..." << endl << "Done [OK]" << endl; + return true; + } + + if (dbg && known_adr.size()>0) { + log << " known addresses="; + for(size_t i=0;i hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + // + // FIX: start the BluetoothFindFirstDevice() function in a thread, terminate the thread if it has run for longer than, say 2+timeout. + // + // Old code: + // BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp=Get_BLUETOOTH_DEVICE_SEARCH_PARAMS(timeout); + // BLUETOOTH_DEVICE_INFO bdi=Get_BLUETOOTH_DEVICE_INFO(); + // DeviceAutoClose hbf(BluetoothFindFirstDevice(&bdsp,&bdi),&BluetoothFindDeviceClose); + // DWORD status=GetLastError(); + + unsigned long tid=0; + DeviceAutoClose h(CreateThread(0,0,BluetoothFindFirstDevice_thread,&p,0,&tid),CloseHandle); + timer tthread; + + Sleep(timeout); + while(!p.done && tthread.elapsed()*1000<2*timeout) Sleep(20); // or use WaitForMultipleObjects(...); + } + + if(!p.done){ + log << " ** error: BluetoothFindFirstDevice() stalled" << endl<< "Done [FAILED]" << endl; + return false; + } + else if(p.hbf==0){ + log << " ** error: BluetoothFindFirstDevice() returned null hbf" << endl<< "Done [FAILED]" << endl; + return false; + } + + assert(p.hbf!=0); + BLUETOOTH_DEVICE_INFO bdi=p.bdi; + DeviceAutoClose hbf(p.hbf,&BluetoothFindDeviceClose); + + if (hbf()==NULL) { + log << " ** error: no devices found" << endl << "Done [FAILED]" << endl; + return false; + } + + int i=0; + while(true){ + if (ReachedMax(i)) break; + const string fd=ConvertName(bdi.szName); + log << " found device [" << i << "]: <" << (fd.size()>0 ? fd : "NONAME") << ">" << endl; + + if (MatchDevice(d,known_adr,bdi,dbg)){ + log << " found match..." << endl; + if (dbg) log << tostring(bdi); + + // write binary dbi, test code for now + //ofstream ox("bdi.out",ios::binary); + //ox.write(reinterpret_cast(&bdi),sizeof(bdi)); + + log << " trying to open..." << endl; + DWORD status=BluetoothUpdateDeviceRecord(&bdi); + if (status!=ERROR_SUCCESS) throw_("BluetoothUpdateDeviceRecord() failed"); + if (dbg) log << " BluetoothUpdateDeviceRecord()...OK" << endl; + + { + DeviceAutoClose hRadio(RadioInfo(btr,dbg),&CloseHandle); + if (hRadio()==NULL) throw_("failed to get radio"); + if (dbg) log << " RadioInfo()...OK" << endl; + + const GUID service=HumanInterfaceDeviceServiceClass_UUID; + status=BluetoothSetServiceState(hRadio(),&bdi,&service,BLUETOOTH_SERVICE_ENABLE); + + if (dbg && status==ERROR_SUCCESS) log << " BluetoothSetServiceState()...OK" << endl; + if (dbg && status!=ERROR_SUCCESS) log << " BluetoothSetServiceState()...ERROR" << endl; + } + + if (status!=ERROR_SUCCESS) { + string t; + if (status==ERROR_INVALID_PARAMETER) t="ERROR_INVALID_PARAMETER"; + else if (status==ERROR_SERVICE_DOES_NOT_EXIST) t="ERROR_SERVICE_DOES_NOT_EXIST"; + else if (status==E_INVALIDARG) t="E_INVALIDARG"; + // if (status!=E_INVALIDARG) throw_("BluetoothSetServiceState() failed with errorcode " + t); + // else log << " ** error: BluetoothSetServiceState() returned " << t << endl; + + if (status==E_INVALIDARG) invalidargflag=true; + log << " ** error: BluetoothSetServiceState() returned " << t << endl; + log << endl << "Done [FAILED]" << endl; + return false; + } + + // sleep while windows slowly does strange things + Sleep(btsleep); + + if (lib!="none") + { + if (Wiilib::OpenDevices(lib,wiitimeout,rumble,dbg)==0) { + log << " ** error: service could not be opened..." << endl<< "Done [FAILED]" << endl; + return false; + } + } + + log << " service on device enabled..." << endl << " opended device successfully..." << endl << "Done [OK]" << endl; + return true; + } + + if (BluetoothFindNextDevice(hbf(), &bdi) == FALSE) break; + ++i; + } + log << " ** error: device not mathced" << endl<< "Done [FAILED]" << endl; + return false; +} + +bool AutoOpenDevice(const string& d,const string& btr,const int timeout,const int wiitimeout,const int usbsleep,const int btsleep,const int usbmsleep,const string& cf,const string& lib,const int removemode,const int usbmode,const bool wb,const int scanretries,const bool dbg,bool& invalidargflag) +{ + FUNSTACK; + + int cl=0; + if (g_automode_callback==0) g_automode_callback=&DummyCallback; + + if (usbmode==2 && ScanforUSBmicrodevs()!=1) { + log << "Could not find any USBm devices" << endl << "Wiiscan done [FAILED]" << endl; + return false; + } + if (usbmode==3 && ScanforDelcomdevs()!=1) { + log << "Could not find any Delcom devices" << endl << "Wiiscan done [FAILED]" << endl; + return false; + } + + g_automode_callback(cl++); + + timer total; + assert(usbsleep>=0 && btsleep>=0 && usbmsleep>=0); + + log << "Auto-connecting to device <" << d << ">" << endl; + + // check valid config file + if (!FileExists(cf)) throw_("config file <" + cf + "> is missing"); + const Configfile c(cf); + if (dbg) log << c; + if (!c.hasEntry("all_usb_hubs")) throw_("config file missing entry"); + if (!c.hasEntry("active_usb_hub")) throw_("config file missing entry"); + if (!c.hasEntry("allowed_wiimote_adr")) throw_("config file missing entry"); + if (!c.hasEntry("whiteboard_software")) throw_("config file missing entry"); + + // test if already opened + g_automode_callback(cl++); + + if (removemode==0 && Wiilib::OpenDevices(lib,wiitimeout,false,dbg,true)>=1) { + log << " service is already opened..." << endl << "Done [OK]" << endl; + + if (wb){ + // fireup whiteboard software + if (!c.hasEntry("whiteboard_software")) log << " ** warning: config file is missing entry " << endl; + else System(c.Get("whiteboard_software",true),false,false); + } + + return true; + } + + // power down now if using USB io board + g_automode_callback(cl++); + USBupdown(usbmode,false,dbg); + timer t; + + // remove old entries in HID + g_automode_callback(cl++); + if(removemode!=2) RemoveDevice(d,dbg); + + // Cycle usb hub, turn off usb power and restart the wiimote + g_automode_callback(cl++); + if(usbmode==1){ + const string usbhub=c.Get("active_usb_hub"); + if(usbhub!="\"\""){ + USBupdown(usbmode,false,dbg,usbhub); + Sleep(usbsleep); + USBupdown(usbmode,true,dbg,usbhub); + + g_automode_callback(cl++); + + // wait for usb hub to be up, takes some seconds (1000ms to 2000ms approx) + Sleep(1800); + + // wait for usb radio to be ready + g_automode_callback(cl++); + HANDLE hRadio=RadioInfo(btr,dbg); + + int i=0; + while(hRadio==NULL) { + if (i++==0) log << " radio not ready, delaying."; + else log << "."; + Sleep(500); + hRadio=RadioInfo(btr,dbg); + if (i>10) throw_("could not connect to bluetooth radio device"); + } + CloseHandle(hRadio); + if (i>0) log << endl; + } + } else if (usbmode!=0) { + while (t.elapsed()*1000=1 ); + while(!openok && ++retries<=scanretries){ + openok=OpenDevice(d,btr,min(retries*timeout,8000),wiitimeout,c("allowed_wiimote_adr"),btsleep,lib,false,false,dbg,invalidargflag); + } + + if (openok){ + g_automode_callback(cl++); + if (wb){ + g_automode_callback(cl++); + // terminate running wb's + + // fireup whiteboard software + if (!c.hasEntry("whiteboard_software")) log << " ** warning: config file is missing entry " << endl; + else System(c.Get("whiteboard_software",true),false,false); + } + } + if (dbg) log << " Elapsed time: " << total.elapsed() << " [s]" << endl; + + // taking longer than 20 sec will bring up an annoying windows reboot dialog, + // that can be ignored, hence warn at 18 sec + if (total.elapsed()>18) log << " ** warning: connection took a long time to finish, this may cause a windows reboot dialog, that can be ignored" << endl; + + if (!openok) log << " ** warning: could not open device" << endl << "Done [FAILED]" << endl; + + assert(cl<10); + g_automode_callback(42); + return openok; +} + +int Usage(const args& arg,const string msg="") +{ + FUNSTACK; + if (msg.size()>0) log << msg << endl; + log << "Usage: " << arg[0] << " <-a | -c | -d | -r | -s | -usbup | -usbdown> [-cf ] [-lf ] [-b ] [-t ] [-u ] [-p ] [-w ] [-q ] [-f ] [-m ] [-l ] [-y] [-wb] [-v] " << endl; + log << " " << Version() << " " << Config() << endl; + log << " modes:" << endl; + log << " -a : autoconnect to device" << endl; + log << " -c : connect the device, that matches this name" << endl; + log << " -d : deletes the device, that matches this name" << endl; + log << " -r: lookup and list bluetooth radio devices" << endl; + log << " -s: scan external bluetooth devices" << endl; + log << " -usbdown: disable usb hubs" << endl; + log << " -usbup: enable usb hubs" << endl; + log << " options:" << endl; + log << " -cf : specify a distinct configurationfile, default=" << DEFAULT_configfile << endl; + log << " -lf : specify a distinct logfile, default=" << DEFAULT_logfile << endl; + log << " -b : automode bluetooth connection sleep in milliseconds, default=" << DEFAULT_btsleep << endl; + log << " -t : timeout for bluetooth stack in milliseconds, default=" << DEFAULT_timeout << endl; + log << " -u : automode usb connection sleep in milliseconds, default=" << DEFAULT_usbsleep << endl; + log << " -p : automode usbm post-connection sleep in milliseconds, default=" << DEFAULT_usbmsleep << endl; + log << " -w : timeout for wiimote in milliseconds, default=" << DEFAULT_wiitimeout << endl; + log << " -q : use bluetooth radio with this address (not working), default=any device" << endl; + log << " -f : pre-remove mode of device, 0=remove if not connectable, 1=always remove, 2=never remove, default=" << DEFAULT_removemode << endl; + log << " -m : choose USB powercycle mode, 0=no power cycle, 1=use USB hub, 2=use USBm IO hardware, 3=use USB Delcon IO hardware" << endl; + log << " -l : use specific wiimote library, lib can be one-of {wiiuse,wiimotelib, default=" << DEFAULT_wiilib << endl; + log << " -y : scan retries in automode, default=" << DEFAULT_scanretries<< endl; + log << " -wb: start whiteboard in automode" << endl; + log << " -nowb: do not start whiteboard in automode" << endl; + log << " -v: enable extra debugging printouts" << endl; + log << " default mode: -a \"" << NINTENDO_DEV << "\"" << endl; + log << " note: \"nintendo\" is a shortcut for \"" << NINTENDO_DEV << "\"" << endl; + return -1; +} + + +//#include +#include +#include + +int DoConnect() +{ + //BLUETOOTH_DEVICE_INFO device; + //device=*(BLUETOOTH_DEVICE_INFO*)lpParam; + + BLUETOOTH_DEVICE_INFO device=Get_BLUETOOTH_DEVICE_INFO(); + ifstream ix("bdi.out",ios::binary); + ix.read(reinterpret_cast(&device),sizeof(device)); + + string info; + //SOCKET s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_L2CAP); + SOCKET s = socket(AF_BTH, SOCK_SEQPACKET , BTHPROTO_L2CAP); + + if (s == INVALID_SOCKET) + { + MessageBox(NULL,"","Invalid Socket",MB_OK); + exit(1); + } + + SOCKADDR_BTH bthSockAddr; + + bthSockAddr.addressFamily=AF_BTH; + bthSockAddr.btAddr=device.Address.ullLong; + bthSockAddr.port=0x11; //BT_PORT_ANY; + //bthSockAddr.serviceClassId=SerialPortServiceClass_UUID; + bthSockAddr.serviceClassId=HumanInterfaceDeviceServiceClass_UUID; + + if(0!=connect(s,(sockaddr*)&bthSockAddr,sizeof(bthSockAddr))) + MessageBox(NULL,"","Connect",MB_OK); + else + { + info="Connected to: "; + //info+=device.szName; + MessageBox(NULL,info.c_str(),"Connect",MB_OK); + } + + return 0; +} + +BOOL RegFun(LPVOID p,PBLUETOOTH_DEVICE_INFO pDevice) +{ + cout << "\n## RegFun called backed..."; + return 0; +} + +int TestOpen(const string& d,const string& btr,const bool dbg) +{ + +//Connect_using_BT_Api(); +//return 0; + + int status; + + USBupdown(3,true,dbg); + //Sleep(1000); + //return DoConnect();½ + +// USBupdown(3,false,dbg); + + BLUETOOTH_DEVICE_INFO bdi=Get_BLUETOOTH_DEVICE_INFO(); + ifstream ix("bdi.out",ios::binary); + ix.read(reinterpret_cast(&bdi),sizeof(bdi)); + + DeviceAutoClose hRadio(RadioInfo(btr,dbg),&CloseHandle); + if (hRadio()==NULL) throw_("failed to get radio"); + if (dbg) log << " RadioInfo()...OK" << endl; + + +BluetoothEnableIncomingConnections(hRadio(),1); +/* +for(int ii=0;ii<6;++ii) bdi.Address.rgBytes[ii]=0; + + HBLUETOOTH_AUTHENTICATION_REGISTRATION reg; + status=BluetoothRegisterForAuthentication(&bdi,®,RegFun,0); +*/ + + + Sleep(500); + USBupdown(3,true,dbg); + Sleep(1000); + + // Lenovo BT adr= 00:50:B6:A0:48:8C + + WCHAR pass[256]; + pass[5]=0x00; // 0x00 = 0 + pass[4]=0x50; + pass[3]=0xB6; + pass[2]=0xA0; + pass[1]=0x48; + pass[0]=0x8C; + + pass[6]=0; + + for(int i=0;i<6;++i) cout << dec << " [" << i << "]=" << hex << pass[i]; + + status=BluetoothAuthenticateDevice(NULL,hRadio(),&bdi,pass,6); + //status=BluetoothAuthenticateDevice(NULL,hRadio(),&bdi,0,0); + + // status=BluetoothAuthenticateDeviceEx(); // only found on Vista + + if (status==ERROR_SUCCESS) cout << "** OK: BluetoothAuthenticateDevice"; + else cout << "** ERROR: BluetoothAuthenticateDevice, " << status; + + if (status==ERROR_INVALID_PARAMETER) cout << "(ERROR_INVALID_PARAMETER)"; + if (status==ERROR_NO_MORE_ITEMS) cout << "(BTH_ERROR_NO_CONNECTION)"; + + + if (status==BTH_ERROR_AUTHENTICATION_FAILURE) cout << "(BTH_ERROR_AUTHENTICATION_FAILURE)"; + if (status==BTH_ERROR_NO_CONNECTION) cout << "(BTH_ERROR_NO_CONNECTION)"; + if (status==BTH_ERROR_PAGE_TIMEOUT ) cout << "(BTH_ERROR_PAGE_TIMEOUT )"; + if (status==BTH_ERROR_HARDWARE_FAILURE ) cout << "(BTH_ERROR_HARDWARE_FAILURE )"; + if (status==BTH_ERROR_AUTHENTICATION_FAILURE) cout << "(BTH_ERROR_AUTHENTICATION_FAILURE)"; + if (status==BTH_ERROR_MEMORY_FULL ) cout << "(BTH_ERROR_MEMORY_FULL )"; + if (status==BTH_ERROR_CONNECTION_TIMEOUT ) cout << "(BTH_ERROR_CONNECTION_TIMEOUT )"; + if (status==BTH_ERROR_LMP_RESPONSE_TIMEOUT) cout << "(BTH_ERROR_LMP_RESPONSE_TIMEOUT)"; + if (status==BTH_ERROR_MAX_NUMBER_OF_CONNECTIONS ) cout << "(BTH_ERROR_MAX_NUMBER_OF_CONNECTIONS)"; + if (status==BTH_ERROR_PAIRING_NOT_ALLOWED ) cout << "(BTH_ERROR_PAIRING_NOT_ALLOWED )"; + if (status==BTH_ERROR_UNSPECIFIED_ERROR ) cout << "(BTH_ERROR_UNSPECIFIED_ERROR)"; + if (status==BTH_ERROR_LOCAL_HOST_TERMINATED_CONNECTION ) cout << "(BTH_ERROR_LOCAL_HOST_TERMINATED_CONNECTION)½"; + + if (status==ERROR_SUCCESS) { + cout << "Running wb...\n"; + int n= system("D:\\wiiscan-0.9-release-2.0\\bin\\Release\\WiimoteWhiteboard.exe"); + cout << " returned n=" << n << "\n"; + } + +/* + const GUID service=HumanInterfaceDeviceServiceClass_UUID; + + status=BluetoothSetServiceState(hRadio(),&bdi,&service,BLUETOOTH_SERVICE_ENABLE); + if (dbg && status==ERROR_SUCCESS) log << " BluetoothSetServiceState(0)...OK" << endl; + else if (dbg && status==E_INVALIDARG) log << " BluetoothSetServiceState(0)...E_INVALIDARG" << endl; + else if (dbg && status!=ERROR_SUCCESS) log << " BluetoothSetServiceState(0)...ERROR" << endl; + +*/ + cout << "\n"; + Sleep(100000); + +/* + //RemoveDevice(d,dbg); + Sleep(2000); + USBupdown(3,true,dbg); + Sleep(3000); + + //return Wiilib::OpenDevices("wiiuse",4000,false,true,false); + + WCHAR pass[]=L"1234"; + return BluetoothAuthenticateDevice(NULL,hRadio(),&bdi,pass,4); + + if (dbg) log << tostring(bdi); + + status=BluetoothSetServiceState(hRadio(),&bdi,&service,BLUETOOTH_SERVICE_ENABLE); + if (dbg && status==ERROR_SUCCESS) log << " BluetoothSetServiceState(1)...OK" << endl; + else if (dbg && status==E_INVALIDARG) log << " BluetoothSetServiceState(1)...E_INVALIDARG" << endl; + else if (dbg && status!=ERROR_SUCCESS) log << " BluetoothSetServiceState(1)...ERROR" << endl; +*/ + + return 1; +} + + +int main(int argc,char** argv) +{ + FUNSTACK; + bool invalidargflag=false; + try{ + SetNiceLevel(-15); // set to time-critical + args arg(argc,argv); + + // first, parse the command line configfile, then load the config, finally override any configfile options with given command line options + string cf=arg.parseval("-cf",DEFAULT_configfile); + + bool v=false,wb=false; + int timeout =DEFAULT_timeout; + int wiitimeout=DEFAULT_wiitimeout; + int usbsleep =DEFAULT_usbsleep; + int usbmsleep =DEFAULT_usbmsleep; + int btsleep =DEFAULT_btsleep; + int usbmode =DEFAULT_usbmode; + int removemode=DEFAULT_removemode; + int scanretries=DEFAULT_scanretries; + string a,c,d,btr,lf=DEFAULT_logfile,dev=NINTENDO_DEV,lib=DEFAULT_wiilib; + + if (FileExists(cf)){ + const Configfile cnf(cf); + if (cnf.hasEntry("option_device")) dev=strip(cnf.Get("option_device"),'"'); + if (cnf.hasEntry("option_timeout")) timeout=cnf.Get("option_timeout"); + if (cnf.hasEntry("option_wiitimeout")) wiitimeout=cnf.Get("option_wiitimeout"); + if (cnf.hasEntry("option_usbsleep")) usbsleep=cnf.Get("option_usbsleep"); + if (cnf.hasEntry("option_usbmsleep")) usbmsleep=cnf.Get("option_usbmsleep"); + if (cnf.hasEntry("option_btsleep")) btsleep=cnf.Get("option_btsleep"); + if (cnf.hasEntry("option_usbpowermode")) usbmode=cnf.Get("option_usbpowermode"); + if (cnf.hasEntry("option_removemode")) removemode=cnf.Get("option_removemode"); + if (cnf.hasEntry("option_debug")) v=cnf.Get("option_debug"); + if (cnf.hasEntry("option_startwhiteboard")) wb=cnf.Get("option_startwhiteboard"); + if (cnf.hasEntry("option_logfile")) lf=cnf.Get("option_logfile"); + if (cnf.hasEntry("option_btradio")) btr=cnf.Get("option_btradio"); + if (cnf.hasEntry("option_wiilib")) lib=cnf.Get("option_wiilib"); + if (cnf.hasEntry("option_scanretries"))scanretries=cnf.Get("option_scanretries"); + } + + // parse argumets via args class + const bool r=arg.parseopt("-r"); + const bool s=arg.parseopt("-s"); + const bool u=arg.parseopt("-?"); + const bool usbdown=arg.parseopt("-usbdown"); + const bool usbup=arg.parseopt("-usbup"); + v=arg.parseopt("-v") || v; + wb=arg.parseopt("-wb") || wb; + wb=!arg.parseopt("-nowb") && wb; + timeout =arg.parseval("-t",timeout); + wiitimeout=arg.parseval("-w",wiitimeout); + usbsleep =arg.parseval("-u",usbsleep); + usbmsleep =arg.parseval("-p",usbmsleep); + btsleep =arg.parseval("-b",btsleep); + usbmode =arg.parseval("-m",usbmode); + removemode=arg.parseval("-f",removemode); + a=arg.parseval("-a",""); + c=arg.parseval("-c",""); + d=arg.parseval("-d",""); + lf=arg.parseval("-lf",lf); + btr=arg.parseval("-q",btr); + lib=arg.parseval("-l",lib); + scanretries=arg.parseval("-y",scanretries); + + if (arg.size()!=1) return Usage(arg); + + // apply shortcuts + if (a=="nintendo") a=dev; + if (c=="nintendo") c=dev; + if (d=="nintendo") d=dev; + + // open log file before Usage call + if(lf!=DEFAULT_logfile){ + lf=strip(lf,'"'); // cannot handle filenames in citation quotes + if (lf=="cout" || lf=="std::cout") g_log=&cout; + else if (lf=="cerr" || lf=="std::cerr") g_log=&cerr; + else { + g_log.open(lf,ios_base::app); + g_log.writelogheader("Wiiscan::main()"); + if (v) log << "Using logfile <" << lf << ">" << endl; + } + } + + const int check=(r ? 1 : 0)+ (s ? 1 : 0) + (a.size()>0 ? 1 : 0) + (c.size()>0 ? 1 : 0) + (d.size()>0 ? 1 : 0) + usbup + usbdown; + if (check>1 || u) return Usage(arg); + + if (timeout<20) throw_("bad value of timeout, should be >=20"); + if (wiitimeout<20)throw_("bad value of wiitimeout, should be >=20"); + if (usbsleep<20) throw_("bad value of usbsleep, should be >=20"); + if (usbmsleep<20) throw_("bad value of usbmsleep, should be >=20"); + if (btsleep<20) throw_("bad value of btsleep, should be >=20"); + if (usbdown && usbup) throw_("bad value of usbdown/up, cannot be used at the same time"); + if (wiitimeout%1000!=0) throw_("bad value, wiitimeout should be divisable with 1000"); + if (btr.size()>0 && btr.size()!=17) throw_("bluetooth radio must be in the form xx:xx:xx:xx:xx:xx"); + if (usbmode<0 || usbmode>3) throw_("bad usbpowermode, must be 0,1,2, or 3"); + if (removemode<0 || removemode>2) throw_("bad removemode, must be 0,1 or 2"); + if (scanretries<=0) throw_("bad value of scanretries, should be > 0"); + if (a.size()==0) a=NINTENDO_DEV; + + if (v) { + log << Version() << " " << Config() << endl; + log << "Values:" << endl; + log << " a=" << a << endl; + log << " c=" << c << endl; + log << " d=" << d << endl; + log << " r=" << r << endl; + log << " s=" << s << endl; + log << " v=" << v << endl; + log << " cf=" << cf << endl; + log << " lf=" << lf << endl; + log << " wb=" << wb << endl; + log << " btr=" << btr << endl; + log << " lib=" << lib << endl; + log << " usbmode=" << usbmode << endl; + log << " removemode=" << removemode << endl; + log << " scanretries=" << scanretries << endl; + log << " usbup =" << usbup << endl; + log << " usbdown=" << usbup << endl; + log << "Timeouts:" << endl; + log << " timeout =" << timeout << endl; + log << " wiitimeout=" << wiitimeout << endl; + log << " usbsleep =" << usbsleep << endl; + log << " usbmsleep =" << usbmsleep << endl; + log << " btsleep =" << btsleep << endl; + } + + bool ret=true; + +return TestOpen(NINTENDO_DEV,btr,true); + + if (r) ret=RadioInfo (btr.size()==0 ? "all" : btr,v)!=NULL; + else if (s) ret=ScanDevices (timeout); + else if (c.size()>0) ret=OpenDevice (c,btr,timeout,wiitimeout,vector(),btsleep,lib,true,true,v,invalidargflag); + else if (d.size()>0) ret=RemoveDevice(d,v); + else if (usbup) ret=USBupdown (usbmode,true, v,Configfile(cf).Get("active_usb_hub",true)); + else if (usbdown) ret=USBupdown (usbmode,false,v,Configfile(cf).Get("active_usb_hub",true)); + else ret=AutoOpenDevice(a,btr,timeout,wiitimeout,usbsleep,btsleep,usbmsleep,cf,lib,removemode,usbmode,wb,scanretries,v,invalidargflag); // default mode + + log << "Wiiscan done " << (ret ? "[OK]" : "[FAILED]") << endl; + + if (invalidargflag) { + if (v) log << "** warning: return=-3 (E_INVALIDARG)" << endl; + return -3; + } + + if (v) log << "returning: return=" << (ret ? "0" : "-1") << endl; + return ret ? 0 : -1; // 0=Ok, -1=fail, -2=fail with exception, -3=fail with E_INVALIDARG + } + CATCH_ALL; + + assert(invalidargflag==false); // flag not setted => no throw in when E_INVALIDARG encoutered + log << "Wiiscan done [FAILED]" << endl; + return -2; +} + +}; // namespace Wiiscan + + +const int MAX_RADIOS=1; +const int MAX_DEVICES=16; +const int MAX_MAC_ADDR_LENGHT=256; +const int MAX_STRING_LENGHT=1024; + +string GetLastErrorMessage(const int n) {return "error n";} + +// PFN_DEVICE_CALLBACK + +BOOL WINAPI RegFun2(LPVOID pvParam,const BLUETOOTH_DEVICE_INFO* pDevice) +{ + cout << "\n**RegFun2**\n"; + return 1; +} + +int BT_radio_find_radios(HANDLE hRadio[MAX_RADIOS]) +{ +int i = 0; +HBLUETOOTH_RADIO_FIND hFind; +BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(btfrp) }; + +hFind = BluetoothFindFirstRadio( &btfrp, &(hRadio[i]) ); +if ( NULL != hFind ) +{ +//printf("found first radio\n"); +i++; +while(TRUE == BluetoothFindNextRadio(hFind, &hRadio[i])) +{ +i++; +if (i >= MAX_RADIOS) +{ +printf("Cannot assign next radio, MAX_RADIOS = %d reached\n", MAX_RADIOS); +return MAX_RADIOS; +} +} +if (FALSE == BluetoothFindRadioClose(hFind)) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +} +else +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +return i; +} + +int BT_radio_find_devices(unsigned char address_table[MAX_DEVICES][MAX_MAC_ADDR_LENGHT], +char nameAsString_table[MAX_DEVICES][MAX_STRING_LENGHT], +HANDLE* hRadio) +{ +BLUETOOTH_DEVICE_INFO deviceInfo; +deviceInfo.dwSize = sizeof(deviceInfo); +HBLUETOOTH_DEVICE_FIND hDeviceFind; + +BLUETOOTH_DEVICE_SEARCH_PARAMS deviceSearchParams; +memset(&deviceSearchParams, 0, sizeof(deviceSearchParams)); +deviceSearchParams.dwSize = sizeof(deviceSearchParams); +deviceSearchParams.fReturnAuthenticated = true; +deviceSearchParams.fReturnRemembered = true; +deviceSearchParams.fReturnUnknown = true; +deviceSearchParams.fReturnConnected = true; +deviceSearchParams.fIssueInquiry = true; +deviceSearchParams.cTimeoutMultiplier = 10; +deviceSearchParams.hRadio = *hRadio; +int i = 0; + +hDeviceFind = BluetoothFindFirstDevice(&deviceSearchParams, &deviceInfo); +if (NULL != hDeviceFind) +{ +do +{ +memcpy(address_table[i], &deviceInfo.Address.rgBytes[0], MAX_MAC_ADDR_LENGHT); +memcpy(&nameAsString_table[i], &deviceInfo.szName, MAX_STRING_LENGHT); +//wprintf(L"Device: %s\n", deviceInfo.szName); +//BluetoothDisplayDeviceProperties(0, &deviceInfo); +i++; +if (i >= MAX_DEVICES) +{ +printf("Cannot assign next device, MAX_DEVICES = %d reached\n", MAX_DEVICES); +return MAX_DEVICES; +} +} +while(BluetoothFindNextDevice(hDeviceFind, &deviceInfo)); + +if (FALSE == BluetoothFindDeviceClose(hDeviceFind)) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +} +else +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +return i; +} + +void Connect_using_BT_Api() +{ +int i, j, result; +HANDLE hRadio[MAX_RADIOS]; +int found_radio_cnt = 0; +int selected_radio; +int found_radio_dev_cnt = 0; +int selected_radio_dev; + +BLUETOOTH_RADIO_INFO radioInfo; +radioInfo.dwSize = sizeof(radioInfo); +BLUETOOTH_DEVICE_INFO deviceInfo; +memset(&deviceInfo, 0, sizeof(deviceInfo)); +deviceInfo.dwSize = sizeof(deviceInfo); + +unsigned char radio_dev_address[MAX_DEVICES][MAX_MAC_ADDR_LENGHT]; +char radio_dev_names [MAX_DEVICES][MAX_STRING_LENGHT]; + +found_radio_cnt = BT_radio_find_radios(hRadio); +if (found_radio_cnt == 0) +{ +printf("no radios found\n"); +exit(1); +} +for (i = 0; i < found_radio_cnt; i++) +{ +if (ERROR_SUCCESS == BluetoothGetRadioInfo(hRadio[0], &radioInfo)) +{ +wprintf(L"Radio[%2d]: %10s\n", i, radioInfo.szName); +} +} +printf("Select radio: "); +//scanf("%d", &selected_radio); +selected_radio=0; + +if (selected_radio < 0) selected_radio = 0; +if (selected_radio >= found_radio_cnt) selected_radio = found_radio_cnt - 1; + +if (ERROR_SUCCESS == BluetoothGetRadioInfo(hRadio[selected_radio], &radioInfo)) +{ +wprintf(L"selected: %10s\n", radioInfo.szName); +} + +if (FALSE == BluetoothEnableDiscovery(hRadio[selected_radio], TRUE)) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +if (FALSE == BluetoothEnableIncomingConnections(hRadio[selected_radio], TRUE)) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} + +found_radio_dev_cnt = BT_radio_find_devices(radio_dev_address, radio_dev_names, &hRadio[selected_radio]); +if (found_radio_dev_cnt == 0) +{ +printf("no radio devices found\n"); +exit(1); +} + +for (i = 0; i < found_radio_dev_cnt; i++) +{ +wprintf(L"Radio device[%2d]: %20s ", i, radio_dev_names[i]); +printf("addr: (%2x:%2x:%2x:%2x:%2x:%2x)\n", +radio_dev_address[i][5], +radio_dev_address[i][4], +radio_dev_address[i][3], +radio_dev_address[i][2], +radio_dev_address[i][1], +radio_dev_address[i][0]); +} + +printf("Select device: "); +scanf("%d", &selected_radio_dev); +if (selected_radio_dev < 0) selected_radio_dev = 0; +if (selected_radio_dev >= found_radio_dev_cnt) selected_radio_dev = found_radio_dev_cnt - 1; + +#if 1 //function works, but not needed +BLUETOOTH_SELECT_DEVICE_PARAMS seldevquery; +memset(&seldevquery, 0, sizeof(seldevquery)); +seldevquery.dwSize = sizeof(seldevquery); +seldevquery.cNumOfClasses = 5; +seldevquery.hwndParent = 0; +seldevquery.fForceAuthentication = true; +seldevquery.fShowAuthenticated = true; +seldevquery.fShowRemembered = true; +seldevquery.fShowUnknown = true; +seldevquery.fAddNewDeviceWizard = false; +seldevquery.fSkipServicesPage = false; +seldevquery.pfnDeviceCallback = RegFun2, //dev_callback; +seldevquery.pvParam = 0; +seldevquery.cNumDevices = 2; +seldevquery.pDevices = &deviceInfo; + +if (FALSE == (&seldevquery)) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +else +{ +printf("poszlo: BluetoothSelectDevices\n"); +wprintf(L"Selected device: %20s ", deviceInfo.szName); +printf("addr: (%2x:%2x:%2x:%2x:%2x:%2x)\n", +deviceInfo.Address.rgBytes[5], +deviceInfo.Address.rgBytes[4], +deviceInfo.Address.rgBytes[3], +deviceInfo.Address.rgBytes[2], +deviceInfo.Address.rgBytes[1], +deviceInfo.Address.rgBytes[0]); +} +#endif + +#if 1 +memcpy(&deviceInfo.Address.rgBytes[0], radio_dev_address[selected_radio_dev], MAX_MAC_ADDR_LENGHT); +if (ERROR_SUCCESS == BluetoothGetDeviceInfo(hRadio[selected_radio], &deviceInfo)) +{ +wprintf(L"Selected device: %20s ", deviceInfo.szName); +printf("addr: (%2x:%2x:%2x:%2x:%2x:%2x)\n", +deviceInfo.Address.rgBytes[5], +deviceInfo.Address.rgBytes[4], +deviceInfo.Address.rgBytes[3], +deviceInfo.Address.rgBytes[2], +deviceInfo.Address.rgBytes[1], +deviceInfo.Address.rgBytes[0]); + +// BluetoothUpdateDeviceRecord(&deviceInfo); +// BluetoothRemoveDevice(deviceInfo.Address); +} +else +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +#endif + +//GUID pService = {0x00001101, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; +GUID pService = (GUID)SerialPortServiceClass_UUID; +//GUID pService = (GUID)OBEXFileTransferServiceClass_UUID; +//GUID pService = (GUID)OBEXObjectPushServiceClass_UUID; + +if (ERROR_SUCCESS != BluetoothSetServiceState(hRadio[selected_radio], &deviceInfo, &pService, BLUETOOTH_SERVICE_ENABLE)) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +else +{ +printf("BluetoothSetServiceState enabled (COM)\n"); +} + +#if 1 +GUID guidServices[MAX_DEVICES]; +memset(guidServices, 0, sizeof(guidServices)); +DWORD numServices = MAX_DEVICES; +memcpy(&deviceInfo.Address.rgBytes[0], radio_dev_address[selected_radio_dev], MAX_MAC_ADDR_LENGHT); + +if (ERROR_SUCCESS == BluetoothEnumerateInstalledServices(hRadio[selected_radio], +&deviceInfo, +&numServices, +guidServices)) +{ +printf("enumerated services: %d\n", numServices); +} +else +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +#endif + +#if 1 +if (ERROR_SUCCESS != BluetoothSetServiceState(hRadio[selected_radio], &deviceInfo, &pService, BLUETOOTH_SERVICE_DISABLE)) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +else +{ +printf("BluetoothSetServiceState disabled (COM)\n"); +} +#endif + +//function works +HBLUETOOTH_AUTHENTICATION_REGISTRATION phRegHandle; +if (ERROR_SUCCESS != BluetoothRegisterForAuthentication(&deviceInfo, +&phRegHandle, +Wiiscan::RegFun, // auth_callback, +hRadio[selected_radio])) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} + +WCHAR tempkey[12] = {0}; +PWCHAR pszPasskey = tempkey; +for (j = 0; j < 8; j+=2) +{ +((unsigned char*)pszPasskey)[j] = '0'; +} +for (j = 1; j < 8; j+=2) +{ +((unsigned char*)pszPasskey)[j] = 0x0; +} +ULONG ulPasskeyLength = 4; +wprintf(L"pszPasskey: %20s\n", pszPasskey); + +//works!!!! +result = BluetoothAuthenticateDevice(0, hRadio[selected_radio], &deviceInfo, pszPasskey, ulPasskeyLength); +if (result == ERROR_NO_MORE_ITEMS) +{ +printf("%s\n", "device already authenticated"); +} +else if(result != ERROR_SUCCESS) +{ +printf("%s\n", GetLastErrorMessage(GetLastError())); +} +CloseHandle( hRadio[selected_radio] ); +} + +/* +void Connect_using_SWA() +{ +int i; +//--- to open a socket ----- +SOCKET s; +WORD wVersionRequested = 0x202; +WSADATA m_data; + +WSAPROTOCOL_INFO protocolInfo; +int protocolInfoSize = sizeof(protocolInfo); + +char known_addresses[MAX_DEVICES][MAX_STRING_LENGHT]; +char known_names [MAX_DEVICES][MAX_STRING_LENGHT]; +char selected_addresses [MAX_DEVICES][MAX_STRING_LENGHT]; +char selected_names [MAX_DEVICES][MAX_STRING_LENGHT]; + +//BTH_DEVICE_INFO *pDeviceInfo; + +int found_dev_cnt = 0;; +int selected_dev; +int found_serv_cnt = 0; +int selected_serv; + +if (0 == WSAStartup(wVersionRequested, &m_data)) +{ +if (0 != BT_open_socket(&s, &protocolInfo, &protocolInfoSize)) +{ +exit(1); +} + +found_dev_cnt = BT_find_all_devices(known_addresses, known_names, &protocolInfo); +if (found_dev_cnt == 0) +{ +printf("no devices found\n"); +exit(1); +} + +for (i = 0; i < found_dev_cnt; i++) +{ +printf("[%2d]: %20s ", i, known_names[i]); +printf("addr: %s\n", known_addresses[i]); +} +printf("Select device: "); +scanf("%d", &selected_dev); +if (selected_dev < 0) selected_dev = 0; +if (selected_dev >= found_dev_cnt) selected_dev = found_dev_cnt - 1; + +found_serv_cnt = BT_find_devices_by_context(selected_addresses, selected_names, &protocolInfo, known_addresses[selected_dev]); +if (found_serv_cnt == 0) +{ +printf("no services found\n"); +exit(1); +} + +for (i = 0; i < found_serv_cnt; i++) +{ +printf("[%2d]: %20s ", i, selected_names[i]); +printf("addr: %s\n", selected_addresses[i]); +} +printf("Select service: "); +scanf("%d", &selected_serv); +if (selected_serv < 0) selected_serv = 0; +if (selected_serv >= found_serv_cnt) selected_serv = found_serv_cnt - 1; + +printf("selected: %s\n", selected_names[selected_serv]); + + +WSACleanup(); +} +} +*/ \ No newline at end of file diff --git a/cpp/wiiscan/wiiscan.vcproj b/cpp/wiiscan/wiiscan.vcproj new file mode 100644 index 0000000..76318e4 --- /dev/null +++ b/cpp/wiiscan/wiiscan.vcproj @@ -0,0 +1,243 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +