wiiuse
wiiuse_internal.h
Go to the documentation of this file.
1 /*
2  * wiiuse
3  *
4  * Written By:
5  * Michael Laforest < para >
6  * Email: < thepara (--AT--) g m a i l [--DOT--] com >
7  *
8  * Copyright 2006-2007
9  *
10  * This file is part of wiiuse.
11  *
12  * Mac device classes based on wiic_internal.h from WiiC, written By:
13  * Gabriele Randelli
14  * Email: < randelli (--AT--) dis [--DOT--] uniroma1 [--DOT--] it >
15  *
16  * Copyright 2010
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  *
31  * $Header$
32  *
33  */
34 
50 #ifndef WIIUSE_INTERNAL_H_INCLUDED
51 #define WIIUSE_INTERNAL_H_INCLUDED
52 
53 #ifndef WIIUSE_PLATFORM
54 #if defined(_WIN32)
55 #define WIIUSE_PLATFORM
56 #define WIIUSE_WIN32
57 #elif defined(__linux)
58 #define WIIUSE_PLATFORM
59 #define WIIUSE_BLUEZ
60 #elif defined(__APPLE__)
61 #define WIIUSE_PLATFORM
62 #define WIIUSE_MAC
63 #else
64 #error "Platform not yet supported!"
65 #endif
66 #endif
67 
68 #ifdef WIIUSE_WIN32
69 #include <winsock2.h>
70 #endif
71 #ifdef WIIUSE_BLUEZ
72 #include <arpa/inet.h> /* htons() */
73 #include <bluetooth/bluetooth.h>
74 #endif
75 #ifdef WIIUSE_BT_EMBEDDED
76 #include <arpa/inet.h> /* htons() */
77 #include <bt-embedded/l2cap.h>
78 #endif
79 #ifdef WIIUSE_MAC
80 /* mac */
81 #include <CoreFoundation/CoreFoundation.h> /*CFRunLoops and CFNumberRef in Bluetooth classes*/
82 #include <IOBluetooth/IOBluetoothUserLib.h> /*IOBluetoothDeviceRef and IOBluetoothL2CAPChannelRef*/
83 #endif
84 
85 #include "definitions.h"
86 
87 #if defined(_MSC_VER) && _MSC_VER < 1700
88 /* MS compilers of pre-VC2010 versions don't have stdint.h
89  * and I can't get VC2010's stdint.h to compile nicely in
90  * WiiUse
91  */
92 #include "wiiuse_msvcstdint.h"
93 #else
94 #include <stdint.h>
95 #endif
96 
97 /********************
98  *
99  * Wiimote internal codes
100  *
101  ********************/
102 
105 /* Communication channels */
106 #define WM_OUTPUT_CHANNEL 0x11
107 #define WM_INPUT_CHANNEL 0x13
108 
109 #define WM_SET_REPORT 0x50
110 #define WM_SET_DATA 0xA0
111 
112 /* commands */
113 #define WM_CMD_LED 0x11
114 #define WM_CMD_REPORT_TYPE 0x12
115 #define WM_CMD_RUMBLE 0x13
116 #define WM_CMD_IR 0x13
117 #define WM_CMD_CTRL_STATUS 0x15
118 #define WM_CMD_WRITE_DATA 0x16
119 #define WM_CMD_READ_DATA 0x17
120 #define WM_CMD_IR_2 0x1A
121 
122 /* input report ids */
123 #define WM_RPT_CTRL_STATUS 0x20
124 #define WM_RPT_READ 0x21
125 #define WM_RPT_WRITE 0x22
126 #define WM_RPT_BTN 0x30
127 #define WM_RPT_BTN_ACC 0x31
128 #define WM_RPT_BTN_EXP_8 0x32
129 #define WM_RPT_BTN_ACC_IR 0x33
130 #define WM_RPT_BTN_EXP 0x34
131 #define WM_RPT_BTN_ACC_EXP 0x35
132 #define WM_RPT_BTN_IR_EXP 0x36
133 #define WM_RPT_BTN_ACC_IR_EXP 0x37
134 
135 #define WM_BT_INPUT 0x01
136 #define WM_BT_OUTPUT 0x02
137 
138 /* Identify the wiimote device by its class */
139 
140 /* (Explanation and mac classes taken from WiiC)
141  * The different codes wrt. to Linux
142  * is a bit hard to explain.
143  * Looking at Bluetooth CoD format, we have 24 bits.
144  * In wiic Linux they are stored in three fields,
145  * each one 8bit long. The result number is
146  * 0x002504. However, MacOS Bluetooth does
147  * not store them in such a way, rather it uses
148  * the concept of major service, major class,
149  * and minor class, that are respectivelly
150  * 11bit, 5bit, and 6bit long. Hence, the
151  * numbers are different.
152  * The Wiimote CoD Bluetooth division is the following:
153  * 00000000001 00101 000001 00 (major service - major class - minor class - format type)
154  * This can also be seen in the WiiC Linux way:
155  * 00000000 00100101 00000100
156  */
157 #ifdef WIIUSE_MAC
158 #define WM_DEV_MINOR_CLASS 0x01 // Regular wiimote
159 #define WM_DEV_MAJOR_CLASS 0x05
160 #define WM_DEV_MAJOR_SERVICE 0x01
161 
162 #define WM_PLUS_DEV_MINOR_CLASS 0x02 // For the newer RVL-CNT-01-TR (MotionPlus Inside)
163 #define WM_PLUS_DEV_MAJOR_CLASS 0x05
164 #define WM_PLUS_DEV_MAJOR_SERVICE 0x00
165 #else
166 #define WM_DEV_CLASS_0 0x04 // Regular wiimote
167 #define WM_DEV_CLASS_1 0x25
168 #define WM_DEV_CLASS_2 0x00
169 
170 #define WM_PLUS_DEV_CLASS_0 0x08 // For the newer RVL-CNT-01-TR (MotionPlus Inside)
171 #define WM_PLUS_DEV_CLASS_1 0x05
172 #define WM_PLUS_DEV_CLASS_2 0x00
173 #endif
174 #define WM_VENDOR_ID 0x057E
175 #define WM_PRODUCT_ID 0x0306 // Regular wiimote
176 #define WM_PRODUCT_ID_TR 0x0330 // RVL-CNT-01-TR Wiimote (with MotionPlus built-in)
177 
178 /* controller status stuff */
179 #define WM_MAX_BATTERY_CODE 0xC8
180 
181 /* offsets in wiimote memory */
182 #define WM_MEM_OFFSET_CALIBRATION 0x16
183 #define WM_EXP_MEM_BASE 0x04A40000
184 #define WM_EXP_ID 0x04A400FA
185 #define WM_EXP_MEM_ENABLE 0x04A40040
186 #define WM_EXP_MEM_ENABLE1 0x04A400F0
187 #define WM_EXP_MEM_ENABLE2 0x04A400FB
188 #define WM_EXP_MEM_CALIBR 0x04A40020
189 #define WM_EXP_MOTION_PLUS_IDENT 0x04A600FA
190 #define WM_EXP_MOTION_PLUS_ENABLE 0x04A600FE
191 #define WM_EXP_MOTION_PLUS_INIT 0x04A600F0
192 #define WM_REG_IR 0x04B00030
193 #define WM_REG_IR_BLOCK1 0x04B00000
194 #define WM_REG_IR_BLOCK2 0x04B0001A
195 #define WM_REG_IR_MODENUM 0x04B00033
196 
197 /* unknown Wii Balance Board offsets used for init */
198 #define WM_EXP_BBOARD_INIT1 0x04A400F1
199 
200 #define WM_IR_TYPE_BASIC 0x01
201 #define WM_IR_TYPE_EXTENDED 0x03
202 
203 /* controller status flags for the first message byte */
204 /* bit 1 is unknown */
205 #define WM_CTRL_STATUS_BYTE1_ATTACHMENT 0x02
206 #define WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED 0x04
207 #define WM_CTRL_STATUS_BYTE1_IR_ENABLED 0x08
208 #define WM_CTRL_STATUS_BYTE1_LED_1 0x10
209 #define WM_CTRL_STATUS_BYTE1_LED_2 0x20
210 #define WM_CTRL_STATUS_BYTE1_LED_3 0x40
211 #define WM_CTRL_STATUS_BYTE1_LED_4 0x80
212 
213 /* aspect ratio */
214 #define WM_ASPECT_16_9_X 660
215 #define WM_ASPECT_16_9_Y 370
216 #define WM_ASPECT_4_3_X 560
217 #define WM_ASPECT_4_3_Y 420
218 
223 /* decrypted expansion id codes (located at 0x04A400FC) */
224 #define EXP_ID_CODE_NUNCHUK 0xA4200000
225 #define EXP_ID_CODE_WII_BOARD 0xA4200402
226 #define EXP_ID_CODE_CLASSIC_CONTROLLER 0xA4200101
227 #define EXP_ID_CODE_GUITAR 0xA4200103
228 #define EXP_ID_CODE_MOTION_PLUS 0xA4200405
229 #define EXP_ID_CODE_MOTION_PLUS_NUNCHUK 0xA4200505
230 #define EXP_ID_CODE_MOTION_PLUS_CLASSIC 0xA4200705
231 #define EXP_ID_CODE_TATACON 0xA4200111
232 
233 /* decrypted M+ codes at 0x04A600FA */
234 #define EXP_ID_CODE_INACTIVE_MOTION_PLUS 0xA6200005
235 #define EXP_ID_CODE_INACTIVE_MOTION_PLUS_BUILTIN 0xA4200005
237 #define EXP_ID_CODE_NLA_MOTION_PLUS 0xA6200405
238 #define EXP_ID_CODE_NLA_MOTION_PLUS_NUNCHUK 0xA6200505
239 #define EXP_ID_CODE_NLA_MOTION_PLUS_CLASSIC 0xA6200705
241 #define EXP_HANDSHAKE_LEN 224
242 
243 /********************
244  *
245  * End Wiimote internal codes
246  *
247  ********************/
248 
249 #define WIIMOTE_INIT_STATES (WIIMOTE_STATE_IR_SENS_LVL3)
250 
251 /* macro to manage states */
252 #define WIIMOTE_ENABLE_STATE(wm, s) (wm->state |= (s))
253 #define WIIMOTE_DISABLE_STATE(wm, s) (wm->state &= ~(s))
254 #define WIIMOTE_TOGGLE_STATE(wm, s) \
255  ((wm->state & (s)) ? WIIMOTE_DISABLE_STATE(wm, s) : WIIMOTE_ENABLE_STATE(wm, s))
256 
257 #define WIIMOTE_IS_FLAG_SET(wm, s) ((wm->flags & (s)) == (s))
258 #define WIIMOTE_ENABLE_FLAG(wm, s) (wm->flags |= (s))
259 #define WIIMOTE_DISABLE_FLAG(wm, s) (wm->flags &= ~(s))
260 #define WIIMOTE_TOGGLE_FLAG(wm, s) \
261  ((wm->flags & (s)) ? WIIMOTE_DISABLE_FLAG(wm, s) : WIIMOTE_ENABLE_FLAG(wm, s))
262 
263 #define NUNCHUK_IS_FLAG_SET(wm, s) ((*(wm->flags) & (s)) == (s))
264 
265 /*
266  * Largest known payload is 21 bytes.
267  * Add 1 (Win32) or 2 (Mac, *nix) for the prefix and round up to a power of 2.
268  */
269 #define MAX_PAYLOAD 32
270 
271 /*
272  * Smooth tilt calculations are computed with the
273  * exponential moving average formula:
274  * St = St_last + (alpha * (tilt - St_last))
275  * alpha is between 0 and 1
276  */
277 #define WIIUSE_DEFAULT_SMOOTH_ALPHA 0.07f
278 
279 #define SMOOTH_ROLL 0x01
280 #define SMOOTH_PITCH 0x02
281 
282 #define WIIUSE_READ_TIMEOUT 5000
283 
285 #include "wiiuse.h"
288 #define _STRINGIFY(s) _STRINGIFY_IMPL(s)
289 #define _STRINGIFY_IMPL(s) #s
290 
291 /* wiiuse version, from public per-component version defines */
292 #define WIIUSE_VERSION _STRINGIFY(WIIUSE_MAJOR) "." _STRINGIFY(WIIUSE_MINOR) "." _STRINGIFY(WIIUSE_MICRO)
293 
294 #ifdef _MSC_VER
295 #define INLINE_UTIL __inline
296 #else
297 #define INLINE_UTIL static inline
298 #endif
299 
300 #ifdef __cplusplus
301 extern "C" {
302 #endif
303 
304 /* not part of the api */
305 
312 void wiiuse_millisleep(int durationMilliseconds);
313 
314 int wiiuse_set_report_type(struct wiimote_t *wm);
317 int wiiuse_send(struct wiimote_t *wm, byte report_type, byte *msg, int len);
318 int wiiuse_read_data_cb(struct wiimote_t *wm, wiiuse_read_cb read_cb, byte *buffer, unsigned int offset,
319  uint16_t len);
320 int wiiuse_write_data_cb(struct wiimote_t *wm, unsigned int addr, byte *data, byte len,
321  wiiuse_write_cb write_cb);
322 
323 #ifdef WIIUSE_DOXYGEN_PARSING
324 
329 uint8_t from_big_endian_uint8_t(byte *buf);
336 uint16_t from_big_endian_uint16_t(byte *buf);
337 
344 uint32_t from_big_endian_uint32_t(byte *buf);
355 void to_big_endian_uint8_t(byte *buf, uint8_t val);
356 
363 void to_big_endian_uint16_t(byte *buf, uint16_t val);
364 
371 void to_big_endian_uint32_t(byte *buf, uint32_t val);
384 void buffer_big_endian_uint16_t(byte **buf, uint16_t val);
385 
390 uint16_t unbuffer_big_endian_uint16_t(byte **buf);
391 
394 void buffer_big_endian_uint8_t(byte **buf, uint8_t val);
395 
398 uint8_t unbuffer_big_endian_uint8_t(byte **buf);
399 
402 void buffer_big_endian_uint32_t(byte **buf, uint32_t val);
403 
406 uint8_t unbuffer_big_endian_uint32_t(byte **buf)
407 
408 
409 #else /* this else is true when not in doxygen */
410 
411 INLINE_UTIL void to_big_endian_uint8_t(byte *buf, uint8_t val) { memcpy(buf, &val, 1); }
412 
413 INLINE_UTIL uint8_t from_big_endian_uint8_t(byte *buf)
414 {
415  uint8_t beVal;
416  memcpy(&beVal, buf, 1);
417  return beVal;
418 }
419 
420 #define WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(_TYPE, _TOBE, _FROMBE) \
421  INLINE_UTIL void to_big_endian_##_TYPE(byte *buf, _TYPE val) \
422  { \
423  _TYPE beVal = _TOBE(val); \
424  memcpy(buf, &beVal, sizeof(_TYPE)); \
425  } \
426  INLINE_UTIL _TYPE from_big_endian_##_TYPE(byte *buf) \
427  { \
428  _TYPE beVal; \
429  memcpy(&beVal, buf, sizeof(_TYPE)); \
430  return _FROMBE(beVal); \
431  }
432 
433 WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint16_t, htons, ntohs)
434 WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint32_t, htonl, ntohl)
435 
436 #undef WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS
437 
438 #define WIIUSE_DECLARE_BUFFERING_OPS(_TYPE) \
439  INLINE_UTIL void buffer_big_endian_##_TYPE(byte **buf, _TYPE val) \
440  { \
441  to_big_endian_##_TYPE(*buf, val); \
442  *buf += sizeof(_TYPE); \
443  } \
444  INLINE_UTIL _TYPE unbuffer_big_endian_##_TYPE(byte **buf) \
445  { \
446  byte *current = *buf; \
447  *buf += sizeof(_TYPE); \
448  return from_big_endian_##_TYPE(current); \
449  }
450 
451 WIIUSE_DECLARE_BUFFERING_OPS(uint8_t)
452 WIIUSE_DECLARE_BUFFERING_OPS(uint16_t)
453 WIIUSE_DECLARE_BUFFERING_OPS(uint32_t)
454 
455 #undef WIIUSE_DECLARE_BUFFERING_OPS
456 
457 #endif /* not in doxygen */
458 
459 #ifdef __cplusplus
460 }
461 #endif
462 
464 #endif /* WIIUSE_INTERNAL_H_INCLUDED */
Main Wiimote device structure.
Definition: wiiuse.h:793
API header file.
int wiiuse_write_data_cb(struct wiimote_t *wm, unsigned int addr, byte *data, byte len, wiiuse_write_cb write_cb)
Write data to the wiimote (callback version).
Definition: wiiuse.c:654
void wiiuse_send_next_pending_read_request(struct wiimote_t *wm)
Send the next pending data read request to the wiimote.
Definition: wiiuse.c:494
void wiiuse_send_next_pending_write_request(struct wiimote_t *wm)
Send the next pending data write request to the wiimote.
Definition: wiiuse.c:710
int wiiuse_send(struct wiimote_t *wm, byte report_type, byte *msg, int len)
Send a packet to the wiimote.
Definition: wiiuse.c:749
void(* wiiuse_read_cb)(struct wiimote_t *wm, byte *data, uint16_t len)
Callback that handles a read event.
Definition: wiiuse.h:364
int wiiuse_read_data_cb(struct wiimote_t *wm, wiiuse_read_cb read_cb, byte *buffer, unsigned int offset, uint16_t len)
Read data from the wiimote (callback version).
Definition: wiiuse.c:411
General definitions.
void wiiuse_millisleep(int durationMilliseconds)
Cross-platform call to sleep for at least the specified number of milliseconds.
Definition: util.c:44
void(* wiiuse_write_cb)(struct wiimote_t *wm, unsigned char *data, unsigned short len)
Callback that handles a write event.
Definition: wiiuse.h:906
int wiiuse_set_report_type(struct wiimote_t *wm)
Set the report type based on the current wiimote state.
Definition: wiiuse.c:325