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_MAC
76 /* mac */
77 #include <CoreFoundation/CoreFoundation.h> /*CFRunLoops and CFNumberRef in Bluetooth classes*/
78 #include <IOBluetooth/IOBluetoothUserLib.h> /*IOBluetoothDeviceRef and IOBluetoothL2CAPChannelRef*/
79 #endif
80 
81 #include "definitions.h"
82 
83 #if defined(_MSC_VER) && _MSC_VER < 1700
84 /* MS compilers of pre-VC2010 versions don't have stdint.h
85  * and I can't get VC2010's stdint.h to compile nicely in
86  * WiiUse
87  */
88 #include "wiiuse_msvcstdint.h"
89 #else
90 #include <stdint.h>
91 #endif
92 
93 /********************
94  *
95  * Wiimote internal codes
96  *
97  ********************/
98 
101 /* Communication channels */
102 #define WM_OUTPUT_CHANNEL 0x11
103 #define WM_INPUT_CHANNEL 0x13
104 
105 #define WM_SET_REPORT 0x50
106 #define WM_SET_DATA 0xA0
107 
108 /* commands */
109 #define WM_CMD_LED 0x11
110 #define WM_CMD_REPORT_TYPE 0x12
111 #define WM_CMD_RUMBLE 0x13
112 #define WM_CMD_IR 0x13
113 #define WM_CMD_CTRL_STATUS 0x15
114 #define WM_CMD_WRITE_DATA 0x16
115 #define WM_CMD_READ_DATA 0x17
116 #define WM_CMD_IR_2 0x1A
117 
118 /* input report ids */
119 #define WM_RPT_CTRL_STATUS 0x20
120 #define WM_RPT_READ 0x21
121 #define WM_RPT_WRITE 0x22
122 #define WM_RPT_BTN 0x30
123 #define WM_RPT_BTN_ACC 0x31
124 #define WM_RPT_BTN_EXP_8 0x32
125 #define WM_RPT_BTN_ACC_IR 0x33
126 #define WM_RPT_BTN_EXP 0x34
127 #define WM_RPT_BTN_ACC_EXP 0x35
128 #define WM_RPT_BTN_IR_EXP 0x36
129 #define WM_RPT_BTN_ACC_IR_EXP 0x37
130 
131 #define WM_BT_INPUT 0x01
132 #define WM_BT_OUTPUT 0x02
133 
134 /* Identify the wiimote device by its class */
135 
136 /* (Explanation and mac classes taken from WiiC)
137  * The different codes wrt. to Linux
138  * is a bit hard to explain.
139  * Looking at Bluetooth CoD format, we have 24 bits.
140  * In wiic Linux they are stored in three fields,
141  * each one 8bit long. The result number is
142  * 0x002504. However, MacOS Bluetooth does
143  * not store them in such a way, rather it uses
144  * the concept of major service, major class,
145  * and minor class, that are respectivelly
146  * 11bit, 5bit, and 6bit long. Hence, the
147  * numbers are different.
148  * The Wiimote CoD Bluetooth division is the following:
149  * 00000000001 00101 000001 00 (major service - major class - minor class - format type)
150  * This can also be seen in the WiiC Linux way:
151  * 00000000 00100101 00000100
152  */
153 #ifdef WIIUSE_MAC
154 #define WM_DEV_MINOR_CLASS 0x01 // Regular wiimote
155 #define WM_DEV_MAJOR_CLASS 0x05
156 #define WM_DEV_MAJOR_SERVICE 0x01
157 
158 #define WM_PLUS_DEV_MINOR_CLASS 0x02 // For the newer RVL-CNT-01-TR (MotionPlus Inside)
159 #define WM_PLUS_DEV_MAJOR_CLASS 0x05
160 #define WM_PLUS_DEV_MAJOR_SERVICE 0x00
161 #else
162 #define WM_DEV_CLASS_0 0x04 // Regular wiimote
163 #define WM_DEV_CLASS_1 0x25
164 #define WM_DEV_CLASS_2 0x00
165 
166 #define WM_PLUS_DEV_CLASS_0 0x08 // For the newer RVL-CNT-01-TR (MotionPlus Inside)
167 #define WM_PLUS_DEV_CLASS_1 0x05
168 #define WM_PLUS_DEV_CLASS_2 0x00
169 #endif
170 #define WM_VENDOR_ID 0x057E
171 #define WM_PRODUCT_ID 0x0306 // Regular wiimote
172 #define WM_PRODUCT_ID_TR 0x0330 // RVL-CNT-01-TR Wiimote (with MotionPlus built-in)
173 
174 /* controller status stuff */
175 #define WM_MAX_BATTERY_CODE 0xC8
176 
177 /* offsets in wiimote memory */
178 #define WM_MEM_OFFSET_CALIBRATION 0x16
179 #define WM_EXP_MEM_BASE 0x04A40000
180 #define WM_EXP_ID 0x04A400FA
181 #define WM_EXP_MEM_ENABLE 0x04A40040
182 #define WM_EXP_MEM_ENABLE1 0x04A400F0
183 #define WM_EXP_MEM_ENABLE2 0x04A400FB
184 #define WM_EXP_MEM_CALIBR 0x04A40020
185 #define WM_EXP_MOTION_PLUS_IDENT 0x04A600FA
186 #define WM_EXP_MOTION_PLUS_ENABLE 0x04A600FE
187 #define WM_EXP_MOTION_PLUS_INIT 0x04A600F0
188 #define WM_REG_IR 0x04B00030
189 #define WM_REG_IR_BLOCK1 0x04B00000
190 #define WM_REG_IR_BLOCK2 0x04B0001A
191 #define WM_REG_IR_MODENUM 0x04B00033
192 
193 /* unknown Wii Balance Board offsets used for init */
194 #define WM_EXP_BBOARD_INIT1 0x04A400F1
195 
196 #define WM_IR_TYPE_BASIC 0x01
197 #define WM_IR_TYPE_EXTENDED 0x03
198 
199 /* controller status flags for the first message byte */
200 /* bit 1 is unknown */
201 #define WM_CTRL_STATUS_BYTE1_ATTACHMENT 0x02
202 #define WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED 0x04
203 #define WM_CTRL_STATUS_BYTE1_IR_ENABLED 0x08
204 #define WM_CTRL_STATUS_BYTE1_LED_1 0x10
205 #define WM_CTRL_STATUS_BYTE1_LED_2 0x20
206 #define WM_CTRL_STATUS_BYTE1_LED_3 0x40
207 #define WM_CTRL_STATUS_BYTE1_LED_4 0x80
208 
209 /* aspect ratio */
210 #define WM_ASPECT_16_9_X 660
211 #define WM_ASPECT_16_9_Y 370
212 #define WM_ASPECT_4_3_X 560
213 #define WM_ASPECT_4_3_Y 420
214 
219 /* decrypted expansion id codes (located at 0x04A400FC) */
220 #define EXP_ID_CODE_NUNCHUK 0xA4200000
221 #define EXP_ID_CODE_WII_BOARD 0xA4200402
222 #define EXP_ID_CODE_CLASSIC_CONTROLLER 0xA4200101
223 #define EXP_ID_CODE_GUITAR 0xA4200103
224 #define EXP_ID_CODE_MOTION_PLUS 0xA4200405
225 #define EXP_ID_CODE_MOTION_PLUS_NUNCHUK 0xA4200505
226 #define EXP_ID_CODE_MOTION_PLUS_CLASSIC 0xA4200705
227 #define EXP_ID_CODE_TATACON 0xA4200111
228 
229 /* decrypted M+ codes at 0x04A600FA */
230 #define EXP_ID_CODE_INACTIVE_MOTION_PLUS 0xA6200005
231 #define EXP_ID_CODE_INACTIVE_MOTION_PLUS_BUILTIN 0xA4200005
233 #define EXP_ID_CODE_NLA_MOTION_PLUS 0xA6200405
234 #define EXP_ID_CODE_NLA_MOTION_PLUS_NUNCHUK 0xA6200505
235 #define EXP_ID_CODE_NLA_MOTION_PLUS_CLASSIC 0xA6200705
237 #define EXP_HANDSHAKE_LEN 224
238 
239 /********************
240  *
241  * End Wiimote internal codes
242  *
243  ********************/
244 
245 #define WIIMOTE_INIT_STATES (WIIMOTE_STATE_IR_SENS_LVL3)
246 
247 /* macro to manage states */
248 #define WIIMOTE_ENABLE_STATE(wm, s) (wm->state |= (s))
249 #define WIIMOTE_DISABLE_STATE(wm, s) (wm->state &= ~(s))
250 #define WIIMOTE_TOGGLE_STATE(wm, s) \
251  ((wm->state & (s)) ? WIIMOTE_DISABLE_STATE(wm, s) : WIIMOTE_ENABLE_STATE(wm, s))
252 
253 #define WIIMOTE_IS_FLAG_SET(wm, s) ((wm->flags & (s)) == (s))
254 #define WIIMOTE_ENABLE_FLAG(wm, s) (wm->flags |= (s))
255 #define WIIMOTE_DISABLE_FLAG(wm, s) (wm->flags &= ~(s))
256 #define WIIMOTE_TOGGLE_FLAG(wm, s) \
257  ((wm->flags & (s)) ? WIIMOTE_DISABLE_FLAG(wm, s) : WIIMOTE_ENABLE_FLAG(wm, s))
258 
259 #define NUNCHUK_IS_FLAG_SET(wm, s) ((*(wm->flags) & (s)) == (s))
260 
261 /*
262  * Largest known payload is 21 bytes.
263  * Add 1 (Win32) or 2 (Mac, *nix) for the prefix and round up to a power of 2.
264  */
265 #define MAX_PAYLOAD 32
266 
267 /*
268  * Smooth tilt calculations are computed with the
269  * exponential moving average formula:
270  * St = St_last + (alpha * (tilt - St_last))
271  * alpha is between 0 and 1
272  */
273 #define WIIUSE_DEFAULT_SMOOTH_ALPHA 0.07f
274 
275 #define SMOOTH_ROLL 0x01
276 #define SMOOTH_PITCH 0x02
277 
278 #define WIIUSE_READ_TIMEOUT 5000
279 
281 #include "wiiuse.h"
284 #define _STRINGIFY(s) _STRINGIFY_IMPL(s)
285 #define _STRINGIFY_IMPL(s) #s
286 
287 /* wiiuse version, from public per-component version defines */
288 #define WIIUSE_VERSION _STRINGIFY(WIIUSE_MAJOR) "." _STRINGIFY(WIIUSE_MINOR) "." _STRINGIFY(WIIUSE_MICRO)
289 
290 #ifdef _MSC_VER
291 #define INLINE_UTIL __inline
292 #else
293 #define INLINE_UTIL static inline
294 #endif
295 
296 #ifdef __cplusplus
297 extern "C" {
298 #endif
299 
300 /* not part of the api */
301 
308 void wiiuse_millisleep(int durationMilliseconds);
309 
310 int wiiuse_set_report_type(struct wiimote_t *wm);
313 int wiiuse_send(struct wiimote_t *wm, byte report_type, byte *msg, int len);
314 int wiiuse_read_data_cb(struct wiimote_t *wm, wiiuse_read_cb read_cb, byte *buffer, unsigned int offset,
315  uint16_t len);
316 int wiiuse_write_data_cb(struct wiimote_t *wm, unsigned int addr, byte *data, byte len,
317  wiiuse_write_cb write_cb);
318 
319 #ifdef WIIUSE_DOXYGEN_PARSING
320 
325 uint8_t from_big_endian_uint8_t(byte *buf);
332 uint16_t from_big_endian_uint16_t(byte *buf);
333 
340 uint32_t from_big_endian_uint32_t(byte *buf);
351 void to_big_endian_uint8_t(byte *buf, uint8_t val);
352 
359 void to_big_endian_uint16_t(byte *buf, uint16_t val);
360 
367 void to_big_endian_uint32_t(byte *buf, uint32_t val);
380 void buffer_big_endian_uint16_t(byte **buf, uint16_t val);
381 
386 uint16_t unbuffer_big_endian_uint16_t(byte **buf);
387 
390 void buffer_big_endian_uint8_t(byte **buf, uint8_t val);
391 
394 uint8_t unbuffer_big_endian_uint8_t(byte **buf);
395 
398 void buffer_big_endian_uint32_t(byte **buf, uint32_t val);
399 
402 uint8_t unbuffer_big_endian_uint32_t(byte **buf)
403 
404 
405 #else /* this else is true when not in doxygen */
406 
407 INLINE_UTIL void to_big_endian_uint8_t(byte *buf, uint8_t val) { memcpy(buf, &val, 1); }
408 
409 INLINE_UTIL uint8_t from_big_endian_uint8_t(byte *buf)
410 {
411  uint8_t beVal;
412  memcpy(&beVal, buf, 1);
413  return beVal;
414 }
415 
416 #define WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(_TYPE, _TOBE, _FROMBE) \
417  INLINE_UTIL void to_big_endian_##_TYPE(byte *buf, _TYPE val) \
418  { \
419  _TYPE beVal = _TOBE(val); \
420  memcpy(buf, &beVal, sizeof(_TYPE)); \
421  } \
422  INLINE_UTIL _TYPE from_big_endian_##_TYPE(byte *buf) \
423  { \
424  _TYPE beVal; \
425  memcpy(&beVal, buf, sizeof(_TYPE)); \
426  return _FROMBE(beVal); \
427  }
428 
429 WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint16_t, htons, ntohs)
430 WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint32_t, htonl, ntohl)
431 
432 #undef WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS
433 
434 #define WIIUSE_DECLARE_BUFFERING_OPS(_TYPE) \
435  INLINE_UTIL void buffer_big_endian_##_TYPE(byte **buf, _TYPE val) \
436  { \
437  to_big_endian_##_TYPE(*buf, val); \
438  *buf += sizeof(_TYPE); \
439  } \
440  INLINE_UTIL _TYPE unbuffer_big_endian_##_TYPE(byte **buf) \
441  { \
442  byte *current = *buf; \
443  *buf += sizeof(_TYPE); \
444  return from_big_endian_##_TYPE(current); \
445  }
446 
447 WIIUSE_DECLARE_BUFFERING_OPS(uint8_t)
448 WIIUSE_DECLARE_BUFFERING_OPS(uint16_t)
449 WIIUSE_DECLARE_BUFFERING_OPS(uint32_t)
450 
451 #undef WIIUSE_DECLARE_BUFFERING_OPS
452 
453 #endif /* not in doxygen */
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
460 #endif /* WIIUSE_INTERNAL_H_INCLUDED */
Main Wiimote device structure.
Definition: wiiuse.h:751
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:649
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:705
int wiiuse_send(struct wiimote_t *wm, byte report_type, byte *msg, int len)
Send a packet to the wiimote.
Definition: wiiuse.c:744
void(* wiiuse_read_cb)(struct wiimote_t *wm, byte *data, uint16_t len)
Callback that handles a read event.
Definition: wiiuse.h:358
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:42
void(* wiiuse_write_cb)(struct wiimote_t *wm, unsigned char *data, unsigned short len)
Callback that handles a write event.
Definition: wiiuse.h:854
int wiiuse_set_report_type(struct wiimote_t *wm)
Set the report type based on the current wiimote state.
Definition: wiiuse.c:325