Initialize Tizen 2.3
[framework/connectivity/bluez.git] / mobile / plugins / wiimote.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2011  David Herrmann <dh.herrmann@googlemail.com>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <bluetooth/bluetooth.h>
29 #include <glib.h>
30
31 #include "plugin.h"
32 #include "adapter.h"
33 #include "device.h"
34 #include "log.h"
35 #include "storage.h"
36
37 /*
38  * Nintendo Wii Remote devices require the bdaddr of the host as pin input for
39  * authentication. This plugin registers a pin-callback and forces this pin
40  * to be used for authentication.
41  *
42  * There are two ways to place the wiimote into discoverable mode.
43  *  - Pressing the red-sync button on the back of the wiimote. This module
44  *    supports pairing via this method. Auto-reconnect should be possible after
45  *    the device was paired once.
46  *  - Pressing the 1+2 buttons on the front of the wiimote. This module does
47  *    not support this method since this method never enables auto-reconnect.
48  *    Hence, pairing is not needed. Use it without pairing if you want.
49  * After connecting the wiimote you should immediately connect to the input
50  * service of the wiimote. If you don't, the wiimote will close the connection.
51  * The wiimote waits about 5 seconds until it turns off again.
52  * Auto-reconnect is only enabled when pairing with the wiimote via the red
53  * sync-button and then connecting to the input service. If you do not connect
54  * to the input service, then auto-reconnect is not enabled.
55  * If enabled, the wiimote connects to the host automatically when any button
56  * is pressed.
57  */
58
59 static ssize_t wii_pincb(struct btd_adapter *adapter, struct btd_device *device,
60                                                 char *pinbuf, gboolean *display)
61 {
62         uint16_t vendor, product;
63         bdaddr_t sba, dba;
64         char addr[18], name[25];
65
66         adapter_get_address(adapter, &sba);
67         device_get_address(device, &dba, NULL);
68         ba2str(&dba, addr);
69
70         vendor = btd_device_get_vendor(device);
71         product = btd_device_get_product(device);
72
73         device_get_name(device, name, sizeof(name));
74         name[sizeof(name) - 1] = 0;
75
76         if (g_str_equal(name, "Nintendo RVL-CNT-01") ||
77                                 (vendor == 0x057e && product == 0x0306)) {
78                 DBG("Forcing fixed pin on detected wiimote %s", addr);
79                 memcpy(pinbuf, &sba, 6);
80                 return 6;
81         }
82
83         return 0;
84 }
85
86 static int wii_probe(struct btd_adapter *adapter)
87 {
88         btd_adapter_register_pin_cb(adapter, wii_pincb);
89
90         return 0;
91 }
92
93 static void wii_remove(struct btd_adapter *adapter)
94 {
95         btd_adapter_unregister_pin_cb(adapter, wii_pincb);
96 }
97
98 static struct btd_adapter_driver wii_driver = {
99         .name   = "wiimote",
100         .probe  = wii_probe,
101         .remove = wii_remove,
102 };
103
104 static int wii_init(void)
105 {
106         return btd_register_adapter_driver(&wii_driver);
107 }
108
109 static void wii_exit(void)
110 {
111         btd_unregister_adapter_driver(&wii_driver);
112 }
113
114 BLUETOOTH_PLUGIN_DEFINE(wiimote, VERSION,
115                 BLUETOOTH_PLUGIN_PRIORITY_LOW, wii_init, wii_exit)